niapy

niapy.runner

Implementation of Runner utility class.

class niapy.runner.Runner(dimension=10, max_evals=1000000, runs=1, algorithms='ArtificialBeeColonyAlgorithm', problems='Ackley')[source]

Bases: object

Runner utility feature.

Feature which enables running multiple algorithms with multiple problems. It also support exporting results in various formats (e.g. Pandas DataFrame, JSON, Excel)

Variables
  • dimension (int) – Dimension of problem

  • max_evals (int) – Number of function evaluations

  • runs (int) – Number of repetitions

  • algorithms (Union[List[str], List[Algorithm]]) – List of algorithms to run

  • problems (List[Union[str, Problem]]) – List of problems to run

Initialize Runner.

Parameters
  • dimension (int) – Dimension of problem

  • max_evals (int) – Number of function evaluations

  • runs (int) – Number of repetitions

  • algorithms (List[Algorithm]) – List of algorithms to run

  • problems (List[Union[str, Problem]]) – List of problems to run

__init__(dimension=10, max_evals=1000000, runs=1, algorithms='ArtificialBeeColonyAlgorithm', problems='Ackley')[source]

Initialize Runner.

Parameters
  • dimension (int) – Dimension of problem

  • max_evals (int) – Number of function evaluations

  • runs (int) – Number of repetitions

  • algorithms (List[Algorithm]) – List of algorithms to run

  • problems (List[Union[str, Problem]]) – List of problems to run

run(export='dataframe', verbose=False)[source]

Execute runner.

Parameters
  • export (str) – Takes export type (e.g. dataframe, json, xls, xlsx) (default: “dataframe”)

  • verbose (bool) – Switch for verbose logging (default: {False})

Returns

Returns dictionary of results

Return type

dict

Raises

TypeError – Raises TypeError if export type is not supported

task_factory(name)[source]

Create optimization task.

Parameters

name (str) – Problem name.

Returns

Optimization task to use.

Return type

Task

niapy.task

The implementation of tasks.

class niapy.task.OptimizationType(value)[source]

Bases: enum.Enum

Enum representing type of optimization.

Variables
  • MINIMIZATION (int) – Represents minimization problems and is default optimization type of all algorithms.

  • MAXIMIZATION (int) – Represents maximization problems.

MAXIMIZATION = -1.0
MINIMIZATION = 1.0
class niapy.task.Task(problem=None, dimension=None, lower=None, upper=None, optimization_type=OptimizationType.MINIMIZATION, repair_function=<function limit>, max_evals=inf, max_iters=inf, cutoff_value=None, enable_logging=False)[source]

Bases: object

Class representing an optimization task.

Date:

2019

Author:

Klemen Berkovič and others

Variables
  • problem (Problem) – Optimization problem.

  • dimension (int) – Dimension of the problem.

  • lower (numpy.ndarray) – Lower bounds of the problem.

  • upper (numpy.ndarray) – Upper bounds of the problem.

  • range (numpy.ndarray) – Search range between upper and lower limits.

  • optimization_type (OptimizationType) – Optimization type to use.

  • iters (int) – Number of algorithm iterations/generations.

  • evals (int) – Number of function evaluations.

  • max_iters (int) – Maximum number of algorithm iterations/generations.

  • max_evals (int) – Maximum number of function evaluations.

  • cutoff_value (float) – Reference function/fitness values to reach in optimization.

  • x (numpy.ndarray) – Best found individual.

  • x_f (float) – Best found individual function/fitness value.

Initialize task class for optimization.

Parameters
  • problem (Union[str, Problem]) – Optimization problem.

  • dimension (Optional[int]) – Dimension of the problem. Will be ignored if problem is instance of the Problem class.

  • lower (Optional[Union[float, Iterable[float]]]) – Lower bounds of the problem. Will be ignored if problem is instance of the Problem class.

  • upper (Optional[Union[float, Iterable[float]]]) – Upper bounds of the problem. Will be ignored if problem is instance of the Problem class.

  • optimization_type (Optional[OptimizationType]) – Set the type of optimization. Default is minimization.

  • repair_function (Optional[Callable[[numpy.ndarray, numpy.ndarray, numpy.ndarray, Dict[str, Any]], numpy.ndarray]]) – Function for repairing individuals components to desired limits.

  • max_evals (Optional[int]) – Number of function evaluations.

  • max_iters (Optional[int]) – Number of generations or iterations.

  • cutoff_value (Optional[float]) – Reference value of function/fitness function.

  • enable_logging (Optional[bool]) – Enable/disable logging of improvements.

__init__(problem=None, dimension=None, lower=None, upper=None, optimization_type=OptimizationType.MINIMIZATION, repair_function=<function limit>, max_evals=inf, max_iters=inf, cutoff_value=None, enable_logging=False)[source]

Initialize task class for optimization.

Parameters
  • problem (Union[str, Problem]) – Optimization problem.

  • dimension (Optional[int]) – Dimension of the problem. Will be ignored if problem is instance of the Problem class.

  • lower (Optional[Union[float, Iterable[float]]]) – Lower bounds of the problem. Will be ignored if problem is instance of the Problem class.

  • upper (Optional[Union[float, Iterable[float]]]) – Upper bounds of the problem. Will be ignored if problem is instance of the Problem class.

  • optimization_type (Optional[OptimizationType]) – Set the type of optimization. Default is minimization.

  • repair_function (Optional[Callable[[numpy.ndarray, numpy.ndarray, numpy.ndarray, Dict[str, Any]], numpy.ndarray]]) – Function for repairing individuals components to desired limits.

  • max_evals (Optional[int]) – Number of function evaluations.

  • max_iters (Optional[int]) – Number of generations or iterations.

  • cutoff_value (Optional[float]) – Reference value of function/fitness function.

  • enable_logging (Optional[bool]) – Enable/disable logging of improvements.

eval(x)[source]

Evaluate the solution A.

Parameters

x (numpy.ndarray) – Solution to evaluate.

Returns

Fitness/function values of solution.

Return type

float

is_feasible(x)[source]

Check if the solution is feasible.

Parameters

x (Union[numpy.ndarray, Individual]) – Solution to check for feasibility.

Returns

True if solution is in feasible space else False.

Return type

bool

next_iter()[source]

Increments the number of algorithm iterations.

plot()[source]

Plot a simple convergence graph.

repair(x, rng=None)[source]

Repair solution and put the solution in the random position inside of the bounds of problem.

Parameters
  • x (numpy.ndarray) – Solution to check and repair if needed.

  • rng (Optional[numpy.random.Generator]) – Random number generator.

Returns

Fixed solution.

Return type

numpy.ndarray

return_conv()[source]

Get values of x and y axis for plotting covariance graph.

Returns

  1. List of ints of function evaluations.

  2. List of ints of function/fitness values.

Return type

Tuple[List[int], List[float]]

stopping_condition()[source]

Check if optimization task should stop.

Returns

True if number of function evaluations or number of algorithm iterations/generations or reference values is reach else False.

Return type

bool

stopping_condition_iter()[source]

Check if stopping condition reached and increase number of iterations.

Returns

True if number of function evaluations or number of algorithm iterations/generations or reference values is reach else False.

Return type

bool