constraints#
Module defines the optimization problem constraints.
- class wip.modules.constraints.Constraints[source]#
Bases:
object
A class for defining and manipulating linear programming constraints.
- add_constraint(constraint: pulp.LpConstraint) None: #
Add a new constraint to the list of constraints.
- remove_constraint(constraint: pulp.LpConstraint) None: #
Remove a constraint from the list of constraints.
- get_constraint_names() List[str]: #
Get a list of all the constraints’ names as the list.
- get_constraint_values(variables: List[pulp.LpVariable]) List[float]: #
Get a list values from all constraints in a list of variable values.
- get_constraint_coefficients(variables: List[pulp.LpVariable]) List[List[float]]: #
Get a list of coefficients from all constraints in a list of variable values.
- get_constraint_bounds() List[Tuple[float, float]]: #
Get a list of lower and upper bounds for all constraints.
- set_constraint_bounds(bounds: List[Tuple[float, float]]) None: #
Set the lower and upper bounds of all constraints in the list.
- set_constraint_coefficients(coefficients: List[List[float]]) None: #
Set the coefficients of all constraints in the list.
Methods
define_term_limit
(term, features_limits, ...)Define the limit for a term in a constraint.
parse_complex_constraints
(terms, ...[, ...])Parse complex constraints, where there is a function in the feature.
write_compressao_min_lim
(file)Define the minimum allowable compression value.
write_feature_constraints
(feature, file, ...)Write lower and upper bound values for a feature (tag).
write_simple_constraints
(file)Write constraints that are constant and each range
write_simple_range_terms
(file, scalers, ...)opera com range_constraints
write_special_constraints
(file, scalers)Write special constraints, specific to each production plant.
write_targets_limits
(file, datasets, ...)Write the constraints created using each model target.
write_variable_constraints
(file, ...)Write constraints that are variable for each range (complex constraints).
define_operator_term
define_range_terms
generic_term_writing
measure_new_coef
parse_data
parse_range_complex_constraints
parse_type_complex_terms
write_calcario_equal
write_complex_constraints
write_gran_ocs_tm_equality
write_gran_ocs_tm_min
write_mill_feed_rate
write_vent_rotation
- static define_term_limit(term, features_limits, range_min, range_max)[source]#
Define the limit for a term in a constraint.
- Parameters
- Returns
The limit for the term in the constraint.
- Return type
- static generic_term_writing(file, first_token, second_token, first_coef, second_coef, operator, final_coef, commom_token=True, constraint_name=None)[source]#
- static parse_complex_constraints(terms, features_limits, scalers, range_min=None, range_max=None)[source]#
Parse complex constraints, where there is a function in the feature.
- Parameters
terms (
list
) – A list of dictionaries, where each dictionary represents a term in the constraint.features_limits (
dict
) – A dictionary containing the minimum and maximum values for each feature.scalers (
dict
) – A dictionary containing theMinMaxScaler
objects for each feature.range_min (
float
, defaultNone
) – The minimum range value for the constraint.range_max (
float
, defaultNone
) – The maximum range value for the constraint.
- Returns
A list of tuples, where each tuple represents a term in the constraint.
- Return type
- static write_compressao_min_lim(file)[source]#
Define the minimum allowable compression value.
Original Tag name:
"COMP_MCOMP_PQ_L@08QU"
Default lower-bound value: 290The function tries to get the lower-bound value from the
wip.constants.critical_cols_dict
dictionary. It searches for the key “COMP_MCOMP_PQ_L@08QU”, and if not found, it tries searching for the key “compressao”. If none of the keys are found, then the default value of 290 is used.- Parameters
file (
File
) – The constraints’ file to write constraint to.scalers (
Dict[str
,sklearn.preprocessing.MinMaxScaler]
) – Dictionary of scalers fitted to each tag, during the predictive models’ creation.
- static write_feature_constraints(feature, file, lmin, lmax)[source]#
Write lower and upper bound values for a feature (tag).
- Parameters
feature (
str
) – The name of the tag to define the lower- and upper-bounds.file (
str | TextIO
) – File to write the feature’s lower- and upper-bounds to.lmin (
int | float | Tuple[float
,float]
) – Lower-bound value of the feature.lmax (
int | float | Tuple[float
,float]
) – Upper-bound value of the feature.
- static write_gran_ocs_tm_equality(file: str, scalers: Dict[str, MinMaxScaler])[source]#
- Parameters
file (str) –
scalers (Dict[str, MinMaxScaler]) –
- static write_simple_range_terms(file, scalers, features_limits)[source]#
opera com range_constraints
- static write_special_constraints(file, scalers)[source]#
Write special constraints, specific to each production plant.
- static write_targets_limits(file, datasets, features_limits)[source]#
Write the constraints created using each model target.
removing those that are already defined
- static write_variable_constraints(file, features_limits, scalers, range_min, range_max)[source]#
Write constraints that are variable for each range (complex constraints).
- Parameters
file (
str | Path
) – The file to write the constraints to.features_limits (
dict
) – A dictionary containing the minimum and maximum values for each feature.scalers (
dict
) – A dictionary containing theMinMaxScaler
objects for each feature.range_min (
Optional[Union[int
,float]]
) – The minimum range value for the constraint.range_max (
Optional[Union[int
,float]]
) – The maximum range value for the constraint.
- wip.modules.constraints.add_energia_pensa_quantile_constraint(prob, datasets, prod_range, lb_limit=0.25, ub_limit=0.75)[source]#
Add constraint to optimization that limits models target tags using quantiles.
Function constraints the following tags:
"energia_prensa"
"energia_moinho"
"relacao_gran"
"gas"
- Parameters
prob (
pulp.LpProblem
) – The problem to which the constraint is added.datasets (
Dict[str
,pd.DataFrame]
) – A dictionary where keys represent model names and values represent the corresponding datasets aspandas.DataFrame
.prod_range (
int
) – The production range to which the constraint is applied.lb_limit (
float
, default0.25
) – The lower-bound limit of the constraint.ub_limit (
float
, default0.75
) – The upper-bound limit of the constraint.
Notes
The
quantile_limits
module does not work for some of the models’ target variables. Therefore, this function was created to add the quantile constraint to tags that represent target variables that are not supported by thequantile_limits
module.Changed in version 2.10.0: Removed from function the lower- and upper-bound definitions for tag “compressao”
- wip.modules.constraints.constraint_taxa_alimentacao_disco(prob: LpProblem, lb_value: int = 90, ub_value: int = 140) LpProblem [source]#
Apply constraints to ensure specific variables are zero or within bounds.
This function enforces constraints on a set of variables within a linear programming problem to ensure that each variable
PESO1_I@08PE-BW-840I-XXM1
is set to 0 if its corresponding binary variableFUNC1_D@08PE-BD-840I-XXM1
is 0. Otherwise, it ensures that thePESO1_I
variables are within specified lower and upper bounds.- Parameters
prob (
pulp.LpProblem
) – The linear programming problem instance to which the constraints will be added.lb_value (
int
, default90
) – The lower-bound value for thePESO1_I
variables when their correspondingFUNC1_D
variables are 1.ub_value (
int
, default140
) – The upper-bound value for thePESO1_I
variables when their correspondingFUNC1_D
variables are 1.
- Returns
The modified LP problem instance with the new constraints added.
- Return type
pulp.LpProblem
Notes
The function assumes that the
scalers
dictionary’s keys correctly match thePESO1_I
variable names and that each scaler has aninverse_transform
method to rescale bounds.The function modifies the
prob
object directly by adding constraints to it.
- wip.modules.constraints.filters_vacuum_bombs_relationship(prob: LpProblem, func_filters: Optional[List[str]] = None, func_vacuum_bombs: Optional[List[str]] = None) LpProblem [source]#
- wip.modules.constraints.fix_grupos_de_queima_limits(prob: LpProblem, scalers: Dict[str, MinMaxScaler])[source]#
Ensures subsequent variables have intersecting limits in an LP problem.
This function adjusts the upper limit of predefined variables within a linear programming (LP) problem to ensure that for each pair of subsequent variables, their limits intersect. Specifically, it focuses on variables named
TEMP1_I@08QU-QU-855I-GQXX
forXX
in the range 9 to 16, adjusting the upper limit of the first variable in the pair to match the lower limit of the second if they do not intersect. This is relevant in scenarios such as optimizing temperature control processes where continuity and overlap in operational ranges are required.- Parameters
prob (
pulp.LpProblem
) – The linear programming problem instance containing the variables to be adjusted.scalers (
Dict[str
,MinMaxScaler]
) – A dictionary mapping variable names to scaler objects. These scalers are used to transform the bounds of the variables to and from a standardized scale.
- Returns
The modified LP problem instance with adjusted variable limits.
- Return type
pulp.LpProblem
Notes
This function directly modifies the
prob
object passed to it, adjusting the upper and lower bounds of specific variables based on the provided scalers. It is specifically designed for variables with names following the patternTEMP1_I@08QU-QU-855I-GQXX
whereXX
ranges from 09 to 16.
- wip.modules.constraints.link_nro_filtros_func_lpvars(prob: pulp.LpProblem, func_tags: List[str] | None = None, n_filters_tag: str | None = None) pulp.LpProblem [source]#
- wip.modules.constraints.link_rota_func_filtros(prob: LpProblem, rota_filtros_low_bound: float = 0.8, rota_filtros_up_bound: float = 1.1, min_filters_active: int = 6) LpProblem [source]#
Link filter rotation and “functioning” tags.
This function iterates over predefined pairs of binary and continuous variables in a linear programming problem. It links these variables using the
lp_define_or_constraint
function, applying optional MinMax inverse scaling to the continuous variables, if a value for the parameter :param:`scalers` is provided. The function mainly operates on a provided PuLP problem object, modifying it by adding constraints.The filter rotation and functioning tags are linked as follows:
If functioning tag is equal to 0, then the rotation tag is also zero.
If functioning tag is equal to 1, then the rotation tag must be between the specified :param:`rota_filtros_low_bound` and :param:`rota_filtros_up_bound` lower- and upper-bounds.
- Parameters
prob (
pulp.LpProblem
) – The linear programming problem to which constraints and links are added.rota_filtros_low_bound (
float
, default0.8
) – The lower bound to be used in thelp_define_or_constraint
function for linking variables.rota_filtros_up_bound (
float
, default1.1
) – The upper bound to be used in thelp_define_or_constraint
function for linking variables.min_filters_active (
int
, default6
) –Minimum quantity of filters that need to be working in parallel. In other words:
\sum_{i=1}^{10} \text{FUNC1_D@08FI-FL-827I-0i} \geq \text{min_filters_active}
- Returns
The modified linear programming problem with added constraints.
- Return type
pulp.LpProblem
Examples
>>> prob = pulp.LpProblem("Example_Problem", pulp.LpMaximize) >>> prob = link_rota_func_filtros(prob) This adds constraints to 'prob', linking predefined pairs of binary and continuous variables, and optionally applies scaling to the continuous variables.
Notes
If a scaler name corresponding to a certain filter rotation variable is not found, the variable won’t get re-scaled back to its original range, even though the :param:`scalers` dictionary was provided.