energia.represent.ations.program#

Program

Functions

dataclass([cls, init, repr, eq, order, ...])

Add dunder methods based on the fields defined in the class.

Classes

I(*members[, size, start, mutable, tag, ...])

Set of index elements (X).

Prg([name, tol, canonical, tag])

A mathematical program.

Program([name, tol, canonical, tag, model])

The mathematical program of the model

class Program(name: str = 'prog', tol: float = None, canonical: bool = True, tag: str = '', model: Model = None)[source]#

Bases: Prg

The mathematical program of the model

Variables:
  • players (I) – Players index set

  • periods (I) – Periods index set

  • locations (I) – Locations index set

  • linkages (I) – Linkages index set

  • sources (I) – Sources index set

  • sinks (I) – Sinks index set

  • spaces (I) – Spaces index set

  • currencies (I) – Currencies index set

  • envs (I) – Environments index set

  • socs (I) – Societies index set

  • ecos (I) – Ecosystems index set

  • resources (I) – Resources index set

  • lands (I) – Lands index set

  • materials (I) – Materials index set

  • processes (I) – Processes index set

  • storages (I) – Storages index set

  • transits (I) – Transits index set

  • states (I) – States index set

  • controls (I) – Decisions index set

  • streams (I) – Streams index set

  • consequences (I) – Consequences index set

Note

  • all the index sets are generated post initialization

model: Model = None#
property A: list[list[float]]#

Matrix of Variable coefficients

property A_with_NN: list[list[float]]#

Matrix of Variable coefficients with non-negative constraints

property B: list[float]#

RHS Parameter vector

property B_with_NN: list[float]#

RHS Parameter vector with non-negative constraints

property C: list[float]#

Transpose of the Vector of Objective Coefficients \(C^{T}\)

property CrA: list[list[float]]#

Critical Region A matrix

property CrB: list[float]#

Critical Region RHS vector

property F: list[list[float]]#

Matrix of Parameteric Variable coefficients

property G: list[list[float]]#

Coefficient matrix of inequality (leq) constraints

Example

The following constraints:

\[ \begin{align}\begin{aligned}5 \cdot \mathbf{v}_2 - 3 \cdot \mathbf{v}_3 = 0\\-4 \cdot \mathbf{v}_3 + \frac{\mathbf{v}_1}{13} + 0.55 \leq 0\\3.73 \cdot \mathbf{v}_0 - 2 \cdot \theta_1 + 21 \leq 0\end{aligned}\end{align} \]
property H: list[list[float]]#

Coefficient matrix of equality constraints

h = 0

property NN: list[list[float]]#

Matrix of Variable coefficients for non negative cons

property P: list[list[int]]#

Ordinals of continuous variables \(v \in \mathcal{V}\)

Example

The following constraints:

\[ \begin{align}\begin{aligned}5 \cdot \mathbf{v}_2 - 3 \cdot \mathbf{v}_3 + 15.2 \leq 0\\\mathbf{v}_0 = 1\\-4 \cdot \mathbf{v}_3 + \frac{\mathbf{v}_1}{13} = 0\end{aligned}\end{align} \]

Correspond to:

\[\begin{split}P = \begin{bmatrix} 2 & 3 \\ 0 & \\ 3 & 1 \end{bmatrix}\end{split}\]
property Z: list[list[int]]#

Ordinals of parametric variables \(\theta \in \Theta\)

Example

The following constraints:

\[ \begin{align}\begin{aligned}\mathbf{v}_1 - 2 \cdot \theta_1 + 21 \leq 0\\\mathbf{v}_0 - 7.23 \cdot \theta_0 = 0\\\theta_1 - 2 \cdot \mathbf{v}_0 - 31.56\end{aligned}\end{align} \]

Corresponds to:

\[\begin{split}Z = \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix}\end{split}\]
add_constraint(name: str, constraint: C)#

Adds a constraint set to the program

Parameters:
  • name (str) – name of constraint

  • constraint (C) – constraint object

add_function(name: str, function: F)#

Add a function set to the program

Parameters:
  • name (str) – name of function

  • function (F) – function object

add_index(name: str, index: I)#

Adds new index to program

Parameters:
  • name (str) – name of index

  • index (I) – index set to be added

add_indices(index: I, members: list[str] = None)#

Adds indices from an index set to the program

Parameters:
  • index (I) – Index set who elements are to be added to the program.

  • members (list[str], optional) – List of members to be added to the index set.

add_objective(objective: O)#

Adds an objective set to the program

Parameters:

objective (O) – objective object

add_parameter(name: str, parameter: P)#

Adds new parameter set to program

Parameters:
  • name (str) – name of parameter set

  • parameter (P) – parameter set to be added

add_theta(name: str, theta: T)#

Adds new theta set to program

Parameters:
  • name (str) – name of theta set

  • theta (T) – theta set to be added

add_variable(name: str, variable: V)#

Adds new variable set to program

Parameters:
  • name (str) – name of variable set

  • variable (V) – variable set to be added

bnrvars(n: bool = False) list[int | V]#

binary variables

canonical: bool = True#
cntbnrvars(n: bool = False) list[int | V]#

continuous and binary variables integer variables are excluded

cntvars(n: bool = False) list[int | V]#

continuous variables

cons(n: bool = False) list[int | C]#

constraints

draw(variable: V = None, n_sol: int = 0)#

Plots the solution for a variable

eqcons(n: bool = False) list[int | C]#

equality constraints

property eqcons_sets: list[C]#

equality constraint sets

eval(*theta_vals: float, n_sol: int = 0, roundoff: int = 4) list[float]#

Evaluates the variable value as a function of parametric variables

Parameters:
  • theta_vals (float) – values of the parametric variables

  • n_sol (int, optional) – solution number, defaults to 0

  • roundoff (int, optional) – round off the evaluated value, defaults to 4

Returns:

list of values

Return type:

list[float]

Raises:

ValueError – if number of theta values provided does not match number of thetas in the problem

property formulation: Model | MPLP_Program#

Returns the latest formulation of the program

Returns:

Formulation object

Return type:

GPModel | MPLP_Program

gurobi() Model#

Gurobi Model

itgvars(n: bool = False) list[int | V]#

integer variables

latex(descriptive: bool = False, categorical: bool = False, category: str = None, as_document: bool = False) str#

Return a LaTeX/Markdown-compatible representation of the mathematical program. - In Markdown mode: uses Markdown headers (##, ###) - In document mode: uses LaTeX section commands

lb(function: V | F)#

Finds the lower bound of a variable or function

leqcons(n: bool = False) list[int | C]#

less than or equal constraints

property leqcons_sets: list[C]#

less than or equal constraint sets

lp()#

LP File

make_A_df(longname: bool = False) DataFrame#

Create a DataFrame from the A matrix.

Parameters:

longname (bool) – Whether to use long names for variables. Defaults to False.

Returns:

Columns are the variables, rows are the constraints.

Return type:

DataFrame

make_B_df(longname: bool = False) DataFrame#

Create a DataFrame from the B vector.

Parameters:

longname (bool) – Whether to use long names for variables. Defaults to False.

Returns:

Single column DataFrame with the RHS values.

Return type:

DataFrame

make_C_df(longname: bool = False) DataFrame#

Create a DataFrame from the C matrix.

Parameters:

longname (bool) – Whether to use long names for variables. Defaults to False.

Returns:

Single row DataFrame with the objective coefficients.

Return type:

DataFrame

make_CrA_df(longname: bool = False) DataFrame#

Creates a DataFrame from the Critical Region A matrix.

make_CrB_df(longname: bool = False) DataFrame#

Creates a DataFrame from the Critical Region RHS vector.

make_F_df(longname: bool = False) DataFrame#

Creates a DataFrame from the Theta coefficients matrix.

make_df(longname: bool = False) DataFrame#

Create a DataFrame from the model.

Parameters:

longname (bool) – Whether to use long names for variables. Defaults to False.

Returns:

A DataFrame with the A matrix, B vector, and C vector.

Return type:

DataFrame

mps(name: str = None)#

MPS File

mutate_parameter(parameter_ex: P, parameter_new: P)#

Mutates an existing parameter set in the program

Parameters:
  • parameter_ex (P) – existing parameter set to be mutated

  • parameter_new (P) – incoming parameter set to be added

mutate_theta(theta_ex: T, theta_new: T)#

Mutates an existing theta set in the program

Parameters:
  • theta_ex (T) – existing theta set to be mutated

  • theta_new (T) – incoming theta set to be added

mutate_variable(variable_ex: V, variable_new: V)#

Mutates an existing variable set in the program

Parameters:
  • variable_ex (V) – existing variable set to be mutated

  • variable_new (V) – incoming variable set to be added

name: str = 'prog'#
nncons(n: bool = False) list[int | C]#

non-negativity constraints

property nncons_sets: list[C]#

non-negativity constraint sets

nnvars(n: bool = False) list[int | V]#

non-negative variables

nonbnritgvars(n: bool = False) list[int | V]#

non-binary and integer variables

obj()#

Objective Values

opt(using: str = 'gurobi')#

Determine the optimal solution to the program

output(n_sol: int = 0, slack: bool = True, compare=False)#

Print sol

ppopt() MPLP_Program#

Convert the program to a ppopt.MPLP_Program

renumber()#

Renumbers the constraints, just to be sure

replace_constraint(constraint_ex: C, constraint_new: C)#

Replaces an existing constraint set in the program

Parameters:
  • constraint_ex (C) – existing constraint set to be mutated

  • constraint_new (C) – new constraint set to replace the existing one

replace_function(function_ex: F, function_new: F)#

Replaces an existing function set in the program

Parameters:
  • function_ex (F) – existing function set to be mutated

  • function_new (F) – new function set to replace the existing one

show(descriptive: bool = False, nncons: bool = False, categorical: bool = False, category: str = None)#

Pretty Print

property solution: Solution | Solution#

Returns the latest solution of the program

Returns:

Solution object

Return type:

Solution | MPSolution

solve(using: Literal['combinatorial', 'combinatorial_parallel', 'combinatorial_parallel_exp', 'graph', 'graph_exp', 'graph_parallel', 'graph_parallel_exp', 'combinatorial_graph', 'geometric', 'geometric_parallel', 'geometric_parallel_exp'] = 'combinatorial', tol_mat: float = 1e-09, round_off: int = 4)#

Solve the multiparametric program

tag: str = ''#
tol: float = None#
ub(function: V | F)#

Finds the upper bound of a variable or function

update_theta(constraint: C)#

Updates the theta set and thetas in a constraint

Parameters:

constraint (C) – constraint with thetas to be updated