avatarThe Pragmatic Programmers

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1589

Abstract

an> _ <- <span class="hljs-number">1</span>..<span class="hljs-number">42</span>, ​<span class="hljs-keyword">do</span>​: <span class="hljs-title class_">Enum</span>.random(<span class="hljs-number">0</span>..<span class="hljs-number">1</span>) ​ %<span class="hljs-title class_">Chromosome</span>{​<span class="hljs-symbol">genes:</span>​ genes, ​<span class="hljs-symbol">size:</span><span class="hljs-number">42</span>} ​ ​<span class="hljs-keyword">end</span>​ ​ ​ <span class="hljs-variable">@impl</span> <span class="hljs-literal">true</span> ​ ​<span class="hljs-function"><span class="hljs-keyword">def</span><span class="hljs-title">fitness_function</span></span>(chromosome), ​<span class="hljs-keyword">do</span>​: <span class="hljs-title class_">Enum</span>.sum(chromosome.genes) ​ ​ <span class="hljs-variable">@impl</span> <span class="hljs-literal">true</span> ​ ​<span class="hljs-function"><span class="hljs-keyword">def</span><span class="hljs-title">terminate?</span></span>([best | _]), ​<span class="hljs-keyword">do</span>​: best.fitness == <span class="hljs-number">42</span> ​ ​<span class="hljs-keyword">end</span></pre></div><p id="6fd4">By now, you should be familiar with these functions, as they are identical to the functions you implemented in your previous attempts at solving the One-Max problem. This time, however, they fit within your problem behaviour.</p><p id="b1b3">To run your solution, add the following code below your module definition:</p><div id="b4c8"><pre>​ soln = Genetic<span class="hljs-selector-class">.run</spa

Options

n>(OneMax) ​ ​ IO<span class="hljs-selector-class">.write</span>(​<span class="hljs-string">"​​\n"</span>​) ​ IO<span class="hljs-selector-class">.inspect</span>(soln)</pre></div><p id="f2e9">Now, run one_max.exs:</p><div id="e925"><pre>​ ​$ ​​mix​​ ​​run​​ ​​scripts/one_max.exs​ ​ <span class="hljs-keyword">Current</span> Best: <span class="hljs-number">42</span><span class="hljs-meta">%Type</span>s.Chromosome{ ​ age: <span class="hljs-number">1</span>, ​ fitness: <span class="hljs-number">42</span>, ​ genes: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>,.<span class="hljs-number">.1</span>], ​ size: <span class="hljs-number">42</span> ​ }</pre></div><p id="bbe7"><i>👈 <a href="https://readmedium.com/understanding-and-choosing-genotypes-e0062249641d">Understanding and Choosing Genotypes</a> | <a href="https://readmedium.com/table-of-contents-879fc8614df">TOC</a> | <a href="https://readmedium.com/spelling-words-with-genetic-algorithms-9bfbd77eb3ec">Spelling Words with Genetic Algorithms</a> 👉</i></p><p id="4417"><i>Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats <a href="https://pragprog.com/titles/smgaelixir">directly from the Pragmatic Programmers</a>. If you notice a code error or formatting mistake, please let us know <a href="https://readmedium.com/how-to-report-errata-4e164674347a">here</a> so that we can fix it.</i></p><figure id="179d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*U2E2a23SuM4520w9CUXSWw.jpeg"><figcaption></figcaption></figure></article></body>

Solving One-Max for the Last Time

Genetic Algorithms in Elixir — by Sean Moriarity (31 / 101)

👈 Understanding and Choosing Genotypes | TOC | Spelling Words with Genetic Algorithms 👉

To get your feet wet with the Problem behaviour you created in this chapter, start small by solving the One-Max problem. You already know what the three problem-specific parameters should look like; all you have to do is fit them into the framework you defined in this chapter.

Open the scripts/one_max.exs file and replace the contents with this:

​ ​defmoduleOneMaxdo​
​   @behaviour Problemalias Types.Chromosome
​ 
​   @impl true
​   ​defgenotypedo​
​     genes = for _ <- 1..42, ​do​: Enum.random(0..1)
​     %Chromosome{​genes:​ genes, ​size:42}
​   ​end​
​ 
​   @impl true
​   ​deffitness_function(chromosome), ​do​: Enum.sum(chromosome.genes)
​ 
​   @impl true
​   ​defterminate?([best | _]), ​do​: best.fitness == 42
​ ​end

By now, you should be familiar with these functions, as they are identical to the functions you implemented in your previous attempts at solving the One-Max problem. This time, however, they fit within your problem behaviour.

To run your solution, add the following code below your module definition:

​ soln = Genetic.run(OneMax)
​ 
​ IO.write(​"​​\n"​)
​ IO.inspect(soln)

Now, run one_max.exs:

​ ​$ ​​mix​​ ​​run​​ ​​scripts/one_max.exs​
​ Current Best: 42%Types.Chromosome{
​   age: 1,
​   fitness: 42,
​   genes: [1, 1,..1],
​   size: 42
​ }

👈 Understanding and Choosing Genotypes | TOC | Spelling Words with Genetic Algorithms 👉

Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Smgaelixir
Recommended from ReadMedium