Skip to content

Commit

Permalink
Update to latest JuMP PR
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jun 28, 2023
1 parent 6ef586e commit e71db6e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 20 deletions.
29 changes: 18 additions & 11 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,20 @@ function objective_min_fuel_and_flow_cost_polynomial(pm::AbstractPowerModel; kwa
end
end

function _unstable_pow(x, p)
if p == 0
return 1
elseif p == 1
"""
_pow(x, d::Int)
Compute `x^d` with special case handling for `d == 0` and `d == 1` to avoid
JuMP's default implementation which returns a quadratic for `d in {0, 1, 2}`.
"""
function _pow(x, d::Int)
if d == 0
return 1.0
elseif d == 1
return x
else
return x^d
end
return x^p
end

""
Expand All @@ -142,7 +149,7 @@ function _objective_min_fuel_and_flow_cost_polynomial_linquad(pm::AbstractPowerM
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n) )
gen_cost[(n, i)] = JuMP.@expression(
pm.model,
sum(v * _unstable_pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
sum(v * _pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
end

Expand All @@ -151,7 +158,7 @@ function _objective_min_fuel_and_flow_cost_polynomial_linquad(pm::AbstractPowerM
p_dc = sum( var(pm, n, :p_dc, from_idx[i])[c] for c in conductor_ids(pm, n) )
dcline_cost[(n, i)] = JuMP.@expression(
pm.model,
sum(v * _unstable_pow(p_dc, d-1) for (d, v) in enumerate(reverse(dcline["cost"]))),
sum(v * _pow(p_dc, d-1) for (d, v) in enumerate(reverse(dcline["cost"]))),
)
end
end
Expand Down Expand Up @@ -272,7 +279,7 @@ function _objective_min_fuel_and_flow_cost_polynomial_nl(pm::AbstractPowerModel;
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n))
gen_cost[(n,i)] = JuMP.@expression(
pm.model,
sum(v * _unstable_pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
sum(v * _pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
end

Expand All @@ -282,7 +289,7 @@ function _objective_min_fuel_and_flow_cost_polynomial_nl(pm::AbstractPowerModel;
p_dc = sum( var(pm, n, :p_dc, from_idx[i])[c] for c in conductor_ids(pm, n))
dcline_cost[(n,i)] = JuMP.@expression(
pm.model,
sum(v * _unstable_pow(p_dc, d-1) for (d, v) in enumerate(reverse(dcline["cost"]))),
sum(v * _pow(p_dc, d-1) for (d, v) in enumerate(reverse(dcline["cost"]))),
)
end
end
Expand Down Expand Up @@ -315,7 +322,7 @@ function _objective_min_fuel_cost_polynomial_linquad(pm::AbstractPowerModel; rep
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n) )
gen_cost[(n, i)] = JuMP.@expression(
pm.model,
sum(v * _unstable_pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
sum(v * _pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
end
end
Expand All @@ -336,7 +343,7 @@ function _objective_min_fuel_cost_polynomial_nl(pm::AbstractPowerModel; report::
pg = sum( var(pm, n, :pg, i)[c] for c in conductor_ids(pm, n))
gen_cost[(n,i)] = JuMP.@expression(
pm.model,
sum(v * _unstable_pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
sum(v * _pow(pg, d-1) for (d, v) in enumerate(reverse(gen["cost"]))),
)
end
end
Expand Down
1 change: 0 additions & 1 deletion src/form/acp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ function constraint_power_balance_ls(pm::AbstractACPModel, n::Int, i::Int, bus_a
z_demand = get(var(pm, n), :z_demand, Dict()); _check_var_keys(z_demand, keys(bus_pd), "power factor", "load")
z_shunt = get(var(pm, n), :z_shunt, Dict()); _check_var_keys(z_shunt, keys(bus_gs), "power factor", "shunt")

# this is required for improved performance in NLP models
cstr_p = JuMP.@constraint(pm.model,
sum(p[a] for a in bus_arcs)
+ sum(p_dc[a_dc] for a_dc in bus_arcs_dc)
Expand Down
2 changes: 1 addition & 1 deletion test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#print(pm.model)

@test JuMP.num_nonlinear_constraints(pm.model) == 0
@test JuMP.num_constraints(pm.model, JuMP.NonlinearExpr, JuMP.MOI.EqualTo{Float64}) == 12
@test JuMP.num_constraints(pm.model, JuMP.NonlinearExpr{JuMP.VariableRef}, JuMP.MOI.EqualTo{Float64}) == 12
@test JuMP.num_variables(pm.model) == 28

result = optimize_model!(pm, optimizer=JuMP.optimizer_with_attributes(Ipopt.Optimizer, "print_level"=>0))
Expand Down
2 changes: 1 addition & 1 deletion test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
pm = instantiate_model("../test/data/matpower/case5.m", ACPPowerModel, PowerModels.build_opf, jump_model=m)

@test JuMP.num_nonlinear_constraints(pm.model) == 0
@test JuMP.num_constraints(pm.model, JuMP.NonlinearExpr, JuMP.MOI.EqualTo{Float64}) == 28
@test JuMP.num_constraints(pm.model, JuMP.NonlinearExpr{JuMP.VariableRef}, JuMP.MOI.EqualTo{Float64}) == 28
@test JuMP.num_variables(pm.model) == 49

@test pm.model[:my_var] == x
Expand Down
5 changes: 1 addition & 4 deletions test/opf-obj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ end

@testset "jump model objective type" begin
pm = instantiate_model(data, ACPPowerModel, build_opf)

@test JuMP.objective_function(pm.model) == JuMP.AffExpr(0.0)
# would be good to add a test like this one in a future version where the NL expression can be accessed with a public API
#@test isa(JuMP._nlp_objective_function(pm.model), JuMP.MOI.Nonlinear.Expression)
@test JuMP.objective_function(pm.model) isa JuMP.NonlinearExpr{JuMP.VariableRef}
end

@testset "opf objective" begin
Expand Down
2 changes: 1 addition & 1 deletion test/opf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ end
result = run_opf("../test/data/matpower/case5_npg.m", SOCWRConicPowerModel, sdp_solver)

@test result["termination_status"] == OPTIMAL
# @test isapprox(result["objective"], 3551.71; atol = 40)
# @test isapprox(result["objective"], 3551.71; atol = 40)
@test isapprox(result["objective"], 3602.11; atol = 40)
end
@testset "5-bus with pwl costs" begin
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Pkg
Pkg.pkg"add JuMP#od/nlp-expr MathOptInterface#master Ipopt#od/nlp-expr"
Pkg.pkg"add JuMP#od/nlp-expr"

using PowerModels
import InfrastructureModels
Expand Down

0 comments on commit e71db6e

Please sign in to comment.