<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Runtux Blog (Posts about genetic algorithms)</title><link>https://blog.runtux.com/</link><description></description><atom:link href="https://blog.runtux.com/categories/genetic-algorithms.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2025 &lt;a href="mailto:rsc@runtux.com"&gt;Ralf Schlatterbeck&lt;/a&gt; </copyright><lastBuildDate>Mon, 03 Nov 2025 18:34:10 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Water: A Multi-Objective Benchmark Problem</title><link>https://blog.runtux.com/posts/2025/06/15/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;div&gt;&lt;p&gt;This is a write-up that really should go into the documentation of
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; but I was unable to include &lt;a class="reference external" href="https://plotly.com/"&gt;plotly&lt;/a&gt; graphics into a github
&lt;code class="docutils literal"&gt;README.rst&lt;/code&gt; file, even using the proxy-site &lt;code class="docutils literal"&gt;raw.githack.com&lt;/code&gt;
(which is a page that serves html and javascript files with the correct
content type so that it can be displayed in a web browser – github
choses to serve these files with content-type text/plain). Github
refuses to render an iframe inside  a github page.&lt;/p&gt;
&lt;p&gt;The "Water Resource Planning" problem &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/15/#rts01" id="citation-reference-1" role="doc-biblioref"&gt;[RTS01]&lt;/a&gt; is an engineering problem
that has been used as a benchmark for multi-objective optimization in
the literature including classical papers like the original article on
NSGA_II &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/15/#dpam02" id="citation-reference-2" role="doc-biblioref"&gt;[DPAM02]&lt;/a&gt;. The problem consists of seven constraints and a
five-dimensional objective function.&lt;/p&gt;
&lt;p&gt;I have used this for quite some time to test my NSGA-II implementation
in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;. It is in the examples/nsgaii directory and can (after
compilation) be called with test-problem index &lt;code class="docutils literal"&gt;12&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;examples/nsgaii/optimize 12&lt;/pre&gt;
&lt;p&gt;The output of this program can also be found in the file&lt;/p&gt;
&lt;pre class="literal-block"&gt;test/nsgaii_optimize_12.data&lt;/pre&gt;
&lt;p&gt;For testing I'm shipping a script called &lt;code class="docutils literal"&gt;crowdingplot&lt;/code&gt; to plot
objectives in a multi-objective problem. It is in the same directory as
the code for the example.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blog.runtux.com/posts/2025/06/15/"&gt;Read more…&lt;/a&gt; (3 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2025/06/15/</guid><pubDate>Sun, 15 Jun 2025 11:50:00 GMT</pubDate></item><item><title>Update on non-dominated sorting measurements</title><link>https://blog.runtux.com/posts/2025/06/14/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;div&gt;&lt;p&gt;This is an update on a previous post &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/14/#sch25" id="citation-reference-1" role="doc-biblioref"&gt;[Sch25]&lt;/a&gt; about a new implementation of
non-dominated sorting for &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;. Non-dominated sorting is a central
component of most multi-objective optimization implementations.&lt;/p&gt;
&lt;p&gt;In the recent post I did not mention how many generations my test ran.
The number of generations in the last and in the following tests was
200. In the last test the code was compiled without optimization. The
following measurements were made with &lt;code class="docutils literal"&gt;&lt;span class="pre"&gt;-O3&lt;/span&gt;&lt;/code&gt; the best optimization
&lt;a class="reference external" href="https://gcc.gnu.org/"&gt;gcc&lt;/a&gt; offers. I noticed that my implementation of the evaluation
function for the DTLZ-1 test &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/14/#dtlz05" id="citation-reference-2" role="doc-biblioref"&gt;[DTLZ05]&lt;/a&gt; is quadratic in the number of
objectives so I feared this would influence the tests. It turns out it
didn't, the overhead even for 20 objectives is still too low to be
noticeable.&lt;/p&gt;
&lt;p&gt;In addition to the first tests I'm now measuring – in addition to the
overall time – the time just for the non-dominated sorting. So in the
following we have two types of graphics, the ones where the title is
starting with "Overall" include the time of evaluation function and
whatever the algorithm is doing for bookkeeping. The ones starting with
"Non-dominated" just measure the time for the non-dominated sorting.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blog.runtux.com/posts/2025/06/14/"&gt;Read more…&lt;/a&gt; (7 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2025/06/14/</guid><pubDate>Sat, 14 Jun 2025 08:30:00 GMT</pubDate></item><item><title>Non-dominated Sorting for Multi-Objective Optimization</title><link>https://blog.runtux.com/posts/2025/06/09/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;div&gt;&lt;p&gt;Some of you know that I'm maintaining &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt;, a genetic algorithm
package and a corresponding Python wrapper, &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt;. &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt; implements
multi-objective optimization using NSGA-II &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#dpam02" id="citation-reference-1" role="doc-biblioref"&gt;[DPAM02]&lt;/a&gt; and NSGA-III
&lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#dj14" id="citation-reference-2" role="doc-biblioref"&gt;[DJ14]&lt;/a&gt;, &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#jd14" id="citation-reference-3" role="doc-biblioref"&gt;[JD14]&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Multi-objective optimization means that we have more than one objective
(or fitness-) function. Since typically these functions can contradict
each other – when a candidate solution becomes better in one objective
it might become worse in another objective – we need to establish what
&lt;em&gt;better&lt;/em&gt; means in the context of multi-objective optimization. An
individual &lt;span class="math"&gt;\(A\)&lt;/span&gt; is strictly better than another individual
&lt;span class="math"&gt;\(B\)&lt;/span&gt; – we say individual &lt;span class="math"&gt;\(A\)&lt;/span&gt; &lt;em&gt;dominates&lt;/em&gt; individual
&lt;span class="math"&gt;\(B\)&lt;/span&gt; – when &lt;span class="math"&gt;\(A\)&lt;/span&gt; is better or equal to &lt;span class="math"&gt;\(B\)&lt;/span&gt; in all
objectives and strictly better in at least one of them.&lt;/p&gt;
&lt;p&gt;Since we do not have a single fitness for individuals, multi-objective
algorithms typically establish a ranking of individuals by dominance.
Individuals which are not dominated by any other individual get
dominance rank 0. Then we remove all rank-0 individuals and all
individuals that are then non-dominated get rank 1 etc. This procedure
is typically called non-dominated ranking or non-dominated sorting.
This rank is then one of the components of the new fitness function.&lt;/p&gt;
&lt;p&gt;The NSGA-II paper &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#dpam02" id="citation-reference-4" role="doc-biblioref"&gt;[DPAM02]&lt;/a&gt; implements this by checking each individual
in a population against every other individual to see if one dominates
the other. Then it removes the current non-dominated individuals
one-by-one and establishes ranks as outlined above. This algorithm has a
run-time that grows quadratically with the number of individuals to
sort. In computer science we write this in the &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Big_O_notation"&gt;big O notation&lt;/a&gt; as
&lt;span class="math"&gt;\(O(n^2)\)&lt;/span&gt; where &lt;span class="math"&gt;\(n\)&lt;/span&gt; is the number of individuals.&lt;/p&gt;
&lt;p&gt;In 2003 Jensen &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#jen03" id="citation-reference-5" role="doc-biblioref"&gt;[Jen03]&lt;/a&gt; published an algorithm that can perform the
dominance-ranking of all individuals with effort
&lt;span class="math"&gt;\(O\left(n\cdot(\log n)^{m-1}\right)\)&lt;/span&gt; where &lt;span class="math"&gt;\(m\)&lt;/span&gt; is the number
of objectives (and &lt;span class="math"&gt;\(n\)&lt;/span&gt; is the population size as before). Jensen's
algorithm was updated 10 years later because he had formulated his
algorithm in a way that could not handle individuals with the same
evaluation in one objective which he claimed was easy to remedy but was
not so easy for others &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#fgp13" id="citation-reference-6" role="doc-biblioref"&gt;[FGP13]&lt;/a&gt;. A year later a slight modification
was made to formally prove that the algorithm runtime complexity actually
fits the formula above &lt;a class="citation-reference" href="https://blog.runtux.com/posts/2025/06/09/#bs14" id="citation-reference-7" role="doc-biblioref"&gt;[BS14]&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blog.runtux.com/posts/2025/06/09/"&gt;Read more…&lt;/a&gt; (4 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2025/06/09/</guid><pubDate>Mon, 09 Jun 2025 19:00:00 GMT</pubDate></item><item><title>Optimizing Floating-Point Problems with Evolutionary Algorithms</title><link>https://blog.runtux.com/posts/2024/01/07/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;p&gt;[This is an extended abstract for a talk I'm planning]&lt;/p&gt;
&lt;p&gt;Evolutionary Algorithms (EA) are a superset of genetic algorithms and
related optimization algorithms. Genetic algorithms usually work with
bits and small integer numbers.&lt;/p&gt;
&lt;p&gt;There are other EAs that directly work with floating point numbers,
among the Differential Evolution (DE) &lt;a class="brackets" href="https://blog.runtux.com/posts/2024/01/07/#footnote-1" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; &lt;a class="brackets" href="https://blog.runtux.com/posts/2024/01/07/#footnote-2" id="footnote-reference-2" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; &lt;a class="brackets" href="https://blog.runtux.com/posts/2024/01/07/#footnote-3" id="footnote-reference-3" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The talk gives an introduction to optimization of floating-point
problems with DE. It uses examples from electrical engineering as well
as from optimization of actuation waveforms of inkjet printers.
Piezo-electric inkjet printers use an actuation waveform to jet drops
out of a nozzle. This waveform (among other parameters like the jetted
fluid) determines the quality of the jetted drops.&lt;/p&gt;
&lt;p&gt;For the software I'm using the Python bindings &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt; &lt;a class="brackets" href="https://blog.runtux.com/posts/2024/01/07/#footnote-4" id="footnote-reference-4" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; for the
Parallel Genetic Algorithm Package PGAPack &lt;a class="brackets" href="https://blog.runtux.com/posts/2024/01/07/#footnote-5" id="footnote-reference-5" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; which was originally
developed at Argonne National Laboratories. I'm maintaining both
packages for some years now. Among others, Differential Evolution (DE)
and strategies for multi-objective optimization (using NSGA-II &lt;a class="brackets" href="https://blog.runtux.com/posts/2024/01/07/#footnote-6" id="footnote-reference-6" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;)
where newly implemented.&lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="footnote-1" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2024/01/07/#footnote-reference-1"&gt;1&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution -- a simple
and efficient adaptive scheme for global optimization over
continuous spaces. Technical Report TR-95-012, International
Computer Science Institute (ICSI), March 1995.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-2" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2024/01/07/#footnote-reference-2"&gt;2&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution -- a simple
and efficient heuristic for global optimization over continuous
spaces. Journal of Global Optimization, 11(4):341–359, December 1997.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-3" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2024/01/07/#footnote-reference-3"&gt;3&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Kenneth V. Price, Rainer M. Storn, and Jouni A. Lampinen.
Differential Evolution: A Practical Approach to Global Optimization.
Springer, Berlin, Heidelberg, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-4" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2024/01/07/#footnote-reference-4"&gt;4&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;, a general-purpose, data-structure-neutral, parallel genetic
algorithm library&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-5" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2024/01/07/#footnote-reference-5"&gt;5&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGAPy&lt;/a&gt;: Python Wrapper for PGAPack Parallel Genetic Algorithm Library&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-6" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2024/01/07/#footnote-reference-6"&gt;6&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Kalyanmoy Deb, Samir Agrawal, Amrit Pratap, and T. Meyarivan.
A fast elitist non-dominated sorting genetic algorithm for
multi-objective optimization: NSGA-II. In Marc Schoenauer,
Kalyanmoy Deb, Günther Rudolph, Xin Yao, Evelyne Lutton, Juan
Julian Merelo, and Hans-Paul Schwefel, editors, Parallel Problem
Solving from Nature – PPSN VI, volume 1917 of Lecture Notes in
Computer Science, pages 849–858. Springer, Paris, France, September
2000.&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>howto</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2024/01/07/</guid><pubDate>Sun, 07 Jan 2024 17:00:00 GMT</pubDate></item><item><title>Differential Evolution Illustrated</title><link>https://blog.runtux.com/posts/2023/04/07/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;p&gt;This post again applies to &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt; or other Genetic
Algorithm (GA) implementations that support Differential Evolution (DE).&lt;/p&gt;
&lt;p&gt;Differential Evolution (DE) &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/04/07/#footnote-1" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/04/07/#footnote-2" id="footnote-reference-2" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/04/07/#footnote-3" id="footnote-reference-3" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; is a population based
optimizer that is similar to other evolutionary algorithms. It is quite
powerful and implemented in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; (and usable in the Python wrapper
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGAPy&lt;/a&gt;). To illustrate some points about this algorithm I've constructed
a simple parcours in &lt;a class="reference external" href="https://github.com/openscad/openscad"&gt;OpenSCAD&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;img alt="/images/parcours.gif" src="https://blog.runtux.com/images/parcours.gif"&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;To test the optimizer with this parcours the gene in the genetic
algorithm (DE calls this the parameters) is the X and Y coordinate to
give a position on the parcours. In the following we're also calling
these coordinates a &lt;em&gt;vector&lt;/em&gt; as is common in the DE literature.  We
initialize the population in the region of the start of the ramp. We
allow the population to leave the initialization range.&lt;/p&gt;
&lt;p&gt;Note that the corners of the parcours are flat areas with the same
height. Areas like this are generally difficult for optimization
algorithms because the algorithm cannot know in which direction better
values (if available at all) can be found. This is also the reason why
we do not put the initialization range inside the flat area at the
bottom of the parcours. Note that normally one would initialize the
population over the whole area to be searched. In our case we want to
demonstrate that the algorithm is well capable of finding a solution far
from the initialization range.&lt;/p&gt;
&lt;p&gt;The algorithm of DE is quite simple (see, e.g. &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/04/07/#footnote-3" id="footnote-reference-4" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; p.37-47): Each
member &lt;span class="math"&gt;\(\mathbf x_{i,g}\)&lt;/span&gt; in the population of the current
generation &lt;span class="math"&gt;\(g\)&lt;/span&gt;, where &lt;span class="math"&gt;\(i\)&lt;/span&gt; is the index of the population
member is crossed over with a mutant vector &lt;span class="math"&gt;\(v_{i,g}\)&lt;/span&gt;. The mutant
vector is generated by selecting three population members at random
(without replacement) from the population and combining them as follows.&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
\mathbf v_{i,g} = \mathbf x_{r_0,g}
                  + F \cdot (\mathbf x_{r_1,g} - \mathbf x_{r_2,g})
\end{equation*}
&lt;/div&gt;
&lt;p&gt;As can be seen, the weighted difference of two random members of the
population is added to a third population member. All indeces
&lt;span class="math"&gt;\(r_0, r_1,\)&lt;/span&gt; and &lt;span class="math"&gt;\(r_2\)&lt;/span&gt; are different and different from
&lt;span class="math"&gt;\(i\)&lt;/span&gt;. The weight &lt;span class="math"&gt;\(F\)&lt;/span&gt; is a configuration parameter that is
typically &lt;span class="math"&gt;\(0.3 \le F &amp;lt; 1\)&lt;/span&gt;. This algorithm is the &lt;em&gt;classic&lt;/em&gt; DE,
also often termed &lt;code class="docutils literal"&gt;&lt;span class="pre"&gt;de-rand-1-bin&lt;/span&gt;&lt;/code&gt;. A variant uses the index of the &lt;em&gt;best&lt;/em&gt;
individual instead of &lt;span class="math"&gt;\(r_0\)&lt;/span&gt; and is called &lt;code class="docutils literal"&gt;&lt;span class="pre"&gt;de-best-1-bin&lt;/span&gt;&lt;/code&gt;. If more
than a single difference is added, another variant would be, e.g.,
&lt;code class="docutils literal"&gt;&lt;span class="pre"&gt;de-rand-2-bin&lt;/span&gt;&lt;/code&gt;. The last component of the name &lt;code class="docutils literal"&gt;bin&lt;/code&gt; refers to
uniform crossover &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/04/07/#footnote-4" id="footnote-reference-5" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, a binomial distribution. In DE, parameters from
the mutant vector are selected with a certain probability &lt;span class="math"&gt;\(Cr\)&lt;/span&gt;.
Note that DE makes sure that at least one parameter from the mutant
vector is selected (otherwise the original vector &lt;span class="math"&gt;\(\mathbf
x_{i,g}\)&lt;/span&gt; would be unchanged).&lt;/p&gt;
&lt;p&gt;More details about DE and the usage with &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; can be found in the
section &lt;a class="reference external" href="https://pgapack.readthedocs.io/en/latest/part1.html#sec-differential-evolution"&gt;Differential Evolution&lt;/a&gt; and in the &lt;a class="reference external" href="https://pgapack.readthedocs.io/en/latest/part2.html#sec-mutation"&gt;Mutation&lt;/a&gt; sections of the
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; documentation.&lt;/p&gt;
&lt;p&gt;To use the &lt;a class="reference external" href="https://github.com/openscad/openscad"&gt;OpenSCAD&lt;/a&gt; model for optimization, we convert it to a
greyscale heightmap using an &lt;a class="reference external" href="https://fenrus75.github.io/FenrusCNCtools/javascript/stl2png.html"&gt;STL to PNG converter&lt;/a&gt; (STL is a &lt;a class="reference external" href="https://en.wikipedia.org/wiki/STL_(file_format)"&gt;3D
format&lt;/a&gt; that can be generated by &lt;a class="reference external" href="https://github.com/openscad/openscad"&gt;OpenSCAD&lt;/a&gt;). The evaluation
function then simply returns the greyscale value at the given location
(after rounding the gene X and Y values to an integer). Values outside
the picture are returned as 0 (black). The resulting optimization run is
shown below in an animated image. The population is quite small, there
are only 6 individuals in the population. We clearly see that when the
differences between individuals gets large (on the straight ridges of
the parcours), the optimization proceeds in larger steps. We also see
that the flat corners can take quite some time to escape from and in the
corners the algorithm slows down. Finally in the last corner, the cone
is climbed and the individuals converge almost to a single point. The
algorithm is stopped when the first individual returns an evaluation of
the largest greyscale value (representing white). Note that I cheated a
little in that many optimization runs take longer (in particular the
optimization can get stuck for many generations in the flat parts of the
ridge) and I selected a run that produced a good animation. So the
selected run is not the average but one of the shorter runs.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;img alt="/images/animated.gif" src="https://blog.runtux.com/images/animated.gif"&gt;
&lt;p&gt; &lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="footnote-1" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/04/07/#footnote-reference-1"&gt;1&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient heuristic for global optimization over continuous
spaces. &lt;em&gt;Journal of Global Optimization&lt;/em&gt;, 11(4):341–359, December 1997.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-2" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/04/07/#footnote-reference-2"&gt;2&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient adaptive scheme for global optimization over
continuous spaces. Technical Report TR-95-012, International
Computer Science Institute (ICSI), March 1995.
&lt;a class="reference external" href="ftp://ftp.icsi.berkeley.edu/pub/techreports/1995/tr-95-012.pdf"&gt;Available from the International Computer Science Institute&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-3" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/04/07/#footnote-reference-3"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/04/07/#footnote-reference-4"&gt;2&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Kenneth V. Price, Rainer M. Storn, and Jouni A. Lampinen.
&lt;em&gt;Differential Evolution: A Practical Approach to Global Optimization.&lt;/em&gt;
Springer, Berlin, Heidelberg, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-4" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/04/07/#footnote-reference-5"&gt;4&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Gilbert Syswerda. Uniform crossover in genetic algorithms. In J.
David Schaffer, editor, Proceedings of the Third International
Conference on Genetic Algorithms (ICGA), pages 2–9. Morgan
Kaufmann, Fairfax, Virginia, June 1989.&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>howto</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2023/04/07/</guid><pubDate>Fri, 07 Apr 2023 20:00:00 GMT</pubDate></item><item><title>Notes on Premature Convergence in Genetic Algorithms</title><link>https://blog.runtux.com/posts/2023/01/06/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;p&gt;This post again applies to &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt; or other Genetic
Algorithm (GA) implementations.&lt;/p&gt;
&lt;p&gt;When optimizing solutions with GA, it sometimes happens that a
sub-optimal solution is found because the whole population converges
early to a part of the search space where no better solutions are found
anymore. This effect is called &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Premature_convergence"&gt;premature convergence&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It is usually hard to detect &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Premature_convergence"&gt;premature convergence&lt;/a&gt;, a good measure is
the mean &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Hamming_distance"&gt;hamming distance&lt;/a&gt; between individuals. In &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; reporting of
the &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Hamming_distance"&gt;hamming distance&lt;/a&gt; can be enabled with the reporting option
&lt;code class="docutils literal"&gt;PGA_REPORT_HAMMING&lt;/code&gt;, set with the function &lt;code class="docutils literal"&gt;PGASetPrintOptions&lt;/code&gt; in
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; and with the &lt;code class="docutils literal"&gt;print_options&lt;/code&gt; constructor parameter in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt;.
Unfortunately this is only implemented for the binary datatype.&lt;/p&gt;
&lt;p&gt;One reason for the effect of &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Premature_convergence"&gt;premature convergence&lt;/a&gt; is the use of
&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Fitness_proportionate_selection"&gt;Fitness Proportionate Selection&lt;/a&gt; as detailed in an earlier post in
this blog &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-1" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;. If during the early stages of the search an individual
is discovered that is much better than anything found so far, the chance
is high that this individual takes over the whole population when
&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Fitness_proportionate_selection"&gt;Fitness Proportionate Selection&lt;/a&gt; is in use, preventing any further
progress of the algorithm.  The reason is that an individual that is
much better than all others gets an unreasonably high proportion of the
roulette wheel when selecting individuals for the next generation
resulting in all other genetic material having only a slim chance of
making it into the next generation.&lt;/p&gt;
&lt;p&gt;Another reason for premature convergence can be a small population size.
Goldberg et. al. &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-9" id="footnote-reference-2" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;9&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; give estimates of the population size for classic
GA with a small alphabet (the number of different allele values, e.g. 0
and 1 for a binary GA) with cardinality &lt;span class="math"&gt;\(\chi\)&lt;/span&gt;, a problem specific
building block size that overcomes deception &lt;span class="math"&gt;\(k \ll n\)&lt;/span&gt; where
&lt;span class="math"&gt;\(k\)&lt;/span&gt; is the building block size (a measure for the difficulty of
the problem) and &lt;span class="math"&gt;\(n\)&lt;/span&gt; is the string (gene) length. They show
that the population size should be &lt;span class="math"&gt;\(O(m\chi^k)\)&lt;/span&gt; with &lt;span class="math"&gt;\(m=n/k\)&lt;/span&gt;
so that for a given difficulty of the problem &lt;span class="math"&gt;\(k\)&lt;/span&gt; the population
size is proportional to the string length &lt;span class="math"&gt;\(n\)&lt;/span&gt;. This result,
however, does not readily translate to problems with a large alphabet,
e.g. floating-point representations like the &lt;code class="docutils literal"&gt;real&lt;/code&gt; data type in
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;. For floating point representations, difficulty usually
translates to how multimodal a problem is, i.e., how many &lt;em&gt;peaks&lt;/em&gt; (in
case of a maximization problem) or &lt;em&gt;valleys&lt;/em&gt; (in case of a minimization
problem) there are in the objective function.&lt;/p&gt;
&lt;p&gt;Now if with an adequate population size &lt;em&gt;and&lt;/em&gt; an appropriate selection
scheme premature convergence still occurs, there are some mechanisms
that can be used.&lt;/p&gt;
&lt;section id="prevention-of-duplicates"&gt;
&lt;h2&gt;Prevention of Duplicates&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; implements a mechanism for preventing duplicate gene strings
(individuals). In previous implementations the computation effort was
quadratic in the population size &lt;span class="math"&gt;\(N\)&lt;/span&gt;, i.e. the effort was
&lt;span class="math"&gt;\(O(N^2)\)&lt;/span&gt; (it compared each new individual with &lt;em&gt;all&lt;/em&gt; current
members of the population, once for each new individual). In the latest
versions it uses hashing for detecting duplicates, reducing the overhead
to &lt;span class="math"&gt;\(O(N)\)&lt;/span&gt;, a small constant overhead for each new individual.&lt;/p&gt;
&lt;p&gt;For user-defined data types this means that the user has to define a
hash function for the data type in addition to the string comparison
function. For the built-in data types (binary, integer, real) this is
automatically available.&lt;/p&gt;
&lt;p&gt;Prevention of duplicates works quite well for binary and integer data
types, especially if the genetic operators have a high probability of
producing duplicates. It does not work so well for the real data type
because new individuals tend to be different from other individuals even
if they can often be very close to already existing individuals.&lt;/p&gt;
&lt;/section&gt;
&lt;section id="restricted-tournament-replacement"&gt;
&lt;h2&gt;Restricted Tournament Replacement&lt;/h2&gt;
&lt;p&gt;An algorithm originally called &lt;em&gt;restricted tournament selection&lt;/em&gt; by
Harik &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-2" id="footnote-reference-3" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-3" id="footnote-reference-4" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; and later adopted under the name of &lt;em&gt;restricted
tournament replacement&lt;/em&gt; (RTR) by Pelikan &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-4" id="footnote-reference-5" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; uses the old and new
population for deciding if a candidate individual will be allowed into
the new population. It works by randomly selecting a number of members
from the old population (called the selection window), chosing the
individual which is most similar to the candidate, and allows the
candidate into the new population only if it is better than this most
similar individual.&lt;/p&gt;
&lt;p&gt;The default for the number of individuals randomly selected from the old
population (the window size) by default is &lt;span class="math"&gt;\(\min (n,
\frac{N}{20})\)&lt;/span&gt; &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-4" id="footnote-reference-6" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; where &lt;span class="math"&gt;\(n\)&lt;/span&gt; is the string (gene) length and &lt;span class="math"&gt;\(N\)&lt;/span&gt;
is the population size. This can be set by the user with the
&lt;code class="docutils literal"&gt;PGASetRTRWindowSize&lt;/code&gt; function for &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; and with the
&lt;code class="docutils literal"&gt;rtr_window_size&lt;/code&gt; constructor parameter of &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The RTR algorithm needs a similarity metric for deciding how
close an individual is to another. By default this uses a &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Taxicab_geometry"&gt;manhattan
distance&lt;/a&gt; (equivalent to the &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Hamming_distance"&gt;hamming distance&lt;/a&gt; for binary genes), i.e. an
allele-by-allele sum of distances, but can be set to an euclidian
distance or a user-defined metric with the user-function mechanism of
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;. Comparison of an euclidian distance metric for RTR is
in the &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy/blob/master/examples/magic_square.py"&gt;magic square example&lt;/a&gt; in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt; where the use of the euclidian
distance can be turned on with a command-line option.&lt;/p&gt;
&lt;p&gt;Restricted tournament replacement works well not only for binary and
integer genes but also for real genes. It can be combined with different
evolutionary algorithm settings.&lt;/p&gt;
&lt;p&gt;The effect of RTR on a problem that tends to suffer from &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Premature_convergence"&gt;premature
convergence&lt;/a&gt; can be seen in the test program &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/mgh/testprog.f"&gt;&lt;code class="docutils literal"&gt;examples/mgh/testprog.f&lt;/code&gt;&lt;/a&gt;,
this implements several test functions from an old benchmark of
nonlinear test problems &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-5" id="footnote-reference-7" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;. The test function that exhibits premature
convergence is what the authors call a "Gaussian function", described as
example (9) in the paper &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-5" id="footnote-reference-8" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; and implemented as function 3 in
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/mgh/objfcn77.f"&gt;&lt;code class="docutils literal"&gt;examples/mgh/objfcn77.f&lt;/code&gt;&lt;/a&gt;. This function is given as&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
f(x) = x_1 \cdot e^{\frac{x_2(t_i - x_3)^2}{2}} - y_i
\end{equation*}
&lt;/div&gt;
&lt;p&gt;with&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
t_i = (8 - i) / 2
\end{equation*}
&lt;/div&gt;
&lt;p&gt;And tabulated values for &lt;span class="math"&gt;\(y_i\)&lt;/span&gt; given in the paper or the
implementation in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/mgh/objfcn77.f"&gt;&lt;code class="docutils literal"&gt;examples/mgh/objfcn77.f&lt;/code&gt;&lt;/a&gt;. The minimization problem
from these equations is&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
f (x) = \sum_{i=1}^{m} f_i^2(x)
\end{equation*}
&lt;/div&gt;
&lt;p&gt;with &lt;span class="math"&gt;\(m=15\)&lt;/span&gt; for this test problem. The authors &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-5" id="footnote-reference-9" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; give the vector
&lt;span class="math"&gt;\(x_0 = (0.4, 1, 0)\)&lt;/span&gt; for the minimum
&lt;span class="math"&gt;\(f(x_0) = 1.12793 \cdot 10^{-8}\)&lt;/span&gt;
they found. The original Fortran implementation in
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/mgh/testprog.f"&gt;&lt;code class="docutils literal"&gt;examples/mgh/testprog.f&lt;/code&gt;&lt;/a&gt; uses a population size of 10000 with default
settings for the &lt;code class="docutils literal"&gt;real&lt;/code&gt; data type of &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;. The large population
size is chosen because otherwise the problem exhibits &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Premature_convergence"&gt;premature
convergence&lt;/a&gt;. It finds a solution in 100 generations
&lt;span class="math"&gt;\(x_0=(0.3983801, 0.9978369, 0.009918243)\)&lt;/span&gt; with an evaluation value
of &lt;span class="math"&gt;\(f(x_0)=2.849966\cdot 10^{-5}\)&lt;/span&gt;. The number of function
evaluations needed were 105459 (this is a little less than
&lt;span class="math"&gt;\(10000 + 100 \cdot 1000\)&lt;/span&gt;, i.e. evaluation of the initial
generation plus evaluation of 10% of the population of each generation,
the probability of crossover and mutation is not 100%, so it happens
that none of the operators is performed on an individual and it is not
re-evaluated).&lt;/p&gt;
&lt;p&gt;I've implemented the same problem with Differential Evolution &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-6" id="footnote-reference-10" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;,
&lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-7" id="footnote-reference-11" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;7&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2023/01/06/#footnote-8" id="footnote-reference-12" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;8&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/mgh/testprogde.c"&gt;&lt;code class="docutils literal"&gt;examples/mgh/testprogde.c&lt;/code&gt;&lt;/a&gt; (the driver program
implemented in C because I really do not speak Fortran but using the
same functions from the Fortran implementation linking the Fortran and C
code into a common executable). This uses a population size of 2000
(large for most problems when using Differential Evolution, again for
the reason of premature convergence) and finds the solution
&lt;span class="math"&gt;\(x_0=(0.3992372, 0.9990791, -0.0007697923)\)&lt;/span&gt; with an evaluation
value of &lt;span class="math"&gt;\(f(x_0)=7.393123\cdot 10^{-7}\)&lt;/span&gt; in only 30 generations.
This amounts to 62000 function evaluations (Differential Evolution
creates all individuals for the new generation and decides afterwards
which to keep).&lt;/p&gt;
&lt;p&gt;When using RTR with this problem in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/mgh/testprogdertr.c"&gt;&lt;code class="docutils literal"&gt;examples/mgh/testprogdertr.c&lt;/code&gt;&lt;/a&gt;, the
population size can be reduced to 250 and even after 100 generations the
search has not converged to a suboptimal solution. After 100 generations
we find &lt;span class="math"&gt;\(x_0=(0.398975, 1.000074, -6.719886 \cdot 10^{-5})\)&lt;/span&gt; and
&lt;span class="math"&gt;\(f(x_0)=1.339766\cdot 10^{-8}\)&lt;/span&gt; (but also with some changed settings of
the Differential Evolution algorithm). This amounts to 25250 function
evaluations.&lt;/p&gt;
&lt;/section&gt;
&lt;section id="restart"&gt;
&lt;h2&gt;Restart&lt;/h2&gt;
&lt;p&gt;A last resort when the above mechanisms do not work is to regularly
restart the GA whenever the population has converged too much. The
restart mechanism implemented in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; uses the best individual from
the population to re-seed a new population with variations created by
mutation from this best individual. Restarts can be enabled by setting
&lt;code class="docutils literal"&gt;PGASetRestartFlag&lt;/code&gt; in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; or using the &lt;code class="docutils literal"&gt;restart&lt;/code&gt; constructor
parameter in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt;. The frequency (default is every 50 generations) of
restarts can be set with &lt;code class="docutils literal"&gt;PGASetRestartFrequencyValue&lt;/code&gt; in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; and
the &lt;code class="docutils literal"&gt;restart_frequency&lt;/code&gt; constructor parameter in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt;.&lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="footnote-1" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-1"&gt;1&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Ralf Schlatterbeck. &lt;a class="reference external" href="https://blog.runtux.com/posts/2022/12/03/"&gt;Proportional Selection (Roulette-Wheel) in
Genetic Algorithms&lt;/a&gt;. Blog post, Open Source Consulting, December
2022.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-2" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-3"&gt;2&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Georges Harik. Finding multiple solutions in problems of bounded
difficulty. IlliGAL Report 94002, Illinois Genetic Algorithm Lab,
May 1994.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-3" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-4"&gt;3&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Georges R. Harik. Finding multimodal solutions using restricted
tournament selection. In Larry J.  Eshelman, editor, &lt;em&gt;Proceedings
of the International Conference on Genetic Algorithms (ICGA)&lt;/em&gt;,
pages 24–31. Morgan Kaufmann, July 1995.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-4" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-5"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-6"&gt;2&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Martin Pelikan. &lt;em&gt;Hierarchical Bayesian Optimization Algorithm:
Toward a New Generation of Evolutionary Algorithms&lt;/em&gt;, volume 170 of
&lt;em&gt;Studies in Fuzziness and Soft Computing&lt;/em&gt;. Springer, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-5" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-7"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-8"&gt;2&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-9"&gt;3&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Jorge J. Moré, Burton S. Garbow, and Kenneth E. Hillstrom.
&lt;em&gt;Testing unconstrained optimization software.&lt;/em&gt; ACM Transactions
on Mathematical Software, 7(1):17–41, March 1981.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-6" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-10"&gt;6&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient heuristic for global optimization over continuous
spaces. &lt;em&gt;Journal of Global Optimization&lt;/em&gt;, 11(4):341–359, December 1997.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-7" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-11"&gt;7&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient adaptive scheme for global optimization over
continuous spaces. Technical Report TR-95-012, International
Computer Science Institute (ICSI), March 1995.
&lt;a class="reference external" href="ftp://ftp.icsi.berkeley.edu/pub/techreports/1995/tr-95-012.pdf"&gt;Available from the International Computer Science Institute&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-8" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-12"&gt;8&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Kenneth V. Price, Rainer M. Storn, and Jouni A. Lampinen.
&lt;em&gt;Differential Evolution: A Practical Approach to Global Optimization.&lt;/em&gt;
Springer, Berlin, Heidelberg, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-9" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2023/01/06/#footnote-reference-2"&gt;9&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;David E. Goldberg, Kalyanmoy Deb, and James H. Clark. Genetic
algorithms, noise, and the sizing of populations. &lt;em&gt;Complex
Systems&lt;/em&gt;, 6(4):333–362, 1992. &lt;a class="reference external" href="https://wpmedia.wolfram.com/uploads/sites/13/2018/02/06-4-3.pdf"&gt;Available from Complex Systems&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;
&lt;/section&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>howto</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2023/01/06/</guid><pubDate>Fri, 06 Jan 2023 17:45:00 GMT</pubDate></item><item><title>Proportional Selection (Roulette-Wheel) in Genetic Algorithms</title><link>https://blog.runtux.com/posts/2022/12/03/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;p&gt;Some of you know that I'm maintaining &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt;, a genetic algorithm
package and a corresponding Python wrapper, &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGApy&lt;/a&gt;. Recently the
question came up why &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt;, when using &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Fitness_proportionate_selection"&gt;Fitness Proportionate Selection&lt;/a&gt;
(also known as Roulette-Wheel selection and also called proportional
selection) errors out because it &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/issues/7"&gt;cannot normalize the fitness value&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt;, the user supplies a real-valued evaluation function (which
is called for each individual) and specifies if the result of that
evaluation should be minimized or maximized (defining the optimization
direction).&lt;/p&gt;
&lt;p&gt;When using &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Fitness_proportionate_selection"&gt;Fitness proportionate selection&lt;/a&gt;, this evaluation value must be
mapped to a positive value, assigning each evaluation a share on the
roulette-wheel. If the optimization direction is &lt;em&gt;minimization&lt;/em&gt;, small
values are better and need a larger part of the roulette-wheel so that
they have a higher probability of being selected. So we need to remap
the raw evaluation values to a nonnegative and monotonically increasing
&lt;em&gt;fitness&lt;/em&gt;. For a &lt;em&gt;minimization&lt;/em&gt; problem, we're computing the maximum
(worst) evaluation and then the fitness is the difference of this
maximum and the evaluation for that individual (after scaling the
maximum a little so that no fitness value is exactly zero):&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
F = E_{max} - E
\end{equation*}
&lt;/div&gt;
&lt;p&gt;where &lt;span class="math"&gt;\(F\)&lt;/span&gt; is the fitness of the current individual,
&lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt; is the maximum of all evaluations in this generation,
and &lt;span class="math"&gt;\(E\)&lt;/span&gt; is the evaluation of that individual.&lt;/p&gt;
&lt;p&gt;Now when evaluation values differ by several orders of magnitude, it can
happen that the difference in that formula ends up being &lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt;
for many (different) evaluation values. I'm calling this an &lt;em&gt;overflow&lt;/em&gt;
in the error message which is probably not the best name for it.&lt;/p&gt;
&lt;p&gt;That overflow happens when &lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt; is large compared to the current
evaluation value &lt;span class="math"&gt;\(E\)&lt;/span&gt; so that the difference ends up being
&lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt; (i.e. the subtraction of &lt;span class="math"&gt;\(E\)&lt;/span&gt; had no effect).
In the code we check for this condition:&lt;/p&gt;
&lt;pre class="literal-block"&gt;if (cmax == cmax - evalue &amp;amp;&amp;amp; evalue != 0)&lt;/pre&gt;
&lt;p&gt;This condition triggers when subtracting the evaluation &lt;span class="math"&gt;\(E\)&lt;/span&gt; from
the current &lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt; does not change &lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt; even though
&lt;span class="math"&gt;\(E\)&lt;/span&gt; is not zero. So &lt;span class="math"&gt;\(E\)&lt;/span&gt; is so small compared to
&lt;span class="math"&gt;\(E_{max}\)&lt;/span&gt; that the double data type cannot represent the
difference. This happens whenever the &lt;em&gt;units in the last place&lt;/em&gt; (called
&lt;em&gt;ulps&lt;/em&gt; by Goldberg (not the same Goldberg who wrote the Genetic
Algorithms book afaik)) of the &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Significand"&gt;significand&lt;/a&gt; (also called mantissa) is
larger than the value that should be subtracted &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-1" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In our example &lt;span class="math"&gt;\(E_{max} = 1.077688 * 10^{22}\)&lt;/span&gt; and the
evaluation where this failed was &lt;span class="math"&gt;\(E = 10000\)&lt;/span&gt;. The IEEE 754 double
precision floating point format has 53 bit of &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Significand"&gt;significand&lt;/a&gt; which can
represent numbers up to &lt;span class="math"&gt;\(2^{54} - 1 = 18014398509481983\)&lt;/span&gt; or about
&lt;span class="math"&gt;\(1.8 * 10^{16}\)&lt;/span&gt;. So you see that the number 10000 is just below the
&lt;em&gt;ulps&lt;/em&gt;. We can try in python (which uses double for floating-point
values):&lt;/p&gt;
&lt;pre class="literal-block"&gt;&amp;gt;&amp;gt;&amp;gt; 1.077688e22 - 10000 == 1.077688e22
True&lt;/pre&gt;
&lt;p&gt;Why do we make this check in the program? Letting the search continue
with such an overflow (or how we want to call it) would map many
different evaluation values to the same fitness. So the genetic
algorithm could not distinguishing these individuals.&lt;/p&gt;
&lt;p&gt;So what can we do about it when that error happens?&lt;/p&gt;
&lt;p&gt;The short answer: Use a different selection scheme. There is a reason
why the default in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt; is &lt;em&gt;not&lt;/em&gt; proportional selection.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Fitness_proportionate_selection"&gt;Fitness proportionate selection&lt;/a&gt; (aka roulette wheel selection) has other
problems, too. It has too much selection pressure in the beginning and
too low at the end (also mentioned in the Wikipedia article but beware,
I've written parts of it).&lt;/p&gt;
&lt;p&gt;Blickle and Thiele &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-2" id="footnote-reference-2" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; did a mathematical analysis of different
selection schemes and showed that proportional selection is typically not a
good idea (it was historically the first selection scheme and is
described in Goldberg's (the other Goldberg) book &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-3" id="footnote-reference-3" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; which is probably
the reason it is still being used). Note that there is an earlier report
from Blickle and Thiele &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-4" id="footnote-reference-4" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; that is more frank about the use of
proportional selection: "All the undesired properties together led us to
the conclusion that proportional selection is a very unsuited selection
scheme. Informally one can say that the only advantage of proportional
selection is that it is so difficult to prove the disadvantages" (&lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-4" id="footnote-reference-5" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;,
p. 42), they were not as outspoken in the final paper :-)&lt;/p&gt;
&lt;p&gt;We're seeing this in the example above: We have very high differences
between good and bad evaluation (in fact so large that the fitness
cannot be computed, see above). So when using proportional selection the
very good individuals will be selected with too high probability
resulting in &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Premature_convergence"&gt;premature convergence&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That all said: If you're doing optimization with genes of type 'real',
(represented by double values in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt;)
you may want to try Differential Evolution &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-5" id="footnote-reference-6" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-6" id="footnote-reference-7" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-7" id="footnote-reference-8" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;7&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;. At least in
my experiments with antenna optimization &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-8" id="footnote-reference-9" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;8&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; the results outperform the
standard genetic algorithm, but this is reported by several
practitioners &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/12/03/#footnote-7" id="footnote-reference-10" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;7&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;. Examples of how to use this are in
&lt;code class="docutils literal"&gt;examples/deb/optimize.c&lt;/code&gt; or &lt;code class="docutils literal"&gt;examples/mgh/testprogde.c&lt;/code&gt; in
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGApack&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code class="docutils literal"&gt;PGASetDECrossoverProb&lt;/code&gt; is critical, for problems where the dimensions
cannot typically be optimized separately the value should be set close
to 1 but not equal to 1.&lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="footnote-1" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-1"&gt;1&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;David Goldberg. What every computer scientist should know
about floating-point arithmetic. ACM Computing Surveys,
23(1):5–48, 1991.
&lt;a class="reference external" href="https://dl.acm.org/doi/pdf/10.1145/103162.103163"&gt;ACM makes this available for free&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-2" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-2"&gt;2&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Blickle, Tobias; Thiele, Lothar (1996). "A Comparison of Selection
Schemes Used in Evolutionary Algorithms". Evolutionary Computation.
4 (4): 361–394. &lt;a class="reference external" href="https://dl.acm.org/doi/pdf/10.1162/evco.1996.4.4.361"&gt;Available from ACM&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-3" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-3"&gt;3&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;David E. Goldberg. Genetic Algorithms in Search, Optimization &amp;amp;
Machine Learning. Addison Wesley, October 1989.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-4" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-4"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-5"&gt;2&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Tobias Blickle and Lothar Thiele. A comparison of selection
schemes used in genetic algorithms. TIK-Report 11 Version 2,
Computer Engineering and Communications Lab (TIK), December 1995.
&lt;a class="reference external" href="https://tik-old.ee.ethz.ch/file/6c0e384dceb283cd4301339a895b72b8/TIK-Report11.pdf"&gt;Available from ETH&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-5" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-6"&gt;5&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient heuristic for global optimization over continuous
spaces. Journal of Global Optimization, 11(4):341–359, December 1997.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-6" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-7"&gt;6&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient adaptive scheme for global optimization over
continuous spaces. Technical Report TR-95-012, International
Computer Science Institute (ICSI), March 1995.
&lt;a class="reference external" href="ftp://ftp.icsi.berkeley.edu/pub/techreports/1995/tr-95-012.pdf"&gt;Available from the International Computer Science Institute&lt;/a&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-7" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;7&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-8"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-10"&gt;2&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Kenneth V. Price, Rainer M. Storn, and Jouni A. Lampinen.
Differential Evolution: A Practical Approach to Global Optimization.
Springer, Berlin, Heidelberg, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-8" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/12/03/#footnote-reference-9"&gt;8&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Ralf Schlatterbeck. &lt;a class="reference external" href="https://blog.runtux.com/posts/2021/12/27/"&gt;Multi-objective antenna optimization&lt;/a&gt;. Blog post,
Open Source Consulting, December 2021.&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>howto</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2022/12/03/</guid><pubDate>Sat, 03 Dec 2022 15:28:00 GMT</pubDate></item><item><title>Epsilon-Constrained Optimization</title><link>https://blog.runtux.com/posts/2022/08/29/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;p&gt;[Update 2022-10-18: Replace epsilon with delta in description of example
problem 7 in pgapack]&lt;/p&gt;
&lt;p&gt;Many optimization problems involve constraints that valid solutions must
satisfy. A &lt;a class="reference internal" href="https://blog.runtux.com/posts/2022/08/29/#constrained-optimization-problem"&gt;constrained optimization problem&lt;/a&gt;
is typically written as a nonlinear programming problem &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-1" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;div class="math" id="constrained-optimization-problem"&gt;
\begin{align*}
\hbox{Minimize}   \; &amp;amp; f_i(\vec{x}),            &amp;amp; i &amp;amp;= 1, \ldots, I \\
\hbox{Subject to} \; &amp;amp; g_j(\vec{x}) \le 0,      &amp;amp; j &amp;amp;= 1, \ldots, J \\
                     &amp;amp; h_k(\vec{x}) = 0,        &amp;amp; k &amp;amp;= 1, \ldots, K \\
                     &amp;amp; x_m^l \le x_m \le x_m^u, &amp;amp; m &amp;amp;= 1, \ldots, M \\
\end{align*}
&lt;/div&gt;
&lt;p&gt;In this problem, there are &lt;span class="math"&gt;\(n\)&lt;/span&gt; variables (the vector
&lt;span class="math"&gt;\(\vec{x}\)&lt;/span&gt; is of size &lt;span class="math"&gt;\(n\)&lt;/span&gt;), &lt;span class="math"&gt;\(J\)&lt;/span&gt; inequality constraints,
&lt;span class="math"&gt;\(K\)&lt;/span&gt; equality constraints and the variable &lt;span class="math"&gt;\(x_m\)&lt;/span&gt; must be in the
range &lt;span class="math"&gt;\(|x_m^l, x_m^u|\)&lt;/span&gt; (often called a box constraint). The
functions &lt;span class="math"&gt;\(f_i\)&lt;/span&gt; are called the objective functions. If there is more
than one objective function, the problem is said to be a multi
objective optimization problem as described previously in this blog &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-2" id="footnote-reference-2" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;.
In the following I will use some terms that were introduced in that
earlier blog-post without further explanation.&lt;/p&gt;
&lt;p&gt;The objective functions are not necessarily minimized (as in the
formula) but can also be maximized if the best solutions to a problem
requires maximization. Note that the inequality constraints are often
depicted with a &lt;span class="math"&gt;\(\ge\)&lt;/span&gt; sign but the formula can easily be changed
(e.g. by multiplying with -1) to use a &lt;span class="math"&gt;\(\le\)&lt;/span&gt; sign.&lt;/p&gt;
&lt;p&gt;Since it is very hard to fulfil equality constraints, especially if they
involve nonlinear functions of the input variables, equality constraints
are often converted to inequality constraints using an δ‑neighborhood:&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
-\delta \le h_k(\vec{x}) \le \delta
\end{equation*}
&lt;/div&gt;
&lt;p&gt;Where δ is chosen according to the requirements of the problem
for which a solution is sought.&lt;/p&gt;
&lt;p&gt;One very successful method of solving constrained optimization problems
is to consider a lexicographic ordering of constraints and objective
function values. Candidate solutions to the optimization problem are
first sorted by violated constraints (typically the sum over all
violated constraints) and then by the objective function value(s) &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-1" id="footnote-reference-3" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;.
When comparing two individuals during selection in the genetic algorithm
there are three cases: If both individuals violate constraints, the
individual with the lower constraint violation wins. If one violates
constraints and the other does not, the one not violating constraints
wins. In the last case where both individuals do not violate constraints
the normal comparison is used (which depends on the algorithm and if
we're minimizing or maximizing).
This method, originally proposed by Deb &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-1" id="footnote-reference-4" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, is implemented in the
genetic algorithm package I'm maintaining, &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;, and my Python
wrapper &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;PGAPy&lt;/a&gt; for it.&lt;/p&gt;
&lt;p&gt;With this algorithm for handling constraints, the constraints are
optimized first before the algorithm "looks" at the objective
function(s) at all. It often happens that the algorithm ends up
searching in a region of the input space where no good solutions exist
(but no constraints are violated). Hard problems often contain equality
constraints (converted to inequality constraints as indicated earlier)
or other "hard" constraints.  In my previous blog post &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-2" id="footnote-reference-5" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; on
antenna optimization I wrote: "the optimization algorithm has a hard
time finding the director solutions at all. Only in one of a handful
experiments I was able to obtain the pareto front plotted above".&lt;/p&gt;
&lt;p&gt;In that experiment I was running 50 searches and only 5 of them did
&lt;em&gt;not&lt;/em&gt; get stuck in a local optimum. A similar thing happens for the
problem (7) in Deb's paper &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-1" id="footnote-reference-6" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; which has equality constraints. I've
implemented this as &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/deb/deb7.c"&gt;example problem 7&lt;/a&gt; in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;. It only finds a
solution near the (known) optimum when &lt;span class="math"&gt;\(\delta \ge 10^{-2}\)&lt;/span&gt; for
all equality constraints (I didn't experiment with different random
seeds for the optimizer, maybe a better solution would be possible with
a different random seed). In the paper &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-1" id="footnote-reference-7" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, Deb uses
&lt;span class="math"&gt;\(\delta = 10^{-3}\)&lt;/span&gt; for the same reason.&lt;/p&gt;
&lt;p&gt;One method for handling this problem was appealing because it is so
easy to understand and implement:
Takahama and Sakai were first experimenting with a method for relaxing
constraints during the early stages of optization with a formulation
they called an α‑constrained genetic algorithm &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-3" id="footnote-reference-8" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;. They
later simplified the formulation and called the resulting algorithm
ε constrained optimization. It can be applied to different
optimization algorithms, not just genetic algorithms and variants &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-4" id="footnote-reference-9" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;.
Of special interest is the application of the method to differential
evolution &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-5" id="footnote-reference-10" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-6" id="footnote-reference-11" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; but of course it can also be applied to other forms
of genetic algorithms.&lt;/p&gt;
&lt;p&gt;Note that the ε in the name of the algorithm &lt;em&gt;can&lt;/em&gt; be used for
the δ used when converting an equality constraint to inequality
constraints but is not limited to this case.&lt;/p&gt;
&lt;p&gt;During the run of the optimizer in each generation a new value for
ε is computed.  The comparison of individuals outlined above is
modified, so that an individual is handled like it was not violating any
constraints if the constraint violation is below ε. So if both
individuals have constraint violations larger than ε, the one
with lower violation wins. If one violation is below ε and the
other above, the individual with the violation below ε wins. And
finally if the constraint violations of both individuals are below
ε, the normal comparison takes place.&lt;/p&gt;
&lt;p&gt;The last case is the key to the success of this algorithm: Even though
the search proceeds into a direction where the constraint violations are
minimized, &lt;em&gt;at the same time&lt;/em&gt; good solutions in terms of the objective
function are found.&lt;/p&gt;
&lt;p&gt;The algorithm begins by initializing &lt;span class="math"&gt;\(\varepsilon_0\)&lt;/span&gt; with the constraint
violation of the individuum with index &lt;span class="math"&gt;\(\theta\)&lt;/span&gt; from the initial
population sorted by constraint violation, where
&lt;span class="math"&gt;\(\theta\)&lt;/span&gt; is a parameter of the algorithm between 1 and the
population size, a good value uses the individuum at about 20%
of the population size which is also the default in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt;.
In each generation &lt;span class="math"&gt;\(t\)&lt;/span&gt;, &lt;span class="math"&gt;\(\varepsilon_t\)&lt;/span&gt; is computed by&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
\varepsilon_t = \varepsilon_0 \left(1-\frac{t}{T_c}\right)^{cp}
\end{equation*}
&lt;/div&gt;
&lt;p&gt;up to generation &lt;span class="math"&gt;\(T_c\)&lt;/span&gt;. After that generation, ε is set to
0. The exponent &lt;span class="math"&gt;\(cp\)&lt;/span&gt; is between 2 and 10. The 2010 paper &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-6" id="footnote-reference-12" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;
recommends to set &lt;span class="math"&gt;\(cp = 0.3 cp + 0.7 cp_\min\)&lt;/span&gt; at
generation &lt;span class="math"&gt;\(T_\lambda = 0.95 T_c\)&lt;/span&gt; where &lt;span class="math"&gt;\(cp_\min\)&lt;/span&gt; is
the fixed value 3. The initial value of &lt;span class="math"&gt;\(cp\)&lt;/span&gt; is chosen so that
&lt;span class="math"&gt;\(\varepsilon_\lambda=10^{-5}\)&lt;/span&gt; at generation &lt;span class="math"&gt;\(T_\lambda\)&lt;/span&gt; unless
it is smaller than &lt;span class="math"&gt;\(cp_\min\)&lt;/span&gt; in which case it is set to
&lt;span class="math"&gt;\(cp_\min\)&lt;/span&gt;.  &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;PGAPack&lt;/a&gt; implements this
schedule for &lt;span class="math"&gt;\(cp\)&lt;/span&gt; by default but allows to change &lt;span class="math"&gt;\(cp\)&lt;/span&gt;
at start and during run of the optimizer, so it's possible to easily
implement a different schedule for &lt;span class="math"&gt;\(cp\)&lt;/span&gt; – the default works
quite nicely, though.&lt;/p&gt;
&lt;p&gt;With the ε constraint method, example 7 from Deb &lt;a class="brackets" href="https://blog.runtux.com/posts/2022/08/29/#footnote-1" id="footnote-reference-13" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; can be
optimized with a precision of &lt;span class="math"&gt;\(10^{-6}\)&lt;/span&gt; in my experiments, see the
&lt;code class="docutils literal"&gt;epsilon_generation&lt;/code&gt; parameter in the &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack/blob/master/examples/deb/optimize.c"&gt;optimizer example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a class="reference external" href="https://github.com/schlatterbeck/antenna-optimizer"&gt;antenna-optimizer&lt;/a&gt; with an ε‑generation of 50 (that's
the &lt;span class="math"&gt;\(T_c\)&lt;/span&gt; parameter of the algorithm) gets stuck in the local
optimum only in &lt;em&gt;one&lt;/em&gt; of 50 cases, all other cases find good results:&lt;/p&gt;
&lt;iframe class="iframe-embed" width="640" height="480" scrolling="no" frameborder="no" src="https://blog.runtux.com/content/2022/plot-50.html"&gt;
&lt;/iframe&gt;&lt;p&gt;In that picture all the solutions that are dominated by solutions from
another run are drawn in black.  It can be seen that the data from run
number 16 did not contribute any non-dominated solutions (on the right
side in the legend the number 16 is missing). You can turn off the
display of the dominated solutions by clicking on the black dot in the
legend.&lt;/p&gt;
&lt;p&gt;When I increase the ε‑generation to 60, the run with random
seed 16 also finds a solution:&lt;/p&gt;
&lt;iframe class="iframe-embed" width="640" height="480" scrolling="no" frameborder="no" src="https://blog.runtux.com/content/2022/plot-50+60.html"&gt;
&lt;/iframe&gt;&lt;p&gt;We also see that the solutions are quite good (quite near to the pareto
front) for all runs, the black "shadow" of the dominated solutions is
quite near to the real pareto front and it is enough to do a single run
of the algorithm for finding a good set of solutions.&lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="footnote-1" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-1"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-3"&gt;2&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-4"&gt;3&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-6"&gt;4&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-7"&gt;5&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-13"&gt;6&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Kalyanmoy Deb. An efficient constraint handling method for
genetic algorithms. Computer Methods in Applied Mechanics and
Engineering, 186(2–4):311–338, June 2000.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-2" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-2"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-5"&gt;2&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Ralf Schlatterbeck. &lt;a class="reference external" href="https://blog.runtux.com/posts/2021/12/27/"&gt;Multi-objective antenna optimization&lt;/a&gt;.
Blog post, Open Source Consulting, December 2021.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-3" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-8"&gt;3&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Tetsuyuki Takahama and Setsuko Sakai. Constrained optimization
by α constrained genetic algorithm (αGA). Systems
and Computers in Japan, 35(5):11–22, May 2004.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-4" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-9"&gt;4&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Tetsuyuki Takahama and Setsuko Sakai. Constrained optimization
by ε constrained particle swarm optimizer with ε‑level control. In Abraham et al., editors, Soft Computing as
Transdisciplinary Science and Technology, volume 29 of Advances
in Soft Computing, pages 1019–1029. Springer, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-5" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-10"&gt;5&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Tetsuyuki Takahama and Setsuko Sakai. Constrained optimization by
the ε constrained differential evolution with gradient-based
mutation and feasible elites. In IEEE International Conference on
Evolutionary Computation (CEC). Vancouver, BC, Canada, July 2006.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-6" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class="backrefs"&gt;(&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-11"&gt;1&lt;/a&gt;,&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2022/08/29/#footnote-reference-12"&gt;2&lt;/a&gt;)&lt;/span&gt;
&lt;p&gt;Tetsuyuki Takahama and Setsuko Sakai. Constrained optimization by
the ε constrained differential evolution with an archive and
gradient-based mutation. In IEEE Congress on Evolutionary Computation
(CEC), Barcelona, Spain, July 2010.&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>hamradio</category><category>hardware</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2022/08/29/</guid><pubDate>Mon, 29 Aug 2022 16:00:00 GMT</pubDate></item><item><title>Multi-Objective Antenna Optimization</title><link>https://blog.runtux.com/posts/2021/12/27/</link><dc:creator>Ralf Schlatterbeck</dc:creator><description>&lt;p&gt;For quite some time I'm optimizing antennas using genetic algorithms.
I'm using the &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; parallel genetic algorithm package originally
by David Levine from Argonne National Laboratory which I'm maintaining.
Longer than maintaining &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; I'm developing a &lt;a class="reference external" href="https://python.org"&gt;Python&lt;/a&gt; wrapper for
&lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; called &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;pgapy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For the antenna simulation part I'm using Tim Molteno's &lt;a class="reference external" href="https://github.com/tmolteno/python-necpp"&gt;PyNEC&lt;/a&gt;, a python
wrapper for the Numerical Electromagnetics Code (NEC) version 2 written
in C++ (aka NEC++) and wrapped for &lt;a class="reference external" href="https://python.org"&gt;Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Using these packages I've written a small open source framework to
optimize antennas called &lt;a class="reference external" href="https://github.com/schlatterbeck/antenna-optimizer"&gt;antenna-optimizer&lt;/a&gt;.  This can use
traditional genetic algorithm method with bit-strings as genes as well
as a floating-point representation with operators suited for
floating-point genes.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;parallel&lt;/em&gt; in &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; tells us that the evaluation function of the
genetic algorithm can be parallelized. When optimizing antennas we
simulate each candidate parameters for an antenna using the antenna
simulation of PyNEC. Antenna simulation is still (the original NEC code
is from the 1980s and was conceived using punched cards for I/O) a
CPU-intensive undertaking. So the fact that with &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; we can run
many simulations in parallel using the message passing interface
(MPI) standard &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-1" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; is good news.&lt;/p&gt;
&lt;p&gt;For &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; – and also for &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapy"&gt;pgapy&lt;/a&gt; – I've recently implemented some
classic algorithms that have proven very useful over time:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;p&gt;Differential Evolution &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-2" id="footnote-reference-2" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;2&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-3" id="footnote-reference-3" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;3&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;, &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-4" id="footnote-reference-4" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;4&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; is a very successful
optimization algorithm for floating-point genes that is very
interesting for electromagnetics problems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The elitist Nondominated Sorting Genetic Algorithm NSGA-II &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-5" id="footnote-reference-5" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;5&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;
allows to optimize multiple objectives in a single run of the optimizer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can have constraints on the optimization using constraint functions
that are minimized. For a solution to be valid, all constraints must
be zero or negative. &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-6" id="footnote-reference-6" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;6&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Traditionally with genetic algorithms only a single evaluation function,
also called &lt;em&gt;objective function&lt;/em&gt; is possible. With NSGA-II it is
possible to have several objective functions. We call such an algorithm
a &lt;em&gt;multi-objective&lt;/em&gt; optimization algorithm.&lt;/p&gt;
&lt;p&gt;For antenna simulation this means that we don't need to combine
different antenna criteria like gain, forward/backward ratio, and
standing wave ratio (VSWR) into a single evaluation function which I was
using in &lt;a class="reference external" href="https://github.com/schlatterbeck/antenna-optimizer"&gt;antenna-optimizer&lt;/a&gt;, but instead we can specify them
separately and leave the optimization to the genetic search.&lt;/p&gt;
&lt;p&gt;With multiple objectives, however, typically when a solution is better
in one objective, it can be worse in another objective and vice-versa.
So we are searching for solutions that are &lt;em&gt;strictly&lt;/em&gt; better than other
solutions. A solution is said to &lt;em&gt;dominate&lt;/em&gt; another solution when it is
strictly better in one objective but not worse in any other objective.
All solutions that fulfill this criterion are said to be
&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Pareto_efficiency"&gt;pareto-optimal&lt;/a&gt; named after the italian scientist &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Vilfredo_Pareto"&gt;Vilfredo
Pareto&lt;/a&gt; who first defined the concept of pareto optimality. All
solutions that fulfill the pareto optimality criterion are said to lie
on a &lt;em&gt;pareto front&lt;/em&gt;. For two objectives the pareto front can be shown in
a scatter-plot as we will see below.&lt;/p&gt;
&lt;p&gt;Since &lt;a class="reference external" href="https://github.com/schlatterbeck/pgapack"&gt;pgapack&lt;/a&gt; follows a "mix and match" approach to genetic algorithms
we can combine successful strategies for different parts of a genetic
algorithm:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;p&gt;We can use Differential Evolution just for the mutation/crossover part
of the genetic algorithm&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can combine this with the nondominated sorting replacement of
NSGA-II&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can define some of our objectives as constraints. For our problem
it makes sense to only allow antennas that do not exceed a given
standing-wave ratio. So we do not allow antennas with a VSWR &amp;gt; 1.8.
The necessary constraint function is &lt;span class="math"&gt;\(S - 1.8 \le 0\)&lt;/span&gt; where
&lt;span class="math"&gt;\(S\)&lt;/span&gt; is the voltage standing wave ratio (VSWR).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With this combination we can successfully compute antennas for the 70cm
ham-radio band (430 MHz - 440 MHz). The antenna uses what we call a
folded dipole (the thing with the rounded corners) and a straight
element. The measures in the figure represent the lenghts optimized by
the genetic algorithm. The two dots in the middle of the folded dipole
element represent the point where the antenna feed-line is connected.&lt;/p&gt;
&lt;img alt="/images/2ele.png" src="https://blog.runtux.com/images/2ele.png"&gt;
&lt;p&gt;A first example simulates antenna
parameters for the lowest, the highest and the medium frequency. The
gain and forward/backward ratio are computed for the medium frequency
only:&lt;/p&gt;
&lt;img alt="/images/twoele-v1.png" src="https://blog.runtux.com/images/twoele-v1.png"&gt;
&lt;p&gt;In this graph (a scatter plot) the first objective (the gain) is graphed
against the second objective, the forward/backward ratio. All numbers
are taken from the medium frequency. Each dot represents a simulated
antenna. All antennas have a VSWR lower than 1.8 on the minimum, medium,
and maximum frequency.&lt;/p&gt;
&lt;p&gt;With this success I was experimenting with different settings of the
Differential Evolution parameters. It is well-known that Differential
Evolution performance on decomposable problems is better with a low
crossover-rate, while it is better on non-decomposable problems with a
high crossover rate. A decomposable problem is one where the different
dimensions can be optimized separately, this was first observed by
Salomon in 1996 &lt;a class="brackets" href="https://blog.runtux.com/posts/2021/12/27/#footnote-7" id="footnote-reference-7" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;7&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt;. I had been using a crossover-rate of 0.2 and my
hope was that the optimization would be better and faster with a higher
crossover rate. The experiment below uses a crossover-rate of 0.9.&lt;/p&gt;
&lt;p&gt;In addition I was experimenting with &lt;em&gt;dither&lt;/em&gt;: Differential Evolution
allows to randomly change the scale-factor &lt;span class="math"&gt;\(F\)&lt;/span&gt;, by which the
difference of two vectors is multiplied slightly for each generated
variation. In the first implementation I was setting &lt;em&gt;dither&lt;/em&gt; to 0, now
I had a dither of 0.2. Imagine my surprise when with these settings I
found a completely different Pareto front for the solution:&lt;/p&gt;
&lt;img alt="/images/twoele-v2.png" src="https://blog.runtux.com/images/twoele-v2.png"&gt;
&lt;p&gt;To make it easier to see that the second discovered front completely
dominates the front that was first discovered, I've plotted the two
fronts into a single graph:&lt;/p&gt;
&lt;img alt="/images/twoele-v3.png" src="https://blog.runtux.com/images/twoele-v3.png"&gt;
&lt;p&gt;Now since the second discovered front looks too good to be true (over
the whole frequency range) for a two-element antenna, lets take a look
what is happening here. First we show the orientation of the antenna and
the computed gain pattern for one of the antennas from the middle of the
lower front:&lt;/p&gt;
&lt;img alt="/images/middle-lower-antenna.png" src="https://blog.runtux.com/images/middle-lower-antenna.png" style="width: 45%;"&gt;
&lt;img alt="/images/middle-lower-pattern.png" src="https://blog.runtux.com/images/middle-lower-pattern.png" style="width: 45%;"&gt;
&lt;p&gt;The antenna has – as already indicated in the pareto-front graphics
– a gain of about 6.6 dBi and a forward/backward ratio of about
11 dB in the middle of the band at 435 MHz.
The colors on the antenna denote the currents on the antenna structure.
If you want to look at this yourself, here is a link to the &lt;a class="reference external" href="https://blog.runtux.com/content/2021/middle-lower.nec"&gt;NEC input
file for antenna 1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now lets compare this with one of the antennas of the "orange front",
where we get a lot better values:&lt;/p&gt;
&lt;img alt="/images/middle-higher-antenna.png" src="https://blog.runtux.com/images/middle-higher-antenna.png" style="width: 45%;"&gt;
&lt;img alt="/images/middle-higher-pattern.png" src="https://blog.runtux.com/images/middle-higher-pattern.png" style="width: 45%;"&gt;
&lt;p&gt;This antenna is in the middle of the pareto front above and has
a gain of about 6.7 dBi and a forward/backward ratio of about
16 dB in the middle of the band at 435 MHz. Can you spot the
difference to the first antenna? Yes: The maximum gain is in the
opposite direction of the first antenna. We say that for the first
antenna the straight element acts as a &lt;em&gt;reflector&lt;/em&gt; while for the second
antenna it acts as a &lt;em&gt;director&lt;/em&gt;.
If you want to look at this yourself, here is a link to the &lt;a class="reference external" href="https://blog.runtux.com/content/2021/middle-higher.nec"&gt;NEC input
file for antenna 2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we look at the frequency plot of gain and forward/backward ratio of
the antennas, the plot for the first antenna (with the &lt;em&gt;reflector&lt;/em&gt;
element) is on the left, while the plot for the antenna with the
&lt;em&gt;director&lt;/em&gt; element is on the right.&lt;/p&gt;
&lt;img alt="/images/middle-lower-fplot.png" src="https://blog.runtux.com/images/middle-lower-fplot.png" style="width: 45%;"&gt;
&lt;img alt="/images/middle-higher-fplot.png" src="https://blog.runtux.com/images/middle-higher-fplot.png" style="width: 45%;"&gt;
&lt;p&gt;We see that the forward/backward ratio of the &lt;em&gt;director&lt;/em&gt; antenna ranges
from more than 10 dB to more than 25 dB while the &lt;em&gt;reflector&lt;/em&gt;
design ranges from 9.3 dB to 11.75 dB. For the minimum gain the
&lt;em&gt;reflector&lt;/em&gt; design is slightly better (from 6.35-6.85 dBi vs.
6.3-7.05 dBi). So this needs further experiments. When forcing a
&lt;em&gt;reflector&lt;/em&gt; design and changing the evaluation function to return the
&lt;em&gt;minimum&lt;/em&gt; gain and F/B ratio over the three (start, middle, end)
frequencies we get:&lt;/p&gt;
&lt;img alt="/images/twoele-reflector.png" src="https://blog.runtux.com/images/twoele-reflector.png"&gt;
&lt;p&gt;The same for a &lt;em&gt;director&lt;/em&gt; design (also with the &lt;em&gt;minimum&lt;/em&gt; gain and F/B
ratio over the three frequencies start, middle, end) we get:&lt;/p&gt;
&lt;img alt="/images/twoele-director.png" src="https://blog.runtux.com/images/twoele-director.png"&gt;
&lt;p&gt;With these result, the sweet spot for an antenna to build is probably at
or above 10 dB F/B ratio and a gain of about 6.2 dBi. Going for some
1/10 dBi more gain and sacrificing several dB of F/B ratio doesn't
seem sensible. Comparing the director vs. reflector design we notice
(contrary to at least &lt;em&gt;my&lt;/em&gt; intuition) that the director design has a
better F/B ratio over the whole frequency range. If, however the antenna
is to be used for relay operation, where the sending frequency (the
relay input) is in the lower half of the frequency range and the relay
output (the receiving frequency) is in the upper half, we will probably
chose a &lt;em&gt;reflector&lt;/em&gt; design because there the gain is higher when sending
and the F/B ratio is higher when receiving (compare the two earlier
gain and F/B ratio plots).&lt;/p&gt;
&lt;p&gt;Also note that the optimization algorithm has a hard time finding the
director solutions at all. Only in one of a handful experiments I was
able to obtain the pareto front plotted above. The design is more
narrowband than the reflector design and the algorithm often converges
to a local optimimum. The higher difference in  gain and F/B range of
the director design also tells us that it will be harder to build: Not
getting the dimensions exactly right will probably not reach the
predicted simulation results. The reflector design is a little more
tolerant in this regard.&lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="footnote-1" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-1"&gt;1&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;MPI: A message-passing interface standard, version 4.0.
Standard, &lt;a class="reference external" href="https://www.mpi-forum.org/"&gt;Message Passing Interface Forum&lt;/a&gt;, June 2021.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-2" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-2"&gt;2&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient adaptive scheme for global optimization over
continuous spaces. Technical Report TR-95-012, International
Computer Science Institute (ICSI), March 1995.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-3" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-3"&gt;3&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Rainer Storn and Kenneth Price. Differential evolution – a simple
and efficient heuristic for global optimization over continuous spaces.
Journal of Global Optimization, 11(4):341–359, December 1997.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-4" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-4"&gt;4&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Kenneth V. Price, Rainer M. Storn, and Jouni A. Lampinen.
Differential Evolution: A Practical Approach to Global
Optimization.  Springer, Berlin, Heidelberg, 2005.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-5" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-5"&gt;5&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Kalyanmoy Deb, Amrit Pratap, Sameer Agarwal, and T. Meyarivan.
A fast and elitist multiobjective genetic algorithm: NSGA-II.
IEEE Transactions on Evolutionary Computation, 6(2):182–197,
April 2002.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-6" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-6"&gt;6&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Kalyanmoy Deb. An efficient constraint handling method for genetic
algorithms. Computer Methods in Applied Mechanics and Engineering,
186(2–4):311–338, June 2000.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class="footnote brackets" id="footnote-7" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://blog.runtux.com/posts/2021/12/27/#footnote-reference-7"&gt;7&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Ralf Salomon. Re-evaluating genetic algorithm performance under
coordinate rotation of benchmark functions. a survey of some
theoretical and practical aspects of genetic algorithms.
Biosystems, 39(3):263–278, 1996.&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;</description><category>documentation</category><category>english</category><category>genetic algorithms</category><category>hamradio</category><category>hardware</category><category>howto</category><category>open source</category><category>optimization</category><guid>https://blog.runtux.com/posts/2021/12/27/</guid><pubDate>Mon, 27 Dec 2021 17:05:00 GMT</pubDate></item></channel></rss>