optilab.data_classes package

Submodules

optilab.data_classes.bounds module

Class representing bounds of the search space.

class Bounds(lower: float, upper: float)[source]

Bases: object

Class representing bounds of the search space.

lower: float

The lower bound of search space.

upper: float

The upper bound of search space.

to_list() List[float][source]

Return the bounds as a list of two floats.

Returns:

List containing the lower and upper bound.

__len__() float[source]

Returns the width of the search space - the distance between the lower and upper bound.

Returns:

The width of the search space.

__str__() str[source]

Express the bounds as a string.

Returns:

Simple, printable string representation of this object.

is_valid() bool[source]

Check if the bounds are valid, i.e. if lower bound is below upper bound.

Returns:

True if bounds are valid, false otherwise.

__contains__(point: Point) bool[source]

Check if a point lies in the bounds. This method overrides the “in” operator.

Returns:

True if point lies in the bounds.

random_point(dim: int) Point[source]

Sample the bounds for a random point of given dimensionality.

Parameters:

dim – The dimensionality of the point.

Returns:

Randomly sampled point from the search space.

random_point_list(num_points: int, dim: int) PointList[source]

Sample the bounds for a list of random points of given dimensionality.

Parameters:
  • num_points – The number of points to sample.

  • dim – The dimensionality of the points.

Returns:

List of randomly sampled points from the search space.

reflect(point: Point) Point[source]

Handle bounds by reflecting the point back into the search area.

Parameters:

point – The point to handle.

Returns:

Reflected point.

wrap(point: Point) Point[source]

Handle bounds by wrapping the point around the search area.

Parameters:

point – The point to handle.

Returns:

Wrapped point.

project(point: Point) Point[source]

Handle bounds by projecting the point onto the bounds of the search area.

Parameters:

point – The point to handle.

Returns:

Projected point.

handle_bounds(point: Point, mode: str) Point[source]

Function to choose the bound handling method by name of the method.

Parameters:
  • point – The point to handle.

  • mode – Bound handling mode to use, choose from reflect, wrap or project.

Returns:

Handled point.

optilab.data_classes.function_metadata module

Metadata of objective function.

class FunctionMetadata(name: str, dim: int, hyperparameters: Dict[str, Any])[source]

Bases: object

Metadata of objective function.

name: str

Name of the function.

dim: int

Dimensionality of the function.

hyperparameters: Dict[str, Any]

Other hyperparameters of the function.

optilab.data_classes.optimization_run module

Class containing information about an optimization run.

class OptimizationRun(model_metadata: OptimizerMetadata, function_metadata: FunctionMetadata, bounds: Bounds, tolerance: float, logs: List[PointList])[source]

Bases: object

Dataclass containing information about an optimization run.

model_metadata: OptimizerMetadata

Metadata describing the model used in optimization.

function_metadata: FunctionMetadata

Metadata describing the optimized function.

bounds: Bounds

Bounds of the search space.

tolerance: float

Tolerated error value to stop the search.

logs: List[PointList]

Logs of points from the optimization runs.

bests_y(raw_values: bool = False) List[float][source]

Get a list of best y values from each log.

Parameters:

raw_values – If false, values below tolerance values are set to tolerance, else return real y values. Default is false.

Returns:

List of the best values from each log.

log_lengths() List[float][source]

Get a list of log lengths.

Returns:

List of the lengths of logs.

stats(raw_values: bool = False) DataFrame[source]

Make a summary of the run.

Parameters:

raw_values – If false, values below tolerance values are set to tolerance, else return real y values. Default is false.

Returns:

pd.DataFrame: Dataframe containing stats and summary of the run.

remove_x() None[source]

Set x values of points in the logs to None. This is done to save memory and storage since x values are rarely used.

optilab.data_classes.optimizer_metadata module

Metadata of an optimizer model.

class OptimizerMetadata(name: str, population_size: int, hyperparameters: Dict[str, Any] | None = None)[source]

Bases: object

Metadata of an optimizer model.

name: str

Name of the optimizer.

population_size: int

Number of points generated in each generation.

hyperparameters: Dict[str, Any] | None = None

Other hyperparameters of the optimizer, optional.

optilab.data_classes.point module

Point dataclass, representing a point in the search space.

class Point(x: ndarray | None, y: float | None = None, is_evaluated: bool = False)[source]

Bases: object

Point dataclass, representing a point in the search space.

x: ndarray | None

1D vector representing the point from the search space

y: float | None = None

Function value for this point.

is_evaluated: bool = False

Wheather the value was generated by the objective function.

__post_init__()[source]

Post init hook to ensure that the x value is float 64.

dim() int[source]

Return the dimensionality of the point.

Returns:

Dimensionality of the point, equal to length of x.

remove_x() None[source]

Set x to None. This is done mostly to save memory and storage space since x is rarely used.

__eq__(other) bool[source]

Compare with other point. Points are considered equal if their x values are equal. Other properites are not compared.

Parameters:

other – Another instance of Point class.

Returns:

True if x values of self and other are equal, False otherwise.

optilab.data_classes.point_list module

Class holding a list of points.

class PointList(points: List[Point])[source]

Bases: object

Class holding a list of points. Might be used as optimization run result log or as a train set for surrogate function.

points: List[Point]

The list of points

classmethod from_list(xs: List[ndarray]) PointList[source]

Alternative constructor that takes a list of x values.

Parameters:

xs – List of x values.

Returns:

Object of class PointList containing points with given x.

append(new_point: Point) None[source]

Add new point to the list.

Parameters:

new_point – Point to append to this object.

extend(new_points: PointList) None[source]

Append a list of points to this PointList.

Parameters:

new_points – A list of point to append to this object.

x() ndarray[source]

Get all x values of points in this list.

Returns:

List containing x values of all points.

y() ndarray[source]

Get all y values of points in this list.

Returns:

List of y values of all points.

pairs() Tuple[ndarray, ndarray][source]

Return the contents of this point list as list of x and list of y values. This is potentially useful for quickly accesing point values for training surrofates.

Returns:

Lists of x and y values.

only_evaluated() PointList[source]

Return list of only those points that have been evaluated.

Returns:

List containing evaluated points.

__getitem__(index: int | slice) Point | PointList[source]

Allows indexing and slicing this object like a list.

Parameters:

index – The index or slice of objects to fetch.

Returns:

A single Point object for integer index,

or a new PointList instance for slicing.

__len__() int[source]

Return number of points stored in the list.

Returns:

Number of points stored in the list.

rank(*, reverse: bool = False) None[source]

Sort points by y value in place ascending.

Parameters:

reverse – If true, sorting is done descending. Default False.

x_difference(other) PointList[source]

Return list of points in self that do not appear in other based on their x values.

Parameters:

other – Another PointList to compare against.

Returns:

List of points in self that are not in other.

remove_x() None[source]

Set x values of points to None. This is done to save memory since xs are rarely used.

best() Point[source]

Get the best point by y value from the PointList.

Returns:

The Point with the lowest y value in the list.

best_index() int[source]

Get the index of the best point by y value in the PointList.

Returns:

The index of the point with the lowest y value.

best_y() float[source]

Get the best y value found. If list is empty, infinity is returned.

Returns:

The best y value found.

slice_to_best() PointList[source]

Return a list of all points up to the best in the list, including the best.

Returns:

List of points up to the best point.

Module contents

Classes used for data exchange.