-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (127 loc) · 5.22 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import math
import os
import sys
import json
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from agent import Agent, FixedAgent
from actions import RightGaussianCongestedAction, ConstantAction
from simulation import Simulation
from get_params import *
#settings to control the simulation
args = get_args(*sys.argv[1:])
AGENT_NUMBERS = [1000, 1000, 1000] #number of agents of each type
#parameters (sensitivity and cost for each action) for each agent type
INITIAL_AGENT_PARAMETERS = [
[[1.3, 0], [1, 0], [1, 0]],
[[1, 0.1], [1.35, 0.11], [1.4, 0]],
[[1.4, -0.2], [0.7, 0], [0.8, 0]],
]
#entry [i][j] controls the strength of interaction from agents of group
#i to agents of group j - WAIFW (who acquires influence from whom) matrix
"""
INFLUENCE_MATRIX = (
(1, 0.3, 0.3),
(0.3, 1, 0.3),
(0.3, 0.3, 1),
)
"""
INFLUENCE_MATRIX = (
(0.1, 0.03, 0.03),
(0.003, 0.1, 0.03),
(0.03, 0.03, 0.1),
)
LEARNING_RATE = 0.1
DISCOUNT_RATE = 0.75
COLOURS = ["blue", "red", "orange", "green", "purple"]
PLOT_FREQUENCY = 1_000 #how often to display a plot of actions so far
TOTAL_TIMESTEPS = args.total_timesteps
#the set of actions that agents can take
actions = [RightGaussianCongestedAction("Car", 0, 1, 0.4),
RightGaussianCongestedAction("Bus", 0.4, 1.14, 0.35),
ConstantAction("Walk", 1),
]
#create a list to store the agents
agents = []
for agent_type_number, amount in enumerate(AGENT_NUMBERS):
agents_of_current_type = []
for j in range(amount):
#create an agent
a = Agent(
action_count=len(actions),
learning_rate=LEARNING_RATE,
discount_rate=DISCOUNT_RATE,
parameters = [[a[0], a[1]] for a in INITIAL_AGENT_PARAMETERS[agent_type_number]],
group_number=agent_type_number
)
agents_of_current_type.append(a)
agents.append(agents_of_current_type)
simulation = Simulation(
actions=actions,
agents=agents,
topology=None,
)
simulation.plot_action_profiles()
#create a graph to represent social connections of agents
if args.graph_gen == "ws":
agent_graph = nx.watts_strogatz_graph(sum(AGENT_NUMBERS), int(args.graph_param1), args.graph_param2)
elif args.graph_gen == "er":
agent_graph = nx.erdos_renyi_graph(sum(AGENT_NUMBERS), args.graph_param1)
elif args.graph_gen == "ba":
agent_graph = nx.barabasi_albert_graph(sum(AGENT_NUMBERS), int(args.graph_param1))
#subax1 = plt.subplot(121)
#nx.draw(agent_graph, with_labels=False, font_weight='bold')
#plt.show()
#annotate nodes in the agent graph with agent objects
current_index = 0
for agent_type_number, amount in enumerate(AGENT_NUMBERS):
for i in range(amount):
agent_graph.nodes[current_index]["agent_object"] = agents[agent_type_number][i]
current_index += 1
step = 0
while step < args.total_timesteps:
step += 1
simulation.timestep()
if step == 6000 and args.intervention and args.use_agent_graph:
#apply intervention
print(step, INITIAL_AGENT_PARAMETERS)
INITIAL_AGENT_PARAMETERS[0][2][0] *= 1.14
INITIAL_AGENT_PARAMETERS[2][2][0] *= 1.8
for agent_node in range(sum(AGENT_NUMBERS)):
current_agent = agent_graph.nodes[agent_node]["agent_object"]
if not args.use_agent_graph:
if step == 6000 and args.intervention:
#apply intervention to individual nodes when there is no agent graph,
#as changing intial params will have no effect
print(step, INITIAL_AGENT_PARAMETERS)
if current_agent.group_number == 0:
current_agent.reward_parameters[2][0] *= 1.14
elif current_agent.group_number == 2:
current_agent.reward_parameters[2][0] *= 1.8
if args.use_agent_graph:
#propogate influence through the social graph
#retrieve the agent object associated with this node
current_agent = agent_graph.nodes[agent_node]["agent_object"]
neighbours = agent_graph[agent_node]
#calculate the amount of influence this agent has towards each action
influence = np.zeros(len(actions))
for key in neighbours:
#retrieve the agent object of the neighbour
neighbour = agent_graph.nodes[key]["agent_object"]
influence[neighbour.previous_action] += INFLUENCE_MATRIX[neighbour.group_number][current_agent.group_number]
#normalise by dividing by the number of neighbours - having more
#neighbours shouldn't make you more susceptible to influence
if len(neighbours) > 0:
influence /= len(neighbours)
#change sensitivities of agent to account for influences
for action in range(len(actions)):
original = INITIAL_AGENT_PARAMETERS[current_agent.group_number][action][0]
current_agent.reward_parameters[action][0] = original + influence[action]
agent_node = 0
name = args.run_name
if args.use_agent_graph:
name += "_" + str(args.graph_gen) + "_p1_" + str(args.graph_param1) + "_p2_" + str(args.graph_param2)
simulation.save(name)
with open("jan_runs/" + name + "args.json", "w") as f:
f.write(json.dumps(vars(args)))