-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent_simulator.py
76 lines (68 loc) · 2.64 KB
/
agent_simulator.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
import gym
import deeprl_hw1.rl as rl
def run_policy(env,policy,gamma):
total_reward = 0
step_num = 0
state = env.reset()
is_terminal = False
while not is_terminal:
nextstate, reward, is_terminal, debug_info = env.step(policy[state])
total_reward += pow(gamma,step_num) * reward
step_num +=1
state = nextstate
return total_reward
def stochastic_run(env, gamma, tol = 1e-3):
total_reward = 0
max_iterations = int(1e3)
policy, iteration, value_func = rl.value_iteration_sync(env, gamma, max_iterations, tol)
for i in range(10000):
#policy, iteration, value_func= rl.value_iteration_sync(env, gamma, max_iterations,tol)
reward = run_policy(env, policy, gamma)
total_reward += reward
total_reward = total_reward / 10000
return total_reward
# env = gym.make('Deterministic-4x4-FrozenLake-v0')
# gamma = 0.9
# initial_state = env.reset()
# policy, iteration, value_func = rl.value_iteration_sync(env, gamma)
# total_reward = run_policy(env,policy,gamma)
# print("computed value: " + str(total_reward))
# print("simulated value: " + str(value_func[initial_state]))
# print('\n')
#
# env = gym.make('Deterministic-8x8-FrozenLake-v0')
# gamma = 0.9
# initial_state = env.reset()
# policy, iteration, value_func = rl.value_iteration_sync(env, gamma)
# total_reward = run_policy(env,policy,gamma)
# print("computed value: " + str(total_reward))
# print("simulated value: " + str(value_func[initial_state]))
# print('\n')
env = gym.make('Stochastic-4x4-FrozenLake-v0')
gamma = 0.9
max = 1e3
initial_state = env.reset()
policy, iteration, value_func= rl.value_iteration_sync(env, gamma)
reward = stochastic_run(env, gamma)
tol = 1e-10
policy_tol, iteration_tol, value_func_tol= rl.value_iteration_sync(env, gamma,max,tol)
reward_tol = stochastic_run(env, gamma, tol)
print("computed value: " + str(value_func[initial_state]))
print("computed value (tol = 1e-10): " + str(value_func_tol[initial_state]))
print("simulated value: " + str(reward_tol))
print("simulated value (tol = 1e-3): " + str(reward))
print('\n')
env = gym.make('Stochastic-8x8-FrozenLake-v0')
gamma = 0.9
max = 1e3
initial_state = env.reset()
policy, iteration, value_func= rl.value_iteration_sync(env, gamma)
reward = stochastic_run(env, gamma)
tol = 1e-10
policy_tol, iteration_tol, value_func_tol= rl.value_iteration_sync(env, gamma,max,tol)
reward_tol = stochastic_run(env, gamma, tol)
print("computed value: " + str(value_func[initial_state]))
print("computed value (tol = 1e-10): " + str(value_func_tol[initial_state]))
print("simulated value: " + str(reward_tol))
print("simulated value (tol = 1e-3): " + str(reward))
print('\n')