forked from Joachm/evolving_hebbian_learning_rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollout.py
57 lines (41 loc) · 1.32 KB
/
rollout.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
'''
Adapted from E. Najarro
https://github.com/enajx/HebbianMetaLearning
'''
import gym
import pybullet
from hebbian_weights_update import *
from policies import MLP_heb
import torch
import torch.nn as nn
import matplotlib.pyplot
def weights_init(m):
if isinstance(m, torch.nn.Linear):
torch.nn.init.uniform_(m.weight.data, -0.1, 0.1)
def fitness_function(coeffs, inds):
env = gym.make('AntBulletEnv-v0')
with torch.no_grad():
inp_size = env.observation_space.shape[0]
action_size = env.action_space.shape[0]
NNet = MLP_heb(inp_size, action_size)
NNet.apply(weights_init)
w1, w2, w3 = list(NNet.parameters())
w1 = w1.detach().numpy()
w2 = w2.detach().numpy()
w3 = w3.detach().numpy()
obs = env.reset()
done = False
total_reward = 0
while True:
o0,o1,o2, action = NNet(obs)
obs, reward, done, info = env.step(action)
o0 = o0.numpy()
o1 = o1.numpy()
o2 = o2.numpy()
action = action.numpy()
total_reward += reward
if done:
break
w1, w2, w3 = hebbian_update(coeffs, w1, w2, w3, o0, o1, o2, action, inds)
env.close()
return total_reward