diff --git a/README.md b/README.md index b699443..9feaa49 100644 --- a/README.md +++ b/README.md @@ -3,53 +3,87 @@ ReverseManufacturing.jl **ReverseManufacturing.jl** is an optimization package for logistic decisions related to reverse manufacturing processes. For example, the package can be used to determine where to build recycling plants, what sizes should they have and which customers should be served by which plants. The package supports customized reverse manufacturing pipelines, with multiple types of plants, multiple types of product and multiple time periods. +Table of Contents +================= -Data Specification -================== + * [ReverseManufacturing.jl](#reversemanufacturingjl) + * [Installation](#installation) + * [Typical Usage](#typical-usage) + * [Describing an instance](#describing-an-instance) + * [Products](#products) + * [Processing Plants](#processing-plants) + * [Optimizing](#optimizing) + * [Current Limitations](#current-limitations) + * [Authors](#authors) -Each instance in ReverseManufacturing.jl is represented as a JSON file with two sections: `products` and `plants`. Below, we describe each section in more detail. For a concrete example, see the file `instances/samples/s2.json`. +Installation +------------ +The package was developed and tested with Julia 1.3 and may not be compatible with newer versions. To install it, launch the Julia console, type `]` to switch to package manager mode and run: -Products --------- +``` +pkg> add git@github.com:iSoron/ReverseManufacturing.git +``` + +To make sure that the package has been correctly installed + +``` +pkg> test ReverseManufacturing +``` + +Typical Usage +------------- + +### Describing an instance + +The first step when using ReverseManufacturing.jl is describing the reverse manufacturing pipeline and the relevant data. Each input file is a JSON file with two sections: `products` and `plants`. Below, we describe each section in more detail. For a concrete example, see the file `instances/samples/s2.json`. + +#### Products The **products** section describes all products and subproducts in the simulation. The field `instance["products"]` is a dictionary mapping the name of the product to a dictionary which describes its characteristics. Each product description contains the following keys: -| Key | Description -|:------------------------------|:----------------------------------- -| `transportation cost` | The cost (in dollars per km) to transport this product -| `initial amounts` | A dictionary mapping the name of each location to its description. See below for more information. If this product is not initially available, this key may be omitted. +* `transportation cost`, the cost (in dollars per km) to transport this product. +* `initial amounts,` a dictionary mapping the name of each location to its description (see below). If this product is not initially available, this key may be omitted. Each product may have some amount available at the beginning of the simulation. In this case, the key `initial amounts` maps to a dictionary with the following keys: -| Key | Description -|:------------------------------|:----------------------------------- -| `latitude` | The latitude of the location, in degrees. -| `longitude` | The longitude of the location, in degrees. -| `amount` | The amount (in kg) of the product initially available at the location. +* `latitude`, the latitude of the location, in degrees. +* `longitude`, the longitude of the location, in degrees. +* `amount`, the amount (in kg) of the product initially available at the location. -Processing Plants ------------------ +#### Processing Plants The **plants** section describes the available types of reverse manufacturing plants, their potential locations and associated costs, as well as their inputs and outputs. The field `instance["plants"]` is a dictionary mapping the name of the plant to a dictionary with the following keys: -| Key | Description -|:------------------------------|:----------------------------------- -| `input` | The name of the product that this plant takes as input. Only one input is accepted per plant. -| `outputs` | A dictionary specifying how many kg of each product is produced for each kg of input. For example, if the plant outputs 0.5 kg of P2 and 0.25 kg of P3 for each kg of P1 provided, then this entry should be `{"P2": 0.5, "P3": 0.25}`. If the plant does not output anything, this key may be omitted. -| `locations` | A dictionary mapping the name of the location to a dictionary which describes the site characteristics. See below for a more detailed explanation. +* `input`, the name of the product that this plant takes as input. Only one input is accepted per plant. +* `outputs`, a dictionary specifying how many kg of each product is produced for each kg of input. For example, if the plant outputs 0.5 kg of P2 and 0.25 kg of P3 for each kg of P1 provided, then this entry should be `{"P2": 0.5, "P3": 0.25}`. If the plant does not output anything, this key may be omitted. +* `locations`, a dictionary mapping the name of the location to a dictionary which describes the site characteristics (see below). + +Each type of plant is associated with a set of potential locations where it can be built. Each location is represented by a dictionary with the following keys: + +* `latitude`, the latitude of the location, in degrees. +* `longitude`, the longitude of the location, in degrees. +* `opening cost`, the cost (in dollars) to open the plant. +* `fixed operating cost`, the cost (in dollars) to keep the plant open, even if the plant doesn't process anything. +* `variable operating cost`, the cost (in dollars per kg) that the plant incurs to process each kg of input. + +### Optimizing + +After creating a JSON file describing the reverse manufacturing process and the input data, the following example illustrates how to use the package to find the optimal set of decisions: + +```julia +using ReverseManufacturing -Each type of plant is associated with a set of potential locations. Each location is represented by a dictionary with the following keys: +ReverseManufacturing.solve("/home/user/instance.json") +``` -| Key | Description -|:------------------------------|:----------------------------------- -| `latitude` | The latitude of the location, in degrees. -| `longitude` | The longitude of the location, in degrees. -| `opening cost` | The cost (in dollars) to open the plant. -| `fixed operating cost` | The cost (in dollars) to keep the plant open, even if the plant doesn't process anything. -| `variable operating cost` | The cost (in dollars per kg) that the plant incurs to process each kg of input. +The optimal logistics plan will be printed to the screen. +Current Limitations +------------------- +* Each plant is only allowed exactly one product as input +* No support for multi-period simulations Authors -======= +------- * **Alinson S. Xavier,** Argonne National Laboratory <> * **Nwike Iloeje,** Argonne National Laboratory <> \ No newline at end of file diff --git a/src/model.jl b/src/model.jl index 0fc8883..a165f14 100644 --- a/src/model.jl +++ b/src/model.jl @@ -1,7 +1,7 @@ # Copyright (C) 2019 Argonne National Laboratory # Written by Alinson Santos Xavier -using JuMP, LinearAlgebra, Geodesy +using JuMP, LinearAlgebra, Geodesy, Cbc mutable struct ReverseManufacturingModel mip::JuMP.Model @@ -207,4 +207,85 @@ function calculate_distance(source_lat, source_lon, dest_lat, dest_lon)::Float64 return round(distance(x, y) / 1000.0, digits=2) end +function print_solution(instance, model) + vals = Dict() + for a in values(model.arcs) + vals[a] = JuMP.value(model.vars.flow[a]) + end + for n in values(model.process_nodes) + vals[n] = JuMP.value(model.vars.node[n]) + end + for (plant_name, plant) in instance.plants + for (location_name, location) in plant["locations"] + unused = true + printstyled(@sprintf("%s (at %s):\n", plant_name, location_name), + bold=true, + color=:red) + for arc in model.arcs + arc.dest.plant_name == plant_name || continue + arc.dest.location_name == location_name || continue + arc.source.plant_name != plant_name || continue + vals[arc] > 0.0 || continue + @printf(" ← Receive %.2f kg of %s from %s (at %s)\n", + vals[arc], + arc.source.product_name, + arc.source.plant_name, + arc.source.location_name) + @printf(" Distance %.2f km\n", arc.values["distance"]) + @printf(" Transportation cost %.2f USD\n", + vals[arc] * arc.costs["transportation"]) + if arc.costs["variable"] > 0 + @printf(" Variable operating cost %.2f USD\n", + arc.costs["variable"] * vals[arc]) + else + @printf(" Revenue %.2f USD\n", + -arc.costs["variable"] * vals[arc]) + end + unused = false + end + for arc in model.arcs + arc.source.plant_name == plant_name || continue + arc.source.location_name == location_name || continue + arc.dest.plant_name == plant_name || continue + arc.dest.location_name == location_name || continue + vals[arc] > 0.0 || continue + @printf(" ↺ Convert %.2f kg of %s into %.2f kg of %s\n", + vals[arc] / arc.values["weight"], + arc.source.product_name, + vals[arc], + arc.dest.product_name) + unused = false + end + for arc in model.arcs + arc.source.plant_name == plant_name || continue + arc.source.location_name == location_name || continue + arc.dest.plant_name != plant_name || continue + vals[arc] > 0.0 || continue + @printf(" → Send %.2f kg of %s to %s (at %s)\n", + vals[arc], + arc.dest.product_name, + arc.dest.plant_name, + arc.dest.location_name) + unused = false + end + if unused + println(" Not used") + end + println() + end + end + printstyled(@sprintf("Total profit: %.2f USD", -objective_value(model.mip)), + bold=true, + color=:red) +end + +function solve(filename::String; + optimizer=with_optimizer(Cbc.Optimizer, + logLevel=0)) + instance = ReverseManufacturing.readfile(filename) + model = ReverseManufacturing.build_model(instance, optimizer) + JuMP.optimize!(model.mip) + print_solution(instance, model) +end + export FlowArc diff --git a/test/model_test.jl b/test/model_test.jl index 35d6d6c..5693626 100644 --- a/test/model_test.jl +++ b/test/model_test.jl @@ -71,4 +71,8 @@ using ReverseManufacturing, Cbc, JuMP, Printf # "$(a.dest.product_name) $(a.dest.plant_name) $(a.dest.location_name)", # a.weight) # end +end + +@testset "Solve" begin + ReverseManufacturing.solve("$(pwd())/../instances/samples/s2.json") end \ No newline at end of file