Skip to content

Commit

Permalink
Merge pull request #234 from bknueven/fixfrom224
Browse files Browse the repository at this point in the history
Hooks for UC as subblock
  • Loading branch information
jeanpaulwatson authored Jun 15, 2021
2 parents 40a2a26 + effadcc commit dbf9b2e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
44 changes: 27 additions & 17 deletions egret/common/lazy_ptdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,9 @@ def _generate_contingency_bounds(mb, cn, minimum_limit, maximum_limit):

## violation adder
def add_violations(lazy_violations, flows, mb, md, solver, ptdf_options,
PTDF, time=None, prepend_str=""):

PTDF, time=None, prepend_str="", obj_multi=None):
# obj_multi is for models where this
# UC instance is part of a larger model
if time is None:
model = mb
else:
Expand Down Expand Up @@ -619,11 +620,14 @@ def add_violations(lazy_violations, flows, mb, md, solver, ptdf_options,
obj_coef = m.TimePeriodLengthHours*m.BranchLimitPenalty[bn]

if persistent_solver:
if m is not m.model():
m_model = m.model()
if m is not m_model and obj_multi is None:
raise RuntimeError("Cannot add lazy var for branch slacks if part of a larger model")
if obj_multi is not None:
obj_coef = obj_multi*obj_coef
## update the objective through the add_column method
solver.add_column(m, mb.pf_slack_pos[bn], obj_coef, [], [])
solver.add_column(m, mb.pf_slack_neg[bn], obj_coef, [], [])
solver.add_column(m_model, mb.pf_slack_pos[bn], obj_coef, [], [])
solver.add_column(m_model, mb.pf_slack_neg[bn], obj_coef, [], [])
else:
m.BranchViolationCost[time].expr += ( obj_coef*mb.pf_slack_pos[bn] + \
obj_coef*mb.pf_slack_neg[bn] )
Expand All @@ -632,14 +636,14 @@ def add_violations(lazy_violations, flows, mb, md, solver, ptdf_options,

_add_interface_violations(lazy_violations, flows, mb, md, solver, ptdf_options,
PTDF, model, baseMVA, persistent_solver, rel_ptdf_tol, abs_ptdf_tol,
time, prepend_str)
time, prepend_str, obj_multi)
_add_contingency_violations(lazy_violations, flows, mb, md, solver, ptdf_options,
PTDF, model, baseMVA, persistent_solver, rel_ptdf_tol, abs_ptdf_tol,
time, prepend_str)
time, prepend_str, obj_multi)

def _add_interface_violations(lazy_violations, flows, mb, md, solver, ptdf_options,
PTDF, model, baseMVA, persistent_solver, rel_ptdf_tol, abs_ptdf_tol,
time, prepend_str):
time, prepend_str, obj_multi):
## in case there's no interfaces
if not hasattr(mb, 'ineq_pf_interface_bounds'):
return
Expand All @@ -659,11 +663,14 @@ def _add_interface_violations(lazy_violations, flows, mb, md, solver, ptdf_optio
obj_coef = m.TimePeriodLengthHours*m.InterfaceLimitPenalty[i_n]

if persistent_solver:
if m is not m.model():
raise RuntimeError("Cannot add lazy var for branch slacks if part of a larger model")
m_model = m.model()
if m is not m_model and obj_multi is None:
raise RuntimeError("Cannot add lazy var for interface slacks if part of a larger model")
if obj_multi is not None:
obj_coef = obj_multi*obj_coef
## update the objective through the add_column method
solver.add_column(m, mb.pfi_slack_pos[i_n], obj_coef, [], [])
solver.add_column(m, mb.pfi_slack_neg[i_n], obj_coef, [], [])
solver.add_column(m_model, mb.pfi_slack_pos[i_n], obj_coef, [], [])
solver.add_column(m_model, mb.pfi_slack_neg[i_n], obj_coef, [], [])
else:
m.InterfaceViolationCost[time].expr += (obj_coef*mb.pfi_slack_pos[i_n] + \
obj_coef*mb.pfi_slack_neg[i_n] )
Expand All @@ -673,7 +680,7 @@ def _add_interface_violations(lazy_violations, flows, mb, md, solver, ptdf_optio

def _add_contingency_violations(lazy_violations, flows, mb, md, solver, ptdf_options,
PTDF, model, baseMVA, persistent_solver, rel_ptdf_tol, abs_ptdf_tol,
time, prepend_str):
time, prepend_str, obj_multi):
## in case there's no contingencies
if not hasattr(mb, 'ineq_pf_contingency_branch_thermal_bounds'):
return
Expand All @@ -689,11 +696,14 @@ def _add_contingency_violations(lazy_violations, flows, mb, md, solver, ptdf_opt
obj_coef = pyo.value(m.TimePeriodLengthHours*m.ContingencyLimitPenalty)

if persistent_solver:
if m is not m.model():
raise RuntimeError("Cannot add lazy var for branch slacks if part of a larger model")
m_model = m.model()
if m is not m_model and obj_multi is None:
raise RuntimeError("Cannot add lazy var for branch contingency slacks if part of a larger model")
if obj_multi is not None:
obj_coef = obj_multi*obj_coef
## update the objective through the add_column method
solver.add_column(m, mb.pfc_slack_pos[cn,bn], obj_coef, [], [])
solver.add_column(m, mb.pfc_slack_neg[cn,bn], obj_coef, [], [])
solver.add_column(m_model, mb.pfc_slack_pos[cn,bn], obj_coef, [], [])
solver.add_column(m_model, mb.pfc_slack_neg[cn,bn], obj_coef, [], [])
else:
m.ContingencyViolationCost[time].expr += (obj_coef*mb.pfc_slack_pos[cn,bn] + \
obj_coef*mb.pfc_slack_neg[cn,bn] )
Expand Down
14 changes: 10 additions & 4 deletions egret/models/unit_commitment.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,9 @@ def create_super_tight_unit_commitment_model(model_data,
return _get_uc_model(model_data, formulation_list, relaxed)


def _lazy_ptdf_warmstart_copy_violations(m, md, t_subset, solver, ptdf_options, prepend_str):
def _lazy_ptdf_warmstart_copy_violations(m, md, t_subset, solver, ptdf_options, prepend_str, obj_multi=None):
# obj_multi is for models where this
# UC instance is part of a larger model
if len(t_subset) > 1:
raise Exception("_lazy_ptdf_warmstart_copy_violations only handles t_subset with one element -- somebody should fix that!")
slacks = None
Expand Down Expand Up @@ -682,7 +684,7 @@ def _lazy_ptdf_warmstart_copy_violations(m, md, t_subset, solver, ptdf_options,
flows[t_o], viol_lazy[t_o] = lpu.copy_active_to_next_time(m, b_other, PTDF_other, slacks, slacks_I, slacks_C)

logger.debug(prepend_str+"adding {0} flow constraints at time {1}".format(len(viol_lazy[t_o]),t_o))
lpu.add_violations(viol_lazy[t_o], flows[t_o], b_other, md, solver, ptdf_options, PTDF_other, time=t_o, prepend_str=prepend_str)
lpu.add_violations(viol_lazy[t_o], flows[t_o], b_other, md, solver, ptdf_options, PTDF_other, time=t_o, prepend_str=prepend_str, obj_multi=obj_multi)
total_flow_constr_added += len(viol_lazy[t_o])
logger.info(prepend_str+"added {0} flow constraint(s)".format(total_flow_constr_added))

Expand All @@ -699,6 +701,7 @@ def _lazy_ptdf_solve(m, solver, persistent_solver, symbolic_solver_labels, solve
else:
results = solver.solve(main_model, tee=solver_tee, symbolic_solver_labels=symbolic_solver_labels, load_solutions=False, **solve_method_options)
m.solutions.load_from(results)
return results

def _lazy_ptdf_normal_terminatation(all_viol_in_model, results, i, prepend_str):
if all_viol_in_model:
Expand Down Expand Up @@ -763,13 +766,16 @@ def _lazy_ptdf_termination_cleanup(m, md, time_periods, solver, ptdf_options, pr
results = _lazy_ptdf_solve(m, solver, persistent_solver, symbolic_solver_labels, solver_tee, vars_to_load, solve_method_options)
if persistent_solver and duals and (results is not None) and (vars_to_load is None):
solver.load_duals()
return results

def _lazy_ptdf_violation_adder(m, md, flows, viol_lazy, time_periods, solver, ptdf_options, prepend_str,i):
def _lazy_ptdf_violation_adder(m, md, flows, viol_lazy, time_periods, solver, ptdf_options, prepend_str, i, obj_multi=None):
# obj_multi is for models where this
# UC instance is part of a larger model
total_flow_constr_added = 0
for t in time_periods:
b = m.TransmissionBlock[t]
PTDF = b._PTDF
lpu.add_violations(viol_lazy[t], flows[t], b, md, solver, ptdf_options, PTDF, time=t, prepend_str=prepend_str)
lpu.add_violations(viol_lazy[t], flows[t], b, md, solver, ptdf_options, PTDF, time=t, prepend_str=prepend_str,obj_multi=obj_multi)
total_flow_constr_added += len(viol_lazy[t])
logger.info(prepend_str+"iteration {0}, added {1} flow constraint(s)".format(i,total_flow_constr_added))

Expand Down

0 comments on commit dbf9b2e

Please sign in to comment.