Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcasting between agents #100

Open
jhppires opened this issue Jan 11, 2022 · 1 comment
Open

Broadcasting between agents #100

jhppires opened this issue Jan 11, 2022 · 1 comment

Comments

@jhppires
Copy link

  • 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 (#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: johannpires@furg.br

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

@0x1F602
Copy link

0x1F602 commented May 21, 2023

@jhppires I think separating the listening and sending behaviours is optimal. I like sending behaviours to be oneshot and listening behaviours to be cyclic. when a message is received, fire off a one shot reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants