## Example 11.4 – Simple Agent to Agent Interaction

The last few examples with agents were looking at fixed steering points to influence the agent’s behavior. In this example, the agents themselves can influence the behavior of others. We will start with some very simple behaviors, and in future examples we will add a few more behaviors and play around with them. Bear in mind though, even changing the parameters of one simple behavior without completely rewriting the code can have a big effect on the resulting form.

In this example, we will imagine there are a group of agents walking around in a square room. They walk in a straight line until they encounter an obstacle. If this obstacle is a wall, they simply reflect themselves back into the room. This behavior of reflection is probably not what a person or animal would do in real life, but it is useful to know how reflect works since it can come in handy in many applications. It will also keep our agents from running away. 😉

The second behavior is an avoidance behavior. People perform this behavior all the time, often without realizing it. If you are walking through a crowd of people, you subtly alter your path to avoid collisions. In our case, the agents will, once a neighbor comes into a sphere of effect (their “comfort zone”), alter their vector to avoid the neighbor.

**Step One – Initial Setup**

This is a straightforward version of a setup I have used many times before, so i won’t explain in too much detail. The top piece produces a “Movement Increment” factor based on the overall dimensions. The other pieces produce a container of points, and another container of associated, semi-random vectors. These will go into the loop.

**Step Two – First Behavior – Reflection at Walls**

As the points start moving along their vectors, eventually they will come to the area’s outer boundary. I don’t want them to run away, so I am going to simply reflect them back into the playing field before they cross the line. I do this by finding the closest point along the edge curve, and testing the distance to the edge. If the distance is too low (less than the movement interval), the agents are reflected. To do this, I use the “Sift” component to temporarily take out of the list the subset of points and vectors that are of interest. After reflecting, I will bring the two lists back together with the “Combine” component.

The math for this was a bit harder than I thought, but I found an explanation of what was going on here. Basically, you need two vectors, the incoming vector to be reflected, and the Normal vector (the vector perpendicular to the surface of reflection). These two are magically summed together to get what is called the vector “Dot Product” using complex equations. These three things, the Vector, the Normal, and the Dot Product are then fed into an equation to get the reflected vector. If you don’t have the patience to figure out what is going on, just copy exactly what I did in the script above, using the right formula.

Note The formula box will turn red as there are null values going in from our sift operation…but we need to keep those nulls to recombine the data later…so just ignore the red. Once the reflected vectors are produced, the are recombined with the rest of the vectors, keeping their position in the list.

**Step Three – Keeping A Point History**

if I run the loop as done in step two, my points should be moving and reflecting correctly. It will only keep the latest point in the sequence, however. I want to keep track of the whole history of points, however, so I am going to add some data structuring and listing elements to my script per the image above. Pay close attention to the data structure! the basic concept is to use a combination of “List Item” to get the leading point in each list of points, to move this point in some direction based on a series of behaviors, and then to insert this moved point back into an ongoing list using “Insert item”.

**Step Four – Avoidance Behavior**

This is the most important concept in this example, introducing a behavior to steer the agents based on proximity to a neighbor. I am calling this the second behavior, but the way I set up the overall loop, you will notice it is actually happening first (before the reflection routine).

The image below explains graphically what is going on.

Basically, each Agent has an invisible bubble around it, a Comfort Zone. If no other agent is int the comfort zone. The current vector will continue unaltered. But when another agent is in the bubble…watch out! In this case, the Agent will try and move away from the neighbor who happens to be closest in this bubble. The way this is done is through simple vector addition, adding the current vector with the vector between the agent’s neighbor and back to itself, multiplied by a scaling factor. If the scaling factor is low, the avoidance will be slight and slow. If the factor is high, the current heading will have less importance and the Agent will try and get away from the neighbor as quickly as possible.

Note, if you were to reverse the vector between the agent and its neighbor, it would become an attraction behavior instead of avoidance behavior. Below is a detail of the “Avoidance” behavior for reference.

**Step Five – Track Agent History through circle sizes**

That was the basic agent process. This last step is just something I did after the loop to show the agent’s current location with a big circle, and its history with exponentially smaller circles. I won’t explain the logic, but you should try your own ways of representing the agents and their movement over time (fading circles?). You can decide how many rounds you want to show. Showing footprints from the last 50 iterations, 100, or 200. will influence the pattern.

**Variations**

In the variations below, I used a 20° starting angle for all examples except the first. I showed a history of 200 iterations. The only factors I played with were the “Area of Effect” and the “Strength of Turn”. Note in the first two examples both of these are “zero” which essentially turns the avoidance behavior off.

Note Some interesting patterns and personalities emerge by changing the two factors. When “Strength of Turn” is high, the agents seem anxious and nervous, but when this is low, they seem cool, calm, and collected.

When the radius of effect is high, combined with a high strength of turn, the agents crawl off into their own personal corners and cry, afraid of the world and everything around them. The most interesting and dynamic patterns emerge when the two factors are balanced somewhere in the middle.

I’ve been trying to troubleshoot this script for a few days now. I followed the steps / explanation and my script seems to be correct but each of my agents’ path are splitting into three paths for some reason. Any clue?

LikeLike

when things start multiplying or splitting unexplainably, that’s usually a hint something is up with the data structure. Maybe you missed one of the grafts or flattens

LikeLike

step 1 is giving me issues…I basically can’t map the randomized vector/vector angles to the starting agents.all the randomized angles are starting from 0,0,0.

LikeLike

technically the vectors are directions…they have no fixed Position in space. if they are all starting at 0,0,0 then you are using 0,0,0 as an anchor Point.

if the list of vectors in step one is structured the same as the list of Points at the end of step one, the vectors should be associated with the Point that has a corresponding data position

LikeLike

Hi there! Your tutorial is so inspiring!

May I ask what is the full name of the “loop start” component in step 1?

Many thanks!

LikeLike

The loop start and loop end components both come from the “Anemone” add on which can be dowloaded for free from http://www.food4rhino.com. You can refer to example 8.1 https://generativelandscapes.wordpress.com/2014/08/28/ice-ray-lattice-basic-recursion-example-8-1/ for the basics on how to set up a loop.

LikeLike

I’ve been having a lot of fun messing with / building up this definition!

Do you have any tips / thoughts on how to bound the agents within a 3 dimensional space aka reflect off of surfaces vs curves?

I’ve tried creating a “birdcage” (contouring a brep in x, y, z) to maintain the logic of your definition (boundary defined by a curve) but was unsuccessful.

I also tried srf closest point instead of curve closest point for the “test point close to edge” step but still unsuccessful.

Thanks!

LikeLike