Skip to content

Commit

Permalink
WIP: update to new JuMP nonlinear syntax
Browse files Browse the repository at this point in the history
 * All instances are a straight swap, dropping the NL part of macro names.
 * Duplicated code blocks that were added to work-around NL macros have
   been removed
 * Tests have been updated to new solution values

Questions:

 * Should the test values have changed? Macro parsing might give slightly
   different numerical values. Would that make a difference?
 * I'll comment in-line, but are the higher-order polynomial objectives
   correct?
  • Loading branch information
odow committed Apr 21, 2023
1 parent c9918ba commit 65eafbe
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 257 deletions.
19 changes: 10 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Memento = "f28f55f0-a522-5efc-85c2-fe41dfb9b2d9"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
HiGHS = "~0.3, ~1"
InfrastructureModels = "~0.6, ~0.7"
Ipopt = "~0.8, ~0.9, ~1"
JSON = "~0.18, ~0.19, ~0.20, ~0.21"
JuMP = "~0.22, ~0.23, 1"
Juniper = "~0.8, ~0.9"
Memento = "~1.0, ~1.1, ~1.2, ~1.3, ~1.4"
NLsolve = "4.0"
SCS = "~0.9, ~1.0"
HiGHS = "1"
InfrastructureModels = "0.6, 0.7"
Ipopt = "1"
JSON = "0.18, 0.19, 0.20, 0.21"
JuMP = "1"
Juniper = "0.8, 0.9"
Memento = "1"
NLsolve = "4"
SCS = "1"
julia = "1.6"

[extras]
Expand Down
42 changes: 22 additions & 20 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,17 @@ function _objective_min_fuel_and_flow_cost_polynomial_nl(pm::AbstractPowerModel;

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+3) for (d,v) in enumerate(cost_rev_nl)) )
# TODO(odow): is the correct? Or should it be pg^(d+2.0)?
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+3.0) 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

Expand All @@ -296,21 +297,21 @@ function _objective_min_fuel_and_flow_cost_polynomial_nl(pm::AbstractPowerModel;

cost_rev = reverse(dcline["cost"])
if length(cost_rev) == 1
dcline_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1])
dcline_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1])
elseif length(cost_rev) == 2
dcline_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*p_dc)
dcline_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*p_dc)
elseif length(cost_rev) == 3
dcline_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*p_dc + cost_rev[3]*p_dc^2)
dcline_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*p_dc + cost_rev[3]*p_dc^2)
elseif length(cost_rev) >= 4
cost_rev_nl = cost_rev[4:end]
dcline_cost[(n,i)] = JuMP.@NLexpression(pm.model, cost_rev[1] + cost_rev[2]*p_dc + cost_rev[3]*p_dc^2 + sum( v*p_dc^(d+2) for (d,v) in enumerate(cost_rev_nl)) )
dcline_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*p_dc + cost_rev[3]*p_dc^2 + sum( v*p_dc^(d+2.0) for (d,v) in enumerate(cost_rev_nl)) )
else
dcline_cost[(n,i)] = JuMP.@NLexpression(pm.model, 0.0)
dcline_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( dcline_cost[(n,i)] for (i,dcline) in nw_ref[:dcline])
Expand Down Expand Up @@ -366,21 +367,22 @@ function _objective_min_fuel_cost_polynomial_nl(pm::AbstractPowerModel; report::

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+3) for (d,v) in enumerate(cost_rev_nl)) )
# TODO(odow): is this correct? Or should it be pg^(d+2.0)?
gen_cost[(n,i)] = JuMP.@expression(pm.model, cost_rev[1] + cost_rev[2]*pg + cost_rev[3]*pg^2 + sum( v*pg^(d+3.0) 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 Down Expand Up @@ -585,14 +587,14 @@ function objective_max_loadability(pm::AbstractPowerModel)
time_elapsed = Dict(n => get(ref(pm, n), :time_elapsed, 1) for n in nws)

load_weight = Dict(n =>
Dict(i => get(load, "weight", 1.0) for (i,load) in ref(pm, n, :load))
Dict(i => get(load, "weight", 1.0) for (i,load) in ref(pm, n, :load))
for n in nws)

#println(load_weight)

return JuMP.@objective(pm.model, Max,
sum(
(
sum(
(
time_elapsed[n]*(
sum(z_shunt[n][i] for (i,shunt) in ref(pm, n, :shunt)) +
sum(load_weight[n][i]*abs(load["pd"])*z_demand[n][i] for (i,load) in ref(pm, n, :load))
Expand Down
Loading

0 comments on commit 65eafbe

Please sign in to comment.