Loop at many agents with tha samework

28 views
Skip to first unread message

salima

unread,
Dec 11, 2016, 5:25:34 AM12/11/16
to spade-users
Hello,
I have many agents with the same work. I want to create a class Agent which take as parameter the number of the agent. in the main class i want to do a loop based at the number of the agent.
Thanks a lot.

Markus Schatten

unread,
Dec 11, 2016, 8:48:23 AM12/11/16
to spade...@googlegroups.com
Something like:

agents = []
for i in range( 100 ):
agents.append( MyAgent( 'agent_no_%d...@127.0.0.1' % i, 'secret' ) )


Or is the number needed inside the agent, e.g. as an attribute? If you
don't need the attribute, you can access the name of the agents
through the attribute .name. If you need the attribute, you need to
extend __init__ in your class, e.g.:

class MyAgent( Agent ):
def __init__( self, num, *args, **kwargs ):
Agent.__init__( *args, **kwargs )
self.number = num

Then you can instance agents as:

a = MyAgent( 1, 'age...@127.0.0.1', 'secret' )

Best,

M.
--
Markus Schatten, PhD
Assistant professor and head of Artificial Intelligence Lab
University of Zagreb
Faculty of Organization and Informatics
Pavlinska 2, 42000 Varazdin, Croatia
http://www.foi.hr/nastavnici/schatten.markus/index.html
http://www.researchgate.net/profile/Markus_Schatten1
http://ai.foi.hr
> --
> You received this message because you are subscribed to the Google Groups
> "spade-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to spade-users...@googlegroups.com.
> To post to this group, send email to spade...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

salima

unread,
Dec 19, 2016, 1:37:15 AM12/19/16
to spade-users
Thank you for the help.
I finally tried the examlpe below and it works. Can you help me with other examples in the internet.

Best,

import spade
import threading


class MyAgent(spade.Agent.Agent):
def __init__(self, num, *args, **kwargs ):
threading.Thread.__init__(self)
self.number = num

def run(self):
print('Starting Agent')
print(self.number)

if __name__ == "__main__":
a = MyAgent(1,"ag...@127.0.0.1", "secret")
    a.start() 

salima

unread,
Dec 19, 2016, 5:06:43 AM12/19/16
to spade-users
I Try now to do the same thing to an Agent which has a oneShot Behavior. 
import spade
import threading


class MyAgent(spade.Agent.Agent):
def __init__(self, num, *args, **kwargs ):
threading.Thread.__init__(self)
self.number = num

    class MyBehav(spade.Behaviour.OneShotBehaviour):
def onStart(self):
print "Starting behaviour . . ."

def _process(self):
print "Hello World from a OneShot"

def onEnd(self):
print "Ending behaviour . . ."

def _setup(self):
print "MyAgent starting . . ."
b = self.MyBehav()
self.addBehaviour(b, None)


if __name__ == "__main__":
a = MyAgent(1, "ag...@127.0.0.1", "secret")
a.start()

It doesn't work.

Markus Schatten

unread,
Dec 20, 2016, 5:33:55 AM12/20/16
to spade...@googlegroups.com
Hi,

why do you try to initialize the agent as a thread? Agents are threads
by default, no need to initialize. Also, each behaviour is a thread.
You need to call __init__ from the Agent class.

M.
--
Markus Schatten, PhD
Assistant professor and head of Artificial Intelligence Lab
University of Zagreb
Faculty of Organization and Informatics
Pavlinska 2, 42000 Varazdin, Croatia
http://www.foi.hr/nastavnici/schatten.markus/index.html
http://www.researchgate.net/profile/Markus_Schatten1
http://ai.foi.hr


Reply all
Reply to author
Forward
0 new messages