Core#

class Algorithm(name: str = '')#

Bases: ABC

Minimal base class for CIFY algorithms. The only required method to implement is the iterate method which is used in Task.

__init__(name: str = '')#
Parameters:

name – An optional name for the Algorithm. If name isn’t specified, the class name will be used.

abstract iterate(f: ObjectiveFunction)#

The only function that must be overridden when implementing your own algorithm. This function must be the logic of one iteration of your algorithm.

Parameters:

f – The ObjectiveFunction to optimize.

class ObjectiveFunction(f: Callable, bounds: list, dim: int | None = None, opt: Optimization = Optimization.Min, name: str = '')#

Bases: object

Defines an objective function to be optimized.

__init__(f: Callable, bounds: list, dim: int | None = None, opt: Optimization = Optimization.Min, name: str = '')#
Parameters:
  • f – The function to be optimized.

  • bounds – The bounds of the search space. Bounds should be passed

in as a list of lists, e.g. [[L, U], [L, U]], or as a [L, U] and use the dim parameter to specify the dimensions. :param dim: The number of dimensions. If unspecified, the length of the bounds will be used. :param optimization: Indicates whether the ObjectiveFunction is to be minimized or maximized. :param name: An optional name for the objective function. If unspecified, the function name will be used.

property evaluations: int#

Returns the number of evaluations performed and resets the counter.

Returns:

Number of function evaluations.

property f: Callable#

Returns the function to be optimized.

property opt: Optimization#

Returns the ObjectiveFunction’s optimization type, either minimization or maximization.

property bounds: list#

The boundary constraints represented as a list of the form:

[[LB1, UB1], …, [LBn, UBn]]

where UB is the upper bound and LB is the lower bound of the search space for that dimension. Can also be passed as a list of the form:

[LB, UB]

where the bounds will apply to all dimensions of the search space.

cmp(a, b) bool#

Returns whether a is better than b.

dim() int#

The number of dimensions of the search space.

eval(vector) float#

Evaluates a vector using the objective function.

Parameters:

vector – The vector to evaluate.

in_bounds(vector: ndarray | List, index: int | None = None)#

Returns whether the vector is within the bounds of the optimization problem.

lower_bounds() list#

Returns a list of floats consisting of the lower bounds of the ObjectiveFunction search space per dimension.

upper_bounds() list#

Returns a list of floats consisting of the upper bounds of the ObjectiveFunction search space per dimension.

sample() ndarray#

Uniformly samples a vector from the bounds of the optimization problem.

class Optimization(value)#

Bases: Enum

The Optimization class is an enum that is used to define whether an ObjectiveFunction is to be minimized or maximized. The class contains two methods, of which one is more useful, cmp.

Min = 1#
Max = 2#
cmp(a, b, equal=False) bool#

A comparison function returned by an objective function which can be used when constructing algorithms to handle optimizing minimization or maximization objective functions. The cmp function takes two arguments and returns True if a is better than b otherwise False.

For minimization objective functions, cmp function checks whether a is less than b, and for maximization objective functions cmp checks whether a greater than b. Think of it as saying, “is a better than the b”.

Returns:

Whether a is better than b.

best(a, b) bool#

Use cmp to return the better parameter.

Returns:

a if cmp(a,b) is True otherwise ``b.

default()#
Returns:

A worst case value, i.e np.inf for Min and -np.inf

for Max.

verb() str#
Returns:

A verb representation of the optimization type.

class Position(input: ndarray | List | ObjectiveFunction)#

Bases: object

Minimal object that defines a decision vector, and it’s evaluation.

__init__(input: ndarray | List | ObjectiveFunction)#
__init__(vector: ndarray | list, f: ObjectiveFunction | None = None, eval_on_init: bool = True)
__init__(vector: ndarray | list, f: ObjectiveFunction | None = None, eval_on_init: bool = True)
__init__(f: ObjectiveFunction, eval_on_init: bool = True)
Parameters:

vector – The position vector. This will always be

converted to a Numpy array. :param f: The ObjectiveFunction to use for Position evaluation.

property value: float#

Returns the current value of the Position.

property vector: ndarray#

Returns the vector as a Numpy array.

dim()#

Returns the dimension of the decision vector.

copy()#

Returns a deep copy of the Position object.

eval(f)#

Evaluates the Position and sets its value field.

class Task(optimizer, f, optimization=Optimization.Min, max_evaluations=inf, max_iterations=inf, cutoff_value=None, log_evaluations=None, log_iterations=None, metrics=None)#

Bases: object

Defines a task to be executed.

__init__(optimizer, f, optimization=Optimization.Min, max_evaluations=inf, max_iterations=inf, cutoff_value=None, log_evaluations=None, log_iterations=None, metrics=None)#
eval(vector) float#
Returns:

The objective function value of the vector.

stopping_condition() bool#
Returns:

Whether the stopping condition has been reached.

next_iteration()#

Increments the iterations counter.

run()#

Optimizes the objective function.

start()#

Start recording the time taken to optimize.

end()#

End recording the time taken to optimize.

time_taken() float#
Returns:

Time taken in seconds to optimize..

log(verbose=True)#
rng() Generator#

Returns the global random number generator used for stochastic operations.

set_seed(seed)#

Sets the global seed for the internal random number generator.

Parameters:

seed (None, int, array-like[ints], numpy.SeedSequence, BitGenerator,) – The seed value to be used by the generator, defaults to None

Generator, optional

positions(n: int, f: ObjectiveFunction)#
Returns:

A list of n positions.