forked from asmith26/h-DQN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
131 lines (114 loc) · 4.24 KB
/
run.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
import numpy as np
import matplotlib.pyplot as plt
from collections import namedtuple
from envs.mdp import StochasticMDP
from agent.hDQN import hDQN
plt.style.use('ggplot')
def one_hot(state):
vector = np.zeros(6)
vector[state] = 1.0
return np.expand_dims(vector, axis=0)
def main():
ActorExperience = namedtuple('ActorExperience', ['state', 'goal', 'action', 'reward', 'next_state', 'done'])
MetaExperience = namedtuple('MetaExperience', ['state', 'goal', 'reward', 'next_state', 'done'])
env = StochasticMDP()
agent = hDQN()
n_states = 6
n_episodes = 12000
episode_div_factor = 1000
visits = np.zeros((n_episodes // episode_div_factor, n_states))
anneal_factor = 0.9 / n_episodes
print('Annealing factor: ' + str(anneal_factor))
for episode in range(n_episodes):
episode_thousand = episode // episode_div_factor
print('\n\n### EPISODE ' + str(episode) + '###')
state = env.reset()
visits[episode_thousand][state] += 1
done = False
while not done:
goal = agent.select_goal(one_hot(state))
agent.goal_selected[goal] += 1
print('\nNew Goal: ' + str(goal) + '\nState-Actions: ')
total_external_reward = 0
goal_reached = False
while not done and not goal_reached:
action = agent.select_move(one_hot(state), one_hot(goal), goal)
print(str((state,action)) + '; ')
next_state, external_reward, done = env.step(action)
visits[episode_thousand][next_state] += 1
intrinsic_reward = agent.criticize(goal, next_state)
goal_reached = next_state == goal
if goal_reached:
agent.goal_success[goal] += 1
print('Goal reached!! ')
if next_state == 5:
print('S5 reached!! ')
exp = ActorExperience(one_hot(state), one_hot(goal), action, intrinsic_reward, one_hot(next_state), done)
agent.store(exp, meta=False)
agent.update(meta=False)
agent.update(meta=True)
total_external_reward += external_reward
state = next_state
exp = MetaExperience(one_hot(state), one_hot(goal), total_external_reward, one_hot(next_state), done)
agent.store(exp, meta=True)
#Annealing
agent.meta_epsilon -= anneal_factor
avg_success_rate = agent.goal_success[goal] / agent.goal_selected[goal]
if(avg_success_rate == 0 or avg_success_rate == 1):
agent.actor_epsilon -= anneal_factor
else:
agent.actor_epsilon = 1- avg_success_rate
if(agent.actor_epsilon < 0.1):
agent.actor_epsilon = 0.1
print('meta_epsilon: ' + str(agent.meta_epsilon))
print('actor_epsilon ' + str(goal) + ': ' + str(agent.actor_epsilon))
if (episode % 100 == 99):
print('')
print(str(visits/1000) + '')
eps = list(range(1,13))
plt.subplot(2, 3, 1)
plt.plot(eps, visits[:,0]/1000)
plt.xlabel('Episodes (*1000)')
plt.ylim(-0.01, 2.0)
plt.xlim(1, 12)
plt.title('S1')
plt.grid(True)
plt.subplot(2, 3, 2)
plt.plot(eps, visits[:,1]/1000)
plt.xlabel('Episodes (*1000)')
plt.ylim(-0.01, 2.0)
plt.xlim(1, 12)
plt.title('S2')
plt.grid(True)
plt.subplot(2, 3, 3)
plt.plot(eps, visits[:,2]/1000)
plt.xlabel('Episodes (*1000)')
plt.ylim(-0.01, 2.0)
plt.xlim(1, 12)
plt.title('S3')
plt.grid(True)
plt.subplot(2, 3, 4)
plt.plot(eps, visits[:,3]/1000)
plt.xlabel('Episodes (*1000)')
plt.ylim(-0.01, 2.0)
plt.xlim(1, 12)
plt.title('S4')
plt.grid(True)
plt.subplot(2, 3, 5)
plt.plot(eps, visits[:,4]/1000)
plt.xlabel('Episodes (*1000)')
plt.ylim(-0.01, 2.0)
plt.xlim(1, 12)
plt.title('S5')
plt.grid(True)
plt.subplot(2, 3, 6)
plt.plot(eps, visits[:,5]/1000)
plt.xlabel('Episodes (*1000)')
plt.ylim(-0.01, 2.0)
plt.xlim(1, 12)
plt.title('S6')
plt.grid(True)
plt.savefig('first_run.png')
plt.show()
if __name__ == '__main__':
main()