<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.7.3.1">Jekyll</generator><link href="https://replomancer.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://replomancer.github.io/" rel="alternate" type="text/html" /><updated>2018-08-28T11:50:40+00:00</updated><id>https://replomancer.github.io/</id><title type="html">Lukasz Kozuchowski</title><subtitle>Programming blog</subtitle><entry><title type="html">Chemotaxis</title><link href="https://replomancer.github.io/Chemotaxis/" rel="alternate" type="text/html" title="Chemotaxis" /><published>2018-08-23T00:00:00+00:00</published><updated>2018-08-23T00:00:00+00:00</updated><id>https://replomancer.github.io/Chemotaxis</id><content type="html" xml:base="https://replomancer.github.io/Chemotaxis/">&lt;p&gt;Here’s a little bacteria simulation I’ve built in ClojureScript.
I took a lazy approach and instead of starting from scratch I modified
&lt;a href=&quot;https://github.com/juliangamble/clojure-conj-2014-paradigms-of-core-async/tree/950964320bbff17cdd3da7bbcae00ac85dbcd388/9.Ants-CLJS-Array-Optimised/ants-cljs&quot; target=&quot;_blank&quot;&gt;Julian Gamble’s ClojureScript port&lt;/a&gt; of Rich Hickey’s ants colony demo.&lt;/p&gt;

&lt;p&gt;One common behaviour of bacteria is movement in response to chemical stimuli
(&lt;a href=&quot;https://en.wikipedia.org/wiki/Chemotaxis&quot; target=&quot;_blank&quot;&gt;chemotaxis&lt;/a&gt;).
Wikipedia lists &lt;a href=&quot;https://en.wikipedia.org/wiki/Taxis&quot; target=&quot;_blank&quot;&gt;many types of taxis&lt;/a&gt;
or movement towards and away from stimuli such as phototaxis (stimulation by
light) or thermotaxis (stimulation by heat), but I decided to simulate
chemotaxis only.&lt;/p&gt;

&lt;p&gt;The organisms in question obviously cannot acquire and process a lot
of information about the environment. Some bacteria (e.g. E. coli)
observe the concentration of chemicals as they move and continue moving
forward if the concentration of attracting chemicals increases (or repelling
ones decreases). Otherwise they change direction randomly by flipping over.&lt;/p&gt;

&lt;p&gt;The simulation is probably not very realistic because of scale,
speed etc. and it lacks negative stimuli (poison), but it nicely
demonstrates the effectiveness of the very simple movement strategy.&lt;/p&gt;

&lt;p&gt;Click the image below to see a working demo:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://cdn.rawgit.com/replomancer/chemotaxis-cljs/master/resources/public/index.html&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;/images/posts/chemotaxis.png&quot; alt=&quot;chemotaxis&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;starting-point-ants-colony-in-clojurescript&quot;&gt;Starting point: ants colony in ClojureScript&lt;/h2&gt;

&lt;p&gt;Rich Hickey’s ants colony simulation is a demo of Clojure’s concurrency
features from more than 10 years ago. I decided to use a ClojureScript port
to have something runnable in a browser. The ported version I found on github
differs a lot from the original. In particular:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;CLJS has no agents or refs. Concurrency in the CLJS version is introduced
with Communicating Sequential Processes (CSP).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;For performance reasons the CLJS version makes heavy use of JavaScript
mutable arrays in place of Clojure persistent data structures. This makes
the code far from idiomatic. I believe it was &lt;a href=&quot;https://www.bestinclass.dk/da/posts/brians-brain-optimized-clojurescript-html5&quot; target=&quot;_blank&quot;&gt;Lau Jensen’s blog post&lt;/a&gt;
that inspired the array-based solution.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;CSP on the front end is a nice change from typical callbacks-heavy
JavaScript code. E.g. instead of calling setTimeout you can read from timeout
channels. Fun fact: ClojureScript’s core.async library has been &lt;a href=&quot;https://github.com/ubolonton/js-csp&quot; target=&quot;_blank&quot;&gt;ported to JavaScript&lt;/a&gt; so you can use CSP channels with ES6 generators.&lt;/p&gt;

&lt;h2 id=&quot;chemotaxis-cljs&quot;&gt;Chemotaxis-cljs:&lt;/h2&gt;

&lt;p&gt;I’ve cleaned the code, added figwheel and replaced all ant-specific
things with bacteria and chemotaxis. Changes include closing the world
borders for more realism (the world is no longer a torus).&lt;/p&gt;

&lt;p&gt;The main part is in this function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clojure&quot; data-lang=&quot;clojure&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;defn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;behave&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get-bacterium-coord&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;occupied-cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curr-food-amount&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get-cell-food&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;occupied-cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prev-food-amount&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get-bacterium-food&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ahead-coord&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;delta-loc&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get-bacterium-dir&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; When food is less abundant, the bacterium flips and chooses&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; a new dir randomly (sometimes it's the same as the old dir).&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; It also flips when the way is blocked (by world borders or another&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; bacteria). Otherwise it continues moving forward.&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;or&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curr-food-amount&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prev-food-amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
              &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outside-world?&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ahead-coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
              &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get-cell-bacterium&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ahead-coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set-bacterium-dir&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rand-int&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; remember how much food there was&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set-bacterium-food&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curr-food-amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; eat some of the available food&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set-cell-food&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;occupied-cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Math.floor&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curr-food-amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;;; move&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bacterium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It’s amazing how such a simple strategy can work so well (see the demo above).&lt;/p&gt;

&lt;p&gt;Source: &lt;a href=&quot;https://github.com/replomancer/chemotaxis-cljs&quot; target=&quot;_blank&quot;&gt;https://github.com/replomancer/chemotaxis-cljs&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><summary type="html">Here’s a little bacteria simulation I’ve built in ClojureScript. I took a lazy approach and instead of starting from scratch I modified Julian Gamble’s ClojureScript port of Rich Hickey’s ants colony demo.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://replomancer.github.io/posts/chemotaxis.png" /></entry></feed>