Update get_solution

This commit is contained in:
2020-05-04 23:24:13 -05:00
parent cbd7bc5247
commit f970dca68d
3 changed files with 136 additions and 139 deletions

View File

@@ -4,13 +4,13 @@
using JSON, JSONSchema using JSON, JSONSchema
struct Product mutable struct Product
name::String name::String
transportation_cost::Float64 transportation_cost::Float64
end end
struct CollectionCenter mutable struct CollectionCenter
name::String name::String
latitude::Float64 latitude::Float64
longitude::Float64 longitude::Float64
@@ -19,7 +19,7 @@ struct CollectionCenter
end end
struct Plant mutable struct Plant
plant_name::String plant_name::String
location_name::String location_name::String
input::Product input::Product
@@ -37,7 +37,7 @@ struct Plant
end end
struct Instance mutable struct Instance
products::Array{Product, 1} products::Array{Product, 1}
collection_centers::Array{CollectionCenter, 1} collection_centers::Array{CollectionCenter, 1}
plants::Array{Plant, 1} plants::Array{Plant, 1}

View File

@@ -100,7 +100,7 @@ function create_shipping_node_constraints!(model::ManufacturingModel)
end end
function create_process_node_constraints!(model) function create_process_node_constraints!(model::ManufacturingModel)
mip, vars, graph = model.mip, model.vars, model.graph mip, vars, graph = model.mip, model.vars, model.graph
for n in graph.process_nodes for n in graph.process_nodes
@@ -135,137 +135,128 @@ function solve(filename::String; optimizer=Cbc.Optimizer)
println("Optimizing...") println("Optimizing...")
JuMP.optimize!(model.mip) JuMP.optimize!(model.mip)
# println("Extracting solution...") println("Extracting solution...")
# return get_solution(instance, model) return get_solution(model)
end end
# function get_solution(instance::ReverseManufacturingInstance, function get_solution(model::ManufacturingModel)
# model::ReverseManufacturingModel) mip, vars, graph, instance = model.mip, model.vars, model.graph, model.instance
# vals = Dict() output = Dict(
# for a in values(model.arcs) "plants" => Dict(),
# vals[a] = JuMP.value(model.vars.flow[a]) "costs" => Dict(
# end "fixed" => 0.0,
# for n in values(model.process_nodes) "variable" => 0.0,
# vals[n] = JuMP.value(model.vars.open_plant[n]) "transportation" => 0.0,
# end "disposal" => 0.0,
"total" => 0.0,
"expansion" => 0.0,
)
)
# output = Dict( plant_to_process_node = Dict(n.plant => n for n in graph.process_nodes)
# "plants" => Dict(), plant_to_shipping_nodes = Dict()
# "costs" => Dict( for p in instance.plants
# "fixed" => 0.0, plant_to_shipping_nodes[p] = []
# "variable" => 0.0, for a in plant_to_process_node[p].outgoing_arcs
# "transportation" => 0.0, push!(plant_to_shipping_nodes[p], a.dest)
# "disposal" => 0.0, end
# "total" => 0.0, end
# "expansion" => 0.0,
# )
# )
# for (plant_name, plant) in instance.plants for plant in instance.plants
# skip_plant = true skip_plant = true
# plant_dict = Dict{Any, Any}() process_node = plant_to_process_node[plant]
# input_product_name = plant["input"] plant_dict = Dict{Any, Any}(
"input" => Dict(),
"output" => Dict(
"send" => Dict(),
"dispose" => Dict(),
),
"total input" => 0.0,
"total output" => Dict(),
"latitude" => plant.latitude,
"longitude" => plant.longitude,
"capacity" => JuMP.value(vars.capacity[process_node]),
"fixed cost" => JuMP.value(vars.open_plant[process_node]) * (plant.opening_cost + plant.fixed_operating_cost),
"expansion cost" => JuMP.value(vars.expansion[process_node]) * plant.expansion_cost,
)
output["costs"]["fixed"] += plant_dict["fixed cost"]
output["costs"]["expansion"] += plant_dict["expansion cost"]
# for (location_name, location) in plant["locations"] # Inputs
# skip_location = true for a in process_node.incoming_arcs
# process_node = model.process_nodes[input_product_name, plant_name, location_name] val = JuMP.value(vars.flow[a])
if val <= 1e-3
continue
end
skip_plant = false
dict = Dict{Any, Any}(
"amount" => val,
"distance" => a.values["distance"],
"latitude" => a.source.location.latitude,
"longitude" => a.source.location.longitude,
"transportation cost" => a.source.product.transportation_cost * val,
"variable operating cost" => plant.variable_operating_cost * val,
)
if a.source.location isa CollectionCenter
plant_name = "Origin"
location_name = a.source.location.name
else
plant_name = a.source.location.plant_name
location_name = a.source.location.location_name
end
# plant_loc_dict = Dict{Any, Any}( if plant_name keys(plant_dict["input"])
# "input" => Dict(), plant_dict["input"][plant_name] = Dict()
# "output" => Dict( end
# "send" => Dict(), plant_dict["input"][plant_name][location_name] = dict
# "dispose" => Dict(), plant_dict["total input"] += val
# ), output["costs"]["transportation"] += dict["transportation cost"]
# "total input" => 0.0, output["costs"]["variable"] += dict["variable operating cost"]
# "total output" => Dict(), end
# "latitude" => location["latitude"],
# "longitude" => location["longitude"],
# "capacity" => round(JuMP.value(model.vars.capacity[process_node]), digits=2)
# )
# plant_loc_dict["fixed cost"] = round(vals[process_node] * process_node.fixed_cost, digits=5) # Outputs
# plant_loc_dict["expansion cost"] = round(JuMP.value(model.vars.expansion[process_node]) * process_node.expansion_cost, digits=5) for shipping_node in plant_to_shipping_nodes[plant]
# output["costs"]["fixed"] += plant_loc_dict["fixed cost"] product_name = shipping_node.product.name
# output["costs"]["expansion"] += plant_loc_dict["expansion cost"] plant_dict["total output"][product_name] = 0.0
plant_dict["output"]["send"][product_name] = product_dict = Dict()
# # Inputs disposal_amount = JuMP.value(vars.dispose[shipping_node])
# for a in process_node.incoming_arcs if disposal_amount > 1e-5
# if vals[a] <= 1e-3 plant_dict["output"]["dispose"][product_name] = disposal_dict = Dict()
# continue disposal_dict["amount"] = JuMP.value(model.vars.dispose[shipping_node])
# end disposal_dict["cost"] = disposal_dict["amount"] * plant.disposal_cost[shipping_node.product]
# skip_plant = skip_location = false plant_dict["total output"][product_name] += disposal_amount
# val = round(vals[a], digits=5) output["costs"]["disposal"] += disposal_dict["cost"]
# if !(a.source.plant_name in keys(plant_loc_dict["input"])) end
# plant_loc_dict["input"][a.source.plant_name] = Dict()
# end
# if a.source.plant_name == "Origin"
# product = instance.products[a.source.product_name]
# source_location = product["initial amounts"][a.source.location_name]
# else
# source_plant = instance.plants[a.source.plant_name]
# source_location = source_plant["locations"][a.source.location_name]
# end
# # Input for a in shipping_node.outgoing_arcs
# cost_transportation = round(a.costs["transportation"] * val, digits=5) val = JuMP.value(vars.flow[a])
# plant_loc_dict["input"][a.source.plant_name][a.source.location_name] = dict = Dict() if val <= 1e-3
# cost_variable = round(a.costs["variable"] * val, digits=5) continue
# dict["amount"] = val end
# dict["distance"] = a.values["distance"] skip_plant = false
# dict["transportation cost"] = cost_transportation dict = Dict(
# dict["variable operating cost"] = cost_variable "amount" => val,
# dict["latitude"] = source_location["latitude"] "distance" => a.values["distance"],
# dict["longitude"] = source_location["longitude"] "latitude" => a.dest.plant.latitude,
# plant_loc_dict["total input"] += val "longitude" => a.dest.plant.longitude,
)
if a.dest.plant.plant_name keys(product_dict)
product_dict[a.dest.plant.plant_name] = Dict()
end
product_dict[a.dest.plant.plant_name][a.dest.plant.location_name] = dict
plant_dict["total output"][product_name] += val
end
end
# output["costs"]["transportation"] += cost_transportation if !skip_plant
# output["costs"]["variable"] += cost_variable if plant.plant_name keys(output["plants"])
# end output["plants"][plant.plant_name] = Dict()
end
# # Outputs output["plants"][plant.plant_name][plant.location_name] = plant_dict
# for output_product_name in keys(plant["outputs"]) end
# plant_loc_dict["total output"][output_product_name] = 0.0 end
# plant_loc_dict["output"]["send"][output_product_name] = product_dict = Dict()
# shipping_node = model.shipping_nodes[output_product_name, plant_name, location_name]
# disposal_amount = JuMP.value(model.vars.dispose[shipping_node])
# if disposal_amount > 1e-5
# plant_loc_dict["output"]["dispose"][output_product_name] = disposal_dict = Dict()
# disposal_dict["amount"] = JuMP.value(model.vars.dispose[shipping_node])
# disposal_dict["cost"] = disposal_dict["amount"] * shipping_node.disposal_cost
# plant_loc_dict["total output"][output_product_name] += disposal_amount
# output["costs"]["disposal"] += disposal_dict["cost"]
# end
# for a in shipping_node.outgoing_arcs
# if vals[a] <= 1e-3
# continue
# end
# skip_plant = skip_location = false
# if !(a.dest.plant_name in keys(product_dict))
# product_dict[a.dest.plant_name] = Dict{Any,Any}()
# end
# dest_location = instance.plants[a.dest.plant_name]["locations"][a.dest.location_name]
# val = round(vals[a], digits=5)
# plant_loc_dict["total output"][output_product_name] += val
# product_dict[a.dest.plant_name][a.dest.location_name] = dict = Dict()
# dict["amount"] = val
# dict["distance"] = a.values["distance"]
# dict["latitude"] = dest_location["latitude"]
# dict["longitude"] = dest_location["longitude"]
# end
# end
# if !skip_location
# plant_dict[location_name] = plant_loc_dict
# end
# end
# if !skip_plant
# output["plants"][plant_name] = plant_dict
# end
# end
# output["costs"]["total"] = sum(values(output["costs"]))
# return output
# end
output["costs"]["total"] = sum(values(output["costs"]))
return output
end

View File

@@ -38,15 +38,21 @@ using ReverseManufacturing, Cbc, JuMP, Printf, JSON
end end
@testset "build" begin @testset "solve" begin
solution = ReverseManufacturing.solve("$(pwd())/../instances/samples/s1.json") solution = ReverseManufacturing.solve("$(pwd())/../instances/samples/s1.json")
# println(JSON.print(solution, 2)) JSON.print(stdout, solution, 4)
# @test "plants" in keys(solution) @test "costs" in keys(solution)
# @test "F1" in keys(solution["plants"]) @test "fixed" in keys(solution["costs"])
# @test "F2" in keys(solution["plants"]) @test "transportation" in keys(solution["costs"])
# @test "F3" in keys(solution["plants"]) @test "variable" in keys(solution["costs"])
# @test "F4" in keys(solution["plants"]) @test "total" in keys(solution["costs"])
@test "plants" in keys(solution)
@test "F1" in keys(solution["plants"])
@test "F2" in keys(solution["plants"])
@test "F3" in keys(solution["plants"])
@test "F4" in keys(solution["plants"])
# @test "L2" in keys(solution["plants"]["F1"]) # @test "L2" in keys(solution["plants"]["F1"])
# @test "total output" in keys(solution["plants"]["F1"]["L2"]) # @test "total output" in keys(solution["plants"]["F1"]["L2"])