-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
04_cheetah_ga.py
executable file
·153 lines (128 loc) · 4.58 KB
/
04_cheetah_ga.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
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
#!/usr/bin/env python3
import sys
import gym
import roboschool
import collections
import copy
import time
import numpy as np
import torch
import torch.nn as nn
import torch.multiprocessing as mp
from tensorboardX import SummaryWriter
NOISE_STD = 0.01
POPULATION_SIZE = 2000
PARENTS_COUNT = 10
WORKERS_COUNT = 6
SEEDS_PER_WORKER = POPULATION_SIZE // WORKERS_COUNT
MAX_SEED = 2**32 - 1
class Net(nn.Module):
def __init__(self, obs_size, act_size, hid_size=64):
super(Net, self).__init__()
self.mu = nn.Sequential(
nn.Linear(obs_size, hid_size),
nn.Tanh(),
nn.Linear(hid_size, hid_size),
nn.Tanh(),
nn.Linear(hid_size, act_size),
nn.Tanh(),
)
def forward(self, x):
return self.mu(x)
def evaluate(env, net):
obs = env.reset()
reward = 0.0
steps = 0
while True:
obs_v = torch.FloatTensor([obs])
action_v = net(obs_v)
obs, r, done, _ = env.step(action_v.data.numpy()[0])
reward += r
steps += 1
if done:
break
return reward, steps
def mutate_net(net, seed, copy_net=True):
new_net = copy.deepcopy(net) if copy_net else net
np.random.seed(seed)
for p in new_net.parameters():
noise_t = torch.tensor(np.random.normal(size=p.data.size()).astype(np.float32))
p.data += NOISE_STD * noise_t
return new_net
def build_net(env, seeds):
torch.manual_seed(seeds[0])
net = Net(env.observation_space.shape[0], env.action_space.shape[0])
for seed in seeds[1:]:
net = mutate_net(net, seed, copy_net=False)
return net
OutputItem = collections.namedtuple('OutputItem', field_names=['seeds', 'reward', 'steps'])
def worker_func(input_queue, output_queue):
env = gym.make("RoboschoolHalfCheetah-v1")
cache = {}
while True:
parents = input_queue.get()
if parents is None:
break
new_cache = {}
for net_seeds in parents:
if len(net_seeds) > 1:
net = cache.get(net_seeds[:-1])
if net is not None:
net = mutate_net(net, net_seeds[-1])
else:
net = build_net(env, net_seeds)
else:
net = build_net(env, net_seeds)
new_cache[net_seeds] = net
reward, steps = evaluate(env, net)
output_queue.put(OutputItem(seeds=net_seeds, reward=reward, steps=steps))
cache = new_cache
if __name__ == "__main__":
mp.set_start_method('spawn')
writer = SummaryWriter(comment="-cheetah-ga")
input_queues = []
output_queue = mp.Queue(maxsize=WORKERS_COUNT)
workers = []
for _ in range(WORKERS_COUNT):
input_queue = mp.Queue(maxsize=1)
input_queues.append(input_queue)
w = mp.Process(target=worker_func, args=(input_queue, output_queue))
w.start()
seeds = [(np.random.randint(MAX_SEED),) for _ in range(SEEDS_PER_WORKER)]
input_queue.put(seeds)
gen_idx = 0
elite = None
while True:
t_start = time.time()
batch_steps = 0
population = []
while len(population) < SEEDS_PER_WORKER * WORKERS_COUNT:
out_item = output_queue.get()
population.append((out_item.seeds, out_item.reward))
batch_steps += out_item.steps
if elite is not None:
population.append(elite)
population.sort(key=lambda p: p[1], reverse=True)
rewards = [p[1] for p in population[:PARENTS_COUNT]]
reward_mean = np.mean(rewards)
reward_max = np.max(rewards)
reward_std = np.std(rewards)
writer.add_scalar("reward_mean", reward_mean, gen_idx)
writer.add_scalar("reward_std", reward_std, gen_idx)
writer.add_scalar("reward_max", reward_max, gen_idx)
writer.add_scalar("batch_steps", batch_steps, gen_idx)
writer.add_scalar("gen_seconds", time.time() - t_start, gen_idx)
speed = batch_steps / (time.time() - t_start)
writer.add_scalar("speed", speed, gen_idx)
print("%d: reward_mean=%.2f, reward_max=%.2f, reward_std=%.2f, speed=%.2f f/s" % (
gen_idx, reward_mean, reward_max, reward_std, speed))
elite = population[0]
for worker_queue in input_queues:
seeds = []
for _ in range(SEEDS_PER_WORKER):
parent = np.random.randint(PARENTS_COUNT)
next_seed = np.random.randint(MAX_SEED)
seeds.append(tuple(list(population[parent][0]) + [next_seed]))
worker_queue.put(seeds)
gen_idx += 1
pass