app.conversion.pulp_converter
PulpConverter
Bases: Converter
Converts a Model class object into a PuLP optimization problem.
This converter handles the transformation of energy system models into linear programming problems that can be solved using the PuLP optimization library. It converts entities, quantities, and relations into PuLP variables and constraints while maintaining temporal relationships and handling time series data resampling.
The converter supports: - Time-indexed variables for dynamic optimization - Linear constraints between variables - Temporal relationships (t-1, t+1 references) - Time series data resampling - Hierarchical entity structures
Attributes
DEFAULT_TIME_SET_SIZE : int Default number of time steps when none is specified (10)
Notes
This converter is designed for linear programming problems only. Non-linear operations like variable multiplication or division by variables will raise ValueError.
Examples
converter = PulpConverter() model_vars = converter.convert_model(energy_model) constraints = model_vars['battery.soc_constraint']
convert_binary_expression(binary_expr, left_result, right_result, operator, t, time_set=None, new_freq=None)
Convert a binary expression to PuLP constraint or arithmetic operation.
Parameters
binary_expr : BinaryExpression The binary expression object (unused) left_result : Any Result of the left operand evaluation right_result : Any Result of the right operand evaluation operator : Operator The operator to apply between left and right operands t : int Current time step (unused) time_set : Optional[Union[int, range]], optional Time set (unused) new_freq : Optional[str], optional Frequency (unused)
Returns
Union[pulp.LpConstraint, pulp.LpExpression] PuLP constraint for comparison operators or expression for arithmetic operators
Raises
ValueError If the operator is not supported or violates linear programming constraints.
convert_entity_reference(entity_ref, entity_id, t, time_set=None, new_freq=None)
Convert an entity reference to a PuLP variable for the given time step.
Parameters
entity_ref : EntityReference The entity reference object containing time offset information entity_id : str The entity ID to look up in the variables dictionary t : int Current time step time_set : Optional[Union[int, range]], optional Total time set (unused here but kept for consistency) new_freq : Optional[str], optional Frequency (unused here but kept for consistency)
Returns
Union[pulp.LpVariable, Any] PuLP variable or expression for the specified time step
Raises
ValueError If the entity_id is not found in the objects dictionary.
convert_model(model, time_set=None, new_freq=None, **kwargs)
Convert the model to an optimization/simulation problem.
Converts the model's entities and their quantities into a flat dictionary of pulp variables suitable for optimization. It also handles the resampling of time series data to the specified frequency and time set.
Parameters
model : Model The model to convert. time_set : Optional[Union[int, range]], optional The number of time steps to represent in the pulp variables. If None, uses model.number_of_time_steps. new_freq : Optional[str], optional The target frequency to resample time series data to (e.g., '15min', '1H'). If None, uses model.frequency.
Returns
Dict[str, Any] Dictionary containing all pulp variables and time set information. Includes a 'time_set' key with the range of time steps.
convert_quantity(quantity, name, time_set=None, freq=None)
Convert the time series data to a format suitable for pulp optimization.
If the quantity is empty, create an empty pulp variable. If the quantity is a Parameter, return its value directly. Otherwise, return the values resampled to the specified time set and frequency.
Parameters
quantity : Quantity The quantity to convert name : str The name for the generated variables time_set : Optional[Union[int, range]], optional The time set specification. If None, uses default size. freq : Optional[str], optional The frequency for resampling
Returns
Union[List[pulp.LpVariable], Any] Either a list of PuLP variables (for empty quantities) or the quantity values
convert_relation(relation, entity_variables, time_set=None, new_freq=None)
Convert a Relation to PuLP constraints for each time step.
This method dynamically assembles relations based on identified entity references and operations. For example: "battery1.discharge_power(t)<2*battery1.discharge_power(t-1)" becomes: for t in time_set: constraint = battery1.discharge_power[t] < 2 * battery1.discharge_power[t-1]
Parameters
relation : Relation The relation to convert entity_variables : Dict[str, Any] Dictionary mapping entity IDs to their PuLP variables time_set : Optional[Union[int, range]], optional The time set to iterate over. If None, uses default size. new_freq : Optional[str], optional Frequency (kept for consistency with interface)
Returns
Dict[str, List] Dictionary containing the constraint name as key and list of constraints as value.
Raises
ValueError If any entity referenced in the relation is not found in entity_variables.