<?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/"
	>

<channel>
	<title>So Jake Says &#187; Sqrt</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/sqrt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Prototyping Genetic Algorithms in Lisp</title>
		<link>http://www.jakevoytko.com/blog/2008/12/04/prototyping-genetic-algorithms-in-lisp/</link>
		<comments>http://www.jakevoytko.com/blog/2008/12/04/prototyping-genetic-algorithms-in-lisp/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 12:00:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[asdf-install]]></category>
		<category><![CDATA[Genetic Algorithm]]></category>
		<category><![CDATA[genetic algorithm design]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Sqrt]]></category>
		<category><![CDATA[Square root]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=320</guid>
		<description><![CDATA[Source code for Genesis is here until I get asdf-install going. What are Genetic Algorithms? Genetic algorithms are one of the best things that computer science has produced. Rather than figuring out a good algorithm yourself, you let your random number generator find one while you sleep! Well, after an up-front design cost to generate [...]]]></description>
			<content:encoded><![CDATA[<p><em>Source code for Genesis is <a href="http://github.com/jakevoytko/genesis/tree/master">here</a> until I get asdf-install going.<br />
</em></p>
<h2>What are Genetic Algorithms?</h2>
<p>Genetic algorithms are one of the best things that computer science has produced. Rather than figuring out a good algorithm yourself, you let your random number generator find one while you sleep! Well, after an up-front design cost to generate proper random functions, and evaluating them, and determining how you&#8217;ll evolve them, and how long they should run&#8230;</p>
<p>I&#8217;ve long been interested in genetic algorithms, and now that I&#8217;ve learned my first language with an eval &#8212; Lisp &#8212; I&#8217;m starting to make a library. I spent the past few days prototyping and testing my code,<br />
and I got some encouraging initial results: I got a great almost-linear approximation to my simple test problem (finding square roots) using only addition, subtraction, multiplication, and division (see below).</p>
<p>I know, square roots. What a scorcher. But real people are making real solutions to real problems using genetic algorithms! NASA engineers produced an antenna. They determined acceptable design parameters for the antenna, figured out how to evaluate new designs, and let randomness determine the rest.</p>
<p>The result is described <a href="http://ti.arc.nasa.gov/projects/esg/research/antenna.htm">here</a>.</p>
<h2>My Genetic Algorithm Plans</h2>
<p>I want to produce a Lisp package over the next few weeks (since I should have a boatload of freetime around Christmas) that makes it easy to define new genetic algorithms. I&#8217;m going to do it in a few stages, and blog about intermediate results if and when they are interesting.</p>
<p>This will also give internet denizens plenty of opportunity to criticize my awful Lisp code! I am a nublet when it comes to Lisp, and I have no illusions about the quality of the code I&#8217;m writing.</p>
<p>This week: prototyping. I picked an easy problem to work out the basics of genetic algorithms. My problem? Finding square roots between 0 and 99, inclusive, with just one rule set.</p>
<blockquote><p><strong>Disclaimer</strong>: This is potentially the stupidest use of genetic programming known to man. Attempting to regularly solve numeric problems with genetic algorithms can lead to blindness, irrational exuberance towards Republican politics, and death.</p></blockquote>
<p>To give you a sense of the data it ingests, I present the main interface:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> genetic-algorithm <span style="color: #66cc66;">&#40;</span>generations starting-rules rule-fun
                          fitness-fun<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Runs 'generations' number of generations. The rules are initially
  set to to 'starting-rules', the rule generating function is
  'rule-fun', and the evaluation function is 'fitness-fun'&quot;</span>
  <span style="color: #66cc66;">&#40;</span>prep-rules starting-rules<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dotimes</span> <span style="color: #66cc66;">&#40;</span>gen-num generations<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>run-generation rule-fun fitness-fun<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Here is the invocation I used for my square root problem:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>genetic-algorithm <span style="color: #cc66cc;">32000</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>/ num <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ num <span style="color: #66cc66;">.</span>2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                   #'sample-rule-fun #'sample-fitness-fun<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The typical work for defining genetic algorithms is split into a few stages:</p>
<p>1) Problem definition: What do I even want to solve?<br />
2) Evaluation function: How far am I from right?<br />
3) Picking components: How can I build my algorithms randomly?</p>
<p><strong>Problem Definition</strong></p>
<p>Find the square root of an integer, n, in the range [0, 99].</p>
<p><strong>Evaluation Function</strong></p>
<p>Sum of the squared error of all of the integers in the problem statement.</p>
<p>Interestingly, we can predict the quality and form of the results: since we are minimizing the sum of the squared errors, the result should mimic a least-squared approach. Since I&#8217;m only using basic arithmetic, it probably won&#8217;t be a very good one (linear at best..).</p>
<h2>Prototype Implementation</h2>
<p><em>Warning: There be dragons in the parens ahead.</em></p>
<p>The rules are stored as a list of lists:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar *RULES* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We also store the possible operators:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar *sample-nodes* #<span style="color: #66cc66;">&#40;</span>leaf<span style="color: #66cc66;"> + </span>leaf<span style="color: #66cc66;"> - </span>leaf<span style="color: #66cc66;"> / </span>leaf<span style="color: #66cc66;"> * </span>leaf<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Woah, what&#8217;s with all of the &#8220;leaf&#8221;s? Well, a balanced binary tree has about as many nodes as leafs (I&#8217;m generalizing&#8230;), so I started at leaf/operator parity and moved up until I stopped getting stack overflows when generating new functions, which happened at one <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>How do the leafs get translated into values? First, they are picked from an array of random values:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defvar *sample-values*
    #<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span> num num randomnum randomfrac<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample-generate-leaf <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Picks a random element from 'list' and translates it into the proper atom
  or list.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>elt <span style="color: #66cc66;">&#40;</span>random-array-element <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> elt
         <span style="color: #66cc66;">&#40;</span>randomnum <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>randomfrac
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>val <span style="color: #66cc66;">&#40;</span>ignore-errors <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> val<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> val <span style="color: #66cc66;">&#40;</span>/ <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>t elt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>These are pretty self descriptive (<em>num</em> is the input number, and it is included twice because I got better results <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ). &#8216;<em>randomnum</em>&#8216; is a random integer in [0, 1023], and &#8216;<em>randomfrac</em>&#8216; is a fraction whose<br />
coefficients are in [0, 1023].</p>
<p>So how is all of this put together?</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample-rule-fun <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Example random rule generator. Generates a random arithmetical
  expression.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>elt <span style="color: #66cc66;">&#40;</span>random-array-element *sample-nodes*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> elt
      <span style="color: #66cc66;">&#40;</span>leaf <span style="color: #66cc66;">&#40;</span>sample-generate-leaf *sample-values*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>t
       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> elt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>at-least-once #'sample-rule-fun<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> sample-generate-leaf <span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Picks a random element from 'arr' and translates it into the proper atom
  or list.&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>elt <span style="color: #66cc66;">&#40;</span>random-array-element arr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> elt
    <span style="color: #66cc66;">&#40;</span>randomnum <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>randomfrac
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>val <span style="color: #66cc66;">&#40;</span>ignore-errors <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> val<span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> val <span style="color: #66cc66;">&#40;</span>/ <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; avoid divide by zero</span>
       val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>t elt<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is also fairly straightforward: if you pick an operator, generate a new list, if you pick a leaf, generate a new value.</p>
<h2>Results</h2>
<h3>Square Root Function Test</h3>
<p>I ran it for about 18 hours (overnight and after work), and when I came back, the results were nothing short of amazing:</p>
<p><a href="http://www.jakevoytko.com/blog/wp-content/uploads/2008/12/sqrt-plot.gif"><img class="alignnone size-full wp-image-322" title="sqrt-plot" src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/12/sqrt-plot.gif" alt="" width="500" height="332" /></a></p>
<p>I included a range of values outside of the initial zone because the results are better than I could have hoped for.</p>
<p>I saved the data, and then was trying to save the rules (there were 18 in all), and then I lost them from my first Emacs freeze. C&#8217;est la vie. However, my initial guess that the data would fit as a least-squares appears to be a better guess than I ever could have hoped. I want to add arbitrary polynomials, and I hope to see a higher-order best-squares fit of the data plot, with much less error.</p>
<h3>Implementation Time</h3>
<p>The first implementation was done in a 4-hour chunk of one evening (during which I played X-Moto for at least an hour&#8230;), which I find impressive. I probably would have spent at least 3 times that on the prototype in C++. This problem is made extremely because of several of Lisp&#8217;s features:</p>
<p><strong>eval</strong>: My method is based on runtime list generation, and eval-ing it as a lambda.</p>
<p><strong>Insanely simple Lisp syntax</strong>: The same syntax that defines lists also defines their programs? This reduces random function generation to a random list generation problem, which is almost trivial.</p>
<p><strong>REPL:</strong> Lisp is a REPL (<strong>r</strong>ead-<strong>e</strong>val-<strong>p</strong>rint <strong>l</strong>oop) language, meaning that it has an interactive prompt. Combining this with Emacs&#8217; multiple buffer capabilities, and it&#8217;s super easy to make changes and reload.</p>
<h2>TODO</h2>
<p>It&#8217;s never finished!</p>
<h3>Rules</h3>
<p>Allow more than one rule set, define the maximum number of rules per set, define rules as arrays for better lookup, etc.</p>
<h3>Better use of eval</h3>
<p>I eval the rules every time I need them, as opposed to the once I should generate them. I will probably see a significant speedup when I do this.</p>
<h3>Define a package</h3>
<p>Namespacing everything has obvious benefits.</p>
<h3>Cross-breed rules</h3>
<p>Allow true genetic properties by copying subsets of rules into other rules.</p>
<h3>Better rule trimming</h3>
<p>My current program produces some stupid rules: such as f(x)=x. In fact, my &#8220;amazing results&#8221; had 4 rules that evaluated a number to itself out of a total if 18 rules.</p>
<h3>Allowing the user to define rule inputs.</h3>
<h3>Putting the package in an easy-to-use location</h3>
<p>Stay tuned for more!</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=320&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/12/04/prototyping-genetic-algorithms-in-lisp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
