francescomecca.eu/_site/index.html
PesceWanda 9075748d9a le s
2016-09-17 15:16:44 +02:00

720 lines
35 KiB
HTML

<!DOCTYPE html>
<html lang="en-us">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Enable responsiveness on mobile devices-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>
Caught in the Net &middot; La rete ti cattura ma libera il pensiero
</title>
<!-- CSS -->
<link rel="stylesheet" href="/public/css/poole.css">
<link rel="stylesheet" href="/public/css/syntax.css">
<link rel="stylesheet" href="/public/css/hyde.css">
<!-- Icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="/public/favicon.ico">
<!-- RSS -->
<link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
</head>
<body class="theme-base-09">
<div class="sidebar">
<div class="container sidebar-sticky">
<div class="sidebar-about">
<h1>
<a href="/">
Caught in the Net
</a>
</h1>
<p class="lead"></p>
</div>
<nav class="sidebar-nav">
<a class="sidebar-nav-item active" href="/">Home</a>
<a class="sidebar-nav-item" href="/about/">About</a>
<a class="sidebar-nav-item" href="/archive/">Archive</a>
<a class="sidebar-nav-item" href="/contattami/">Contact me</a>
<a class="sidebar-nav-item" href="/feed.xml">RSS</a>
<a class="sidebar-nav-item" href="http://francescomecca.eu:3000">Personal Git</a>
<span class="sidebar-nav-item" href="" >Powered by Jekyll and Hyde</span>
</nav>
<p>&copy; 2016. CC BY-SA 4.0 International </p>
</div>
</div>
<h3 class="masthead-title">
<a href="/" title="Home">Caught in the Net</a>
</h3>
<div class="content container">
<div class="posts">
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/07/07/pres-berk/">
A short talk about cryptography at the Berkman Klein Center
</a>
</h1>
<span class="post-date">07 Jul 2016</span>
<p>The 7th of July me and <a href="http://studentprivacy.ca">Aaron</a>, as interns at the <a href="http://cyber.law.harvard.edu">Berkman Klein for Internet and Society</a>, gave a presentation on the basics of cryptography and a quick overview on the essential tools.</p>
<p>What follows is a short summary of that presentation. The slides are available <a href="/wp-content/uploads/2016/fwneas.pptx">here</a></p>
<h3 id="whose-security">Whose Security?</h3>
<p>Let&#39;s define what security is. Security is the possibility to being set free from structural costraints, and as that we can distinguish various levels of security depending on who we are.</p>
<p>Also, if we want to investigate security we should also define our threats: security, as being set free, from intelligence surveillance can be our target. Our concerns as different if we consider instead security from censorship or corporation data mining.</p>
<p><img src="/wp-content/uploads/2016/godmode.png" alt="uber god view">
<img src="/wp-content/uploads/2016/fb.png" alt="facebook law enforcement form"></p>
<p>What is shown above is the <a href="http://www.theverge.com/2016/1/6/10726004/uber-god-mode-settlement-fine">Uber God View</a>, a tool Uber used to track a Buzzfeed&#39;s journalist locations, and the Facebook standard form that is given to law enforcement when requested.</p>
<h3 id="security-is-a-state-of-mind">Security is a state of mind</h3>
<p>Security is hard. It is really rare to reach a state of complete security and even in that case, it depends on our target.</p>
<p>What is important is to train ourselves to security. Security is a state of mind and there are no tools that automatically protect us without our active partecipation.</p>
<p>Let&#39;s explore that in details.</p>
<h3 id="the-layers-of-security">The layers of security</h3>
<p>We can distinguish four layers of security:</p>
<ul>
<li>Device Security</li>
<li>Network Security</li>
<li>Message Security</li>
<li>Human Security</li>
</ul>
<h6 id="device-security-where-everything-happens">Device Security, where everything happens</h6>
<p>Device security is related to the &quot;physical host&quot;.</p>
<p>If the computer we use is tampered, at the hardware level, or the phone is bugged, there is no way to escape using higher level tools.</p>
<p>In other words, it doesn&#39;t matter if we use a super secure password if our computer is registering all our keystrokes and send them to a third party.</p>
<p>Also, device security is useful if we consider that our device can fall into the hands of attackers that may be able to traceback all the activities.</p>
<p>Some precautions for this purpose:</p>
<ul>
<li>full disk encryption</li>
<li>minimal set of application installed</li>
<li>open source operating systems</li>
</ul>
<h6 id="network-security">Network Security</h6>
<p>The network is the infrastructures that our device is attached to. In most of the case, when we consider our computer is the internet (and the GSM network in case of mobile phones).</p>
<p>Network security is essential to evade censorship, behavioural tracking and identity theft.</p>
<p>Some tools that may help in this case:</p>
<ul>
<li>vpn</li>
<li>tor</li>
<li>p2p networks</li>
<li>mesh networks</li>
</ul>
<p>And for the web:</p>
<ul>
<li>opensource web browsers (such as firefox)</li>
<li>no google apps on android phones</li>
<li>https</li>
</ul>
<h6 id="message-security">Message Security</h6>
<p>Message security is the level of protection regarding the content that you want to send or receive.</p>
<p>Message security is essential if you want to avoid any third party snooping and the confidentiality of your messages.</p>
<p>The tools we can use in this context:</p>
<ul>
<li>OTR</li>
<li>opensource messaging protocols (XMPP, matrix)</li>
<li>Signal</li>
<li>PGP</li>
</ul>
<p>Also, always remember that encrypting the content of the message doesn&#39;t guarantee that your identity and the metadata are hidden.</p>
<h6 id="human-security-the-weakest-link">Human Security, the weakest link</h6>
<p>Everything comes down to the human level at a certain point.</p>
<p>This is way it is important to train ourselves in security.</p>
<p>If we consider <a href="https://en.wikipedia.org/wiki/Kevin_Mitnick">Kevin Mitnick&#39;s history</a>, or the recent <a href="http://thehackernews.com/2015/11/fbi-cia-director-hack.html">FBI deputy director hack</a> we see that social engineering plays a big role when we want to undermine the security of an individual of interest.</p>
<p>But security matters even if we are not target of interest.</p>
<p>For example let&#39;s consider our password. If we use the same password on every site and one cracker manages to gain access to just one of them, our whole activities online can be exposed and our identity stolen. <a href="http://www.nydailynews.com/news/national/mark-zuckerberg-twitter-account-hacked-password-dadada-article-1.2662351">This is relevant</a>. Myspace had its database breached and the password of Zuckerberg (even a simple one) was exposed. Given that he used the same password on twitter and other sites, his multiple accounts were compromised.</p>
<h3 id="what-is-tor-and-how-it-works">What is TOR and how it works</h3>
<p>When you visit a website with your mobile phone or a browser on your computer lots of things go on under the hoods.</p>
<p>Your computer, as a client, makes what is called an <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_establishment">handshake</a> with the server.</p>
<p>After telling the server that the client is interested in its content, a series of packets containing data is exchanged.</p>
<p>That is the content of a connection. Inside this packets there are a multitude of information of two kinds:</p>
<ul>
<li>the web page or the content we are trying to visualize</li>
<li>information on the status of both the server and the client</li>
</ul>
<p>The informations contained in every packet can be analized to understand the &quot;identity&quot; of the client that is requesting the content on the server, first of all the IP that is a sort of web address that every computer on the net has.</p>
<p>Not only, during the transmission of this packets, various entity on the communication channel can analize the content and mine our data.</p>
<p><img src="/wp-content/uploads/2016/tor-https-0.png" alt="Cute infographic"></p>
<p>TOR still uses this kind of routine to gather the content of a web page, but instead of connecting directly to the destination server it goes through a series of other servers called relay: instead of going directly from A to B, it goes from A to C to D to E to F to B.</p>
<p>If the web was a kindergarden Alice instead of telling directly her phrase to Bob, she would tell the word to a friend that in turn would tell the word to a friend and so on, until Bob heards the word, without knowing that Alice said that at the beginning.</p>
<p>At this point you should ask yourself: are the data more protected if it goes through a network of relays? It actually is given that every time you send a packet through the TOR network, it gets encrypted so that no one knows it&#39;s content.</p>
<p>To tell the truth, actually the relay (called exit node) that will send the packet to the destination server, knows the content of the packet but does not know the origin.</p>
<p>Ultimately a website can be entirely hosted on the TOR network, called the onion network, so that the packets never exit from the relays and the relay don&#39;t know the phisycal location of the server, so every entity on the network reach a perfect level of anonimacy.</p>
<h3 id="who-owns-the-relays">Who owns the relays?</h3>
<p>Actually every one can host and own a relay if they are willing to do so.
I personally host one right now and there are many others that share a little fraction of their network connection.</p>
<p><img src="/wp-content/uploads/2016/screenraspy.png" alt="My little raspi is moving some packets right now"></p>
<p>Running a relay node is very easy and everybody should do so. Running an exit node instead is more troublesome and I don&#39;t suggest it if you are not a big entity that can handle some sorts of occasional trouble.</p>
<h3 id="don-39-t-play-the-fool-on-the-tor-network">Don&#39;t play the fool on the TOR network</h3>
<p>Of course TOR doesn&#39;t guarantee you perfect anonimacy. At the end it all comes to the human layer.</p>
<p>It&#39;s no use to surf the web through TOR if we then log in to our personal blog or our personal facebook page.</p>
<p>But there are other subtle factors that can be exploited by web companies to gather info and track their users.A</p>
<p>Such factors are:</p>
<ul>
<li>the size of the screen and the colors supported by it</li>
<li>the timezone</li>
<li>canvas and images that the server asks your computer to generate</li>
<li>information about your OS that are sent through packets</li>
<li>the fonts available on your system</li>
<li>touch support</li>
<li>cookies</li>
<li>ads and cross site requests</li>
</ul>
<p>In particular, most of these are exploitable using a web programming language, javascript, that lots of web pages uses to render content. TOR users should avoid the use of javascript.</p>
<h3 id="public-private-key-encryption">Public Private Key Encryption</h3>
<p>While TOR is recent technology, public key encryption is a concept way older.</p>
<p>What happens when we use public / private key encryption tools is conceptually similar to what happens with our physical correspondence.</p>
<p>A public key is similar to our mailbox.</p>
<p>Everyone that knows the location of a person&#39;s mailbox can write a message and put it inside but only the owner of that mailbox, using is own key can open the mailbox and read the various messages.</p>
<p>When we use PGP or GPG (an implementation of the public key encription concept) we generate a pair of key.</p>
<p>A public one that we should broadcast or at least share with our social circle, and a private key that must remain secret at any cost.</p>
<p>Everyone can encrypt every kind of digital content using our public key (that is just a really long string) and only the owner of the private key can proceed to decryption of the content.</p>
<p>This also means that we know who is gonna read the message if encrypted using this kind of technologies.</p>
<p>One easy tool for GPG encryption is <a href="https://www.gnupg.org/%28en%29/related_software/gpa/index.html">GPA</a></p>
<h3 id="metadata">Metadata</h3>
<p>What would you do if you were asked to put under surveillance one person?</p>
<p>For sure placing a bug with microphone and recording capabilities would be the best option.</p>
<p>But what if, instead of recording every thing the subject does, we just take a note of all his actions, without taking care of the content. For example, if the subject speaks to someone, we record the time, the place, the duration of the conversation and all the info of the person he is talking with.
What if, when the person walks into a mall, we record the time, the location, the shops he entered, the money he spent, the number of things bought, but not the things he bought, in detail.</p>
<p>You can see that you can have a fairly precise idea of the habits of the person under your surveillance.</p>
<p>Actually from metadata is easy to grab all kinds of personal information. Also, if a tiny portions of the information we have on the subject are more detailed (for example social network photos) we have a picture as clear as never.</p>
<p>This is not just one of the biggest concern that should pop into your mind when you are talking about nation wide mass surveillance, it is also the core of the business of corporations like Facebook and Google.</p>
<p>Whatsapp does not read the content of your messages but it stores every single bit of metadata that comes with it.</p>
<p>Metadatas are enought to build a complete profile of the users and they are even more dangerous in the hands of an evil state agency.</p>
<h3 id="nothing-to-hide">Nothing to hide</h3>
<p>Even if we have nothing to hide, we have much to fear.</p>
<p>The &quot;nothing to hide&quot; argument is something that everyone of us in this room has heard, at least one time.</p>
<p>We should fear this sentence because it is the ultimate ammision of a big misunderstanding on the whole debate.</p>
<p>Privacy, first of all, is <strong>control</strong> over our data, not only the right to secrecy.</p>
<h6 id="transparency-should-be-for-everyone">Transparency should be for everyone</h6>
<p>There is a big incoherence when asking to your citizens to handle over their data. </p>
<p>Transparency should be a two way thing, while at the current state big three letter agencies, but high level people as well, cover their tracks and are not transparent on their reports. </p>
<p>This enhance a situation of big inequality between the people and the State.</p>
<p>Even worse, it is not the citizen by himself that can choose if he has something to hide, but the autority.</p>
<p>This may seem a little naive to say, but with Bruce words:</p>
<blockquote>
<p>If you give me six lines written by the hand of the most honest of men, I will find something in them which will hang him.</p>
</blockquote>
<p>This is true even without considering social discrimination and mass media manipolation.</p>
<h6 id="the-fundamental-of-society">The fundamental of society</h6>
<p>Every action can be seen as either legal or illegal. When we take a decision this is one of the first, implicit concern.</p>
<p>This is not true in a surveillance system: when you are doing something your concern is all about the possibility of raising suspicion.</p>
<p>An idea not an action is what is needed in such a dystopic condition to prove a citizen guilty.</p>
<h6 id="sometimes-two-wrongs-make-a-right">Sometimes two wrongs make a right</h6>
<p>In America we are now discussing weed legalization.</p>
<p>Do you think that such debate would have been possible if no one could had the possibility, even if against the law, to try that substance and show other citizen the real implications of their actions?</p>
<p>The same goes for gay marriages, that we are discussing in Italy. Challenging the law, breaking it if needed, is a way to improve the current system.</p>
<p>Inside the panopticon every <strong>potential</strong> criminal would be persecuted and this kind of advancement would not be possible.</p>
<h3 id="to-hide-is-to-care">To hide is to care</h3>
<p>A simple truth is that we don&#39;t close the windows to cover up our crimes. </p>
<p>Our innermost experiences become in our intimacy, which is the most sacred place.</p>
</div>
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/07/06/IM-services/">
Phone messaging apps comparison
</a>
</h1>
<span class="post-date">06 Jul 2016</span>
<p>I made this chart for a presentation at the <a href="https://cyber.law.harvard.edu/">Berkman Klein Center</a></p>
<table><thead>
<tr>
<th></th>
<th style="text-align: center">Whatsapp</th>
<th style="text-align: center">Telegram</th>
<th style="text-align: right">Signal</th>
</tr>
</thead><tbody>
<tr>
<td>Source code</td>
<td style="text-align: center">closed source</td>
<td style="text-align: center">open source</td>
<td style="text-align: right">open source</td>
</tr>
<tr>
<td>Api</td>
<td style="text-align: center">none</td>
<td style="text-align: center">various</td>
<td style="text-align: right">library</td>
</tr>
<tr>
<td>Encription protocol</td>
<td style="text-align: center">state of the art</td>
<td style="text-align: center">self made</td>
<td style="text-align: right">state of the art</td>
</tr>
<tr>
<td>Contact list location</td>
<td style="text-align: center">cloud</td>
<td style="text-align: center">cloud</td>
<td style="text-align: right">cloud, encrypted</td>
</tr>
<tr>
<td>Forward Secrecy</td>
<td style="text-align: center">yes</td>
<td style="text-align: center">yes</td>
<td style="text-align: right">yes</td>
</tr>
<tr>
<td>Database</td>
<td style="text-align: center">phone storage</td>
<td style="text-align: center">cloud</td>
<td style="text-align: right">phone storage</td>
</tr>
<tr>
<td>Backup capability</td>
<td style="text-align: center">Icloud or Gcloud</td>
<td style="text-align: center">builtin</td>
<td style="text-align: right">none</td>
</tr>
<tr>
<td>Revenue</td>
<td style="text-align: center">ads (Facebook)</td>
<td style="text-align: center">donation based</td>
<td style="text-align: right">donation based</td>
</tr>
<tr>
<td>Federation</td>
<td style="text-align: center">no</td>
<td style="text-align: center">no</td>
<td style="text-align: right">no</td>
</tr>
<tr>
<td>Alternative download location</td>
<td style="text-align: center">website</td>
<td style="text-align: center">F-Droid</td>
<td style="text-align: right">none</td>
</tr>
<tr>
<td>Uses third party services</td>
<td style="text-align: center">no</td>
<td style="text-align: center">no</td>
<td style="text-align: right">Google Cloud Messaging</td>
</tr>
<tr>
<td>Servers location</td>
<td style="text-align: center">US</td>
<td style="text-align: center">Russia</td>
<td style="text-align: right">US</td>
</tr>
<tr>
<td>Tied to mobile number</td>
<td style="text-align: center">yes</td>
<td style="text-align: center">yes, but nickname available</td>
<td style="text-align: right">yes</td>
</tr>
<tr>
<td>Desktop client</td>
<td style="text-align: center">no</td>
<td style="text-align: center">yes</td>
<td style="text-align: right">no</td>
</tr>
</tbody></table>
</div>
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/07/05/arduino_keyboard/">
Arduino Uno as HID keyboard
</a>
</h1>
<span class="post-date">05 Jul 2016</span>
<p>Turin is the hometown of Arduino. I have been at the <a href="http://fablabtorino.org/">fablab</a> multiple times but I had to come all the way to America to get my hands on a simple Arduino Uno.</p>
<p>For 60$ I bought a cheap (but still good!) mechanical keyboard by Qisan, a clone of the Arduino Uno and a USB host shield.</p>
<p>Given that is 3 years since I have been using a dvorak layout and it&#39;s a pain to change layout on every machine that you have to use.
You can imagine that given this three pieces of hardware together I put together an hardware key mapper for the keyboard.</p>
<p>I have never had experience with Arduino before but it was not that difficult to make it do simple things like blinking the led or send signal through to a serial monitor.</p>
<p>It took me half an hour to wear down all my excitement: the USB Host Shield library broke all the compatibility with the similar project I found wandering online.</p>
<p>In particular <a href="http://hunt.net.nz/users/darran/">this blog</a> has the most precious information and the guy wrote a HID driver that allows the Uno to be seen as a HID device.</p>
<p>It was a noob error but I didn&#39;t checked the various arduino alternatives and I discovered late that just a few have the HID capabilities that would make this work easier. I should have bought and Arduino Due or Leonardo maybe.</p>
<p>Also, the various guides about flashing with a dfu tool are specific to older models of the Uno and it took me some time to figure the name of the new components so that I could flash a new firmware.</p>
<h2 id="a-small-journey-in-the-arduino-world">A small journey in the Arduino world</h2>
<p>It feels pretentious to write a little guide for this kind of work, given also the fact that I have roughly 10 hours of experience with the Arduino. But the other resources are really outdated so I hope this piece can be useful to someone out there.</p>
<p>All the files I have used today are on <a href="http://francescomecca.eu:3000/pesceWanda/arduino_HID_keyboard">my repos</a> and I included also an outdated version of the USB Host Shield library that I used.</p>
<p>The original code from this <a href="http://hunt.net.nz/users/darran/weblog/c6f35/Arduino_USB_Keyboard_Passthrough.html">blog post</a> works like a charm but just as a simple passthrough.</p>
<p>It was not difficult at all to examine the code:
during each loop of the iteration a char array gets read from the shield and if it is contains information Arduino with the Serial.Write method send the data to the host.</p>
<p>The buffer array is a simple array of length 8 and the first two positions are reserved. In particular the first one represent the various modifier keys.</p>
<p>The dvorak layout has the same pairs as the US layout but eventually I got used to having the &#39;@&#39; where at the same place of &#39;Q&#39; (qwerty) and &#39;&quot;&#39; over the &#39;2&#39;.
Also, I am an avid vim user (I should thank Simone Basso for that) and I swapped some keys on the new 65 keys keyboard.
The modifier bit at the beginning of the array came in handy for my code.</p>
<p>An hardware key remapper is a simple but long switch C statement but I decided to consider also the modifier bit: in this way certain keys like the Window (UGH!) key is mapped to a different layer of keys.
I got all the codes for the HID events <a href="http://www.freebsddiary.org/APC/usb_hid_usages.php">here</a>.</p>
<p>The process of flashing the code on the Uno goes like this:</p>
<ul>
<li>write the looping code;</li>
<li>push it to the Arduino using the IDE;</li>
<li>shortcircuit the board so that it goes in DFU mode;</li>
<li>flash the .hex HID firmware;</li>
<li>try your code;</li>
<li>repeat until it&#39;s right.</li>
</ul>
<p><img src="/wp-content/uploads/2016/IMG_20160706_011304.jpg" alt="Everything fits in one picture"></p>
<h2 id="flashing-the-firmware">Flashing the firmware</h2>
<p>The firmware is in my repo but I got it from (here)[<a href="http://hunt.net.nz/users/darran/weblog/a6d52/Arduino_UNO_Keyboard_HID_version_02.html">http://hunt.net.nz/users/darran/weblog/a6d52/Arduino_UNO_Keyboard_HID_version_02.html</a>].
The tool I used to flash it is dfu-programmer (version 0.62).
Every time you want to flash a new firmware the Arduino must be put in DFU mode (you can see the difference with lsusb).
To do that simply create a shortcircuit using a small metal wire on the two pins near the reset button and a led will blink.
This <a href="https://www.youtube.com/watch?v=E8XyRwXQr8Q">video</a> shows the method briefly (no real need for a jumper).
The commands are the following and there is no risk to brick the Uno:</p>
<div class="highlight"><pre><code class="language-" data-lang="">dfu-programmer atmega16u2 erase
dfu-programmer atmega16u2 flash Arduino-keyboard-0.2.hex
dfu-programmer atmega16u2 reset
</code></pre></div>
<p>After each flashing the device needs to be disconnected once. Of course you can flash the original firmware back. It is included in my repo or on the official ones.</p>
<p><img src="/wp-content/uploads/2016/IMG_20160706_011143.jpg" alt="Arduino and the shield"></p>
<p>That&#39;s it, as you can see is not difficult at all. The worst part is gathering the various info that are left dormant in blogs or forums.</p>
</div>
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/05/16/lifehacks2/">
Lifehacks (2)
</a>
</h1>
<span class="post-date">16 May 2016</span>
<ul>
<li><p>If you&#39;re at a party and you don&#39;t know anyone, make it a point to meet the host and introduce yourself. The host can introduce you to other guys/girls and it scores you points.</p></li>
<li><p>Never buy high-end cables, and never buy cables at retail. Cables have higher profit margins than almost everything except extended warranties. Despite what the marketing and sales people will tell you, there is no difference. Need a computer cable? Order it from a wholesaler online. That USB cable that your printer requires will cost you $25 at Staples and $1.50 at Newegg.</p></li>
<li><p>Never quote an entire post unless it&#39;s shorter than the one you write in response.</p></li>
<li><p>Don&#39;t eat food after 6pm.</p></li>
<li><p>In college, always check the library to see if the teacher is using a test bank.</p></li>
</ul>
</div>
<div class="post">
<h1 class="post-title">
<a href="/pescewanda/2016/05/15/genetic-alg/">
Interpolation using a genetic algorithm
</a>
</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&#39;s theory of evolution, the member of the population are going to &quot;reproduce&quot;: 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 &quot;survival by the fittest&quot; 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 &quot;scrambled&quot; 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">&lt;</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 &lt; 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>
<div class="pagination">
<a class="pagination-item older" href="/page2">Older</a>
<span class="pagination-item newer">Newer</span>
</div>
</div>
<!--<link rel="alternate" type="application/rss+xml" title="Francesco Mecca RSS" href="/feed.xml">-->
</body>
</html>