avatarThe Pragmatic Programmers

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

1543

Abstract

yword">end</span>​ ​ »​<span class="hljs-function"><span class="hljs-keyword">def</span><span class="hljs-title">evolve</span></span>(population, problem, generation, opts \ []) ​<span class="hljs-keyword">do</span>​ ​ ... » ​<span class="hljs-keyword">if</span>​ terminate?(population, generation) ​<span class="hljs-keyword">do</span>​ ​ ... ​ |> mutation(opts) » |> evolve(problem, generation+<span class="hljs-number">1</span>, opts) ​ ​<span class="hljs-keyword">end</span></pre></div><p id="10d0">Now, implement your termination criteria like this:</p><div id="4214"><pre>​ ​def​ <span class="hljs-keyword">terminate</span>?(_population, generation), ​<span class="hljs-keyword">do</span>​: generation == <span class="hljs-number">1000</span></pre></div><p id="c318">Now, run your algorithm:</p><div id="09ac"><pre>​ ​$ ​​mix​​ ​​<span class="hljs-built_in">run</span>​​ ​​scripts/cargo.exs​ ​ Current Best: <span class="hljs-number">35</span> ​ [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>] ​ Weight <span class="hljs-keyword">is</span>: <span class="hljs-number">38</span></pre></div><p id="d6bb">While your profit definitely went down, your cargo configuration stayed under the weight limit. You may f

Options

ind if you run this algorithm multiple times, your configuration will vary slightly. Remember, genetic algorithms are subject to randomness, so your solutions will vary. Try to run it mulitple times to see what the best configuration is.</p><p id="89b3">As you can see, how you evaluate solutions significantly impacts the outcome of your algorithms. In this problem, you examined one specific way to evaluate one specific type of problem. Unfortunately, there are countless classes of problems out there. In the next section, you’ll gain a better understanding of fitness and fitness functions, and you’ll learn about some other types of optimization problems and how they’re evaluated.</p><p id="e829"><i>👈 <a href="https://readmedium.com/defining-termination-criteria-d350e21b7319">Defining Termination Criteria</a> | <a href="https://readmedium.com/table-of-contents-879fc8614df">TOC</a> | <a href="https://readmedium.com/crafting-fitness-functions-d8af1ede8127">Crafting Fitness Functions</a> 👉</i></p><p id="b3f0"><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="b1c1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*U2E2a23SuM4520w9CUXSWw.jpeg"><figcaption></figcaption></figure></article></body>

Applying Termination Criteria to Shipping

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

👈 Defining Termination Criteria | TOC | Crafting Fitness Functions 👉

With three possible means of determining when to stop, you need to decide which one works best for your cargo problem. The simplest is to stop after a certain number of generations. Of course, you could use a temperature mechanism; however, it’s too complicated for the task at hand.

Ensure the terminate? callback in the Problem module is set up to accept a generation argument. Additionally, ensure run and evolve are configured to track generations:

​ ​# problem.ex​@callback terminate?(Enum.t(), integer()) :: boolean()
​ 
​ ​# genetic.ex​
​ ​defrun(problem, opts \\ [])
​   population = initialize(&problem.genotype/0, opts)
​   population
»  |> evolve(problem, 0, opts)
​ ​end​
​ 
»​defevolve(population, problem, generation, opts \\ []) ​do​
​   ...
»  ​if​ terminate?(population, generation) ​do​
​   ...
​   |> mutation(opts)
»  |> evolve(problem, generation+1, opts)
​ ​end

Now, implement your termination criteria like this:

​ ​def​ terminate?(_population, generation), ​do​: generation == 1000

Now, run your algorithm:

​ ​$ ​​mix​​ ​​run​​ ​​scripts/cargo.exs​
​ Current Best: 35
​ [0, 1, 1, 1, 0, 1, 0, 0, 0, 1]
​ Weight is: 38

While your profit definitely went down, your cargo configuration stayed under the weight limit. You may find if you run this algorithm multiple times, your configuration will vary slightly. Remember, genetic algorithms are subject to randomness, so your solutions will vary. Try to run it mulitple times to see what the best configuration is.

As you can see, how you evaluate solutions significantly impacts the outcome of your algorithms. In this problem, you examined one specific way to evaluate one specific type of problem. Unfortunately, there are countless classes of problems out there. In the next section, you’ll gain a better understanding of fitness and fitness functions, and you’ll learn about some other types of optimization problems and how they’re evaluated.

👈 Defining Termination Criteria | TOC | Crafting Fitness Functions 👉

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