<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="287489" data-post-id="287489">
  <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’m stuck on chapter 7. The fitness function implementation is acting weird in that the chromosome does not always follow a binary digit representation, which then raises an error. I think it’s some sort of overflow happening in the <code>Bitwise.bxor</code> call but not sure how to troubleshoot. Here’s what I’m seeing:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">GENES: [0, 1, 2, 3, 4, 5, 7, 8]
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a textual representation of an integer

    :erlang.binary_to_integer("01234578", 2)
    scripts/codebreaker.exs:22: Codebreaker.fitness_fun/1
</code></pre>
<p>My code is below. You can see I tried to use <code>Integer.undigits</code> first but that gives an error of <code>invalid digit 2 in base 2</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def fitness_fun(chromosome) do
    IO.inspect(chromosome.genes, label: :GENES)
    target = "ILoveGeneticAlgorithsm"
    encrypted = 'LIjs`B`kqlfDibjwlqmhv'
    cipher = fn word, key -&gt; Enum.map(word, &amp;rem(Bitwise.bxor(&amp;1, key), 32_768)) end
    # key = Integer.undigits(chromosome.genes, 2)

    key =
      chromosome.genes
      |&gt; Enum.map_join(&amp;Integer.to_string(&amp;1))
      |&gt; String.to_integer(2)

    guess = List.to_string(cipher.(encrypted, key))
    String.jaro_distance(target, guess)
  end
</code></pre>
<p>If I rescue the error I just end up with convergence on a bad chromosome for some reason.</p>
<p>I ran the author’s provided code sample for the chapter and have the same issue. Or, I did until I recompiled everything. So something is off in my actual Genetic library code not in the script itself.</p>
<hr>
<p>Okay to ignore all that above. I had implemented a chromosome repair function that was specific to one chromosome type and it was screwing things up. So I don’t get that error but currently my code is converging on a fitness of about 0.97 and never completing. Guess I have to get through the rest of the chapter.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287489" 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/66">Post #65</a>
	                </div>
	            </div>
              <div id="likers-container-287489" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287489"
                     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 #65"></div>
  </section>
</div>
    <div class="postbit" id="287503" data-post-id="287503">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Dear Stephen,</p>
<p>It seems that you had some typos at the <code>target</code> and the <code>encrypted</code> variables. Here is the code of yours working (at in my local machine);</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def fitness_function(chromosome) do
    # IO.inspect(chromosome.genes, label: :GENES)
    target = "ILoveGeneticAlgorithms"
    encrypted = 'LIjs`B`k`qlfDibjwlqmhv'
    cipher = fn word, key -&gt; Enum.map(word, &amp;rem(Bitwise.bxor(&amp;1, key), 32_768)) end
    # key = Integer.undigits(chromosome.genes, 2)

    key =
      chromosome.genes
      |&gt; Enum.map_join(&amp;Integer.to_string(&amp;1))
      |&gt; String.to_integer(2)

    guess = List.to_string(cipher.(encrypted, key))
    String.jaro_distance(target, guess)
  end
</code></pre>
<p>A typical result I get by running this script is:<br>
Current best: 1.09242424242424243<br>
The Key is 2152810337942732805</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287503" 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/67">Post #66</a>
	                </div>
	            </div>
              <div id="likers-container-287503" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287503"
                     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 #66"></div>
  </section>
</div>
    <div class="postbit" id="287563" data-post-id="287563">
  <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>Well that’s embarrassing. Thanks. It’s still weird that it returns different values for the key on different runs.</p>
<p>I finished chapter 7.</p>
<p><code>Integer.undigits/2</code> is much nicer than the provided key generation implementation, imo.</p>
<p>The provided scramble/2 seems inefficient due to the use of Enum.slice in 3 areas and concatenating 3 lists together which requires iterating over the genes multiple times. I came up with a way to circumvent that approach, which I think is more efficient:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def scramble(chromosome, n) do
  start = :rand.uniform(n - 1)
  {lo, hi} =
    if start + n &gt;= chromosome.size do
      {start - n, start}
    else
      {start, start + n}
    end
  swaps =
    lo..hi
    |&gt; Enum.zip(Enum.shuffle(lo..hi))
    |&gt; Map.new()

  arr = :array.from_list(chromosome.genes)
  genes =
    0..:array.sparse_size(arr)
    |&gt; Enum.reduce(arr, fn i, gs -&gt;
          cond do
            i &lt; lo -&gt; gs
            i &gt; hi -&gt; gs
            true -&gt;
              new_val = :array.get(swaps[i], arr)
              :array.set(i, new_val, gs)
          end
        end)
    |&gt; :array.to_list()

  %Chromosome{genes: genes, size: chromosome.size}
end
</code></pre>
<p>My implementations of the swap, uniform, and invert mutation strategies:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @doc "Swap can be done for any genotype"
  def swap(chromosome, n \\ 1) do
    arr = chromosome.genes |&gt; :array.from_list()

    genes =
      1..n
      |&gt; Enum.reduce(arr, fn _i, gs -&gt;
        [swap1, swap2] =
          Stream.repeatedly(fn -&gt; :rand.uniform(:array.sparse_size(arr) - 1) end) |&gt; Enum.take(2)

        v1 = :array.get(swap1, gs)
        v2 = :array.get(swap2, gs)
        gs = :array.set(swap1, v2, gs)
        :array.set(swap2, v1, gs)
      end)
      |&gt; :array.to_list()

    %Chromosome{genes: genes, size: chromosome.size}
  end

  @doc "Uniform applies to binary or real-value genotypes but not permutations. For binary genotypes, the genotype parameter needs to be true. No absolute maximum value is assumed, but the limit for each mutation is arbitrarily fixed at double the current maximal value. Implementation does not ensure unique values for each gene. If unique values are necessary, the genotype parameter must be :unique."
  def uniform(chromosome, true) do
    genes = Stream.repeatedly(fn -&gt; :rand.uniform(2) - 1 end) |&gt; Enum.take(chromosome.size)
    %Chromosome{genes: genes, size: chromosome.size}
  end

  def uniform(chromosome, false) do
    genes =
      Stream.repeatedly(fn -&gt; :rand.uniform(Enum.max(chromosome.genes) * 2) end)
      |&gt; Enum.take(chromosome.size)

    %Chromosome{genes: genes, size: chromosome.size}
  end

  def uniform(chromosome, :unique) do
    genes =
      Stream.repeatedly(fn -&gt; :rand.uniform(Enum.max(chromosome.genes) * 2) end)
      |&gt; Enum.reduce_while(MapSet.new(), fn n, acc -&gt;
        if MapSet.size(acc) == chromosome.size do
          {:halt, acc}
        else
          {:cont, MapSet.put(acc, n)}
        end
      end)

    %Chromosome{genes: genes, size: chromosome.size}
  end

  def uniform(chromosome), do: uniform(chromosome, false)

  @doc "Invert just reverses the genes, I guess."
  def invert(chromosome),
    do: %Chromosome{genes: Enum.reverse(chromosome.genes), size: chromosome.size}
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287563" 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/68">Post #67</a>
	                </div>
	            </div>
              <div id="likers-container-287563" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287563"
                     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 #67"></div>
  </section>
</div>
    <div class="postbit" id="288550" data-post-id="288550">
  <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>Chapter 8 was interesting in the sense of really opening my eyes to how complex genetic algorithms can be made to model various problems.</p>
<p>The actual strategies and implementations addressed in the chapter are fairly similar to things seen in the other stages of the genetic algorithm architecture, so I don’t have much to say about them.</p>
<p>What really piqued my interest though was a bit at the end of the chapter about local neighborhoods of chromosomes and multipopulation genetic algorithms. The author made a sort of throw-away comment that multipopulation genetic algorithms benefit from being parallelizable. That sounds like a great fit for the Elixir/BEAM ecosystem.</p>
<p>On a side note, one of my potential side projects for the genetic algorithm approach is actually holiday scheduling for hospital coverage with my colleagues, so the example in the book was really apt for me.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="288550" 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/69">Post #68</a>
	                </div>
	            </div>
              <div id="likers-container-288550" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="288550"
                     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 #68"></div>
  </section>
</div>
    <div class="postbit" id="290761" data-post-id="290761">
  <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>Sorry for the long delay. With issues at work and my daughter graduating high school (who knew that came with so many different parental involvement activities?) I just haven’t had a free moment. At any rate got around to finishing up chapter 9. Disappointed that it didn’t seem to advance the actual genetic algorithm topic much, but I’m always excited to see <code>libgraph</code> in a project. The idea of tracking population history in an ETS table explored by <code>libgraph</code> functions is interesting. I think this technique could be useful in other situations such as tree traversal and path-finding. The other thought I had, though, was this sort of double abstraction of an ETS table behind a GenServer interface was not quite helpful. Maybe in later chapters where this genealogy is used more the design choice will make more sense.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="290761" 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/70">Post #69</a>
	                </div>
	            </div>
              <div id="likers-container-290761" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="290761"
                     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 #69"></div>
  </section>
</div>
    <div class="postbit" id="292562" data-post-id="292562">
  <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>Going to keep talking to myself here, I guess. <img src="https://forum.elixirforum.com/uploads/default/original/2X/7/776053aa22968f83c10e90c98b23aa0ddd5c9d47.gif?v=15" title=":lol:" class="emoji emoji-custom" alt=":lol:" loading="lazy" width="20" height="20"></p>
<p>I’m moving through the book much more slowly than I would like but life has been unavoidably busy in recent weeks/months. I’ve just gotten through chapter 10 and found it really fun. Eye candy is always exciting. Plus getting a peak at the <code>alex</code> library and Atari game simulation is just so cool. Highly encourage everyone to push through to this bit if you aren’t familiar with <code>alex</code> and are interested in genetic algorithms for bots playing games.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="292562" data-batch-url="/posts/batch_likers">
                        5
                      </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/71">Post #70</a>
	                </div>
	            </div>
              <div id="likers-container-292562" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="292562"
                     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 #70"></div>
  </section>
</div>
    <div class="postbit" id="294562" data-post-id="294562">
  <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>Momma didn’t raise no quitter!<br>
Got through chapter 11. I want to make it clear that this book is actually quite fun to get through. Life has just been bananas around here so I have not had time to keep at it.</p>
<p>Chapter 11 could be extracted out and really be generalized to not only a ton of Elixir practices but also software development in general. I loved the benchmarking vs profiliing distinction and walking through steps of optimization. The use of NIFs was super cool.</p>
<p>On to Chapter 12!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="294562" 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/72">Post #71</a>
	                </div>
	            </div>
              <div id="likers-container-294562" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="294562"
                     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 #71"></div>
  </section>
</div>
    <div class="postbit" id="295014" data-post-id="295014">
  <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’ve finished the book. Chapter 12 was mostly overview of things you’ve probably picked up spending any time with Elixir such as typespecs and using credo and dialyzer. The one thing I would like to see more of from this chapter was the bit on testing. I actually think a second edition of the book written as “Test Driven Development of Genetic Algorithms in Elixir” would be really nice and more reasonably model how real applications might be developed with this approach.<br>
Chapter 13 was an interesting overview of the problem domains that genetic algorithms can contribute to as wells as other texts to further your study. <code>Gene I. Sher. Handbook of Neuroevolution Through Erlang</code> looks particularly interesting.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295014" 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/73">Post #72</a>
	                </div>
	            </div>
              <div id="likers-container-295014" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295014"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>