|
1 | 1 | ""
|
2 | 2 | function build_solution(pm::AbstractPowerModel, solve_time; solution_builder=solution_opf!)
|
3 |
| - # TODO @assert that the model is solved |
4 |
| - |
5 | 3 | sol = _init_solution(pm)
|
6 |
| - data = Dict{String,Any}("name" => pm.data["name"]) |
7 | 4 |
|
| 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"]) |
8 | 40 | if InfrastructureModels.ismultinetwork(pm.data)
|
9 |
| - sol["multinetwork"] = true |
10 |
| - sol_nws = sol["nw"] = Dict{String,Any}() |
11 | 41 | data_nws = data["nw"] = Dict{String,Any}()
|
12 | 42 |
|
13 | 43 | 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) |
21 | 44 | data_nws[n] = Dict(
|
22 | 45 | "name" => get(nw_data, "name", "anonymous"),
|
23 | 46 | "bus_count" => length(nw_data["bus"]),
|
24 | 47 | "branch_count" => length(nw_data["branch"])
|
25 | 48 | )
|
26 | 49 | end
|
27 | 50 | 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) |
33 | 51 | data["bus_count"] = length(pm.data["bus"])
|
34 | 52 | data["branch_count"] = length(pm.data["branch"])
|
35 | 53 | end
|
|
0 commit comments