lp_denorm_constraints#

Module lp_denorm_constraints#

This module provides functions to handle denormalized constraints in Linear Programming (LP) problems, especially useful in machine learning contexts.

Denormalized constraints are required when working with values in LP problems that have been normalized. This module facilitates the conversion back to the original denormalized values.

This module contains a dictionary named DENORMALIZED_CONSTRAINTS that stores the instructions to define the denormalized constraints. The keys of the dictionary represent the name of the constraints to create while the values represent lists of variables that define these constraints. Each element from those lists defines a dictionary specifying how a variable should be added to the constraint. Here’s the macrostructure of the DENORMALIZED_CONSTRAINTS dictionary:

DENORMALIZED_CONSTRAINTS = {
    "<CONSTRAINT_NAME>": [...],
    ...,
}

Each constraint contains a list with the following structure:

[
    {"feature": "<FEATURE_NAME>", "denorm": True/False, "coef": <NUMERIC_VALUE>},
    ...,
    {"operator": <"GTE", "LTE", "EQ">, "value": <NUMERIC_VALUE>},
],

For example, to define the constraint:

5\cdot{\text{x}} - 2\cdot{\text{y}} \leq{22}

Where \text{x} and \text{y} represent optimization problem variables.

The above equation can be defined as follows:

DENORMALIZED_CONSTRAINTS = {
    "5_times_x_minus_2_times_y_leq_22": [
        {"feature": "x", "denorm": True, "coef": 5},
        {"feature": "y", "denorm": True, "coef": -2},
        {"operator": "LTE", "value": 22},
    ],
}

Finally, to define the above constraint to the problem, use the function create_denorm_constraints:

create_denorm_constraints(lp_problem, scalers, DENORMALIZED_CONSTRAINTS)

Where:

  • lp_problem: instance of pulp.LpProblem that represents the optimization problem.

  • scalers: contains a dictionary of sklearn.preprocessing.MinMaxScaler objects that represent the scalers for all variables that the optimization problem has.

Important

For the above code to work, the variables that each constraint uses must exist on the lp_problem instance.

Functions#

  • denormalize_lpvar(variable): Converts a single LP variable back to its original scale.

  • process_term(coefficient, variable): Handles a term in a denormalized constraint, considering its coefficient and variable.

  • process_terms(terms): Processes a list of terms from a denormalized constraint.

  • create_denorm_constraints(constraints): Generates denormalized constraints for an LP problem using the above functions.

Primary usage is expected to be through the create_denorm_constraints() function, which leverages the other functions to produce the denormalized constraints for an LP problem.

Notes

Denormalized constraints are defined after the creation of optimization variables and differ from other modules in their handling of constraints for optimization problems. Other modules typically append instructions for constraint creation to a .txt file.

wip.files.lp_denorm_constraints.create_denorm_constraints(lp_problem: pulp.LpProblem, scalers: Dict[str, sklearn.preprocessing.MinMaxScaler], denormalized_constraints: Optional[Dict[str, List[Dict[str, str | bool | float]]]] = None) pulp.LpProblem[source]#

Create denormalized constraints.

Parameters
  • lp_problem (pulp.LpProblem) – A pulp.LpProblem instance, that represents the optimization problem.

  • scalers (Dict[str, sklearn.preprocessing.MinMaxScaler]) – Dictionary of sklearn.preprocessing.MinMaxScaler scalers for each existing variable.

  • denormalized_constraints (Optional[Dict[str, List[Dict[str, str | bool | float]]]]) –

    Dictionary of constraint names and their corresponding equation terms. If None, the default DENORMALIZED_CONSTRAINTS dictionary is used.

    The denormalized_constraints dictionary must have the following structure:

    denormalized_constraints = {
        "<CONSTRAINT_NAME>": [
            {"feature": "<FEATURE_NAME>", "denorm": True/False, "coef": <NUMERIC_VALUE>},
            ...,
            {"operator": <"GTE", "LTE", "EQ">, "value": <NUMERIC_VALUE>},
        ],
        ...
    }
    

Returns

The pulp.LpProblem instance with the added constraints.

Return type

pulp.LpProblem

Notes

Some variable names are defined inside the optimization problem differently from its name inside the scalers dictionary. Therefore, this function contains a step that calls a function created to match these two different names that the variables have.

These naming inconsistencies occur because PuLP doesn’t allow some characters like “/”, “-“, “+” to be used as names when creating the optimization problem variables. Therefore, PuLP converts names like "ROTA1_I@08PE-PN-840I-06M1" to "ROTA1_I@08PE_PN_840I_06M1" when used to name variables.

wip.files.lp_denorm_constraints.denormalize_lpvar(feature_name: str, lpvar: pulp.LpVariable, scalers: dict) pulp.LpVariable | pulp.LpAffineExpression[source]#

Denormalize an LP variable.

Parameters
  • feature_name (str) – Name of the feature.

  • lpvar (pulp.LpVariable) – LP variable to be denormalized.

  • scalers (dict) – Dictionary of sklearn.preprocessing.MinMaxScaler scalers.

Returns

Denormalized LP variable, if a scaler is found. Otherwise, the original LP variable is returned.

Return type

pulp.LpVariable | pulp.LpAffineExpression

Examples

Assuming that an optimization problem contains a normalized variable named "TEMP1_I@08QU_QU_855I_GQ10" that can range from 0 to 1. Calling this function to denormalize this variable results in the following pulp.LpAffineExpression:

>>> lpvars = {
...     "TEMP1_I@08QU_QU_855I_GQ10": pulp.LpVariable("TEMP1_I@08QU_QU_855I_GQ10", lowBound=0, upBound=1)
... }
>>> denormalize_lpvar("TEMP1_I@08QU_QU_855I_GQ10", lpvars["TEMP1_I@08QU_QU_855I_GQ10"], scalers)
261.25*TEMP1_I@08QU_QU_855I_GQ10 + 1125.71

In other words, the normalized variable gets multiplied by a factor, and then a constant is added to it. The resulting expression results in values that represent the original variable scale.

wip.files.lp_denorm_constraints.fan_consumption_constraint(prob: LpProblem) LpProblem[source]#

Change the problem with constraints related to the fan consumption.

This function adds constraints to the linear programming problem related to the energy consumption of fans. It ensures that the sum of the individual fan consumption equals the total fan consumption and that the total fan consumption doesn’t exceed a defined maximum limit.

Parameters
  • prob (pulp.LpProblem) – The linear programming problem to which the constraints will be added.

  • scalers (dict) – A dictionary containing the scalers used to denormalize the linear programming variables.

Returns

The modified linear programming problem with the new constraints added.

Return type

pulp.LpProblem

See also

denormalize_lpvar

Utility function to denormalize the linear programming variables using the provided scalers.

pulp.LpProblem

Pulp’s object representing a Linear Programming problem.

Notes

The function operates by iterating over predefined fan tag names and uses them to access and change the LpProblem. The maximum total fan consumption is hardcoded as 17. The tag names are specific and should be relevant to the context of the LpProblem being solved.

Examples

Here is a simple example of how to use the fan_consumption_constraint function:

>>> import pulp
>>> _prob = pulp.LpProblem("FanConsumptionProblem", pulp.LpMaximize)
>>> _prob = fan_consumption_constraint(_prob)
wip.files.lp_denorm_constraints.process_term(term, lp_problem, scalers)[source]#

Process a term of a denormalized constraint.

Parameters
  • term (dict) – Term of a denormalized constraint.

  • lp_problem (pulp.LpProblem) – A pulp.LpProblem instance.

  • scalers (dict) – Dictionary of sklearn.preprocessing.MinMaxScaler scalers.

Returns

The processed term that might consist of one of the following:

  • If the term is a feature, returns the LP variable.

  • If the term is an operator, returns a function that receives an LP Affine Expression and returns an LP constraint.

Return type

pulp.LpVariable | pulp.LpAffineExpression | Callable

Raises

ValueError – If the term is invalid. A term is considered invalid if it doesn’t have a "feature" or "operator" key.

wip.files.lp_denorm_constraints.process_terms(terms, lp_problem, scalers)[source]#

Process a list of terms from a denormalized constraint.

Parameters
  • terms (list) – A list of terms from a denormalized constraint.

  • lp_problem (pulp.LpProblem) – A pulp.LpProblem instance.

  • scalers (dict) – Dictionary of sklearn.preprocessing.MinMaxScaler scalers.

Returns

A pulp.LpConstraint instance.

Return type

pulp.LpConstraint