Concepts#
Variable#
- class udao.optimization.Variable#
Variable to optimize.
- class udao.optimization.EnumVariable(values: list)#
Bases:
Variable
Categorical variable (non-numeric)
- class udao.optimization.FloatVariable(lower: float, upper: float)#
Bases:
NumericVariable
Numeric variable with float values.
- class udao.optimization.IntegerVariable(lower: int, upper: int)#
Bases:
NumericVariable
Numeric variable with integer values.
- class udao.optimization.BoolVariable#
Bases:
IntegerVariable
Boolean variable.
Constraint#
- class udao.optimization.Constraint(function: UdaoFunction | Module | Callable[[...], Tensor], lower: float | None = None, upper: float | None = None)#
An optimization element is either an objective or a constraint.
The choice of the type depends on whether a DataProcessor is specified for the problem: - if no DataProcessor is provided: UdaoFunction, it is a callable that takes input_variables and input_parameters - else, th.nn.Module or other Callable returning a tensor.
- Parameters:
function (Union[UdaoFunction, th.nn.Module, Callable[..., th.Tensor]]) – Objective function, either a UdaoFunction or a th.nn.Module if a DataProcessor is provided
lower (Optional[float], optional) – lower bound of the element, by default None
upper (Optional[float], optional) – upper bound of the element, by default None
Objective#
- class udao.optimization.Objective(name: str, minimize: bool, function: UdaoFunction | Module | Callable[[...], Tensor], lower: float | None = None, upper: float | None = None, type: VarTypes = VarTypes.FLOAT)#
Bases:
Constraint
- Parameters:
name (str) – Name of the objective.
minimize (bool) – Direction of the objective: if True, minimize, else maximize.
type (VarTypes) – Type of the objective, by default VarTypes.FLOAT
Problem#
- class udao.optimization.concepts.problem.BaseProblem(variables: Dict[str, Variable], constraints: Sequence[Constraint], data_processor: DataProcessor | None = None, input_parameters: Dict[str, Any] | None = None)#
Base class for optimization problems.
- class udao.optimization.SOProblem(objective: Objective, variables: Dict[str, Variable], constraints: Sequence[Constraint], data_processor: DataProcessor | None = None, input_parameters: Dict[str, Any] | None = None)#
Bases:
BaseProblem
Single-objective optimization problem.
- class udao.optimization.MOProblem(objectives: Sequence[Objective], variables: Dict[str, Variable], constraints: Sequence[Constraint], data_processor: DataProcessor | None = None, input_parameters: Dict[str, Any] | None = None)#
Bases:
BaseProblem
Multi-objective optimization problem.
Utils#
- class udao.optimization.concepts.utils.InaccurateModel(model: Module, std_func: Module, alpha: float)#
Bases:
Module
A helper class to define an inaccurate model as a function of a model and a standard deviation function.
- Parameters:
model (th.nn.Module) – The model to be used
std_func (th.nn.Module) – The standard deviation function associated to the model
alpha (float) – The factor to multiply the standard deviation by before adding it to the model output
- forward(x: Tensor) Tensor #
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class udao.optimization.concepts.utils.ModelComponent(data_processor: DataProcessor, model: Module)#
Bases:
object
A wrapper class for a model paired with a DataProcessor, that complies with the UdaoFunction protocol.
- process_data(input_variables: Dict[str, ndarray] | Dict[str, Any], input_parameters: Dict[str, Any] | None = None) Tuple[Any, BaseIterator] #
Derive the batch input from the input dict.
- class udao.optimization.concepts.utils.UdaoFunction(*args, **kwargs)#
Bases:
Protocol
A function that can be used for a constraint or an objective.
- udao.optimization.concepts.utils.derive_processed_input(data_processor: DataProcessor[UdaoIterator], input_variables: Dict[str, ndarray] | Dict[str, Any], input_parameters: Dict[str, Any] | None = None, device: device | None = None) Tuple[UdaoInput, UdaoIterator] #
Derive the batch input from the input dict using a DataProcessor
- Parameters:
input_non_decision (Dict[str, Any]) – The fixed values for the non-decision inputs
input_variables (Dict[str, np.ndarray] | Dict[str, Any]) – The values for the variables inputs: - a batch of all values if the variable is a numpy array - a batch of a single value if the variable is a single value
- Returns:
The batch input for the model
The iterator used to generate the batch input
- Return type:
Tuple[UdaoInput, BaseIterator]
- udao.optimization.concepts.utils.derive_unprocessed_input(input_variables: Dict[str, ndarray] | Dict[str, Any], input_parameters: Dict[str, Any] | None = None, device: device | None = None) Tuple[Dict[str, Tensor], Dict[str, Any]] #
Derive the input data from the input values, in the case where the input is not processed by a DataProcessor
- Parameters:
input_variables (InputVariables) – a dictionary of the variables inputs (one or several values for several variables samples)
input_parameters (InputParameters, optional) – a dictionary of the non-decision inputs (one value per parameter as the value is fixed), by default None
device (th.device, optional) – device on which to put the resulting tensors, by default th.device(“cpu”)
- Returns:
the input data for the model, with the variables inputs as tensors and the non-decision inputs as values
- Return type:
Tuple[Dict[str, th.Tensor], Dict[str, Any]]