Skip to content

Commit

Permalink
UPD: update to the NonlinearExpr syntax of JuMP v1.15 (#454)
Browse files Browse the repository at this point in the history
* Update to the NonlinearExpr syntax of JuMP v1.15

* Add changelog
  • Loading branch information
odow authored Sep 18, 2024
1 parent 8c46dea commit 68d7855
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 462 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# PowerModelsDistribution.jl Change Log

## staged
## v0.16.0

- Updated to use the new `NonlinearExpr` syntax in JuMP v1.15. In most cases,
there should be no user-visible changes. However, if you access the internal
JuMP model object, the constraints are now `NonlinearExpr` objects instead of
`NLPBlock`. In addition, you may encounter small numerical differences in the
solution due to a different ordering of the constraints.
- Indicated broken tests in `"2w_dy_lead"` and `"3-bus SOCConicUBF opf_bf"` by using `@test_skip`

## v0.15.2
Expand Down
7 changes: 3 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "PowerModelsDistribution"
uuid = "d7431456-977f-11e9-2de3-97ff7677985e"
authors = ["David M Fobes <[email protected]>", "Carleton Coffrin"]
repo = "https://github.com/lanl-ansi/PowerModelsDistribution.jl.git"
version = "0.15.2"
version = "0.16.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand All @@ -27,9 +27,9 @@ FilePaths = "0.8.3"
Glob = "1.3"
Graphs = "1"
InfrastructureModels = "0.7.3, 0.7.5"
Ipopt = "0.9, 1.0.2, 1.1"
Ipopt = "1"
JSON = "0.18, 0.19, 0.20, 0.21"
JuMP = "0.22, 0.23, 1"
JuMP = "1.23.2"
LoggingExtras = "0.4.7, 1"
PolyhedralRelaxations = "0.3.5"
SCS = "0.9, 1.0, 1.1"
Expand All @@ -38,7 +38,6 @@ julia = "1.6"

[extras]
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand Down
1 change: 0 additions & 1 deletion docs/src/reference/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ nw_ids
## Helper functions

```@docs
@smart_constraint
set_lower_bound
set_upper_bound
comp_start_value
Expand Down
4 changes: 2 additions & 2 deletions examples/Extension Tutorial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,10 @@ end;
# ╔═╡ f7b500a6-7dbf-44cb-9433-3b534f13cd6b
begin
# link charge to generator models
JuMP.@NLconstraint(nc_model, [e in EVs, k in K],
JuMP.@constraint(nc_model, [e in EVs, k in K],
nc_Pg_ek[e,k] == -nc_P_ek[e,k]
)
JuMP.@NLconstraint(nc_model, [e in EVs, k in K],
JuMP.@constraint(nc_model, [e in EVs, k in K],
nc_Qg_ek[e,k] == 0.0
)
end;
Expand Down
16 changes: 0 additions & 16 deletions src/core/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ function _check_var_keys(vars, keys, var_name, comp_name)
end


"""
@smart_constraint model::JuMP.Model vars::Vector expr::JuMP.Expression
Detection of whether a constraint should be NL or not"
"""
macro smart_constraint(model, vars, expr)
esc(quote
if _has_nl_expression($vars)
JuMP.@NLconstraint($model, $expr)
else
JuMP.@constraint($model, $expr)
end
end)
end


"""
function set_lower_bound(
x::JuMP.VariableRef,
Expand Down
25 changes: 0 additions & 25 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -811,31 +811,6 @@ function _make_full_matrix_variable(diag::Vector{T}, lowertriangle::Vector{T}, u
return matrix
end

# TODO refactor into several functions
"helper to determine if expession has any Nonlinear terms"
function _has_nl_expression(x)::Bool
if isa(x, JuMP.NonlinearExpression)
return true
elseif isa(x, Array)
if any(_has_nl_expression.(x))
return true
end
elseif isa(x, Dict)
for i in values(x)
if _has_nl_expression(i)
return true
end
end
elseif isa(x, JuMP.Containers.DenseAxisArray)
for i in values(x.data)
if _has_nl_expression(i)
return true
end
end
end
return false
end


"""
correct_mc_voltage_angle_differences!(data::Dict{String,<:Any}, default_pad::Real=deg2rad(10.0))
Expand Down
90 changes: 30 additions & 60 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,59 +247,29 @@ end

"gen connections adaptation of min fuel cost polynomial linquad objective"
function _objective_mc_min_fuel_cost_polynomial_linquad(pm::AbstractUnbalancedPowerModel; report::Bool=true)
pg_contains_nl_exp = any(x<:JuMP.NonlinearExpression for x in vcat([typeof.(isa(pg, JuMP.Containers.DenseAxisArray) ? pg.data : pg) for nw in nw_ids(pm) for (id,pg) in var(pm, nw, :pg)]...))
gen_cost = Dict()

if !pg_contains_nl_exp
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum(var(pm, n, :pg, i))

if length(gen["cost"]) == 1
gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
gen_cost[(n,i)] = gen["cost"][1]*pg + gen["cost"][2]
elseif length(gen["cost"]) == 3
gen_cost[(n,i)] = gen["cost"][1]*pg^2 + gen["cost"][2]*pg + gen["cost"][3]
else
gen_cost[(n,i)] = 0.0
end
end
end
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum(var(pm, n, :pg, i))

return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
)
else
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
bus = gen["gen_bus"]

#to avoid function calls inside of @NLconstraint:
pg = var(pm, n, :pg, i)
pg = isa(pg, JuMP.Containers.DenseAxisArray) ? pg.data : pg

int_dim = length(pg)
if length(gen["cost"]) == 1
gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, gen["cost"][1]*sum(pg[i] for i in 1:int_dim) + gen["cost"][2])
elseif length(gen["cost"]) == 3
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, gen["cost"][1]*sum(pg[i] for i in 1:int_dim)^2 + gen["cost"][2]*sum(pg[i] for i in 1:int_dim) + gen["cost"][3])
else
gen_cost[(n,i)] = 0.0
end
if length(gen["cost"]) == 1
gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
gen_cost[(n,i)] = gen["cost"][1]*pg + gen["cost"][2]
elseif length(gen["cost"]) == 3
gen_cost[(n,i)] = gen["cost"][1]*pg^2 + gen["cost"][2]*pg + gen["cost"][3]
else
gen_cost[(n,i)] = 0.0
end
end

return JuMP.@NLobjective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
)
end

return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
)
end


Expand Down Expand Up @@ -375,21 +345,21 @@ function _objective_mc_min_fuel_cost_polynomial_nl(pm::AbstractUnbalancedPowerMo

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1])
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1])
elseif length(cost_rev) == 2
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*pg)
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg)
elseif length(cost_rev) == 3
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
elseif length(cost_rev) >= 4
cost_rev_nl = cost_rev[4:end]
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
else
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, 0.0)
gen_cost[(n,i)] = JuMP.@expression(pm.model, 0.0)
end
end
end

return JuMP.@NLobjective(pm.model, Min,
return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
Expand All @@ -406,21 +376,21 @@ function _objective_mc_min_fuel_cost_polynomial_nl_switch(pm::AbstractUnbalanced

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1])
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1])
elseif length(cost_rev) == 2
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*pg)
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg)
elseif length(cost_rev) == 3
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
elseif length(cost_rev) >= 4
cost_rev_nl = cost_rev[4:end]
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
else
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, 0.0)
gen_cost[(n,i)] = JuMP.@expression(pm.model, 0.0)
end
end
end

return JuMP.@NLobjective(pm.model, Min,
return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] ) +
sum( var(pm, n, :switch_state, l) for l in ids(pm, n, :switch_dispatchable))
Expand Down Expand Up @@ -448,7 +418,7 @@ function objective_variable_pg_cost(pm::AbstractUnbalancedIVRModel; report::Bool
for (i, gen) in nw_ref[:gen]
pg = var(pm, n, :pg, i)
for line in gen_lines[i]
JuMP.@NLconstraint(pm.model, pg_cost[i] >= line.slope*sum(pg[c] for c in gen["connections"]) + line.intercept)
JuMP.@constraint(pm.model, pg_cost[i] >= line.slope*sum(pg[c] for c in gen["connections"]) + line.intercept)
end
end
end
Expand Down
18 changes: 3 additions & 15 deletions src/core/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ function _IM.solution_preprocessor(pm::AbstractUnbalancedPowerModel, solution::D
end


"custom `build_solution_values` for multiconductor (vector) variables"
function _IM.build_solution_values(var::JuMP.Containers.DenseAxisArray{<:JuMP.VariableRef,1})
return JuMP.value.(var.data)
end


"custom `build_solution_values` for multiconductor (vector) nonlinear expressions"
function _IM.build_solution_values(var::JuMP.Containers.DenseAxisArray{<:JuMP.NonlinearExpression,1})
return JuMP.value.(var.data)
end


"custom `build_solution_values` for multiconductor (vector) generic affine expressions"
function _IM.build_solution_values(var::JuMP.Containers.DenseAxisArray{<:JuMP.GenericAffExpr,1})
return JuMP.value.(var.data)
"custom `build_solution_values` for multiconductor (vector) JuMP expressions"
function _IM.build_solution_values(var::JuMP.AbstractJuMPScalar)
return JuMP.value(var)
end


Expand Down
Loading

2 comments on commit 68d7855

@pseudocubic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115419

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.16.0 -m "<description of version>" 68d7855eab0d20ecccac5d4a33c0a48b6165e3b1
git push origin v0.16.0

Please sign in to comment.