Move graph creation to graph.jl; simplify model.jl

This commit is contained in:
2020-05-03 16:04:57 -05:00
parent aa4f6537fe
commit cbd7bc5247
8 changed files with 458 additions and 484 deletions

42
test/graph_test.jl Normal file
View File

@@ -0,0 +1,42 @@
# Copyright (C) 2020 Argonne National Laboratory
# Written by Alinson Santos Xavier <axavier@anl.gov>
using ReverseManufacturing
@testset "Graph" begin
@testset "build_graph" begin
basedir = dirname(@__FILE__)
instance = ReverseManufacturing.load("$basedir/../instances/samples/s1.json")
graph = ReverseManufacturing.build_graph(instance)
process_node_by_location_name = Dict(n.plant.location_name => n
for n in graph.process_nodes)
@test length(graph.plant_shipping_nodes) == 8
@test length(graph.collection_shipping_nodes) == 10
@test length(graph.process_nodes) == 6
node = graph.collection_shipping_nodes[1]
@test node.location.name == "C1"
@test length(node.incoming_arcs) == 0
@test length(node.outgoing_arcs) == 2
@test node.outgoing_arcs[1].source.location.name == "C1"
@test node.outgoing_arcs[1].dest.plant.plant_name == "F1"
@test node.outgoing_arcs[1].dest.plant.location_name == "L1"
@test node.outgoing_arcs[1].values["distance"] == 1095.62
node = process_node_by_location_name["L1"]
@test node.plant.plant_name == "F1"
@test node.plant.location_name == "L1"
@test length(node.incoming_arcs) == 10
@test length(node.outgoing_arcs) == 2
node = process_node_by_location_name["L3"]
@test node.plant.plant_name == "F2"
@test node.plant.location_name == "L3"
@test length(node.incoming_arcs) == 2
@test length(node.outgoing_arcs) == 2
@test length(graph.arcs) == 38
end
end

View File

@@ -12,11 +12,9 @@ using ReverseManufacturing
plants = instance.plants
products = instance.products
plant_name_to_plant = Dict(p.name => p for p in plants)
location_name_to_plant = Dict(p.location_name => p for p in plants)
product_name_to_product = Dict(p.name => p for p in products)
p2 = product_name_to_product["P2"]
p3 = product_name_to_product["P3"]
@test length(centers) == 10
@test centers[1].name == "C1"
@@ -28,8 +26,9 @@ using ReverseManufacturing
@test length(plants) == 6
plant = plant_name_to_plant["L1"]
@test plant.name == "L1"
plant = location_name_to_plant["L1"]
@test plant.plant_name == "F1"
@test plant.location_name == "L1"
@test plant.input.name == "P1"
@test plant.latitude == 0
@test plant.longitude == 0
@@ -40,26 +39,33 @@ using ReverseManufacturing
@test plant.max_capacity == 1000
@test plant.expansion_cost == 1
p2 = product_name_to_product["P2"]
p3 = product_name_to_product["P3"]
@test length(plant.output) == 2
@test plant.output[p2] == 0.2
@test plant.output[p3] == 0.5
@test plant.disposal_limit[p2] == 1
@test plant.disposal_limit[p3] == 1
@test plant.disposal_cost[p2] == -10
@test plant.disposal_cost[p3] == -10
@test length(plant.disposal) == 2
@test plant.disposal[1].product.name == "P2"
@test plant.disposal[1].cost == -10
@test plant.disposal[1].limit == 1
plant = plant_name_to_plant["L3"]
@test plant.name == "L3"
plant = location_name_to_plant["L3"]
@test plant.location_name == "L3"
@test plant.input.name == "P2"
@test plant.latitude == 25
@test plant.longitude == 65
@test plant.opening_cost == 3000
@test plant.fixed_operating_cost == 50
@test plant.variable_operating_cost == 50
@test plant.base_capacity == Inf
@test plant.max_capacity == Inf
@test plant.base_capacity == 1e8
@test plant.max_capacity == 1e8
@test plant.expansion_cost == 0
p4 = product_name_to_product["P4"]
@test plant.output[p3] == 0.05
@test plant.output[p4] == 0.8
@test plant.disposal_limit[p3] == 0.0
@test plant.disposal_limit[p4] == 0.0
end
end

View File

@@ -4,61 +4,54 @@
using ReverseManufacturing, Cbc, JuMP, Printf, JSON
@testset "Model" begin
instance = ReverseManufacturing.load("samples/s1")
model = ReverseManufacturing.build_model(instance, Cbc.Optimizer)
# Verify nodes
@test ("P1", "Origin", "C1") in keys(model.shipping_nodes)
@test ("P1", "Origin", "C3") in keys(model.shipping_nodes)
@test ("P1", "Origin", "C8") in keys(model.shipping_nodes)
@test ("P2", "F1", "L1") in keys(model.shipping_nodes)
@test ("P2", "F1", "L2") in keys(model.shipping_nodes)
@test ("P3", "F1", "L1") in keys(model.shipping_nodes)
@test ("P3", "F1", "L2") in keys(model.shipping_nodes)
@test ("P3", "F2", "L3") in keys(model.shipping_nodes)
@test ("P3", "F2", "L4") in keys(model.shipping_nodes)
@test ("P4", "F2", "L3") in keys(model.shipping_nodes)
@test ("P4", "F2", "L4") in keys(model.shipping_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.shipping_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) == 16.43
@test arc.costs["variable"] == 30.0
p2_f1_l1 = model.shipping_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)
@testset "build" begin
basedir = dirname(@__FILE__)
instance = ReverseManufacturing.load("$basedir/../instances/samples/s1.json")
graph = ReverseManufacturing.build_graph(instance)
model = ReverseManufacturing.build_model(instance, graph, Cbc.Optimizer)
process_node_by_location_name = Dict(n.plant.location_name => n
for n in graph.process_nodes)
shipping_node_by_location_and_product_names = Dict((n.location.location_name, n.product.name) => n
for n in graph.plant_shipping_nodes)
@test length(model.vars.flow) == 38
@test length(model.vars.dispose) == 8
@test length(model.vars.open_plant) == 6
@test length(model.vars.capacity) == 6
@test length(model.vars.expansion) == 6
l1 = process_node_by_location_name["L1"]
v = model.vars.capacity[l1]
@test lower_bound(v) == 0.0
@test upper_bound(v) == 1000.0
v = model.vars.expansion[l1]
@test lower_bound(v) == 0.0
@test upper_bound(v) == 750.0
v = model.vars.dispose[shipping_node_by_location_and_product_names["L1", "P2"]]
@test lower_bound(v) == 0.0
@test upper_bound(v) == 1.0
end
@testset "build" begin
solution = ReverseManufacturing.solve("$(pwd())/../instances/samples/s1.json")
# println(JSON.print(solution, 2))
# @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 "total output" in keys(solution["plants"]["F1"]["L2"])
# @test "capacity" in keys(solution["plants"]["F1"]["L1"])
end
end
@testset "Solve" begin
solution = ReverseManufacturing.solve("$(pwd())/../instances/samples/s1.json")
println(JSON.print(solution, 2))
@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 "total output" in keys(solution["plants"]["F1"]["L2"])
@test "capacity" in keys(solution["plants"]["F1"]["L1"])
end

View File

@@ -5,5 +5,6 @@ using Test
@testset "ReverseManufacturing" begin
include("instance_test.jl")
#include("model_test.jl")
include("graph_test.jl")
include("model_test.jl")
end