-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathargparse_agent.py
More file actions
229 lines (191 loc) · 7.85 KB
/
argparse_agent.py
File metadata and controls
229 lines (191 loc) · 7.85 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import gym
from custom_env import threshold_env
from DQN import KerasDQN
import numpy as np
import csv
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("n_agents", type=int)
parser.add_argument("n_actions", type=int)
parser.add_argument("threshold", help="How many agents can successfully transmit at the same time", type=int)
parser.add_argument("feature_histories", help="Use information from the past x steps as features", type=int)
parser.add_argument("save_data_path", type=str)
args = parser.parse_args()
n_agents = args.n_agents
n_actions = args.n_actions
threshold = args. threshold
feature_histories = args.feature_histories
save_data_path = args.save_data_path
# --------------------> Parameters <--------------------
save = True # Save data to CSV
n_iterations = 1 # How many full simulations to run
# ------------------------------------------------------------
# --------------------- Create Env ---------------------
n_steps = 1e4
transmit_and_sense = False
env = threshold_env(n_agents, threshold, n_steps, transmit_and_sense=transmit_and_sense)
# ------------------------------------------------------
def state_to_observations(state):
"""
Input:
- obs [list or np.array]: Concatenated list of all observations
Returns:
- list of lists of observations for each agent
"""
n_obs_per_agent = len(state) // n_agents
#print("n_obs_per_agent", n_obs_per_agent)
agent_obs = [np.array(state[i * n_obs_per_agent: (i + 1) * n_obs_per_agent]).reshape(1, -1) for i in range(n_agents)]
return agent_obs
# ---------------------- Training Loop --------------------
currIt = 0
while True:
# --------------------- Create Agents ---------------------
n_inputs = 4 * feature_histories
# DQN
agents = [KerasDQN(n_inputs, n_actions,
hidden_layer_one_dims=128,
hidden_layer_two_dims=256,
batch_size=64,
epsilon_min=0.05) for _ in range(n_agents)]
# ------------------------------------------------------
stepIdx = 0
rewards = []
action_list = []
states = []
scores = [[] for _ in range(n_agents)] # is this the same as rewards?
rewards = []
state = env.reset() # If I refactor state, make this work
state = [np.zeros(n_inputs).reshape(1, -1) for _ in range(n_agents)]
next_state = [np.zeros(n_inputs).reshape(1, -1) for _ in range(n_agents)]
print("starting next state", next_state)
# For multi-step actions
state_at_action = [np.zeros(n_inputs).reshape(1, -1) for _ in range(n_agents)]
future_actions = [[] for _ in range(n_agents)]
action_duration = [0 for _ in range(n_agents)]
reward_over_actions = [[] for _ in range(n_agents)]
actions = [0 for _ in range(n_agents)] # Action selected by the agent (could be multi-step)
actions_to_take = [0 for _ in range(n_agents)] # do/don't transmit on this step. In {0, 1}
while True:
#print("\nStep", stepIdx)
# Get Actions ------------------------------
for i in range(n_agents):
# if buffer is 0 don't use RL, also don't save if no RL was used
if state[i][0][-1] == 0:
#actions.append(-1) # Original
actions[i] = -1
future_actions[i] = [-1]
# If the action_duration is 0, get a new action,
elif action_duration[i] == 0: # make sure this can't be negative
# Get action, save state, set future actions, and action_duration
agent_action = agents[i].choose_action(state[i])
#print("agent", i, "action", agent_action)
state_at_action[i] = state[i]
if agent_action == 0:
future_actions[i] = [0]
elif agent_action == 1:
future_actions[i] = [1]
elif agent_action == 2:
future_actions[i] = [0, 1]
elif agent_action == 3:
future_actions[i] = [0, 0, 1]
elif agent_action == 4:
future_actions[i] = [0, 0, 0, 1]
elif agent_action == 5:
future_actions[i] = [0, 0, 0, 0, 1]
elif agent_action == 6:
future_actions[i] = [0, 0, 0, 0, 0, 1]
elif agent_action == 7:
future_actions[i] = [0, 0, 0, 0, 0, 0, 1]
elif agent_action == 8:
future_actions[i] = [0, 0, 0, 0, 0, 0, 0, 1]
elif agent_action == 9:
future_actions[i] = [0, 0, 0, 0, 0, 0, 0, 0, 1]
elif agent_action == 10:
future_actions[i] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
else:
raise ValueError
# Update actions if a new decision is made
actions[i] = agent_action
action_duration[i] = len(future_actions[i])
# Set action to take by popping future action
actions_to_take[i] = future_actions[i].pop(0)
# -------------------------------------------------------------------
# Take an environment step
new_state_info, reward, done, info = env.step(actions_to_take)
#print("next state before", new_state_info)
"""
# Feature history stuff (testing)
for i in range(n_agents):
# Shift elements to the left
for j in range(len(next_state[i][0]) - len(new_state_info[i])):
next_state[i][0][j] = next_state[i][0][j + len(new_state_info[i])]
# Add new state information to the end of the list
for k in range(len(new_state_info[i])):
next_state[i][0][-1 * len(new_state_info[i]) + k] = new_state_info[i][k]
"""
next_state = state_to_observations(new_state_info)
#print("next state after", next_state, "\n")
"""
print("state", state)
print("actions to take", actions_to_take)
print("next_state", next_state)
print("reward", reward)
print("done", done)
print("info", info)
"""
# Decrement all action durations
action_duration = [duration - 1 for duration in action_duration]
# Remember reward and transitions
for i in range(n_agents):
agent_action = actions[i]
#print("action", agent_action)
# Check if RL was not used
if agent_action == -1: # RL agent not invoked. Do not save transition to memory
continue
# Add reward to reward_over_actions
agent_reward = reward[i] # For now, reward is the same for all agents
reward_over_actions[i].append(agent_reward)
# Save transitions only when action_duration == 0
if action_duration[i] == 0:
agent_state = state_at_action[i]
agent_next_state = next_state[i]
# Average reward
agent_average_reward_over_action = float(np.mean(reward_over_actions[i]))
# Save transition with the state at the time of the action decision and
# the average reward over the course of the action
agents[i].remember(agent_state, agent_action, agent_average_reward_over_action,
agent_next_state, done)
agents[i].learn() # Could be moved outside of the "if" block
# Clear reward_over_actions
reward_over_actions = [[] for _ in range(n_agents)]
for i in range(n_agents):
scores[i].append(reward[i])
# print("reward", reward)
# print("acitons", actions)
# print("states", state)
rewards.append(reward.copy())
action_list.append(actions.copy())
states.append(state)
# print(len(rewards), reward)
# print(len(action_list), action_list)
# print(len(states), states)
# print()
state = next_state
stepIdx += 1
if stepIdx % 100 == 0:
print("Step: ", stepIdx)
for i in range(n_agents):
print("mean (last 50)", np.mean(scores[i][-50:]))
if i == (n_agents - 1):
print()
if done:
# Record data in CSV
if save == True:
data = [list(reward) + list(action) + list(np.array(state).flatten()) for reward, action, state in zip(rewards, action_list, states)]
with open(save_data_path + "data" + str(currIt) + ".csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
break
currIt += 1
if currIt == n_iterations:
break