oemof.solph package¶
Submodules¶
oemof.solph.linear_constraints module¶
The linear_contraints module contains the pyomo constraints wrapped in functions. These functions are used by the ‘_assembler methods of the OptimizationModel()class.
The module frequently uses the dictionaries I and O for the construction of constraints. I and O contain all components’ uids as dictionary keys and the relevant input input/output uids as dictionary items.
Illustrative Example:
Consider the following example of a chppowerplant modeled with 4 entities (3 busses, 1 component) and their unique ids being stored in a list called uids:
>>> uids = ['bus_el', 'bus_th', 'bus_coal','pp_coal'] >>> I = {'pp_coal': 'bus_coal'} >>> O = {'pp_coal': ['bus_el', 'bus_th']} >>> print(I['pp_coal']) bus_coal
In mathematical notation I, O can be seen as indexed index sets The elements of the sets are the uids of all components (index: e). The the inputs/outputs uids are the elements of the accessed set by the component index e. Generally the index e is the index for the uidssets containing the uids of objects for which the constraints are build. For all mathematical constraints the following definitions hold:
Inputs:
Outputs:
Simon Hilpert (simon.hilpert@fhflensburg.de)

oemof.solph.linear_constraints.
add_bus_balance
(model, block=None)[source]¶ Adds constraint for the inputouput balance of bus objects.
The mathematical formulation for the balance is as follows:
With
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_dispatch_source
(model, block)[source]¶ Creates dispatchable source bounds/constraints.
First the maximum value for the output of the source will be set. Then a constraint is defined that determines the dispatch of the source. This dispatch can be used in the objective function to add cost for dispatch of sources.
The mathemathical formulation of the constraint is as follows:
With and beeing the set of unique ids for all entities grouped in the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_eta_total_chp_relation
(model, block)[source]¶ Adds constraints for input(output1,output2) relation as simple function for all objects in block.objs.
The mathematical formulation of the inputoutput relation of a simple transformer is as follows:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_fixed_source
(model, block)[source]¶ Sets fixed source bounds and constraints for all objects in block.objs
The mathematical formulation is as follows:
For investment for component:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_global_output_limit
(model, block=None)[source]¶ Adds constraints to set limit for variables as sum over the total timehorizon for all objects in block.objs
The mathematical formulation is as follows:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_output_gradient_calc
(model, block, grad_direc='both', idx=0)[source]¶ Add constraint to calculate the gradient between two timesteps (positive and negative)
The mathematical formulation for constraints are as follows:
Positive gradient:
Negative gradient:
With and beeing the set of unique ids for all entities grouped in the attribute block.objs.
Additionally: .
indicates the nth output of component (arg: idx)
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class
 grad_direc (string) – string defining the direction of the gradient constraint. (‘positive’, negative’, ‘both’)

oemof.solph.linear_constraints.
add_simple_chp_relation
(model, block)[source]¶ Adds constraint for outputoutput relation for all simple combined heat an power units in block.objs.
The mathematical formulation for the constraint is as follows:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_simple_extraction_chp_relation
(model, block)[source]¶ Adds constraints for power to heat relation and equivalent output for a simple extraction combined heat an power units. The constraints represent the PQregion of the extraction unit and are set for all objects in block.objs
The mathematical formulation is as follows:
For Power/Heat ratio:
For euivalent power:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_simple_io_relation
(model, block, idx=0)[source]¶ Adds constraints for inputoutput relation as simple function for all objects in block.objs.
The mathematical formulation of the inputoutput relation of a simple transformer is as follows:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally .
indicates the nth output of component (arg: idx)
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class
 idx (integer) – Index to choose which output to select (from list of Outputs: O[e][idx])

oemof.solph.linear_constraints.
add_storage_balance
(model, block)[source]¶ Constraint to build the storage balance in every timestep
The mathematical formulation of the constraint is as follows:With and beeing the set of unique ids for all entities grouped in the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_constraints.
add_storage_charge_discharge_limits
(model, block)[source]¶ Constraints that limit the discharge and charge power by the crate
Constraints are for investment models only.
The mathematical formulation for the constraints is as follows:
Discharge:
Charge:

oemof.solph.linear_constraints.
add_two_inputs_one_output_relation
(model, block)[source]¶ Adds constraint for the inputoutput relation of a post heating transformer with two input flows.
Then the amount of all input flows multiplied with their efficiency will be the output flow. The efficiency of the each will be calculated so that the sum of the efficiency of both flows might be greater than one.
The mathematical formulation for the constraint is as follows:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally: .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class
oemof.solph.linear_mixed_integer_constraints module¶
The module contains linear mixed integer constraints.
@author: Simon Hilpert (simon.hilpert@fhflensburg.de)

oemof.solph.linear_mixed_integer_constraints.
add_minimum_downtime
(model, block)[source]¶ Adds minimum downtime constraints for for components grouped inside block.objs.
The mathematical formulation for constraints is as follows:
Extra constraints for last timesteps:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_mixed_integer_constraints.
add_minimum_uptime
(model, block)[source]¶ Adds minimum uptime constraints for for components in block
The mathematical formulation for constraints is as follows:
Extra constraint for the last timesteps:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_mixed_integer_constraints.
add_output_gradient_constraints
(model, block, grad_direc='both')[source]¶ Creates constraints to model the output gradient for milpmodels.
If gradient direction is positive:
If gradient direction is negative:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class
 grad_direc (string) – direction of gradient (“both”, “positive”, “negative”)
References
[1] M. Steck (2012): “Entwicklung und Bewertung von Algorithmen zur Einsatzplanerstelleung virtueller Kraftwerke”, PhDThesis, TU Munich, p.38

oemof.solph.linear_mixed_integer_constraints.
add_shutdown_constraints
(model, block)[source]¶ Creates constraints to model the shut down of a component.
The mathematical formulation for the constraint is as follows:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_mixed_integer_constraints.
add_startup_constraints
(model, block)[source]¶ Creates constraints to model the start up of a components.
The mathematical formulation of constraint is as follows:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_mixed_integer_constraints.
add_variable_linear_eta_relation
(model, block)[source]¶ Adds constraint for inputoutput relation for all units with variable efficiency grouped in block.objs.
The mathematical formulation for the constraint is as follows:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class

oemof.solph.linear_mixed_integer_constraints.
set_bounds
(model, block, side='output')[source]¶ Set upper and lower bounds via constraints.
The bounds are set with constraints using the binary status variable of the components. The mathematical formulation is as follows:
If side is output:
If side is input:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data. Bounds are altered at model attributes (variables) of model
 block (SimpleBlock()) – block to group all constraints and variables etc., block corresponds to one oemof base class
 side (string) – string to select on which side the bounds should be set (ìnput, output)
oemof.solph.objective_expressions module¶
The module contains different objective expression terms.
@author: Simon Hilpert (simon.hilpert@fhflensburg.de)

oemof.solph.objective_expressions.
add_capex
(model, block, ref='output')[source]¶ Add capital expenditure to linear objective.
If reference is output (e.g. powerplants):
If reference is capacity (e.g. storages):
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
 ref (string) – string to check if capex is referred to capacity (storage) or output (e.g. powerplant)
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_curtailment_costs
(model, block=None)[source]¶ Cost term for dispatchable sources in linear objective.
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_excess_slack_costs
(model, block=None)[source]¶ Artificial cost term for excess slack variables.
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock() block : SimpleBlock()) – block to group all objects corresponding to one oemof base class
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_input_costs
(model, block)[source]¶ Adds costs for usage of input (fuel, elec, etc. ) if not included in opex
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_opex_fix
(model, block, ref=None)[source]¶ Fixed operation expenditure term for linear objective function.
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
 ref (string) – string to check if capex is referred to capacity (storage) or output (e.g. powerplant)
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_opex_var
(model, block, ref='output')[source]¶ Variable operation expenditure term for linear objective function.
If reference of opex is output:
If reference of opex is input:
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
 ref (string) – Reference side on which opex are based on (e.g. powerplant MWhth > input or MWhel > output)
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_ramping_costs
(model, block, grad_direc='positive')[source]¶ Add gradient costs for components to linear objective expression.
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
 grad_direc (string) – direction of gradient for which the costs are added to the objective expression
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_revenues
(model, block, ref='output', idx=0)[source]¶ Revenue term for linear objective function.
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
 ref (string) – Reference side for revnues (‘output’ or ‘input’ Note: ‘input’ not defined)
 idx (integer) – Integer indicating which output from list to select if entity has multiple outputs
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_shortage_slack_costs
(model, block=None)[source]¶ Artificial cost term for shortage slack variables.
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_shutdown_costs
(model, block)[source]¶ Adds shutdown costs for components to objective expression
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
Returns: Return type: Expression

oemof.solph.objective_expressions.
add_startup_costs
(model, block)[source]¶ Adds startup costs for components to objective expression
Parameters:  model (OptimizationModel() instance) –
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
Returns: Return type: Expression
oemof.solph.optimization_model module¶
@contact Simon Hilpert (simon.hilpert@fhflensburg.de)

class
oemof.solph.optimization_model.
OptimizationModel
(energysystem, loglevel=20)[source]¶ Bases:
pyomo.core.base.PyomoModel.ConcreteModel
Create Pyomo model of the energy system.
Parameters:  entities (list with all entity objects) –
 timesteps (list with all timesteps as integer values) –
 options (nested dictionary with options to set.) –

default_assembler
(block)[source]¶ Method for setting optimization model objects for blocks
Parameters:  self (OptimizationModel() instance) –
 block (SimpleBlock()) –

edges
(components)[source]¶ Method that creates a list with all edges for the objects in components.
Parameters:  self –
 components (list of component objects) –
Returns: edges
Return type: list with tupels that represent the edges

objective_assembler
(objective_options)[source]¶ calls functions to add predefined objective functions

results
()[source]¶ Returns a nested dictionary of the results of this optimization model.
The dictionary is keyed by the
Entities
of the optimization model, that isom.results()[s][t]
holds the time series representing values attached to the edge (i.e. the flow) from s to t, where s and t are instances ofEntity
.Time series belonging only to one object, like e.g. shadow prices of commodities on a certain
Bus
, dispatch values of aDispatchSource
or storage values of aStorage
are treated as belonging to an edge looping from the object to itself. This means they can be accessed viaom.results()[object][object]
.Object attributes holding optimization results, like e.g. add_cap for storage objects, can be accessed like this:
om.results()[s].add_cap()
where s is a storage object.
The value of the objective function is stored under the
om.results().objective
attribute.Note that the optimization model has to be solved prior to invoking this method.

solve
(**kwargs)[source]¶ Method that takes care of the communication with the solver to solve the optimization model.
Parameters:  self (pyomo.ConcreteModel() object) –
 **kwargs (keywords) – Possible keys can be set see below
 string (solver) – solver to be used e.g. “glpk”,”gurobi”,”cplex”
 debug (boolean) – If True model is solved in debug mode. lpfile is written.
 duals (boolean) – If True, duals and reduced costs are imported from the solver results
 verbose (boolean) – If True informations are printed
 solver_io (string) – pyomo solver interface file format: “lp”,”python”,”nl”, etc.
 solve_kwargs (dict) – Other arguments for the pyomo.opt.SolverFactory.solve() method Example : {“solver_io”:”lp”}
 solver_cmdline_options (dict) – Dictionary with command line options for solver Examples: {“mipgap”:”0.01”} results in “–mipgap 0.01” {“interior”:” “} results in “–interior”
Returns: self
Return type: solved pyomo.ConcreteModel() instance

oemof.solph.optimization_model.
assembler
(e, om, block)[source]¶  Assemblers are the functions adding constraints to an optimization
 model for a set of objects.
This is the most general form of assembler function, called only if no other, more specific assemblers have been found. Since we don’t know what to do in this case, we can only throw a
TypeError
.Parameters:  entity (An object. Only used to figure out which assembler function to call) – by dispatching on its type. Not used otherwise.
It’s a good idea to set this to None if the function is called
directly via
assembler.registry
.  om (The optimization model. Should be an instance of) –
pyomo.ConcreteModel
.  block (A pyomo block.) –
Returns: om – bus balances.
Return type: The optimization model passed in as an argument, with additional
oemof.solph.predefined_objectives module¶
This module contains predefined objectives that can be used to model energy systems.
@author: Simon Hilpert (simon.hilpert@fhflensburg.de)

oemof.solph.predefined_objectives.
minimize_cost
(self, cost_objects=None, revenue_objects=None)[source]¶ Builds objective function that minimises the total costs.
 Costs included are:
 opex_var, opex_fix, curtailment_costs (dispatch sources), annualised capex (investment components)
Parameters:  self (pyomo model instance) –
 cost_blocks (array like) – list containing classes of objects that are included in cost terms of objective function
 revenue_blocks (array like) – list containing classes of objects that are included in revenue terms of objective function
oemof.solph.pyomo_fastbuild module¶
@author: Simon Hilpert

oemof.solph.pyomo_fastbuild.
l_constraint
(model, name, constraints, *args)[source]¶ A replacement for pyomo’s Constraint that quickly builds linear constraints.
Instead of: model.constraint_name = Constraint(index1,index2,...,rule=f)
Call: l_constraint(model, constraint_name, constraints, index1, index2,...)
This is a copy of the code from the python module pypsa. Thanks to Tom Brown / Jonas Hoersch for implementing it!
Parameters:  model (pyomo.ConcreteModel() / SimpleBlock() instance) – pyomo model or block with constructed components
 name (string) – Name of constraint to be constructed
 constraints (dict) –
Constraints is a dictionary of constraints of the form:
 constraints[i] = [[(coeff1, var1), (coeff2, var2),...], sense,
 constant_term]
sense is one of “==”,”<=”,”>=”.
constant_term is constant rhs of equation
I.e. variable coefficients are stored as a list of tuples.
 *args – arguments passed to the pyomo.Constraint() class.

oemof.solph.pyomo_fastbuild.
mutate_variable
(var=None, value=None, bounds=(0, None), fix=False, index=None)[source]¶ Mutates existing pyomo variable, could be used to extend an emtpy Var() object or an existing indexed pyomo variable. It’s possible to overwrite information for existing indices.
Parameters:  var (pyomo.core.base.var.Var() object) – pyomo variable to be extended
 value (list) – values of variable in list
 bounds (list) – lower and upper bounds of variable as tuples in list
 index (type of var._index elements (most likely tuple)) – list of indices for extension (Warning: if existing indices are used, data for for these indices will be overwritten.)
 fix (list) – list with boolean elements, if true, variable value is fixed, i.e. converted to parameter in optproblem
Example
 extend_variable(om.w, value=[10], bounds=[(0,10)], fix=[True],
 index=[(‘bel’,’sink’,155)])
oemof.solph.variables module¶
Module contains variable definitions and constraint to bound variables (e.g. for investement).
@author: Simon Hilpert (simon.hilpert@fhflensburg.de)

oemof.solph.variables.
add_binary
(model, block, relaxed=False)[source]¶ Creates all status variables (binary) for block.objs
Status variable indicates if a unit is turned on or off. E.g. if a transformer is switched on/off > y=1/0
Parameters:  model (pyomo.ConcreteModel()) – A pyomoobject to be solved containing all Variables, Constraints, Data Variables are added as attributes to the model
 objs (array_like (list)) – all components for which the status variable is created
 relaxed (boolean) – If True “binary” variables will be created as continuous variables with bounds of 0 and 1.

oemof.solph.variables.
add_continuous
(model, edges)[source]¶ Adds all variables corresponding to the edges of the bipartite graph for all timesteps.
The function uses the pyomo class Var() to create the optimization variables. As indexsets the provided edges of the graph (in general all edges) and the defined timesteps are used. The following variables are created: Variables for all the edges (If specific components such as disptach sources and storages exist.)
Parameters:  model (OptimizationModel() instance) – A object to be solved containing all Variables, Constraints, Data Variables are added as attributes to the model
 edges (array_like (list)) – edges will be a list containing tuples representing the directed edges of the graph by using unique ids of components and buses. e.g. [(‘coal’, ‘pp_coal’), (‘pp_coal’, ‘b_el’),...]

oemof.solph.variables.
set_bounds
(model, block, side='output')[source]¶ Sets bounds for variables that represent the weight of the edges of the graph if investment models are calculated.
For investment models upper and lower bounds will be modeled via additional constraints. The mathematical description for the constraint is as follows:
If side is output
With investment:
If side is input:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data Constraints are added as attributes to the model
 block (SimpleBlock()) –
 side (string) – Side of component for which the bounds are set (‘input’ or ‘output’)

oemof.solph.variables.
set_fixed_sink_value
(model, block)[source]¶ Setting a value und fixes the variable of input.
The mathematical formulation is as follows:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data Attributes are altered of the model
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class

oemof.solph.variables.
set_outages
(model, block, outagetype='period', side='output')[source]¶ Fixes component input/output to zeros for modeling outages.
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data.
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
 outagetype (string) – Type to model outages of component if outages is scalar. ‘period’ yield one timeblock where component is off, while ‘random_days’ will sample random days over the timehorizon where component is off
 side (string) – Side of component to fix to zero: ‘output’, ‘input’.

oemof.solph.variables.
set_storage_cap_bounds
(model, block)[source]¶ Alters/sets upper and lower bounds for variables that represent the absolut state of charge e.g. filling level of a storage component.
For investment models upper and lower bounds will be modeled via additional constraints. The mathematical description for the constraint is as follows:
If investment:
With and beeing the set of unique ids for all entities grouped inside the attribute block.objs.
Additionally .
Parameters:  model (OptimizationModel() instance) – An object to be solved containing all Variables, Constraints, Data Bounds are altered at model attributes (variables) of model
 block (SimpleBlock()) – block to group all objects corresponding to one oemof base class
Module contents¶
The solphpackage contains funtionalities for creating and solving an optimizaton problem. The problem is created from oemof base classes. Solph depend on pyomo.