Skip to content

Commit

Permalink
WIP: upgrade to JuMP 1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjospina committed Sep 12, 2024
1 parent 3c9b05a commit 0c9d6d7
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 240 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ StsDOpt = "6b7f5ccb-1a75-42de-b5f3-d93534753244"
InfrastructureModels = "0.7.8"
Ipopt = "0.9, 1.0.2"
JSON = "~0.18, ~0.19, ~0.20, ~0.21"
JuMP = "~0.22, ~0.23, 1"
JuMP = "1.15"
LinearAlgebra = "1.6"
PowerModels = "0.20"
PowerModelsDistribution = "0.15.2"
PowerModels = "0.21"
PowerModelsDistribution = "0.16.0"
SCS = "~1.0, ~1.1"
julia = "1.6"
StsDOpt = "0.1.6"
Expand Down
1 change: 0 additions & 1 deletion src/PowerModelsITD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module PowerModelsITD

# Borrow dependencies from other packages
import InfrastructureModels: optimize_model!, @im_fields, nw_id_default
import PowerModelsDistribution: @smart_constraint, _has_nl_expression

# Import necessary (additional) packages/functions
import LinearAlgebra
Expand Down
62 changes: 31 additions & 31 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function objective_mc_variable_pg_cost(pm::_PMD.AbstractUnbalancedIVRModel; repo
pg_cost_expr += point.cost*pg_cost_lambda[i]
end
# Important: This constraint had to be changed to a Non-linear constraint
JuMP.@NLconstraint(pm.model, pg_expr == sum(_PMD.var(pm, n, :pg, i)[c] for c in gen["connections"]))
JuMP.@constraint(pm.model, pg_expr == sum(_PMD.var(pm, n, :pg, i)[c] for c in gen["connections"]))
pg_cost[i] = pg_cost_expr
end
report && _IM.sol_component_value(pm, _PMD.pmd_it_sym, n, :gen, :pg_cost, _PMD.ids(pm, n, :gen), pg_cost)
Expand Down Expand Up @@ -251,14 +251,14 @@ function _objective_itd_min_fuel_cost_polynomial_linquad(pmitd::AbstractIVRPower
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 = _PM.var(pm, n, :pg, i)
if length(gen["cost"]) == 1
pm_gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, gen["cost"][1]*pg + gen["cost"][2])
pm_gen_cost[(n,i)] = JuMP.@expression(pm.model, gen["cost"][1]*pg + gen["cost"][2])
elseif length(gen["cost"]) == 3
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, gen["cost"][1]*pg^2 + gen["cost"][2]*pg + gen["cost"][3])
pm_gen_cost[(n,i)] = JuMP.@expression(pm.model, gen["cost"][1]*pg^2 + gen["cost"][2]*pg + gen["cost"][3])
else
pm_gen_cost[(n,i)] = 0.0
end
Expand All @@ -272,14 +272,14 @@ function _objective_itd_min_fuel_cost_polynomial_linquad(pmitd::AbstractIVRPower
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 = _PMD.var(pmd, n, :pg, i)
if length(gen["cost"]) == 1
pmd_gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmd.model, gen["cost"][1]*sum(pg[c] for c in gen["connections"]) + gen["cost"][2])
pmd_gen_cost[(n,i)] = JuMP.@expression(pmd.model, gen["cost"][1]*sum(pg[c] for c in gen["connections"]) + gen["cost"][2])
elseif length(gen["cost"]) == 3
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmd.model, gen["cost"][1]*sum(pg[c] for c in gen["connections"])^2 + gen["cost"][2]*sum(pg[c] for c in gen["connections"]) + gen["cost"][3])
pmd_gen_cost[(n,i)] = JuMP.@expression(pmd.model, gen["cost"][1]*sum(pg[c] for c in gen["connections"])^2 + gen["cost"][2]*sum(pg[c] for c in gen["connections"]) + gen["cost"][3])
else
pmd_gen_cost[(n,i)] = 0.0
end
Expand All @@ -288,7 +288,7 @@ function _objective_itd_min_fuel_cost_polynomial_linquad(pmitd::AbstractIVRPower


# ITD (Combined objective)
return JuMP.@NLobjective(pmitd.model, Min,
return JuMP.@objective(pmitd.model, Min,
sum(
sum( pm_gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in _PM.nws(pm))
Expand Down Expand Up @@ -317,20 +317,20 @@ function _objective_itd_min_fuel_cost_polynomial_nl(pmitd::AbstractIVRPowerModel
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 = _PM.var(pm, n, :pg, i)
cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1])
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1])
elseif length(cost_rev) == 2
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
elseif length(cost_rev) == 3
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.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]
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.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)) )
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.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
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, 0.0)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, 0.0)
end
end
end
Expand All @@ -345,22 +345,22 @@ function _objective_itd_min_fuel_cost_polynomial_nl(pmitd::AbstractIVRPowerModel
cost_rev = reverse(gen["cost"])

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

# ITD (Combined objective)
return JuMP.@NLobjective(pmitd.model, Min,
return JuMP.@objective(pmitd.model, Min,
sum(
sum( pm_gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in _PM.nws(pm))
Expand Down Expand Up @@ -391,16 +391,16 @@ function _objective_itd_min_fuel_cost_polynomial_nl(pmitd::AbstractPowerModelITD

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1])
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1])
elseif length(cost_rev) == 2
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
elseif length(cost_rev) == 3
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.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]
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.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)) )
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.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
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, 0.0)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, 0.0)
end
end
end
Expand All @@ -419,22 +419,22 @@ function _objective_itd_min_fuel_cost_polynomial_nl(pmitd::AbstractPowerModelITD
JuMP.@constraint(pmitd.model, pg_aux == pg)

if length(cost_rev) == 1
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1])
pmd_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1])
elseif length(cost_rev) == 2
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg_aux)
pmd_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg_aux)
elseif length(cost_rev) == 3
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg_aux + cost_rev[3]*pg_aux^2)
pmd_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg_aux + cost_rev[3]*pg_aux^2)
elseif length(cost_rev) >= 4
cost_rev_nl = cost_rev[4:end]
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg_aux + cost_rev[3]*pg_aux^2 + sum( v*pg_aux^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
pmd_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg_aux + cost_rev[3]*pg_aux^2 + sum( v*pg_aux^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
else
pmd_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, 0.0)
pmd_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, 0.0)
end
end
end

# ITD (Combined objective)
return JuMP.@NLobjective(pmitd.model, Min,
return JuMP.@objective(pmitd.model, Min,
sum(
sum( pm_gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in _PM.nws(pm))
Expand Down
10 changes: 5 additions & 5 deletions src/core/objective_dmld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,16 @@ function _objective_itd_min_fuel_polynomial_nl_distribution_load_setpoint_delta(

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1])
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1])
elseif length(cost_rev) == 2
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
elseif length(cost_rev) == 3
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.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]
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+3) for (d,v) in enumerate(cost_rev_nl)) )
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+3) for (d,v) in enumerate(cost_rev_nl)) )
else
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, 0.0)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, 0.0)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/core/objective_dmld_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ function _objective_itd_min_fuel_polynomial_nl_distribution_load_setpoint_delta_

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1])
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1])
elseif length(cost_rev) == 2
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg)
elseif length(cost_rev) == 3
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.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]
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+3) for (d,v) in enumerate(cost_rev_nl)) )
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+3) for (d,v) in enumerate(cost_rev_nl)) )
else
pm_gen_cost[(n,i)] = JuMP.@NLexpression(pmitd.model, 0.0)
pm_gen_cost[(n,i)] = JuMP.@expression(pmitd.model, 0.0)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/core/objective_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function _objective_variable_pg_cost(pm::_PM.AbstractIVRModel; report::Bool=true
for (n, nw_ref) in _PM.nws(pm)
gen_lines = _PM.calc_cost_pwl_lines(nw_ref[:gen])

#to avoid function calls inside of @NLconstraint
#to avoid function calls inside of @constraint
pg_cost = _PM.var(pm, n)[:pg_cost] = JuMP.@variable(pm.model,
[i in _PM.ids(pm, n, :gen)], base_name="$(n)_pg_cost",
)
Expand All @@ -197,7 +197,7 @@ function _objective_variable_pg_cost(pm::_PM.AbstractIVRModel; report::Bool=true
for (i, gen) in nw_ref[:gen]
pg = _PM.var(pm, n, :pg, i)
for line in gen_lines[i]
JuMP.@NLconstraint(pm.model, pg_cost[i] >= line.slope*pg + line.intercept)
JuMP.@constraint(pm.model, pg_cost[i] >= line.slope*pg + line.intercept)
end
end
end
Expand All @@ -217,7 +217,7 @@ function _objective_variable_dc_cost(pm::_PM.AbstractIVRModel, report::Bool=true
for (n, nw_ref) in _PM.nws(pm)
dcline_lines = _PM.calc_cost_pwl_lines(nw_ref[:dcline])

#to avoid function calls inside of @NLconstraint
#to avoid function calls inside of @constraint
p_dc_cost = _PM.var(pm, n)[:p_dc_cost] = JuMP.@variable(pm.model,
[i in _PM.ids(pm, n, :dcline)], base_name="$(n)_p_dc_cost",
)
Expand All @@ -227,7 +227,7 @@ function _objective_variable_dc_cost(pm::_PM.AbstractIVRModel, report::Bool=true
arc = (i, dcline["f_bus"], dcline["t_bus"])
p_dc_var = _PM.var(pm, n, :p_dc)[arc]
for line in dcline_lines[i]
JuMP.@NLconstraint(pm.model, p_dc_cost[i] >= line.slope*p_dc_var + line.intercept)
JuMP.@constraint(pm.model, p_dc_cost[i] >= line.slope*p_dc_var + line.intercept)
end
end
end
Expand Down
Loading

0 comments on commit 0c9d6d7

Please sign in to comment.