Skip to content

Commit a32a228

Browse files
committedJan 16, 2017
removed TNEP data set type with refrence dict
1 parent 1c68a59 commit a32a228

File tree

7 files changed

+44
-79
lines changed

7 files changed

+44
-79
lines changed
 

‎src/core/base.jl

+17-52
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@ export
44
GenericPowerModel,
55
setdata, setsolver, solve
66

7-
type TNEPDataSets
8-
branches
9-
arcs_from
10-
arcs_to
11-
arcs
12-
bus_arcs
13-
buspairs
14-
end
15-
16-
177
abstract AbstractPowerModel
188
abstract AbstractPowerFormulation
199
abstract AbstractConicPowerFormulation <: AbstractPowerFormulation
@@ -34,39 +24,20 @@ type GenericPowerModel{T<:AbstractPowerFormulation} <: AbstractPowerModel
3424
end
3525

3626
# default generic constructor
37-
function GenericPowerModel{T}(data::Dict{AbstractString,Any}, vars::T; setting = Dict{AbstractString,Any}(), solver = JuMP.UnsetSolver(), data_processor = process_raw_mp_data)
38-
data, ref, ext = data_processor(data)
27+
function GenericPowerModel{T}(data::Dict{AbstractString,Any}, vars::T; setting = Dict{AbstractString,Any}(), solver = JuMP.UnsetSolver())
28+
3929
pm = GenericPowerModel{T}(
4030
Model(solver = solver), # model
4131
data, # data
4232
setting, # setting
4333
Dict{AbstractString,Any}(), # solution
44-
ref, # sets
45-
ext # ext
34+
build_ref(data), # refrence data
35+
Dict{Symbol,Any}() # ext
4636
)
4737

4838
return pm
4939
end
5040

51-
function process_raw_mp_data(data::Dict{AbstractString,Any})
52-
ref = build_ref(data)
53-
54-
ext = Dict{Symbol,Any}()
55-
56-
return data, ref, ext
57-
end
58-
59-
function process_raw_mp_ne_data(data::Dict{AbstractString,Any})
60-
ref = build_ref(data)
61-
ne_sets = build_ne_sets(data)
62-
63-
ext = Dict{Symbol,Any}()
64-
ext[:ne] = ne_sets
65-
66-
return data, ref, ext
67-
end
68-
69-
7041
#
7142
# Just seems too hard to maintain with the default constructor
7243
#
@@ -120,6 +91,7 @@ function run_generic_model(data::Dict{AbstractString,Any}, model_constructor, so
12091
return build_solution(pm, status, solve_time; solution_builder = solution_builder)
12192
end
12293

94+
12395
function build_ref(data::Dict{AbstractString,Any})
12496
ref = Dict{Symbol,Any}()
12597
for (key, item) in data
@@ -167,30 +139,23 @@ function build_ref(data::Dict{AbstractString,Any})
167139

168140
ref[:buspairs] = buspair_parameters(ref[:arcs_from], ref[:branch], ref[:bus])
169141

170-
return ref
171-
end
142+
if haskey(ref, :ne_branch)
143+
ref[:ne_branch] = filter((i, branch) -> branch["br_status"] == 1 && branch["f_bus"] in keys(ref[:bus]) && branch["t_bus"] in keys(ref[:bus]), ref[:ne_branch])
172144

173-
function build_ne_sets(data::Dict{AbstractString,Any})
174-
bus_lookup = Dict([(Int(bus["index"]), bus) for bus in data["bus"]])
175-
branch_lookup = Dict([(Int(branch["index"]), branch) for branch in data["ne_branch"]])
145+
ref[:ne_arcs_from] = [(i,branch["f_bus"],branch["t_bus"]) for (i,branch) in ref[:ne_branch]]
146+
ref[:ne_arcs_to] = [(i,branch["t_bus"],branch["f_bus"]) for (i,branch) in ref[:ne_branch]]
147+
ref[:ne_arcs] = [ref[:ne_arcs_from]; ref[:ne_arcs_to]]
176148

177-
# filter turned off stuff
178-
bus_lookup = filter((i, bus) -> bus["bus_type"] != 4, bus_lookup)
179-
branch_lookup = filter((i, branch) -> branch["br_status"] == 1 && branch["f_bus"] in keys(bus_lookup) && branch["t_bus"] in keys(bus_lookup), branch_lookup)
180-
181-
arcs_from = [(i,branch["f_bus"],branch["t_bus"]) for (i,branch) in branch_lookup]
182-
arcs_to = [(i,branch["t_bus"],branch["f_bus"]) for (i,branch) in branch_lookup]
183-
arcs = [arcs_from; arcs_to]
149+
ne_bus_arcs = Dict([(i, []) for (i,bus) in ref[:bus]])
150+
for (l,i,j) in ref[:ne_arcs]
151+
push!(ne_bus_arcs[i], (l,i,j))
152+
end
153+
ref[:ne_bus_arcs] = ne_bus_arcs
184154

185-
bus_arcs = Dict([(i, []) for (i,bus) in bus_lookup])
186-
for (l,i,j) in arcs_from
187-
push!(bus_arcs[i], (l,i,j))
188-
push!(bus_arcs[j], (l,j,i))
155+
ref[:ne_buspairs] = buspair_parameters(ref[:ne_arcs_from], ref[:ne_branch], ref[:bus])
189156
end
190157

191-
buspairs = buspair_parameters(arcs_from, branch_lookup, bus_lookup)
192-
193-
return TNEPDataSets(branch_lookup, arcs_from, arcs_to, arcs, bus_arcs, buspairs)
158+
return ref
194159
end
195160

196161

‎src/core/objective.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ end
2323
### Cost of building lines
2424
function objective_tnep_cost{T}(pm::GenericPowerModel{T})
2525
line_ne = getvariable(pm.model, :line_ne)
26-
branches = pm.ext[:ne].branches
26+
branches = pm.ref[:ne_branch]
2727
return @objective(pm.model, Min, sum( branches[i]["construction_cost"]*line_ne[i] for (i,branch) in branches) )
2828
end

‎src/core/variable.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function variable_active_line_flow{T}(pm::GenericPowerModel{T}; bounded = true)
8181
end
8282

8383
function variable_active_line_flow_ne{T}(pm::GenericPowerModel{T})
84-
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= p_ne[(l,i,j) in pm.ext[:ne].arcs] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "p_start"))
84+
@variable(pm.model, -pm.ref[:ne_branch][l]["rate_a"] <= p_ne[(l,i,j) in pm.ref[:ne_arcs]] <= pm.ref[:ne_branch][l]["rate_a"], start = getstart(pm.ref[:ne_branch], l, "p_start"))
8585
return p_ne
8686
end
8787

@@ -96,7 +96,7 @@ function variable_reactive_line_flow{T}(pm::GenericPowerModel{T}; bounded = true
9696
end
9797

9898
function variable_reactive_line_flow_ne{T}(pm::GenericPowerModel{T})
99-
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= q_ne[(l,i,j) in pm.ext[:ne].arcs] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "q_start"))
99+
@variable(pm.model, -pm.ref[:ne_branch][l]["rate_a"] <= q_ne[(l,i,j) in pm.ref[:ne_arcs]] <= pm.ref[:ne_branch][l]["rate_a"], start = getstart(pm.ref[:ne_branch], l, "q_start"))
100100
return q_ne
101101
end
102102

@@ -254,8 +254,8 @@ function variable_line_indicator{T}(pm::GenericPowerModel{T})
254254
end
255255

256256
function variable_line_ne{T}(pm::GenericPowerModel{T})
257-
branches = pm.ext[:ne].branches
258-
@variable(pm.model, 0 <= line_ne[l in keys(pm.ext[:ne].branches)] <= 1, Int, start = getstart(branches, l, "line_tnep_start", 1.0))
257+
branches = pm.ref[:ne_branch]
258+
@variable(pm.model, 0 <= line_ne[l in keys(branches)] <= 1, Int, start = getstart(branches, l, "line_tnep_start", 1.0))
259259
return line_ne
260260
end
261261

‎src/form/acp.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
function constraint_active_kcl_shunt_ne{T <: AbstractACPForm}(pm::GenericPowerModel{T}, bus)
6969
i = bus["index"]
7070
bus_arcs = pm.ref[:bus_arcs][i]
71-
bus_arcs_ne = pm.ext[:ne].bus_arcs[i]
71+
bus_arcs_ne = pm.ref[:ne_bus_arcs][i]
7272
bus_gens = pm.ref[:bus_gens][i]
7373

7474
v = getvariable(pm.model, :v)
@@ -97,7 +97,7 @@ end
9797
function constraint_reactive_kcl_shunt_ne{T <: AbstractACPForm}(pm::GenericPowerModel{T}, bus)
9898
i = bus["index"]
9999
bus_arcs = pm.ref[:bus_arcs][i]
100-
bus_arcs_ne = pm.ext[:ne].bus_arcs[i]
100+
bus_arcs_ne = pm.ref[:ne_bus_arcs][i]
101101
bus_gens = pm.ref[:bus_gens][i]
102102

103103
v = getvariable(pm.model, :v)

‎src/form/dcp.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ function variable_active_line_flow{T <: StandardDCPForm}(pm::GenericPowerModel{T
4747
end
4848

4949
function variable_active_line_flow_ne{T <: StandardDCPForm}(pm::GenericPowerModel{T})
50-
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= p_ne[(l,i,j) in pm.ext[:ne].arcs_from] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "p_start"))
50+
@variable(pm.model, -pm.ref[:ne_branch][l]["rate_a"] <= p_ne[(l,i,j) in pm.ref[:ne_arcs_from]] <= pm.ref[:ne_branch][l]["rate_a"], start = getstart(pm.ref[:ne_branch], l, "p_start"))
5151

52-
p_ne_expr = Dict([((l,i,j), 1.0*p_ne[(l,i,j)]) for (l,i,j) in pm.ext[:ne].arcs_from])
53-
p_ne_expr = merge(p_ne_expr, Dict([((l,j,i), -1.0*p_ne[(l,i,j)]) for (l,i,j) in pm.ext[:ne].arcs_from]))
52+
p_ne_expr = Dict([((l,i,j), 1.0*p_ne[(l,i,j)]) for (l,i,j) in pm.ref[:ne_arcs_from]])
53+
p_ne_expr = merge(p_ne_expr, Dict([((l,j,i), -1.0*p_ne[(l,i,j)]) for (l,i,j) in pm.ref[:ne_arcs_from]]))
5454

5555
pm.model.ext[:p_ne_expr] = p_ne_expr
5656
end
@@ -95,7 +95,7 @@ end
9595
function constraint_active_kcl_shunt_ne{T <: AbstractDCPForm}(pm::GenericPowerModel{T}, bus)
9696
i = bus["index"]
9797
bus_arcs = pm.ref[:bus_arcs][i]
98-
bus_arcs_ne = pm.ext[:ne].bus_arcs[i]
98+
bus_arcs_ne = pm.ref[:ne_bus_arcs][i]
9999
bus_gens = pm.ref[:bus_gens][i]
100100

101101
pg = getvariable(pm.model, :pg)
@@ -365,7 +365,7 @@ end
365365
function constraint_active_kcl_shunt_ne{T <: AbstractDCPLLForm}(pm::GenericPowerModel{T}, bus)
366366
i = bus["index"]
367367
bus_arcs = pm.ref[:bus_arcs][i]
368-
bus_arcs_ne = pm.ext[:ne].bus_arcs[i]
368+
bus_arcs_ne = pm.ref[:ne_bus_arcs][i]
369369
bus_gens = pm.ref[:bus_gens][i]
370370

371371
p = getvariable(pm.model, :p)

‎src/form/wr.jl

+13-13
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ end
6363
function constraint_active_kcl_shunt_ne{T <: AbstractWRForm}(pm::GenericPowerModel{T}, bus)
6464
i = bus["index"]
6565
bus_arcs = pm.ref[:bus_arcs][i]
66-
bus_arcs_ne = pm.ext[:ne].bus_arcs[i]
66+
bus_arcs_ne = pm.ref[:ne_bus_arcs][i]
6767
bus_gens = pm.ref[:bus_gens][i]
6868

6969
w = getvariable(pm.model, :w)
@@ -91,7 +91,7 @@ end
9191
function constraint_reactive_kcl_shunt_ne{T <: AbstractWRForm}(pm::GenericPowerModel{T}, bus)
9292
i = bus["index"]
9393
bus_arcs = pm.ref[:bus_arcs][i]
94-
bus_arcs_ne = pm.ext[:ne].bus_arcs[i]
94+
bus_arcs_ne = pm.ref[:ne_bus_arcs][i]
9595
bus_gens = pm.ref[:bus_gens][i]
9696

9797
w = getvariable(pm.model, :w)
@@ -277,9 +277,9 @@ end
277277

278278
function constraint_complex_voltage_ne{T <: AbstractWRForm}(pm::GenericPowerModel{T})
279279
buses = pm.ref[:bus]
280-
branches = pm.ext[:ne].branches
280+
branches = pm.ref[:ne_branch]
281281

282-
wr_min, wr_max, wi_min, wi_max = compute_voltage_product_bounds(pm.ext[:ne].buspairs)
282+
wr_min, wr_max, wi_min, wi_max = compute_voltage_product_bounds(pm.ref[:ne_buspairs])
283283
bi_bp = Dict([(i, (b["f_bus"], b["t_bus"])) for (i,b) in branches])
284284

285285
w = getvariable(pm.model, :w)
@@ -291,7 +291,7 @@ function constraint_complex_voltage_ne{T <: AbstractWRForm}(pm::GenericPowerMode
291291
w_to = getvariable(pm.model, :w_to_ne)
292292

293293
cs = Set()
294-
for (l,i,j) in pm.ext[:ne].arcs_from
294+
for (l,i,j) in pm.ref[:ne_arcs_from]
295295
c1 = @constraint(pm.model, w_from[l] <= z[l]*buses[branches[l]["f_bus"]]["vmax"]^2)
296296
c2 = @constraint(pm.model, w_from[l] >= z[l]*buses[branches[l]["f_bus"]]["vmin"]^2)
297297

@@ -454,23 +454,23 @@ end
454454

455455
function variable_voltage_magnitude_sqr_from_ne{T <: AbstractWRForm}(pm::GenericPowerModel{T})
456456
buses = pm.ref[:bus]
457-
branches = pm.ext[:ne].branches
458-
@variable(pm.model, 0 <= w_from_ne[i in keys(pm.ext[:ne].branches)] <= buses[branches[i]["f_bus"]]["vmax"]^2, start = getstart(pm.ref[:bus], i, "w_from_start", 1.001))
457+
branches = pm.ref[:ne_branch]
458+
@variable(pm.model, 0 <= w_from_ne[i in keys(pm.ref[:ne_branch])] <= buses[branches[i]["f_bus"]]["vmax"]^2, start = getstart(pm.ref[:bus], i, "w_from_start", 1.001))
459459
return w_from_ne
460460
end
461461

462462
function variable_voltage_magnitude_sqr_to_ne{T <: AbstractWRForm}(pm::GenericPowerModel{T})
463463
buses = pm.ref[:bus]
464-
branches = pm.ext[:ne].branches
465-
@variable(pm.model, 0 <= w_to_ne[i in keys(pm.ext[:ne].branches)] <= buses[branches[i]["t_bus"]]["vmax"]^2, start = getstart(pm.ref[:bus], i, "w_to", 1.001))
464+
branches = pm.ref[:ne_branch]
465+
@variable(pm.model, 0 <= w_to_ne[i in keys(pm.ref[:ne_branch])] <= buses[branches[i]["t_bus"]]["vmax"]^2, start = getstart(pm.ref[:bus], i, "w_to", 1.001))
466466
return w_to_ne
467467
end
468468

469469
function variable_complex_voltage_product_ne{T <: AbstractWRForm}(pm::GenericPowerModel{T})
470-
wr_min, wr_max, wi_min, wi_max = compute_voltage_product_bounds(pm.ext[:ne].buspairs)
471-
bi_bp = Dict([(i, (b["f_bus"], b["t_bus"])) for (i,b) in pm.ext[:ne].branches])
472-
@variable(pm.model, min(0, wr_min[bi_bp[b]]) <= wr_ne[b in keys(pm.ext[:ne].branches)] <= max(0, wr_max[bi_bp[b]]), start = getstart(pm.ext[:ne].buspairs, bi_bp[b], "wr_start", 1.0))
473-
@variable(pm.model, min(0, wi_min[bi_bp[b]]) <= wi_ne[b in keys(pm.ext[:ne].branches)] <= max(0, wi_max[bi_bp[b]]), start = getstart(pm.ext[:ne].buspairs, bi_bp[b], "wi_start"))
470+
wr_min, wr_max, wi_min, wi_max = compute_voltage_product_bounds(pm.ref[:ne_buspairs])
471+
bi_bp = Dict([(i, (b["f_bus"], b["t_bus"])) for (i,b) in pm.ref[:ne_branch]])
472+
@variable(pm.model, min(0, wr_min[bi_bp[b]]) <= wr_ne[b in keys(pm.ref[:ne_branch])] <= max(0, wr_max[bi_bp[b]]), start = getstart(pm.ref[:ne_buspairs], bi_bp[b], "wr_start", 1.0))
473+
@variable(pm.model, min(0, wi_min[bi_bp[b]]) <= wi_ne[b in keys(pm.ref[:ne_branch])] <= max(0, wi_max[bi_bp[b]]), start = getstart(pm.ref[:ne_buspairs], bi_bp[b], "wi_start"))
474474
return wr_ne, wi_ne
475475
end
476476

‎src/prob/tnep.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export run_tnep
66

77
function run_tnep(file, model_constructor, solver; kwargs...)
8-
return run_generic_model(file, model_constructor, solver, post_tnep; data_processor = process_raw_mp_ne_data, solution_builder = get_tnep_solution, kwargs...)
8+
return run_generic_model(file, model_constructor, solver, post_tnep; solution_builder = get_tnep_solution, kwargs...)
99
end
1010

1111
# the general form of the tnep optimization model
@@ -35,7 +35,7 @@ function post_tnep{T}(pm::GenericPowerModel{T})
3535
constraint_reactive_kcl_shunt_ne(pm, bus)
3636
end
3737

38-
for (i,branch) in pm.ext[:ne].branches
38+
for (i,branch) in pm.ref[:ne_branch]
3939
constraint_active_ohms_yt_ne(pm, branch)
4040
constraint_reactive_ohms_yt_ne(pm, branch)
4141

0 commit comments

Comments
 (0)
Please sign in to comment.