This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies.py
177 lines (158 loc) · 5.34 KB
/
policies.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
from qing_env import MyGridEnv
from twolayernet import PolicyNet
"""
包含策略的定义
"""
class Policy:
"""
策略的基类
"""
def __init__(self, env: MyGridEnv):
"""
初始化策略
:param num_states: 状态数
:param num_actions: 动作数
"""
raise NotImplementedError
def get_probs(self, state, action):
"""
获取在给定状态下,所有动作的概率
:param state: 给定的状态
:return: 所有动作的概率
"""
raise NotImplementedError
def sample_action(self, state):
"""
根据给定的状态,按照策略选择一个动作
:param state: 给定的状态
:return: 选择的动作
"""
raise NotImplementedError
def showpolicy(self):
raise NotImplementedError
def play(self):
raise NotImplementedError
class TabularPolicy(Policy):
def showpolicy(self):
"""
用小方格的方式输出策略,每个小方格表示一个状态,如果是起始点则用'S'表示,如果是终止点则用'T'表示,障碍物用'X'表示,其他用箭头表示动作
,如果不动就用'*'表示。
"""
for i in range(0, self.env.size):
print('-' * (5 + 4 * (self.env.size - 1)))
out = '| '
for j in range(0, self.env.size):
token = ""
# 选出概率最大的
possibilities = self.probs[i][j]
action = np.argmax(possibilities)
if action == 0:
token = '↑'
elif action == 1:
token = '↓'
elif action == 2:
token = '←'
elif action == 3:
token = '→'
else:
token = '*'
if (i, j) in self.env.barriers:
token = '■'
# elif (i,j) == self.env.start:
# token = 'S'
elif (i, j) == self.env.target:
token = 'T'
out += token + ' | '
print(out)
print('-' * (5 + 4 * (self.env.size - 1)))
def play(self):
length = 0
total_reward = 0
while True:
action = self.sample_action(self.env.state)
state, reward, done, _ = self.env.step(action)
total_reward += reward
match action:
case 0:
arrow = '↑'
case 1:
arrow = '↓'
case 2:
arrow = '←'
case 3:
arrow = '→'
case _:
arrow = '*'
print(state, reward, done, arrow)
length += 1
if done:
break
print("length:", length)
print("total reward:", total_reward)
def sample_action(self, state):
"""
根据给定的状态,按照策略选择一个动作
:param state: 给定的状态
:return: 选择的动作
"""
return np.random.choice(self.env.action_space.n, p=self.get_probs(state))
def __init__(self, env: MyGridEnv):
"""
初始化策略
:param num_states: 状态数
:param num_actions: 动作数
"""
self.env = env
self.probs = np.zeros((env.size, env.size, env.action_space.n))
for i in range(env.size):
for j in range(env.size):
self.probs[i, j] = np.full(env.action_space.n, 1.0 / env.action_space.n)
def get_probs(self, state, action=None):
"""
获取在给定状态下,所有动作的概率
:param state: 给定的状态
:return: 所有动作的概率
"""
if action is not None:
return self.probs[state[0], state[1], action]
return self.probs[state[0], state[1]]
class FunctionPolicy(Policy):
def __init__(self, env: MyGridEnv):
super().__init__(env)
self.env = env
self.net = PolicyNet(2, 100, 5)
def get_probs(self, state, action=None):
x, y = state
if action is not None:
return self.net.predict(*state)[action]
else:
return self.net.predict((x, y))
def showpolicy(self):
for i in range(0, self.env.size):
print('-' * (5 + 4 * (self.env.size - 1)))
out = '| '
for j in range(0, self.env.size):
token = ""
# 选出概率最大的
possibilities = self.get_probs((i, j))
action = np.argmax(possibilities)
if action == 0:
token = '↑'
elif action == 1:
token = '↓'
elif action == 2:
token = '←'
elif action == 3:
token = '→'
else:
token = '*'
if (i, j) in self.env.barriers:
token = '■'
# elif (i,j) == self.env.start:
# token = 'S'
elif (i, j) == self.env.target:
token = 'T'
out += token + ' | '
print(out)
print('-' * (5 + 4 * (self.env.size - 1)))