# \[ANN\] Metaheuristics.jl

**URL:** <https://discourse.julialang.org/t/ann-metaheuristics-jl/68477>\
**Category:** Package Announcements\
**Tags:** announcement, optimization\
**Created:** [September 20, 2021, 4:00pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477 "2021-09-20T16:00:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmejia8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmejia8/32/29338_2.png) [@jmejia8](https://discourse.julialang.org/u/jmejia8)\
**Post date:** [September 20, 2021, 4:00pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/1 "2021-09-20T16:00:23Z")

</div>

After several years of development, I share with you [Metaheuristics.jl](https://github.com/jmejia8/Metaheuristics.jl)

This package implements state-of-the-art metaheuristic algorithms for single and multi-objective optimization. The aim of this package is to provide easy to use (and fast) metaheuristics for numerical global optimization.

Metaheuristics.jl includes:

- Popular algorithms such as Differential Evolution, PSO, NSGA-II, NSGA-III, and more…
- Performance Indicators such as IGD, hypervolume, among others.
- Test Problems for single and multi-objective optimization.
- Some visualization features in terminal via [UnicodePlots.jl](https://github.com/JuliaPlots/UnicodePlots.jl)

**Example 1**

Objective function:

```plaintext
julia> f(x) = 10length(x) + sum( x.^2 - 10cos.(2π*x) )
f (generic function with 1 method)

```

Bounds for `x∈ [-5,5]⁵ `:

```plaintext
julia> bounds = [-5ones(5) 5ones(5)]
5×2 Matrix{Float64}:
 -5.0 5.0
 -5.0 5.0
 -5.0 5.0
 -5.0 5.0
 -5.0 5.0

```

Optimize:

```plaintext
julia> result = optimize(f, bounds)
+=========== RESULT ==========+
  iteration: 1429
    minimum: 0
  minimizer: [1.8792427588475406e-10, 1.6562253377998926e-9, -2.2532566630145983e-9, 1.649414863704028e-9, 4.005275371381464e-9]
    f calls: 49981
 total time: 0.2483 s
+============================+

```

**Example 2**

```plaintext
julia> f, bounds, front = Metaheuristics.TestProblems.ZDT3();

julia> optimize(f, bounds, NSGA2())
+=========== RESULT ==========+
  iteration: 500
...
non-dominated solution(s):
                           F space
          ┌────────────────────────────────────────┐ 
        2 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⢣⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠘⠲⠤⠀⠀⠀⠀⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
   f_2 │⠀⠀⠀⠀⠀⠀⠀⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠢⡄⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢱⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠻⠍⠉⠉⠉⠉⠉⠉⢭⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠀⠀⠀⠀⠀⠀⠀⢰⠀⠀⠀│ 
          │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢇⠀⠀│ 
       -1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
          └────────────────────────────────────────┘ 
          0 0.9
                             f_1
    f calls: 50000
  feasibles: 100 / 100 in final population
 total time: 1.4626 s
+============================+

```

Comments and suggestions are welcome.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 20, 2021, 4:24pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/2 "2021-09-20T16:24:14Z")

</div>

Congratulations on the package! I see you have some constraint handling in some of the algorithms. I think this might be the first Julia package for metaheuristics that can handle equality and inequality constraints natively. Would be great to create a wrapper for Metaheuristics in [Nonconvex.jl](https://github.com/JuliaNonconvex/Nonconvex.jl).

---

<div class="post-metadata">

**Author:** ![jmejia8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmejia8/32/29338_2.png) [@jmejia8](https://discourse.julialang.org/u/jmejia8)\
**Post date:** [September 21, 2021, 1:06am UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/3 "2021-09-21T01:06:32Z")

</div>

Thanks for your comment. Good suggestion!

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [September 21, 2021, 3:36pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/4 "2021-09-21T15:36:00Z")

</div>

Looks like a great package!

Very often I need to optimize expensive black-box functions, and having an optimizer that offers multi-threaded, simultaneous evaluation of multiple candidate solutions significantly reduces execution time. Please consider adding this option to your population-based optimizers.

---

<div class="post-metadata">

**Author:** ![jmejia8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmejia8/32/29338_2.png) [@jmejia8](https://discourse.julialang.org/u/jmejia8)\
**Post date:** [September 21, 2021, 3:52pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/5 "2021-09-21T15:52:25Z")

</div>

That is a great suggestion. The evaluation of multiple solutions will be included in future versions.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [April 18, 2023, 7:31pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/6 "2023-04-18T19:31:27Z")

</div>

> [@PeterSimon](#):
>
> Very often I need to optimize expensive black-box functions, and having an optimizer that offers multi-threaded, simultaneous evaluation of multiple candidate solutions significantly reduces execution time. Please consider adding this option to your population-based optimizers.

For the reference, parallel evaluation is now available (since?) in Metaheuristics.jl:

- [Examples · Metaheuristics.jl](https://jmejia8.github.io/Metaheuristics.jl/stable/examples/#Batch-Evaluation)
- [Parallelization · Metaheuristics.jl](https://jmejia8.github.io/Metaheuristics.jl/stable/tutorials/parallelization/)

---

<div class="post-metadata">

**Author:** ![jmejia8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmejia8/32/29338_2.png) [@jmejia8](https://discourse.julialang.org/u/jmejia8)\
**Post date:** [April 18, 2023, 9:00pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/7 "2023-04-18T21:00:08Z")

</div>

Parallel evaluation is available since v3.2.x (released mid-2022). BTW, v3.3 is coming with some new features:

> <https://github.com/jmejia8/Metaheuristics.jl/issues/79>
>
> Install the unstable version hosted in the \`develop\` branch.
> 
> \`\`\`julia
> pkg\> …add Metaheuristics#develop
> \`\`\`
> 
> \*\*Optimizers with default parameters\*\*
> 
> \`\`\`julia
> f, bounds, pareto\_solutions = Metaheuristics.TestProblems.get\_problem(:sphere);
> optimize(f, bounds, ECA)
> \`\`\`
> 
> Pass parameters and \`Options\` via as \`kargs...\`
> 
> \`\`\`julia
> optimize(f, bounds, ECA, N = 20, iterations = 10, seed = 1)
> \`\`\`
> 
> is similar to
> 
> \`\`\`julia
> optimize(f, bounds, ECA(N = 20, options=Options(iterations = 10, seed = 1))
> \`\`\`
> 
> \*\*Using the \[\`SearchSpaces\`\](https://jmejia8.github.io/SearchSpaces.jl/stable/searchspaces/) module to define search spaces.\*\*
> 
> \`\`\`julia
> fn(x) = abs(sum(diff(x)))
> space = boxconstraints(zeros(10), 100ones(10)) # float
> optimize(fn, space, ECA)
> 
> \# bounds
> space = boxconstraints(zeros(10), 100ones(10), rigid = false) # float
> optimize(fn, space, ECA)
> 
> space = boxconstraints(zeros(Int, 10), 100ones(Int, 10)) # integers
> optimize(fn, space, ECA)
> 
> \# discrete
> space = BitArraySpace(10) # 10D array of booleans
> optimize(fn, space, GA)
> 
> space = PermutationSpace(10) # 10-permutation
> optimize(fn, space, GA)
> 
> \`\`\`
> 
> \*\*Termination criteria based on convergence\*\*
> 
> \*\*Other performance improvements\*\*
> 
> \*\*Clear message errors (https://github.com/jmejia8/Metaheuristics.jl/issues/65)\*\*
> 
> \`\`\`julia
> ff(x) = "abcd"
> optimize(ff, \[zeros(2) ones(2)\] , ECA )
> \`\`\`
> output:
> \`\`\`
> ERROR: Objective function should return either a numerical value or a Tuple depending on the problem.
> Current output: abcd of type String
> Examples at https://jmejia8.github.io/Metaheuristics.jl/stable/examples
> Stacktrace:
> \[1\] error(s::String)
> \`\`\`

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [June 9, 2024, 11:55am UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/8 "2024-06-09T11:55:51Z")

</div>

Say, I am only an occasional user to this interesting package.

After working fine earlier, now it seems that it does not regocnize ‘BitArraySpace’

```julia
UndefVarError: `BitArraySpace` not defined

```

I have tried Metaheuristics.BitArraySpace, etc and looked all over, but cannot figure out why the same code which worked earlier now does not work  
Thanks for any hints

---

<div class="post-metadata">

**Author:** ![jmejia8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmejia8/32/29338_2.png) [@jmejia8](https://discourse.julialang.org/u/jmejia8)\
**Post date:** [June 12, 2024, 8:13am UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/9 "2024-06-12T08:13:32Z")

</div>

Which version are you using?

---

<div class="post-metadata">

**Author:** ![AkchurinDA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akchurinda/32/49367_2.png) [@AkchurinDA](https://discourse.julialang.org/u/AkchurinDA)\
**Post date:** [November 26, 2024, 6:23pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/10 "2024-11-26T18:23:09Z")

</div>

Hi! Thank you for the package - it is absolutely great.  
I have just one question:  
How does `Metaheuristics.jl` “penalize” the objective function for not satisfying the constraints? From what I gather, it doesn’t use penalty method. Does it use something along the lines f^{\prime}(x) = f(x) (1 + \sum |g(x)|)?

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [December 2, 2024, 12:40pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/11 "2024-12-02T12:40:28Z")

</div>

Good question.

Looking at the list of algorithms [Algorithms · Metaheuristics.jl](https://jmejia8.github.io/Metaheuristics.jl/stable/algorithms/), I would say that the question shouldn’t be asked at the higher level of `Metaheuristics.jl`, but at the lower level of each algo, since some of them don’t support constraint.

Looking at the classical [DE](https://jmejia8.github.io/Metaheuristics.jl/stable/algorithms/#Metaheuristics.DE) algo, I see no discussion of constraints in the doc. I looked at the [DE code](https://github.com/jmejia8/Metaheuristics.jl/tree/master/src/algorithms/singleobjective/DE), but found nothing about constraints in DE.jl. However, there is a [epsilonDE.jl](https://github.com/jmejia8/Metaheuristics.jl/blob/master/src/algorithms/singleobjective/DE/epsilonDE.jl) which seems to be specialized for constrainted problem. But is this epsilonDE documented? Or is there an automatic switch to epsilonDE when calling DE solver on a constrained problem?

---

<div class="post-metadata">

**Author:** ![tbk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbk/32/212273_2.png) [@tbk](https://discourse.julialang.org/u/tbk)\
**Post date:** [December 2, 2024, 1:30pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/12 "2024-12-02T13:30:17Z")

</div>

It seems to be documented [here](https://jmejia8.github.io/Metaheuristics.jl/stable/algorithms/#%5CvarepsilonDE).

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [December 2, 2024, 1:50pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/13 "2024-12-02T13:50:27Z")

</div>

Ah yes, my mistake. Since DE and epsilonDE are siblings in the source tree, I was expecting them side-by-side in the table.

But now back to @AkchurinDA question: how are constraints implemented? Actually, to make things tidier, I suggest @AkchurinDA could create a dedicated thread .

---

<div class="post-metadata">

**Author:** ![AkchurinDA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akchurinda/32/49367_2.png) [@AkchurinDA](https://discourse.julialang.org/u/AkchurinDA)\
**Post date:** [December 9, 2024, 4:48pm UTC](https://discourse.julialang.org/t/ann-metaheuristics-jl/68477/14 "2024-12-09T16:48:05Z")

</div>

Opened a new thread on this topic at [Constraints in the Genetic Algorithm implemented in `Metaheuristics.jl`](https://discourse.julialang.org/t/constraints-in-the-genetic-algorithm-implemented-in-metaheuristics-jl/123628).
