<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="283442" data-post-id="283442">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="stevensonmt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/120/20503_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  stevensonmt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Below are my stream of consciousness notes on Chapter 2. Overall not a lot of new meat to chew in this chapter, but I appreciate the time taken to ensure the basic concepts are established before moving into the deep end.</p>
<h1><a name="p-283442-ch-2-1" class="anchor" href="#p-283442-ch-2-1" aria-label="Heading link" rel="nofollow"></a>Ch 2</h1>
<p>Populations should use data structures that implement <code>Enumerable</code> protocol.<br>
Using <code>List</code> as default will be simplest form of population.</p>
<ul>
<li>
<p>Initialize population</p>
<ul>
<li>Random list of <code>chromosomes</code> (i.e., possible solutions)</li>
<li>Initialization function should be generic (i.e., not care structure used by chromosome)</li>
</ul>
</li>
<li>
<p>Evaluate population</p>
<ul>
<li>Apply the fitness function (i.e., sort population according to fitness criteria)</li>
<li>Fitness will be defined unique to each problem</li>
<li><code>population -&gt; fitness.() -&gt; sorted_population</code></li>
</ul>
</li>
<li>
<p>Select parents</p>
<ul>
<li>Elitism selection pairs strongest chromosomes as parents</li>
<li>This is the point my background gets in the way of the metaphor. In biology, a population is a collection of organisms. Each organism can have one or many chromosomes. Each chromosome can have one or many genes. Each gene can have one or many values. Unclear if this is just the simplest implementation of a genetic algorithm and in future chapters or more advanced implementations the additional nested structure is utilized more consistent with the biology metaphor.</li>
<li><code>population -&gt; transform_into_parent_pairs.() -&gt; population_ready_for_crossover</code></li>
</ul>
</li>
<li>
<p>Creating children</p>
<ul>
<li>Apply the crossover function to parents</li>
<li><code>parents -&gt; crossover.() -&gt; children</code> where size(children) == size(initial_pop)</li>
</ul>
</li>
<li>
<p>Mutation</p>
<ul>
<li>Prevents premature convergence</li>
<li><code>population -&gt; mutate.() -&gt; x_men</code> where <code>mutate</code> only affects a small % of population</li>
</ul>
</li>
<li>
<p>Termination criteria</p>
<ul>
<li>criteria are problem-specific</li>
<li>simplest case is reaching desired value known ahead of time (max value in one-max e.g.)</li>
</ul>
</li>
<li>
<p>Hyperparameters</p>
<ul>
<li>set prior to training algorithm</li>
<li>includes pop size, mutation rate, etc</li>
<li>use optional list of parameters in keyword list to allow rapidly adjusting these params<br>
(I would have instinctively used module attributes, but this is more opaque and less<br>
generic for a framework)</li>
</ul>
</li>
</ul>
<p>Using <code>mix run &lt;project_root&gt;/scripts/&lt;script&gt;.exs</code> is a pretty cool structure for evaluating examples using a lib.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283442" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-283442" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283442"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="283501" data-post-id="283501">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="AndrGab" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/AndrGab/120/23208_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  AndrGab
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi <a class="mention" href="/u/astonj" rel="nofollow">@AstonJ</a><br>
I have a Biomedical Science Degree in Brazil and I’m a elixir developer.<br>
I’d like to help the project. How can I do that?<br>
Thank you</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283501" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-283501" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283501"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #33"></div>
  </section>
</div>
    <div class="postbit" id="283507" data-post-id="283507">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="AstonJ" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/AstonJ/120/15_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  AstonJ
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi Andre, welcome! <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>In what way would you like to help? Would you like to join this book club? <img src="https://forum.elixirforum.com/images/emoji/apple/blush.png?v=15" title=":blush:" class="emoji" alt=":blush:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283507" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-283507" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283507"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #34"></div>
  </section>
</div>
    <div class="postbit" id="283536" data-post-id="283536">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cjbottaro" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cjbottaro/120/1469_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cjbottaro
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I finished chapters 1-4 last night. It went pretty quickly because I could skip everything regarding the Elixir language itself. I almost (selfishly) wished it didn’t try to teach Elixir.</p>
<p>I noticed a few strange paradigms…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{{h1, t1}, {h2, t2}}
  = {Enum.split(p1, cx_point),
     Enum.split(p2, cx_point)}
</code></pre>
<p>Seems much more readable to me like this…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{h1, t1} = Enum.split(p1, cx_point)
{h2, t2} = Enum.split(p2, cx_point)
</code></pre>
<p>Also the “your select() function should produce something easy for your crossover() function to consume, hence use tuples”…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">population = Enum.chunk_every(population, 2)
|&gt; Enum.map(&amp;List.to_tuple/1)
...
Enum.reduce(population, [], fn {p1, p2}, acc -&gt; ... end)
</code></pre>
<p>Why not just pattern match on a 2-element list and skip the map iteration?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">population = Enum.chunk_every(population, 2)
...
Enum.reduce(population, [], fn [p1, p2], acc -&gt; ... end)
</code></pre>
<p>As for the genetic algorithm material itself… love it! It’s extremely accessible. The author makes it so easy and intuitive to understand.</p>
<p>I was surprised to learn that genotypes are usually pretty simply (most commonly just a bitstring). I would have imagined they would get pretty complex like, structs and recursive data structures and what not. There are the tree/graph genotypes, but he said they aren’t very effective.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283536" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-283536" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283536"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #38"></div>
  </section>
</div>
    <div class="postbit" id="283555" data-post-id="283555">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="m4hi2" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/m4hi2/120/23554_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  m4hi2
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>I noticed a few strange paradigms…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{{h1, t1}, {h2, t2}}
  = {Enum.split(p1, cx_point),
     Enum.split(p2, cx_point)}
</code></pre>
<p>Seems much more readable to me like this…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{h1, t1} = Enum.split(p1, cx_point)
{h2, t2} = Enum.split(p2, cx_point)
</code></pre>
</blockquote>
<p>I was away from elixir for almost a year and this part got me scratching my head. The way you wrote is easier to read.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283555" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-283555" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283555"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #39"></div>
  </section>
</div>
    <div class="postbit" id="283699" data-post-id="283699">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="stevensonmt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/120/20503_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  stevensonmt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>More stream of consciousness notes on the third chapter. Starting to get more interesting as I can tell by more of my notes ending in question marks. <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<h1><a name="p-283699-ch-3-1" class="anchor" href="#p-283699-ch-3-1" aria-label="Heading link" rel="nofollow"></a>Ch 3</h1>
<p>What sort of problem would the age of chromosome determine fitness? Is this simply another method of preventing premature convergence? Or just a way to keep an algorithm from running forever when an optimal solution can’t be pre-defined?</p>
<blockquote>
<p>Genes are typically represented using list types or other enumberable data types, like trees, sets, and arrays.</p>
</blockquote>
<p>Not sure about trees, but arrays, at least the <code>:array</code> implementation from Erlang, do not implement <code>Enumerable</code>. I guess you could implement it manually. Author probably just meant arrays more generically.</p>
<ul>
<li>Structs for chromosomes
<ul>
<li>needs fields for fitness, size, age, genes as basic foundation</li>
<li>why is size a basic need?</li>
<li>using <code>@enforce_keys</code> for fields that don’t have a default defined but that shouldn’t be <code>nil</code> is an obvious technique that I had not even realized was happening in other code I’ve read</li>
</ul>
</li>
</ul>
<p>I have no formal comp sci education, but I’ve been reading about programming and practicing writing programs for about 6 years off and on. This is probably the first time I’ve seen anyone actually define the term <strong>abstraction</strong> in the context of programming and explicitly state the purpose an abstraction should serve.</p>
<blockquote>
<p>The purpose of abstraction is sto force you to think of things at different levels of specificity.</p>
</blockquote>
<h2><a name="p-283699-behaviours-are-a-contract-2" class="anchor" href="#p-283699-behaviours-are-a-contract-2" aria-label="Heading link" rel="nofollow"></a>Behaviours are a contract</h2>
<ul>
<li>Behaviours enforce specifications by requiring certain functions be implemented</li>
<li>Callbacks are function signatures with return types</li>
<li>Since genetic algos require genotypes, fitness functions, and termination criteria, we need callbacks for all 3
<ul>
<li>all examples in book will use numbers for fitness but not necessary, only need to be sortable in some way</li>
</ul>
</li>
</ul>
<h2><a name="p-283699-encoding-3" class="anchor" href="#p-283699-encoding-3" aria-label="Heading link" rel="nofollow"></a>Encoding</h2>
<ul>
<li>Encoding = choice of data type to represent solutions
<ul>
<li>should not have extraneous information</li>
</ul>
</li>
<li>Importance of choosing correct and sparse encoding does not seem unique to genetic algos but I appreciate the point being emphasized here as it’s something I continually struggle with</li>
<li>Reducing solutions to collections of binary representations is simplest and allows bitstring as genotype
<ul>
<li>People are always doing clever things with bitwise that I don’t grok, so I’m curious how much that sort of thing is going to come up in future chapters</li>
</ul>
</li>
<li>Permutation also common and probably more intuitive to me</li>
<li>Real Value genotype feels inappropriately named so I must be missing something. The examples show a collection of genes with real-value alleles. Whereas permutation the entire collection of genes, i.e., the genotype, is what’s assessed.</li>
<li>Just going to ignore tree/graph genotypes but sounds very interesting from academic perspective</li>
</ul>
<h2><a name="p-283699-onemax-4" class="anchor" href="#p-283699-onemax-4" aria-label="Heading link" rel="nofollow"></a>OneMax</h2>
<p>Nothing particularly interesting going on here.</p>
<h2><a name="p-283699-speller-5" class="anchor" href="#p-283699-speller-5" aria-label="Heading link" rel="nofollow"></a>Speller</h2>
<ul>
<li><code>Stream.repeatedly</code> + <code>Enum.random</code> is another little nugget with obvious utility that I see all the time and immediately forget whenever I could actually use it</li>
<li>Increasing population size and mutation rate did not help convergence</li>
</ul>
<p>Generic note: using module name as parameter and then dot notation to call functions from that module – another light bulb moment for me.</p>
<p>Already seeing that this book is teaching me not only the specifics of genetic algorithms but some Elixir specific practices that I probably should have picked up by now.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283699" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/41">Post #40</a>
	                </div>
	            </div>
              <div id="likers-container-283699" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283699"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #40"></div>
  </section>
</div>
    <div class="postbit" id="283723" data-post-id="283723">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cjbottaro" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cjbottaro/120/1469_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cjbottaro
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I tried to make a sudoku solver using what I learned from Ch 1-5 of the book. It doesn’t work… <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"> I let it run for about 20k generations and the score didn’t seem like it was moving.</p>
<p>Fitness is the count of rows and columns that all contain unique numbers (i.e. the count of solved rows/cols, so a score of 18 means solved). My crossover is the same as the OneMax example… which is think is the culprit.</p>
<p>Any ideas? I Googled and mostly it’s just code examples. Not really any solutions in plain English. There is one blog post, but his took 2.6 million generations to solve. The top Google hit is a Python example that got it in dozens of generations… <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283723" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/42">Post #41</a>
	                </div>
	            </div>
              <div id="likers-container-283723" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283723"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #41"></div>
  </section>
</div>
    <div class="postbit" id="283766" data-post-id="283766">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="stevensonmt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/120/20503_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  stevensonmt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>What are you using for the fitness function and how did you model the chromosome?</p>
<p>This <a href="https://micsymposium.org/mics_2009_proceedings/mics2009_submission_66.pdf" rel="noopener nofollow ugc">article</a> suggests sudoku is generally a difficult problem space for genetic algorithms.</p>
<p>I’m running into an issue trying to encode possible solutions as bitstrings where each spot on the grid is encoded with its index and value as &lt;&lt;index::8, value::4&gt;&gt; so that the total for each spot is 12 bits, and the total for a 9x9 puzzle is 12 * 81 = 972 bits. I had thought with this encoding I could check that randomly generated solutions matched the positions given in the puzzle input via</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">&lt;&lt;solution::972&gt;&gt; = encoded_solution
&lt;&lt;puzzle::972&gt;&gt; = encoded_puzzle
(solution &amp;&amp;&amp; puzzle) == puzzle
</code></pre>
<p>but this ends up being way too inefficient due to the number of combinations that are invalid.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283766" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-283766" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283766"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #42"></div>
  </section>
</div>
    <div class="postbit" id="283770" data-post-id="283770">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cjbottaro" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cjbottaro/120/1469_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cjbottaro
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="cjbottaro" data-post="42" data-topic="54827">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cjbottaro/48/1469_2.png" class="avatar"> cjbottaro:</div>
<blockquote>
<p>Fitness is the count of rows and columns that all contain unique numbers (i.e. the count of solved rows/cols, so a score of 18 means solved). My crossover is the same as the OneMax example… which is think is the culprit.</p>
</blockquote>
</aside>
<p>For modeling, I store “the board” as a map of points, and the genes are also a map of points. It’s super inefficient, but ehh, it was easy.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def genotype(_i) do
    board = %{
      {0, 0} =&gt; 5,
      {1, 0} =&gt; 3,
      ...
    }

    genes = Enum.reduce(0..8, %{}, fn x, acc -&gt;
      Enum.reduce(0..8, acc, fn y, acc -&gt;
        if Map.has_key?(board, {x, y}) do
          acc
        else
          Map.put(acc, {x, y}, Enum.random(1..9))
        end
      end)
    end)

    %Chromosome{ genes: genes, extra: %{board: board} }
  end

  def fitness(%Chromosome{} = c) do
    Enum.reduce(0..8, 0, fn i, acc -&gt;
      score_row(c, i) + score_col(c, i) + acc
    end)
  end
</code></pre>
<p>Where <code>score_row</code> and <code>score_col</code> return 1 if all numbers are unique, or 0 otherwise. I also experimented with score being the number of unique items in a row/col. Didn’t change anything.</p>
<p>Oh, also my selection is just elite.</p>
<p>I’m curious how this works:<br>
<a href="https://github.com/ctjacobs/sudoku-genetic-algorithm" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ctjacobs/sudoku-genetic-algorithm</a></p>
<p>Which gets the answer in only 37 generations… but I don’t really want to sift through a whole bunch of undocumented and unorganized python code. Kinda wish the readme just said the general strategy in English.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283770" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-283770" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283770"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #43"></div>
  </section>
</div>
    <div class="postbit" id="283790" data-post-id="283790">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="stevensonmt" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/120/20503_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  stevensonmt
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think it would be possible to have all valid rows and all valid columns but still not have all valid sub-grids.</p><aside class="onebox stackexchange" data-onebox-src="https://math.stackexchange.com/questions/2331022/can-a-solved-sudoku-game-have-an-invalid-region-if-all-rows-and-columns-are-vali">
  <header class="source">

      <a href="https://math.stackexchange.com/questions/2331022/can-a-solved-sudoku-game-have-an-invalid-region-if-all-rows-and-columns-are-vali" target="_blank" rel="noopener nofollow ugc">math.stackexchange.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://math.stackexchange.com/users/263239/dragonfly" target="_blank" rel="noopener nofollow ugc">
    <img alt="dragonfly" src="https://www.gravatar.com/avatar/6acb720977e38bcb85f595a0b8fc2249?s=256&amp;d=identicon&amp;r=PG" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://math.stackexchange.com/questions/2331022/can-a-solved-sudoku-game-have-an-invalid-region-if-all-rows-and-columns-are-vali" target="_blank" rel="noopener nofollow ugc">Can a solved Sudoku game have an invalid region if all rows and columns are valid?</a>
</h4>

<div class="tags">
  <strong>examples-counterexamples, puzzle, latin-square, sudoku</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://math.stackexchange.com/users/263239/dragonfly" target="_blank" rel="noopener nofollow ugc">
    dragonfly
  </a>
  on <a href="https://math.stackexchange.com/questions/2331022/can-a-solved-sudoku-game-have-an-invalid-region-if-all-rows-and-columns-are-vali" target="_blank" rel="noopener nofollow ugc">12:06PM - 21 Jun 17 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
I don’t think that’s your issue with efficiency though.</p>
<p>FWIW my naive solution is struggling as well. I’m just using a list of integers 1..9 with length 81 as the board. The puzzle input is processed with each wildcard/blank replaced by random integer 1..9 to generate the chromosome. Crossover is at a random point in the list. Mutation is a potential problem. The way the lib is setup in the book the mutation function is hard coded and not modifiable per problem. In this case we can’t shuffle the genes outright because the puzzle provides some fixed values. Should <code>mutation/2</code> and maybe even <code>crossover/2</code> be part of the behaviour callbacks?<br>
For now I’ve just set the mutation rate to zero. I’m using a wholesale population reset if the temperature drops below a certain value and a generation limit to terminate if perfect fitness is not found. Can’t figure out why but for some reason after the first time it resets it then resets after every generation automatically unless I reset with a really high default temperature. It’s still not getting anywhere close to a full solution though. I think the best fitness I’ve seen is 7 out of a needed 27. I haven’t let it run more than a few hundred generations yet though.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283790" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/genetic-algorithms-in-elixir-book-club/54827/45">Post #44</a>
	                </div>
	            </div>
              <div id="likers-container-283790" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283790"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #44"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/54827/load_more?page=5">Load more posts (28 remaining)</a>
</div></template></turbo-stream>