-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
74 lines (63 loc) · 2.96 KB
/
Copy pathTest.py
File metadata and controls
74 lines (63 loc) · 2.96 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
import numpy as np
from gym_lunar_rover.algorithms.DDDQL import InferenceDDDQNAgent
from gym_lunar_rover.algorithms.MAPPO import InferenceMAPPOAgent
from gym_lunar_rover.envs.Test_env import TestEnv
from gym_lunar_rover.envs.Utils import *
# Parámetros para la creación del entorno
n_agents = 3
grid_size = 12
vision_range = 3
observation_shape = vision_range*2+1
info_shape = 7
know_pos = False
test_env = TestEnv(n_agents, grid_size, vision_range, know_pos=know_pos, render_mode='human')
action_dim = test_env.action_space.nvec[0]
def test(algorithm, steps):
match algorithm:
case 'DDDQL':
model_filename = generate_filename(algorithm,'model_weights', steps, 'weights.h5')
if not check_file_exists(model_filename):
print("Faltan ficheros para el modelo que se quiere entrenar")
return
agent = InferenceDDDQNAgent(observation_shape, info_shape, action_dim, model_filename)
case 'MAPPO':
model_filename = generate_filename(algorithm,'actor_weights', steps, 'weights.h5')
if not check_file_exists(model_filename):
print("Faltan ficheros para el modelo que se quiere entrenar")
return
agent = InferenceMAPPOAgent(observation_shape, info_shape, action_dim, model_filename)
# Se prueba el modelo en distintos entornos fijados con semillas
# para igualar las comparaciones entre algoritmos y modelos
seeds = [1,2,4,5]
for i, seed in enumerate(seeds):
test_env.reset(seed)
dones = [False]*test_env.n_agents
num_steps = 0
while not all(dones):
for i, rover in enumerate(test_env.unwrapped.rovers):
# Si el Rover ha terminado saltamos al siguiente
if rover.done:
continue
available_actions = rover.get_movements()
observation, visits = rover.get_observation()[0:2]
# Normalizamos la observación en el rango 0-1
norm_observation = normalize_obs(observation)
# Normalizamos las visitas en el rango 0-1
norm_visits = normalize_visits(visits)
# Normalizamos las posiciones en el rango 0-1
info = normalize_pos(rover.position + rover.mine_pos + rover.blender_pos, grid_size)
info = np.append(info, int(rover.mined))
action = agent.act(norm_observation, norm_visits, info, available_actions)
step_act = rover.step(action)
dones[i] = step_act[3]
num_steps +=1
print(f"Terminado episodio {i+1} (seed {seed}) con una recompensa total de {test_env.total_reward} en {num_steps} pasos")
def main():
# Número de steps del modelo que queremos testear
model_steps = 3750000
# Algoritmo que queremos testear (DDDQL o MAPPO)
algorithm = 'DDDQL'
# algorithm = 'MAPPO'
test(algorithm, model_steps)
if __name__ == "__main__":
main()