{"id":100,"date":"2022-01-16T15:21:35","date_gmt":"2022-01-16T15:21:35","guid":{"rendered":"http:\/\/www.asafslaboratory.com\/?p=100"},"modified":"2025-07-07T11:21:32","modified_gmt":"2025-07-07T11:21:32","slug":"flocking-automatons","status":"publish","type":"post","link":"https:\/\/www.asafslaboratory.com\/?p=100","title":{"rendered":"Flocking Automatons"},"content":{"rendered":"\n<p>Greetings and welcome to the first episode of THE ROAD TO AI, where I document my humble efforts in understanding the intricate simplicity of Artificial Intelligence.<\/p>\n\n\n\n<p>I guess my journey begins back in the university in a class called &#8220;Numeric Algebra&#8221;. In that class we had learned to compute, solve and approximate continuous functions using discrete methods.<br>One of the cooler things we&#8217;d learned was the function interpolation using backpropagation. <br>As I saw that one could actually predict data points, I immediately thought to my self &#8220;I wonder if that&#8217;s how AI&#8217;s operate?&#8221;.<\/p>\n\n\n\n<p>Two years later I found myself in need of building a particle physics engine. Conceptually, its simple &#8211; All I had to do was to create a &#8220;world&#8221; which contained particles in the shape of small circles and simulate the interactions between them, and between them and outside forces. Needless to say, doing that elegantly was not that trivial. <br>Searching for answers in the deep realm of YouTube &#8211; I found out about this amazing guy named <strong>Daniel Shiffman<\/strong> who hosted a channel called &#8220;<a href=\"https:\/\/www.youtube.com\/c\/TheCodingTrain\">The Coding Train<\/a>&#8220;. Daniel is a very nice, humble and unbelievably awesome teacher. He teaches several programming languages and environments such as the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Processing <\/strong>&#8211; a Java based framework that simplifies the creating of visual animation and other sorts of art<\/li>\n\n\n\n<li><strong>P5js <\/strong>&#8211; The same as processing but for web based applications. This language is JavaScript based.<\/li>\n\n\n\n<li><strong>TensorFlow.js<\/strong> &#8211; A huge library developed by Google to quickly deploy ML algorithms usually within a cloud computing service.<\/li>\n\n\n\n<li>ML5js &#8211; A friendly machine learning library for web apps. Based on Tensorflow.js<\/li>\n\n\n\n<li><strong>GIT <\/strong>&#8211; The ins and outs of GitHub  <\/li>\n<\/ul>\n\n\n\n<p>He also teaches <strong>Cabana<\/strong>, <strong>Html<\/strong>, and other cool tutorials like working with data, <strong>API&#8217;s <\/strong>and such.<br>I went through the whole series of &#8220;The Nature Of The Code&#8221; in which he follows a whole semester&#8217;s curriculum programming a physics engine from particle interactions, to planetary motion, ending with <strong>Genetic Algorithms, Neural Networks and Neuroevolution.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-gallery alignright has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"300\" data-id=\"114\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Make-your-own-nn-250x300.png\" alt=\"\" class=\"wp-image-114\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Make-your-own-nn-250x300.png 250w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Make-your-own-nn-300x360.png 300w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Make-your-own-nn.png 525w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"183\" data-id=\"115\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Nature-of-the-code-300x183.png\" alt=\"\" class=\"wp-image-115\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Nature-of-the-code-300x183.png 300w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Nature-of-the-code.png 581w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>I was <strong>SO<\/strong> fascinated by this course, I had even bought the book Daniel had written on the subject. It&#8217;s pay as you will, free for download and everything, but I did end up paying to support his work. In the books and the Youtube series he also references a book named &#8211; &#8220;Make Your Own Neural Network&#8221; by Tariq Rashid; Where Tariq teaches the math around the concept of Neural Networks, and then walks the reader through writing their own one in <strong>Python<\/strong>. Of course, I had bought that one too \ud83d\ude00<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>So after giving my sort-of online mentor the proper credit and introduction, lets move on to one of my first projects:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong><span style=\"text-decoration: underline;\">FLOCKING(Boids) SIMULATION<\/span><\/strong><\/p>\n\n\n\n<p>The Boids algorithm, developed by <strong>Craig W. Reynolds<\/strong>, is used to simulate the flocking behavior of birds. The beauty of it is in its simplicity.<br>The algorithm consists of a population of agents where each individual agent able to perceive its surroundings, is <strong>forced<\/strong> to abide theses three rules:<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide\" style=\"grid-template-columns:21% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"216\" height=\"508\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/image-5.png\" alt=\"Image taken from WIKIPEDIA\" class=\"wp-image-117 size-full\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/image-5.png 216w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/image-5-128x300.png 128w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<ol id=\"block-044c33f4-14b3-4e32-b96f-8e4828ebd08e\" class=\"wp-block-list\">\n<li><strong>Separation &#8211;<\/strong><br>Each agent should avoid interfering with its local neighbors.<br><br><br><br><br><\/li>\n\n\n\n<li><strong>Alignment &#8211;<\/strong><br>Each agent should align to average velocity of its local neighbors.<br><br><br><br><br><\/li>\n\n\n\n<li><strong>Cohesion <\/strong>&#8211;<br>Each agent should strive for the average position of its local neighbors.<\/li>\n<\/ol>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In Atom IDE <img loading=\"lazy\" decoding=\"async\" width=\"20\" height=\"20\" class=\"wp-image-137\" style=\"width: 20px;\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/stsmall507x507-pad600x600f8f8f8.u1.jpg\" alt=\"\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/stsmall507x507-pad600x600f8f8f8.u1.jpg 600w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/stsmall507x507-pad600x600f8f8f8.u1-300x300.jpg 300w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/stsmall507x507-pad600x600f8f8f8.u1-150x150.jpg 150w\" sizes=\"auto, (max-width: 20px) 100vw, 20px\" \/>, I created two new classes &#8211;<br><code>class Agent{}<br>class AgentSystem{}<\/code><\/p>\n\n\n\n<p>The AgentSystem class was implemented as a separate entity to handle all of the agents. <\/p>\n\n\n\n<p>The Agent class had the three methods &#8211; <br><code>align(agentsList){}<\/code><br><code>cohese(agentsList){}<\/code><br><code>separate(agentsList){}<\/code><\/p>\n\n\n\n<p>The alignment was the easy part &#8211;<br> <br><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"528\" class=\"wp-image-122\" style=\"width: 700px;\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-191831.png\" alt=\"\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-191831.png 651w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-191831-300x226.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n\n\n\n<p><br>Then the Cohesion, which is almost the same but rather then averaging over the velocities, we need to average over the positions, attain the vector pointing from the agent to the desired position, then continue as in the alignment &#8211;<br> <br><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"714\" class=\"wp-image-123\" style=\"width: 700px;\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-192526.png\" alt=\"\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-192526.png 554w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-192526-294x300.png 294w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-192526-300x306.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Lastly &#8211; We need to implement the separation algorithm. Here we calculate the average vector pointing from the agent to its neighbors, multiplying by -1 and we get the direction in which we need to go. We also apply a scaling factor to increase the &#8220;need&#8221; to run faster from closer agents. <br><br><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"623\" class=\"wp-image-124\" style=\"width: 700px;\" src=\"http:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-193446.png\" alt=\"\" srcset=\"https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-193446.png 612w, https:\/\/www.asafslaboratory.com\/wp-content\/uploads\/2022\/01\/Screenshot-2022-01-16-193446-300x267.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>And <strong>VOILA! <\/strong>we have the flocking simulation right here for your convenience:<br>Feel free to adjust the force factor of each of the three rules, the perception radius of each agent, or the separation distance, and see how the complex population interacts &#8211; <br><\/p>\n\n\n<p><iframe\n  src=\"https:\/\/asafdov.github.io\/Flocking-Simulator\/\"\n  width=\"100%\"\n  height=\"600px\"\n  frameborder=\"0\"\n  allowfullscreen\nscrolling=\"no\"\n  loading=\"lazy\"\n><\/iframe><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>A few optimizations I should do &#8211; <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>I could condense the three methods together, so that I only needed to iterate over all the other agents once for each agent instead of <strong>three times<\/strong>. I intestinally did that for my own educational purposes. <br><\/li>\n\n\n\n<li>I could divide the whole space into squares of sum size so that for each agent I would only have to iterate over the neighbors in its square. <br><\/li>\n\n\n\n<li>I could also narrow each of the agents field of view, so that it could only &#8220;see&#8221; a certain degrees in front of it, instead of the hole radius surrounding it.<br><\/li>\n\n\n\n<li>Something cool to try would be to set a perception radius for each one of the different rules<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p>Anyway, I hope you enjoyed this post just as I enjoyed writing this program. <\/p>\n\n\n\n<p>Stay tuned for the next episode of THE ROAD TO AI.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Until then, Refactor life as you please <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Greetings and welcome to the first episode of THE ROAD TO AI, where I document my humble efforts in understanding the intricate simplicity of Artificial Intelligence. I guess my journey begins back in the university in a class called &#8220;Numeric Algebra&#8221;. In that class we had learned to compute, solve and approximate continuous functions using&#8230;<\/p>\n","protected":false},"author":1,"featured_media":117,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-road-to-ai"],"_links":{"self":[{"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100"}],"version-history":[{"count":9,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions\/488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=\/wp\/v2\/media\/117"}],"wp:attachment":[{"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asafslaboratory.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}