Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_buy_sell_price(m):
# Sets
m.com_sell = pyomo.Set(
within=m.com,
initialize=commodity_subset(m.com_tuples, 'Sell'),
doc='Commodities that can be sold')
m.com_buy = pyomo.Set(
within=m.com,
initialize=commodity_subset(m.com_tuples, 'Buy'),
doc='Commodities that can be purchased')
# Variables
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')
# Rules
m.res_sell_step = pyomo.Constraint(
m.loc_tech_r2 = po.Set(initialize=self._sets["loc_tech_r2"], ordered=True)
m.loc_tech_export = po.Set(initialize=self._sets["loc_tech_export"], ordered=True)
m.loc_tech_purchase = po.Set(initialize=self._sets["loc_tech_purchase"], ordered=True)
m.loc_tech_milp = po.Set(initialize=self._sets["loc_tech_milp"], ordered=True)
m.loc_tech_transmission = po.Set(initialize=self._sets["loc_tech_transmission"], ordered=True)
m.loc_tech_finite_r = po.Set(initialize=self._sets["loc_tech_finite_r"], ordered=True)
m.loc_tech_demand = po.Set(initialize=self._sets["loc_tech_demand"], ordered=True)
m.loc_tech_supply = po.Set(initialize=self._sets["loc_tech_supply"], ordered=True)
m.loc_tech_supply_plus = po.Set(initialize=self._sets["loc_tech_supply_plus"], ordered=True)
m.loc_tech_conversion = po.Set(initialize=self._sets["loc_tech_conversion"], ordered=True)
m.loc_tech_conversion_plus = po.Set(initialize=self._sets["loc_tech_conversion_plus"], ordered=True)
m.loc_tech_unmet = po.Set(initialize=self._sets["loc_tech_unmet"], ordered=True)
m.loc_tech_2out = po.Set(initialize=self._sets["loc_tech_2out"], ordered=True)
m.loc_tech_3out = po.Set(initialize=self._sets["loc_tech_3out"], ordered=True)
m.loc_tech_2in = po.Set(initialize=self._sets["loc_tech_2in"], ordered=True)
m.loc_tech_3in = po.Set(initialize=self._sets["loc_tech_3in"], ordered=True)
# Timeseries
for param in self.config_model.timeseries_constraints:
setattr(m, 'y_' + param + '_timeseries',
po.Set(initialize=self._sets['y_' + param + '_timeseries'],
within=m.y))
#
# Parameters
#
# Here we set timeseries data as Pyomo parameters as it makes constraints
# generation significantly quicker. Other data comes from config_model
# dictionary to avoid over-dependance on Pyomo
for param in self.config_model.timeseries_constraints:
for (stf, site, process) in m.pro_tuples
for (s, pro, _) in tuple(m.r_in_min_fraction_dict.keys())
if process == pro and s == stf],
doc='Processes with partial input')
m.pro_partial_input_tuples = pyomo.Set(
within=m.stf * m.sit * m.pro * m.com,
initialize=[(stf, site, process, commodity)
for (stf, site, process) in m.pro_partial_tuples
for (s, pro, commodity) in tuple(m.r_in_min_fraction_dict
.keys())
if process == pro and s == stf],
doc='Commodities with partial input ratio,'
'e.g. (2020,Mid,Coal PP,Coal)')
m.pro_partial_output_tuples = pyomo.Set(
within=m.stf * m.sit * m.pro * m.com,
initialize=[(stf, site, process, commodity)
for (stf, site, process) in m.pro_partial_tuples
for (s, pro, commodity) in tuple(m.r_out_min_fraction_dict
.keys())
if process == pro and s == stf],
doc='Commodities with partial input ratio, e.g. (Mid,Coal PP,CO2)')
# Variables
# costs
m.costs = pyomo.Var(
m.cost_type,
within=pyomo.Reals,
doc='Costs by type (EUR/a)')
m.pro = pyomo.Set(
initialize=m.process.index.get_level_values('Process').unique(),
doc='Set of conversion processes')
# tranmission (e.g. hvac, hvdc, pipeline...)
m.tra = pyomo.Set(
initialize=m.transmission.index.get_level_values('Transmission').unique(),
doc='Set of transmission technologies')
# storage (e.g. hydrogen, pump storage)
m.sto = pyomo.Set(
initialize=m.storage.index.get_level_values('Storage').unique(),
doc='Set of storage technologies')
# cost_type
m.cost_type = pyomo.Set(
initialize=['Inv', 'Fix', 'Var', 'Fuel','Revenue','Purchase','Startup'],
doc='Set of cost types (hard-coded)')
# tuple sets
m.com_tuples = pyomo.Set(
within=m.sit*m.com*m.com_type,
initialize=m.commodity.index,
doc='Combinations of defined commodities, e.g. (Mid,Elec,Demand)')
m.pro_tuples = pyomo.Set(
within=m.sit*m.pro,
initialize=m.process.index,
doc='Combinations of possible processes, e.g. (North,Coal plant)')
m.tra_tuples = pyomo.Set(
within=m.sit*m.sit*m.tra*m.com,
initialize=m.transmission.index,
doc='Combinations of possible transmission, e.g. (South,Mid,hvac,Elec)')
m.com = pyomo.Set(
initialize=m.commodity.index.get_level_values('Commodity').unique(),
doc='Set of commodities')
# commodity type (i.e. SupIm, Demand, Stock, Env)
m.com_type = pyomo.Set(
initialize=m.commodity.index.get_level_values('Type').unique(),
doc='Set of commodity types')
# process (e.g. Wind turbine, Gas plant, Photovoltaics...)
m.pro = pyomo.Set(
initialize=m.process.index.get_level_values('Process').unique(),
doc='Set of conversion processes')
# tranmission (e.g. hvac, hvdc, pipeline...)
m.tra = pyomo.Set(
initialize=m.transmission.index.get_level_values('Transmission').unique(),
doc='Set of transmission technologies')
# storage (e.g. hydrogen, pump storage)
m.sto = pyomo.Set(
initialize=m.storage.index.get_level_values('Storage').unique(),
doc='Set of storage technologies')
# cost_type
m.cost_type = pyomo.Set(
initialize=['Inv', 'Fix', 'Var', 'Fuel','Revenue','Purchase','Startup'],
doc='Set of cost types (hard-coded)')
# tuple sets
m.com_tuples = pyomo.Set(
within=m.sit*m.com*m.com_type,
default=None)
# DEPRECATED
model.StageCostVariable = Param(model.Stages,
mutable=True)
# it is often the case that a subset of the stage variables are strictly "derived"
# variables, in that their values are computable once the values of other variables
# in that stage are known. it generally useful to know which variables these are,
# as it is unnecessary to post non-anticipativity constraints for these variables.
# further, attempting to force non-anticipativity - either implicitly or explicitly -
# on these variables can cause issues with decomposition algorithms.
model.StageDerivedVariables = Set(model.Stages,
initialize=[],
ordered=True)
model.NodeDerivedVariables = Set(model.Nodes,
initialize=[],
ordered=True)
# scenario data can be populated in one of two ways. the first is "scenario-based",
# in which a single .dat file contains all of the data for each scenario. the .dat
# file prefix must correspond to the scenario name. the second is "node-based",
# in which a single .dat file contains only the data for each node in the scenario
# tree. the node-based method is more compact, but the scenario-based method is
# often more natural when parameter data is generated via simulation. the default
# is scenario-based.
model.ScenarioBasedData = Param(within=Boolean,
default=True,
mutable=True)
# do we bundle, and if so, how?
model.Bundling = Param(within=Boolean,
m.com_type = pyomo.Set(
initialize=m.commodity.index.get_level_values('Type').unique(),
doc='Set of commodity types')
# process (e.g. Wind turbine, Gas plant, Photovoltaics...)
m.pro = pyomo.Set(
initialize=m.process.index.get_level_values('Process').unique(),
doc='Set of conversion processes')
# tranmission (e.g. hvac, hvdc, pipeline...)
m.tra = pyomo.Set(
initialize=m.transmission.index.get_level_values('Transmission').unique(),
doc='Set of transmission technologies')
# storage (e.g. hydrogen, pump storage)
m.sto = pyomo.Set(
initialize=m.storage.index.get_level_values('Storage').unique(),
doc='Set of storage technologies')
# cost_type
m.cost_type = pyomo.Set(
initialize=['Inv', 'Fix', 'Var', 'Fuel','Revenue','Purchase','Startup'],
doc='Set of cost types (hard-coded)')
# tuple sets
m.com_tuples = pyomo.Set(
within=m.sit*m.com*m.com_type,
initialize=m.commodity.index,
doc='Combinations of defined commodities, e.g. (Mid,Elec,Demand)')
m.pro_tuples = pyomo.Set(
within=m.sit*m.pro,
initialize=m.process.index,
def CreateAbstractScenarioTreeModel():
from pyomo.core import (AbstractModel, Set, Param, Boolean)
model = AbstractModel()
# all set/parameter values are strings, representing the
# names of various entities/variables.
model.Stages = Set(ordered=True)
model.Nodes = Set(ordered=True)
model.NodeStage = Param(model.Nodes,
within=model.Stages,
mutable=True)
model.Children = Set(model.Nodes,
within=model.Nodes,
initialize=[],
ordered=True)
model.ConditionalProbability = Param(model.Nodes,
mutable=True)
model.Scenarios = Set(ordered=True)
model.ScenarioLeafNode = Param(model.Scenarios,
within=model.Nodes,
mutable=True)
model.StageVariables = Set(model.Stages,
initialize=[],
ordered=True)
model.NodeVariables = Set(model.Nodes,