Skip to content

Commit b80f4fe

Browse files
committed
add fix for when result count is zero
1 parent 124e9da commit b80f4fe

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ PowerModels.jl Change Log
77
- Added iterative flow limit cut OPF solvers (#619)
88
- Improved connected components computation time (#504)
99
- Removed `Inf` bounds from variables (#630)
10-
- Removal of unused functions in `solution.jl`: `get_solution`, `add_generator_power_setpoint`, `add_storage_setpoint`, `add_branch_flow_setpoint`, `add_dcline_flow_setpoint` (breaking) (#637)
10+
- Removal of unused functions in `solution.jl`: `get_solution`, `add_generator_power_setpoint`, `add_storage_setpoint`, `add_branch_flow_setpoint`, `add_dcline_flow_setpoint` (breaking) (#637)
11+
- Fixed a solution reporting bug when the optimizer result count is zero
1112

1213
### v0.13.1
1314
- Added DCMPPowerModel for replication of Matpower's DC model (#612)

src/core/solution.jl

+35-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
11
""
22
function build_solution(pm::AbstractPowerModel, solve_time; solution_builder=solution_opf!)
3-
# TODO @assert that the model is solved
4-
53
sol = _init_solution(pm)
6-
data = Dict{String,Any}("name" => pm.data["name"])
74

5+
# TODO replace with JuMP.result_count(pm.model) after version v0.21
6+
# try-catch is needed until solvers reliably support ResultCount()
7+
result_count = 1
8+
try
9+
result_count = _MOI.get(pm.model, _MOI.ResultCount())
10+
catch
11+
Memento.warn(_LOGGER, "the given optimizer does not provide the ResultCount() attribute, assuming the solver returned a solution which may be incorrect.");
12+
end
13+
14+
if result_count > 0
15+
if InfrastructureModels.ismultinetwork(pm.data)
16+
sol["multinetwork"] = true
17+
sol_nws = sol["nw"] = Dict{String,Any}()
18+
19+
for (n,nw_data) in pm.data["nw"]
20+
sol_nw = sol_nws[n] = Dict{String,Any}()
21+
sol_nw["baseMVA"] = nw_data["baseMVA"]
22+
if haskey(nw_data, "conductors")
23+
sol_nw["conductors"] = nw_data["conductors"]
24+
end
25+
pm.cnw = parse(Int, n)
26+
solution_builder(pm, sol_nw)
27+
end
28+
else
29+
sol["baseMVA"] = pm.data["baseMVA"]
30+
if haskey(pm.data, "conductors")
31+
sol["conductors"] = pm.data["conductors"]
32+
end
33+
solution_builder(pm, sol)
34+
end
35+
else
36+
Memento.warn(_LOGGER, "model has no results, solution cannot be built")
37+
end
38+
39+
data = Dict{String,Any}("name" => pm.data["name"])
840
if InfrastructureModels.ismultinetwork(pm.data)
9-
sol["multinetwork"] = true
10-
sol_nws = sol["nw"] = Dict{String,Any}()
1141
data_nws = data["nw"] = Dict{String,Any}()
1242

1343
for (n,nw_data) in pm.data["nw"]
14-
sol_nw = sol_nws[n] = Dict{String,Any}()
15-
sol_nw["baseMVA"] = nw_data["baseMVA"]
16-
if haskey(nw_data, "conductors")
17-
sol_nw["conductors"] = nw_data["conductors"]
18-
end
19-
pm.cnw = parse(Int, n)
20-
solution_builder(pm, sol_nw)
2144
data_nws[n] = Dict(
2245
"name" => get(nw_data, "name", "anonymous"),
2346
"bus_count" => length(nw_data["bus"]),
2447
"branch_count" => length(nw_data["branch"])
2548
)
2649
end
2750
else
28-
sol["baseMVA"] = pm.data["baseMVA"]
29-
if haskey(pm.data, "conductors")
30-
sol["conductors"] = pm.data["conductors"]
31-
end
32-
solution_builder(pm, sol)
3351
data["bus_count"] = length(pm.data["bus"])
3452
data["branch_count"] = length(pm.data["branch"])
3553
end

test/output.jl

+22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
@test length(result["solution"]["bus"]) == 24
2121
@test length(result["solution"]["gen"]) == 33
2222
end
23+
24+
@testset "infeasible case" begin
25+
# make sure code does not crash when ResultCount == 0
26+
# change objective to linear so the case can load into cbc
27+
data = parse_file("../test/data/matpower/case24.m")
28+
for (i,gen) in data["gen"]
29+
if gen["ncost"] > 2
30+
gen["ncost"] = 2
31+
gen["cost"] = gen["cost"][length(gen["cost"])-1:end]
32+
end
33+
end
34+
result = run_opf(data, DCPPowerModel, cbc_solver)
35+
36+
@test haskey(result, "optimizer")
37+
@test haskey(result, "termination_status")
38+
@test haskey(result, "primal_status")
39+
@test haskey(result, "dual_status")
40+
@test haskey(result, "solve_time")
41+
@test haskey(result, "solution")
42+
@test !isnan(result["solve_time"])
43+
@test length(result["solution"]) == 1
44+
end
2345
end
2446

2547
@testset "test branch flow output" begin

0 commit comments

Comments
 (0)