home processing download documents tutorial python tutorial gallery source about
 Tutorials (back to the list of tutorials)

Multi-Agent Algorithm: Update Method (requires iGeo version 7.4.0 or higher)

     Inheriting Agent Class

In this section, coding techniques to use agents are shown. The easiest way to implement your agent is to create a new class inheriting the class "IAgent". IAgent is a template class of agents in iGeo. The code below shows the simplest code to inherit IAgent. This code doesn't create any geometry because it doesn't define any methods yet.

import processing.opengl.*;
import igeo.*;

void setup(){
  size(480, 360, IG.GL);
  MyAgent agent = new MyAgent();
}

class MyAgent extends IAgent{
}

     Define Update Method

The most important method of IAgent is "update()" method. This method is executed at every update cycle of iGeo. The code below shows a template to override update() method.

import processing.opengl.*;
import igeo.*;

void setup(){
  size(480, 360, IG.GL);
  MyAgent agent = new MyAgent();
}

class MyAgent extends IAgent{
  void update(){
    // definition of update behavior
  }
}

Now here is an example to define the behavior of the agent with update() method. Inside the setup() method, IG.duration(int) method defines the number of updating cycle inside iGeo. If you put -1, it makes the cycle endless but please use it carefully because the code could run until it eats up all available memory inside Processing. Not setting the duration with IG.duration(int) also makes the cycle endless.

import processing.opengl.*;
import igeo.*;

void setup(){
  size(480, 360, IG.GL);
  IG.duration(10);
  MyAgent agent = new MyAgent(new IVec(0,0,0));
}

class MyAgent extends IAgent{
  IVec pos;

  MyAgent(IVec p){ pos = p; }

  void update(){
    new IBox(pos, 10, 10, 10);
    new MyAgent(pos.dup().add(10,10,10));
    del();
  }
}

In the code above, MyAgent class has a field of "IVec pos;" to store a location of the agent. Then the constructor is defined to initialize the filed pos.

Inside the update() method, the code creates an instance of IBox at the location of pos and another instance of MyAgent at shifted location, and then it deletes itself. In the updating cycle of iGeo, in every cycle, a new MyAgent instance is created at the shifted location and the instance is deleted in the next cycle after creating a new one. This cycle is repeated until the whole updating cycle ends with the duration specified by IG.duration(int) method.


     Update Method Example1

The code below is an example of multi-agent algorithm using a class inheriting IAgent class. One agent creates random number from 0 to 4 of child agents and put them in random direction. It occasionally proliferates and goes upwards in Z because the random direction excludes negative direction in Z.

import processing.opengl.*;
import igeo.*;

void setup() {
  size(480, 360, IG.GL);
  IG.duration(400);
  int num = 10;
  for (int i=0; i < num; i++) {
    new MyAgent(IRandom.pt(30,30,0), 5).clr(0.5);
  }
  IG.transparent(); //transparent graphic mode
}

class MyAgent extends IAgent {
  IVec pos;
  double size;

  MyAgent(IVec pt, double sz) { 
    pos = pt; 
    size = sz;
  }

  public void update() {
    new IBox(pos, size, size, size).clr(this.clr());
    int childNum = IRandom.getInt(0, 2);

    for (int i=0; i < childNum; i++) {
      double nextSize = IRandom.get(1, 4);
      IVec nextPos = pos.dup();
      //random direction
      double dir=IRandom.getInt(0, 5);
      if (dir==0) nextPos.add(size, 0, 0); //right
      else if (dir==1) nextPos.add(-nextSize, 0, 0); //left
      else if (dir==2) nextPos.add(0, size, 0); //up
      else if (dir==3) nextPos.add(0, -nextSize, 0); //down
      else if (dir==4) nextPos.add(0, 0, size); //above
      // slightly chaning the color
      int r = clr().getRed() + IRandom.getInt(-10, 10);
      int g = clr().getGreen() + IRandom.getInt(-10, 10);
      int b = clr().getBlue() + IRandom.getInt(-10, 10);
      new MyAgent(nextPos, nextSize).clr(r, g, b);
    }
    del();
  }
}

One thing to be noted is that in the setup() method, there is a method of "IG.transparent()", which set the iGeo's shading mode to be "transparent shade without edges" mode. This is because putting wireframe is relatively heavy operation and skipped to show the behavior of the agents faster. Other methods to set shading modes are, IG.wireframe(), IG.fill(), IG.wireframeFill() (default), and IG.wireframeTransparent().


     Update Method Example2

Here is another example of multi-agent algorithm creating lines.

import processing.opengl.*;
import igeo.*;

void setup() {
  size(480, 360, IG.GL);
  IG.duration(300);
  int num = 10;
  for (int i=0; i < num; i++) {
    new MyLineAgent(IRandom.pt(30,30,0),
                    new IVec(10,0,0)).clr(0);
  }
}

static class MyLineAgent extends IAgent {
  IVec pos;
  IVec dir;

  MyLineAgent(IVec pt, IVec dir) { 
    pos = pt; 
    this.dir = dir;
  }

  public void update() {
    IVec pos2 = pos.dup().add(dir);
    new ICurve(pos, pos2).clr(clr());
    int childNum = IRandom.getInt(0, 2); 

    for (int i=0; i < childNum; i++) {
      IVec dir2 = dir.dup();
      double angle = IRandom.get(-PI/4, PI/4);
      dir2.rot(new IVec(0,0,1), angle);
      
      int r = clr().getRed() + IRandom.getInt(-10, 10);
      int g = clr().getGreen() + IRandom.getInt(-10, 10);
      int b = clr().getBlue() + IRandom.getInt(-10, 10);
      new MyLineAgent(pos2, dir2).clr(r,g,b);
    }
    del();
  }
}


     Save Files after Update

When you use agents with IAgent, you cannot save geometries into a file inside setup() method with IG.save(String) because when setup() is executed, the agents have not generated any geometries yet.

Instead, if you type a keyboard shortcut "control + s" after the updating cycle started, a dialog window shows up and you can save geometries agents generate.


(back to the list of tutorials)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
PRIVACY POLICY
ABOUT/CONTACT