otaf.optimization package

Module contents

class otaf.optimization.AcceptTest(storage, defect_description, distance_threshold=1.0, surrogate_model=None)[source]

Bases: object

Custom class for accepting or rejecting new steps in the optimization process.

Attributes:

storageOptimizationStorage

An instance of the OptimizationStorage class to store and manage optimization points.

linear_constraintsLinearConstraint

A function to enforce linear constraints on the variables.

distance_thresholdfloat

Minimum distance between points to avoid revisiting.

surrogate_modelcallable

A callable to update a surrogate model based on new points.

__call__(f_new, x_new, f_old, x_old)[source]

Decide whether to accept or reject a new step in the optimization.

Parameters:

f_newfloat

The function value at the new point.

x_newarray-like

The coordinates of the new point.

f_oldfloat

The function value at the previous point.

x_oldarray-like

The coordinates of the previous point.

Returns:

: bool

True if the new step is accepted, False otherwise.

class otaf.optimization.Callback(storage, stop_criterion=None)[source]

Bases: object

A callback class to store accepted local minima and allow data sharing between components.

__call__(x, f, accept)[source]

Store the current local minimum if accepted and apply the stop criterion if provided.

Parameters:

xarray-like

The coordinates of the current minimum.

ffloat

The function value at the current minimum.

acceptbool

Whether the current minimum was accepted.

Returns:

: bool or None

Return True to stop the basinhopping routine if the stop criterion is met, otherwise return None.

get_data()[source]

Retrieve the stored local minima points.

class otaf.optimization.ModifiedBasinHopping(func, x0, constraints=None, niter=100, T=1.0, stepsize=0.5, minimizer_kwargs=None, interval=50, disp=False, niter_success=None, seed=None, jac=False, jac_threshold=1e-05)[source]

Bases: object

accept_test(f_new, x_new, f_old, x_old)[source]

Metropolis criterion for acceptance of the new point.

callback(x, f, accept)[source]

Optional callback function to store minima.

run()[source]
take_step(x)[source]

Takes a random step and projects it back into the feasible space if constraints are violated.

class otaf.optimization.OptimizationStorage(bounds=None, constraint_function=None)[source]

Bases: object

Class to store, manage, and analyze optimization data. This class keeps track of points explored during optimization, whether generated by a global or local optimizer, and verifies if the points respect user-defined constraints and bounds.

Key Features:

  • Stores each point’s coordinates and the corresponding objective function value.

  • Tracks whether each point was generated by a local or global optimization method.

  • Automatically checks if points respect user-specified variable bounds.

  • Evaluates whether points satisfy given constraints (passed via a constraint function).

  • Offers utility methods to filter, manage, and query stored data.

Parameters:

boundslist of tuple or None, optional

List of tuples defining (lower, upper) bounds for each variable. If None, no bounds are applied (default: None).

constraint_functioncallable or None, optional

A function that takes a point (array-like) as input and returns a boolean indicating whether the point satisfies the constraints (True if satisfied, False otherwise). If None, no constraints are applied (default: None).

Attributes:

datapandas.DataFrame

A DataFrame that stores optimization points and related metadata, with the following columns: - ‘point’ : array-like, the coordinates of the explored point. - ‘function_value’ : float, the value of the objective function at the given point. - ‘source’ : str, whether the point was generated from a local or global optimizer (‘local’ or ‘global’). - ‘bounds_respected’ : bool, whether the point respects the variable bounds. - ‘constraints_respected’ : bool, whether the point satisfies the constraint function.

Methods:

add_point(x, f, source):

Adds a new optimization point with its function value, source, and checks for bounds and constraints compliance.

check_bounds(x):

Verifies if a point respects the variable bounds.

check_constraints(x):

Verifies if a point satisfies the constraint function.

apply_checks_to_all():

Reapplies bounds and constraints checks to all stored points, updating the DataFrame.

get_all_data():

Returns the complete DataFrame of stored optimization points.

get_points_by_source(source):

Filters and returns points generated by either the local or global optimizer.

clear_storage():

Clears the stored data while retaining the DataFrame structure.

__iter__()[source]

Iterate over all points in the storage by default.

add_point(x, f, source)[source]

Add a point, its function value, and its source (local or global) to the DataFrame.

apply_checks_to_all()[source]

Apply bounds and constraint checks to all entries in the DataFrame.

check_bounds(x)[source]

Check if the point respects the bounds.

check_constraints(x)[source]

Check if the point respects the constraints.

clear_storage()[source]

Clear the DataFrame.

filter_points(source=None, bounds_respected=None, constraints_respected=None)[source]

Filter the points based on source, bounds compliance, and constraint compliance.

Parameters:

sourcestr, optional

Filter by the source of the point (‘local’ or ‘global’).

bounds_respectedbool, optional

Filter by whether the bounds were respected.

constraints_respectedbool, optional

Filter by whether the constraints were respected.

Returns:

: pd.DataFrame

A DataFrame filtered according to the provided criteria.

get_all_data()[source]

Return the entire DataFrame with all stored points.

get_points_by_source(source)[source]

Return points filtered by their source (local or global).

iter_points(source=None, bounds_respected=None, constraints_respected=None)[source]

Generator for iterating over points based on specific filters.

Parameters:

sourcestr, optional

Iterate over points filtered by their source (‘local’ or ‘global’).

bounds_respectedbool, optional

Iterate over points filtered by whether bounds were respected.

constraints_respectedbool, optional

Iterate over points filtered by whether constraints were respected.

Yields:

indexint

Index of the point in the DataFrame.

rowpd.Series

Row from the DataFrame with point information.

class otaf.optimization.StepTaking(storage, defect_description, stepsize=0.5)[source]

Bases: object

Custom class for generating bounded steps in the optimization space.

Attributes:

linear_conditioncallable

A function to enforce linear constraints on subsets of variables.

stepsizefloat

The size of the step to be taken (default 0.5).

__call__(x)[source]

Generate a new candidate step based on the current position ‘x’. The step respects the provided bounds and enforces the linear condition.

Parameters:

xarray-like

The current point in the search space.

Returns:

: new_x : array-like

The new point after taking a step.

otaf.optimization.bounds_from_composed_distribution(composed_distribution, tol=1e-24)[source]
otaf.optimization.check_constraint_with_tolerance(linear_constraint, x, tolerance=1e-06)[source]

Check if the vector x satisfies the linear constraint within a specified tolerance.

Parameters:
  • linear_constraint (scipy.optimize.LinearConstraint) – The linear constraint object containing A, lb, and ub.

  • x (array_like) – The vector of independent variables to check.

  • tolerance (float, optional) – The tolerance within which the constraint should be satisfied (default is 1e-6).

Returns:

  • bool – True if the constraint is satisfied within the tolerance, False otherwise.

  • dict – A dictionary containing the lower and upper residuals.

otaf.optimization.create_constraint_checker(constraint, tolerance=1e-06)[source]

Creates a function to check if a vector x satisfies the given constraint (linear or nonlinear) within a specified tolerance.

Parameters:
  • constraint (LinearConstraint or NonlinearConstraint) – The constraint object containing A, lb, and ub for linear, or fun, lb, and ub for nonlinear.

  • tolerance (float, optional) – The tolerance within which the constraint should be satisfied (default is 1e-6).

Returns:

A function that takes a vector x and returns a boolean indicating if the constraint is satisfied within the tolerance, and the residuals as a dictionary.

Return type:

function

otaf.optimization.lambda_constraint_dict_from_composed_distribution(composed_distribution, tol=1e-12, keep_feasible=True)[source]

Condition lambda parameters to sum to 1 for each feature.

Parameters:

sample (ot.Sample) – Lambda parameters with each feature ending in an integer sequence.

Returns:

Conditioned lambda parameters.

Return type:

ot.Sample

Raises:

ValueError – If the description is missing or invalid.

otaf.optimization.scaling(scale_factor)[source]

Decorator to scale the output of a function by a specified scaling factor.

Parameters:

scale_factor (float) – The factor by which to scale the output of the wrapped function.

Returns:

A decorator that applies the scaling to the output of the wrapped function.

Return type:

function