Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_abs(self):
m = ConcreteModel()
m.x = Var(bounds=(-1, 1), initialize=0)
mc_expr = mc(abs((m.x)))
self.assertEqual(mc_expr.lower(), 0)
self.assertEqual(mc_expr.upper(), 1)
# io_options are getting relayed to the subsolver
# here).
#
self.results.append(opt.solve(self._instance,
tee=self._tee,
timelimit=self._timelimit))
#
stop_time = time.time()
self.wall_time = stop_time - start_time
#
# Deactivate the block that contains the optimality conditions,
# and reactivate SubModel
#
submodel = self._instance._transformation_data['bilevel.linear_mpec'].submodel_cuid. find_component(self._instance)
for (name, data) in submodel.component_map(active=False).items():
if not isinstance(data,Var) and not isinstance(data,Set):
data.activate()
# TODO: delete this subblock
self._instance._transformation_data['bilevel.linear_mpec'].block_cuid.find_component(self._instance).deactivate()
#
# Return the sub-solver return condition value and log
#
return pyutilib.misc.Bunch(rc=getattr(opt,'_rc', None),
log=getattr(opt,'_log',None))
# storage capacities as expression objects
m.cap_sto_c = pyomo.Expression(
m.sto_tuples,
rule=def_storage_capacity_rule,
doc='Total storage size (MWh)')
m.cap_sto_p = pyomo.Expression(
m.sto_tuples,
rule=def_storage_power_rule,
doc='Total storage power (MW)')
m.e_sto_in = pyomo.Var(
m.tm, m.sto_tuples,
within=pyomo.NonNegativeReals,
doc='Power flow into storage (MW) per timestep')
m.e_sto_out = pyomo.Var(
m.tm, m.sto_tuples,
within=pyomo.NonNegativeReals,
doc='Power flow out of storage (MW) per timestep')
m.e_sto_con = pyomo.Var(
m.t, m.sto_tuples,
within=pyomo.NonNegativeReals,
doc='Energy content of storage (MWh) in timestep')
# storage rules
m.def_storage_state = pyomo.Constraint(
m.tm, m.sto_tuples,
rule=def_storage_state_rule,
doc='storage[t] = (1 - sd) * storage[t-1] + in * eff_i - out / eff_o')
m.res_storage_input_by_power = pyomo.Constraint(
m.tm, m.sto_tuples,
rule=res_storage_input_by_power_rule,
if 'loc_techs_om_cost' in model_data_dict['sets']:
backend_model.cost_var = po.Var(backend_model.costs, backend_model.loc_techs_om_cost, backend_model.timesteps, within=po.Reals)
if 'loc_techs_investment_cost' in model_data_dict['sets'] and run_config['mode'] != 'operate':
backend_model.cost_investment = po.Var(backend_model.costs, backend_model.loc_techs_investment_cost, within=po.Reals)
if 'loc_techs_purchase' in model_data_dict['sets'] and run_config['mode'] != 'operate':
backend_model.purchased = po.Var(backend_model.loc_techs_purchase, within=po.Binary)
if 'group_demand_share_per_timestep_decision' in model_data_dict['data'] and run_config['mode'] != 'operate':
backend_model.demand_share_per_timestep_decision = po.Var(backend_model.loc_tech_carriers_prod, within=po.NonNegativeReals)
if 'loc_techs_milp' in model_data_dict['sets']:
if run_config['mode'] != 'operate':
backend_model.units = po.Var(backend_model.loc_techs_milp, within=po.NonNegativeIntegers)
backend_model.operating_units = po.Var(backend_model.loc_techs_milp, backend_model.timesteps, within=po.NonNegativeIntegers)
# For any milp tech, we need to update energy_cap, as energy_cap_max and energy_cap_equals
# are replaced by energy_cap_per_unit
if run_config['mode'] == 'operate':
for k, v in backend_model.units.items():
backend_model.energy_cap[k] = v * backend_model.energy_cap_per_unit[k]
if 'loc_techs_asynchronous_prod_con' in model_data_dict['sets']:
backend_model.prod_con_switch = po.Var(backend_model.loc_techs_asynchronous_prod_con, backend_model.timesteps, within=po.Binary)
backend_model.bigM = run_config.get('bigM', 1e10)
if run_config.get('ensure_feasibility', False):
backend_model.unmet_demand = po.Var(backend_model.loc_carriers, backend_model.timesteps, within=po.NonNegativeReals)
backend_model.unused_supply = po.Var(backend_model.loc_carriers, backend_model.timesteps, within=po.NegativeReals)
backend_model.bigM = run_config.get('bigM', 1e10)
doc='Use of stock commodity source (MW) per timestep')
m.e_co_sell = pyomo.Var(
m.tm, m.com_tuples,
within=pyomo.NonNegativeReals,
doc='Use of sell commodity source (MW) per timestep')
m.e_co_buy = pyomo.Var(
m.tm, m.com_tuples,
within=pyomo.NonNegativeReals,
doc='Use of buy commodity source (MW) per timestep')
# process
m.cap_pro = pyomo.Var(
m.pro_tuples,
within=pyomo.NonNegativeReals,
doc='Total process capacity (MW)')
m.cap_pro_new = pyomo.Var(
m.pro_tuples,
within=pyomo.NonNegativeReals,
doc='New process capacity (MW)')
m.tau_pro = pyomo.Var(
m.tm, m.pro_tuples,
within=pyomo.NonNegativeReals,
doc='Power flow (MW) through process')
m.e_pro_in = pyomo.Var(
m.tm, m.pro_tuples, m.com,
within=pyomo.NonNegativeReals,
doc='Power flow of commodity into process (MW) per timestep')
m.e_pro_out = pyomo.Var(
m.tm, m.pro_tuples, m.com,
within=pyomo.NonNegativeReals,
doc='Power flow out of process (MW) per timestep')
m.onlinestatus = pyomo.Var(
if 'loc_techs_supply_plus' in model_data_dict['sets']:
backend_model.resource_con = po.Var(backend_model.loc_techs_supply_plus, backend_model.timesteps, within=po.NonNegativeReals)
if run_config['mode'] != 'operate':
backend_model.resource_cap = po.Var(backend_model.loc_techs_supply_plus, within=po.NonNegativeReals)
if 'loc_techs_export' in model_data_dict['sets']:
backend_model.carrier_export = po.Var(backend_model.loc_tech_carriers_export, backend_model.timesteps, within=po.NonNegativeReals)
if 'loc_techs_om_cost' in model_data_dict['sets']:
backend_model.cost_var = po.Var(backend_model.costs, backend_model.loc_techs_om_cost, backend_model.timesteps, within=po.Reals)
if 'loc_techs_investment_cost' in model_data_dict['sets'] and run_config['mode'] != 'operate':
backend_model.cost_investment = po.Var(backend_model.costs, backend_model.loc_techs_investment_cost, within=po.Reals)
if 'loc_techs_purchase' in model_data_dict['sets'] and run_config['mode'] != 'operate':
backend_model.purchased = po.Var(backend_model.loc_techs_purchase, within=po.Binary)
if 'group_demand_share_per_timestep_decision' in model_data_dict['data'] and run_config['mode'] != 'operate':
backend_model.demand_share_per_timestep_decision = po.Var(backend_model.loc_tech_carriers_prod, within=po.NonNegativeReals)
if 'loc_techs_milp' in model_data_dict['sets']:
if run_config['mode'] != 'operate':
backend_model.units = po.Var(backend_model.loc_techs_milp, within=po.NonNegativeIntegers)
backend_model.operating_units = po.Var(backend_model.loc_techs_milp, backend_model.timesteps, within=po.NonNegativeIntegers)
# For any milp tech, we need to update energy_cap, as energy_cap_max and energy_cap_equals
# are replaced by energy_cap_per_unit
if run_config['mode'] == 'operate':
for k, v in backend_model.units.items():
backend_model.energy_cap[k] = v * backend_model.energy_cap_per_unit[k]
if 'loc_techs_asynchronous_prod_con' in model_data_dict['sets']:
backend_model.prod_con_switch = po.Var(backend_model.loc_techs_asynchronous_prod_con, backend_model.timesteps, within=po.Binary)
m.cap_online = pyomo.Var(
m.t, m.pro_partial_tuples,
within=pyomo.NonNegativeReals,
doc='Online capacity (MW) of process per timestep')
m.startup_pro = pyomo.Var(
m.tm, m.pro_partial_tuples,
within=pyomo.NonNegativeReals,
doc='Started capacity (MW) of process per timestep')
# transmission
m.cap_tra = pyomo.Var(
m.tra_tuples,
within=pyomo.NonNegativeReals,
doc='Total transmission capacity (MW)')
m.cap_tra_new = pyomo.Var(
m.tra_tuples,
within=pyomo.NonNegativeReals,
doc='New transmission capacity (MW)')
m.e_tra_in = pyomo.Var(
m.tm, m.tra_tuples,
within=pyomo.NonNegativeReals,
doc='Power flow into transmission line (MW) per timestep')
m.e_tra_out = pyomo.Var(
m.tm, m.tra_tuples,
within=pyomo.NonNegativeReals,
doc='Power flow out of transmission line (MW) per timestep')
# storage
m.cap_sto_c = pyomo.Var(
m.sto_tuples,
within=pyomo.NonNegativeReals,
if 'group_demand_share_per_timestep_decision' in model_data_dict['data'] and run_config['mode'] != 'operate':
backend_model.demand_share_per_timestep_decision = po.Var(backend_model.loc_tech_carriers_prod, within=po.NonNegativeReals)
if 'loc_techs_milp' in model_data_dict['sets']:
if run_config['mode'] != 'operate':
backend_model.units = po.Var(backend_model.loc_techs_milp, within=po.NonNegativeIntegers)
backend_model.operating_units = po.Var(backend_model.loc_techs_milp, backend_model.timesteps, within=po.NonNegativeIntegers)
# For any milp tech, we need to update energy_cap, as energy_cap_max and energy_cap_equals
# are replaced by energy_cap_per_unit
if run_config['mode'] == 'operate':
for k, v in backend_model.units.items():
backend_model.energy_cap[k] = v * backend_model.energy_cap_per_unit[k]
if 'loc_techs_asynchronous_prod_con' in model_data_dict['sets']:
backend_model.prod_con_switch = po.Var(backend_model.loc_techs_asynchronous_prod_con, backend_model.timesteps, within=po.Binary)
backend_model.bigM = run_config.get('bigM', 1e10)
if run_config.get('ensure_feasibility', False):
backend_model.unmet_demand = po.Var(backend_model.loc_carriers, backend_model.timesteps, within=po.NonNegativeReals)
backend_model.unused_supply = po.Var(backend_model.loc_carriers, backend_model.timesteps, within=po.NegativeReals)
backend_model.bigM = run_config.get('bigM', 1e10)
def build_model():
"""
Base Model
Optimal solution:
Select units 1, 3, 8
Objective value -36.62
"""
m = ConcreteModel()
m.streams = RangeSet(25)
m.x = Var(m.streams, bounds=(0, 50), initialize=5)
m.stage1_split = Constraint(expr=m.x[1] == m.x[2] + m.x[4])
m.first_stage = Disjunction(expr=[
[
# Unit 1
m.x[2] == exp(m.x[3]) - 1,
m.x[4] == 0, m.x[5] == 0
],
[
# Unit 2
m.x[5] == log(m.x[4] + 1),
m.x[2] == 0, m.x[3] == 0
]
])
m.stage1_mix = Constraint(expr=m.x[3] + m.x[5] == m.x[6])
m.stage2_split = Constraint(expr=m.x[6] == sum(m.x[i] for i in (7, 9, 11, 13)))