Examples

This package provides different tools for optimization. Hence, this section gives different examples for using the implemented Metaheuristics.

Single-Objective Optimization

julia> using Metaheuristics
julia> f(x) = 10length(x) + sum( x.^2 - 10cos.(2π*x) ) # objective functionf (generic function with 1 method)
julia> bounds = [-5ones(10) 5ones(10)]' # limits/bounds2×10 LinearAlgebra.Adjoint{Float64,Array{Float64,2}}: -5.0 -5.0 -5.0 -5.0 -5.0 -5.0 -5.0 -5.0 -5.0 -5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0
julia> information = Information(f_optimum = 0.0); # information on the minimization problem
julia> options = Options(f_calls_limit = 9000*10, f_tol = 1e-5); # generic settings
julia> algorithm = ECA(information = information, options = options) # metaheuristic used to optimizeECA(η_max=2.0, K=7, N=0, N_init=0, p_exploit=0.95, p_bin=0.02, p_cr=Float64[], ε=0.0, adaptive=false, resize_population=false)
julia> result = optimize(f, bounds, algorithm) # start the minimization proccess+=========== RESULT ==========+ iteration: 1286 minimum: 0.994959 minimizer: [2.022792658636484e-9, 1.0408459806107786e-9, 5.221905631841421e-9, -0.9949586395178999, -2.6757897934052962e-9, -1.8134268044865381e-9, -2.5629136943485616e-9, -2.0367253987841657e-10, -6.489216592784661e-9, -1.3129013294632502e-9] f calls: 90020 total time: 2.5556 s stop reason: Maximum objective function calls exceeded. +============================+
julia> minimum(result)0.9949590570932969
julia> minimizer(result)10-element Array{Float64,1}: 2.022792658636484e-9 1.0408459806107786e-9 5.221905631841421e-9 -0.9949586395178999 -2.6757897934052962e-9 -1.8134268044865381e-9 -2.5629136943485616e-9 -2.0367253987841657e-10 -6.489216592784661e-9 -1.3129013294632502e-9
julia> result = optimize(f, bounds, algorithm) # note that second run is faster+=========== RESULT ==========+ iteration: 1286 minimum: 0.994959 minimizer: [2.022792658636484e-9, 1.0408459806107786e-9, 5.221905631841421e-9, -0.9949586395178999, -2.6757897934052962e-9, -1.8134268044865381e-9, -2.5629136943485616e-9, -2.0367253987841657e-10, -6.489216592784661e-9, -1.3129013294632502e-9] f calls: 89950 total time: 0.4356 s stop reason: Maximum number of iterations exceeded. +============================+

Constrained Optimization

It is common that optimization models include constraints that must be satisfied for example: The Rosenbrock function constrained to a disk

Minimize:

\[{\displaystyle f(x,y)=(1-x)^{2}+100(y-x^{2})^{2}}\]

subject to:

\[{\displaystyle x^{2}+y^{2}\leq 2}\]

where $-2 \leq x,y \leq 2$.

In Metaheuristics.jl, a feasible solution is such that $g(x) \leq 0$ and $h(x) \approx 0$. Hence, in this example the constraint is given by $g(x) = x^2 + y^2 - 2 \leq 0$. Moreover, the equality and inequality constraints must be saved into Arrays.

Constraints handling

In this package, if the algorithm was not designed for constrained optimization, then solutions with lower constraint violation sum will be preferred.

julia> using Metaheuristics
julia> function f(x) x,y = x[1], x[2] fx = (1-x)^2+100(y-x^2)^2 gx = [x^2 + y^2 - 2] # inequality constraints hx = [0.0] # equality constraints # order is important return fx, gx, hx endf (generic function with 1 method)
julia> bounds = [-2.0 -2; 2 2]2×2 Array{Float64,2}: -2.0 -2.0 2.0 2.0
julia> optimize(f, bounds, ECA(N=30, K=3))+=========== RESULT ==========+ iteration: 43 minimum: 3.25061e-08 minimizer: [0.9998503621472485, 0.9996906895267705] f calls: 1290 feasibles: 30 / 30 in final population total time: 2.0757 s stop reason: Small difference of objective function values. +============================+

Multiobjective Optimization

To implement a multiobjective optimization problem and solve it, you can proceed as usual. Here, you need to provide constraints if they exist, otherwise put gx = [0.0]; hx = [0.0]; to indicate an unconstrained multiobjective problem.

julia> using Metaheuristics
julia> function f(x) # objective functions v = 1.0 + sum(x .^ 2) fx1 = x[1] * v fx2 = (1 - sqrt(x[1])) * v fx = [fx1, fx2] # constraints gx = [0.0] # inequality constraints hx = [0.0] # equality constraints # order is important return fx, gx, hx endf (generic function with 1 method)
julia> bounds = [zeros(30) ones(30)]';
julia> optimize(f, bounds, NSGA2())+=========== RESULT ==========+ iteration: 500 population: ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀F space⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ┌────────────────────────────────────────┐ 1 │⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⣅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ f₂ │⠀⠀⠀⠈⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠙⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠈⠢⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠑⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠤⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠢⠤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠐⠢⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 0 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠐⢤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └────────────────────────────────────────┘ ⠀0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀3⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀f₁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ non-dominated solution(s): ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀F space⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ┌────────────────────────────────────────┐ 1 │⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⣅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ f₂ │⠀⠀⠀⠈⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠙⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠈⠢⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠑⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠤⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠢⠤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠐⠢⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 0 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠐⢤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ └────────────────────────────────────────┘ ⠀0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀3⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀f₁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ f calls: 50000 feasibles: 100 / 100 in final population total time: 6.5004 s stop reason: Maximum number of iterations exceeded. +============================+

Bilevel Optimization

Bilevel optimization problems can be solved by using the package BilevelHeuristics.jl which extends Metaheuristics.jl for handling those hierarchical problems.

Defining objective functions corresponding to the BO problem.

Upper level (leader problem):

using BilevelHeuristics

F(x, y) = sum(x.^2) + sum(y.^2)
bounds_ul = [-ones(5) ones(5)] 

Lower level (follower problem):

f(x, y) = sum((x - y).^2) + y[1]^2
bounds_ll = [-ones(5) ones(5)];

Approximate solution:

res = optimize(F, f, bounds_ul, bounds_ll, BCA())

Output:

+=========== RESULT ==========+
  iteration: 108
    minimum: 
          F: 4.03387e-10
          f: 2.94824e-10
  minimizer: 
          x: [-1.1460768817533927e-5, 7.231706879604178e-6, 3.818596951258517e-6, 2.294324313691869e-6, 1.8770952450067828e-6]
          y: [1.998748659975197e-6, 9.479307908087866e-6, 6.180041276047425e-6, -7.642051857319683e-6, 2.434166021682429e-6]
    F calls: 2503
    f calls: 5062617
    Message: Stopped due UL function evaluations limitations. 
 total time: 26.8142 s
+============================+

See BilevelHeuristics documentation for more information.

Decision-Making

Although Metaheuristics is focused on the optimization part, some decision-making algorithms are available in this package (see Multi-Criteria Decision-Making).

The following example shows how to perform a posteriori decision-making.

julia> # load the problem
julia> f, bounds, pf = Metaheuristics.TestProblems.ZDT1();

julia> # perform multi-objective optimization
julia> res = optimize(f, bounds, NSGA2());

julia> # user preferences
julia> w = [0.5, 0.5];

julia> # set the decision-making algorithm
julia> dm_method = CompromiseProgramming(Tchebysheff())

julia> # find the best decision
julia> sol = best_alternative(res, w, dm_method)
(f = [0.38493217206706115, 0.38037042164979956], g = [0.0], h = [0.0], x = [3.849e-01, 7.731e-06, …, 2.362e-07])

Providing Initial Solutions

Sometimes you may need to use the starter solutions you need before the optimization process begins, well, this example illustrates how to do it.

julia> using Metaheuristics
julia> f, bounds, optimums = Metaheuristics.TestProblems.get_problem(:sphere);
julia> D = size(bounds,2);
julia> x_known = 0.6ones(D) # known solution10-element Array{Float64,1}: 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6 0.6
julia> X = [ bounds[1,:] + rand(D).* ( bounds[2,:] - bounds[1,:]) for i in 1:19 ]; # random solutions (uniform distribution)
julia> push!(X, x_known); # save an interest solution
julia> population = [ Metaheuristics.create_child(x, f(x)) for x in X ]; # generate the population with 19+1 solutions
julia> prev_status = State(Metaheuristics.get_best(population), population); # prior state
julia> method = ECA(N = length(population))ECA(η_max=2.0, K=7, N=20, N_init=20, p_exploit=0.95, p_bin=0.02, p_cr=Float64[], ε=0.0, adaptive=false, resize_population=false)
julia> method.status = prev_status; # say to ECA that you have generated a population
julia> optimize(f, bounds, method) # optimize+=========== RESULT ==========+ iteration: 5001 minimum: 6.01446e-135 minimizer: [1.5420667709680898e-68, 5.152130841593568e-68, 1.1116796645669154e-68, 1.3470136926965672e-68, -5.667607771010109e-69, 1.3874844203873058e-68, 4.761370882803728e-68, -2.396758415228037e-69, 1.3973262409935682e-68, 1.115793397269834e-68] f calls: 100000 total time: 0.8883 s stop reason: Maximum objective function calls exceeded. +============================+

Batch Evaluation

Evaluating multiple solutions at the same time can reduce computational time. To do that, define your function on an input N x D matrix and function values into matrices with outcomes in rows for all N solutions. Also, you need to put parallel_evaluation=true in the Options to indicate that your f is prepared for parallel evaluations.

f(X) = begin
    fx = sum(X.^2, dims=2)       # objective function ∑x²
    gx = sum(X.^2, dims=2) .-0.5 # inequality constraints ∑x² ≤ 0.5
    hx = zeros(0,0)              # equality constraints
    fx, gx, hx
end

options = Options(parallel_evaluation=true)

res = optimize(f, [-10ones(5) 10ones(5)], ECA(options=options))

See Parallelization tutorial for more details.

Modifying an Existing Metaheuristic

You may need to modify one of the implemented metaheuristic to improve the algorithm performance or test new mechanisms. This example illustrate how to do it.

Modifying algorithms could break stuff

Be cautious when modify a metaheuristic due to those changes will overwrite the default method for that metaheuristic.

Let's assume that we want to modify the stop criteria for ECA. See Contributing for more details.

julia> using Metaheuristics
julia> import LinearAlgebra: norm
julia> # overwrite method function Metaheuristics.stop_criteria!( status, parameters::ECA, # It is important indicate the modified Metaheuristic problem, information, options, args...; kargs... ) if status.stop # nothing to do return end # Diversity-based stop criteria x_mean = zeros(length(status.population[1].x)) for sol in status.population x_mean += sol.x end x_mean /= length(status.population) distances_mean = sum(sol -> norm( x_mean - sol.x ), status.population) distances_mean /= length(status.population) # stop when solutions are close enough to the geometrical center new_stop_condition = distances_mean <= 1e-3 status.stop = new_stop_condition # (optional and not recommended) print when this critaria is met if status.stop @info "Diversity-based stop criteria" @show distances_mean end return end
julia> f, bounds, opt = Metaheuristics.TestProblems.get_problem(:sphere);
julia> optimize(f, bounds, ECA())[ Info: Diversity-based stop criteria distances_mean = 0.0009884954378502641 +=========== RESULT ==========+ iteration: 180 minimum: 3.75885e-07 minimizer: [-6.447969588995779e-5, -3.3647224060899024e-5, -0.00014998126559769018, 0.00010329395380834097, 0.00010520237291068181, 0.000278148112905754, 9.238303288409012e-5, 0.0001929917690896446, 0.00034893649662849136, -0.0002854129900453423] f calls: 12600 total time: 0.3746 s stop reason: Unknown stop reason. +============================+