home processing download documents tutorial python tutorial gallery source about
 Python 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.

add_library('igeo')

def setup() : 
    size(480, 360, IG.GL)
    agent = MyAgent()

class MyAgent(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.

add_library('igeo')

def setup() : 
    size(480, 360, IG.GL)
    agent = MyAgent()

class MyAgent(IAgent) : 
    def update(self) : 
        # 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.

add_library('igeo')

def setup() : 
    size(480, 360, IG.GL)
    IG.duration(10)
    agent = MyAgent(IVec(0,0,0))

class MyAgent(IAgent) : 
    def __init__(self, p) :
       self.pos = p
    
    def update(self) : 
        IBox(self.pos, 10, 10, 10)
        MyAgent(self.pos.dup().add(10,10,10))
        self.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.

add_library('igeo')

def setup() : 
    size(480, 360, IG.GL)
    IG.duration(400)
    num = 10
    for i in range(num) : 
        MyAgent(IRand.pt(30,30,0), 5).clr(0.5)
    IG.transparent() #transparent graphic mode

class MyAgent(IAgent) : 
  
    def __init__(self, pt, sz) : 
        self.pos = pt
        self.size = sz
  
    def update(self) : 
        IBox(self.pos, self.size, self.size, self.size).clr(self.clr())
        childNum = IRand.getInt(0, 2)
        
        for i in range(childNum) : 
            nextSize = IRand.get(1, 4)
            nextPos = self.pos.dup()
            #random direction
            dir=IRand.getInt(0, 5)
            if dir==0 :  
                nextPos.add(self.size, 0, 0) #right
            elif dir==1 : 
                nextPos.add(-nextSize, 0, 0) #left
            elif dir==2 : 
                nextPos.add(0, self.size, 0) #up
            elif dir==3 : 
                nextPos.add(0, -nextSize, 0) #down
            elif dir==4 : 
                nextPos.add(0, 0, self.size) #above
            # slightly chaning the color
            r = self.clr().getRed() + IRand.getInt(-10, 10)
            g = self.clr().getGreen() + IRand.getInt(-10, 10)
            b = self.clr().getBlue() + IRand.getInt(-10, 10)
            MyAgent(nextPos, nextSize).clr(r, g, b)
        
        self.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.

add_library('igeo')

def setup() : 
    size(480, 360, IG.GL)
    IG.duration(300)
    num = 10
    for i in range(num) : 
        MyLineAgent(IRand.pt(30,30,0), IVec(10,0,0)).clr(0)

class MyLineAgent(IAgent) :
    
    def __init__(self, pt, dir) :
        self.pos = pt 
        self.dir = dir
    
    def update(self) : 
        pos2 = self.pos.dup().add(self.dir)
        ICurve(self.pos, pos2).clr(self.clr())
        childNum = IRand.getInt(0, 2) 
        
        for i in range(childNum) : 
            dir2 = self.dir.dup()
            angle = IRand.get(-PI/4, PI/4)
            dir2.rot(IVec(0,0,1), angle)
            
            r = self.clr().getRed() + IRand.getInt(-10, 10)
            g = self.clr().getGreen() + IRand.getInt(-10, 10)
            b = self.clr().getBlue() + IRand.getInt(-10, 10)
            MyLineAgent(pos2, dir2).clr(r,g,b)
        self.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