-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgeneralized_iter.py
More file actions
50 lines (39 loc) · 1.42 KB
/
generalized_iter.py
File metadata and controls
50 lines (39 loc) · 1.42 KB
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
import numpy as np
from snake import SnakeEnv, TableAgent, eval_game
from value_iter import PolicyIterationWithTimer, ValueIteration, timer
# general iteration
class GeneralizedPolicyIteration(object):
def __init__(self):
self.pi_algo = PolicyIterationWithTimer()
self.vi_algo = ValueIteration()
def generalized_policy_iteration(self, agent):
self.vi_algo.value_iteration(agent, 10)
self.pi_algo.policy_iteration(agent, 1)
def policy_iteration_demo():
np.random.seed(0)
env = SnakeEnv(10, [3, 6])
agent = TableAgent(env)
pi_algo = PolicyIterationWithTimer()
with timer('Timer PolicyIter'):
pi_algo.policy_iteration(agent)
print('return_pi={}'.format(eval_game(env, agent)))
def value_iteration_demo():
np.random.seed(0)
env = SnakeEnv(10, [3, 6])
agent = TableAgent(env)
pi_algo = ValueIteration()
with timer('Timer ValueIter'):
pi_algo.value_iteration(agent)
print('return_pi={}'.format(eval_game(env, agent)))
def generalized_iteration_demo():
np.random.seed(0)
env = SnakeEnv(10, [3, 6])
agent = TableAgent(env)
pi_algo = GeneralizedPolicyIteration()
with timer('Timer GeneralizedIter'):
pi_algo.generalized_policy_iteration(agent)
print('return_pi={}'.format(eval_game(env, agent)))
if __name__ == '__main__':
policy_iteration_demo()
value_iteration_demo()
generalized_iteration_demo()