niapy.util

Module with implementation of utility classes and functions.

exception niapy.util.FesException(message='Reached the allowed number of the function evaluations!!!')[source]

Bases: Exception

Exception for exceeding number of maximum function evaluations.

Author:

Klemen Berkovič

Date:

2018

License:

MIT

See also

Initialize the exception.

Parameters

message (Optional[str]) – Message show when this exception is thrown

__init__(message='Reached the allowed number of the function evaluations!!!')[source]

Initialize the exception.

Parameters

message (Optional[str]) – Message show when this exception is thrown

exception niapy.util.GenException(message='Reached the allowed number of the algorithm evaluations!!!')[source]

Bases: Exception

Exception for exceeding number of algorithm iterations/generations.

Author:

Klemen Berkovič

Date:

2018

License:

MIT

See also

Initialize the exception.

Parameters

message (Optional[str]) – Message that is shown when this exceptions is thrown

__init__(message='Reached the allowed number of the algorithm evaluations!!!')[source]

Initialize the exception.

Parameters

message (Optional[str]) – Message that is shown when this exceptions is thrown

exception niapy.util.RefException(message='Reached the reference point!!!')[source]

Bases: Exception

Exception for exceeding reference value of function/fitness value.

Author:

Klemen Berkovič

Date:

2018

License:

MIT

See also

Initialize the exception.

Parameters

message (Optional[str]) – Message that is show when this exception is thrown.

__init__(message='Reached the reference point!!!')[source]

Initialize the exception.

Parameters

message (Optional[str]) – Message that is show when this exception is thrown.

exception niapy.util.TimeException(message='Reached the allowed run time of the algorithm')[source]

Bases: Exception

Exception for exceeding time limit.

Author:

Klemen Berkovič

Date:

2018

License:

MIT

See also

Initialize the exception.

Parameters

message (Optional[str]) – Message that is show when this exception is thrown.

__init__(message='Reached the allowed run time of the algorithm')[source]

Initialize the exception.

Parameters

message (Optional[str]) – Message that is show when this exception is thrown.

niapy.util.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.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.get_argparser()[source]

Create/Make parser for parsing string.

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

    Name of algorithm to use. Default value is jDE.

  • -b or –benchmark (str):

    Name of benchmark to use. Default values is Benchmark.

  • -d or –dimension (int):

    Number of dimensions/components usd by benchmark. 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.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_argparser().

  • ArgumentParser.parse_args()

niapy.util.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.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.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.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.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.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.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.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