181 lines
14 KiB
HTML
181 lines
14 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width">
|
||
<title>The Buridan&#8217;s donkey paradox | Caught in the Net</title>
|
||
<link rel="stylesheet" href="../../../../../assets/fonts/opensans.css">
|
||
<link href="../../../../../assets/css/normalize.css" rel="stylesheet" type="text/css">
|
||
<link href="../../../../../assets/css/cayman.css" rel="stylesheet" type="text/css">
|
||
<meta name="theme-color" content="#5670d4">
|
||
<meta name="generator" content="Nikola (getnikola.com)">
|
||
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="en" href="../../../../../rss.xml">
|
||
<link rel="canonical" href="francescomecca.eu/blog/2015/9/20/the-buridans-donkey-paradox/">
|
||
<meta name="author" content="Francesco Mecca">
|
||
<link rel="prev" href="../../11/about-perl-packages-in-gentoo/" title="About Perl packages in Gentoo" type="text/html">
|
||
<link rel="next" href="../../../../2016/4/2/buridan_donkey/" title="The Buridan's donkey in python" type="text/html">
|
||
<meta property="og:site_name" content="Caught in the Net">
|
||
<meta property="og:title" content="The Buridan&#8217;s donkey paradox">
|
||
<meta property="og:url" content="francescomecca.eu/blog/2015/9/20/the-buridans-donkey-paradox/">
|
||
<meta property="og:description" content="The Buridan’s donkey is an illustration of a paradox regarding the philosophy of moral determinism and free will.
|
||
|
||
|
||
|
||
The paradox shows an hypothetical situation in which a donkey searching for food ">
|
||
<meta property="og:type" content="article">
|
||
<meta property="article:published_time" content="2015-09-20T10:34:36Z">
|
||
<meta property="article:tag" content="buridan">
|
||
<meta property="article:tag" content="buridan c++">
|
||
<meta property="article:tag" content="buridan donkey">
|
||
<meta property="article:tag" content="buridan'ass">
|
||
<meta property="article:tag" content="c++11">
|
||
<meta property="article:tag" content="PesceWanda">
|
||
<meta property="article:tag" content="poll()">
|
||
<meta property="article:tag" content="poll.h">
|
||
</head>
|
||
<body>
|
||
<div id="container">
|
||
|
||
<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="../../../../../pages/about/">About me</a>
|
||
<a class="btn" href="../../../../../pages/contattami/">Contact me</a>
|
||
<a class="btn" href="../../../../../archiveall.html">Archive</a>
|
||
<a class="btn" href="../../../../../rss.xml">RSS</a>
|
||
<a class="btn" href="http://francescomecca.eu/git/pesceWanda">Personal Git</a>
|
||
<a class="btn" href="https://github.com/FraMecca">Github</a>
|
||
<a class="btn" href="../../../../../wp-content/curriculum/CV_Mecca_Francesco.pdf">Curriculum</a>
|
||
</section><section class="main-content"><div class="post">
|
||
|
||
<header><h1 class="post-title">
|
||
|
||
<h1 class="p-name post-title" itemprop="headline name">The Buridan&#8217;s donkey paradox</h1>
|
||
|
||
</h1>
|
||
</header><p class="dateline post-date">20 September 2015</p>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="e-content entry-content" itemprop="articleBody text">
|
||
<div>
|
||
<p style="text-align: left;">
|
||
The Buridan’s donkey is an illustration of a paradox regarding the philosophy of moral determinism and free will.
|
||
</p>
|
||
|
||
<p style="text-align: left;">
|
||
The paradox shows an hypothetical situation in which a donkey searching for food is caught in the middle of two equally appealing stacks of hay located at the same distance from the donkey. Because the donkey has no real reason to choose one over the other he dies of starvation.
|
||
</p>
|
||
|
||
<p><a href="https://en.wikipedia.org/wiki/Buridan's_ass"><img class="aligncenter size-full wp-image-209" src="http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress.jpg" alt="Deliberations_of_Congress" width="431" height="425" srcset="http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress-300x296.jpg 300w, http://francescomecca.eu/wp-content/uploads/2015/09/Deliberations_of_Congress.jpg 431w" sizes="(max-width: 431px) 100vw, 431px"></a>I have decided to write a cli program that chooses for me when I can’t make up my mind.</p>
|
||
<p>The program is written in C++ and when invoked along with two or more arguments it puts them in a vector and then changes the order randomly.</p>
|
||
<p>.. code:: c</p>
|
||
<pre class="code literal-block"><span></span><span class="cp">#include</span> <span class="cpf"><iostream></span><span class="cp"></span>
|
||
<span class="cp">#include</span> <span class="cpf"><vector></span><span class="cp"></span>
|
||
<span class="cp">#include</span> <span class="cpf"><algorithm></span><span class="cp"></span>
|
||
<span class="cp">#include</span> <span class="cpf"><random></span><span class="cp"></span>
|
||
<span class="cp">#include</span> <span class="cpf"><string></span><span class="cp"></span>
|
||
<span class="cp">#include</span> <span class="cpf"><sys/poll.h></span><span class="cp"></span>
|
||
<span class="n">using</span> <span class="n">namespace</span> <span class="n">std</span><span class="p">;</span>
|
||
|
||
<span class="kr">struct</span> <span class="n">pollfd</span> <span class="n">stdin_poll</span> <span class="o">=</span> <span class="p">{</span>
|
||
<span class="p">.</span><span class="n">fd</span> <span class="o">=</span> <span class="n">fileno</span> <span class="p">(</span><span class="n">stdin</span><span class="p">),</span> <span class="p">.</span><span class="n">events</span> <span class="o">=</span> <span class="n">POLLIN</span>
|
||
<span class="p">};</span>
|
||
|
||
<span class="kr">void</span> <span class="nf">read_from_piped_input</span> <span class="p">(</span><span class="n">vector</span><span class="o"><</span><span class="kr">string</span><span class="o">>&</span> <span class="n">lst</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="kr">string</span> <span class="n">x</span><span class="p">;</span>
|
||
<span class="k">while</span> <span class="p">(</span><span class="n">getline</span> <span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span> <span class="p">{</span>
|
||
<span class="n">lst</span><span class="p">.</span><span class="n">push_back</span> <span class="p">(</span><span class="n">x</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
|
||
<span class="kr">void</span> <span class="nf">read_from_arguments</span> <span class="p">(</span><span class="kr">const</span> <span class="kr">int</span><span class="o">&</span> <span class="n">argc</span><span class="p">,</span> <span class="kr">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[],</span> <span class="n">vector</span><span class="o"><</span><span class="kr">string</span><span class="o">>&</span> <span class="n">lst</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="n">cout</span> <span class="o"><<</span> <span class="s">"Usage: asino [string] [string] ..."</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
|
||
<span class="n">exit</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="k">for</span> <span class="p">(</span><span class="n">vector</span><span class="o"><</span><span class="kr">string</span><span class="o">>::</span><span class="n">size_type</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">argc</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="n">lst</span><span class="p">.</span><span class="n">push_back</span> <span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
|
||
<span class="kr">int</span> <span class="nf">main</span> <span class="p">(</span><span class="kr">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kr">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
|
||
<span class="p">{</span>
|
||
<span class="n">vector</span><span class="o"><</span><span class="kr">string</span><span class="o">></span> <span class="n">lst</span><span class="p">;</span>
|
||
<span class="kr">int</span> <span class="n">poll_ret</span> <span class="o">=</span> <span class="n">poll</span> <span class="p">(</span><span class="o">&</span><span class="n">stdin_poll</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">poll_ret</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="n">read_from_piped_input</span> <span class="p">(</span><span class="n">lst</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="k">else</span> <span class="p">{</span>
|
||
<span class="n">read_from_arguments</span> <span class="p">(</span><span class="n">argc</span><span class="p">,</span> <span class="n">argv</span><span class="p">,</span> <span class="n">lst</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
|
||
<span class="n">random_device</span> <span class="n">rd</span><span class="p">;</span>
|
||
<span class="n">mt19937</span> <span class="n">m</span><span class="p">(</span><span class="n">rd</span><span class="p">());</span>
|
||
<span class="n">shuffle</span> <span class="p">(</span><span class="n">lst</span><span class="p">.</span><span class="nf">begin</span> <span class="p">(),</span> <span class="n">lst</span><span class="p">.</span><span class="nf">end</span> <span class="p">(),</span> <span class="n">m</span><span class="p">);</span>
|
||
|
||
<span class="kr">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
|
||
<span class="k">for</span> <span class="p">(</span><span class="n">vector</span><span class="o"><</span><span class="kr">string</span><span class="o">>::</span><span class="n">iterator</span> <span class="n">it</span> <span class="o">=</span> <span class="n">lst</span><span class="p">.</span><span class="nf">begin</span> <span class="p">();</span> <span class="n">it</span> <span class="o">!=</span> <span class="n">lst</span><span class="p">.</span><span class="nf">end</span> <span class="p">();</span> <span class="o">++</span><span class="n">it</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="n">cout</span> <span class="o"><<</span> <span class="n">i</span><span class="o">++</span> <span class="o"><<</span> <span class="s">". "</span> <span class="o"><<</span> <span class="o">*</span><span class="n">it</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre>
|
||
|
||
|
||
<p>I have used the <a href="http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine" target="_blank">Mersenne Twister PRNG</a> just to give it a try.</p>
|
||
<p style="text-align: left;">
|
||
One of the challenges was to read from stdin instead of arguments when the program is piped after another program in the shell:
|
||
</p>
|
||
|
||
<p>.. code:: sh</p>
|
||
<pre class="code literal-block"><span></span>ls /media/my_movies/ | buridan
|
||
</pre>
|
||
|
||
|
||
<p>So I have used poll() that checks for a specified amount of time if the selected device (/dev/stdin in my case) can perform I/O operations; in my code:</p>
|
||
<p>.. code:: bash</p>
|
||
<pre class="code literal-block"><span></span>poll (&stdin_poll, 1, 0)
|
||
</pre>
|
||
|
||
|
||
<p>I selected the POLLIN as event so poll() only checks if there is data to read, 1 as the number of items in the fds array, 0 milliseconds of timeout because when the program is invoked /dev/stdin may already contain input.</p>
|
||
<p>The program should be compiled this way:</p>
|
||
<p>.. code:: bash</p>
|
||
<pre class="code literal-block"><span></span>g++ -std=c++11 ./program.cpp -o output
|
||
</pre>
|
||
|
||
|
||
<p>You are free to reuse this little piece of code as you wish.</p>
|
||
<p>EDIT: 02-04-2016
|
||
The original idea for the Buridan's donkey came from my mentor <a href="https://twitter.com/bassosimone">Simone Basso</a> who wrote the original one in haskell.</p>
|
||
<p style="text-align: right;">
|
||
Francesco Mecca
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<aside class="postpromonav"><nav><h4>Categories</h4>
|
||
|
||
<ul itemprop="keywords" class="tags">
|
||
<li><a class="tag p-category" href="../../../../../categories/buridan/" rel="tag">buridan</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/buridan-c%2B%2B/" rel="tag">buridan c++</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/buridan-donkey/" rel="tag">buridan donkey</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/buridanass/" rel="tag">buridan'ass</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/c%2B%2B11/" rel="tag">c++11</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/pescewanda/" rel="tag">PesceWanda</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/poll/" rel="tag">poll()</a></li>
|
||
<li><a class="tag p-category" href="../../../../../categories/pollh/" rel="tag">poll.h</a></li>
|
||
</ul></nav></aside><p class="sourceline"><a href="index.md" class="sourcelink">Source</a></p>
|
||
|
||
|
||
|
||
<footer class="site-footer" id="footer"><span> CC BY-SA 4.0 International.<br></span>
|
||
<span class="site-footer-credits"><a href="https://getnikola.com">Nikola</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
|
||
</footer></section>
|
||
</div>
|
||
</body>
|
||
</html>
|