diff --git a/.gitignore b/.gitignore index 1b84cb7..db0c8fe 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ instances/Makefile instances/run.jl notebooks .idea +*.lp diff --git a/instances/solutions/s1.json b/instances/solutions/s1.json index 90989d8..f9ea945 100644 --- a/instances/solutions/s1.json +++ b/instances/solutions/s1.json @@ -54,15 +54,72 @@ }, "F1": { "L1": { - "total output": {}, + "total output": { + "P2": [ + 200.0, + 200.0 + ], + "P3": [ + 500.0, + 500.0 + ] + }, "latitude": 0.0, "expansion cost": [ 750.0, 0.0 ], "output": { - "dispose": {}, - "send": {} + "dispose": { + "P2": { + "amount": [ + 1.0, + 1.0 + ], + "cost": [ + -10.0, + -10.0 + ] + }, + "P3": { + "amount": [ + 1.0, + 1.0 + ], + "cost": [ + -10.0, + -10.0 + ] + } + }, + "send": { + "P2": { + "F2": { + "L4": { + "amount": [ + 199.0, + 199.0 + ], + "latitude": 0.75, + "distance": 85.87, + "longitude": 0.2 + } + } + }, + "P3": { + "F4": { + "L6": { + "amount": [ + 499.0, + 499.0 + ], + "latitude": 50.0, + "distance": 6893.41, + "longitude": 50.0 + } + } + } + } }, "longitude": 0.0, "total input": [ @@ -155,7 +212,16 @@ ] }, "L2": { - "total output": {}, + "total output": { + "P2": [ + 747.728, + 747.728 + ], + "P3": [ + 1869.3199999999997, + 1869.3199999999997 + ] + }, "latitude": 0.5, "expansion cost": [ 3738.6399999999967, @@ -163,7 +229,34 @@ ], "output": { "dispose": {}, - "send": {} + "send": { + "P2": { + "F2": { + "L4": { + "amount": [ + 747.728, + 747.728 + ], + "latitude": 0.75, + "distance": 43.35, + "longitude": 0.2 + } + } + }, + "P3": { + "F4": { + "L6": { + "amount": [ + 1869.3199999999997, + 1869.3199999999997 + ], + "latitude": 50.0, + "distance": 6828.89, + "longitude": 50.0 + } + } + } + } }, "longitude": 0.5, "total input": [ @@ -308,38 +401,17 @@ } }, "F2": { - "L3": { - "total output": {}, - "latitude": 25.0, - "expansion cost": [ - 0.0, - 0.0 - ], - "output": { - "dispose": {}, - "send": {} - }, - "longitude": 65.0, - "total input": [ - 0.0, - 0.0 - ], - "fixed operating cost": [ - -0.0, - -0.0 - ], - "input": {}, - "opening cost": [ - -0.0, - 0.0 - ], - "capacity": [ - 0.0, - 0.0 - ] - }, "L4": { - "total output": {}, + "total output": { + "P4": [ + 757.3824000000001, + 757.3824000000001 + ], + "P3": [ + 47.336400000000005, + 47.336400000000005 + ] + }, "latitude": 0.75, "expansion cost": [ 0.0, @@ -347,7 +419,34 @@ ], "output": { "dispose": {}, - "send": {} + "send": { + "P4": { + "F3": { + "L5": { + "amount": [ + 757.3824000000001, + 757.3824000000001 + ], + "latitude": 100.0, + "distance": 8811.73, + "longitude": 100.0 + } + } + }, + "P3": { + "F4": { + "L6": { + "amount": [ + 47.336400000000005, + 47.336400000000005 + ], + "latitude": 50.0, + "distance": 6824.63, + "longitude": 50.0 + } + } + } + } }, "longitude": 0.2, "total input": [ @@ -513,16 +612,16 @@ 130.0 ], "total": [ - 225924.922057, - 216936.282057 + 225904.922057, + 216916.282057 ], "transportation": [ 133.464057, 133.464057 ], "disposal": [ - 0.0, - 0.0 + -20.0, + -20.0 ] } } diff --git a/src/model.jl b/src/model.jl index 0c76261..8f55968 100644 --- a/src/model.jl +++ b/src/model.jl @@ -197,7 +197,7 @@ function get_solution(model::ManufacturingModel) end for plant in instance.plants - skip_plant = false + skip_plant = true process_node = plant_to_process_node[plant] plant_dict = Dict{Any, Any}( "input" => Dict(), @@ -261,40 +261,42 @@ function get_solution(model::ManufacturingModel) output["costs"]["variable operating"] += dict["variable operating cost"] end -# # Outputs -# for shipping_node in plant_to_shipping_nodes[plant] -# product_name = shipping_node.product.name -# plant_dict["total output"][product_name] = 0.0 -# plant_dict["output"]["send"][product_name] = product_dict = Dict() + # Outputs + for shipping_node in plant_to_shipping_nodes[plant] + product_name = shipping_node.product.name + plant_dict["total output"][product_name] = zeros(T) + plant_dict["output"]["send"][product_name] = product_dict = Dict() -# disposal_amount = JuMP.value(vars.dispose[shipping_node]) -# if disposal_amount > 1e-5 -# plant_dict["output"]["dispose"][product_name] = disposal_dict = Dict() -# disposal_dict["amount"] = JuMP.value(model.vars.dispose[shipping_node]) -# disposal_dict["cost"] = disposal_dict["amount"] * plant.disposal_cost[shipping_node.product] -# plant_dict["total output"][product_name] += disposal_amount -# output["costs"]["disposal"] += disposal_dict["cost"] -# end + disposal_amount = [JuMP.value(vars.dispose[shipping_node, t]) for t in 1:T] + if sum(disposal_amount) > 1e-5 + skip_plant = false + plant_dict["output"]["dispose"][product_name] = disposal_dict = Dict() + disposal_dict["amount"] = [JuMP.value(model.vars.dispose[shipping_node, t]) for t in 1:T] + disposal_dict["cost"] = [disposal_dict["amount"][t] * plant.disposal_cost[shipping_node.product][t] + for t in 1:T] + plant_dict["total output"][product_name] += disposal_amount + output["costs"]["disposal"] += disposal_dict["cost"] + end -# for a in shipping_node.outgoing_arcs -# val = JuMP.value(vars.flow[a]) -# if val <= 1e-3 -# continue -# end -# skip_plant = false -# dict = Dict( -# "amount" => val, -# "distance" => a.values["distance"], -# "latitude" => a.dest.location.latitude, -# "longitude" => a.dest.location.longitude, -# ) -# if a.dest.location.plant_name ∉ keys(product_dict) -# product_dict[a.dest.location.plant_name] = Dict() -# end -# product_dict[a.dest.location.plant_name][a.dest.location.location_name] = dict -# plant_dict["total output"][product_name] += val -# end -# end + for a in shipping_node.outgoing_arcs + vals = [JuMP.value(vars.flow[a, t]) for t in 1:T] + if sum(vals) <= 1e-3 + continue + end + skip_plant = false + dict = Dict( + "amount" => vals, + "distance" => a.values["distance"], + "latitude" => a.dest.location.latitude, + "longitude" => a.dest.location.longitude, + ) + if a.dest.location.plant_name ∉ keys(product_dict) + product_dict[a.dest.location.plant_name] = Dict() + end + product_dict[a.dest.location.plant_name][a.dest.location.location_name] = dict + plant_dict["total output"][product_name] += vals + end + end if !skip_plant if plant.plant_name ∉ keys(output["plants"]) diff --git a/test/graph_test.jl b/test/graph_test.jl index d88554d..4699a46 100644 --- a/test/graph_test.jl +++ b/test/graph_test.jl @@ -6,7 +6,7 @@ using ReverseManufacturing @testset "Graph" begin @testset "build_graph" begin basedir = dirname(@__FILE__) - instance = ReverseManufacturing.load("$basedir/../instances/samples/s1.json") + instance = ReverseManufacturing.load("$basedir/../instances/s1.json") graph = ReverseManufacturing.build_graph(instance) process_node_by_location_name = Dict(n.location.location_name => n for n in graph.process_nodes) diff --git a/test/instance_test.jl b/test/instance_test.jl index 5d0c6cf..6ac024e 100644 --- a/test/instance_test.jl +++ b/test/instance_test.jl @@ -6,7 +6,7 @@ using ReverseManufacturing @testset "Instance" begin @testset "load" begin basedir = dirname(@__FILE__) - instance = ReverseManufacturing.load("$basedir/../instances/samples/s1.json") + instance = ReverseManufacturing.load("$basedir/../instances/s1.json") centers = instance.collection_centers plants = instance.plants diff --git a/test/model_test.jl b/test/model_test.jl index b3e53bc..4f70c0e 100644 --- a/test/model_test.jl +++ b/test/model_test.jl @@ -6,7 +6,7 @@ using ReverseManufacturing, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormat @testset "Model" begin @testset "build" begin basedir = dirname(@__FILE__) - instance = ReverseManufacturing.load("$basedir/../instances/samples/s1.json") + instance = ReverseManufacturing.load("$basedir/../instances/s1.json") graph = ReverseManufacturing.build_graph(instance) model = ReverseManufacturing.build_model(instance, graph, Cbc.Optimizer) @@ -43,8 +43,8 @@ using ReverseManufacturing, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormat end @testset "solve" begin - solution = ReverseManufacturing.solve("$(pwd())/../instances/samples/s1.json") - #JSON.print(stdout, solution, 4) + solution = ReverseManufacturing.solve("$(pwd())/../instances/s1.json") + JSON.print(stdout, solution, 4) @test "costs" in keys(solution) @test "fixed operating" in keys(solution["costs"])