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

Updates for JuMP's New NL Interface #901

Merged
merged 4 commits into from
Jan 19, 2024
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ PowerModels.jl Change Log
### Staged
- nothing

### v0.21.0
- Update to new JuMP nonlinear interface (breaking)

### v0.20.1
- Fix tests for latest ipopt jll

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 = "PowerModels"
uuid = "c36e90e8-916a-50a6-bd94-075b64ef4655"
authors = ["Carleton Coffrin"]
repo = "https://github.com/lanl-ansi/PowerModels.jl"
version = "0.20.1"
version = "0.21.0"

[deps]
InfrastructureModels = "2030c09a-7f63-5d83-885d-db604e0e9cc0"
Expand All @@ -18,7 +18,7 @@ HiGHS = "~1"
InfrastructureModels = "~0.6, ~0.7"
Ipopt = "~1"
JSON = "~0.21"
JuMP = "1"
JuMP = "1.15"
Juniper = "~0.9"
Memento = "~1.0, ~1.1, ~1.2, ~1.3, ~1.4"
NLsolve = "4.0"
Expand Down
111 changes: 17 additions & 94 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
@@ -1,103 +1,26 @@
"""
Checks if any generator cost model will require a JuMP nonlinear expression
"""
function check_nl_gen_cost_models(pm::AbstractPowerModel)
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
if haskey(gen, "cost")
if gen["model"] == 2 && length(gen["cost"]) > 3
return true
end
end
end
end
return false
end

"""
Checks if any dcline cost model will require a JuMP nonlinear expression
"""
function check_nl_dcline_cost_models(pm::AbstractPowerModel)
for (n, nw_ref) in nws(pm)
for (i,dcline) in nw_ref[:dcline]
if haskey(dcline, "cost")
if dcline["model"] == 2 && length(dcline["cost"]) > 3
return true
end
end
end
end
return false
end


""
function objective_min_fuel_and_flow_cost(pm::AbstractPowerModel; kwargs...)
nl_gen = check_nl_gen_cost_models(pm)
nl_dc = check_nl_dcline_cost_models(pm)

nl = nl_gen || nl_dc || typeof(pm) <: AbstractIVRModel

expression_pg_cost(pm; kwargs...)
expression_p_dc_cost(pm; kwargs...)

if !nl
return JuMP.@objective(pm.model, Min,
sum(
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen]) +
sum( var(pm, n, :p_dc_cost, i) for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
else
pg_cost = Dict()
p_dc_cost = Dict()
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg_cost[(n,i)] = var(pm, n, :pg_cost, i)
end
for (i,dcline) in nw_ref[:dcline]
p_dc_cost[(n,i)] = var(pm, n, :p_dc_cost, i)
end
end

return JuMP.@NLobjective(pm.model, Min,
sum(
sum( pg_cost[n,i] for (i,gen) in nw_ref[:gen]) +
sum( p_dc_cost[n,i] for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end
return JuMP.@objective(pm.model, Min,
sum(
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen]) +
sum( var(pm, n, :p_dc_cost, i) for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end


""
function objective_min_fuel_cost(pm::AbstractPowerModel; kwargs...)
nl_gen = check_nl_gen_cost_models(pm)

nl = nl_gen || typeof(pm) <: AbstractIVRModel

expression_pg_cost(pm; kwargs...)

if !nl
return JuMP.@objective(pm.model, Min,
sum(
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen])
for (n, nw_ref) in nws(pm))
)
else
pg_cost = Dict()
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg_cost[(n,i)] = var(pm, n, :pg_cost, i)
end
end

return JuMP.@NLobjective(pm.model, Min,
sum(
sum( pg_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( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen])
for (n, nw_ref) in nws(pm))
)
end


Expand Down Expand Up @@ -293,7 +216,7 @@
expr += point.mw*cost_lambda[i]
cost_expr += point.cost*cost_lambda[i]
end
JuMP.@NLconstraint(pm.model, expr == sum(x for x in x_list))
JuMP.@constraint(pm.model, expr == sum(x for x in x_list))

return cost_expr
end
Expand All @@ -313,7 +236,7 @@
return cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2
else # length(cost_terms) >= 4
cost_nl = cost_terms[4:end]
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
end
end

Expand Down Expand Up @@ -355,18 +278,18 @@

# note that `cost_terms` should be providing in ascending order (the reverse of the Matpower spec.)
function _polynomial_cost_expression(pm::AbstractPowerModel, x_list, cost_terms; nw=0, id=1, var_name="x")
x = JuMP.@NLexpression(pm.model, sum(x for x in x_list))
x = JuMP.@expression(pm.model, sum(x for x in x_list))
if length(cost_terms) == 0
return 0.0
elseif length(cost_terms) == 1
return cost_terms[1]
elseif length(cost_terms) == 2
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x)
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x)
elseif length(cost_terms) == 3
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2)
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2)
else # length(cost_terms) >= 4
cost_nl = cost_terms[4:end]
return JuMP.@NLexpression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )
return JuMP.@expression(pm.model, cost_terms[1] + cost_terms[2]*x + cost_terms[3]*x^2 + sum( v*x^(d+2) for (d,v) in enumerate(cost_nl)) )

Check warning on line 292 in src/core/objective.jl

View check run for this annotation

Codecov / codecov/patch

src/core/objective.jl#L292

Added line #L292 was not covered by tests
end
end

Expand Down
Loading
Loading