<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>module ODE - RDoc Documentation</title>

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
</script>

<script src="./js/jquery.js"></script>
<script src="./js/darkfish.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">



<body id="top" role="document" class="module">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="./index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  

  <div id="class-metadata">
    
    
    <div id="includes-section" class="nav-section">
  <h3>Included Modules</h3>

  <ul class="link-list">
  
  
    <li><a class="include" href="OptimalizationProperties.html">OptimalizationProperties</a>
  
  
  </ul>
</div>

    
    <!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    
    <li ><a href="#method-c-opposite">::opposite</a>
    
    <li ><a href="#method-c-opposite_population">::opposite_population</a>
    
    <li ><a href="#method-c-run">::run</a>
    
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-labelledby="module-ODE">
  <h1 id="module-ODE" class="module">
    module ODE
  </h1>

  <section class="description">
    
  </section>

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    

    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Class Methods</h3>
       </header>

    
      <div id="method-c-opposite" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">opposite</span><span
            class="method-args">(max, min, vector)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="opposite-source">
            <pre><span class="ruby-comment"># File lib/ode.rb, line 6</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">ODE</span>.<span class="ruby-identifier">opposite</span>(<span class="ruby-identifier">max</span>, <span class="ruby-identifier">min</span>, <span class="ruby-identifier">vector</span>)
  <span class="ruby-identifier">x</span> = <span class="ruby-identifier">vector</span>.<span class="ruby-identifier">collect</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">max</span><span class="ruby-operator">+</span><span class="ruby-identifier">min</span><span class="ruby-operator">-</span><span class="ruby-identifier">v</span>}
  <span class="ruby-keyword">return</span> <span class="ruby-constant">Vector</span>[<span class="ruby-operator">*</span><span class="ruby-identifier">x</span>]
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-opposite_population" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">opposite_population</span><span
            class="method-args">(population)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>return Array with Vector object</p>
          
          

          
          <div class="method-source-code" id="opposite_population-source">
            <pre><span class="ruby-comment"># File lib/ode.rb, line 13</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">ODE</span>.<span class="ruby-identifier">opposite_population</span>(<span class="ruby-identifier">population</span>)
  <span class="ruby-identifier">values_in_dimension</span> = []
  <span class="ruby-identifier">dimension</span> = <span class="ruby-identifier">population</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">size</span>

  <span class="ruby-identifier">dimension</span>.<span class="ruby-identifier">times</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">values_in_dimension</span>[<span class="ruby-identifier">i</span>] = []}

  <span class="ruby-comment">#Znalezienie MAX i MIN zakresu w każdej przestrzeni</span>
  <span class="ruby-identifier">population</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">x</span>.<span class="ruby-identifier">each</span>.<span class="ruby-identifier">with_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">xi</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">values_in_dimension</span>[<span class="ruby-identifier">index</span>] <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">xi</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">dimension</span>.<span class="ruby-identifier">times</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">values_in_dimension</span>[<span class="ruby-identifier">i</span>].<span class="ruby-identifier">sort!</span>}

  <span class="ruby-identifier">opposite_population</span> = <span class="ruby-identifier">population</span>.<span class="ruby-identifier">collect</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">ox_tab</span> = []
    <span class="ruby-identifier">x</span>.<span class="ruby-identifier">each</span>.<span class="ruby-identifier">with_index</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">xi</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span> <span class="ruby-identifier">ox_tab</span>[<span class="ruby-identifier">index</span>] = <span class="ruby-identifier">values_in_dimension</span>[<span class="ruby-identifier">index</span>].<span class="ruby-identifier">first</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">values_in_dimension</span>[<span class="ruby-identifier">index</span>].<span class="ruby-identifier">last</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">xi</span>}
    <span class="ruby-constant">Vector</span>[<span class="ruby-operator">*</span><span class="ruby-identifier">ox_tab</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-run" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">run</span><span
            class="method-args">(&function)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p><a href="ODE.html">ODE</a> algorithm</p>
          
          

          
          <div class="method-source-code" id="run-source">
            <pre><span class="ruby-comment"># File lib/ode.rb, line 36</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">ODE</span>.<span class="ruby-identifier">run</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">function</span>)
  <span class="ruby-identifier">rand</span> = <span class="ruby-constant">Random</span>.<span class="ruby-identifier">new</span>()
  <span class="ruby-identifier">results</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-identifier">function_calls</span> = <span class="ruby-value">0</span>
  <span class="ruby-identifier">current_min</span>=<span class="ruby-value">0</span>

  <span class="ruby-comment"># Inicjalizacja</span>
  <span class="ruby-identifier">x</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-identifier">ox</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
  
  <span class="ruby-constant">POPULATION_COUNT</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">tab</span> = []
    <span class="ruby-constant">DIMENSION</span>.<span class="ruby-identifier">times</span>{ <span class="ruby-identifier">tab</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span>()<span class="ruby-operator">*</span><span class="ruby-constant">MAX</span> <span class="ruby-operator">+</span> <span class="ruby-constant">MIN</span> }
    <span class="ruby-identifier">x</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Vector</span>[<span class="ruby-operator">*</span><span class="ruby-identifier">tab</span>]
    <span class="ruby-identifier">ox</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">ODE</span><span class="ruby-operator">::</span><span class="ruby-identifier">opposite</span>(<span class="ruby-constant">MAX</span>, <span class="ruby-constant">MIN</span>, <span class="ruby-identifier">x</span>.<span class="ruby-identifier">last</span>)

    <span class="ruby-identifier">function_calls</span> <span class="ruby-operator">+=</span> <span class="ruby-value">2</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">x</span> = <span class="ruby-identifier">x</span><span class="ruby-operator">+</span><span class="ruby-identifier">ox</span>
  <span class="ruby-identifier">x</span>.<span class="ruby-identifier">sort_by!</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">x</span>)}
  <span class="ruby-identifier">x</span>= <span class="ruby-identifier">x</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-constant">POPULATION_COUNT</span>]


  <span class="ruby-identifier">iterations</span> = <span class="ruby-value">0</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-comment"># Mutacja</span>
    <span class="ruby-identifier">v</span> = <span class="ruby-identifier">x</span>.<span class="ruby-identifier">collect</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">current</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">begin</span> <span class="ruby-identifier">x1</span> = <span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span>(<span class="ruby-constant">POPULATION_COUNT</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x1</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">current</span>
      <span class="ruby-keyword">begin</span> <span class="ruby-identifier">x2</span> = <span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span>(<span class="ruby-constant">POPULATION_COUNT</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x2</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">current</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x2</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x1</span>]
      <span class="ruby-keyword">begin</span> <span class="ruby-identifier">x3</span> = <span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span>(<span class="ruby-constant">POPULATION_COUNT</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x3</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">current</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x3</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x2</span>] <span class="ruby-keyword">or</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x3</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x1</span>]
      <span class="ruby-identifier">x</span>[<span class="ruby-identifier">x1</span>] <span class="ruby-operator">+</span> (<span class="ruby-identifier">x</span>[<span class="ruby-identifier">x2</span>]<span class="ruby-operator">-</span><span class="ruby-identifier">x</span>[<span class="ruby-identifier">x3</span>])<span class="ruby-operator">*</span><span class="ruby-constant">F</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">u</span> = []
    <span class="ruby-identifier">tempU</span> = []

    <span class="ruby-comment"># Rekombinacja</span>
    <span class="ruby-constant">POPULATION_COUNT</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">k</span> = <span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span>(<span class="ruby-constant">DIMENSION</span>)

      <span class="ruby-constant">DIMENSION</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">j</span><span class="ruby-operator">|</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span>() <span class="ruby-operator">&lt;</span> <span class="ruby-constant">CR</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">j</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">k</span>
          <span class="ruby-identifier">tempU</span>[<span class="ruby-identifier">j</span>] = <span class="ruby-identifier">v</span>[<span class="ruby-identifier">i</span>][<span class="ruby-identifier">j</span>]
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">tempU</span>[<span class="ruby-identifier">j</span>] = <span class="ruby-identifier">x</span>[<span class="ruby-identifier">i</span>][<span class="ruby-identifier">j</span>]
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">u</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Vector</span>[<span class="ruby-operator">*</span><span class="ruby-identifier">tempU</span>]

      <span class="ruby-keyword">if</span> <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">x</span>[<span class="ruby-identifier">i</span>]) <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">u</span>[<span class="ruby-identifier">i</span>])
        <span class="ruby-identifier">x</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-identifier">u</span>[<span class="ruby-identifier">i</span>]
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">function_calls</span><span class="ruby-operator">+=</span><span class="ruby-value">2</span>
    <span class="ruby-keyword">end</span>


    <span class="ruby-identifier">current_min</span> = <span class="ruby-identifier">x</span>.<span class="ruby-identifier">min_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">xi</span><span class="ruby-operator">|</span> <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">xi</span>)}
    <span class="ruby-identifier">current_min</span> = <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">current_min</span>)
    <span class="ruby-identifier">results</span>[<span class="ruby-identifier">iterations</span>] = <span class="ruby-identifier">current_min</span>



    <span class="ruby-comment"># Opoosite vectors</span>
    <span class="ruby-keyword">if</span>(<span class="ruby-identifier">rand</span>.<span class="ruby-identifier">rand</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">JR</span>)
      <span class="ruby-identifier">ox</span> = <span class="ruby-constant">ODE</span><span class="ruby-operator">::</span><span class="ruby-identifier">opposite_population</span>(<span class="ruby-identifier">x</span>)
      <span class="ruby-identifier">x</span> = <span class="ruby-identifier">x</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">ox</span>
      <span class="ruby-identifier">x</span>.<span class="ruby-identifier">sort_by!</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">x</span>)}
      <span class="ruby-identifier">x</span>= <span class="ruby-identifier">x</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-constant">POPULATION_COUNT</span>]
      <span class="ruby-identifier">function_calls</span> <span class="ruby-operator">+=</span> <span class="ruby-value">2</span><span class="ruby-operator">*</span><span class="ruby-constant">POPULATION_COUNT</span>

      <span class="ruby-identifier">iterations</span> <span class="ruby-operator">+=</span><span class="ruby-value">1</span>
      <span class="ruby-identifier">current_min</span> = <span class="ruby-identifier">x</span>.<span class="ruby-identifier">min_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">xi</span><span class="ruby-operator">|</span> <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">xi</span>)}
      <span class="ruby-identifier">current_min</span> = <span class="ruby-identifier">function</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">current_min</span>)
      <span class="ruby-identifier">results</span>[<span class="ruby-identifier">iterations</span>] = <span class="ruby-identifier">current_min</span>



    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">iterations</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span> <span class="ruby-keyword">while</span> <span class="ruby-identifier">current_min</span> <span class="ruby-operator">&gt;</span> <span class="ruby-constant">VTR</span>  <span class="ruby-keyword">and</span>  <span class="ruby-identifier">function_calls</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">MAX_FUNCTION_CALLS</span>



  <span class="ruby-keyword">if</span> <span class="ruby-identifier">function_calls</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">MAX_FUNCTION_CALLS</span>
    <span class="ruby-identifier">succes</span> = <span class="ruby-keyword">true</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">succes</span> = <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">data</span> = {<span class="ruby-identifier">iteracje</span><span class="ruby-operator">:</span> <span class="ruby-identifier">iterations</span>, <span class="ruby-identifier">succes</span><span class="ruby-operator">:</span> <span class="ruby-identifier">succes</span>, <span class="ruby-identifier">results</span><span class="ruby-operator">:</span> <span class="ruby-identifier">results</span>, <span class="ruby-identifier">nfc</span><span class="ruby-operator">:</span> <span class="ruby-identifier">function_calls</span> }
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">data</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
    </section>
  
  </section>
</main>


<footer id="validator-badges" role="contentinfo">
  <p><a href="http://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="http://docs.seattlerb.org/rdoc/">RDoc</a> 4.2.0.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>

