Skip to content

Commit 8a1dc36

Browse files
authored
Updates for JuMP's New NL Interface (#901)
* updates for new nl interface * fix for ci on julia v1.6 on windows
1 parent a2856d1 commit 8a1dc36

12 files changed

+202
-336
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ PowerModels.jl Change Log
44
### Staged
55
- nothing
66

7+
### v0.21.0
8+
- Update to new JuMP nonlinear interface (breaking)
9+
710
### v0.20.1
811
- Fix tests for latest ipopt jll
912

Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "PowerModels"
22
uuid = "c36e90e8-916a-50a6-bd94-075b64ef4655"
33
authors = ["Carleton Coffrin"]
44
repo = "https://github.com/lanl-ansi/PowerModels.jl"
5-
version = "0.20.1"
5+
version = "0.21.0"
66

77
[deps]
88
InfrastructureModels = "2030c09a-7f63-5d83-885d-db604e0e9cc0"
@@ -18,7 +18,7 @@ HiGHS = "~1"
1818
InfrastructureModels = "~0.6, ~0.7"
1919
Ipopt = "~1"
2020
JSON = "~0.21"
21-
JuMP = "1"
21+
JuMP = "1.15"
2222
Juniper = "~0.9"
2323
Memento = "~1.0, ~1.1, ~1.2, ~1.3, ~1.4"
2424
NLsolve = "4.0"

src/core/objective.jl

+17-94
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,26 @@
1-
"""
2-
Checks if any generator cost model will require a JuMP nonlinear expression
3-
"""
4-
function check_nl_gen_cost_models(pm::AbstractPowerModel)
5-
for (n, nw_ref) in nws(pm)
6-
for (i,gen) in nw_ref[:gen]
7-
if haskey(gen, "cost")
8-
if gen["model"] == 2 && length(gen["cost"]) > 3
9-
return true
10-
end
11-
end
12-
end
13-
end
14-
return false
15-
end
16-
17-
"""
18-
Checks if any dcline cost model will require a JuMP nonlinear expression
19-
"""
20-
function check_nl_dcline_cost_models(pm::AbstractPowerModel)
21-
for (n, nw_ref) in nws(pm)
22-
for (i,dcline) in nw_ref[:dcline]
23-
if haskey(dcline, "cost")
24-
if dcline["model"] == 2 && length(dcline["cost"]) > 3
25-
return true
26-
end
27-
end
28-
end
29-
end
30-
return false
31-
end
32-
33-
341
""
352
function objective_min_fuel_and_flow_cost(pm::AbstractPowerModel; kwargs...)
36-
nl_gen = check_nl_gen_cost_models(pm)
37-
nl_dc = check_nl_dcline_cost_models(pm)
38-
39-
nl = nl_gen || nl_dc || typeof(pm) <: AbstractIVRModel
40-
413
expression_pg_cost(pm; kwargs...)
424
expression_p_dc_cost(pm; kwargs...)
435

44-
if !nl
45-
return JuMP.@objective(pm.model, Min,
46-
sum(
47-
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen]) +
48-
sum( var(pm, n, :p_dc_cost, i) for (i,dcline) in nw_ref[:dcline])
49-
for (n, nw_ref) in nws(pm))
50-
)
51-
else
52-
pg_cost = Dict()
53-
p_dc_cost = Dict()
54-
for (n, nw_ref) in nws(pm)
55-
for (i,gen) in nw_ref[:gen]
56-
pg_cost[(n,i)] = var(pm, n, :pg_cost, i)
57-
end
58-
for (i,dcline) in nw_ref[:dcline]
59-
p_dc_cost[(n,i)] = var(pm, n, :p_dc_cost, i)
60-
end
61-
end
62-
63-
return JuMP.@NLobjective(pm.model, Min,
64-
sum(
65-
sum( pg_cost[n,i] for (i,gen) in nw_ref[:gen]) +
66-
sum( p_dc_cost[n,i] for (i,dcline) in nw_ref[:dcline])
67-
for (n, nw_ref) in nws(pm))
68-
)
69-
end
6+
return JuMP.@objective(pm.model, Min,
7+
sum(
8+
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen]) +
9+
sum( var(pm, n, :p_dc_cost, i) for (i,dcline) in nw_ref[:dcline])
10+
for (n, nw_ref) in nws(pm))
11+
)
7012
end
7113

7214

7315
""
7416
function objective_min_fuel_cost(pm::AbstractPowerModel; kwargs...)
75-
nl_gen = check_nl_gen_cost_models(pm)
76-
77-
nl = nl_gen || typeof(pm) <: AbstractIVRModel
78-
7917
expression_pg_cost(pm; kwargs...)
8018

81-
if !nl
82-
return JuMP.@objective(pm.model, Min,
83-
sum(
84-
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen])
85-
for (n, nw_ref) in nws(pm))
86-
)
87-
else
88-
pg_cost = Dict()
89-
for (n, nw_ref) in nws(pm)
90-
for (i,gen) in nw_ref[:gen]
91-
pg_cost[(n,i)] = var(pm, n, :pg_cost, i)
92-
end
93-
end
94-
95-
return JuMP.@NLobjective(pm.model, Min,
96-
sum(
97-
sum( pg_cost[n,i] for (i,gen) in nw_ref[:gen])
98-
for (n, nw_ref) in nws(pm))
99-
)
100-
end
19+
return JuMP.@objective(pm.model, Min,
20+
sum(
21+
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen])
22+
for (n, nw_ref) in nws(pm))
23+
)
10124
end
10225

10326

@@ -293,7 +216,7 @@ function _pwl_cost_expression(pm::AbstractPowerModel, x_list, points; nw=0, id=1
293216
expr += point.mw*cost_lambda[i]
294217
cost_expr += point.cost*cost_lambda[i]
295218
end
296-
JuMP.@NLconstraint(pm.model, expr == sum(x for x in x_list))
219+
JuMP.@constraint(pm.model, expr == sum(x for x in x_list))
297220

298221
return cost_expr
299222
end
@@ -313,7 +236,7 @@ function _polynomial_cost_expression(pm::AbstractPowerModel, x_list::Array{JuMP.
313236
return cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2
314237
else # length(cost_terms) >= 4
315238
cost_nl = cost_terms[4:end]
316-
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
239+
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
317240
end
318241
end
319242

@@ -355,18 +278,18 @@ end
355278

356279
# note that `cost_terms` should be providing in ascending order (the reverse of the Matpower spec.)
357280
function _polynomial_cost_expression(pm::AbstractPowerModel, x_list, cost_terms; nw=0, id=1, var_name="x")
358-
x = JuMP.@NLexpression(pm.model, sum(x for x in x_list))
281+
x = JuMP.@expression(pm.model, sum(x for x in x_list))
359282
if length(cost_terms) == 0
360283
return 0.0
361284
elseif length(cost_terms) == 1
362285
return cost_terms[1]
363286
elseif length(cost_terms) == 2
364-
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x)
287+
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x)
365288
elseif length(cost_terms) == 3
366-
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2)
289+
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2)
367290
else # length(cost_terms) >= 4
368291
cost_nl = cost_terms[4:end]
369-
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
292+
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
370293
end
371294
end
372295

0 commit comments

Comments
 (0)