From dde0d40282539934bad4d385d0b824d83eeb483a Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Wed, 9 Nov 2022 10:11:48 -0600 Subject: [PATCH] Fix tests --- .gitignore | 1 + src/RELOG.jl | 1 - src/model/build.jl | 14 ++++++ src/model/getsol.jl | 2 +- src/model/resolve.jl | 97 -------------------------------------- src/model/solve.jl | 2 +- test/model/build_test.jl | 35 +++++++------- test/model/resolve_test.jl | 13 ----- test/model/solve_test.jl | 13 ++--- test/runtests.jl | 1 - 10 files changed, 39 insertions(+), 140 deletions(-) delete mode 100644 src/model/resolve.jl delete mode 100644 test/model/resolve_test.jl diff --git a/.gitignore b/.gitignore index 56f7226..39b2eee 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Manifest.toml data build benchmark +**/*.log diff --git a/src/RELOG.jl b/src/RELOG.jl index d8d4226..d202938 100644 --- a/src/RELOG.jl +++ b/src/RELOG.jl @@ -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") diff --git a/src/model/build.jl b/src/model/build.jl index 176b102..7e668b4 100644 --- a/src/model/build.jl +++ b/src/model/build.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}, diff --git a/src/model/getsol.jl b/src/model/getsol.jl index b3117fc..09bcafc 100644 --- a/src/model/getsol.jl +++ b/src/model/getsol.jl @@ -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) diff --git a/src/model/resolve.jl b/src/model/resolve.jl deleted file mode 100644 index 6cd7862..0000000 --- a/src/model/resolve.jl +++ /dev/null @@ -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 diff --git a/src/model/solve.jl b/src/model/solve.jl index 81af576..fdd4177 100644 --- a/src/model/solve.jl +++ b/src/model/solve.jl @@ -53,7 +53,7 @@ end function solve( instance::Instance; - optimizer=nothing, + optimizer=HiGHS.Optimizer, marginal_costs=true, return_model=false ) diff --git a/test/model/build_test.jl b/test/model/build_test.jl index 5dc554a..e85f6a2 100644 --- a/test/model/build_test.jl +++ b/test/model/build_test.jl @@ -1,14 +1,13 @@ # Copyright (C) 2020 Argonne National Laboratory # Written by Alinson Santos Xavier -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 diff --git a/test/model/resolve_test.jl b/test/model/resolve_test.jl deleted file mode 100644 index 9021000..0000000 --- a/test/model/resolve_test.jl +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright (C) 2020 Argonne National Laboratory -# Written by Alinson Santos Xavier - -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 diff --git a/test/model/solve_test.jl b/test/model/solve_test.jl index 0f03bd4..0d63874 100644 --- a/test/model/solve_test.jl +++ b/test/model/solve_test.jl @@ -1,19 +1,16 @@ # Copyright (C) 2020 Argonne National Laboratory # Written by Alinson Santos Xavier -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"]) diff --git a/test/runtests.jl b/test/runtests.jl index 3fcf070..9012e6b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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