From 99975db5cddf30b2e872ca8aae21e79eba90e559 Mon Sep 17 00:00:00 2001 From: Alinson S Xavier Date: Thu, 27 May 2021 18:01:05 -0500 Subject: [PATCH] Implement UnitCommitment.write --- docs/usage.md | 8 ++++---- src/model.jl | 11 +++++++++-- test/model_test.jl | 9 ++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index a670b66..447757e 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -52,11 +52,11 @@ model = UnitCommitment.build_model( # Solve model UnitCommitment.optimize!(model) -# Extract solution and write it to a file +# Extract solution solution = UnitCommitment.solution(model) -open("/path/to/output.json", "w") do file - JSON.print(file, solution, 2) -end + +# Write solution to a file +UnitCommitment.write("/path/to/output.json", solution) ``` ### Solving benchmark instances diff --git a/src/model.jl b/src/model.jl index 136359e..ac69979 100644 --- a/src/model.jl +++ b/src/model.jl @@ -578,7 +578,14 @@ function solution(model::JuMP.Model) end -function fix!(model::JuMP.Model, solution)::Nothing +function write(filename::AbstractString, solution::AbstractDict)::Nothing + open(filename, "w") do file + JSON.print(file, solution, 2) + end +end + + +function fix!(model::JuMP.Model, solution::AbstractDict)::Nothing instance, T = model[:instance], model[:instance].time is_on = model[:is_on] prod_above = model[:prod_above] @@ -600,7 +607,7 @@ function fix!(model::JuMP.Model, solution)::Nothing end -function set_warm_start!(model::JuMP.Model, solution)::Nothing +function set_warm_start!(model::JuMP.Model, solution::AbstractDict)::Nothing instance, T = model[:instance], model[:instance].time is_on = model[:is_on] prod_above = model[:prod_above] diff --git a/test/model_test.jl b/test/model_test.jl index 1257173..c736489 100644 --- a/test/model_test.jl +++ b/test/model_test.jl @@ -16,14 +16,17 @@ using UnitCommitment, LinearAlgebra, Cbc, JuMP optimizer=optimizer, variable_names=true, ) - @test name(model[:is_on]["g1", 1]) == "is_on[g1,1]" - JuMP.write_to_file(model, "test.mps") - # Optimize and retrieve solution UnitCommitment.optimize!(model) solution = UnitCommitment.solution(model) + + # Write solution to a file + filename = tempname() + UnitCommitment.write(filename, solution) + loaded = JSON.parsefile(filename) + @test length(loaded["Is on"]) == 6 # Verify solution @test UnitCommitment.validate(instance, solution)