-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.py
72 lines (65 loc) · 1.96 KB
/
simulate.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
import numpy as np
step_counter = 0
def step_reward(env, obs):
envName = env.unwrapped.spec.id
if envName == 'CartPole-v1':
return 1 # reward staying alive (balancing)
elif envName == 'MountainCar-v0':
reward = -1 # punish not finishing
reward += np.abs(obs[1])*100 # reward high speeds
#reward += np.abs(obs[0])*10 # reward reaching goal area
# if obs[0] > -0.6 and obs[0] < -0.4: reward = -2 # punish staying in start area
return reward
elif envName == 'Pendulum-v0':
return 3*obs[1] # reward equal to height of pendulum sin(theta)
elif envName == 'Acrobot-v1':
pass
elif envName == 'gym_custom:CartPoleSwingUp-v0':
return 0.001*(4.8-np.abs(obs[0]))/4.8 - np.abs(obs[2])
def stop_condition(env, total_reward, done):
envName = env.unwrapped.spec.id
if envName == 'CartPole-v1':
return done
elif envName == 'MountainCar-v0':
return done
elif envName == 'Pendulum-v0':
global step_counter
step_counter += 1
if total_reward < -4000:
step_counter = 0
return True
elif step_counter > 200:
step_counter = 0
return True
return False
elif envName == 'Acrobot-v1':
pass
elif envName == 'gym_custom:CartPoleSwingUp-v0':
pass
'''SIMULATE'''
def render(env, agent):
obs = env.reset()
done = False
while not done:
env.render()
action = agent.get_action(obs)
obs, _, done, _ = env.step(action)
def simulate_single(env, agent):
accumilated_reward = 0.0
obs = env.reset()
done = False
while not done:
action = agent.get_action(obs)
obs, reward, done, info = env.step(action)
#accumilated_reward += reward
#accumilated_reward += 1
accumilated_reward += step_reward(env, obs)
done = stop_condition(env, accumilated_reward, done)
return accumilated_reward
def simulate_batch(env, agent, batch_weights):
rewards = []
for weights in batch_weights:
agent.set_weights(weights)
#for t in range(RUNS_PER_INDIVIDUAL):
rewards.append(simulate_single(env, agent))
return np.array(rewards)