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

Revised Multiphase Objective Functions #308

Merged
merged 1 commit into from
Jun 20, 2018
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
50 changes: 16 additions & 34 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function _make_per_unit(data::Dict{String,Any}, mva_base::Real)
apply_func(dcline, "qmaxf", rescale)
apply_func(dcline, "qminf", rescale)

_rescale_cost_model(dcline, mva_base, haskey(data, "phases"))
_rescale_cost_model(dcline, mva_base)
end

if haskey(data, "gen")
Expand All @@ -248,7 +248,7 @@ function _make_per_unit(data::Dict{String,Any}, mva_base::Real)
apply_func(gen, "qmax", rescale)
apply_func(gen, "qmin", rescale)

_rescale_cost_model(gen, mva_base, haskey(data, "phases"))
_rescale_cost_model(gen, mva_base)
end
end

Expand Down Expand Up @@ -346,7 +346,7 @@ function _make_mixed_units(data::Dict{String,Any}, mva_base::Real)
apply_func(dcline, "qmaxf", rescale)
apply_func(dcline, "qminf", rescale)

_rescale_cost_model(dcline, 1.0/mva_base, haskey(data, "phases"))
_rescale_cost_model(dcline, 1.0/mva_base)
end

if haskey(data, "gen")
Expand All @@ -360,46 +360,27 @@ function _make_mixed_units(data::Dict{String,Any}, mva_base::Real)
apply_func(gen, "qmax", rescale)
apply_func(gen, "qmin", rescale)

_rescale_cost_model(gen, 1.0/mva_base, haskey(data, "phases"))
_rescale_cost_model(gen, 1.0/mva_base)
end
end

end


""
function _rescale_cost_model(comp::Dict{String,Any}, scale::Real, multiphase::Bool)
function _rescale_cost_model(comp::Dict{String,Any}, scale::Real)
if "model" in keys(comp) && "cost" in keys(comp)
if !multiphase
if comp["model"] == 1
for i in 1:2:length(comp["cost"])
comp["cost"][i] = comp["cost"][i]/scale
end
elseif comp["model"] == 2
degree = length(comp["cost"])
for (i, item) in enumerate(comp["cost"])
comp["cost"][i] = item*(scale^(degree-i))
end
else
warn(LOGGER, "Skipping cost model of type $(comp["model"]) in per unit transformation")
if comp["model"] == 1
for i in 1:2:length(comp["cost"])
comp["cost"][i] = comp["cost"][i]/scale
end
else
phases = length(comp["model"])
for ph in 1:phases
ph_str = isa(comp["model"], PowerModels.MultiPhaseValue) ? " on phase $(ph)" : ""
if comp["model"][ph] == 1
for i in 1:2:length(comp["cost"][ph])
comp["cost"][ph][i] = comp["cost"][ph][i]/scale
end
elseif comp["model"][ph] == 2
degree = length(comp["cost"][ph])
for (i, item) in enumerate(comp["cost"][ph])
comp["cost"][ph][i] = item*(scale^(degree-i))
end
else
warn(LOGGER, "Skipping cost model of type $(comp["model"][ph])$(ph_str) in per unit transformation")
end
elseif comp["model"] == 2
degree = length(comp["cost"])
for (i, item) in enumerate(comp["cost"])
comp["cost"][i] = item*(scale^(degree-i))
end
else
warn(LOGGER, "Skipping cost model of type $(comp["model"]) in per unit transformation")
end
end
end
Expand Down Expand Up @@ -1237,7 +1218,8 @@ end
"feild names that should not be multi-phase values"
phaseless = Set(["index", "bus_i", "bus_type", "status", "gen_status",
"br_status", "gen_bus", "load_bus", "shunt_bus", "f_bus", "t_bus",
"transformer", "area", "zone", "base_kv"])
"transformer", "area", "zone", "base_kv",
"model", "ncost", "cost", "startup", "shutdown"])

phase_matrix = Set(["br_r", "br_x"])

Expand Down
110 changes: 51 additions & 59 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ function check_cost_models(pm::GenericPowerModel)
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
if haskey(gen, "cost")
for h in phase_ids(pm, n)
if model == nothing
model = gen["model"][h]
else
if gen["model"][h] != model
error("cost models are inconsistent, the typical model is $(model) however model $(gen["model"]) is given on generator $(i)")
end
if model == nothing
model = gen["model"]
else
if gen["model"] != model
error("cost models are inconsistent, the typical model is $(model) however model $(gen["model"]) is given on generator $(i)")
end
end
else
Expand All @@ -28,13 +26,11 @@ function check_cost_models(pm::GenericPowerModel)

for (i,dcline) in nw_ref[:dcline]
if haskey(dcline, "model")
for h in phase_ids(pm, n)
if model == nothing
model = dcline["model"][h]
else
if dcline["model"][h] != model
error("cost models are inconsistent, the typical model is $(model) however model $(dcline["model"]) is given on dcline $(i)")
end
if model == nothing
model = dcline["model"]
else
if dcline["model"] != model
error("cost models are inconsistent, the typical model is $(model) however model $(dcline["model"]) is given on dcline $(i)")
end
end
else
Expand Down Expand Up @@ -71,19 +67,15 @@ Checks that all cost models are polynomials, quadratic or less
function check_polynomial_cost_models(pm::GenericPowerModel)
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
for h in phase_ids(pm, n)
@assert gen["model"][h] == 2
if length(gen["cost"][h]) > 3
error("only cost models of degree 3 or less are supported at this time, given cost model of degree $(length(gen["cost"][h])) on generator $(i)")
end
@assert gen["model"] == 2
if length(gen["cost"]) > 3
error("only cost models of degree 3 or less are supported at this time, given cost model of degree $(length(gen["cost"])) on generator $(i)")
end
end
for (i,dcline) in nw_ref[:dcline]
for h in phase_ids(pm, n)
@assert dcline["model"][h] == 2
if length(dcline["cost"][h]) > 3
error("only cost models of degree 3 or less are supported at this time, given cost model of degree $(length(dcline["cost"][h])) on dcline $(i)")
end
@assert dcline["model"] == 2
if length(dcline["cost"]) > 3
error("only cost models of degree 3 or less are supported at this time, given cost model of degree $(length(dcline["cost"])) on dcline $(i)")
end
end
end
Expand All @@ -102,10 +94,12 @@ function objective_min_polynomial_fuel_cost(pm::GenericPowerModel)

return @objective(pm.model, Min,
sum(
sum(
sum( getmpv(gen["cost"],h)[1]*var(pm, n, h, :pg, i)^2 + getmpv(gen["cost"],h)[2]*var(pm, n, h, :pg, i) + getmpv(gen["cost"],h)[3] for (i,gen) in nw_ref[:gen]) +
sum(getmpv(dcline["cost"],h)[1]*var(pm, n, h, :p_dc, from_idx[n][i])^2 + getmpv(dcline["cost"],h)[2]*var(pm, n, h, :p_dc, from_idx[n][i]) + getmpv(dcline["cost"],h)[3] for (i,dcline) in nw_ref[:dcline])
for h in phase_ids(pm, n))
sum( gen["cost"][1]*sum( var(pm, n, h, :pg, i) for h in phase_ids(pm, n))^2 +
gen["cost"][2]*sum( var(pm, n, h, :pg, i) for h in phase_ids(pm, n))+
gen["cost"][3] for (i,gen) in nw_ref[:gen]) +
sum( dcline["cost"][1]*sum( var(pm, n, h, :p_dc, from_idx[n][i]) for h in phase_ids(pm, n))^2 +
dcline["cost"][2]*sum( var(pm, n, h, :p_dc, from_idx[n][i]) for h in phase_ids(pm, n)) +
dcline["cost"][3] for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end
Expand Down Expand Up @@ -147,10 +141,12 @@ function objective_min_polynomial_fuel_cost(pm::GenericPowerModel{T}) where T <:

return @objective(pm.model, Min,
sum(
sum(
sum( getmpv(gen["cost"],h)[1]*var(pm, n, h, :pg_sqr, i) + getmpv(gen["cost"],h)[2]*var(pm, n, h, :pg, i) + getmpv(gen["cost"],h)[3] for (i,gen) in nw_ref[:gen]) +
sum(getmpv(dcline["cost"],h)[1]*var(pm, n, h, :p_dc_sqr, i) + getmpv(dcline["cost"],h)[2]*var(pm, n, h, :p_dc, from_idx[n][i]) + getmpv(dcline["cost"],h)[3] for (i,dcline) in nw_ref[:dcline])
for h in phase_ids(pm, n))
sum( gen["cost"][1]*sum( var(pm, n, h, :pg_sqr, i) for h in phase_ids(pm, n)) +
gen["cost"][2]*sum( var(pm, n, h, :pg, i) for h in phase_ids(pm, n)) +
gen["cost"][3] for (i,gen) in nw_ref[:gen]) +
sum( dcline["cost"][1]*sum( var(pm, n, h, :p_dc_sqr, i) for h in phase_ids(pm, n)) +
dcline["cost"][2]*sum( var(pm, n, h, :p_dc, from_idx[n][i]) for h in phase_ids(pm, n)) +
dcline["cost"][3] for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end
Expand Down Expand Up @@ -186,11 +182,11 @@ end
compute lines in m and b from from pwl cost models
data is a list of components
"""
function get_lines(data, ph::Int)
function get_lines(data)
lines = Dict{Int,Any}()
for (i,comp) in data
@assert comp["model"][ph] == 1
line_data = slope_intercepts(getmpv(comp["cost"], ph))
@assert comp["model"] == 1
line_data = slope_intercepts(comp["cost"])
lines[i] = line_data
for i in 2:length(line_data)
if line_data[i-1]["slope"] > line_data[i]["slope"]
Expand All @@ -206,40 +202,36 @@ end
function objective_min_pwl_fuel_cost(pm::GenericPowerModel)

for (n, nw_ref) in nws(pm)
for h in phase_ids(pm, n)
pg_cost = var(pm, n, h)[:pg_cost] = @variable(pm.model,
[i in ids(pm, n, :gen)], basename="$(n)_$(h)_pg_cost"
)
pg_cost = var(pm, n)[:pg_cost] = @variable(pm.model,
[i in ids(pm, n, :gen)], basename="$(n)_pg_cost"
)

# pwl cost
gen_lines = get_lines(nw_ref[:gen], h)
for (i, gen) in nw_ref[:gen]
for line in gen_lines[i]
@constraint(pm.model, pg_cost[i] >= line["slope"]*var(pm, n, h, :pg, i) + line["intercept"])
end
# pwl cost
gen_lines = get_lines(nw_ref[:gen])
for (i, gen) in nw_ref[:gen]
for line in gen_lines[i]
@constraint(pm.model, pg_cost[i] >= line["slope"]*sum(var(pm, n, h, :pg, i) for h in phase_ids(pm, n)) + line["intercept"])
end
end

dc_p_cost = var(pm, n, h)[:p_dc_cost] = @variable(pm.model,
[i in ids(pm, n, :dcline)], basename="$(n)_$(h)_dc_p_cost",
)
dc_p_cost = var(pm, n)[:p_dc_cost] = @variable(pm.model,
[i in ids(pm, n, :dcline)], basename="$(n)_dc_p_cost",
)

# pwl cost
dcline_lines = get_lines(nw_ref[:dcline], h)
for (i, dcline) in nw_ref[:dcline]
for line in dcline_lines[i]
@constraint(pm.model, dc_p_cost[i] >= line["slope"]*var(pm, n, h, :p_dc)[i] + line["intercept"])
end
# pwl cost
dcline_lines = get_lines(nw_ref[:dcline])
for (i, dcline) in nw_ref[:dcline]
for line in dcline_lines[i]
@constraint(pm.model, dc_p_cost[i] >= line["slope"]*sum(var(pm, n, h, :p_dc)[i] for h in phase_ids(pm, n)) + line["intercept"])
end

end

end

return @objective(pm.model, Min,
sum(
sum(
sum( var(pm, n, h, :pg_cost, i) for (i,gen) in nw_ref[:gen]) +
sum( var(pm, n, h, :p_dc_cost, i) for (i,dcline) in nw_ref[:dcline])
for h in phase_ids(pm, n))
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
Expand Down
4 changes: 2 additions & 2 deletions test/multi-nw-ph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
result = PowerModels.run_mn_mp_opf(mn_mp_data, ACPPowerModel, ipopt_solver)

@test result["status"] == :LocalOptimal
@test isapprox(result["objective"], 2.63659e5; atol = 1e0)
@test isapprox(result["objective"], 2.67529e5; atol = 1e0)

@test length(result["solution"]["nw"]) == 2

Expand All @@ -179,7 +179,7 @@
result = PowerModels.run_mn_mp_opf(mn_mp_data, ACPPowerModel, ipopt_solver)

@test result["status"] == :LocalOptimal
@test isapprox(result["objective"], 1.12131e5; atol = 1e0)
@test isapprox(result["objective"], 120623.0; atol = 1e1)

@test length(result["solution"]["nw"]) == 2

Expand Down
28 changes: 14 additions & 14 deletions test/multiphase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ end
result = PowerModels.run_mp_opf(mp_data, ACPPowerModel, ipopt_solver)

@test result["status"] == :LocalOptimal
@test isapprox(result["objective"], 17720.6; atol = 1e-1)
@test isapprox(result["objective"], 47267.9; atol = 1e-1)

for ph in 1:mp_data["phases"]
@test isapprox(result["solution"]["gen"]["1"]["pg"][ph], 1.58067; atol = 1e-3)
Expand Down Expand Up @@ -240,30 +240,30 @@ end
@test_throws(TESTLOG, ErrorException, PowerModels.check_keys(mp_data_3p, ["load"]))

# check_cost_functions
mp_data_3p["gen"]["1"]["model"][2] = 1
mp_data_3p["gen"]["1"]["ncost"][2] = 1
mp_data_3p["gen"]["1"]["cost"][2] = [0.0, 1.0, 0.0]
mp_data_3p["gen"]["1"]["model"] = 1
mp_data_3p["gen"]["1"]["ncost"] = 1
mp_data_3p["gen"]["1"]["cost"] = [0.0, 1.0, 0.0]
@test_throws(TESTLOG, ErrorException, PowerModels.check_cost_functions(mp_data_3p))

mp_data_3p["gen"]["1"]["cost"][2] = [0.0, 0.0]
mp_data_3p["gen"]["1"]["cost"] = [0.0, 0.0]
@test_throws(TESTLOG, ErrorException, PowerModels.check_cost_functions(mp_data_3p))

mp_data_3p["gen"]["1"]["ncost"][2] = 2
mp_data_3p["gen"]["1"]["cost"][2] = [0.0, 0.0, 0.0, 0.0]
mp_data_3p["gen"]["1"]["ncost"] = 2
mp_data_3p["gen"]["1"]["cost"] = [0.0, 0.0, 0.0, 0.0]
@test_throws(TESTLOG, ErrorException, PowerModels.check_cost_functions(mp_data_3p))

mp_data_3p["gen"]["1"]["model"][2] = 2
mp_data_3p["gen"]["1"]["model"] = 2
@test_throws(TESTLOG, ErrorException, PowerModels.check_cost_functions(mp_data_3p))

setlevel!(TESTLOG, "info")

mp_data_3p["gen"]["1"]["model"][2] = 3
@test_warn(TESTLOG, "Skipping cost model of type 3 on phase 2 in per unit transformation", PowerModels.make_mixed_units(mp_data_3p))
@test_warn(TESTLOG, "Skipping cost model of type 3 on phase 2 in per unit transformation", PowerModels.make_per_unit(mp_data_3p))
@test_warn(TESTLOG, "Unknown phase 2 generator cost model of type 3", PowerModels.check_cost_functions(mp_data_3p))
mp_data_3p["gen"]["1"]["model"] = 3
@test_warn(TESTLOG, "Skipping cost model of type 3 in per unit transformation", PowerModels.make_mixed_units(mp_data_3p))
@test_warn(TESTLOG, "Skipping cost model of type 3 in per unit transformation", PowerModels.make_per_unit(mp_data_3p))
@test_warn(TESTLOG, "Unknown generator cost model of type 3", PowerModels.check_cost_functions(mp_data_3p))

mp_data_3p["gen"]["1"]["model"][2] = 1
mp_data_3p["gen"]["1"]["cost"][2][3] = 3000
mp_data_3p["gen"]["1"]["model"] = 1
mp_data_3p["gen"]["1"]["cost"][3] = 3000
@test_warn(TESTLOG, "pwl x value 3000.0 is outside the generator bounds 0.0-20.0", PowerModels.check_cost_functions(mp_data_3p))

@test_nowarn PowerModels.check_voltage_angle_differences(mp_data_3p)
Expand Down