moptipy.mock package

Mock parts of the moptipy API (mainly for testing purposes).

Submodules

moptipy.mock.components module

Generate random mock experiment parameters.

class moptipy.mock.components.Algorithm(name, strength, jitter, complexity)[source]

Bases: object

An immutable algorithm description record.

complexity: float

The algorithm complexity, in (0, 1), larger values are worst

static create(n, forbidden=None, random=Generator(PCG64) at 0x7F6680AB4C80)[source]

Create a set of fixed mock algorithms.

Parameters:
  • n (int) – the number of algorithms to generate

  • random (Generator, default: Generator(PCG64) at 0x7F6680AB4C80) – a random number generator

  • forbidden (Any | None, default: None) – the forbidden names and strengths and so on

Return type:

tuple[Algorithm, ...]

Returns:

a tuple of algorithms

jitter: float

The algorithm jitter, in (0, 1), larger values are worst

name: str

The algorithm name.

strength: float

The algorithm strength, in (0, 1), larger values are worst

class moptipy.mock.components.BasePerformance(algorithm, instance, performance, jitter, speed)[source]

Bases: object

An algorithm applied to a problem instance description record.

algorithm: Algorithm

the algorithm.

static create(instance, algorithm, random=Generator(PCG64) at 0x7F6680AB4C80)[source]

Compute the basic performance of an algorithm on a problem instance.

Parameters:
  • instance (Instance) – the instance tuple

  • algorithm (Algorithm) – the algorithm tuple

  • random (Generator, default: Generator(PCG64) at 0x7F6680AB4C80) – the random number generator

Return type:

BasePerformance

Returns:

a tuple of the performance in (0, 1); bigger values are worse, and a jitter in (0, 1), where bigger values are worse

instance: Instance

the problem instance

jitter: float

the performance jitter, in (0, 1), larger values are worst

performance: float

the base performance, in (0, 1), larger values are worst

speed: float

the time per FE, in (0, 1), larger values are worst

class moptipy.mock.components.Experiment(instances, algorithms, applications, per_instance_seeds)[source]

Bases: object

An immutable experiment description.

algorithm_applications(algorithm)[source]

Get the applications of an algorithm to the instances.

Parameters:

algorithm (str | Algorithm) – the algorithm

Return type:

tuple[BasePerformance, ...]

Returns:

the applications

algorithm_names: tuple[str, ...]

the algorithm names

algorithms: tuple[Algorithm, ...]

The algorithms.

applications: tuple[BasePerformance, ...]

The applications of the algorithms to the instances.

static create(n_instances, n_algorithms, n_runs, random=Generator(PCG64) at 0x7F6680AB4C80)[source]

Create an experiment definition.

Parameters:
  • n_instances (int) – the number of instances

  • n_algorithms (int) – the number of algorithms

  • n_runs (int) – the number of per-instance runs

  • random (Generator, default: Generator(PCG64) at 0x7F6680AB4C80) – the random number generator to use

Return type:

Experiment

get_algorithm(name)[source]

Get an algorithm by name.

Parameters:

name (str) – the algorithm name

Return type:

Algorithm

Returns:

the algorithm instance

get_instance(name)[source]

Get an instance by name.

Parameters:

name (str) – the instance name

Return type:

Instance

Returns:

the instance

instance_applications(instance)[source]

Get the applications of the algorithms to a specific instance.

Parameters:

instance (str | Instance) – the instance

Return type:

tuple[BasePerformance, ...]

Returns:

the applications

instance_names: tuple[str, ...]

the instance names

instances: tuple[Instance, ...]

The instances.

per_instance_seeds: tuple[tuple[int, ...]]

The random seeds per instance.

seeds_for_instance(instance)[source]

Get the seeds for the specified instance.

Parameters:

instance (str | Instance) – the instance

Return type:

tuple[int, ...]

Returns:

the seeds

class moptipy.mock.components.Instance(name, hardness, jitter, scale, best, worst, attractors)[source]

Bases: object

An immutable instance description record.

attractors: tuple[int, ...]

The set of attractors, i.e., local optima - including best and worst

best: int

The best (smallest) possible objective value

static create(n, forbidden=None, random=Generator(PCG64) at 0x7F6680AB4C80)[source]

Create a set of fixed problem instances.

Parameters:
  • n (int) – the number of instances to generate

  • random (Generator, default: Generator(PCG64) at 0x7F6680AB4C80) – a random number generator

  • forbidden (Any | None, default: None) – the forbidden names and hardnesses

Return type:

tuple[Instance, ...]

Returns:

a tuple of instances

hardness: float

The instance hardness, in (0, 1), larger values are worst

jitter: float

The instance jitter, in (0, 1), larger values are worst

name: str

The instance name.

scale: float

The instance scale, in (0, 1), larger values are worst

worst: int

The worst (largest) possible objective value

moptipy.mock.components.fixed_random_generator()[source]

Get the single, fixed random generator for the dummy experiment API.

Return type:

Generator

Returns:

the random number generator

moptipy.mock.components.get_run_seeds(instance, n_runs)[source]

Get the seeds for the runs.

Parameters:
  • instance (Instance) – the mock instance

  • n_runs (int) – the number of runs

Return type:

tuple[int, ...]

Returns:

a tuple of seeds

moptipy.mock.end_results module

Compute the end result of one mock run.

class moptipy.mock.end_results.EndResults(experiment, results, max_fes=None, max_time_millis=None)[source]

Bases: object

An immutable set of end results.

static create(experiment, max_fes=None, max_time_millis=None)[source]

Create the end results for a given experiment.

Parameters:
  • experiment (Experiment) – the experiment

  • max_fes (int | None, default: None) – the maximum number of FEs

  • max_time_millis (int | None, default: None) – the maximum time

Return type:

EndResults

Returns:

the end results

experiment: Experiment

The experiment.

max_fes: int | None

The maximum permitted FEs.

max_time_millis: int | None

The maximum permitted milliseconds.

results: tuple[EndResult, ...]

The end results.

results_for_algorithm(algorithm)[source]

Get the end results per algorithm.

Parameters:

algorithm (str | Algorithm) – the algorithm

Return type:

tuple[EndResult, ...]

Returns:

the end results

results_for_instance(instance)[source]

Get the end results per instance.

Parameters:

instance (str | Instance) – the instance

Return type:

tuple[EndResult, ...]

Returns:

the end results

moptipy.mock.end_results.end_result(performance, seed, max_fes=None, max_time_millis=None)[source]

Compute the end result of a mock run.

Parameters:
  • performance (BasePerformance) – the performance record

  • seed (int) – the random seed

  • max_fes (int | None, default: None) – the maximum number of FEs

  • max_time_millis (int | None, default: None) – the maximum time

Return type:

EndResult

Returns:

the end result record

moptipy.mock.mo_problem module

The mock multi-objective optimization problem.

class moptipy.mock.mo_problem.MockMOProblem(objectives, weights)[source]

Bases: WeightedSum

A mock-up of a multi-objective optimization problem.

static for_dtype(n, dtype)[source]

Create a mock multi-objective problem.

Parameters:
  • n (int) – the number of objectives

  • dtype (dtype) – the optional dtype

Return type:

MockMOProblem

Returns:

the mock multi-objective problem

get_objectives()[source]

Get the internal objective functions.

Return type:

tuple[MockObjective, ...]

Returns:

the internal mock objective functions

sample(fs)[source]

Sample one vector of ojective values.

Parameters:

fs (ndarray) – the array to receive the objective values

Return type:

int | float

Returns:

the scalarized objective values

moptipy.mock.objective module

A mock-up of an objective function.

class moptipy.mock.objective.MockObjective(is_int=True, lb=-inf, ub=inf, fmin=None, fattractors=None, fmax=None, seed=None)[source]

Bases: Objective

A mock-up of an objective function.

evaluate(x)[source]

Return a mock objective value.

Parameters:

x – the candidate solution

Return type:

float | int

Returns:

the objective value

fattractors: Final[tuple[int | float, ...]]

the mean value the function actually takes on

fmax: Final[int | float]

the maximum value the function actually takes on

fmin: Final[int | float]

the minimum value the function actually takes on

static for_type(dtype)[source]

Create a mock objective function with values bound by a given dtype.

Parameters:

dtype (dtype) – the numpy data type

Return type:

MockObjective

Returns:

the mock objective function

is_always_integer()[source]

Return True if evaluate() will always return an int value.

Return type:

bool

Returns:

True if evaluate() will always return an int or False if also a float may be returned.

is_int: Final[bool]

is this objective integer?

lb: Final[int | float]

the lower bound

log_parameters_to(logger)[source]

Log the special parameters of tis mock objective function.

Return type:

None

lower_bound()[source]

Get the lower bound of the objective value.

Return type:

float | int

Returns:

the lower bound of the objective value

name: Final[str]

the name of this objective function

sample()[source]

Sample the mock objective function.

Return type:

int | float

Returns:

the value of the mock objective function

seed: Final[int]

the random seed

ub: Final[int | float]

the upper bound

upper_bound()[source]

Get the upper bound of the objective value.

Return type:

float | int

Returns:

the upper bound of the objective value

moptipy.mock.utils module

Utilities for mock objects.

moptipy.mock.utils.DEFAULT_TEST_DTYPES: Final[tuple[dtype, ...]] = (dtype('float16'), dtype('float32'), dtype('float64'), dtype('float128'), dtype('int8'), dtype('int16'), dtype('int32'), dtype('int64'), dtype('uint8'), dtype('uint16'), dtype('uint32'), dtype('uint64'))

The default types to be used for testing.

moptipy.mock.utils.make_ordered_list(definition, is_int, random)[source]

Make an ordered list of elements, filling in gaps.

This function takes a list template where some values may be defined and some may be left None. The None values are then replaced such that an overall ordered list is created where each value is larger than its predecessor. The original non-None elements are kept in place. Of course, this process may fail, in which case None is returned.

Parameters:
  • definition (Sequence[int | float | None]) – a template with None for gaps to be filled

  • is_int (bool) – should all generated values be integers?

  • random (Generator) – the generator

Return type:

list[int | float] | None

Returns:

the refined tuple with all values filled in

>>> from numpy.random import default_rng
>>> rg = default_rng(11)
>>> make_ordered_list([None, 10, None, None, 50], True, rg)
[-10, 10, 42, 47, 50]
>>> make_ordered_list([None, 10, None, None, 50, None, None], False, rg)
[-5.136, 10, 13.228, 15.19, 50, 115.953, 125.961]
>>> print(make_ordered_list([9, None, 10, None, None, 50], True, rg))
None
>>> make_ordered_list([8, None, 10, None, None, 50], True, rg)
[8, 9, 10, 45, 47, 50]
>>> make_ordered_list([9, None, 10, None, None, 50], False, rg)
[9, 9.568, 10, 47.576, 49.482, 50]
moptipy.mock.utils.sample_from_attractors(random, attractors, is_int=False, lb=-inf, ub=inf)[source]

Sample from a given range using the specified attractors.

Parameters:
  • random (Generator) – the random number generator

  • attractors (Sequence[int | float]) – the attractor points

  • lb (int | float, default: -inf) – the lower bound

  • ub (int | float, default: inf) – the upper bound

  • is_int (bool, default: False) – shall we sample integer values?

Return type:

int | float

Returns:

the value

Raises:

ValueError – if the sampling failed

>>> from numpy.random import default_rng
>>> rg = default_rng(11)
>>> sample_from_attractors(rg, [5, 20])
15.198106552324713
>>> sample_from_attractors(rg, [2], lb=0, ub=10, is_int=True)
3
>>> sample_from_attractors(rg, [5, 20], lb=4)
4.7448464616061665
>>> sample_from_attractors(rg, [5, 20], ub=22)
1.044618552249311
>>> sample_from_attractors(rg, [5, 20], lb=0, ub=30, is_int=True)
6
>>> sample_from_attractors(rg, [5, 20], lb=4, ub=22, is_int=True)
20