Assignment 2 – Against Entropy: A Super Simple Loop Demonstration

[⚠️ Trigger Warning: This article uses intense, hyperbolic language and metaphors for dramatic effect, otherwise my high school English literature class is a waste. Reader discretion advised.]

Let’s get down to business.

Below is the p5 sketch, click 👆 anywhere to “introduce chaos” within the system.

In case p5js Editor website is down, below is the recording of working demo on YouTube.

TL;DR : Conceptualization

Against Entropy is a self-organizing system simulated in p5.js, where you play as the point of view of the universe, giving the system “problems” or “disturbances” – and the system will simply not care about any of your attempts to create problems, and will effortlessly restore the order, and get things working in no time.

1) 🤔 Long-Winded Conceptualization

This artwork is a direct response to Casey Reas’s Talk. I am confused by his appreciation for chaos and randomness. I hate chaos and unpredictability – just imagine growing up in a country with only 4ish hours of daily electricity, without knowing when it will be cut off… your computer just shut down, all your day’s work unsaved…year after year .

“I hate chaos and unpredictability…Chaos is usually the enemy of my heartblood… aesthetics and practicality”

~ Pi

My brain is simple. Things in life need to 1)  work practically – even in impossible situations and 2) be extremely beautiful. Anything which leads to these, I utilize any means to achieve them. Anything which prevents these, I avoid. Chaos is usually the enemy of my heartblood… aesthetics and practicality.

Chaos gives rise to unreliable systems. Unreliable systems are the worst, and they kill lives.  Systems need to work. When they fail, they need to restore themselves to working state.

Systems need to work. When they fail, they need to restore themselves to working state automatically.

~ Pi

Unless such unreliable systems result in extreme aesthetics (as exemplified by Casey Reas), or exhibits tangible practicality (such as in pseudorandom number generators utilized for encryption), or achieves both, it must be annihilated, eradicated, and exterminated through merciless massacre, carried out in the coldest blood and with “negative” empathy and iron fist, then shot twice and bayonetted to guarantee its complete cessation of existence.

This is how much I disgust unreliable systems.

And the professor taught loops and randomness today. And I am feeling swarm robotics these days. So, I needed a system which:

  1. Restores back to working state
  2. Uses randomness and
  3. Use Loops
  4. And of course, the cool swarms

Hence the piece “Against Entropy” is born.

Against Entropy – Pi (2024)
Against Entropy is a self-organizing system simulated in p5js, where you play as the point of view of the universe, giving the system “problems” or “disturbances” – and the system will simply not care about any of your attempts, and will effortlessly restore the order.

Imagine a universe where “perfection” and “order” is the norm. Things always work perfectly beautifully and practically – even in impossible situations, things work out.

A world where problems are identified, and annihilated even before they have a chance to form, so that the “concept of problem” is non-existent. Only working solutions exist. No need to think outside the box to solve that problem. Why think outside the box when the box does not exist in the first place?

It is an art piece about …
Need to learn to code but has only limited electricity in a third-world country? No Problem ! Just go to the library, check out the programming books, read, copy the code on the paper by hand, use your brain as the computer to execute it line by line, predict the output, and confirm when the limited electricity is available.
Everyone is using the washing machine in the NYUAD dorm? No Problem ! Just give an alarm at 2 AM and do the laundry when nobody is doing it.
Need to imitate the sound of Chinese ethnic zither instrument but only have a guitar? No Problem ! Just mess around, and discover that playing tremolo with a pick near the bridge of the guitar in minor pentatonic scale perfectly imitates this Chinese ethnic instrument.
Need a fool-proof way to get rich during a gold rush? No Problem ! Just sell shovels and gold mining equipment to those adrenaline rushed fools and “get their gold” instead
Screw up your meat noodle soup taste while cooking? No Problem ! Enhance the taste by simply pouring in coconut milk, making it like a luxury meal. She will be super impressed, and eat the entire bowl.
Need to train an audio-based neural network, but find that today’s audio processing technology is quite limiting? No Problem ! Just do what these guys do, transform the audio into a spectrogram, and convert the audio problem completely into a computer vision problem, which has a lot of out of the box solutions available already.
Got no one to jam good music with? No Problem ! Tune your guitar to Open D tuning., where you can play all the percussion, bass, melody and chords as a one man band.
Need to very quickly calculate the inverse quare root to calculate your vertex shaders, but the hardware is not powerful since it is 1999 ? No Problem !  Just single handedly write a one-liner inverse square root finding algorithm by exploiting hex values and some bit shift in C code such that

i = 0x5f3759df - ( i >> 1 );

… maybe you will revolutionize the entire early 2000s 3D game industry.
The world facing a deadly COVID-19 virus outbreak? No Problem ! Perhaps if the world had paid attention to high-school students uploading awareness videos onto YouTube months before World Health Organization declares it an official pandemic, and masked up, and we would not have had Zoom classes.

And if you say “Pi, don’t we have to be a genius to see these so-called hacks?” No, these are not hacks, this is pure common sense. You need such arrogance+laziness in your life such that when life hands you with problems you just say “I am super lazy, so I am just going to look at the rules of system, exploit the system with its own rules at its weakest places, so that the entire system crumbles down, and submits to you with unquestioning obedience so that you can command the system to do anything at will, with zero or minimal effort”.

So many of us are cursed with “prerequisite cancer” analysis paralysis .. I need this, I need that, I need tomato potato tomato potato before I can do X,Y,Z … you don’t need experience, you just need to be truly super lazy… and truly be in love with wanting to solve that problem. Then the problem automatically solve itself in the most optimized way, without you doing anything… this is the Supreme Art of Problem Solving.

Then we will have no idea what “struggle” is because we never experience problems, but instantly sees the solution and execute it. That’s good, who needs the journey when you can find an optimized way to claim the reward without the journey?

In short, we don’t “khalas” the system. The system has to “khalas” on us, and meet all our demands, by default, with zero compromise.

2) ⚙️ Technical Plan of Attack & Implementation

The code I am very proud of is of course, the use of loops. Because this piece is about editing the behaviour of a large group of entities, it is all loops over and over again.

//On line 57
for (let i = 0; i < NUM_GROUPS; i++) {
  swarms.push(
    Array.from(
      { length: SWARM_SIZE }, ...

//On line 141
for (let i = 0; i < points.length; i++) {...

// On line 332, this is the blast modelling, without the loops, I will have to write code for each entity in the swarm 😱😱😱
repel() {
    for (let swarm of swarms) {
      for (let entity of swarm) {
        let distance = p5.Vector.dist(entity.location, this.location);
        if (distance <= this.radius * 1.8) {...
// ... and so on

Also, the professor told us that using some random() is appreciated, so here are some randomness…

// When initializing the entities in the swarm, I utilized the random function such as
    swarms.push(
      Array.from(
        { length: SWARM_SIZE },
        () => new Entity(random(width), random(height), i)
      )
    );

Having implemented loops and randomness, let’s add some simple minor details 👌.

The algorithm to simulate a swarm of self-organizing organisms/entities is called the BOIDs algorithm. In human language, to implement a swarm cluster of self-organizing organisms, you just need to tell them to always move toward a goal, and not hit each other.  I also wanted explosions to disturb these swarms, so I lay down the laws of my universe in human language as –

  1. Never get lost. (a.k.a Always have a goal)
  2. Don’t bump into each other.
  3. Explosions are dangerous, run away from them.

Then sprinkle some maths magic and convert the rules into a viable model as follows, to get one cluster of swarm with n entities which follows one target :

swarm

Seeking Behaviour (a.k.a Always have a goal)

Separation Behavior (a.k.a Don’t bump into each other)

Acceleration and Velocity Update


Blast Interaction (a.k.a Explosions are dangerous, run away from them)

To model the explosion, I just have a linear force model.  Easy peasy lemon squeezy 🍋.

Monitoring Chaos of the System

I am also quantifying chaos of the system using average distance of entities to the center. This allows me to tie the rest of the system (audio,colors) to the state of the system. It goes red when chaotic, and green when in order. The chaos is formally defined as
The Stable Chaos Value is the radius of the swarm circle to the origin of the circle. This is because if the average distance of all particles to the center is the same as the radius of the circle, it means the particles are on average, more or less moving in a perfect circle, and hence more organized and less chaotic. As blasts occurs, they are more scattered, average distance increases, and hence chaos value rises.

The formal definitions of funky symbols above are

the steering force applied to an entity
desired velocity, calculated as the normalized vector pointing from the entity’s current location towards the target, scaled to maximum speed
the entity’s current velocity
the separation force
the difference vector between the current entity and the ith neighbor, normalized and weighted by the inverse of their distance.
the number of entities within the desired separation radius
New Velocity of the entity
the total acceleration applied to the entity (sum of all forces)
New Position of the entity
Current Position of the entity
the force exerted by the blast
the direction vector from the blast center to the entity
a scaling factor representing the intensity of the blast
the distance of the ith entity P from the central point C
the number of neighbors within the desired separation radius, in the case of Separation Behavior. In the case of Chaos Calculation, N is the total number of entities considered (we are not using all the entities in calculation for optimization purposes).
Are the equations displaying 👀? If they are, read on.

🤔 Wait a minute, these equations are texts and not images? If you fool around, this Intro to IM website cannot render LaTeX while official wordpress says we should be able to render an equation if you go

$latex i\hbar\frac{\partial}{\partial t}\left|\Psi(t)\right>=H\left|\Psi(t)\right>$
Did Pi just say “No Problem! Just because this wordpress site does not support LaTeX does not mean I cannot write LaTeX” and wrote his own API for LaTeX rendering using KaTeX, probably host it on somewhere like vercel, then render all of his equations and embedded it through iframes?

Practicality and Aesthetics!!!

Self Organizing Behaviour by expanding number of clusters

Then to give a self organizing behaviour as a ring, I just create an array of clusters where each entities in each cluster follows the corresponding target, and these targets rotate in a circle. Then just hide the target so the audience goes “Woah 🤯🤯!”.

3) 🎨 Artistic Plan of Attack & Implementation

Once the practicality part is handled, we enhance the aesthetics through trails, colors, audio,  and connecting lines.

I considered adding bloom effects, but this would overload both my computer and yours, so I decided against it.

4) 💪 Challenges

Again, no challenge. The entire sketch was done in one sitting during the last class lecture.

5) 💡 Potential Improvements

To make the movements of the entities more realistic, I could have used proportional–integral–derivative (PID) controllers. My current model has no calculus, but by just adding these differential and integral terms below:

you can have particles reacting dynamically according to how far they are from the target, and not just static arithmatic.

Also yes, add obstacles, obstacle avoidance and path finding.

6) 🖥️ Source code

🖥️ Source code is just a single sketch.js file at : https://github.com/Pi-31415/Intro-To-IM/blob/main/Assignment-2/swarmfinal.js

📖 References :

Good artists copy, great artists steal. Of course, I stole everything by googling these publicly available stuff below 🤫😉.

Leave a Reply