<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=“www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>

Module: NEAT::DSL

  &mdash; Documentation by YARD 0.8.7.3

</title>

<link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />

<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />

<script type=“text/javascript” charset=“utf-8”>

hasFrames = window.top.frames.main ? true : false;
relpath = '../';
framesUrl = "../frames.html#!" + escape(window.location.href);

</script>

<script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>

<script type="text/javascript" charset="utf-8" src="../js/app.js"></script>

</head>
<body>
  <div id="header">
    <div id="menu">

  <a href="../_index.html">Index (D)</a> &raquo;
  <span class='title'><span class='object_link'><a href="../NEAT.html" title="NEAT (module)">NEAT</a></span></span>
   &raquo; 
  <span class="title">DSL</span>

<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>

</div>

  <div id="search">

<a class="full_list_link" id="class_list_link"
    href="../class_list.html">
  Class List
</a>

<a class="full_list_link" id="method_list_link"
    href="../method_list.html">
  Method List
</a>

<a class="full_list_link" id="file_list_link"
    href="../file_list.html">
  File List
</a>

</div>

  <div class="clear"></div>
</div>

<iframe id="search_frame"></iframe>

<div id="content"><h1>Module: NEAT::DSL

</h1>

<dl class=“box”>

  <dt class="r1">Includes:</dt>
  <dd class="r1">Math, <span class='object_link'><a href="../NEAT.html" title="NEAT (module)">NEAT</a></span>, <span class='object_link'><a href="BasicNeuronTypes.html" title="NEAT::BasicNeuronTypes (module)">BasicNeuronTypes</a></span></dd>

<dt class="r2 last">Defined in:</dt>
<dd class="r2 last">lib/rubyneat/dsl.rb</dd>

</dl> <div class=“clear”></div>

<h2>Constant Summary</h2>

<h2>Constant Summary</h2>

<h3 class="inherited">Constants included
   from <span class='object_link'><a href="../NEAT.html" title="NEAT (module)">NEAT</a></span></h3>
<p class="inherited"><span class='object_link'><a href="../NEAT.html#STIMULUS-constant" title="NEAT::STIMULUS (constant)">STIMULUS</a></span></p>

  <h2>
    Instance Method Summary
    <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
  </h2>

  <ul class="summary">

      <li class="public ">
<span class="summary_signature">

    <a href="#compare-instance_method" title="#compare (instance method)">- (Object) <strong>compare</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Fitness ordering – given 2 fitness numbers, use the to compare them (or the equivalent, following the +1, 0, -1 that is in the sense of ).</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#condition_boolean_vector-instance_method" title="#condition_boolean_vector (instance method)">- (Object) <strong>condition_boolean_vector</strong>(vec, sig = :tanh) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Helper function to Condition boolean vectors to be +1 if true, -1 if false (0 if sigmoid).</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#cost-instance_method" title="#cost (instance method)">- (Object) <strong>cost</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Calculation to add the cost to the fitness, resulting in a fitness that incorporates the cost for sorting purposes.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#define-instance_method" title="#define (instance method)">- (Object) <strong>define</strong>(name = NEAT.random_name_generator, &amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>DSL – Define defines the parameters to the controller.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#evolve-instance_method" title="#evolve (instance method)">- (Object) <strong>evolve</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>DSL – Run evolution.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#fitness-instance_method" title="#fitness (instance method)">- (Object) <strong>fitness</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>fitness function calls the block with 2 vectors or two hashes, input and output vectors of the critter being evaluated for fitness, as well as a sequence number that can be used to index what the actual output should be.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#method_missing-instance_method" title="#method_missing (instance method)">- (Object) <strong>method_missing</strong>(m, *args, &amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>This is used to handle the details of our DSL.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#query-instance_method" title="#query (instance method)">- (Object) <strong>query</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Query function is called with the sequence (time evolution) number, and returns an array or hash of parameters that will be given to the input nodes.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#recurrence-instance_method" title="#recurrence (instance method)">- (Object) <strong>recurrence</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'></div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#report-instance_method" title="#report (instance method)">- (Object) <strong>report</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Report on evaluations.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#run_engine-instance_method" title="#run_engine (instance method)">- (Object) <strong>run_engine</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Run the engine.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#simple_fitness_error-instance_method" title="#simple_fitness_error (instance method)">- (Object) <strong>simple_fitness_error</strong>(v1, v2) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Helper function to do a simple fitness calculation on the basis of the sum of the square of the diffences of the element in the two vectors.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#stop_on_fitness-instance_method" title="#stop_on_fitness (instance method)">- (Object) <strong>stop_on_fitness</strong>(&amp;block) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Stop the progression once the fitness criteria is reached for the most fit critter.</p> </div></span>

</li>

      <li class="public ">
<span class="summary_signature">

    <a href="#uncondition_boolean_vector-instance_method" title="#uncondition_boolean_vector (instance method)">- (Object) <strong>uncondition_boolean_vector</strong>(vec, sig = :tanh) </a>

</span>

  <span class="summary_desc"><div class='inline'>

<p>Helper function to Uncondition boolean vectors to be +1 if true, -1 if false FIXME we need a better discrimination function.</p> </div></span>

</li>

  </ul>

<h3 class="inherited">Methods included from <span class='object_link'><a href="../NEAT.html" title="NEAT (module)">NEAT</a></span></h3>
<p class="inherited"><span class='object_link'><a href="../NEAT.html#controller-class_method" title="NEAT.controller (method)">controller</a></span>, <span class='object_link'><a href="../NEAT.html#controller%3D-class_method" title="NEAT.controller= (method)">controller=</a></span>, <span class='object_link'><a href="../NEAT.html#create_controller-class_method" title="NEAT.create_controller (method)">create_controller</a></span>, <span class='object_link'><a href="../NEAT.html#dpp-class_method" title="NEAT.dpp (method)">dpp</a></span>, <span class='object_link'><a href="../NEAT.html#gaussian-class_method" title="NEAT.gaussian (method)">gaussian</a></span>, <span class='object_link'><a href="../NEAT.html#new_innovation-class_method" title="NEAT.new_innovation (method)">new_innovation</a></span>, <span class='object_link'><a href="../NEAT.html#random_name_generator-class_method" title="NEAT.random_name_generator (method)">random_name_generator</a></span></p>

<div id=“method_missing_details” class=“method_details_list”>

<h2>Dynamic Method Handling</h2>
<p class="notice this">
  This class handles dynamic methods through the <tt>method_missing</tt> method

</p>

  <div class="method_details first">
<h3 class="signature first" id="method_missing-instance_method">

  - (<tt>Object</tt>) <strong>method_missing</strong>(m, *args, &amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>This is used to handle the details of our DSL.</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

114 115 116 117 118 119 120 121 122 123 124</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 114</span>

<span class='kw'>def</span> <span class='id identifier rubyid_method_missing'>method_missing</span><span class='lparen'>(</span><span class='id identifier rubyid_m'>m</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='comment'># we want to catch parameters settings here.

</span> <span class='kw'>if</span> <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_parms'>parms</span><span class='period'>.</span><span class='id identifier rubyid_respond_to?'>respond_to?</span> <span class='lparen'>(</span><span class='id identifier rubyid_assignment'>assignment</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_m'>m</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span> <span class='op'>+</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>=</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span>

  <span class='id identifier rubyid_raise'>raise</span> <span class='const'>NeatException</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Missing value(s) to %s</span><span class='tstring_end'>&quot;</span></span> <span class='op'>%</span> <span class='id identifier rubyid_m'>m</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
  <span class='id identifier rubyid_val'>val</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_size'>size</span> <span class='op'>==</span> <span class='int'>1</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_args'>args</span><span class='lbracket'>[</span><span class='int'>0</span><span class='rbracket'>]</span> <span class='op'>:</span> <span class='id identifier rubyid_args'>args</span>
  <span class='gvar'>$log</span><span class='period'>.</span><span class='id identifier rubyid_debug'>debug</span> <span class='lbrace'>{</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Caught method %s with parameter of %s</span><span class='tstring_end'>&quot;</span></span> <span class='op'>%</span> <span class='lbracket'>[</span><span class='id identifier rubyid_assignment'>assignment</span><span class='comma'>,</span> <span class='id identifier rubyid_val'>val</span><span class='rbracket'>]</span> <span class='rbrace'>}</span>
  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_parms'>parms</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_assignment'>assignment</span><span class='comma'>,</span> <span class='id identifier rubyid_val'>val</span><span class='rparen'>)</span>
<span class='kw'>else</span>
  <span class='kw'>super</span>
<span class='kw'>end</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

</div>

<div id="instance_method_details" class="method_details_list">
  <h2>Instance Method Details</h2>

    <div class="method_details first">
<h3 class="signature first" id="compare-instance_method">

  - (<tt>Object</tt>) <strong>compare</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>Fitness ordering – given 2 fitness numbers, use the &lt;=&gt; to compare them (or the equivalent, following the +1, 0, -1 that is in the sense of &lt;=&gt;)</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

63 64 65</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 63</span>

<span class='kw'>def</span> <span class='id identifier rubyid_compare'>compare</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_compare_func'>compare_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="condition_boolean_vector-instance_method">

  - (<tt>Object</tt>) <strong>condition_boolean_vector</strong>(vec, sig = :tanh)

</h3><div class=“docstring”>

<div class="discussion">

<p>Helper function to Condition boolean vectors to be +1 if true, -1 if false (0 if sigmoid)</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

81 82 83</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 81</span>

<span class='kw'>def</span> <span class='id identifier rubyid_condition_boolean_vector'>condition_boolean_vector</span><span class='lparen'>(</span><span class='id identifier rubyid_vec'>vec</span><span class='comma'>,</span> <span class='id identifier rubyid_sig'>sig</span> <span class='op'>=</span> <span class='symbol'>:tanh</span><span class='rparen'>)</span>

<span class='id identifier rubyid_vec'>vec</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='id identifier rubyid_b'>b</span> <span class='op'>?</span> <span class='int'>1</span> <span class='op'>:</span> <span class='lparen'>(</span><span class='lparen'>(</span><span class='id identifier rubyid_sig'>sig</span> <span class='op'>==</span> <span class='symbol'>:sigmoid</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='int'>0</span> <span class='op'>:</span> <span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span><span class='rbrace'>}</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="cost-instance_method">

  - (<tt>Object</tt>) <strong>cost</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>Calculation to add the cost to the fitness, resulting in a fitness that incorporates the cost for sorting purposes.</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

69 70 71</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 69</span>

<span class='kw'>def</span> <span class='id identifier rubyid_cost'>cost</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_cost_func'>cost_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="define-instance_method">

  - (<tt>Object</tt>) <strong>define</strong>(name = NEAT.random_name_generator, &amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>DSL – Define defines the parameters to the controller.</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 15</span>

<span class='kw'>def</span> <span class='id identifier rubyid_define'>define</span><span class='lparen'>(</span><span class='id identifier rubyid_name'>name</span> <span class='op'>=</span> <span class='const'>NEAT</span><span class='period'>.</span><span class='id identifier rubyid_random_name_generator'>random_name_generator</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='lbracket'>[</span>
 <span class='symbol'>:inputs</span><span class='comma'>,</span>
 <span class='symbol'>:outputs</span><span class='comma'>,</span>
 <span class='symbol'>:hidden</span>  <span class='comment'># we really don&#39;t care about mapping hidden neurons, but we&#39;ll ignore them later.

</span> <span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_iometh'>iometh</span><span class='op'>|</span>

   <span class='id identifier rubyid_instance_eval'>instance_eval</span> <span class='tstring'><span class='tstring_beg'>%Q[</span><span class='tstring_content'>
  def </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_iometh'>iometh</span><span class='embexpr_end'>}</span><span class='tstring_content'>(nodes = nil, &amp;block)
neui = unless nodes.nil?
         nodes
       else
         block.()
       end
NEAT::controller.neural_</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_iometh'>iometh</span><span class='embexpr_end'>}</span><span class='tstring_content'> = if neui.kind_of? Hash
                                      neui
                                    else
                                      Hash[neui.map{|n| [NEAT::random_name_generator, n]}]
                                    end
  end</span><span class='tstring_end'>]</span></span>
 <span class='kw'>end</span>
 <span class='id identifier rubyid_block'>block</span><span class='period'>.</span><span class='lparen'>(</span><span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='rparen'>)</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="evolve-instance_method">

  - (<tt>Object</tt>) <strong>evolve</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>DSL – Run evolution</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 39</span>

<span class='kw'>def</span> <span class='id identifier rubyid_evolve'>evolve</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='comment'># Query function is called with the sequence (time evolution) number,

</span> <span class='comment'># and returns an array or hash of parameters that will be given </span> <span class='comment'># to the input nodes. In the case of hash, the keys in the hash </span> <span class='comment'># shall correspond to the names given to the input neurons. </span> <span class='kw'>def</span> <span class='id identifier rubyid_query'>query</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_query_func'>query_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='kw'>end</span>

<span class='kw'>def</span> <span class='id identifier rubyid_recurrence'>recurrence</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>
  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_recurrence_func'>recurrence_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='kw'>end</span>

<span class='comment'># fitness function calls the block with 2 vectors or two hashes, input and output

</span> <span class='comment'># vectors of the critter being evaluated for fitness, as well as a sequence </span> <span class='comment'># number that can be used to index what the actual output should be. </span> <span class='comment'># |vin, vout, seq| </span> <span class='kw'>def</span> <span class='id identifier rubyid_fitness'>fitness</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_fitness_func'>fitness_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='kw'>end</span>

<span class='comment'># Fitness ordering -- given 2 fitness numbers,

</span> <span class='comment'># use the &lt;=&gt; to compare them (or the equivalent, following </span> <span class='comment'># the +1, 0, -1 that is in the sense of &lt;=&gt;) </span> <span class='kw'>def</span> <span class='id identifier rubyid_compare'>compare</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_compare_func'>compare_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='kw'>end</span>

<span class='comment'># Calculation to add the cost to the fitness, resulting in a fitness

</span> <span class='comment'># that incorporates the cost for sorting purposes. </span> <span class='kw'>def</span> <span class='id identifier rubyid_cost'>cost</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_cost_func'>cost_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='kw'>end</span>

<span class='comment'># Stop the progression once the fitness criteria is reached

</span> <span class='comment'># for the most fit critter </span> <span class='kw'>def</span> <span class='id identifier rubyid_stop_on_fitness'>stop_on_fitness</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

  <span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_stop_on_fit_func'>stop_on_fit_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='kw'>end</span>

<span class='comment'># Helper function to

</span> <span class='comment'># Condition boolean vectors to be +1 if true, -1 if false (0 if sigmoid) </span> <span class='kw'>def</span> <span class='id identifier rubyid_condition_boolean_vector'>condition_boolean_vector</span><span class='lparen'>(</span><span class='id identifier rubyid_vec'>vec</span><span class='comma'>,</span> <span class='id identifier rubyid_sig'>sig</span> <span class='op'>=</span> <span class='symbol'>:tanh</span><span class='rparen'>)</span>

  <span class='id identifier rubyid_vec'>vec</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='id identifier rubyid_b'>b</span> <span class='op'>?</span> <span class='int'>1</span> <span class='op'>:</span> <span class='lparen'>(</span><span class='lparen'>(</span><span class='id identifier rubyid_sig'>sig</span> <span class='op'>==</span> <span class='symbol'>:sigmoid</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='int'>0</span> <span class='op'>:</span> <span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span><span class='rbrace'>}</span>
<span class='kw'>end</span>

<span class='comment'># Helper function to

</span> <span class='comment'># Uncondition boolean vectors to be +1 if true, -1 if false </span> <span class='comment'># FIXME we need a better discrimination function </span> <span class='kw'>def</span> <span class='id identifier rubyid_uncondition_boolean_vector'>uncondition_boolean_vector</span><span class='lparen'>(</span><span class='id identifier rubyid_vec'>vec</span><span class='comma'>,</span> <span class='id identifier rubyid_sig'>sig</span> <span class='op'>=</span> <span class='symbol'>:tanh</span><span class='rparen'>)</span>

  <span class='id identifier rubyid_vec'>vec</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_o'>o</span><span class='op'>|</span> <span class='id identifier rubyid_o'>o</span> <span class='op'>&gt;</span> <span class='lparen'>(</span><span class='lparen'>(</span><span class='id identifier rubyid_sig'>sig</span> <span class='op'>==</span> <span class='symbol'>:sigmoid</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='float'>0.5</span> <span class='op'>:</span> <span class='int'>0</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='kw'>true</span> <span class='op'>:</span> <span class='kw'>false</span><span class='rbrace'>}</span>
<span class='kw'>end</span>

<span class='comment'># Helper function to do a simple fitness calculation

</span> <span class='comment'># on the basis of the sum of the square of the diffences </span> <span class='comment'># of the element in the two vectors. </span> <span class='kw'>def</span> <span class='id identifier rubyid_simple_fitness_error'>simple_fitness_error</span><span class='lparen'>(</span><span class='id identifier rubyid_v1'>v1</span><span class='comma'>,</span> <span class='id identifier rubyid_v2'>v2</span><span class='rparen'>)</span>

  <span class='id identifier rubyid_sqrt'>sqrt</span> <span class='id identifier rubyid_v1'>v1</span><span class='period'>.</span><span class='id identifier rubyid_zip'>zip</span><span class='lparen'>(</span><span class='id identifier rubyid_v2'>v2</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='lparen'>(</span><span class='id identifier rubyid_a'>a</span> <span class='op'>-</span> <span class='id identifier rubyid_b'>b</span><span class='rparen'>)</span> <span class='op'>**</span> <span class='float'>2.0</span><span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_reduce'>reduce</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_m'>m</span><span class='comma'>,</span> <span class='id identifier rubyid_c'>c</span><span class='op'>|</span> <span class='id identifier rubyid_m'>m</span> <span class='op'>+</span> <span class='id identifier rubyid_c'>c</span><span class='rbrace'>}</span>
<span class='kw'>end</span>

<span class='id identifier rubyid_block'>block</span><span class='period'>.</span><span class='lparen'>(</span><span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='rparen'>)</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="fitness-instance_method">

  - (<tt>Object</tt>) <strong>fitness</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>fitness function calls the block with 2 vectors or two hashes, input and output vectors of the critter being evaluated for fitness, as well as a sequence number that can be used to index what the actual output should be. |vin, vout, seq|</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

56 57 58</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 56</span>

<span class='kw'>def</span> <span class='id identifier rubyid_fitness'>fitness</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_fitness_func'>fitness_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="query-instance_method">

  - (<tt>Object</tt>) <strong>query</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>Query function is called with the sequence (time evolution) number, and returns an array or hash of parameters that will be given to the input nodes. In the case of hash, the keys in the hash shall correspond to the names given to the input neurons.</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

44 45 46</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 44</span>

<span class='kw'>def</span> <span class='id identifier rubyid_query'>query</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_query_func'>query_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="recurrence-instance_method">

  - (<tt>Object</tt>) <strong>recurrence</strong>(&amp;block)

</h3><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

48 49 50</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 48</span>

<span class='kw'>def</span> <span class='id identifier rubyid_recurrence'>recurrence</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_recurrence_func'>recurrence_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="report-instance_method">

  - (<tt>Object</tt>) <strong>report</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>Report on evaluations</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

103 104 105</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 103</span>

<span class='kw'>def</span> <span class='id identifier rubyid_report'>report</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_report_hook'>report_hook</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="run_engine-instance_method">

  - (<tt>Object</tt>) <strong>run_engine</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>Run the engine. The block is called on each generation.</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

108 109 110 111</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 108</span>

<span class='kw'>def</span> <span class='id identifier rubyid_run_engine'>run_engine</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_end_run_func'>end_run_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>
<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_run'>run</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="simple_fitness_error-instance_method">

  - (<tt>Object</tt>) <strong>simple_fitness_error</strong>(v1, v2)

</h3><div class=“docstring”>

<div class="discussion">

<p>Helper function to do a simple fitness calculation on the basis of the sum of the square of the diffences of the element in the two vectors.</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

95 96 97</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 95</span>

<span class='kw'>def</span> <span class='id identifier rubyid_simple_fitness_error'>simple_fitness_error</span><span class='lparen'>(</span><span class='id identifier rubyid_v1'>v1</span><span class='comma'>,</span> <span class='id identifier rubyid_v2'>v2</span><span class='rparen'>)</span>

<span class='id identifier rubyid_sqrt'>sqrt</span> <span class='id identifier rubyid_v1'>v1</span><span class='period'>.</span><span class='id identifier rubyid_zip'>zip</span><span class='lparen'>(</span><span class='id identifier rubyid_v2'>v2</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='lparen'>(</span><span class='id identifier rubyid_a'>a</span> <span class='op'>-</span> <span class='id identifier rubyid_b'>b</span><span class='rparen'>)</span> <span class='op'>**</span> <span class='float'>2.0</span><span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_reduce'>reduce</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_m'>m</span><span class='comma'>,</span> <span class='id identifier rubyid_c'>c</span><span class='op'>|</span> <span class='id identifier rubyid_m'>m</span> <span class='op'>+</span> <span class='id identifier rubyid_c'>c</span><span class='rbrace'>}</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="stop_on_fitness-instance_method">

  - (<tt>Object</tt>) <strong>stop_on_fitness</strong>(&amp;block)

</h3><div class=“docstring”>

<div class="discussion">

<p>Stop the progression once the fitness criteria is reached for the most fit critter</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

75 76 77</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 75</span>

<span class='kw'>def</span> <span class='id identifier rubyid_stop_on_fitness'>stop_on_fitness</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_block'>block</span><span class='rparen'>)</span>

<span class='const'>NEAT</span><span class='op'>::</span><span class='id identifier rubyid_controller'>controller</span><span class='period'>.</span><span class='id identifier rubyid_stop_on_fit_func'>stop_on_fit_func</span> <span class='op'>=</span> <span class='id identifier rubyid_block'>block</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

    <div class="method_details ">
<h3 class="signature " id="uncondition_boolean_vector-instance_method">

  - (<tt>Object</tt>) <strong>uncondition_boolean_vector</strong>(vec, sig = :tanh)

</h3><div class=“docstring”>

<div class="discussion">

<p>Helper function to Uncondition boolean vectors to be +1 if true, -1 if false FIXME we need a better discrimination function</p>

</div>

</div> <div class=“tags”>

</div><table class=“source_code”>

<tr>
  <td>
    <pre class="lines">

88 89 90</pre>

</td>
<td>
  <pre class="code"><span class="info file"># File 'lib/rubyneat/dsl.rb', line 88</span>

<span class='kw'>def</span> <span class='id identifier rubyid_uncondition_boolean_vector'>uncondition_boolean_vector</span><span class='lparen'>(</span><span class='id identifier rubyid_vec'>vec</span><span class='comma'>,</span> <span class='id identifier rubyid_sig'>sig</span> <span class='op'>=</span> <span class='symbol'>:tanh</span><span class='rparen'>)</span>

<span class='id identifier rubyid_vec'>vec</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span><span class='op'>|</span><span class='id identifier rubyid_o'>o</span><span class='op'>|</span> <span class='id identifier rubyid_o'>o</span> <span class='op'>&gt;</span> <span class='lparen'>(</span><span class='lparen'>(</span><span class='id identifier rubyid_sig'>sig</span> <span class='op'>==</span> <span class='symbol'>:sigmoid</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='float'>0.5</span> <span class='op'>:</span> <span class='int'>0</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='kw'>true</span> <span class='op'>:</span> <span class='kw'>false</span><span class='rbrace'>}</span>

<span class='kw'>end</span></pre>

  </td>
</tr>

</table> </div>

</div>

</div>

  <div id="footer">
Generated on Sun Mar 16 16:36:51 2014 by
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
0.8.7.3 (ruby-2.1.1).

</div>

</body>

</html>