Skip to content

Commit

Permalink
Add template problem and tests for maximal demand delivery specificat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
tasseff committed Sep 16, 2021
1 parent 683e5e1 commit 1ef7ad9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/WaterModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ include("form/lrd.jl")
include("prob/wf.jl")
include("prob/owf.jl")
include("prob/des.jl")
include("prob/mdd.jl")

include("util/relax.jl")
include("util/variable_index.jl")
Expand Down
38 changes: 38 additions & 0 deletions src/prob/mdd.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function solve_mdd(network, model_constructor, optimizer; kwargs...)
return solve_model(network, model_constructor, optimizer, build_mdd; kwargs...)
end


function run_mdd(network, model_constructor, optimizer; kwargs...)
Memento.warn(_LOGGER, "\"run_\" methods should be renamed \"solve_\" and will be deprecated in future versions.")
return solve_owf(network, model_constructor, optimizer; kwargs...)
end


function solve_mn_mdd(file, model_constructor, optimizer; kwargs...)
return solve_model(file, model_constructor, optimizer, build_mn_mdd; multinetwork=true, kwargs...)
end


function run_mn_mdd(network, model_constructor, optimizer; kwargs...)
Memento.warn(_LOGGER, "\"run_\" methods should be renamed \"solve_\" and will be deprecated in future versions.")
return solve_mn_mdd(network, model_constructor, optimizer; kwargs...)
end


function build_mdd(wm::AbstractWaterModel)
# Build the water flow problem.
build_wf(wm)

# Add the demand maximization objective.
objective_max_demand(wm)
end


function build_mn_mdd(wm::AbstractWaterModel)
# Build the water flow problem.
build_mn_wf(wm)

# Add the demand maximization objective.
objective_max_demand(wm)
end
30 changes: 30 additions & 0 deletions test/mdd.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Iterate over all possible WaterModels formulations.
for formulation in [NCWaterModel, NCDWaterModel, CRDWaterModel, LAWaterModel, LRDWaterModel, PWLRDWaterModel]
@testset "Maximal Demand Delivery Problems (Single Network): $(formulation)" begin
network = WaterModels.parse_file("../test/data/epanet/snapshot/pump-hw-lps.inp")
set_flow_partitions_si!(network, 10.0, 1.0e-4)
t_h = WaterModels._calc_head_per_unit_transform(network)

wm = instantiate_model(network, formulation, build_mdd)
result = WaterModels.optimize_model!(wm, optimizer = _choose_solver(wm, ipopt, cbc))

@test _is_valid_status(result["termination_status"])
@test isapprox(result["solution"]["node"]["1"]["h"], t_h(10.0), rtol = 1.0e-3)
@test isapprox(result["solution"]["pump"]["1"]["status"], 1.0, atol = 1.0e-3)
@test result["solution"]["node"]["2"]["h"] > t_h(10.0)

# In some relaxations, the objective may be near zero.
@test result["objective"] >= -1.0e-6
end

@testset "Maximal Demand Delivery Problems (Multinetwork): $(formulation)" begin
network = WaterModels.parse_file("../test/data/epanet/multinetwork/owf-hw-lps.inp")
network_mn = WaterModels.make_multinetwork(network)
set_flow_partitions_si!(network_mn, 10.0, 1.0e-4)

wm = instantiate_model(network_mn, formulation, build_mn_mdd)
result = WaterModels.optimize_model!(wm, optimizer = _choose_solver(wm, ipopt, cbc))

@test _is_valid_status(result["termination_status"])
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ include("common.jl")

include("des.jl")

include("mdd.jl")

include("relax.jl")

include("variable_index.jl")
Expand Down

0 comments on commit 1ef7ad9

Please sign in to comment.