<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Random Hacks: Robot localization using a particle system monad</title>
    <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Robot localization using a particle system monad" by Christopher Tay</title>
      <description>&lt;p&gt;Chris, you&amp;#8217;re right. Its sequential monte carlo. Thanks for pointing it out.&lt;/p&gt;</description>
      <pubDate>Thu, 06 Sep 2007 10:14:14 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:502fa05d-44be-44b5-98a7-90a2da526851</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-493</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by chris</title>
      <description>&lt;p&gt;Christopher, you are wrong. A particle filter is not a markov chain monte carlo (MCMC) algorithm.
&lt;span class="caps"&gt;A MCMC&lt;/span&gt; algorithm is an iterative algorithm   and is used for batch tasks. Particle filters only rely on importance sampling and resampling mechanisms and allow you to address sequential problems.&lt;/p&gt;</description>
      <pubDate>Thu, 02 Aug 2007 06:41:03 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:189fe971-7a5a-4e52-ad40-b7d7c7edf956</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-486</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Eric</title>
      <description>&lt;p&gt;Adding &lt;code&gt;mzero&lt;/code&gt; to a probability monad has tricky semantics.&lt;/p&gt;


	&lt;p&gt;In the particle system above, I keep the &amp;#8220;impossible&amp;#8221; particles and give them a weight of 0.  In a real implementation, you would generally want to replace these particles by &amp;#8220;spliting&amp;#8221; high-probability particles in half, or by resampling the particles periodically, resetting all the weights to 1.&lt;/p&gt;


	&lt;p&gt;In an &lt;a href="http://www.randomhacks.net/articles/2007/02/22/bayes-rule-and-drug-tests" rel="nofollow"&gt;earlier hack&lt;/a&gt; I used the &amp;#8220;missing probability&amp;#8221; to represent the normalization factor needed by Bayes&amp;#8217; rule.  It&amp;#8217;s important to understand exactly how &lt;code&gt;guard&lt;/code&gt; fits into your larger semantics; there are quite a few traps for the unwary.&lt;/p&gt;


	&lt;p&gt;At a minimum, if you&amp;#8217;re going to build a sampling-based probability monad with an &lt;code&gt;mzero&lt;/code&gt;, you&amp;#8217;re going to have to make sure that &lt;code&gt;sample 200 (guard False)&lt;/code&gt; immediately returns zero samples.&lt;/p&gt;</description>
      <pubDate>Fri, 25 May 2007 16:17:08 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:8d427617-1696-427e-9937-d327b1b6e9b8</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-461</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Samer Abdallah</title>
      <description>&lt;p&gt;I&amp;#8217;ve been playing with sampling monads and I&amp;#8217;ve run up against a problem which I believe will afflict this code as well.  The problem comes up when we add a conditioning operator, or equivalently, if we allow an mzero into the monad. A trivial example might be something like&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;y = do u &amp;lt;- uniform01; guard (u&amp;gt;=0.5); return u&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;or, in my code&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;y = cond (&amp;gt;=0.5) uniform01&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;this is a (not  particularly good) way of sampling
uniformly on [0.5,1), but you get the idea.
If we now elaborate this to&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;z = uniform01 &amp;gt;&amp;gt;= \u-&amp;gt;cond (&amp;gt;=2u) uniform01&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;or&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;z = do
   u &amp;lt;- uniform01;
   v &amp;lt;- uniform01;
   guard (v&amp;gt;=2u);
   return v&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;we get something where, if the first sampling returns u&amp;gt;=0.5, the guard on the second sample
cannot succeed. A naive sampling method which tries to repeat the second sampling operation until the guard succeeds will get stuck in an inifite loop, as there is no way for it to realise that its task is hopeless and it needs to backtrack and retry the first sampling operation.&lt;/p&gt;


	&lt;p&gt;In your code, the sample problem would occur
when calling |sample1 x| if x cannot return any
samples (due to guards being present in the definition of x).&lt;/p&gt;


	&lt;p&gt;As I see it, I can think of two ways around this.&lt;/p&gt;


	&lt;p&gt;One is to supplement the representation of the random variable to include sufficient static information about the support of the distribution so that cond can immediately reduce to mzero if the conditioning event (ie a set) does not intersect with the support.&lt;/p&gt;


	&lt;p&gt;The other is to have the representation be a stream of |Maybe a| and implement a kind of &amp;#8216;fair&amp;#8217; binding
in x &amp;gt;&amp;gt;= f (or equivalently a &amp;#8216;fair&amp;#8217; join) which &lt;strong&gt;interleaves&lt;/strong&gt; the stream of samples from each application of f to samples from x. That way,
if x returns [x1,x2,...], and guards make |f x1| a stream of |Nothing|, then at some point, we will get
to try |f x2|, |f x3| and so on. This is the same as the &amp;#8216;fair conjuction&amp;#8217; proposed by Shan et al in their
LogicT monad (see http://okmij.org/ftp/Computation/monads.html). I&amp;#8217;m just trying it now but looks like it&amp;#8217;s going to be very inefficient because it can waste a lot of samples and suffers
from combinatorial explosion when you start tupling up random variables.&lt;/p&gt;


	&lt;p&gt;Have you come up against this problem and do you have any thoughts on the matter?&lt;/p&gt;</description>
      <pubDate>Mon, 21 May 2007 19:44:56 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:65516e57-33e0-4d36-8e99-c2415a3bfaec</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-456</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Eric</title>
      <description>&lt;p&gt;Noel &amp;amp; Christopher: Thanks for comments!&lt;/p&gt;


	&lt;p&gt;Kalman filters, &lt;span class="caps"&gt;AFAICT&lt;/span&gt;, are one of the few models of belief functions that don&amp;#8217;t map cleanly to Haskell monads. So far, I haven&amp;#8217;t been able to define a suitable version of &lt;code&gt;fmap&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;One especially cool thing about particle systems is that you can use a &lt;span class="caps"&gt;GPU&lt;/span&gt; to simulate &lt;a href="http://www.2ld.de/gdc2004/" rel="nofollow"&gt;a million particles&lt;/a&gt; in real-time.&lt;/p&gt;


	&lt;p&gt;Given the handy monadic structure of particle systems&amp;mdash;and their ability to represent non-linear functions&amp;#8212;it&amp;#8217;s worth keeping particle systems in mind. With the right hardware, they&amp;#8217;re fast, flexible and easy to program.&lt;/p&gt;</description>
      <pubDate>Mon, 23 Apr 2007 19:07:42 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0e610de6-5c87-4649-a320-bae8d5ec7765</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-407</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Christopher Tay</title>
      <description>&lt;p&gt;I would like to first say that although I do not program haskell, I really enjoy your blog :) Some points I would like to add&amp;#8230;&lt;/p&gt;


	&lt;p&gt;A particle filter is essentially monte carlo (sampling). It is also known as markov chain monte carlo (MCMC). The particles are samples which represents a certain probability distribution which in this case, represents the state space of the robot. Essentially, the set of particles gives us an approximation of the probability distribution on the state of the robot. Its advantage over the kalman filter are essentially its non linearity (of robot state transitions in this case) and its ability to represent multi-modal distributions (which is not the case for kalman where it is assumed to be gaussian).&lt;/p&gt;


	&lt;p&gt;In contrast, the viterbi algorithm frequently associated with hidden markov models estimates not the probability distribution, but rather the most likely solution.&lt;/p&gt;


	&lt;p&gt;The application of the two methods are not limited by whether the state space is continuous or discret (although it does often have practical implications). Both the viterbi algorithm and particle filters can be applied to discrete and continuous spaces.&lt;/p&gt;</description>
      <pubDate>Mon, 23 Apr 2007 09:35:21 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:1055ef24-5eb6-4b10-82db-09ac092b37be</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-406</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Noel Welsh</title>
      <description>&lt;p&gt;Particle filters and the Viterbi algorithm are used in similar situations.  The Viterbi algorithm is typically used to calculate the most likely transitions through a model given observations when the state space is discrete.  Particle filters are typically used to calculate a set of high probability transitions when the state space is continuous, and not of an easily analysable form (so a Kalman filter cannot be used).&lt;/p&gt;</description>
      <pubDate>Mon, 23 Apr 2007 08:48:02 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:b2a7851c-e037-4332-86a5-5466c48990e4</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-405</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Eric</title>
      <description>&lt;p&gt;I&amp;#8217;m looking forward to your blog post! I don&amp;#8217;t know much about the Viterbi algorithm.&lt;/p&gt;


	&lt;p&gt;Particle systems are similar to random sampling, except that the samples are taken &amp;#8220;in parallel&amp;#8221; instead of one at a time. This offers two advantages:&lt;/p&gt;


	&lt;p&gt;1) You can maintain a series of hypotheses about the world, and update them as new evidence arrives. This is especially handy in robotics.&lt;/p&gt;


	&lt;p&gt;2) You can periodically reallocate your particles towards the most promising hypotheses. This is very handy in long-running computations where a large fraction of hypotheses are ruled out at each step.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve seen nice 2D movies of particle systems, which make these ideas very intuitive. I&amp;#8217;ll have to see if I can dig one up.&lt;/p&gt;</description>
      <pubDate>Sat, 21 Apr 2007 18:45:26 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d746c1a0-60e4-4841-8dd9-dfd62682d0cd</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-404</link>
    </item>
    <item>
      <title>"Robot localization using a particle system monad" by Dan P</title>
      <description>&lt;p&gt;I haven&amp;#8217;t fully read this yet but does this approach must have any relationship with the Viterbi algorithm?&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m writing a blog entry on Viterbi and this stuff looks remarkably similar.&lt;/p&gt;</description>
      <pubDate>Fri, 20 Apr 2007 14:44:15 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:900e0107-2915-4156-84f9-52f7b2802ef5</guid>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad#comment-403</link>
    </item>
    <item>
      <title>Robot localization using a particle system monad</title>
      <description>&lt;p&gt;&lt;b&gt;Refactoring Probability Distributions:&lt;/b&gt;
&lt;a href="http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions" title="PerhapsT"&gt;part 1&lt;/a&gt;, &lt;a href="http://www.randomhacks.net/articles/2007/02/21/randomly-sampled-distributions" title="Random sampling"&gt;part 2&lt;/a&gt;, &lt;a href="http://www.randomhacks.net/articles/2007/02/22/bayes-rule-and-drug-tests" title="Bayes' rule"&gt;part 3&lt;/a&gt;, &lt;a href="http://www.randomhacks.net/articles/2007/03/03/smart-classification-with-haskell" title="Bayesian classification"&gt;part 4&lt;/a&gt;, &lt;b&gt;part 5&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Welcome to the 5th (and final) installment of &lt;i&gt;Refactoring Probability
Distributions!&lt;/i&gt;  Today, let&amp;#8217;s begin with an example from 
&lt;a href="http://seattle.intel-research.net/people/jhightower/pubs/fox2003bayesian/fox2003bayesian.pdf" title="Fox and colleagues, 2003"&gt;Bayesian Filters for Location Estimation&lt;/a&gt; (PDF), an excellent paper by Fox and colleagues.&lt;/p&gt;

&lt;p&gt;In their example, we have a robot in a hallway with 3 doors.  Unfortunately, we don&amp;#8217;t know &lt;i&gt;where&lt;/i&gt; in the hallway the robot is located:&lt;/p&gt;

&lt;p style="text-align: center"&gt;&lt;img
src="http://www.randomhacks.net/files/robot-door-1.png" title="Robot at
first door" /&gt;&lt;/p&gt;

&lt;p&gt;The vertical black lines are &amp;#8220;particles.&amp;#8221;  Each particle represents a
possible location of our robot, chosen at random along the hallway.  At
first, our particles are spread along the entire hallway (the top
row of black lines). Each particle begins life with a weight of 100%, represented by the height of the black line.&lt;/p&gt;

&lt;p&gt;Now imagine that our robot has a &amp;#8220;door sensor,&amp;#8221; which currently tells us that
we&amp;#8217;re in front of a door. This allows us to rule out any particle which is located &lt;i&gt;between&lt;/i&gt; doors. &lt;/p&gt;

&lt;p&gt;So we multiply the weight of each particle by 100% (if it&amp;#8217;s in front of a door) or 0% (if it&amp;#8217;s between doors), which gives us the lower row of particles. If our sensor was less accurate, we might use 90% and 10%, respectively.&lt;/p&gt;

&lt;p&gt;What would this example look like in Haskell?  We &lt;i&gt;could&lt;/i&gt; build a
giant list of particles (with weights), but that would require us to do a
lot of bookkeeping by hand.  Instead, we use a &lt;a href="http://www.randomhacks.net/articles/2007/03/12/monads-in-15-minutes" title="Monads in 15 minutes"&gt;monad&lt;/a&gt; to hide all the
details, allowing us to work with a single particle at a time.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;localizeRobot&lt;/span&gt; &lt;span class='keyglyph'&gt;::&lt;/span&gt; &lt;span class='conid'&gt;WPS&lt;/span&gt; &lt;span class='conid'&gt;Int&lt;/span&gt;
&lt;span class='varid'&gt;localizeRobot&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='keyword'&gt;do&lt;/span&gt;
  &lt;span class='comment'&gt;-- Pick a random starting location.&lt;/span&gt;
  &lt;span class='comment'&gt;-- This will be our first particle.&lt;/span&gt;
  &lt;span class='varid'&gt;pos1&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;uniform&lt;/span&gt; &lt;span class='keyglyph'&gt;[&lt;/span&gt;&lt;span class='num'&gt;0&lt;/span&gt;&lt;span class='keyglyph'&gt;..&lt;/span&gt;&lt;span class='num'&gt;299&lt;/span&gt;&lt;span class='keyglyph'&gt;]&lt;/span&gt;
  &lt;span class='comment'&gt;-- We know we're at a door.  Particles&lt;/span&gt;
  &lt;span class='comment'&gt;-- in front of a door get a weight of&lt;/span&gt;
  &lt;span class='comment'&gt;-- 100%, others get 0%.&lt;/span&gt;
  &lt;span class='keyword'&gt;if&lt;/span&gt; &lt;span class='varid'&gt;doorAtPosition&lt;/span&gt; &lt;span class='varid'&gt;pos1&lt;/span&gt;
    &lt;span class='keyword'&gt;then&lt;/span&gt; &lt;span class='varid'&gt;weight&lt;/span&gt; &lt;span class='num'&gt;1&lt;/span&gt;
    &lt;span class='keyword'&gt;else&lt;/span&gt; &lt;span class='varid'&gt;weight&lt;/span&gt; &lt;span class='num'&gt;0&lt;/span&gt;
  &lt;span class='comment'&gt;-- ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What happens if our robot drives forward?&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad"&gt;Read More&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 19 Apr 2007 20:43:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d14628b2-732e-4b87-934f-c6373df35a5c</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/04/19/robot-localization-particle-system-monad</link>
      <category>Haskell</category>
      <category>Math</category>
      <category>Monads</category>
      <category>Probability</category>
      <category>Robots</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/399</trackback:ping>
    </item>
  </channel>
</rss>
