Interfaces

class pyomo.contrib.mpc.interfaces.model_interface.DynamicModelInterface(model, time, context=NOTSET)[source]

A helper class for working with dynamic models, e.g. those where many components are indexed by some ordered set referred to as “time.”

This class provides methods for interacting with time-indexed components, for instance, loading and extracting data or shifting values by some time offset. It also provides methods for constructing components useful for dynamic optimization.

copy_values_at_time(source_time=None, target_time=None)[source]

Copy values of all time-indexed variables from source time point to target time points.

Parameters:
  • source_time (Float) – Time point from which to copy values.

  • target_time (Float or iterable) – Time point or points to which to copy values.

get_data_at_time(time=None, include_expr=False)[source]

Gets data at a single time point or set of time points. Note that the returned type changes depending on whether a scalar or iterable is supplied.

get_penalty_from_target(target_data, time=None, variables=None, weight_data=None, variable_set=None, tolerance=None, prefer_left=None)[source]

A method to get a quadratic penalty expression from a provided setpoint data structure

Parameters:
  • target_data (ScalarData, TimeSeriesData, or IntervalData) – Holds target values for variables

  • time (Set (optional)) – Points at which to apply the tracking cost. Default will use the model’s time set.

  • variables (List of Pyomo VarData (optional)) – Subset of variables supplied in setpoint_data to use in the tracking cost. Default is to use all variables supplied.

  • weight_data (ScalarData (optional)) – Holds the weights to use in the tracking cost for each variable

  • variable_set (Set (optional)) – A set indexing the list of provided variables, if one already exists.

  • tolerance (Float (optional)) – Tolerance for checking inclusion in an interval. Only may be provided if IntervalData is provided for target_data. In this case the default is 0.0.

  • prefer_left (Bool (optional)) – Flag indicating whether the left end point of intervals should be preferred over the right end point. Only may be provided if IntervalData is provided for target_data. In this case the default is False.

Returns:

Set indexing the list of variables to be penalized, and Expression indexed by this set and time. This Expression contains the weighted tracking cost for each variable at each point in time.

Return type:

Set, Expression

get_piecewise_constant_constraints(variables, sample_points, use_next=True, tolerance=0.0)[source]

A method to get an indexed constraint ensuring that inputs are piecewise constant.

Parameters:
  • variables (List of Pyomo Vars) – Variables to enforce piecewise constant

  • sample_points (List of floats) – Points marking the boundaries of intervals within which variables must be constant

  • use_next (Bool (optional)) – Whether to enforce constancy by setting each variable equal to itself at the next point in time (as opposed to at the previous point in time). Default is True.

  • tolerance (Float (optional)) – Absolute tolerance used to determine whether provided sample points are in the model’s time set.

Returns:

First entry is a Set indexing the list of provided variables (with integers). Second entry is a constraint indexed by this set and time enforcing the piecewise constant condition via equality constraints.

Return type:

Tuple

get_scalar_variable_data()[source]

Get data corresponding to non-time-indexed variables.

Returns:

Maps CUIDs of non-time-indexed variables to the value of these variables.

Return type:

dict

load_data(data, time_points=None, tolerance=0.0, prefer_left=None, exclude_left_endpoint=None, exclude_right_endpoint=None)[source]

Method to load data into the model.

Loads data into indicated variables in the model, possibly at specified time points.

Parameters:
  • data (ScalarData, TimeSeriesData, or mapping) – If ScalarData, loads values into indicated variables at all (or specified) time points. If TimeSeriesData, loads lists of values into time points. If mapping, checks whether each variable and value is indexed or iterable and correspondingly loads data into variables.

  • time_points (Iterable (optional)) – Subset of time points into which data should be loaded. Default of None corresponds to loading into all time points.

shift_values_by_time(dt)[source]

Shift values in time indexed variables by a specified time offset.

class pyomo.contrib.mpc.interfaces.var_linker.DynamicVarLinker(source_variables, target_variables, source_time=None, target_time=None)[source]

The purpose of this class is so that we do not have to call find_component or construct ComponentUIDs in a loop when transferring values between two different dynamic models. It also allows us to transfer values between variables that have different names in different models.