Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: update to new JuMP nonlinear syntax #858

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
106 changes: 29 additions & 77 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,19 @@ function _objective_min_fuel_and_flow_cost_polynomial_linquad(pm::AbstractPowerM
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n) )

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
gen_cost[(n, i)] = JuMP.@expression(
pm.model,
sum(v * pg^(d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
odow marked this conversation as resolved.
Show resolved Hide resolved
end

from_idx = Dict(arc[1] => arc for arc in nw_ref[:arcs_from_dc])
for (i,dcline) in nw_ref[:dcline]
p_dc = sum( var(pm, n, :p_dc, from_idx[i])[c] for c in conductor_ids(pm, n) )

if length(dcline["cost"]) == 1
dcline_cost[(n,i)] = dcline["cost"][1]
elseif length(dcline["cost"]) == 2
dcline_cost[(n,i)] = dcline["cost"][1]*p_dc + dcline["cost"][2]
elseif length(dcline["cost"]) == 3
dcline_cost[(n,i)] = dcline["cost"][1]*p_dc^2 + dcline["cost"][2]*p_dc + dcline["cost"][3]
else
dcline_cost[(n,i)] = 0.0
end
dcline_cost[(n, i)] = JuMP.@expression(
pm.model,
sum(v * p_dc^(d-1) for (d, v) in enumerate(reverse(dcline["cost"]))),
)
end
end

Expand Down Expand Up @@ -273,44 +261,24 @@ function _objective_min_fuel_and_flow_cost_polynomial_nl(pm::AbstractPowerModel;
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n))

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
gen_cost[(n,i)] = JuMP.@NLexpression(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)
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)
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)) )
else
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, 0.0)
end
gen_cost[(n,i)] = JuMP.@expression(
pm.model,
sum(v * pg^(d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
end

from_idx = Dict(arc[1] => arc for arc in nw_ref[:arcs_from_dc])

for (i,dcline) in nw_ref[:dcline]
p_dc = sum( var(pm, n, :p_dc, from_idx[i])[c] for c in conductor_ids(pm, n))

cost_rev = reverse(dcline["cost"])
if length(cost_rev) == 1
dcline_cost[(n,i)] = JuMP.@NLexpression(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)
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)
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)) )
else
dcline_cost[(n,i)] = JuMP.@NLexpression(pm.model, 0.0)
end
dcline_cost[(n,i)] = JuMP.@expression(
pm.model,
sum(v * p_dc^(d-1) for (d, v) in enumerate(reverse(dcline["cost"]))),
)
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 All @@ -336,16 +304,10 @@ function _objective_min_fuel_cost_polynomial_linquad(pm::AbstractPowerModel; rep
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n) )

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
gen_cost[(n, i)] = JuMP.@expression(
pm.model,
sum(v * pg^(d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
end
end

Expand All @@ -363,24 +325,14 @@ function _objective_min_fuel_cost_polynomial_nl(pm::AbstractPowerModel; report::
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n))

cost_rev = reverse(gen["cost"])
if length(cost_rev) == 1
gen_cost[(n,i)] = JuMP.@NLexpression(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)
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)
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)) )
else
gen_cost[(n,i)] = JuMP.@NLexpression(pm.model, 0.0)
end
gen_cost[(n,i)] = JuMP.@expression(
pm.model,
sum(v * pg^(d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
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 +537,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
Loading