-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Optim via JuMP to Debugging Models (#61)
* add jump model with optim solver for debugging * update variants manifest
- Loading branch information
Showing
2 changed files
with
157 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
time_start = time() | ||
|
||
using PowerModels | ||
using JuMP | ||
using Optim | ||
|
||
pkg_load_time = time() - time_start | ||
|
||
|
||
time_start = time() | ||
|
||
file_name = "../data/pglib_opf_case5_pjm.m" | ||
#file_name = "../data/pglib_opf_case118_ieee.m" | ||
|
||
data = PowerModels.parse_file(file_name) | ||
PowerModels.standardize_cost_terms!(data, order=2) | ||
PowerModels.calc_thermal_limits!(data) | ||
ref = PowerModels.build_ref(data)[:it][:pm][:nw][0] | ||
|
||
data_load_time = time() - time_start | ||
|
||
|
||
time_start = time() | ||
|
||
model = JuMP.Model() | ||
|
||
@variable(model, va[i in keys(ref[:bus])]) | ||
@variable(model, ref[:bus][i]["vmin"] <= vm[i in keys(ref[:bus])] <= ref[:bus][i]["vmax"], start=1.0) | ||
|
||
@variable(model, ref[:gen][i]["pmin"] <= pg[i in keys(ref[:gen])] <= ref[:gen][i]["pmax"], start=(ref[:gen][i]["pmin"] + ref[:gen][i]["pmax"])/2) | ||
@variable(model, ref[:gen][i]["qmin"] <= qg[i in keys(ref[:gen])] <= ref[:gen][i]["qmax"], start=(ref[:gen][i]["qmin"] + ref[:gen][i]["qmax"])/2) | ||
|
||
@variable(model, -ref[:branch][l]["rate_a"] <= p[(l,i,j) in ref[:arcs]] <= ref[:branch][l]["rate_a"]) | ||
@variable(model, -ref[:branch][l]["rate_a"] <= q[(l,i,j) in ref[:arcs]] <= ref[:branch][l]["rate_a"]) | ||
|
||
|
||
@objective(model, Min, sum(gen["cost"][1]*pg[i]^2 + gen["cost"][2]*pg[i] + gen["cost"][3] for (i,gen) in ref[:gen])) | ||
|
||
|
||
for (i,bus) in ref[:ref_buses] | ||
@constraint(model, va[i] == 0) | ||
end | ||
|
||
for (i,bus) in ref[:bus] | ||
bus_loads = [ref[:load][l] for l in ref[:bus_loads][i]] | ||
bus_shunts = [ref[:shunt][s] for s in ref[:bus_shunts][i]] | ||
|
||
@constraint(model, | ||
sum(p[a] for a in ref[:bus_arcs][i]) == | ||
sum(pg[g] for g in ref[:bus_gens][i]) - | ||
sum(load["pd"] for load in bus_loads) - | ||
sum(shunt["gs"] for shunt in bus_shunts)*vm[i]^2 | ||
) | ||
|
||
@constraint(model, | ||
sum(q[a] for a in ref[:bus_arcs][i]) == | ||
sum(qg[g] for g in ref[:bus_gens][i]) - | ||
sum(load["qd"] for load in bus_loads) + | ||
sum(shunt["bs"] for shunt in bus_shunts)*vm[i]^2 | ||
) | ||
end | ||
|
||
# Branch power flow physics and limit constraints | ||
for (i,branch) in ref[:branch] | ||
f_idx = (i, branch["f_bus"], branch["t_bus"]) | ||
t_idx = (i, branch["t_bus"], branch["f_bus"]) | ||
|
||
p_fr = p[f_idx] # p_fr is a reference to the optimization variable p[f_idx] | ||
q_fr = q[f_idx] # q_fr is a reference to the optimization variable q[f_idx] | ||
p_to = p[t_idx] # p_to is a reference to the optimization variable p[t_idx] | ||
q_to = q[t_idx] # q_to is a reference to the optimization variable q[t_idx] | ||
|
||
vm_fr = vm[branch["f_bus"]] # vm_fr is a reference to the optimization variable vm on the from side of the branch | ||
vm_to = vm[branch["t_bus"]] # vm_to is a reference to the optimization variable vm on the to side of the branch | ||
va_fr = va[branch["f_bus"]] # va_fr is a reference to the optimization variable va on the from side of the branch | ||
va_to = va[branch["t_bus"]] # va_fr is a reference to the optimization variable va on the to side of the branch | ||
|
||
g, b = PowerModels.calc_branch_y(branch) | ||
tr, ti = PowerModels.calc_branch_t(branch) | ||
tm = tr^2 + ti^2 | ||
g_fr = branch["g_fr"] | ||
b_fr = branch["b_fr"] | ||
g_to = branch["g_to"] | ||
b_to = branch["b_to"] | ||
|
||
# From side of the branch flow | ||
@constraint(model, p_fr == (g+g_fr)/tm*vm_fr^2 + (-g*tr+b*ti)/tm*(vm_fr*vm_to*cos(va_fr-va_to)) + (-b*tr-g*ti)/tm*(vm_fr*vm_to*sin(va_fr-va_to)) ) | ||
@constraint(model, q_fr == -(b+b_fr)/tm*vm_fr^2 - (-b*tr-g*ti)/tm*(vm_fr*vm_to*cos(va_fr-va_to)) + (-g*tr+b*ti)/tm*(vm_fr*vm_to*sin(va_fr-va_to)) ) | ||
|
||
# To side of the branch flow | ||
@constraint(model, p_to == (g+g_to)*vm_to^2 + (-g*tr-b*ti)/tm*(vm_to*vm_fr*cos(va_to-va_fr)) + (-b*tr+g*ti)/tm*(vm_to*vm_fr*sin(va_to-va_fr)) ) | ||
@constraint(model, q_to == -(b+b_to)*vm_to^2 - (-b*tr+g*ti)/tm*(vm_to*vm_fr*cos(va_fr-va_to)) + (-g*tr-b*ti)/tm*(vm_to*vm_fr*sin(va_to-va_fr)) ) | ||
|
||
# Voltage angle difference limit | ||
@constraint(model, branch["angmin"] <= va_fr - va_to <= branch["angmax"]) | ||
|
||
# Apparent power limit, from side and to side | ||
@constraint(model, p_fr^2 + q_fr^2 <= branch["rate_a"]^2) | ||
@constraint(model, p_to^2 + q_to^2 <= branch["rate_a"]^2) | ||
end | ||
|
||
|
||
set_optimizer(model, Optim.Optimizer) | ||
set_optimizer_attribute(model, "method", IPNewton()) | ||
set_optimizer_attribute(model, "show_trace", true) | ||
|
||
model_build_time = time() - time_start | ||
|
||
|
||
time_start = time() | ||
|
||
JuMP.optimize!(model) | ||
cost = JuMP.objective_value(model) | ||
feasible = JuMP.termination_status(model) | ||
|
||
solve_time = time() - time_start | ||
|
||
|
||
println("") | ||
println("\033[1mSummary\033[0m") | ||
println(" case..........: $(file_name)") | ||
println(" cost..........: $(round(Int, cost))") | ||
println(" feasible......: $(feasible)") | ||
println(" primal status.: $(JuMP.primal_status(model))") | ||
println(" dual status...: $(JuMP.dual_status(model))") | ||
println(" pkg time......: $(pkg_load_time)") | ||
println(" data time.....: $(data_load_time)") | ||
println(" build time....: $(model_build_time)") | ||
println(" solve time....: $(solve_time)") | ||
println("") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters