Skip to content

Commit

Permalink
Update to the NonlinearExpr syntax of JuMP v1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 14, 2024
1 parent 8c46dea commit 20d0bfc
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 461 deletions.
10 changes: 6 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 @@ -23,22 +23,24 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
CSV = "0.8.5, 0.9, 0.10"
Dates = "1.6"
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"
SparseArrays = "1.6"
SpecialFunctions = "2"
Test = "1.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

0 comments on commit 20d0bfc

Please sign in to comment.