Skip to content

Commit

Permalink
UPD: JuMP v1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudocubic committed May 8, 2024
1 parent 8c46dea commit dd61a57
Show file tree
Hide file tree
Showing 20 changed files with 729 additions and 781 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## staged

- Updated to new JuMP v1.15 nonlinear constraint and expression syntax
- Indicated broken tests in `"2w_dy_lead"` and `"3-bus SOCConicUBF opf_bf"` by using `@test_skip`

## v0.15.2
Expand Down
4 changes: 2 additions & 2 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 <dfobes@lanl.gov>", "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 @@ -29,7 +29,7 @@ Graphs = "1"
InfrastructureModels = "0.7.3, 0.7.5"
Ipopt = "0.9, 1.0.2, 1.1"
JSON = "0.18, 0.19, 0.20, 0.21"
JuMP = "0.22, 0.23, 1"
JuMP = ">=1.15,<2"
LoggingExtras = "0.4.7, 1"
PolyhedralRelaxations = "0.3.5"
SCS = "0.9, 1.0, 1.1"
Expand Down
15 changes: 0 additions & 15 deletions src/core/base.jl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +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(
Expand Down
25 changes: 0 additions & 25 deletions src/core/data.jl
100644 → 100755
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
48 changes: 24 additions & 24 deletions src/core/objective.jl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ 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)]...))
pg_contains_nl_exp = any(x<:JuMP.NonlinearExpr 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
Expand Down Expand Up @@ -277,24 +277,24 @@ function _objective_mc_min_fuel_cost_polynomial_linquad(pm::AbstractUnbalancedPo
for (i,gen) in nw_ref[:gen]
bus = gen["gen_bus"]

#to avoid function calls inside of @NLconstraint:
#to avoid function calls inside of @constraint:
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])
gen_cost[(n,i)] = JuMP.@expression(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])
gen_cost[(n,i)] = JuMP.@expression(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
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 @@ -313,9 +313,9 @@ function _objective_mc_min_fuel_cost_polynomial_linquad_switch(pm::AbstractUnbal
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]
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]
gen_cost[(n,i)] = gen["cost"][1]*pg^2 - gen["cost"][2]*(-pg) + gen["cost"][3]
else
gen_cost[(n,i)] = 0.0
end
Expand All @@ -340,14 +340,14 @@ function _objective_mc_min_fuel_cost_polynomial_linquad_switch(pm::AbstractUnbal
for (i,gen) in nw_ref[:gen]
bus = gen["gen_bus"]

#to avoid function calls inside of @NLconstraint:
#to avoid function calls inside of @constraint:
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]
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]
gen_cost[(n,i)] = gen["cost"][1]*pg^2 - gen["cost"][2]*(-pg) + gen["cost"][3]
else
gen_cost[(n,i)] = 0.0
end
Expand Down Expand Up @@ -375,21 +375,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 +406,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 All @@ -438,7 +438,7 @@ function objective_variable_pg_cost(pm::AbstractUnbalancedIVRModel; report::Bool
for (n, nw_ref) in nws(pm)
gen_lines = calc_cost_pwl_lines(nw_ref[:gen])

#to avoid function calls inside of @NLconstraint
#to avoid function calls inside of @constraint
pg_cost = var(pm, n)[:pg_cost] = JuMP.@variable(pm.model,
[i in ids(pm, n, :gen)], base_name="$(n)_pg_cost",
)
Expand All @@ -448,7 +448,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
100644 → 100755
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 dd61a57

Please sign in to comment.