turboflow.utilities.optimization_utils module
- turboflow.utilities.optimization_utils.evaluate_constraints(data, constraints)[source]
Evaluates the constraints based on the provided data and constraint definitions.
- Parameters:
- datadict
A dictionary containing performance data against which the constraints will be evaluated.
- constraintslist of dicts
A list of constraint definitions, where each constraint is defined as a dictionary. Each dictionary must have ‘variable’ (str), ‘type’ (str, one of ‘=’, ‘>’, ‘<’), and ‘value’ (numeric).
- Returns:
- tuple of numpy.ndarray
Returns two numpy arrays: the first is an array of equality constraints, and the second is an array of inequality constraints. These arrays are flattened and concatenated from the evaluated constraint values.
- Raises:
- ValueError
If an unknown constraint type is specified in the constraints list.
- turboflow.utilities.optimization_utils.evaluate_objective_function(data, objective_function)[source]
Evaluates the objective function based on the provided data and configuration.
- Parameters:
- datadict
A dictionary containing performance data against which the objective function will be evaluated.
- objective_functiondict
A dictionary defining the objective function. It must have ‘variable’ (str) and ‘type’ (str, either ‘minimize’ or ‘maximize’).
- Returns:
- float
The value of the objective function, adjusted for optimization. Positive for minimization and negative for maximization.
- Raises:
- ValueError
If an unknown objective function type is specified in the configuration.
- turboflow.utilities.optimization_utils.render_and_evaluate(expression, data)[source]
Render variables prefixed with ‘$’ in an expression and evaluate the resulting expression.
This function processes an input string expr, identifying all occurrences of variables indicated by a leading ‘$’ symbol. Each such variable is resolved to its value from the provided context dictionary. The expression with all variables resolved is then evaluated and the result is returned.
This function is useful to render strings defined in a YAML configuration file to values that are calculated within the code and stored in a dicitonary.
- Parameters:
- exprstr
The expression string containing variables to be rendered. Variables in the expression are expected to be prefixed with a ‘$’ symbol.
- datadict
A dictionary containing variables and their corresponding values. These variables are used to render values in the expression.
- Returns:
- The result of evaluating the rendered expression. The type of the result depends on the
- expression.
Notes
pattern: A regular expression pattern used to identify variables within the expression. Variables are expected to be in the format $variableName, potentially with dot-separated sub-properties (e.g., $variable.property).
replace_with_value: An inner function that takes a regex match object and returns the value of the variable from context. match.group(1) returns the first captured group from the matched text, which in this case is the variable name excluding the leading ‘$’ symbol. For example, in $variableName, match.group(1) would return variableName.
The function uses Python’s eval for evaluation, which should be used cautiously as it can execute arbitrary code. Ensure that the context and expressions are from a trusted source.
- turboflow.utilities.optimization_utils.render_nested_value(nested_key, data)[source]
Retrieves a value from a nested structure (dictionaries or objects with attributes) using a dot-separated key.
This function is designed to navigate through a combination of dictionaries and objects. For an object to be compatible with this function, it must implement a keys() method that returns its attribute names.
This function is intended as a subroutine of the more genera
render_expression
- Parameters:
- nested_keystr
A dot-separated key string that specifies the path in the structure. For example, ‘level1.level2.key’ will retrieve data[‘level1’][‘level2’][‘key’] if data is a dictionary, or data.level1.level2.key if data is an object or a combination of dictionaries and objects.
- datadict or object
The starting dictionary or object from which to retrieve the value. This can be a nested structure of dictionaries and objects.
- Returns:
- value
The value retrieved from the nested structure using the specified key. The type of the value depends on what is stored at the specified key in the structure.
- Raises:
- KeyError
If the specified nested key is not found in the data structure. The error message includes the part of the path that was successfully traversed and the available keys or attributes at the last valid level.