-
Notifications
You must be signed in to change notification settings - Fork 12
/
jump.jl
executable file
·175 lines (144 loc) · 6.68 KB
/
jump.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env julia
###### AC-OPF using JuMP ######
#
# implementation reference: https://github.com/lanl-ansi/PowerModelsAnnex.jl/blob/master/src/model/ac-opf.jl
# only the built-in AD library is supported
#
import PowerModels
import Ipopt
import JuMP
function solve_opf(file_name)
time_data_start = time()
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_data_start
time_model_start = time()
model = JuMP.Model(Ipopt.Optimizer)
#JuMP.set_optimizer_attribute(model, "print_level", 0)
JuMP.@variable(model, va[i in keys(ref[:bus])])
JuMP.@variable(model, ref[:bus][i]["vmin"] <= vm[i in keys(ref[:bus])] <= ref[:bus][i]["vmax"], start=1.0)
for i in keys(ref[:bus])
JuMP.set_name(va[i], "va_$i")
JuMP.set_name(vm[i], "vm_$i")
end
JuMP.@variable(model, ref[:gen][i]["pmin"] <= pg[i in keys(ref[:gen])] <= ref[:gen][i]["pmax"])
JuMP.@variable(model, ref[:gen][i]["qmin"] <= qg[i in keys(ref[:gen])] <= ref[:gen][i]["qmax"])
for i in keys(ref[:gen])
JuMP.set_name(pg[i], "pg_$i")
JuMP.set_name(qg[i], "qg_$i")
end
JuMP.@variable(model, -ref[:branch][l]["rate_a"] <= p[(l,i,j) in ref[:arcs]] <= ref[:branch][l]["rate_a"])
JuMP.@variable(model, -ref[:branch][l]["rate_a"] <= q[(l,i,j) in ref[:arcs]] <= ref[:branch][l]["rate_a"])
for (l, i, j) in ref[:arcs]
JuMP.set_name(p[(l, i, j)], "p_$(l)_$(i)_$(j)")
JuMP.set_name(q[(l, i, j)], "q_$(l)_$(i)_$(j)")
end
JuMP.@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]
JuMP.@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]]
JuMP.@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
)
JuMP.@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]
q_fr = q[f_idx]
p_to = p[t_idx]
q_to = q[t_idx]
vm_fr = vm[branch["f_bus"]]
vm_to = vm[branch["t_bus"]]
va_fr = va[branch["f_bus"]]
va_to = va[branch["t_bus"]]
g, b = PowerModels.calc_branch_y(branch)
tr, ti = PowerModels.calc_branch_t(branch)
ttm = 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
JuMP.@constraint(model, p_fr == (g+g_fr)/ttm*vm_fr^2 + (-g*tr+b*ti)/ttm*(vm_fr*vm_to*cos(va_fr-va_to)) + (-b*tr-g*ti)/ttm*(vm_fr*vm_to*sin(va_fr-va_to)) )
JuMP.@constraint(model, q_fr == -(b+b_fr)/ttm*vm_fr^2 - (-b*tr-g*ti)/ttm*(vm_fr*vm_to*cos(va_fr-va_to)) + (-g*tr+b*ti)/ttm*(vm_fr*vm_to*sin(va_fr-va_to)) )
# To side of the branch flow
JuMP.@constraint(model, p_to == (g+g_to)*vm_to^2 + (-g*tr-b*ti)/ttm*(vm_to*vm_fr*cos(va_to-va_fr)) + (-b*tr+g*ti)/ttm*(vm_to*vm_fr*sin(va_to-va_fr)) )
JuMP.@constraint(model, q_to == -(b+b_to)*vm_to^2 - (-b*tr+g*ti)/ttm*(vm_to*vm_fr*cos(va_to-va_fr)) + (-g*tr-b*ti)/ttm*(vm_to*vm_fr*sin(va_to-va_fr)) )
# Voltage angle difference limit
JuMP.@constraint(model, branch["angmin"] <= va_fr - va_to <= branch["angmax"])
# Apparent power limit, from side and to side
JuMP.@constraint(model, p_fr^2 + q_fr^2 <= branch["rate_a"]^2)
JuMP.@constraint(model, p_to^2 + q_to^2 <= branch["rate_a"]^2)
end
model_variables = JuMP.num_variables(model)
# for consistency with other solvers, skip the variable bounds in the constraint count
model_constraints = JuMP.num_constraints(model; count_variable_in_set_constraints = false)
model_build_time = time() - time_model_start
time_solve_start = time()
JuMP.optimize!(model)
cost = JuMP.objective_value(model)
feasible = (JuMP.termination_status(model) == JuMP.LOCALLY_SOLVED)
solve_time = time() - time_solve_start
total_time = time() - time_data_start
nlp_block = JuMP.MOI.get(JuMP.unsafe_backend(model), JuMP.MOI.NLPBlock())
total_callback_time =
nlp_block.evaluator.eval_objective_timer +
nlp_block.evaluator.eval_objective_gradient_timer +
nlp_block.evaluator.eval_constraint_timer +
nlp_block.evaluator.eval_constraint_jacobian_timer +
nlp_block.evaluator.eval_hessian_lagrangian_timer
println("")
println("\033[1mSummary\033[0m")
println(" case........: $(file_name)")
println(" variables...: $(model_variables)")
println(" constraints.: $(model_constraints)")
println(" feasible....: $(feasible)")
println(" cost........: $(round(Int, cost))")
println(" total time..: $(total_time)")
println(" data time.: $(data_load_time)")
println(" build time: $(model_build_time)")
println(" solve time: $(solve_time)")
println(" callbacks: $(total_callback_time)")
println("")
println(" callbacks time:")
println(" * obj.....: $(nlp_block.evaluator.eval_objective_timer)")
println(" * grad....: $(nlp_block.evaluator.eval_objective_gradient_timer)")
println(" * cons....: $(nlp_block.evaluator.eval_constraint_timer)")
println(" * jac.....: $(nlp_block.evaluator.eval_constraint_jacobian_timer)")
println(" * hesslag.: $(nlp_block.evaluator.eval_hessian_lagrangian_timer)")
println("")
return Dict(
"case" => file_name,
"variables" => model_variables,
"constraints" => model_constraints,
"feasible" => feasible,
"cost" => cost,
"time_total" => total_time,
"time_data" => data_load_time,
"time_build" => model_build_time,
"time_solve" => solve_time,
"time_callbacks" => total_callback_time,
"solution" => Dict(
JuMP.name(v) => JuMP.value(v) for v in JuMP.all_variables(model)
),
)
end
if isinteractive() == false
solve_opf("$(@__DIR__)/data/opf_warmup.m")
end