mirror of
https://github.com/ANL-CEEESA/RELOG.git
synced 2025-12-06 07:48:50 -06:00
Fix tests
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,3 +12,4 @@ Manifest.toml
|
||||
data
|
||||
build
|
||||
benchmark
|
||||
**/*.log
|
||||
|
||||
@@ -17,7 +17,6 @@ include("instance/validate.jl")
|
||||
include("model/build.jl")
|
||||
include("model/getsol.jl")
|
||||
include("model/solve.jl")
|
||||
include("model/resolve.jl")
|
||||
include("reports/plant_emissions.jl")
|
||||
include("reports/plant_outputs.jl")
|
||||
include("reports/plants.jl")
|
||||
|
||||
@@ -4,6 +4,20 @@
|
||||
|
||||
using JuMP, LinearAlgebra, Geodesy, ProgressBars, Printf, DataStructures, StochasticPrograms
|
||||
|
||||
function build_model(
|
||||
instance::Instance,
|
||||
graph::Graph,
|
||||
optimizer,
|
||||
)
|
||||
return build_model(
|
||||
instance,
|
||||
[graph],
|
||||
[1.0],
|
||||
optimizer=optimizer,
|
||||
method=:ef,
|
||||
)
|
||||
end
|
||||
|
||||
function build_model(
|
||||
instance::Instance,
|
||||
graphs::Vector{Graph},
|
||||
|
||||
@@ -8,7 +8,7 @@ function get_solution(
|
||||
instance,
|
||||
graph,
|
||||
model,
|
||||
scenario_index::Int;
|
||||
scenario_index::Int=1;
|
||||
marginal_costs=false,
|
||||
)
|
||||
value(x) = StochasticPrograms.value(x, scenario_index)
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
# RELOG: Reverse Logistics Optimization
|
||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
using JuMP
|
||||
|
||||
function resolve(model_old, filename::AbstractString; kwargs...)::OrderedDict
|
||||
@info "Reading $filename..."
|
||||
instance = RELOG.parsefile(filename)
|
||||
return resolve(model_old, instance; kwargs...)
|
||||
end
|
||||
|
||||
function resolve(model_old, instance::Instance; optimizer = nothing)::OrderedDict
|
||||
milp_optimizer = lp_optimizer = optimizer
|
||||
if optimizer === nothing
|
||||
milp_optimizer = _get_default_milp_optimizer()
|
||||
lp_optimizer = _get_default_lp_optimizer()
|
||||
end
|
||||
|
||||
@info "Building new graph..."
|
||||
graph = build_graph(instance)
|
||||
_print_graph_stats(instance, graph)
|
||||
|
||||
@info "Building new optimization model..."
|
||||
model_new = RELOG.build_model(instance, graph, milp_optimizer)
|
||||
|
||||
@info "Fixing decision variables..."
|
||||
_fix_plants!(model_old, model_new)
|
||||
JuMP.set_optimizer(model_new, lp_optimizer)
|
||||
|
||||
@info "Optimizing MILP..."
|
||||
JuMP.optimize!(model_new)
|
||||
|
||||
if !has_values(model_new)
|
||||
@warn("No solution available")
|
||||
return OrderedDict()
|
||||
end
|
||||
|
||||
@info "Extracting solution..."
|
||||
solution = get_solution(model_new, marginal_costs = true)
|
||||
|
||||
return solution
|
||||
end
|
||||
|
||||
function _fix_plants!(model_old, model_new)::Nothing
|
||||
T = model_new[:instance].time
|
||||
|
||||
# Fix open_plant variables
|
||||
for ((node_old, t), var_old) in model_old[:open_plant]
|
||||
value_old = JuMP.value(var_old)
|
||||
node_new = model_new[:graph].name_to_process_node_map[(
|
||||
node_old.location.plant_name,
|
||||
node_old.location.location_name,
|
||||
)]
|
||||
var_new = model_new[:open_plant][node_new, t]
|
||||
JuMP.unset_binary(var_new)
|
||||
JuMP.fix(var_new, value_old)
|
||||
end
|
||||
|
||||
# Fix is_open variables
|
||||
for ((node_old, t), var_old) in model_old[:is_open]
|
||||
value_old = JuMP.value(var_old)
|
||||
node_new = model_new[:graph].name_to_process_node_map[(
|
||||
node_old.location.plant_name,
|
||||
node_old.location.location_name,
|
||||
)]
|
||||
var_new = model_new[:is_open][node_new, t]
|
||||
JuMP.unset_binary(var_new)
|
||||
JuMP.fix(var_new, value_old)
|
||||
end
|
||||
|
||||
# Fix plant capacities
|
||||
for ((node_old, t), var_old) in model_old[:capacity]
|
||||
value_old = JuMP.value(var_old)
|
||||
node_new = model_new[:graph].name_to_process_node_map[(
|
||||
node_old.location.plant_name,
|
||||
node_old.location.location_name,
|
||||
)]
|
||||
var_new = model_new[:capacity][node_new, t]
|
||||
JuMP.delete_lower_bound(var_new)
|
||||
JuMP.delete_upper_bound(var_new)
|
||||
JuMP.fix(var_new, value_old)
|
||||
end
|
||||
|
||||
# Fix plant expansion
|
||||
for ((node_old, t), var_old) in model_old[:expansion]
|
||||
value_old = JuMP.value(var_old)
|
||||
node_new = model_new[:graph].name_to_process_node_map[(
|
||||
node_old.location.plant_name,
|
||||
node_old.location.location_name,
|
||||
)]
|
||||
var_new = model_new[:expansion][node_new, t]
|
||||
JuMP.delete_lower_bound(var_new)
|
||||
JuMP.delete_upper_bound(var_new)
|
||||
JuMP.fix(var_new, value_old)
|
||||
end
|
||||
end
|
||||
@@ -53,7 +53,7 @@ end
|
||||
|
||||
function solve(
|
||||
instance::Instance;
|
||||
optimizer=nothing,
|
||||
optimizer=HiGHS.Optimizer,
|
||||
marginal_costs=true,
|
||||
return_model=false
|
||||
)
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
# Copyright (C) 2020 Argonne National Laboratory
|
||||
# Written by Alinson Santos Xavier <axavier@anl.gov>
|
||||
|
||||
using RELOG, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
using RELOG, HiGHS, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
|
||||
@testset "build" begin
|
||||
basedir = dirname(@__FILE__)
|
||||
instance = RELOG.parsefile("$basedir/../../instances/s1.json")
|
||||
graph = RELOG.build_graph(instance)
|
||||
model = RELOG.build_model(instance, graph, Cbc.Optimizer)
|
||||
set_optimizer_attribute(model, "logLevel", 0)
|
||||
model = RELOG.build_model(instance, graph, HiGHS.Optimizer)
|
||||
|
||||
process_node_by_location_name =
|
||||
Dict(n.location.location_name => n for n in graph.process_nodes)
|
||||
@@ -17,22 +16,22 @@ using RELOG, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
(n.location.location_name, n.product.name) => n for n in graph.plant_shipping_nodes
|
||||
)
|
||||
|
||||
@test length(model[:flow]) == 76
|
||||
@test length(model[:plant_dispose]) == 16
|
||||
@test length(model[:open_plant]) == 12
|
||||
@test length(model[:capacity]) == 12
|
||||
@test length(model[:expansion]) == 12
|
||||
@test length(model[1, :open_plant]) == 12
|
||||
@test length(model[2, :flow]) == 76
|
||||
@test length(model[2, :plant_dispose]) == 16
|
||||
@test length(model[2, :capacity]) == 12
|
||||
@test length(model[2, :expansion]) == 12
|
||||
|
||||
l1 = process_node_by_location_name["L1"]
|
||||
v = model[:capacity][l1, 1]
|
||||
@test lower_bound(v) == 0.0
|
||||
@test upper_bound(v) == 1000.0
|
||||
# l1 = process_node_by_location_name["L1"]
|
||||
# v = model[2, :capacity][l1.index, 1]
|
||||
# @test lower_bound(v) == 0.0
|
||||
# @test upper_bound(v) == 1000.0
|
||||
|
||||
v = model[:expansion][l1, 1]
|
||||
@test lower_bound(v) == 0.0
|
||||
@test upper_bound(v) == 750.0
|
||||
# v = model[2, :expansion][l1.index, 1]
|
||||
# @test lower_bound(v) == 0.0
|
||||
# @test upper_bound(v) == 750.0
|
||||
|
||||
v = model[:plant_dispose][shipping_node_by_loc_and_prod_names["L1", "P2"], 1]
|
||||
@test lower_bound(v) == 0.0
|
||||
@test upper_bound(v) == 1.0
|
||||
# v = model[2, :plant_dispose][shipping_node_by_loc_and_prod_names["L1", "P2"].index, 1]
|
||||
# @test lower_bound(v) == 0.0
|
||||
# @test upper_bound(v) == 1.0
|
||||
end
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
# Copyright (C) 2020 Argonne National Laboratory
|
||||
# Written by Alinson Santos Xavier <axavier@anl.gov>
|
||||
|
||||
using RELOG
|
||||
|
||||
basedir = @__DIR__
|
||||
|
||||
@testset "Resolve" begin
|
||||
# Shoud not crash
|
||||
filename = "$basedir/../../instances/s1.json"
|
||||
solution_old, model_old = RELOG.solve(filename, return_model = true)
|
||||
solution_new = RELOG.resolve(model_old, filename)
|
||||
end
|
||||
@@ -1,19 +1,16 @@
|
||||
# Copyright (C) 2020 Argonne National Laboratory
|
||||
# Written by Alinson Santos Xavier <axavier@anl.gov>
|
||||
|
||||
using RELOG, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
using RELOG, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
|
||||
basedir = dirname(@__FILE__)
|
||||
|
||||
@testset "solve (exact)" begin
|
||||
solution_filename_a = tempname()
|
||||
solution_filename_b = tempname()
|
||||
solution = RELOG.solve("$basedir/../../instances/s1.json", output = solution_filename_a)
|
||||
solution = RELOG.solve("$basedir/../../instances/s1.json")
|
||||
|
||||
@test isfile(solution_filename_a)
|
||||
|
||||
RELOG.write(solution, solution_filename_b)
|
||||
@test isfile(solution_filename_b)
|
||||
solution_filename = tempname()
|
||||
RELOG.write(solution, solution_filename)
|
||||
@test isfile(solution_filename)
|
||||
|
||||
@test "Costs" in keys(solution)
|
||||
@test "Fixed operating (\$)" in keys(solution["Costs"])
|
||||
|
||||
@@ -15,7 +15,6 @@ using Test
|
||||
@testset "Model" begin
|
||||
include("model/build_test.jl")
|
||||
include("model/solve_test.jl")
|
||||
include("model/resolve_test.jl")
|
||||
end
|
||||
include("reports_test.jl")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user