Skip to content

Commit

Permalink
Fix tolerance calculation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbonaldo committed Nov 1, 2023
1 parent 4e3ef50 commit bf40ed4
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 147 deletions.
16 changes: 5 additions & 11 deletions test/Electrolyzer/highs_settings.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# HiGHS Solver Parameters
# Common solver settings
Feasib_Tol: 1.0e-04 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-04 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm #HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]

#highs-specific solver settings

# run the crossover routine for ipx
# [type: string, advanced: "on", range: {"off", "on"}, default: "off"]
run_crossover: "on"
Feasib_Tol: 1.0e-04 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm # HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
ipm_optimality_tolerance: 1e-04
15 changes: 6 additions & 9 deletions test/MethodofMorris/highs_settings.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# HiGHS Solver Parameters
# Common solver settings
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: choose # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm #HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]

# run the crossover routine for ipx
# [type: string, advanced: "on", range: {"off", "on"}, default: "off"]
run_crossover: "off"
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm # HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
ipm_optimality_tolerance: 1e-04
11 changes: 6 additions & 5 deletions test/MultiStage/highs_settings.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# HiGHS Solver Parameters
# Common solver settings
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: choose # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm #HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm # HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
ipm_optimality_tolerance: 1e-04

# run the crossover routine for ipx
# [type: string, advanced: "on", range: {"off", "on"}, default: "off"]
Expand Down
16 changes: 5 additions & 11 deletions test/PiecewiseFuel_CO2/highs_settings.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# HiGHS Solver Parameters
# Common solver settings
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: choose # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: choose #HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]

#highs-specific solver settings

# run the crossover routine for ipx
# [type: string, advanced: "on", range: {"off", "on"}, default: "off"]
run_crossover: "off"
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: simplex # HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
10 changes: 5 additions & 5 deletions test/ThreeZones/highs_settings.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# HiGHS Solver Parameters
# Common solver settings
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: choose # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm #HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: ipm # HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
ipm_optimality_tolerance: 1e-04

# run the crossover routine for ipx
# [type: string, advanced: "on", range: {"off", "on"}, default: "off"]
Expand Down
15 changes: 5 additions & 10 deletions test/VREStor/highs_settings.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
# HiGHS Solver Parameters
# Common solver settings
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: choose # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: choose #HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]
Feasib_Tol: 1.0e-05 # Primal feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
Optimal_Tol: 1.0e-05 # Dual feasibility tolerance # [type: double, advanced: false, range: [1e-10, inf], default: 1e-07]
TimeLimit: 1.0e23 # Time limit # [type: double, advanced: false, range: [0, inf], default: inf]
Pre_Solve: on # Presolve option: "off", "choose" or "on" # [type: string, advanced: false, default: "choose"]
Method: simplex # HiGHS-specific solver settings # Solver option: "simplex", "choose" or "ipm" # [type: string, advanced: false, default: "choose"]

#highs-specific solver settings

# run the crossover routine for ipx
# [type: string, advanced: "on", range: {"off", "on"}, default: "off"]
run_crossover: "on"
8 changes: 2 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ end
include("expression_manipulation_test.jl")
end

@testset "Utilities" begin
include("test_utilities.jl")
end

# Test GenX modules
@testset verbose=true "GenX modules" begin
@testset verbose = true "GenX modules" begin
@testset "Three zones" begin
include("test_threezones.jl")
end
Expand All @@ -33,7 +29,7 @@ end

@testset "PiecewiseFuel CO2" begin
include("test_piecewisefuel_CO2.jl")
end
end

@testset "VRE and storage" begin
include("test_VREStor.jl")
Expand Down
15 changes: 7 additions & 8 deletions test/test_VREStor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ genx_setup = Dict(
"ParameterScale" => 1,
"Trans_Loss_Segments" => 1,
"CapacityReserveMargin" => 1,
"ModelingtoGenerateAlternativeSlack: 0" =>1,
"ModelingtoGenerateAlternativeSlack: 0" => 1,
"Solver" => "HiGHS",
"ModelingToGenerateAlternatives" => 0,
"WriteShadowPrices" => 1,
Expand All @@ -37,17 +37,16 @@ genx_setup = Dict(
EP, _, _ = redirect_stdout(devnull) do
run_genx_case_testing(test_path, genx_setup)
end

obj_test = objective_value(EP)
optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")

# Round the objective value to the same number of digits as the tolerance
obj_test = round_objfromtol!(obj_test, optimal_tol)
optimal_tol_rel = get_attribute(EP, "dual_feasibility_tolerance")
optimal_tol = optimal_tol_rel * obj_test # Convert to absolute tolerance

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
# Round objective value and tolerance. Write to test log.
obj_test = round_from_tol!(obj_test, optimal_tol)
optimal_tol = round_from_tol!(optimal_tol, optimal_tol)
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestVREStor
Expand Down
15 changes: 7 additions & 8 deletions test/test_electrolyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Test

include(joinpath(@__DIR__, "utilities.jl"))

obj_true = 6946.9819
obj_true = 6946.9618
test_path = "Electrolyzer"

# Define test inputs
Expand Down Expand Up @@ -34,17 +34,16 @@ genx_setup = Dict(
EP, _, _ = redirect_stdout(devnull) do
run_genx_case_testing(test_path, genx_setup)
end

obj_test = objective_value(EP)
optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")

# Round the objective value to the same number of digits as the tolerance
obj_test = round_objfromtol!(obj_test, optimal_tol)
optimal_tol_rel = get_attribute(EP, "ipm_optimality_tolerance")
optimal_tol = optimal_tol_rel * obj_test # Convert to absolute tolerance

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
# Round objective value and tolerance. Write to test log.
obj_test = round_from_tol!(obj_test, optimal_tol)
optimal_tol = round_from_tol!(optimal_tol, optimal_tol)
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestElectrolyzer
11 changes: 4 additions & 7 deletions test/test_methodofmorris.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,21 @@ genx_setup = Dict(
)

# Run the case and check if the model was built
built = false
built_and_run = false
try
Morris_range = redirect_stdout(devnull) do
EP, inputs, OPTIMIZER = run_genx_case_testing(test_path, genx_setup)
morris(EP, test_path, genx_setup, inputs, test_path, OPTIMIZER)
rm(joinpath(@__DIR__, test_path, "morris.csv"))
end
#TODO: test Morris range
built = true
global built_and_run = true

catch BoundsError
end

@static if VERSION VersionNumber(1, 7)
test_result = Test.@test built broken = true
else
test_result = built ? "Test Passed" : "Test Failed"
end
# Test if the
test_result = Test.@test built_and_run

# Add the results to the test log
write_testlog(test_path, "Build and Run", test_result)
Expand Down
19 changes: 9 additions & 10 deletions test/test_multistage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ using Test

include(joinpath(@__DIR__, "utilities.jl"))

obj_true = [79734.80032, 41630.03494, 27855.20632]
obj_true = [79734.80032, 41630.03494, 27855.20631]
test_path = "MultiStage"

# Define test inputs
multistage_setup = Dict(
"NumStages" => 3 ,
"StageLengths" => [10,10,10],
"NumStages" => 3,
"StageLengths" => [10, 10, 10],
"WACC" => 0.045,
"ConvergenceTolerance" => 0.01,
"Myopic" => 0,
Expand Down Expand Up @@ -47,17 +47,16 @@ genx_setup = Dict(
EP, _, _ = redirect_stdout(devnull) do
run_genx_case_testing(test_path, genx_setup)
end

obj_test = objective_value.([EP[i] for i in 1:3])
optimal_tol = get_attribute.([EP[i] for i in 1:3], "dual_feasibility_tolerance")

# Round the objective value to the same number of digits as the tolerance
obj_test = round_objfromtol!.(obj_test, optimal_tol)
obj_test = objective_value.(EP[i] for i in 1:multistage_setup["NumStages"])
optimal_tol_rel = get_attribute.((EP[i] for i in 1:multistage_setup["NumStages"]), "ipm_optimality_tolerance")
optimal_tol = optimal_tol_rel .* obj_test # Convert to absolute tolerance

# Test the objective value
test_result = @test all(obj_true .- optimal_tol .<= obj_test .<= obj_true .+ optimal_tol)

# Add the results to the test log
# Round objective value and tolerance. Write to test log.
obj_test = round_from_tol!.(obj_test, optimal_tol)
optimal_tol = round_from_tol!.(optimal_tol, optimal_tol)
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestMultiStage
13 changes: 6 additions & 7 deletions test/test_piecewisefuel_CO2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ genx_setup = Dict(
EP, _, _ = redirect_stdout(devnull) do
run_genx_case_testing(test_path, genx_setup)
end

obj_test = objective_value(EP)
optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")

# Round the objective value to the same number of digits as the tolerance
obj_test = round_objfromtol!(obj_test, optimal_tol)
optimal_tol_rel = get_attribute(EP, "dual_feasibility_tolerance")
optimal_tol = optimal_tol_rel * obj_test # Convert to absolute tolerance

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
# Round objective value and tolerance. Write to test log.
obj_test = round_from_tol!(obj_test, optimal_tol)
optimal_tol = round_from_tol!(optimal_tol, optimal_tol)
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestPiecewiseFuelCO2
Expand Down
17 changes: 8 additions & 9 deletions test/test_threezones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ genx_setup = Dict(
"CapacityReserveMargin" => 0,
"CO2Cap" => 2,
"StorageLosses" => 1,
"MinCapReq" => 1 ,
"MaxCapReq" => 0 ,
"MinCapReq" => 1,
"MaxCapReq" => 0,
"Solver" => "HiGHS",
"ParameterScale" => 1,
"WriteShadowPrices" => 1,
Expand All @@ -39,17 +39,16 @@ genx_setup = Dict(
EP, _, _ = redirect_stdout(devnull) do
run_genx_case_testing(test_path, genx_setup)
end

obj_test = objective_value(EP)
optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")

# Round the objective value to the same number of digits as the tolerance
obj_test = round_objfromtol!(obj_test, optimal_tol)
optimal_tol_rel = get_attribute(EP, "ipm_optimality_tolerance")
optimal_tol = optimal_tol_rel * obj_test # Convert to absolute tolerance

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
# Round objective value and tolerance. Write to test log.
obj_test = round_from_tol!(obj_test, optimal_tol)
optimal_tol = round_from_tol!(optimal_tol, optimal_tol)
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestThreeZones
39 changes: 0 additions & 39 deletions test/test_utilities.jl

This file was deleted.

4 changes: 2 additions & 2 deletions test/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function get_exponent_sciform(number::Real)
return number == 0.0 ? 0 : Int(floor(log10(abs(number))))
end

function round_objfromtol!(obj::Real, tol::Real)
function round_from_tol!(obj::Real, tol::Real)
# Round the objective value to the same number of digits as the tolerance
return round(obj, digits=(-1)*get_exponent_sciform(tol))
return round(obj, digits=(-1) * get_exponent_sciform(tol))
end

0 comments on commit bf40ed4

Please sign in to comment.