-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_humanoid_standup.py
67 lines (48 loc) · 1.85 KB
/
best_humanoid_standup.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
import torch
from classes.NEAT import *
species_id = 0
run = 1
species = torch.load(f'runs/humanoid_standup/2023-12-08 21:18:08.795747/species_{run}.pt')
fitnesses = torch.load(f'runs/humanoid_standup/2023-12-08 21:18:08.795747/fitness_perspecies_{run}.pt')
import gymnasium as gym
import datetime
def lunar_fitness(genotype_and_env, inputs, targets):
#error = 0
genotype, env = genotype_and_env
network = NeuralNetwork(genotype)
fitness = 0
num_tries = 1
for _ in range(num_tries):
observation, info = env.reset()
terminated, truncated = False, False
while not terminated and not truncated:
input = {
0:torch.tensor([1.0]),# bias
}
for i in range(1,45):
input[i] = torch.tensor([observation[i-1]]).to(torch.float32)
actions = network.forward(input)
actions = torch.tensor(actions)
actions = 2*actions - 1
# normalize to -0.4, 0.4
actions = actions * 0.4
#actions = actions.tolist()
observation, reward, terminated, truncated, info = env.step(actions)
fitness += reward
fitness /= num_tries
env.close()
return fitness
genotypes = species[species_id].genotypes
best_genotype = genotypes[np.argmax(fitnesses[species_id])]
for species in fitnesses:
print(species, np.max(fitnesses[species]))
#best_genotype.print_genotype()
# env = gym.make("HumanoidStandup-v4", render_mode='human')
# env.reset()
# while True:
# fitness = lunar_fitness((best_genotype, env), None, None)
from gymnasium.wrappers import RecordVideo
env = RecordVideo(gym.make("HumanoidStandup-v4", render_mode='rgb_array') , './video')
# while True:
fitness = lunar_fitness((best_genotype, env), None, None)
env.close()