767 lines
17 KiB
HTML
767 lines
17 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-us">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Caught in the Net</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="theme-color" content="#157878">
|
|
<link rel="stylesheet" href="/css/normalize.css">
|
|
<!--<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>-->
|
|
<link rel="stylesheet" href="/fonts/opensans.css">
|
|
<link rel="stylesheet" href="/css/cayman.css">
|
|
</head>
|
|
|
|
|
|
<body>
|
|
<section class="page-header">
|
|
<h1 class="project-name">Caught in the Net</h1>
|
|
<h2 class="project-tagline">La rete ti cattura ma libera il pensiero</h2>
|
|
<a class="btn" href="/">Home</a>
|
|
<a class="btn" href="/about/">About me</a>
|
|
<a class="btn" href="/contattami/">Contact me</a>
|
|
<a class="btn" href="/archive/">Archive</a>
|
|
<a class="btn" href="/feed.xml">RSS</a>
|
|
<a class="btn" href="http://francescomecca.eu/git/explore/repos">Personal Git</a>
|
|
<a class="btn" href="https://github.com/FraMecca">Github</a>
|
|
<a class="btn" href="/curriculum/CV_Mecca_Francesco.pdf">Curriculum</a>
|
|
</section>
|
|
|
|
|
|
<section class="main-content">
|
|
|
|
<div class="post">
|
|
<h1 class="post-title">Interpolation using a genetic algorithm</h1>
|
|
<span class="post-date">15 May 2016</span>
|
|
<p>This weekend I was in Milan to get a visa and I had the opportunity to work with a friend, Michele, on genetic algorithms.
|
|
It was the first time I dig up in such field and it was very exciting.
|
|
In this post I want to explain some bits of our work.</p>
|
|
|
|
<h2 id="a-brief-introduction-to-ga">A brief introduction to GA</h2>
|
|
|
|
<p>A genetic algorithm is a search/optimization algorithm that uses an heuristic approach to reduce the search space and evolve gradually to a solution.</p>
|
|
|
|
<h4 id="population">Population</h4>
|
|
|
|
<p>It is an algorithm that has its root in the theory of natural selectioni by Charles Darwin.
|
|
The main components of a GA are:</p>
|
|
|
|
<ul>
|
|
<li>the population, that concentrate all the available solutions at a given time;</li>
|
|
<li>the fitness function, that gives an approximation of the quality of the solution codified by a given member of the population.</li>
|
|
</ul>
|
|
|
|
<p>In a GA the first thing to do is to generate a population.</p>
|
|
|
|
<p>A population is a group of objects with given attributes, usually a string, and they contains in some form the solution (usually inside a string); the first population is randomly generated and contains a big number of solutions, but not every solution (this is not a bruteforce approach).</p>
|
|
|
|
<p>After this step the fitness functions evaluates the quality of every solution that a given member carries: the evaluation should be considered from a bottom up point of view.</p>
|
|
|
|
<h4 id="reproduction">Reproduction</h4>
|
|
|
|
<p>Now, as in Darwin's theory of evolution, the member of the population are going to "reproduce": two members are going to be coupled to generate a new member of the second generation and every child member will contain a solution that is the product of the original genes of their parent members.</p>
|
|
|
|
<p>This time the reproduction of the population into a second one is not entirely random. The fitness function gives us an approximation of the quality of every gene that a member carries and by the rule of the "survival by the fittest" the probability that a member is going to reproduce with another one is proportional to the quality of its genes.</p>
|
|
|
|
<p>When we have a second generation of members we can recur on our GA and generate a third generation. From this point we can recur until we converge to a solution that is common to every member, or at least that is suited to our needs.</p>
|
|
|
|
<h4 id="mutation">Mutation</h4>
|
|
|
|
<p>Actually, in some cases, a mutation function can be added, so that, like in real world, some times the genes are "scrambled" indipendently from the fitness function.</p>
|
|
|
|
<p>There is more to a GA, for example we could talk about possible ways of storing the genes inside a member or when to use mutation, anyway I want to stop here and continue with an analysis of my problem.</p>
|
|
|
|
<h2 id="interpolating-a-function-using-a-ga">Interpolating a function using a GA</h2>
|
|
|
|
<p>Me and Michele decided to spend some time developing a little python script to explore GA capabilities and we decided to interpolate some points on a cartesian plane.</p>
|
|
|
|
<p>Our program, that is available <a href="http://francescomecca.eu:3000/pesceWanda/interpol_genetica">here</a> uses a class to define the various members of the population and a string for the genes, a class as well for the points on the plane.</p>
|
|
|
|
<p>The fitness function is not as precise as it should be because this is only a proof of concept:</p>
|
|
|
|
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">mutationProbability</span> <span class="o">=</span> <span class="mf">0.1</span>
|
|
<span class="n">rangeLimit</span> <span class="o">=</span> <span class="mi">5</span>
|
|
<span class="k">def</span> <span class="nf">fitness</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">pointList</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
|
|
<span class="n">value</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pointList</span><span class="p">:</span>
|
|
<span class="n">y</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
|
<span class="n">y</span> <span class="o">+=</span> <span class="n">item</span><span class="o">.</span><span class="n">gene</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="nb">pow</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
|
|
<span class="n">result</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="p">(</span><span class="nb">abs</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">y</span> <span class="o">-</span> <span class="n">y</span><span class="p">)</span> <span class="o">/</span> <span class="n">rangeLimit</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">result</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
|
|
<span class="n">result</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="n">value</span> <span class="o">+=</span> <span class="n">result</span>
|
|
<span class="k">return</span> <span class="n">value</span> <span class="o">/</span> <span class="n">n</span></code></pre></figure>
|
|
|
|
<p>item is just a member of the population, poinList is the list of points and n is the number of points (n - 1 is the grade of the function).</p>
|
|
<div class="highlight"><pre><code class="language-" data-lang="">for i in range(n):
|
|
y += item.gene[i] * pow(p.x, i)
|
|
</code></pre></div>
|
|
<p>this piece of code gives us the value of the function encoded in the genes in the points of pointList;</p>
|
|
<div class="highlight"><pre><code class="language-" data-lang="">result = 1 - (abs (p.y - y) / rangeLimit)
|
|
if result < 0:
|
|
result = 0
|
|
</code></pre></div>
|
|
<p>while here the script stores 1 - the previous result because if the GA has yield a good result there should be distance = 0 from the function evaluated and the points; If this is the case, the fitness function should attribute the highest possible reproduction probability for that member.
|
|
At the end the fitness function returns the total value over the number of points evaluated.</p>
|
|
|
|
<p>As you can see this fitness function is by no means an optimal one. The reproduction probability is higher for functions that crosses some points and are really distant from others rather than for functions that are closer to every point but crosses none.
|
|
Anyway for simple cases the GA yields good results, as an example for points (0 0), (1 4), (2 9) one of the member with the highest reproduction probability has this function in its genes:</p>
|
|
<div class="highlight"><pre><code class="language-" data-lang="">-0.0487839869993989 * x^0 + 4.600339125358671 * x^1 + -0.2780958075230644 * x^2
|
|
</code></pre></div>
|
|
<p>that crosses this points: (0 -0.0488), (1 4.2735), (2 8.0395) given 80 iterations, initial population of 600 members and a two digit approximation.</p>
|
|
|
|
<p>For a more precise computation a higher population size and a really high number of iterations should be used.</p>
|
|
|
|
</div>
|
|
|
|
<!--<div class="related">-->
|
|
<!--<related-posts />-->
|
|
<!--<h2>Related Posts</h2>-->
|
|
<!--<ul class="related-posts">-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2018/03/27/addio-reddit/">-->
|
|
<!--Addio Reddit-->
|
|
<!--<small>27 Mar 2018</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2017/10/02/minidoxguide/">-->
|
|
<!--Minidox, a guide for the Europeans and the Scrooges-->
|
|
<!--<small>02 Oct 2017</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2017/05/09/vaporwave/">-->
|
|
<!--Cyber-utopia and vaporwave-->
|
|
<!--<small>09 May 2017</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2017/05/07/latestage_handbrake/">-->
|
|
<!--Late Stage Capitalism meets FOSS-->
|
|
<!--<small>07 May 2017</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--<li>-->
|
|
<!--<h3>-->
|
|
<!--<a href="/pescewanda/2017/03/20/spazio-digitale-rant-facebook__eng/">-->
|
|
<!--Some shallow thoughts from my tiny virtual space-->
|
|
<!--<small>20 Mar 2017</small>-->
|
|
<!--</a>-->
|
|
<!--</h3>-->
|
|
<!--</li>-->
|
|
<!---->
|
|
<!--</ul>-->
|
|
<!--</div>-->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
<li class="relatedPost">
|
|
<a href="/pescewanda/2016/11/11/machine-learning-PARTE2/">Capire il Machine Learning (parte 2)
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="relatedPost">
|
|
<a href="/pescewanda/2016/11/10/machine-learning-intro/">Capire il Machine Learning (parte 1)
|
|
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<footer class="site-footer">
|
|
<!--<span class="site-footer-owner"><a href="http://francescomecca.eu">Caught in the Net</a> is maintained by <a href="contattami">Francesco Mecca</a>.</span>-->
|
|
<span> CC BY-SA 4.0 International.</br> </span>
|
|
<span class="site-footer-credits"><a href="https://jekyllrb.com">Jekyll</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
|
|
</footer>
|
|
|
|
</section>
|
|
|
|
</body>
|
|
</html>
|