EmiR: Evolutionary minimization for R

Classical minimization methods, like the steepest descent or quasi-Newton techniques, have been proved to struggle in dealing with optimization problems with a high-dimensional search space or subject to complex nonlinear constraints. In the last decade, the interest on metaheuristic nature-inspired algorithms has been growing steadily, due to their flexibility and effectiveness. In this paper we present EmiR, a package for R which implements several metaheuristic algorithms for optimization problems. Unlike other available tools, EmiR can be used not only for unconstrained problems, but also for problems subjected to inequality constraints and for integer or mixed-integer problems. Main features of EmiR, its usage and the comparison with other available tools are presented.


Introduction
Classical optimization techniques, as the steepest descent algorithm and quasi-Newton techniques, are based on the use of differential calculus in locating the optimum solution. Although still widely used in several research areas, these algorithms have been proved to struggle in dealing with optimization problems with a high-dimensional search space or subject to complex nonlinear constraints [1]. In the last decade, the interest on metaheuristic algorithms -also referred to as evolutionary algorithms [2] -has been growing steadily. The term metaheuristic, proposed by Glover [3] in 1986, is composed by the words heuristic, which used to denote algorithms with stochastic components in the past, and meta, which means ''beyond'' or ''higher level''. Metaheuristic algorithms are therefore higherlevel heuristic algorithms, in the sense they are more general in problem-solving [4] and, nowadays, are typically based on a metaphor of natural or man-made processes, like the search for food or haunting of nearly any species of animals [5]. Most of these methods evolve an initial population of individuals, each of them representing a candidate solution to the problem, toward
A detailed description of these algorithms is beyond the scope of this work. However, it is important to highlight that for algorithms where multiple approaches have been proposed in literature, such as SA, PS and GA, we opted for most recent ones, as described in the following. For SA we implemented the populationbased version of the algorithm proposed in 2016 by Askarzadeh et al. [23]. For PS we opted for the general approach with adaptive parameters, inertia on particles and constraints on their maximum velocity [20]. Finally, for GA we decided to implement the version described by Haupt [25], characterized by high efficiency mechanisms of selection and mating. The package provides a single interface to all the available algorithms, by means of the function: minimize(algorithm_id, obj_func, parameters, config, constraints = NULL, ...) whose block diagram is represented in Fig. 1. minimize accepts the following arguments: • algorithm_id -the identification code of the algorithm to be used; • obj_func -the objective function to be minimized/maximized; • parameters -the list of parameters the objective function is minimized/maximized with respect to; • config -the configuration parameters of the algorithm; • constraints -the (optional) list of constraints the objective function is subjected to; • ... -additional (optional) parameters.
The choice of the algorithm to be used is performed by passing its corresponding ID (an object of class character) to the argument algorithm_id. A data.frame object with the list of available algorithms in EmiR, including their IDs and configuration  functions,  can  be  obtained  with  the  function list_of_algorithms.
The objective function to be minimized or maximized has to be specified by means of the argument obj_func. Valid functions have a single vector argument for all the independent variables, as in the following example.
x3 <-x [3] value <-x1^2 + x2^2 + x3^2 return(value) } Depending on the dimension of the objective function, one or more objects of class Parameter have to be defined, one for each independent variable the function depends on. This S4 class is necessary to store range, name and type (integer of continue) for each variable. The class Parameter is not exposed to the user, who instead uses the function parameter, which returns an instance of it. Arguments of function parameter are reported below.

parameter(name, min_val, max_val, integer = FALSE)
With name the user specifies the name of the parameter, with min_val and max_val its range, and with integer whether the variable is integer or continue. As an objective function can depend on multiple variables, a list of Parameter objects has to passed to the argument parameters of the minimize function. When dealing with high-dimensional functions, it could be tedious to create a new Parameter object for each variable one by one. For this reason, EmiR also implements the function parameters, which accepts a matrix as input and returns a list of objects of Parameter.
All algorithms share some common configuration parameters, in addition to those which are algorithm-specific. For this reason, a S4 class for each algorithm has been created to deal with all these different tuning variables. Those classes are not exposed to the user, who instead creates instances of them, by using the configuration functions associated to each algorithm, whose names are reported in the column Configuration function of the list_of_algorithms function's output. Only arguments iterations and population_size do not have default values and have to be specified.
EmiR can also be used for constrained optimization problems, as long as the objective function is subjected to inequality constraints. The S4 class Constraint has been designed to deal with constraints, but it is not exposed to the user, who instead can define a constraint by using the function constraint(func, inequality), where arguments func and inequality expect respectively an object of class function and one of class character with the inequality type (">", ">=", "<=", "<"). The function passed to func has to represent the first term of an inequality with zero. Valid functions depend on one or more of the independent variables of the objective function, and have a single vector argument, like in the example below.
There are also additional parameters and options that can be specified in the function minimize. Just to name a few, it is possible to choose how to handle out-of-boundary solutions (oob_solutions), and to select the approach to use in the constrained optimization (constrained_method).
Other functions in EmiR, mainly related to the graphical presentation of results, will be directly introduced in the following examples.

Examples of unconstrained optimization problems
In this section, few examples on how to use the function minimize for unconstrained optimization problems are presented.

EmiR Minimization Results
It is possible to plot the best function value (best cost) as a function of the iteration using the following function.

Examples of constrained optimization problems
Because of their strong heuristic component, the use of evolutionary algorithms for constrained problems was very limited in the past [29]. However, for both linearly and non-linearly constrained problems with inequality constraints, evolutionary algorithms have proved to be a promising option. EmiR offers three approaches to deal with constrained problems: • penalty method -the constrained problem is converted to an unconstrained one, by adding a term, called penalty function, to the objective function. The penalty function consists of a penalty parameter multiplied by a measure of the violation of the constraints. The penalty parameter is increased at each iteration; • barrier method -the value of the objective function is set equal to an arbitrary large positive (or negative in case of maximization) number if any of the constraints is violated; • acceptance-rejection method -all solutions at each generation are checked for possible violation of the constraints: if so, they are replaced by new randomly generated solutions in the feasible region (FR).
For all previous methods, the user can choose to generate the initial population either in the feasible region of the problem, or in the full range of its parameters.
In this section, two examples on how to use the function minimize for constrained optimization problems are presented.

Example C1: nonlinear programming
Let us consider the following example from [30]: in the interval x ∈ [13, 100] and y ∈ [0, 100]. The coloured contour plot of the objective function, as well as the curves g 1 (x, y) = 0 and g 2 (x, y) = 0, and the FR are shown in Fig. 5 (left). The problem has the following global minimum: f (14.09500, 0.84296) = −6961.81474448783, which is located within a very narrow region of the FR, as shown in Fig. 5 (right).
The following code shows how to set up this constrained optimization problem in EmiR, using the Particle Swarm algorithm.

Comparison with metaheuristicOpt
R already offers a plethora of packages implementing metaheuristic algorithms for optimization problems. Among them, metaheuristicOpt [32], with its collection of 21 algorithms, is the most complete and popular one. Because of the clear overlap with EmiR, in this section we outline the differences between the two packages and compare their performance. Table 1 compares some of the main features offered by the two packages.
EmiR not only comes in handy to overcome the main limitation of metaheuristicOpt, that is it can only be used for continuous unconstrained optimization problems, but its faster execution times makes it appealing also in those cases where metaheuris-ticOpt can be used.
We compared both performance and execution time of the two packages on seven unconstrained problems, using four different algorithms: WOA, MFO, GWO and CS. The choice of these algorithms relies on the fact that the first three do not have specific configuration parameters and the fourth is implemented in a similar way in both packages, making possible to test EmiR and meta-heuristicOpt in the same conditions. Each test was performed 200 times on a six-core Intel Core i7-8750H @2.20 GHz PC, running R v4.1.0 and the latest available versions (at the time of writing) of the two packages: EmiR v1.0.1 and metaheuristicOpt v2.0.0. Table 2 summarizes the results of all tests, by reporting mean (µ) and standard deviation (σ ) of both execution time and best cost of the objective function, as obtained from both packages.
Values in red, in columns µ cost and µ time , indicate they are better than the corresponding ones from the other package. In basically all cases, EmiR performed better (sometimes much better) than metaheuristicOpt in both execution time and best cost.

Impact
EmiR offers a complete solution in R for solving constrained and unconstrained optimization problems. The package has been designed to be highly time effective and user-friendly, sharing a common interface to all the available algorithms implemented. Moreover, EmiR also offers a high-level of customization, as well as a set of graphical functions for the visualization of the algorithm evolution during the optimization process. From the comparison with metaheuristicOpt, which is another package for R with a large collection of metaheuristic algorithms for optimization problems, EmiR stands out for the following features: • core code written in C++ for speed; • possibility to handle constrained optimization problems; • possibility to handle integer and mixed-integer optimization problems; • different options to handle out-of-boundary solutions; • possibility to import the initial state of the population; • tools for plots and animations.
Although a comprehensive comparison between metaheuristic and classical optimization algorithms goes beyond the scope of this work, we also compared the performance of EmiR to some of the most used classical optimization techniques implemented in R: BFGS-method [25] (BFGS), Conjugate Gradient method (CG) [33] and Nelder Mead method (NM) [34]. Table 3 shows the average best cost (from 100 independent tests), for 5 benchmark functions, 1 as obtained from BFGS, CG and NM and three metaheuristic algorithms from EmiR: GA, WOA and ABC. To make the comparison fair, the population size and the number of iterations for each metaheuristic algorithm in this test have been chosen to target a computational time of O(1) second. The results clearly show that metaheuristic algorithms outperformed traditional approaches for the selected objective functions.

Conclusions
In this work we presented EmiR, a new package for R implementing several population-based metaheuristic algorithms for optimization problems. We started introducing its architecture, Table 3 Performance comparison between traditional (BFGS, CG, NM) and metaheuristic (GA, WOA, BAT) algorithms tested with five benchmark functions, as described in the text. The first column reports name and domain of the benchmark functions. based on a common interface to all algorithms, and then we showed its usage and performance by means of several representative examples of unconstrained and constrained optimization problems, with both continuous and integer variables. We also gave an overview of some of graphical tools available in EmiR: from the graph of the best cost as function of the iteration, to the production of animated gif of the population motion. In the second part of this work, we focused on comparing the performance of EmiR with metaheuristicOpt (another package with metaheuristic algorithms for optimization problems), as well as with classical optimization methods. Tests were performed using challenging benchmark functions, with many local minima and/or high dimensionality, and results have proven the effectiveness of EmiR in terms of computational time and convergence to the global minimum. Future version of EmiR will include the addition of new algorithms and the extension of the current graphical tools to high dimensionality functions.