-
Notifications
You must be signed in to change notification settings - Fork 0
/
AFB_3Agents_example.py
70 lines (53 loc) · 2.05 KB
/
AFB_3Agents_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from Agent import Agent
def main():
# Main definition will AFB
# Number of agents
agent_n = 3
# Predefined hashing function
hash_1_2 = {'R,R': 10, 'R,B': 6, 'B,R': 27, 'B,B': 20}
hash_1_3 = {'R,R': 5, 'R,B': 3, 'B,R': 34, 'B,B': 16}
hash_2_3 = {'R,R': 1, 'R,B': 9, 'B,R': 8, 'B,B': 15}
# Domain per agent
domain_agent_1 = ["R", "B"]
domain_agent_2 = ["R", "B"]
domain_agent_3 = ["R", "B"]
# Hash list used per agent
agent1_hash = [hash_1_2, hash_1_3]
agent2_hash = [hash_1_2, hash_2_3]
agent3_hash = [hash_1_3, hash_2_3]
# Contraints per agent
agent1_cons = [2, 3]
agent2_cons = [1, 3]
agent3_cons = [1, 2]
# Domain list per agent
agent1_domains = [domain_agent_1, domain_agent_2, domain_agent_3]
agent2_domains = [domain_agent_2, domain_agent_1, domain_agent_3]
agent3_domains = [domain_agent_3, domain_agent_1, domain_agent_2]
# fill in hash
agent_list = [None,
Agent(1, agent_n, agent1_domains, agent1_cons, agent1_hash),
Agent(2, agent_n, agent2_domains, agent2_cons, agent2_hash),
Agent(3, agent_n, agent3_domains, agent3_cons, agent3_hash)]
# converged(boolean)
converged = False # Hasn't converged yet
while not converged:
for agent in agent_list:
if agent is None: # Skip first index
continue
# do something with agent
msg_list = agent.step() # step through one full unit
# Skip any further execution
if msg_list is None:
continue
# process messages by adding to agent queues
for msg in msg_list:
if msg.get_type() == 'TERMINATE':
converged = True # We have converged.
print("[Agent: %d] %s"%(agent._id, msg))
agent_list[int(msg.get_destination())].receive(msg)
# Print out result
# Select first Agent
print(agent_list[agent_n].solution())
# Execute Simulator
if __name__ == "__main__":
main()