<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Natural Code &#187; genetic</title>
	<atom:link href="http://naturalcode.wordpress.com/tag/genetic/feed/" rel="self" type="application/rss+xml" />
	<link>http://naturalcode.wordpress.com</link>
	<description>Code, science and politics.</description>
	<lastBuildDate>Sat, 30 Aug 2008 22:57:43 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='naturalcode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/8317277a84f31f5d58ceb91d0eb5503f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Natural Code &#187; genetic</title>
		<link>http://naturalcode.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://naturalcode.wordpress.com/osd.xml" title="Natural Code" />
		<item>
		<title>A basic genetic algorithm (part 3)</title>
		<link>http://naturalcode.wordpress.com/2008/06/08/a-basic-genetic-algorithm-part-3/</link>
		<comments>http://naturalcode.wordpress.com/2008/06/08/a-basic-genetic-algorithm-part-3/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 03:50:00 +0000</pubDate>
		<dc:creator>naturalcode</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://naturalcode.wordpress.com/?p=10</guid>
		<description><![CDATA[I&#8217;ve posted the updated code here, on Refactor :my =&#62; &#8216;code&#8217;, a cool little site I just found.
I&#8217;ll go refactor someone else&#8217;s code on there later, could help me with my coding too.
The program works now, although it&#8217;s not too efficient. I need to add sexual reproduction, &#8217;cause right now they&#8217;re asexual and that&#8217;s slower.
EDIT: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=naturalcode.wordpress.com&blog=3890912&post=10&subd=naturalcode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve posted the updated code <a href="http://refactormycode.com/codes/313-genetic-algorithm-for-making-equations">here</a>, on Refactor :my =&gt; &#8216;code&#8217;, a cool little site I just found.</p>
<p>I&#8217;ll go refactor someone else&#8217;s code on there later, could help me with my coding too.</p>
<p>The program works now, although it&#8217;s not too efficient. I need to add sexual reproduction, &#8217;cause right now they&#8217;re asexual and that&#8217;s slower.</p>
<p>EDIT: Okay, I&#8217;ll just leave it up <a href="http://refactormycode.com/codes/313-genetic-algorithm-for-making-equations">on the other site</a> then, because WordPress was throwing HTML in my Ruby, like this :</p>
<p><span><span class="keyword">def</span><span> initialize(copy_genome=</span><span class="string">&#8216;off&#8217;</span><span> &lt;img src=</span><span class="string">&#8220;http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif&#8221;</span><span> alt=</span><span class="string">&#8220;;)&#8221;</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">&#8220;wp-smiley&#8221;</span><span>&gt;<br />
</span></span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/naturalcode.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/naturalcode.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/naturalcode.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/naturalcode.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/naturalcode.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/naturalcode.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/naturalcode.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/naturalcode.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/naturalcode.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/naturalcode.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/naturalcode.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/naturalcode.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=naturalcode.wordpress.com&blog=3890912&post=10&subd=naturalcode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://naturalcode.wordpress.com/2008/06/08/a-basic-genetic-algorithm-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8331396d01d101f09bd298cfb5a0fd5a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">naturalcode</media:title>
		</media:content>
	</item>
		<item>
		<title>A basic genetic algorithm (part 2)</title>
		<link>http://naturalcode.wordpress.com/2008/06/07/a-basic-genetic-algorithm-part-2/</link>
		<comments>http://naturalcode.wordpress.com/2008/06/07/a-basic-genetic-algorithm-part-2/#comments</comments>
		<pubDate>Sat, 07 Jun 2008 05:01:46 +0000</pubDate>
		<dc:creator>naturalcode</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://naturalcode.wordpress.com/?p=8</guid>
		<description><![CDATA[Okay, so I&#8217;ve broken this up into two files :
equation.rb

class Array
  def random
    self[rand(self.length)]
  end
end

class Genome
  attr_accessor :code

  @@decode = {   '0000' =&#62; '0.0', '0001' =&#62; '1.0',
  '0010' =&#62; '2.0', '0011' =&#62; '3.0', '0100' =&#62; '4.0',
  '0101' =&#62; '5.0', '0110' =&#62; '6.0', '0111' =&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=naturalcode.wordpress.com&blog=3890912&post=8&subd=naturalcode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Okay, so I&#8217;ve broken this up into two files :</p>
<p>equation.rb</p>
<pre class="brush: ruby;">
class Array
  def random
    self[rand(self.length)]
  end
end

class Genome
  attr_accessor :code

  @@decode = {   '0000' =&gt; '0.0', '0001' =&gt; '1.0',
  '0010' =&gt; '2.0', '0011' =&gt; '3.0', '0100' =&gt; '4.0',
  '0101' =&gt; '5.0', '0110' =&gt; '6.0', '0111' =&gt; '7.0',
  '1000' =&gt; '8.0', '1001' =&gt; '9.0', '1010' =&gt; '+',
  '1011' =&gt; '-', '1100' =&gt; '*', '1101' =&gt; '/' }
  @@operators = %w[+ - * /]
  @@numbers = %w[0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0]

  def initialize(min_length=4, max_length=32)
    length = min_length + rand(1 + max_length - min_length)
    @code = generate_code(length)
  end

  def generate_code(length)
    code = ''
    1.upto(length) do
      code += ['0', '1'].random
    end
    code
  end

  def decode
    decoded = ''
    expected = :number

    1.upto(@code.length) do |i|
      if i % 4 == 0
        coded = @code.slice((i-4)..(i-1))
        symbol = @@decode[coded].to_s
        puts symbol
        if expected == :number &amp;&amp; @@numbers.include?(symbol)
          decoded += symbol
          expected = <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> perator
        elsif expected == <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> perator &amp;&amp;
        @@operators.include?(symbol)
          decoded += symbol
          expected = :number
        else
          puts &quot;#{expected} expected, #{symbol} found.&quot;
        end
      end
    end

    if expected == :number
      decoded.chop!
    end

    return decoded
  end
end

class Equation
  attr_accessor :genome, :phenotype

  def initialize
    @genome = Genome.new(4, 64)
    @phenotype = @genome.decode
  end
end
</pre>
<p>formula_ga.rb</p>
<pre class="brush: ruby;">
require 'equation'

class Population
  def initialize(size=50, target_number=11)
    @equations = []
    @size = size
    @target_number = target_number
    1.upto(size) { @equations &lt;&lt; Equation.new }
  end

  def members
    @equations
  end

  def evaluate_fitness(equation)
    answer = eval(equation.phenotype)
    deviation = @target_number - answer.to_f
    fitness = 1 / deviation
  end

  def sort_by_fitness(equation_array)

  end

  def next_generation
    reproduction_pool = []
    1.upto(@size / 2) do
      reproduction_pool &lt;&lt; @equations.random
    end
  end
end

pop = Population.new
pop.members.each do |member|
  puts member.genome.code
  puts member.phenotype
  x = eval(member.phenotype)
  puts x.to_s
  puts &quot;Fitness : #{pop.evaluate_fitness(member)}&quot;
  puts &quot;&quot;
end
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/naturalcode.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/naturalcode.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/naturalcode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/naturalcode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/naturalcode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/naturalcode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/naturalcode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/naturalcode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/naturalcode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/naturalcode.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/naturalcode.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/naturalcode.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=naturalcode.wordpress.com&blog=3890912&post=8&subd=naturalcode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://naturalcode.wordpress.com/2008/06/07/a-basic-genetic-algorithm-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8331396d01d101f09bd298cfb5a0fd5a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">naturalcode</media:title>
		</media:content>
	</item>
		<item>
		<title>A basic genetic algorithm (part 1)</title>
		<link>http://naturalcode.wordpress.com/2008/06/03/a-basic-genetic-algorithm/</link>
		<comments>http://naturalcode.wordpress.com/2008/06/03/a-basic-genetic-algorithm/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 00:55:20 +0000</pubDate>
		<dc:creator>naturalcode</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://naturalcode.wordpress.com/?p=6</guid>
		<description><![CDATA[This here is an intro to genetic algorithms with a nice little biological analogy and everything. It starts by explaining the evolution of blind, clumsy, algae-eating, cave-dwelling creatures called Hooters into light-seeing, eagle-dodging, moss-eating machines.
It then goes on to explain how this is relevant to computing, and gives a simple example of a problem that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=naturalcode.wordpress.com&blog=3890912&post=6&subd=naturalcode&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://www.ai-junkie.com/ga/intro/gat1.html" target="_blank">This here</a> is an intro to genetic algorithms with a nice little biological analogy and everything. It starts by explaining the evolution of blind, clumsy, algae-eating, cave-dwelling creatures called Hooters into light-seeing, eagle-dodging, moss-eating machines.</p>
<p>It then goes on to explain how this is relevant to computing, and gives a simple example of a problem that can be solved with a genetic algorithm. In this case it involves evolving equations that give a number you&#8217;re looking for.</p>
<p>I&#8217;m already a little familiar with GAs, I&#8217;ve written one that generated words out of random characters, but it was slightly ugly. Especially the fitness function. I&#8217;ll to do this one properly.</p>
<p>I&#8217;m going to solve the equation problem and post my code later (it&#8217;ll be in Ruby).</p>
<p>UPDATE : Here&#8217;s the code so far. I&#8217;m off to play DOTA.</p>
<pre class="brush: ruby;">

class Array
  def random
    self[rand(self.length)]
  end
end

class Genome
  attr_accessor :code

  def initialize(min_length=4, max_length=32)
    length = min_length + rand(1 + max_length - min_length)
    @code = generate_code(length)
  end

  def generate_code(length)
    code = ''
    1.upto(length) do
      code += ['0', '1'].random
    end
    code
  end
end

class Equation
  def initialize
    @genome = Genome.new
  end

  def genome
    @genome.code
  end
end

class Population
  def initialize(size=50)
    @equations = []
    1.upto(size) { @equations &lt;&lt; Equation.new }
  end

  def members
    @equations
  end
end

Population.new.members.each { |member| puts member.genome}
</pre>
<p>Anyone know of a better way to easily manipulate a series of bits than by storing it in a string? It&#8217;s fine if I just have a few but I like to make these things scale, if possible, and I know from experience that this kind of thing tends toward total memory consumption.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/naturalcode.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/naturalcode.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/naturalcode.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/naturalcode.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/naturalcode.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/naturalcode.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/naturalcode.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/naturalcode.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/naturalcode.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/naturalcode.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/naturalcode.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/naturalcode.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=naturalcode.wordpress.com&blog=3890912&post=6&subd=naturalcode&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://naturalcode.wordpress.com/2008/06/03/a-basic-genetic-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8331396d01d101f09bd298cfb5a0fd5a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">naturalcode</media:title>
		</media:content>
	</item>
	</channel>
</rss>