-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_G_table.py
More file actions
258 lines (228 loc) · 12.2 KB
/
eval_G_table.py
File metadata and controls
258 lines (228 loc) · 12.2 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import itertools
import flax
import jraph
import jax
import jax.numpy as jnp
import flax.linen as nn
import string
import numpy as np
import optax
import matplotlib
from purejaxrl.mfc_wrapper import MFVRPFiniteWrapperEnv
import matplotlib.pyplot as plt
from cycler import cycler
import orbax
import os
from brax.envs import register_environment
from flax.linen.initializers import constant, orthogonal
from typing import Sequence, NamedTuple, Any
from flax.training.train_state import TrainState
import distrax
from purejaxrl.mfc import MFVRPEnv, EnvState
from flax.training import orbax_utils
from purejaxrl.ppo_mfc_mlp import ActorCritic, Transition
from purejaxrl.wrappers import (
LogWrapper,
BraxGymnaxWrapper,
VecEnv,
NormalizeVecObservation,
NormalizeVecReward,
)
def load_and_plot(config, rng):
config["NUM_UPDATES"] = (
config["TOTAL_TIMESTEPS"] // config["NUM_STEPS"] // config["NUM_ENVS"]
)
config["MINIBATCH_SIZE"] = (
config["NUM_ENVS"] * config["NUM_STEPS"] // config["NUM_MINIBATCHES"]
)
print(f'NUM_UPDATES: {config["NUM_UPDATES"]}, MINIBATCH_SIZE: {config["MINIBATCH_SIZE"]}')
num_samps = 50
max_tour_length = 15
datasets = ["clustered", "uniform", "cities"]
debug_print = False
for k in [3, 5, ]: # 8, 10,
Ns = [100, 500, 1000, 5000] if k == 5 or k == 3 else [500,]
exp_dirs = [f"0_l0_clustered_k{k}_N500_samp{965 if k==3 else 945 if k==5 else 950}"]
# The differences between MFC and Finite are from discretization.
# The difference between Finite and Final are from removing detours and transporting leftovers
for dataset in datasets:
print(f"dataset={dataset}")
for exp_dir in exp_dirs:
print(f"exp_dir={exp_dir}")
with open(f'{os.getcwd()}/results/flax_ckpt/{exp_dir}/params.jnp', "rb") as file:
params = jnp.load(file, allow_pickle=True).item()
# with open(f'{os.getcwd()}/results/flax_ckpt/{exp_dir}/brief_metrics.jnp', "rb") as file:
# progress_csv = jnp.load(file, allow_pickle=True).item()
mean_distance_travelled_Ns = []
for N in Ns:
print(f"N={N}")
mean_distance_travelled = []
""" Put result into dummy environment and see what happens """
env = MFVRPEnv(k=k, N=N, dataset=dataset, load_datasets=False)
env_params = env.default_params
# INIT NETWORK
network = ActorCritic(
env.action_space(env_params).shape[0], activation=config["ACTIVATION"]
)
import time
start_time = time.time()
for samp in range(num_samps):
print(f"samp={samp}")
""" Forward pass """
rng, _rng = jax.random.split(rng)
env = MFVRPEnv(k=k, N=N, dataset=dataset, finetuning=samp)
obsv, env_state = env.reset(_rng, env_params)
value0 = None
value1 = None
value2 = None
value3 = None
value4 = None
done = False
states = []
pis = []
rng, _rng = jax.random.split(rng)
timesteps = 0
while not done:
rng, _rng = jax.random.split(rng)
# pi, value = network.apply(train_state.params, obsv)
pi, value = network.apply(params, obsv) # For Debug
pis.append(pi)
action = pi.sample(seed=_rng)
rng, _rng = jax.random.split(rng)
obsv, env_state, reward, done, info = env.step(_rng, env_state, action, env_params)
states.append(env_state)
# print(env_state)
# print(f"{np.array(obsv[-28:][:4])}, {np.mean(env_state.objects[:, -1])}")
# print(reward)
value0 = reward if value0 is None else value0 + reward
value1 = info["v1"] if value1 is None else value1 + info["v1"]
value2 = info["v2"] if value2 is None else value2 + info["v2"]
value3 = info["v3"] if value3 is None else value3 + info["v3"]
value4 = info["v4"] if value4 is None else value4 + info["v4"]
timesteps += 1
if debug_print:
print(f"MFC Lim.: \t {-value2 - value3} \t \t val{value1}")
# print("...")
# print("Finite")
# print("...")
""" Evaluate on real wrapper env """
env = MFVRPFiniteWrapperEnv(k=k, N=N, dataset=dataset, finetuning=samp, debug=True)
obsv, env_state = env.reset(_rng, env_params)
init_state: EnvState = env_state
value0 = None
value1 = None
value2 = None
value3 = None
value4 = None
agent_object_assignments = []
transported_objects = []
dists_pos_to_source = []
dists_source_to_destination = []
done = False
states = []
rng, _rng = jax.random.split(rng)
for curr_time in range(timesteps):
rng, _rng = jax.random.split(rng)
# pi, value = network.apply(train_state.params, obsv)
pi = pis[curr_time]
action = pi.sample(seed=_rng)
rng, _rng = jax.random.split(rng)
obsv, env_state, reward, done, info = env.step(_rng, env_state, action, env_params)
states.append(env_state)
# print(env_state)
# print(f"{np.array(obsv[-28:][:4])}, {np.mean(env_state.objects[:, -1])}")
# print(reward)
value0 = reward if value0 is None else value0 + reward
value1 = info["v1"] if value1 is None else value1 + info["v1"]
value2 = info["v2"] if value2 is None else value2 + info["v2"]
value3 = info["v3"] if value3 is None else value3 + info["v3"]
value4 = info["v4"] if value4 is None else value4 + info["v4"]
agent_object_assignments.append(info["agent_object_assignments"])
transported_objects = info["transported_objects"]
dists_pos_to_source.append(info["dist_pos_to_source"])
dists_source_to_destination.append(info["dist_source_to_destination"])
if done:
break
# print(value0)
# print(value1)
# print(value2)
# print(value3)
# print(value4)
# print(transported_objects)
if debug_print:
print(f"Finite: \t {-value2 - value3} \t \t val{value1}")
""" Perform post processing """
agent_object_assignments = [
[agent_object_assignments[t][agent] for t in range(min(timesteps, len(agent_object_assignments)))
if agent_object_assignments[t][agent] != env.num_objects]
for agent in range(env.num_agents)
]
# Compute the mean_distance_travelled ignoring unnecessary moves
distances_travelled = []
for agent_idx in range(env.num_agents):
curr_pos = init_state.agents[agent_idx]
distance_travelled = 0
for object_idx in agent_object_assignments[agent_idx]:
distance_travelled += np.sqrt(np.sum((init_state.objects[object_idx][:2] - curr_pos) ** 2))
distance_travelled += np.sqrt(np.sum((init_state.objects[object_idx][2:4] - init_state.objects[object_idx][:2]) ** 2))
curr_pos = init_state.objects[object_idx][2:4]
distance_travelled += np.sqrt(np.sum((curr_pos - init_state.depot) ** 2))
distances_travelled.append(distance_travelled)
# mean_distance_travelled.append(np.mean(distances_travelled))
if debug_print:
print(f"NoDetour: \t {np.mean(distances_travelled)}")
# Add not yet transported objects randomly
for obj_idx in range(env.num_objects):
if not transported_objects[obj_idx]:
for agent_idx in range(env.num_agents):
if len(agent_object_assignments[agent_idx]) < max_tour_length:
agent_object_assignments[agent_idx].append(obj_idx)
break
# Compute the final mean_distance_travelled
distances_travelled = []
for agent_idx in range(env.num_agents):
curr_pos = init_state.agents[agent_idx]
distance_travelled = 0
for object_idx in agent_object_assignments[agent_idx]:
distance_travelled += np.sqrt(np.sum((init_state.objects[object_idx][:2] - curr_pos) ** 2))
distance_travelled += np.sqrt(np.sum((init_state.objects[object_idx][2:4] - init_state.objects[object_idx][:2]) ** 2))
curr_pos = init_state.objects[object_idx][2:4]
distance_travelled += np.sqrt(np.sum((curr_pos - init_state.depot) ** 2))
distances_travelled.append(distance_travelled)
mean_distance_travelled.append(np.mean(distances_travelled))
if debug_print:
print(f"Finally: \t {np.mean(distances_travelled)}")
print("...")
end_time = time.time()
mean_distance_travelled_Ns.append(mean_distance_travelled)
print(f"{dataset} k{k} N{N} \n"
f"Obj {np.mean(mean_distance_travelled):.5f} "
f"CI {2 * np.std(mean_distance_travelled) / np.sqrt(len(mean_distance_travelled)):.5f} "
f"trials {len(mean_distance_travelled)} \n"
f"time_taken {end_time - start_time:.5f}\n")
# std_returns = 2 * np.std(mean_distance_travelled_Ns, axis=1) / np.sqrt(len(mean_distance_travelled_Ns[0]))
# mean_returns = np.mean(mean_distance_travelled_Ns, axis=1)
if __name__ == "__main__":
config = {
"LR": 3e-5,
# "NUM_ENVS": 512,
"NUM_ENVS": 2,
"NUM_STEPS": 64,
"TOTAL_TIMESTEPS": 2e7,
"UPDATE_EPOCHS": 4,
"NUM_MINIBATCHES": 8,
"GAMMA": 0.99,
"GAE_LAMBDA": 0.95,
"CLIP_EPS": 0.2,
"ENT_COEF": 0.0,
"VF_COEF": 0.5,
"MAX_GRAD_NORM": 0.5,
"ACTIVATION": "tanh",
"ENV_NAME": "MFVRP-v1",
"ANNEAL_LR": True,
"NORMALIZE_ENV": True,
"DEBUG": True,
"ENV_CONFIG": 999,
}
rng = jax.random.PRNGKey(999)
load_and_plot(config, rng)