Package: benchmark-functions
Benchmark Functions: a Python CollectionA benchmark functions collection written in Python 3.X, suited for assessing the performances of optimisation problems on deterministic functions. Most functions here implemented can be created in an arbitrary number of dimensions (i.e. $R^N\to R$). Suggested boundaries, as well the values of known minima/maxima, are also provided. Finally, every function can be visualised with an interactive widget.InstallationThis module is available onpipand can be installed as follows:$pip3installbenchmark_functionsUsageTo use a function from the collection it is sufficient to instantiate the relative class from the library:importbenchmark_functionsasbffunc=bf.Schwefel(n_dimensions=4)Most functions impelmented can be instantiated with an arbitrary number of dimensions. This can be set with an_dimensionsoptional parameter. If the numer of dimensions are not specified a default value (generally $N=2$) will be used.
Some functions require other specific parameters (e.g. Ackley), these can be set in the constructor, otherwise default values will be taken.
Some functions are only defined for 2 dimensions (e.g. Easom) in these cases non_dimensionsparameter is accepted.Calling directly the instantiated function on a point will provide the function's value:point=[25,-34.6,-112.231,242]func(point)# results in -129.38197657025287The call will perform some internal sanity checks on the passed point, like its dimensionality and type. If you are reasonably sure about the values of your points and want to improve the computational performances, you can pass thevalidate=Falseflag when calling the function.Normally, these functions are used as a minimisation problem, so they are designed accordingly.
An optional flagoppositecan be passed in any function constructor.
If set toTruethe value of the function will be the opposite at each call. The values of theminima/umandmaxima/umfunctions (see below) are modified accordingly.
This is meant to streamline the  use of a maximisation algorithm on these functions.Convenience FunctionsA set of convenience functions are also implemented in the class, namely:namethe name of the function;minima/maximareturns a list ofOptimumobjects of the known global minima/maxima. If any value is unknown, aNonevalue will be present instead;minimum/maximumreturns a singleOptimumof the known global minimum/maximum. If any value is unknown, aNonevalue will be present instead;suggested_boundsreturns a tuple of two elements (LB,UB) each one is a list ofn_dimensionselements, representing the suggested search boundary of the function;showplot the function in an interactive graphic widget. Read the relative section below for more information on this feature;As an example, the following code:print(func.suggested_bounds())will produce([-500.0, -500.0, -500.0, -500.0], [500.0, 500.0, 500.0, 500.0])for the Schwefel function.Known minima/maximaThe minima returned are the ones known and generally considered relevant for the function. In most cases, you should expect to always find included in the list at least the global minimum (if it is known) along some extra local minima that can be useful in assessing optimisation results. Examples are the minima present in the De Jong 5 and Michalewicz functions. In the same fashion, interesting known local maxima are also available.Optimumis a class that contains the following attributes:positiona list with the coordinates;scorethe value of the optimum in the function;typethat is one of: 'Minimum', 'Maximum' or 'Saddle';region_typethe type of region the optimum is located. It can be one of: 'Convex', 'Concave', 'Plateau', 'Saddle' or 'Unknown';Generally a function global minimum/maximum can change with the number of dimensions. For this reason someminima/maximavalues may be missing or inaccurate. If you find a better global optimum please open an issue about that with the coordinates and I'll update the library (see the relevant sections below).Baseline Search TechniquesTwo simple search techniques are also provided out-of-the-box and are available for every function:minimum_random_searchperforms a random search and returns the local minimum point as tuple(point, score)within the boundaries provided by the parameterbounds. If several minima points with the same score are found (e.g. the local minimum is in a plateau) a list of points will be provided instead. Then_samplesparameter (set to $10^7$ by default) specifies the number of samplings performed.minimum_grid_searchperforms a random search and returns the local minimum point as tuple(point, score)within the boundaries provided by the parameterbounds. If several minima points with the same score are found (e.g. the local minimum is in a plateau) a list of points will be provided instead. The optional parametern_edge_points(set to 100 by default) defines the number of points of the grid "edge", meaning that the actual number of points assessed are $(n_edge_points+1)^N$. This function is lightweight in terms of memory, since the grid is created and iterated in place, however it can require a lot of computational time due to the big number of function's evaluations.These techniques are not efficient nor effective, and they are provided only as potential baseline for comparing intelligent optimisation techniques.
For an example of optimisation with theBees Algorithmplease refer tothisandthissnippets.Visualise a functionUsing theshowfunction will plot the benchmark function in an interactive widget.
This can be done only if then_dimensionsis lower than 3. The resulting plot is either a 3D surface (whenn_dimensions=2) or a simple 2D graph plot (n_dimensions=1). If the function is defined in 2 dimensions, it is also possible to plot it as an heatmap setting the function parameterasHeatMap=Trueas follows:func.show(asHeatMap=True)By default, the function will be shown according to the suggested boundaries. It is possible to pass custom boundaries for visualisation purpose using the parameterbounds.
The curve/surface is interpolated according to a number of points uniformly sampled within the considered boundaries. The number of $N\times N$ points can be tuned passing the $N$ value to the parameterresolution(by default $N=50$).A list of points can be optionally plotted along the main function plot, assigning it to the parametershowPoints. For instance, the following call will display the function along all the known local minimima:func.show(showPoints=func.minima())Note: whilst importing and using the library require nothing more than thenumpypython library, in order to visualise the functions thematplotliblibrary is also required.List of Available Functions and Expandability FeaturesFor a list of available functions, instructions to expand the library and other information please refer to theproject homepage.Author and LicenseThis library is developed and maintained by Luca Baronti (gmailaddress:lbaronti) and released underGPL v3 license.Versions Historyv1.1.3fixed Schwefel function definitionv1.1.2Added another functionMade the abstract BenchmarkFunction public by removing the prepended underscore to make extension more streamlinedv1.1.1Fixed an import bugUpdated the READMEv1.1Updated the READMESplit functions_info.json file into several files in a directory with the same name; removed functions_info.json and changed the relative codeAdded function to validate a candidate local minimumAdded optional custom boundaries in the show functionAdded simple minima grid search for all the functionsAdded simple minima random search for all the functionsAdded and verified local minima of De Jong 5 and De Jong 3Refractored API (most getter functions now have a simpler form)Refractored the JSON schema for the functions meta-infoAdded FunctionInfoWriter to facilitate the addition of newfound optimaShow function now optionally accepts a list of points to show on the plotChanged heatmap colour to viridis for consistency reasonsAdded version to the functions infoAdded CI/CD directivesv0.1.2Minor fixes