niapy.util

niapy.util.argparser

Argparser class.

niapy.util.argparser._get_problem_names()[source]

Get problem names.

niapy.util.argparser._optimization_type(x)[source]

Get OptimizationType from string.

Parameters

x (str) – String representing optimization type.

Returns

Optimization type based on type that is defined as enum.

Return type

OptimizationType

niapy.util.argparser.get_argparser()[source]

Create/Make parser for parsing string.

Parser:
  • -a or –algorithm (str):

    Name of algorithm to use. Default value is jDE.

  • -p or –problem (str):

    Name of problem to use. Default values is Ackley.

  • -d or –dimension (int):

    Number of dimensions/components used by problem. Default values is 10.

  • –max-evals (int):

    Number of maximum function evaluations. Default values is inf.

  • –max-iters (int):

    Number of maximum algorithm iterations/generations. Default values is inf.

  • -n or –population-size (int):

    Number of individuals in population. Default values is 43.

  • -r or –run-type (str);
    Run type of run. Value can be:
    • ‘’: No output during the run. Output is shown only at the end of algorithm run.

    • log: Output is shown every time new global best solution is found

    • plot: Output is shown only at the end of run. Output is shown as graph plotted in matplotlib. Graph represents convergence of algorithm over run time of algorithm.

    Default value is ‘’.

  • –seed (list of int or int):

    Set the starting seed of algorithm run. If multiple runs, user can provide list of ints, where each int usd use at new run. Default values is None.

  • –opt-type (str):
    Optimization type of the run. Values can be:
    • min: For minimization problems

    • max: For maximization problems

    Default value is min.

Returns

Parser for parsing arguments from string.

Return type

ArgumentParser

See also

  • ArgumentParser

  • ArgumentParser.add_argument()

niapy.util.argparser.get_args(argv)[source]

Parse arguments form input string.

Parameters

argv (List[str]) – List to parse.

Returns

Where key represents argument name and values it’s value.

Return type

Dict[str, Union[float, int, str, OptimizationType]]

See also

niapy.util.argparser.get_args_dict(argv)[source]

Parse input string.

Parameters

argv (List[str]) – Input string to parse for arguments

Returns

Parsed input string

Return type

dict

See also

  • niapy.utils.get_args()

niapy.util.array

niapy.util.array.full_array(a, dimension)[source]

Fill or create array of length dimension, from value or value form a.

Parameters
  • a (Union[int, float, numpy.ndarray, Iterable[Any]]) – Input values for fill.

  • dimension (int) – Length of new array.

Returns

Array filled with passed values or value.

Return type

numpy.ndarray

niapy.util.array.objects_to_array(objs)[source]

Convert Iterable array or list to NumPy array with dtype object.

Parameters

objs (Iterable[Any]) – Array or list to convert.

Returns

Array of objects.

Return type

numpy.ndarray

niapy.util.distances

niapy.util.distances.euclidean(u, v)[source]

Compute the euclidean distance between two numpy arrays.

Parameters
  • u (numpy.ndarray) – Input array.

  • v (numpy.ndarray) – Input array.

Returns

Euclidean distance between u and v.

Return type

float

niapy.util.factory

Factory functions for getting algorithms and problems by name.

niapy.util.factory.get_algorithm(name, *args, **kwargs)[source]

Get algorithm by name.

Parameters

name (str) – Name of the algorithm.

Returns

An instance of the algorithm instantiated *args and **kwargs.

Return type

Algorithm

Raises

KeyError – If an invalid name is provided.

niapy.util.factory.get_problem(name, *args, **kwargs)[source]

Get problem by name.

Parameters

name (str) – Name of the problem.

Returns

An instance of Problem, instantiated with *args and **kwargs.

Return type

Problem

Raises

KeyError – If an invalid name is provided.

niapy.util.random

niapy.util.random.levy_flight(rng, alpha=0.01, beta=1.5, size=None)[source]

Compute levy flight.

Parameters
  • alpha (float) – Scaling factor.

  • beta (float) – Stability parameter in range (0, 2).

  • (Optional[Union[int (size) – Output size.

  • Iterable[int]]] – Output size.

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

Returns

Sample(s) from a truncated levy distribution.

Return type

Union[float, numpy.ndarray]

niapy.util.repair

niapy.util.repair.limit(x, lower, upper, **_kwargs)[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.

  • lower (numpy.ndarray) – Lower bounds of search space.

  • upper (numpy.ndarray) – Upper bounds of search space.

Returns

Solution in search space.

Return type

numpy.ndarray

niapy.util.repair.limit_inverse(x, lower, upper, **_kwargs)[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.

  • lower (numpy.ndarray) – Lower bounds of search space.

  • upper (numpy.ndarray) – Upper bounds of search space.

Returns

Solution in search space.

Return type

numpy.ndarray

niapy.util.repair.rand(x, lower, upper, rng=None, **_kwargs)[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.

  • lower (numpy.ndarray) – Lower bounds of search space.

  • upper (numpy.ndarray) – Upper bounds of search space.

  • rng (numpy.random.Generator) – Random generator.

Returns

Fixed solution.

Return type

numpy.ndarray

niapy.util.repair.reflect(x, lower, upper, **_kwargs)[source]

Repair solution and put the solution in search space with reflection of how much the solution violates a bound.

Parameters
  • x (numpy.ndarray) – Solution to be fixed.

  • lower (numpy.ndarray) – Lower bounds of search space.

  • upper (numpy.ndarray) – Upper bounds of search space.

Returns

Fix solution.

Return type

numpy.ndarray

niapy.util.repair.wang(x, lower, upper, **_kwargs)[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.

  • lower (numpy.ndarray) – Lower bounds of search space.

  • upper (numpy.ndarray) – Upper bounds of search space.

Returns

Solution in search space.

Return type

numpy.ndarray