commit ab644377b6e5ba019060ca55eec56a9b80211eaf Author: Alinson S Xavier Date: Fri Jan 24 13:00:01 2020 -0600 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3692e3b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.ipynb* +*.ipynb diff --git a/Manifest.toml b/Manifest.toml new file mode 100644 index 0000000..ab8f056 --- /dev/null +++ b/Manifest.toml @@ -0,0 +1,231 @@ +# This file is machine-generated - editing it directly is not advised + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[BenchmarkTools]] +deps = ["JSON", "Printf", "Statistics"] +git-tree-sha1 = "90b73db83791c5f83155016dd1cc1f684d4e1361" +uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" +version = "0.4.3" + +[[BinaryProvider]] +deps = ["Libdl", "SHA"] +git-tree-sha1 = "5b08ed6036d9d3f0ee6369410b830f8873d4024c" +uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" +version = "0.5.8" + +[[Calculus]] +deps = ["LinearAlgebra"] +git-tree-sha1 = "f641eb0a4f00c343bbc32346e1217b86f3ce9dad" +uuid = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9" +version = "0.5.1" + +[[Cbc]] +deps = ["BinaryProvider", "Libdl", "MathOptInterface", "MathProgBase", "SparseArrays", "Test"] +git-tree-sha1 = "0d51c2d66fc22e5e3fc64b6092ba0f0b3839c8c1" +uuid = "9961bab8-2fa3-5c5a-9d89-47fab24efd76" +version = "0.6.6" + +[[CommonSubexpressions]] +deps = ["Test"] +git-tree-sha1 = "efdaf19ab11c7889334ca247ff4c9f7c322817b0" +uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" +version = "0.2.0" + +[[Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "ed2c4abadf84c53d9e58510b5fc48912c2336fbb" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "2.2.0" + +[[CoordinateTransformations]] +deps = ["Compat", "Rotations", "StaticArrays"] +git-tree-sha1 = "47f05d0b7f4999609f92e657147df000818c1f24" +uuid = "150eb455-5306-5404-9cee-2592286d6298" +version = "0.5.0" + +[[DataStructures]] +deps = ["InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "a1b652fb77ae8ca7ea328fa7ba5aa151036e5c10" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.17.6" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[DiffResults]] +deps = ["StaticArrays"] +git-tree-sha1 = "da24935df8e0c6cf28de340b958f6aac88eaa0cc" +uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" +version = "1.0.2" + +[[DiffRules]] +deps = ["NaNMath", "Random", "SpecialFunctions"] +git-tree-sha1 = "10dca52cf6d4a62d82528262921daf63b99704a2" +uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" +version = "1.0.0" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[ForwardDiff]] +deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "NaNMath", "Random", "SpecialFunctions", "StaticArrays"] +git-tree-sha1 = "840700059391d36e2498d89c2e82c08f261f2a2a" +uuid = "f6369f11-7733-5829-9624-2563aa707210" +version = "0.10.8" + +[[Geodesy]] +deps = ["CoordinateTransformations", "Dates", "LinearAlgebra", "StaticArrays", "Test"] +git-tree-sha1 = "f80ea86cb88db337a1906e245e495592f0b5cc25" +uuid = "0ef565a4-170c-5f04-8de2-149903a85f3d" +version = "0.5.0" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[JSON]] +deps = ["Dates", "Mmap", "Parsers", "Unicode"] +git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e" +uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +version = "0.21.0" + +[[JuMP]] +deps = ["Calculus", "DataStructures", "ForwardDiff", "LinearAlgebra", "MathOptInterface", "NaNMath", "Random", "SparseArrays", "Statistics"] +git-tree-sha1 = "ba7f96010ed290d77d5c941c32e5df107ca688a4" +uuid = "4076af6c-e467-56ae-b986-b466b2749572" +version = "0.20.1" + +[[LibGit2]] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LinearAlgebra]] +deps = ["Libdl"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[MathOptInterface]] +deps = ["BenchmarkTools", "LinearAlgebra", "MutableArithmetics", "OrderedCollections", "SparseArrays", "Test", "Unicode"] +git-tree-sha1 = "026c2e55361bc3fdc3287a563e27f32ec4853549" +uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" +version = "0.9.8" + +[[MathProgBase]] +deps = ["Compat"] +git-tree-sha1 = "3bf2e534e635df810e5f4b4f1a8b6de9004a0d53" +uuid = "fdba3010-5040-5b88-9595-932c9decdf73" +version = "0.7.7" + +[[Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[MutableArithmetics]] +deps = ["LinearAlgebra", "SparseArrays", "Test"] +git-tree-sha1 = "fa3483f453a97a8d442810ca939165bf0b370d2e" +uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" +version = "0.1.1" + +[[NaNMath]] +git-tree-sha1 = "928b8ca9b2791081dc71a51c55347c27c618760f" +uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" +version = "0.3.3" + +[[OpenSpecFun_jll]] +deps = ["Libdl", "Pkg"] +git-tree-sha1 = "65f672edebf3f4e613ddf37db9dcbd7a407e5e90" +uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e" +version = "0.5.3+1" + +[[OrderedCollections]] +deps = ["Random", "Serialization", "Test"] +git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.1.0" + +[[Parsers]] +deps = ["Dates", "Test"] +git-tree-sha1 = "0139ba59ce9bc680e2925aec5b7db79065d60556" +uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" +version = "0.3.10" + +[[Pkg]] +deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[Rotations]] +deps = ["LinearAlgebra", "StaticArrays", "Statistics"] +git-tree-sha1 = "4fa94411637e4f4ca4d9b5bfa64d7042541d67b3" +uuid = "6038ab10-8711-5258-84ad-4b1120ba62dc" +version = "0.12.0" + +[[SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[SpecialFunctions]] +deps = ["OpenSpecFun_jll"] +git-tree-sha1 = "268052ee908b2c086cc0011f528694f02f3e2408" +uuid = "276daf66-3868-5448-9aa4-cd146d93841b" +version = "0.9.0" + +[[StaticArrays]] +deps = ["LinearAlgebra", "Random", "Statistics"] +git-tree-sha1 = "5a3bcb6233adabde68ebc97be66e95dcb787424c" +uuid = "90137ffa-7385-5640-81b9-e52037218182" +version = "0.12.1" + +[[Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[[Test]] +deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..290f5d2 --- /dev/null +++ b/Project.toml @@ -0,0 +1,13 @@ +name = "ReverseManufacturing" +uuid = "a2afcdf7-cf04-4913-85f9-c0d81ddf2008" +authors = ["Alinson S Xavier "] +version = "1.0.0" + +[deps] +Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76" +Geodesy = "0ef565a4-170c-5f04-8de2-149903a85f3d" +JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +JuMP = "4076af6c-e467-56ae-b986-b466b2749572" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/README.md b/README.md new file mode 100644 index 0000000..b699443 --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +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. + + +Data Specification +================== + +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`. + +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. + +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. + +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. + +Each type of plant is associated with a set of potential locations. Each location is represented by a dictionary with the following keys: + +| 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. + + +Authors +======= +* **Alinson S. Xavier,** Argonne National Laboratory <> +* **Nwike Iloeje,** Argonne National Laboratory <> \ No newline at end of file diff --git a/instances/samples/s1.json b/instances/samples/s1.json new file mode 100644 index 0000000..19176e7 --- /dev/null +++ b/instances/samples/s1.json @@ -0,0 +1,145 @@ +{ + "products": { + "P1": { + "transportation cost": 1.50, + "initial amounts": { + "C1": { + "latitude": 7.0, + "longitude": 7.0, + "amount": 934.56 + }, + "C2": { + "latitude": 7.0, + "longitude": 19.0, + "amount": 198.95 + }, + "C3": { + "latitude": 84.0, + "longitude": 76.0, + "amount": 212.97 + }, + "C4": { + "latitude": 21.0, + "longitude": 16.0, + "amount": 352.19 + }, + "C5": { + "latitude": 32.0, + "longitude": 92.0, + "amount": 510.33 + }, + "C6": { + "latitude": 14.0, + "longitude": 62.0, + "amount": 471.66 + }, + "C7": { + "latitude": 30.0, + "longitude": 83.0, + "amount": 785.21 + }, + "C8": { + "latitude": 35.0, + "longitude": 40.0, + "amount": 706.17 + }, + "C9": { + "latitude": 74.0, + "longitude": 52.0, + "amount": 30.08 + }, + "C10": { + "latitude": 22.0, + "longitude": 54.0, + "amount": 536.52 + } + } + }, + "P2": { + "transportation cost": 2.00 + }, + "P3": { + "transportation cost": 1.25 + }, + "P4": { + "transportation cost": 1.75 + } + }, + "plants": { + "F1": { + "input": "P1", + "outputs": { + "P2": 0.2, + "P3": 0.5 + }, + "locations": { + "L1": { + "latitude": 0.0, + "longitude": 0.0, + "capacity": 500, + "opening cost": 2000, + "fixed operating cost": 70.0, + "variable operating cost": 70.0 + }, + "L2": { + "latitude": 0.5, + "longitude": 0.5, + "opening cost": 1000, + "capacity": 750, + "opening cost": 1000, + "fixed operating cost": 50.0, + "variable operating cost": 50.0 + } + } + }, + "F2": { + "input": "P2", + "outputs": { + "P3": 0.05, + "P4": 0.80 + }, + "locations": { + "L3": { + "latitude": 25.0, + "longitude": 65.0, + "capacity": 1000, + "opening cost": 3000, + "fixed operating cost": 50.0, + "variable operating cost": 50.0 + }, + "L4": { + "latitude": 0.75, + "longitude": 0.20, + "processing cost": 250.0, + "opening cost": 3000, + "fixed operating cost": 50.0, + "variable operating cost": 50.0 + } + } + }, + "F3": { + "input": "P4", + "locations": { + "L5": { + "latitude": 100.0, + "longitude": 100.0, + "opening cost": 0.0, + "fixed operating cost": 0.0, + "variable operating cost": -15.0 + } + } + }, + "F4": { + "input": "P3", + "locations": { + "L6": { + "latitude": 50.0, + "longitude": 50.0, + "opening cost": 0.0, + "fixed operating cost": 0.0, + "variable operating cost": -15.0 + } + } + } + } +} \ No newline at end of file diff --git a/instances/samples/s2.json b/instances/samples/s2.json new file mode 100644 index 0000000..16947e5 --- /dev/null +++ b/instances/samples/s2.json @@ -0,0 +1,148 @@ +{ + "products": { + "lead-acid batteries": { + "transportation cost": 0.0015, + "initial amounts": { + "Chicago": { + "latitude": 41.881944, + "longitude": -87.627778, + "amount": 175000.0 + }, + "Darien": { + "latitude": 41.745556, + "longitude": -87.981111, + "amount": 30000.0 + }, + "Westmont": { + "latitude": 41.794444, + "longitude": -87.976389, + "amount": 25000.0 + }, + "Milwaukee": { + "latitude": 43.05, + "longitude": -87.95, + "amount": 120000.0 + } + } + }, + "lead": { + "transportation cost": 0.0015 + }, + "lead ingots": { + "transportation cost": 0.0017 + }, + "plastic casings": { + "transportation cost": 0.0023 + }, + "plastic pellets": { + "transportation cost": 0.0020 + } + }, + "plants": { + "Disassembly Plant": { + "input": "lead-acid batteries", + "outputs": { + "lead": 0.60, + "plastic casings": 0.05 + }, + "locations": { + "Darien": { + "latitude": 41.745556, + "longitude": -87.981111, + "variable operating cost": 0.12, + "fixed operating cost": 1000, + "opening cost": 10000.0 + }, + "Lemont": { + "latitude": 41.668784, + "longitude": -87.988845, + "variable operating cost": 0.15, + "fixed operating cost": 1000, + "opening cost": 10000.0 + }, + "Greendale": { + "latitude": 42.9375, + "longitude": -87.996944, + "variable operating cost": 0.18, + "fixed operating cost": 1000, + "opening cost": 10000.0 + } + } + }, + "Plastic Recycling Plant": { + "input": "plastic casings", + "outputs": { + "plastic pellets": 0.9 + }, + "locations": { + "Milwaukee": { + "latitude": 43.05, + "longitude": -87.95, + "variable operating cost": 0.08, + "fixed operating cost": 1000, + "opening cost": 10000.0 + }, + "Chicago": { + "latitude": 41.881944, + "longitude": -87.627778, + "variable operating cost": 0.10, + "fixed operating cost": 1000, + "opening cost": 10000.0 + } + } + }, + "Lead Recycling Plant": { + "input": "lead", + "outputs": { + "lead ingots": 0.9 + }, + "locations": { + "Milwaukee": { + "latitude": 43.05, + "longitude": -87.95, + "variable operating cost": 0.20, + "fixed operating cost": 1000, + "opening cost": 10000.0 + } + } + }, + "Lead Sales Point": { + "input": "lead ingots", + "locations": { + "Chicago": { + "latitude": 41.881944, + "longitude": -87.627778, + "variable operating cost": -1.50, + "fixed operating cost": 1000, + "opening cost": 10000.0 + }, + "Milwaukee": { + "latitude": 43.05, + "longitude": -87.95, + "variable operating cost": -2.75, + "fixed operating cost": 0.0, + "opening cost": 0.0 + } + } + }, + "Plastic Sales Point": { + "input": "plastic pellets", + "locations": { + "Chicago": { + "latitude": 41.881944, + "longitude": -87.627778, + "variable operating cost": -0.50, + "fixed operating cost": 0.0, + "opening cost": 0.0 + }, + "Milwaukee": { + "latitude": 43.05, + "longitude": -87.95, + "variable operating cost": -0.62, + "fixed operating cost": 0.0, + "opening cost": 0.0 + } + } + } + } +} \ No newline at end of file diff --git a/src/ReverseManufacturing.jl b/src/ReverseManufacturing.jl new file mode 100644 index 0000000..4b98668 --- /dev/null +++ b/src/ReverseManufacturing.jl @@ -0,0 +1,8 @@ +# Copyright (C) 2020 Argonne National Laboratory +# Written by Alinson Santos Xavier + +module ReverseManufacturing + include("dotdict.jl") + include("instance.jl") + include("model.jl") +end diff --git a/src/dotdict.jl b/src/dotdict.jl new file mode 100644 index 0000000..ff5ef9a --- /dev/null +++ b/src/dotdict.jl @@ -0,0 +1,67 @@ +# Copyright (C) 2019 Argonne National Laboratory +# Written by Alinson Santos Xavier + +struct DotDict + inner::Dict +end + +DotDict() = DotDict(Dict()) + +function Base.setproperty!(d::DotDict, key::Symbol, value) + setindex!(getfield(d, :inner), value, key) +end + +function Base.getproperty(d::DotDict, key::Symbol) + (key == :inner ? getfield(d, :inner) : d.inner[key]) +end + +function Base.getindex(d::DotDict, key::Int64) + d.inner[Symbol(key)] +end + +function Base.getindex(d::DotDict, key::Symbol) + d.inner[key] +end + +function Base.keys(d::DotDict) + keys(d.inner) +end + +function Base.values(d::DotDict) + values(d.inner) +end + +function Base.iterate(d::DotDict) + iterate(values(d.inner)) +end + +function Base.iterate(d::DotDict, v::Int64) + iterate(values(d.inner), v) +end + +function Base.length(d::DotDict) + length(values(d.inner)) +end + +function Base.show(io::IO, d::DotDict) + print(io, "DotDict with $(length(keys(d.inner))) entries:\n") + count = 0 + for k in keys(d.inner) + count += 1 + if count > 10 + print(io, " ...\n") + break + end + print(io, " :$(k) => $(d.inner[k])\n") + end +end + +function recursive_to_dot_dict(el) + if typeof(el) == Dict{String, Any} + return DotDict(Dict(Symbol(k) => recursive_to_dot_dict(el[k]) for k in keys(el))) + else + return el + end +end + +export recursive_to_dot_dict \ No newline at end of file diff --git a/src/instance.jl b/src/instance.jl new file mode 100644 index 0000000..5d08f8f --- /dev/null +++ b/src/instance.jl @@ -0,0 +1,89 @@ +# Copyright (C) 2020 Argonne National Laboratory +# Written by Alinson Santos Xavier + +using Printf, JSON +import Base.getindex, Base.time + +""" + mutable struct ReverseManufacturingInstance + +Representation of an instance of the Facility Location for Reverse Manufacturing problem. +""" +mutable struct ReverseManufacturingInstance + json::Dict + products::Dict + plants::Dict +end + +function Base.show(io::IO, instance::ReverseManufacturingInstance) + n_plants = length(instance["plants"]) + n_products = length(instance["products"]) + print(io, "ReverseManufacturingInstance with ") + print(io, "$n_plants plants, ") + print(io, "$n_products products") +end + +""" + load(name::String)::ReverseManufacturingInstance + +Loads an instance from the benchmark set. + +Example +======= + + julia> ReverseManufacturing.load("samples/s1.json") + +""" +function load(name::String) :: ReverseManufacturingInstance + basedir = dirname(@__FILE__) + return ReverseManufacturing.readfile("$basedir/../instances/$name.json") +end + + +""" + readfile(path::String)::ReverseManufacturingInstance + +Loads an instance from the given JSON file. + +Example +======= + + julia> ReverseManufacturing.load("/home/user/instance.json") + +""" +function readfile(path::String)::ReverseManufacturingInstance + json = JSON.parsefile(path) + products = Dict(key => json["products"][key] + for key in keys(json["products"])) + plants = Dict(key => json["plants"][key] + for key in keys(json["plants"])) + + for product_name in keys(products) + product = products[product_name] + product["name"] = product_name + product["input plants"] = [] + product["output plants"] = [] + end + + for plant_name in keys(plants) + plant = plants[plant_name] + plant["name"] = plant_name + + # Input product + input_product = products[plant["input"]] + plant["input product"] = input_product + push!(input_product["input plants"], plant) + + # Output products + if haskey(plant, "outputs") + for product_name in keys(plant["outputs"]) + product = products[product_name] + push!(product["output plants"], plant) + end + end + end + + return ReverseManufacturingInstance(json, products, plants) +end + +export ReverseManufacturingInstance diff --git a/src/model.jl b/src/model.jl new file mode 100644 index 0000000..0fc8883 --- /dev/null +++ b/src/model.jl @@ -0,0 +1,210 @@ +# Copyright (C) 2019 Argonne National Laboratory +# Written by Alinson Santos Xavier + +using JuMP, LinearAlgebra, Geodesy + +mutable struct ReverseManufacturingModel + mip::JuMP.Model + vars::DotDict + arcs + decision_nodes + process_nodes +end + +mutable struct Node + product_name::String + plant_name::String + location_name::String + balance::Float64 + incoming_arcs::Array + outgoing_arcs::Array + cost::Float64 +end + +function Node(product_name::String, + plant_name::String, + location_name::String; + balance::Float64 = 0.0, + incoming_arcs::Array = [], + outgoing_arcs::Array = [], + cost::Float64 = 0.0, + ) :: Node + return Node(product_name, + plant_name, + location_name, + balance, + incoming_arcs, + outgoing_arcs, + cost) +end + +function Base.show(io::IO, node::Node) + print(io, "Node($(node.product_name), $(node.plant_name), $(node.location_name)") + if node.balance != 0.0 + print(io, ", $(node.balance)") + end + print(io, ")") +end + +mutable struct Arc + source::Node + dest::Node + costs::Dict + values::Dict +end + +function Base.show(io::IO, arc::Arc) + print(io, "Arc($(arc.source), $(arc.dest))") +end + +function build_model(instance::ReverseManufacturingInstance, + optimizer, + ) :: ReverseManufacturingModel + + mip = isa(optimizer, JuMP.OptimizerFactory) ? Model(optimizer) : direct_model(optimizer) + decision_nodes, process_nodes, arcs = create_nodes_and_arcs(instance) + vars = DotDict() + vars.flow = Dict(a => @variable(mip, lower_bound=0) for a in arcs) + vars.node = Dict(n => @variable(mip, binary=true) for n in values(process_nodes)) + create_decision_node_constraints!(mip, decision_nodes, vars) + create_process_node_constraints!(mip, process_nodes, vars) + flow_costs = sum(a.costs[c] * vars.flow[a] for a in arcs for c in keys(a.costs)) + node_costs = sum(n.cost * vars.node[n] for n in values(process_nodes)) + @objective(mip, Min, flow_costs + node_costs) + return return ReverseManufacturingModel(mip, + vars, + arcs, + decision_nodes, + process_nodes) +end + +function create_decision_node_constraints!(mip, nodes, vars) + for (id, n) in nodes + @constraint(mip, + sum(vars.flow[a] for a in n.incoming_arcs) + n.balance == + sum(vars.flow[a] for a in n.outgoing_arcs)) + end +end + +function create_process_node_constraints!(mip, nodes, vars) + for (id, n) in nodes + # Output amount is implied by input amount + input_sum = sum(vars.flow[a] for a in n.incoming_arcs) + for a in n.outgoing_arcs + @constraint(mip, vars.flow[a] == a.values["weight"] * input_sum) + end + # If plant is closed, input must be zero + @constraint(mip, input_sum <= 1e6 * vars.node[n]) + end +end + +function create_nodes_and_arcs(instance) + arcs = Arc[] + decision_nodes = Dict() + process_nodes = Dict() + + # Create all nodes + for (product_name, product) in instance.products + + # Decision nodes for initial amounts + if haskey(product, "initial amounts") + for location_name in keys(product["initial amounts"]) + amount = product["initial amounts"][location_name]["amount"] + n = Node(product_name, "Origin", location_name, balance=amount) + decision_nodes[n.product_name, n.plant_name, n.location_name] = n + end + end + + # Process nodes for each plant + for plant in product["input plants"] + for (location_name, location) in plant["locations"] + cost = location["opening cost"] + location["fixed operating cost"] + n = Node(product_name, plant["name"], location_name, cost=cost) + process_nodes[n.product_name, n.plant_name, n.location_name] = n + end + end + + # Decision nodes for each plant + for plant in product["output plants"] + for location_name in keys(plant["locations"]) + n = Node(product_name, plant["name"], location_name) + decision_nodes[n.product_name, n.plant_name, n.location_name] = n + end + end + end + + # Create arcs + for (product_name, product) in instance.products + + # Transportation arcs from initial location to plants + if haskey(product, "initial amounts") + for source_location_name in keys(product["initial amounts"]) + source_location = product["initial amounts"][source_location_name] + for dest_plant in product["input plants"] + for dest_location_name in keys(dest_plant["locations"]) + dest_location = dest_plant["locations"][dest_location_name] + source = decision_nodes[product_name, "Origin", source_location_name] + dest = process_nodes[product_name, dest_plant["name"], dest_location_name] + distance = calculate_distance(source_location["latitude"], + source_location["longitude"], + dest_location["latitude"], + dest_location["longitude"]) + costs = Dict("transportation" => product["transportation cost"] * distance, + "variable" => dest_location["variable operating cost"]) + values = Dict("distance" => distance) + a = Arc(source, dest, costs, values) + push!(arcs, a) + push!(source.outgoing_arcs, a) + push!(dest.incoming_arcs, a) + end + end + end + end + + + for source_plant in product["output plants"] + for source_location_name in keys(source_plant["locations"]) + source_location = source_plant["locations"][source_location_name] + + # Process-arcs within a plant + source = process_nodes[source_plant["input"], source_plant["name"], source_location_name] + dest = decision_nodes[product_name, source_plant["name"], source_location_name] + costs = Dict() + values = Dict("weight" => source_plant["outputs"][product_name]) + a = Arc(source, dest, costs, values) + push!(arcs, a) + push!(source.outgoing_arcs, a) + push!(dest.incoming_arcs, a) + + # Transportation-arcs from one plant to another + for dest_plant in product["input plants"] + for dest_location_name in keys(dest_plant["locations"]) + dest_location = dest_plant["locations"][dest_location_name] + source = decision_nodes[product_name, source_plant["name"], source_location_name] + dest = process_nodes[product_name, dest_plant["name"], dest_location_name] + distance = calculate_distance(source_location["latitude"], + source_location["longitude"], + dest_location["latitude"], + dest_location["longitude"]) + costs = Dict("transportation" => product["transportation cost"] * distance, + "variable" => dest_location["variable operating cost"]) + values = Dict("distance" => distance) + a = Arc(source, dest, costs, values) + push!(arcs, a) + push!(source.outgoing_arcs, a) + push!(dest.incoming_arcs, a) + end + end + end + end + end + return decision_nodes, process_nodes, arcs +end + +function calculate_distance(source_lat, source_lon, dest_lat, dest_lon)::Float64 + x = LLA(source_lat, source_lon, 0.0) + y = LLA(dest_lat, dest_lon, 0.0) + return round(distance(x, y) / 1000.0, digits=2) +end + +export FlowArc diff --git a/test/instance_test.jl b/test/instance_test.jl new file mode 100644 index 0000000..b8dd660 --- /dev/null +++ b/test/instance_test.jl @@ -0,0 +1,18 @@ +# Copyright (C) 2020 Argonne National Laboratory +# Written by Alinson Santos Xavier + +using ReverseManufacturing + +@testset "Instance" begin + instance = ReverseManufacturing.load("samples/s1") + plants, products = instance.plants, instance.products + @test length(products) == 4 + + @test sort(collect(keys(plants))) == ["F1", "F2", "F3", "F4"] + @test plants["F1"]["input product"] == products["P1"] + + @test sort(collect(keys(products))) == ["P1", "P2", "P3", "P4"] + @test products["P1"]["input plants"] == [plants["F1"]] + @test products["P1"]["transportation cost"] == 1.5 + @test products["P1"]["initial amounts"]["C1"]["latitude"] == 7.0 +end diff --git a/test/model_test.jl b/test/model_test.jl new file mode 100644 index 0000000..35d6d6c --- /dev/null +++ b/test/model_test.jl @@ -0,0 +1,74 @@ +# Copyright (C) 2020 Argonne National Laboratory +# Written by Alinson Santos Xavier + +using ReverseManufacturing, Cbc, JuMP, Printf + +@testset "Model" begin + instance = ReverseManufacturing.load("samples/s1") + model = ReverseManufacturing.build_model(instance, with_optimizer(Cbc.Optimizer)) + + # Verify nodes + @test ("P1", "Origin", "C1") in keys(model.decision_nodes) + @test ("P1", "Origin", "C3") in keys(model.decision_nodes) + @test ("P1", "Origin", "C8") in keys(model.decision_nodes) + @test ("P2", "F1", "L1") in keys(model.decision_nodes) + @test ("P2", "F1", "L2") in keys(model.decision_nodes) + @test ("P3", "F1", "L1") in keys(model.decision_nodes) + @test ("P3", "F1", "L2") in keys(model.decision_nodes) + @test ("P3", "F2", "L3") in keys(model.decision_nodes) + @test ("P3", "F2", "L4") in keys(model.decision_nodes) + @test ("P4", "F2", "L3") in keys(model.decision_nodes) + @test ("P4", "F2", "L4") in keys(model.decision_nodes) + @test ("P1", "F1", "L1") in keys(model.process_nodes) + @test ("P1", "F1", "L2") in keys(model.process_nodes) + @test ("P2", "F2", "L3") in keys(model.process_nodes) + @test ("P2", "F2", "L4") in keys(model.process_nodes) + @test ("P3", "F4", "L6") in keys(model.process_nodes) + @test ("P4", "F3", "L5") in keys(model.process_nodes) + + # Verify some arcs + p1_orig_c1 = model.decision_nodes["P1", "Origin", "C1"] + p1_f1_l1 = model.process_nodes["P1", "F1", "L1"] + @test length(p1_orig_c1.outgoing_arcs) == 2 + @test length(p1_f1_l1.incoming_arcs) == 10 + + arc = p1_orig_c1.outgoing_arcs[1] + @test arc.dest.location_name == "L1" + @test arc.values["distance"] == 1095.62 + @test round(arc.costs["transportation"], digits=2) == 1643.43 + @test arc.costs["variable"] == 70.0 + + p2_f1_l1 = model.decision_nodes["P2", "F1", "L1"] + p2_f2_l3 = model.process_nodes["P2", "F2", "L3"] + @test length(p2_f1_l1.incoming_arcs) == 1 + @test length(p2_f1_l1.outgoing_arcs) == 2 + + arc = p2_f1_l1.incoming_arcs[1] + @test arc.values["weight"] == 0.2 + @test isempty(arc.costs) + +# @show model.mip +# JuMP.optimize!(model.mip) + +# values = Dict(a => JuMP.value(model.vars.flow[a]) for a in model.arcs) +# @printf("source,dest,amount\n") +# for (arc, value) in values +# if value > 1e-6 +# @printf("%s-%s-%s,%s-%s-%s,%.2f\n", +# arc.source.plant_name, +# arc.source.location_name, +# arc.source.product_name, +# arc.dest.plant_name, +# arc.dest.location_name, +# arc.dest.product_name, +# value) +# end +# end + +# for a in model.arcs +# @printf("%20s\t%20s\t%8.2f\n", +# "$(a.source.product_name) $(a.source.plant_name) $(a.source.location_name)", +# "$(a.dest.product_name) $(a.dest.plant_name) $(a.dest.location_name)", +# a.weight) +# end +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..b4e56e7 --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,9 @@ +# Copyright (C) 2020 Argonne National Laboratory +# Written by Alinson Santos Xavier + +using Test + +@testset "ReverseManufacturing" begin + include("instance_test.jl") + include("model_test.jl") +end \ No newline at end of file