metaheuristic_designer.benchmarks package

Submodules

metaheuristic_designer.benchmarks.benchmark_funcs module

class MaxOnes(dimension, mode='max', constraint_handler=None)[source]

Bases: ObjectiveFunc

objective(population_matrix)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class DiophantineEq(dimension, coeff, target, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class SleepTest(dimension, sleep_time=2, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Sphere(dimension, mode='min', constraint_handler=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class HighCondElliptic(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class BentCigar(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Discus(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Rosenbrock(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Ackley(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Weierstrass(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Griewank(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Rastrigin(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ModSchwefel(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Katsuura(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class HappyCat(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class HGBat(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ExpandedGriewankPlusRosenbrock(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ExpandedShafferF6(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class SumPowell(dimension, mode='min', lim_min=-1, lim_max=1)[source]

Bases: ObjectiveFunc

Sum of Powell function

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class N4XinSheYang(dimension, mode='min', lim_min=-10, lim_max=10)[source]

Bases: ObjectiveFunc

N4 Xin-She Yang function

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

metaheuristic_designer.benchmarks.classic_problems module

class ThreeSAT(clauses)[source]

Bases: ObjectiveFunc

This is the 3-SAT problem that consist in finding if a logical expression given in 3CNF (conjunctive normal form with 3 variables per clause) is satisfiable, in other words, if there is a combination of boolean variables that makes it true.

Format based on https://www.cs.ubc.ca/%7Ehoos/SATLIB/benchm.html

Parameters:

clauses (ndarray) –

A representation of the clauses that defines the logical expression, this will be a matrix of size (n,3), where each component is the index (starting at 1) of the variable in this clause, negation is represented as negative numbers.

For example, the expression (¬a ∨ b ∨ ¬c) ∧ (a ∨ ¬b ∨ d) is represented as [[-1, 2, -3], [1, -2, 4]]

static from_cnf_file(path)[source]
objective(solution)[source]

Calculates the percentage of clauses satisfied in the logical expression.

Parameters:

solution (ndarray) – A binary vector representing the value of each binary variable.

Returns:

The percentage of clauses satisfied with this assignment of variables.

Return type:

perc_satisfied

class BinKnapsack(cost, value, max_weight)[source]

Bases: ObjectiveFunc

This is the 0-1 Knapsack problem that consist in choosing from set of elements which have a certain cost and value to maximize the value without reaching a weight threshold.

Parameters:
  • cost (ndarray) – The cost associated to each of the elements.

  • value (ndarray) – The value associated to each of the elements.

  • max_weight (float) – The maximum weight.

objective(solution)[source]

Calculates the total value of the selection of elements. If the weight is higher than the maximum weight, the value is replaced by the negative weight of the elements.

Parameters:

solution (ndarray) – A binary vector deciding whether to choose or not each element.

Returns:

value – The total value of the objects.

Return type:

float

repair_solution(solution)[source]

Transforms an invalid vector into one that satisfies the restrictions of the problem.

Parameters:

solution (MatrixLike) – A solution that could be violating the restrictions of the problem.

Returns:

repaired_solution – A modified version of the solution passed that satisfies the restrictions of the problem.

Return type:

MatrixLike

class MaxClique(adjacency_matrix)[source]

Bases: ObjectiveFunc

This is the Maximum clique problem which consists on finding the size of the largest subgraph that has all its nodes interconnected (a clique).

Parameters:

adjacency_matrix (ndarray) – The adjacency matrix of the graph.

objective(solution)[source]
Parameters:

solution (ndarray) – A sequence of nodes that will be read from left to right, starting from the first node that creates a clique of size 0, checking if adding a new node from the sequence produces a clique of a larger size. If this is not the case the algorithm ends.

Returns:

clique_size – The size of the clique generated with this sequence

Return type:

int

class TSP(adjacency_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], name: str | None = None, mode: str = 'min')[source]

Bases: ObjectiveFunc

classmethod from_csv(problem_path: Path, name: str | None = None, mode: str = 'min')[source]

Constructs the objective function from a .csv file.

Parameters:
  • problem_path (Path) – Path to the .csv file containing the weights of each edge. The expected format of the file is a table with 3 columns: Edge 1, Edge 2, Weights

  • name (str, optional) – Name to use when showing the user which function is being optimized, by default None

  • mode (str, optional) – Optimization mode to use, by default “min”

Return type:

An object of type TSP (Objective function on vectors)

objective(solutions: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

metaheuristic_designer.benchmarks.img_funcs module

class ImgApprox(img_dim, reference, mode=None, img_name='', diff_func='MSE', name=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ImgEntropy(img_dim, nbins=10, mode=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ImgStd(img_dim, mode=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

Module contents

class MaxOnes(dimension, mode='max', constraint_handler=None)[source]

Bases: ObjectiveFunc

objective(population_matrix)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class DiophantineEq(dimension, coeff, target, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class SleepTest(dimension, sleep_time=2, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Sphere(dimension, mode='min', constraint_handler=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class HighCondElliptic(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class BentCigar(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Discus(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Rosenbrock(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Ackley(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Weierstrass(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Griewank(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Rastrigin(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ModSchwefel(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class Katsuura(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class HappyCat(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class HGBat(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ExpandedGriewankPlusRosenbrock(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ExpandedShafferF6(dimension, mode='min')[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class SumPowell(dimension, mode='min', lim_min=-1, lim_max=1)[source]

Bases: ObjectiveFunc

Sum of Powell function

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class N4XinSheYang(dimension, mode='min', lim_min=-10, lim_max=10)[source]

Bases: ObjectiveFunc

N4 Xin-She Yang function

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ImgApprox(img_dim, reference, mode=None, img_name='', diff_func='MSE', name=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ImgEntropy(img_dim, nbins=10, mode=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ImgStd(img_dim, mode=None)[source]

Bases: ObjectiveFunc

objective(solution)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class ThreeSAT(clauses)[source]

Bases: ObjectiveFunc

This is the 3-SAT problem that consist in finding if a logical expression given in 3CNF (conjunctive normal form with 3 variables per clause) is satisfiable, in other words, if there is a combination of boolean variables that makes it true.

Format based on https://www.cs.ubc.ca/%7Ehoos/SATLIB/benchm.html

Parameters:

clauses (ndarray) –

A representation of the clauses that defines the logical expression, this will be a matrix of size (n,3), where each component is the index (starting at 1) of the variable in this clause, negation is represented as negative numbers.

For example, the expression (¬a ∨ b ∨ ¬c) ∧ (a ∨ ¬b ∨ d) is represented as [[-1, 2, -3], [1, -2, 4]]

static from_cnf_file(path)[source]
objective(solution)[source]

Calculates the percentage of clauses satisfied in the logical expression.

Parameters:

solution (ndarray) – A binary vector representing the value of each binary variable.

Returns:

The percentage of clauses satisfied with this assignment of variables.

Return type:

perc_satisfied

class BinKnapsack(cost, value, max_weight)[source]

Bases: ObjectiveFunc

This is the 0-1 Knapsack problem that consist in choosing from set of elements which have a certain cost and value to maximize the value without reaching a weight threshold.

Parameters:
  • cost (ndarray) – The cost associated to each of the elements.

  • value (ndarray) – The value associated to each of the elements.

  • max_weight (float) – The maximum weight.

objective(solution)[source]

Calculates the total value of the selection of elements. If the weight is higher than the maximum weight, the value is replaced by the negative weight of the elements.

Parameters:

solution (ndarray) – A binary vector deciding whether to choose or not each element.

Returns:

value – The total value of the objects.

Return type:

float

repair_solution(solution)[source]

Transforms an invalid vector into one that satisfies the restrictions of the problem.

Parameters:

solution (MatrixLike) – A solution that could be violating the restrictions of the problem.

Returns:

repaired_solution – A modified version of the solution passed that satisfies the restrictions of the problem.

Return type:

MatrixLike

class MaxClique(adjacency_matrix)[source]

Bases: ObjectiveFunc

This is the Maximum clique problem which consists on finding the size of the largest subgraph that has all its nodes interconnected (a clique).

Parameters:

adjacency_matrix (ndarray) – The adjacency matrix of the graph.

objective(solution)[source]
Parameters:

solution (ndarray) – A sequence of nodes that will be read from left to right, starting from the first node that creates a clique of size 0, checking if adding a new node from the sequence produces a clique of a larger size. If this is not the case the algorithm ends.

Returns:

clique_size – The size of the clique generated with this sequence

Return type:

int

class TSP(adjacency_matrix: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], name: str | None = None, mode: str = 'min')[source]

Bases: ObjectiveFunc

classmethod from_csv(problem_path: Path, name: str | None = None, mode: str = 'min')[source]

Constructs the objective function from a .csv file.

Parameters:
  • problem_path (Path) – Path to the .csv file containing the weights of each edge. The expected format of the file is a table with 3 columns: Edge 1, Edge 2, Weights

  • name (str, optional) – Name to use when showing the user which function is being optimized, by default None

  • mode (str, optional) – Optimization mode to use, by default “min”

Return type:

An object of type TSP (Objective function on vectors)

objective(solutions: ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool]) ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

class IOHObjective(fid: int | str, dimension: int, instance: int = 1, problem_class: None = None, compact_name: bool = False)[source]

Bases: ObjectiveFunc

Adapts an IOH benchmark problem to the ObjectiveFunc interface.

Parameters:
  • fid (int or str) – BBOB function ID (1-24) or name (e.g. "Sphere").

  • dimension (int) – Problem dimensionality.

  • instance (int, optional) – Problem instance (default 1).

  • problem_class (ProblemClass, optional) – IOH problem type (default ProblemClass.BBOB).

  • ioh_options (dict, optional) – Extra keyword arguments passed to ioh.get_problem.

  • compact_name (str, optional) – Use a shortened name for the benchmark when compact_name is True.

objective(x)[source]

Implementation of the objective function.

Parameters:

solution (Any) – The solution for which the fitness will be calculated.

Returns:

objective_value – Value of the objective function given a solution.

Return type:

VectorLike | ScalarLike

attach_logger(logger)[source]
detach_logger()[source]
restart()[source]

Reset the evaluation counter to zero.

class BBOBObjective(fid, dimension, instance, compact_name=None)[source]

Bases: IOHObjective