Skip to content

Commit

Permalink
Merge pull request #53 from intvenlab/auto_turn_on_load_shed
Browse files Browse the repository at this point in the history
  • Loading branch information
danielolsen authored Jun 10, 2020
2 parents 0dcb381 + 56d8ee7 commit e30b548
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
16 changes: 6 additions & 10 deletions pyreisejl/utility/extract_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def extract_data(scenario_info):
solve_time = []
optimize_time = []

extraction_vars = ['pf', 'pg', 'lmp', 'congu', 'congl']
extraction_vars = {'pf', 'pg', 'lmp', 'congu', 'congl'}
sparse_extraction_vars = {'congu', 'congl', 'load_shed'}
temps = {}
outputs = {}
Expand All @@ -106,7 +106,7 @@ def extract_data(scenario_info):
except KeyError:
pass

demand_scaling = output['mdo_save']['demand_scaling'][0]
demand_scaling = output['mdo_save']['demand_scaling'][0][0]
if demand_scaling < 1:
demand_change = round(100 * (1 - demand_scaling))
infeasibilities.append('%s:%s' % (str(i), str(demand_change)))
Expand All @@ -118,26 +118,22 @@ def extract_data(scenario_info):
temps['congl'] = output_mpc['branch']['MU_ST'].T
try:
temps['pf_dcline'] = output_mpc['dcline']['PF_dcline'].T
if i == 0:
extraction_vars.append('pf_dcline')
extraction_vars |= {'pf_dcline'}
except KeyError:
pass
try:
temps['storage_pg'] = output_mpc['storage']['PG'].T
temps['storage_e'] = output_mpc['storage']['Energy'].T
if i == 0:
extraction_vars.append('storage_pg')
extraction_vars.append('storage_e')
extraction_vars |= {'storage_pg', 'storage_e'}
except KeyError:
pass
try:
temps['load_shed'] = output_mpc['load_shed']['load_shed'].T
if i == 0:
extraction_vars.append('load_shed')
extraction_vars |= {'load_shed'}
except KeyError:
pass
for v in extraction_vars:
if i == 0:
if v not in outputs:
interval_length, n_columns = temps[v].shape
total_length = end_index * interval_length
outputs[v] = pd.DataFrame(np.zeros((total_length, n_columns)))
Expand Down
61 changes: 38 additions & 23 deletions src/loop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict,
n_interval::Int, start_index::Int,
inputfolder::String, outputfolder::String)
# Bad (but known) statuses to match against
bad_statuses = (
JuMP.MOI.INFEASIBLE, JuMP.MOI.INFEASIBLE_OR_UNBOUNDED,
JuMP.MOI.NUMERICAL_ERROR, JuMP.MOI.OTHER_LIMIT,
)
numeric_statuses = (
JuMP.MOI.INFEASIBLE_OR_UNBOUNDED, JuMP.MOI.NUMERICAL_ERROR,
JuMP.MOI.OTHER_LIMIT)
infeasible_statuses = (
JuMP.MOI.INFEASIBLE, JuMP.MOI.INFEASIBLE_OR_UNBOUNDED)
# Constant parameters
case = model_kwargs["case"]
storage = model_kwargs["storage"]
Expand All @@ -33,7 +34,8 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict,
for i in 1:n_interval
# These must be declared global so that they persist through the loop.
global m, voi, pg0, storage_e0
model_kwargs["demand_scaling"] = 1.0
@show ("load_shed_enabled" in keys(model_kwargs))
@show ("BarHomogeneous" in keys(solver_kwargs))
interval_start = start_index + (i - 1) * interval
interval_end = interval_start + interval - 1
model_kwargs["start_index"] = interval_start
Expand Down Expand Up @@ -104,32 +106,46 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict,
end
end

# The demand_scaling decrement _should_ only be triggered w/o load_shed
while true
global results
JuMP.optimize!(m)
status = JuMP.termination_status(m)
if status == JuMP.MOI.OPTIMAL
if status == JuMP.MOI.OPTIMAL
f = JuMP.objective_value(m)
results = get_results(f, voi, model_kwargs["case"])
break
elseif status in bad_statuses
model_kwargs["demand_scaling"] -= 0.05
if model_kwargs["demand_scaling"] < 0
error("Too many demand reductions, demand is at zero!")
end
println("Optimization failed, Reducing demand: "
* string(model_kwargs["demand_scaling"]))
bus_demand = _make_bus_demand(
case, interval_start, interval_end)
bus_demand *= model_kwargs["demand_scaling"]
for t in 1:interval, b in sets.load_bus_idx
JuMP.set_normalized_rhs(
voi.powerbalance[b, t], bus_demand[b, t])
end
elseif ((status in numeric_statuses)
& !("BarHomogeneous" in keys(solver_kwargs)))
# if BarHomogeneous is not enabled, enable it and re-build
solver_kwargs["BarHomogeneous"] = 1
println("enable BarHomogeneous")
JuMP.set_parameter(m, "BarHomogeneous", 1)
elseif ((status in infeasible_statuses)
& !("load_shed_enabled" in keys(model_kwargs)))
# if load shed not enabled, enable it and re-build the model
model_kwargs["load_shed_enabled"] = true
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...)
s_kwargs = (; (Symbol(k) => v for (k,v) in solver_kwargs)...)
println("rebuild with load shed")
m = JuMP.direct_model(Gurobi.Optimizer(env; s_kwargs...))
m, voi = _build_model(m; m_kwargs...)
elseif !("BarHomogeneous" in keys(solver_kwargs))
# if BarHomogeneous is not enabled, enable it and re-build
solver_kwargs["BarHomogeneous"] = 1
println("enable BarHomogeneous")
JuMP.set_parameter(m, "BarHomogeneous", 1)
elseif !("load_shed_enabled" in keys(model_kwargs))
model_kwargs["load_shed_enabled"] = true
m_kwargs = (; (Symbol(k) => v for (k,v) in model_kwargs)...)
s_kwargs = (; (Symbol(k) => v for (k,v) in solver_kwargs)...)
println("rebuild with load shed")
m = JuMP.direct_model(Gurobi.Optimizer(env; s_kwargs...))
m, voi = _build_model(m; m_kwargs...)
else
# Something has gone very wrong
@show status
@show keys(model_kwargs)
@show keys(solver_kwargs)
@show JuMP.objective_value(m)
if (("load_shed_enabled" in keys(model_kwargs))
&& (model_kwargs["load_shed_enabled"] == true))
Expand All @@ -155,7 +171,6 @@ function interval_loop(env::Gurobi.Env, model_kwargs::Dict,
# Save results
results_filename = "result_" * string(i-1) * ".mat"
results_filepath = joinpath(outputfolder, results_filename)
save_results(results, results_filepath;
demand_scaling=model_kwargs["demand_scaling"])
save_results(results, results_filepath)
end
end

0 comments on commit e30b548

Please sign in to comment.