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

ContractNetFIPA_example with neo4j #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions examples/Agent_example_neo4j
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# -*- coding: utf-8 -*-
from pade.misc.utility import display_message, start_loop
from pade.core.agent import Agent
from pade.acl.aid import AID
from pade.acl.messages import ACLMessage
from pade.behaviours.protocols import FipaContractNetProtocol
from sys import argv
from random import uniform
import py2neo
from py2neo import Graph


class CompContNet1(FipaContractNetProtocol):
'''CompContNet1

Initial FIPA-ContractNet Behaviour that sends CFP messages
to other feeder agents asking for restoration proposals.
This behaviour also analyzes the proposals and selects the
one it judges to be the best.'''

def __init__(self, agent, message):
super(CompContNet1, self).__init__(
agent=agent, message=message, is_initiator=True)
self.cfp = message

def handle_all_proposes(self, proposes):
"""
"""

super(CompContNet1, self).handle_all_proposes(proposes)

best_proposer = None
highest_score = 0.0
other_proposers = list()
display_message(self.agent.aid.name, 'Analyzing proposals...')

i = 1

# logic to select proposals by the higher available power.
for message in proposes:
a = """Your Neo4j Cypher Query"""
t= graph.run(a).data()
content = message.content
test = max(t, key=lambda x:x['score'])
content = test
best_score = content
display_message(self.agent.aid.name,
'Analyzing proposal {i}'.format(i=i))
display_message(self.agent.aid.name,
'Best Score is: {pot}'.format(pot=best_score))
i += 1
best_score = best_score['score']
if best_score > highest_score:
if best_proposer is not None:
other_proposers.append(best_proposer)

highest_score = best_score
best_proposer = message.sender

else:
other_proposers.append(message.sender)

display_message(self.agent.aid.name,
'The best proposal was: {pot}'.format(
pot=highest_score))

if other_proposers != []:
display_message(self.agent.aid.name,
'Sending REJECT_PROPOSAL answers...')
answer = ACLMessage(ACLMessage.REJECT_PROPOSAL)
answer.set_protocol(ACLMessage.FIPA_CONTRACT_NET_PROTOCOL)
answer.set_content('')
for agent in other_proposers:
answer.add_receiver(agent)

self.agent.send(answer)

if best_proposer is not None:
display_message(self.agent.aid.name,
'Sending ACCEPT_PROPOSAL answer...')

answer = ACLMessage(ACLMessage.ACCEPT_PROPOSAL)
answer.set_protocol(ACLMessage.FIPA_CONTRACT_NET_PROTOCOL)
answer.set_content('OK')
answer.add_receiver(best_proposer)
self.agent.send(answer)

def handle_inform(self, message):
"""
"""
super(CompContNet1, self).handle_inform(message)

display_message(self.agent.aid.name, 'INFORM message received')

def handle_refuse(self, message):
"""
"""
super(CompContNet1, self).handle_refuse(message)

display_message(self.agent.aid.name, 'REFUSE message received')

def handle_propose(self, message):
"""
"""
super(CompContNet1, self).handle_propose(message)

display_message(self.agent.aid.name, 'PROPOSE message received')


class CompContNet2(FipaContractNetProtocol):
'''CompContNet2

FIPA-ContractNet Participant Behaviour that runs when an agent
receives a CFP message. A proposal is sent and if it is selected,
the restrictions are analized to enable the restoration.'''

def __init__(self, agent):
super(CompContNet2, self).__init__(agent=agent,
message=None,
is_initiator=False)

def handle_cfp(self, message):
"""
"""
self.agent.call_later(1.0, self._handle_cfp, message)

def _handle_cfp(self, message):
"""
"""
super(CompContNet2, self).handle_cfp(message)
self.message = message

display_message(self.agent.aid.name, 'CFP message received')

answer = self.message.create_reply()
answer.set_performative(ACLMessage.PROPOSE)
answer.set_content(str(self.agent.pot_disp))
self.agent.send(answer)

def handle_reject_propose(self, message):
"""
"""
super(CompContNet2, self).handle_reject_propose(message)

display_message(self.agent.aid.name,
'REJECT_PROPOSAL message received')

def handle_accept_propose(self, message):
"""
"""

super(CompContNet2, self).handle_accept_propose(message)

display_message(self.agent.aid.name,
'ACCEPT_PROPOSE message received')

answer = message.create_reply()
answer.set_performative(ACLMessage.INFORM)
answer.set_content('OK')
self.agent.send(answer)


class AgentInitiator(Agent):

def __init__(self, aid, participants):
super(AgentInitiator, self).__init__(aid=aid, debug=False)

message = ACLMessage(ACLMessage.CFP)
message.set_protocol(ACLMessage.FIPA_CONTRACT_NET_PROTOCOL)
message.set_content('60.0')

for participant in participants:
message.add_receiver(AID(name=participant))

self.call_later(8.0, self.launch_contract_net_protocol, message)

def launch_contract_net_protocol(self, message):
comp = CompContNet1(self, message)
self.behaviours.append(comp)
comp.on_start()


class AgentParticipant(Agent):

def __init__(self, aid, pot_disp):
super(AgentParticipant, self).__init__(aid=aid, debug=False)

self.pot_disp = pot_disp

comp = CompContNet2(self)

self.behaviours.append(comp)

if __name__ == "__main__":
graph = Graph("bolt://localhost:7687", auth=("Username", "Password"))
agents_per_process = 1
c = 0
agents = list()
for i in range(agents_per_process):
port = int(argv[1]) + c
k = 10000
participants = list()

agent_name = 'agent_participant_{}@localhost:{}'.format(port - k, port - k)
participants.append(agent_name)
agente_part_1 = AgentParticipant(AID(name=agent_name), uniform(100.0, 600.0))
agents.append(agente_part_1)

agent_name = 'agent_participant_{}@localhost:{}'.format(port + k, port + k)
participants.append(agent_name)
agente_part_2 = AgentParticipant(AID(name=agent_name), uniform(100.0, 600.0))
agents.append(agente_part_2)

agent_name = 'agent_initiator_{}@localhost:{}'.format(port, port)
agente_init_1 = AgentInitiator(AID(name=agent_name), participants)
agents.append(agente_init_1)

c += 1000

start_loop(agents)