NiaPy.util

Module with implementation of utility classess and functions.

NiaPy.util.fullArray(a, D)[source]

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

Parameters:
  • a (Union[int, float, numpy.ndarray], Iterable[Any]) – Input values for fill.
  • D (int) – Length of new array.
Returns:

Array filled with passed values or value.

Return type:

numpy.ndarray

NiaPy.util.objects2array(objs)[source]

Convert Iterable array or list to NumPy array.

Parameters:objs (Iterable[Any]) – Array or list to convert.
Returns:Array of objects.
Return type:numpy.ndarray
NiaPy.util.limit_repair(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.
  • kwargs (Dict[str, Any]) – Additional arguments.
Returns:

Solution in search space.

Return type:

numpy.ndarray

NiaPy.util.limitInversRepair(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.
  • kwargs (Dict[str, Any]) – Additional arguments.
Returns:

Solution in search space.

Return type:

numpy.ndarray

NiaPy.util.wangRepair(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.
  • kwargs (Dict[str, Any]) – Additional arguments.
Returns:

Solution in search space.

Return type:

numpy.ndarray

NiaPy.util.randRepair(x, Lower, Upper, rnd=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.6.12/lib/python3.6/site-packages/numpy/random/__init__.py'>, **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.
  • rnd (mtrand.RandomState) – Random generator.
  • kwargs (Dict[str, Any]) – Additional arguments.
Returns:

Fixed solution.

Return type:

numpy.ndarray

NiaPy.util.reflectRepair(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.
  • kwargs (Dict[str, Any]) – Additional arguments.
Returns:

Fix solution.

Return type:

numpy.ndarray

NiaPy.util.MakeArgParser()[source]

Create/Make pareser for parsing string.

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

    Name of algorithm to use. Default value is jDE.

  • -b or –bech (str):

    Name of benchmark to use. Default values is Benchmark.

  • -D (int):

    Number of dimensions/components usd by benchmark. Default values is 10.

  • -nFES (int):

    Number of maximum funciton evaluations. Default values is inf.

  • -nGEN (int):

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

  • -NP (int):

    Number of inidividuals in population. Default values is 43.

  • -r or –runType (str);
    Run type of run. Value can be:
    • ‘’: No output durning the run. Ouput 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 ploted in mathplotlib. Graph represents convegance of algorithm over run time of algorithm.

    Default value is ‘’.

  • -seed (list of int or int):

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

  • -optType (str):
    Optimization type of the run. Values can be:
    • min: For minimaization 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.getArgs(av)[source]

Parse arguments form inputed string.

Parameters:av (str) – String 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.MakeArgParser().
  • ArgumentParser.parse_args()
NiaPy.util.getDictArgs(argv)[source]

Pasre input string.

Parameters:argv (str) – Input string to parse for argumets
Returns:Parsed input string
Return type:dict

See also

  • NiaPy.utils.getArgs()
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 allowd 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 allowd 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.TimeException(message='Reached the allowd 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 allowd run time of the algorithm')[source]

Initialize the exception.

Parameters:message (Optional[str]) – Message that is show when this exception 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.