Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'disjunction_disaggregation_constraints'] \
= ComponentMap()
else:
infodict = parent._gdp_transformation_info = {}
orConstraintMap = infodict['disjunction_or_constraint'] \
= ComponentMap()
disaggregationConstraintMap = infodict[
'disjunction_disaggregation_constraints'] \
= ComponentMap()
if disjunction in disaggregationConstraintMap:
disaggregationConstraint = disaggregationConstraintMap[disjunction]
else:
# add the disaggregation constraint
disaggregationConstraint \
= disaggregationConstraintMap[disjunction] = Constraint(Any)
parent.add_component(
unique_component_name(
parent, '_gdp_chull_relaxation_' + disjunction.getname(
fully_qualified=True, name_buffer=NAME_BUFFER
) + '_disaggregation'),
disaggregationConstraint)
# If the Constraint already exists, return it
if disjunction in orConstraintMap:
orC = orConstraintMap[disjunction]
else:
# add the XOR (or OR) constraints to parent block (with
# unique name) It's indexed if this is an
# IndexedDisjunction, not otherwise
orC = Constraint(disjunction.index_set()) if \
disjunction.is_indexed() else Constraint()
rule=res_process_capacity_rule,
doc='process.cap-lo <= total process capacity <= process.cap-up')
m.res_sell_buy_symmetry = pyomo.Constraint(
m.pro_input_tuples,
rule=res_sell_buy_symmetry_rule,
doc='total power connection capacity must be symmetric in both directions')
m.res_process_throughput_by_partial_1 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=res_process_throughput_by_partial_1_rule,
doc='partial * (process_capacity or 0) <= process throughput ')
m.res_process_throughput_by_partial_2 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=res_process_throughput_by_partial_2_rule,
doc='process throughput <= (process_capacity or 0) ')
m.def_cap_pro_piecewise_1 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_cap_pro_piecewise_1_rule,
doc='process piecewise capacity <= process capacity')
m.def_cap_pro_piecewise_2 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_cap_pro_piecewise_2_rule,
doc='process piecewise capacity <= process.cap-up * online status')
m.def_cap_pro_piecewise_3 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_cap_pro_piecewise_3_rule,
doc='process piecewise capacity >= process capacity - \
process.cap-up * (1 - online status)')
m.def_startupcostfactor_1 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_startupcostfactor_1_rule,
doc='rule 1 for startupcostfactor')
doc="A mapping of variables to their bounds within an "
"active disjunctive scope "
"(when the relevant disjunct is active).")
# fix the disjunct to active, deactivate all nonlinear constraints,
# and apply the big-M transformation
old_disjunct_state = {'fixed': disjunct.indicator_var.fixed,
'value': disjunct.indicator_var.value}
disjunct.indicator_var.fix(1)
model._tmp_var_set = ComponentSet()
model._tmp_constr_deactivated = ComponentSet()
# Maps a variable in a cloned model instance to the original model
# variable
for constraint in disjunct.component_data_objects(
ctype=Constraint, active=True, descend_into=True):
if constraint.body.polynomial_degree() in [0, 1]:
model._tmp_var_set.update(
identify_variables(constraint.body))
model._var_list = list(model._tmp_var_set)
bigM_model = model.clone()
new_var_to_orig = ComponentMap(
zip(bigM_model._var_list, model._var_list))
for constraint in bigM_model.component_data_objects(
ctype=Constraint, active=True,
descend_into=(Block, Disjunct)):
if constraint.body.polynomial_degree() not in [0, 1]:
constraint.deactivate()
TransformationFactory('gdp.bigm').apply_to(bigM_model)
for var in bigM_model._tmp_var_set:
# If variable is fixed, no need to calculate disjunctive bounds
process.cap-up * (1 - online status)')
m.def_startupcostfactor_1 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_startupcostfactor_1_rule,
doc='rule 1 for startupcostfactor')
m.def_startupcostfactor_2 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_startupcostfactor_2_rule,
doc='rule 2 for startupcostfactor')
m.def_startupcostfactor_3 = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=def_startupcostfactor_3_rule,
doc='rule 3 for startupcostfactor')
# transmission
m.def_transmission_capacity = pyomo.Constraint(
m.tra_tuples,
rule=def_transmission_capacity_rule,
doc='total transmission capacity = inst-cap + new capacity')
m.def_transmission_output = pyomo.Constraint(
m.tm, m.tra_tuples,
rule=def_transmission_output_rule,
doc='transmission output = transmission input * efficiency')
m.res_transmission_input_by_capacity = pyomo.Constraint(
m.tm, m.tra_tuples,
rule=res_transmission_input_by_capacity_rule,
doc='transmission input <= total transmission capacity')
m.res_transmission_capacity = pyomo.Constraint(
m.tra_tuples,
rule=res_transmission_capacity_rule,
doc='transmission.cap-lo <= total transmission capacity <= '
'transmission.cap-up')
rule=storage_discharge_depth_constraint_rule
)
if 'loc_tech_carriers_ramping_constraint' in sets:
backend_model.ramping_up_constraint = po.Constraint(
backend_model.loc_tech_carriers_ramping_constraint, backend_model.timesteps,
rule=ramping_up_constraint_rule
)
backend_model.ramping_down_constraint = po.Constraint(
backend_model.loc_tech_carriers_ramping_constraint, backend_model.timesteps,
rule=ramping_down_constraint_rule
)
if 'loc_techs_storage_intra_max_constraint' in sets:
backend_model.storage_intra_max_constraint = po.Constraint(
backend_model.loc_techs_storage_intra_max_constraint, backend_model.timesteps,
rule=storage_intra_max_rule
)
if 'loc_techs_storage_intra_min_constraint' in sets:
backend_model.storage_intra_min_constraint = po.Constraint(
backend_model.loc_techs_storage_intra_min_constraint, backend_model.timesteps,
rule=storage_intra_min_rule
)
if 'loc_techs_storage_inter_max_constraint' in sets:
backend_model.storage_inter_max_constraint = po.Constraint(
backend_model.loc_techs_storage_inter_max_constraint, backend_model.datesteps,
rule=storage_inter_max_rule
)
if 'loc_techs_storage_inter_min_constraint' in sets:
backend_model.storage_inter_min_constraint = po.Constraint(
backend_model.loc_techs_storage_inter_min_constraint, backend_model.datesteps,
m.pro_timevar_output_tuples),
rule=def_process_output_rule,
doc='process output = process throughput * output ratio')
m.def_intermittent_supply = pyomo.Constraint(
m.tm, m.pro_input_tuples,
rule=def_intermittent_supply_rule,
doc='process output = process capacity * supim timeseries')
m.res_process_throughput_by_capacity = pyomo.Constraint(
m.tm, m.pro_tuples,
rule=res_process_throughput_by_capacity_rule,
doc='process throughput <= total process capacity')
m.res_process_maxgrad_lower = pyomo.Constraint(
m.tm, m.pro_maxgrad_tuples,
rule=res_process_maxgrad_lower_rule,
doc='throughput may not decrease faster than maximal gradient')
m.res_process_maxgrad_upper = pyomo.Constraint(
m.tm, m.pro_maxgrad_tuples,
rule=res_process_maxgrad_upper_rule,
doc='throughput may not increase faster than maximal gradient')
m.res_process_capacity = pyomo.Constraint(
m.pro_tuples,
rule=res_process_capacity_rule,
doc='process.cap-lo <= total process capacity <= process.cap-up')
m.res_area = pyomo.Constraint(
m.sit_tuples,
rule=res_area_rule,
doc='used process area <= total process area')
m.res_throughput_by_capacity_min = pyomo.Constraint(
m.tm, m.pro_partial_tuples,
rule=res_throughput_by_capacity_min_rule,
remove_ssc = True
else:
master._scenarios_included = \
set(include_scenarios)
remove_ssc = False
#
# Deactivate second-stage constraints
#
# first count the binding constraints on the top-level ef model
num_first_stage_constraints = len(list(itertools.chain(
master.component_data_objects(SOSConstraint,
active=True,
descend_into=False),
master.component_data_objects(Constraint,
active=True,
descend_into=False))))
# now count the first-stage constraints on the scenario
# instances included in the master ef
for scenario in master_scenario_tree.scenarios:
instance = scenario._instance
for constraint_data in itertools.chain(
instance.component_data_objects(SOSConstraint,
active=True,
descend_into=True),
instance.component_data_objects(Constraint,
active=True,
descend_into=True)):
# Note that it is possible that we are misidentifying
# some constraints as belonging to the first stage. This
# would be the case when no second-stage variables appear
m)],
doc='Combinations of possible dsm_down combinations, e.g. '
'(5001,5003,2020,Mid,Elec)')
# Variables
m.dsm_up = pyomo.Var(
m.tm, m.dsm_site_tuples,
within=pyomo.NonNegativeReals,
doc='DSM upshift')
m.dsm_down = pyomo.Var(
m.dsm_down_tuples,
within=pyomo.NonNegativeReals,
doc='DSM downshift')
# DSM rules
m.def_dsm_variables = pyomo.Constraint(
m.tm, m.dsm_site_tuples,
rule=def_dsm_variables_rule,
doc='DSMup * efficiency factor n == DSMdo (summed)')
m.res_dsm_upward = pyomo.Constraint(
m.tm, m.dsm_site_tuples,
rule=res_dsm_upward_rule,
doc='DSMup <= Cup (threshold capacity of DSMup)')
m.res_dsm_downward = pyomo.Constraint(
m.tm, m.dsm_site_tuples,
rule=res_dsm_downward_rule,
doc='DSMdo (summed) <= Cdo (threshold capacity of DSMdo)')
m.res_dsm_maximum = pyomo.Constraint(
m.tm, m.dsm_site_tuples,
def constraints_in_True_disjuncts(model, config):
"""Yield constraints in disjuncts where the indicator value is set or fixed to True."""
for constr in model.component_data_objects(Constraint):
yield constr
observed_disjuncts = ComponentSet()
for disjctn in model.component_data_objects(Disjunction):
# get all the disjuncts in the disjunction. Check which ones are True.
for disj in disjctn.disjuncts:
if disj in observed_disjuncts:
continue
observed_disjuncts.add(disj)
if fabs(disj.indicator_var.value - 1) <= config.integer_tolerance:
for constr in disj.component_data_objects(Constraint):
yield constr
def build_ordered_component_lists(model, solve_data):
"""Define lists used for future data transfer.
Also attaches ordered lists of the variables, constraints, disjuncts, and
disjunctions to the model so that they can be used for mapping back and
forth.
"""
util_blk = getattr(model, solve_data.util_block_name)
var_set = ComponentSet()
setattr(
util_blk, 'constraint_list', list(
model.component_data_objects(
ctype=Constraint, active=True,
descend_into=(Block, Disjunct))))
setattr(
util_blk, 'disjunct_list', list(
model.component_data_objects(
ctype=Disjunct, descend_into=(Block, Disjunct))))
setattr(
util_blk, 'disjunction_list', list(
model.component_data_objects(
ctype=Disjunction, active=True,
descend_into=(Disjunct, Block))))
# Identify the non-fixed variables in (potentially) active constraints and
# objective functions
for constr in getattr(util_blk, 'constraint_list'):
for v in identify_variables(constr.body, include_fixed=False):
var_set.add(v)