spade icon indicating copy to clipboard operation
spade copied to clipboard

Broadcasting between agents

Open jhppires opened this issue 3 years ago • 1 comments

  • SPADE version: 3.2.0
  • Python version: 3.8.10
  • Operating System: W10

Description

I'm trying to broadcast between agents, I want them to message each other constantly, but I'm not getting the agent to reply back. I saw another similar issue (https://github.com/javipalanca/spade/issues/74) , but I couldn't understand what should be done to solve it (sorry, I'm new to working with multi-agent systems).

To clarify, my project is to implement a framework so that allows the instantiation of strategies for Vehicle Routing Problem. The next step would be to implement contract net protocol, if you have any material that can help me I would appreciate it.

I will leave my email, for any reason: [email protected]

What I Did

Here is my code:

class Package():
    def __init__(self, id, volume, weight, location):
        self.id=id
        self.volume=volume 
        self.weigth=weight
        self.location=location  


class DistributionCenter(Agent):
    class DistributionBehaviour(CyclicBehaviour):
        async def on_start(self):
            ic("-------------Testando Distribution-------------------")

        #precisa do run, por padrão, ao começar um CyclicBehaviour ele procura o start (ñ necessario) e depois obrigatoriamente entre no run
        async def run(self):
            msg = Message(to="Deliveryman********")                # Instantiate the message
            msg.set_metadata("performative", "inform")              # Set the "inform" FIPA performative
            pacote = Package(11111,234,89,12321412)
            msg.body = json.dumps(Ex.para_dict(pacote))               # Set the message content (precisa ser em string)
            #ic(msg.prepare())
            await self.send(msg) 

            # stop agent from behaviour
            await self.agent.stop()
        
        async def receive(self):
            msg = await self.receive(timeout=10)
            if msg:
                ic("to aqui")
                ic("Message received with content: {}".format(msg.body))

            else:
                print("Did not received any message after 10 seconds")

        async def on_end(self):
            ic("Termiando Distribution")
    #Necessario inicializar o Behaviour aqui 
    async def setup(self):
        ic("Agent starting . . .")
        b=self.DistributionBehaviour()
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(b, template)
    
class Deliveryman(Agent):
    class DeliverymanBehaviour(CyclicBehaviour):
        async def on_start(self):
            ic("--------------Testando Deliveryman----------------")

        async def run(self):
            ic("RecvBehav running")
            
            msg = await self.receive(timeout=10) # wait for a message for 10 seconds
            if msg:
                msg_test=json.loads(msg.body)
                ic("Message received with content: {}".format(msg_test))

            else:
                print("Did not received any message after 10 seconds")

            msgD = Message(to="Distribution*******")
            msgD.set_metadata("perfomative","inform")
            msgD.body = "Mensagem Recebida"
            await self.send(msgD)

            # stop agent from behaviour
            await self.agent.stop()

        async def on_end(self):
            ic("Finishing Deliverman")

    async def setup(self):
        ic("ReceiverAgent started")
        b = self.DeliverymanBehaviour()
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(b, template)

if __name__ == "__main__":
    DL = Deliveryman("Deliveryman****", "*******")
    future = DL.start()
    future.result()

    #receiver
    DC = DistributionCenter("Distribution***","********")
    DC.start()

    #print(EC)
    Ex.Interrupt(DC, DL)

Here the output: image

jhppires avatar Jan 11 '22 19:01 jhppires