Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

rlboard.py 7.1 KB

You have to be logged in to leave a comment. Sign In
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  1. # Maze simulation environment for Reinforcement Learning tutorial
  2. # by Dmitry Soshnikov
  3. # http://soshnikov.com
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import cv2
  7. import random
  8. import math
  9. def clip(min,max,x):
  10. if x<min:
  11. return min
  12. if x>max:
  13. return max
  14. return x
  15. def imload(fname,size):
  16. img = cv2.imread(fname)
  17. img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
  18. img = cv2.resize(img,(size,size),interpolation=cv2.INTER_LANCZOS4)
  19. img = img / np.max(img)
  20. return img
  21. def draw_line(dx,dy,size=50):
  22. p=np.ones((size-2,size-2,3))
  23. if dx==0:
  24. dx=0.001
  25. m = (size-2)//2
  26. l = math.sqrt(dx*dx+dy*dy)*(size-4)/2
  27. a = math.atan(dy/dx)
  28. cv2.line(p,(int(m-l*math.cos(a)),int(m-l*math.sin(a))),(int(m+l*math.cos(a)),int(m+l*math.sin(a))),(0,0,0),1)
  29. s = -1 if dx<0 else 1
  30. cv2.circle(p,(int(m+s*l*math.cos(a)),int(m+s*l*math.sin(a))),3,0)
  31. return p
  32. def probs(v):
  33. v = v-v.min()
  34. if (v.sum()>0):
  35. v = v/v.sum()
  36. return v
  37. class Board:
  38. class Cell:
  39. empty = 0
  40. water = 1
  41. wolf = 2
  42. tree = 3
  43. apple = 4
  44. def __init__(self,width,height,size=50):
  45. self.width = width
  46. self.height = height
  47. self.size = size+2
  48. self.matrix = np.zeros((width,height))
  49. self.grid_color = (0.6,0.6,0.6)
  50. self.background_color = (1.0,1.0,1.0)
  51. self.grid_thickness = 1
  52. self.grid_line_type = cv2.LINE_AA
  53. self.pics = {
  54. "wolf" : imload('images/wolf.png',size-4),
  55. "apple" : imload('images/apple.png',size-4),
  56. "human" : imload('images/human.png',size-4)
  57. }
  58. self.human = (0,0)
  59. self.frame_no = 0
  60. def randomize(self,water_size=5, num_water=3, num_wolves=1, num_trees=5, num_apples=3,seed=None):
  61. if seed:
  62. random.seed(seed)
  63. for _ in range(num_water):
  64. x = random.randint(0,self.width-1)
  65. y = random.randint(0,self.height-1)
  66. for _ in range(water_size):
  67. self.matrix[x,y] = Board.Cell.water
  68. x = clip(0,self.width-1,x+random.randint(-1,1))
  69. y = clip(0,self.height-1,y+random.randint(-1,1))
  70. for _ in range(num_trees):
  71. while True:
  72. x = random.randint(0,self.width-1)
  73. y = random.randint(0,self.height-1)
  74. if self.matrix[x,y]==Board.Cell.empty:
  75. self.matrix[x,y] = Board.Cell.tree # tree
  76. break
  77. for _ in range(num_wolves):
  78. while True:
  79. x = random.randint(0,self.width-1)
  80. y = random.randint(0,self.height-1)
  81. if self.matrix[x,y]==Board.Cell.empty:
  82. self.matrix[x,y] = Board.Cell.wolf # wolf
  83. break
  84. for _ in range(num_apples):
  85. while True:
  86. x = random.randint(0,self.width-1)
  87. y = random.randint(0,self.height-1)
  88. if self.matrix[x,y]==Board.Cell.empty:
  89. self.matrix[x,y] = Board.Cell.apple
  90. break
  91. def at(self,pos=None):
  92. if pos:
  93. return self.matrix[pos[0],pos[1]]
  94. else:
  95. return self.matrix[self.human[0],self.human[1]]
  96. def is_valid(self,pos):
  97. return pos[0]>=0 and pos[0]<self.width and pos[1]>=0 and pos[1] < self.height
  98. def move_pos(self, pos, dpos):
  99. return (pos[0] + dpos[0], pos[1] + dpos[1])
  100. def move(self,dpos,check_correctness=True):
  101. new_pos = self.move_pos(self.human,dpos)
  102. if self.is_valid(new_pos) or not check_correctness:
  103. self.human = new_pos
  104. def random_pos(self):
  105. x = random.randint(0,self.width-1)
  106. y = random.randint(0,self.height-1)
  107. return (x,y)
  108. def random_start(self):
  109. while True:
  110. pos = self.random_pos()
  111. if self.at(pos) == Board.Cell.empty:
  112. self.human = pos
  113. break
  114. def image(self,Q=None):
  115. img = np.zeros((self.height*self.size+1,self.width*self.size+1,3))
  116. img[:,:,:] = self.background_color
  117. # Draw water
  118. for x in range(self.width):
  119. for y in range(self.height):
  120. if (x,y) == self.human:
  121. ov = self.pics['human']
  122. img[self.size*y+2:self.size*y+ov.shape[0]+2,self.size*x+2:self.size*x+2+ov.shape[1],:] = np.minimum(ov,1.0)
  123. continue
  124. if self.matrix[x,y] == Board.Cell.water:
  125. img[self.size*y:self.size*(y+1),self.size*x:self.size*(x+1),:] = (0,0,1.0)
  126. if self.matrix[x,y] == Board.Cell.wolf:
  127. ov = self.pics['wolf']
  128. img[self.size*y+2:self.size*y+ov.shape[0]+2,self.size*x+2:self.size*x+2+ov.shape[1],:] = np.minimum(ov,1.0)
  129. if self.matrix[x,y] == Board.Cell.apple: # apple
  130. ov = self.pics['apple']
  131. img[self.size*y+2:self.size*y+ov.shape[0]+2,self.size*x+2:self.size*x+2+ov.shape[1],:] = np.minimum(ov,1.0)
  132. if self.matrix[x,y] == Board.Cell.tree: # tree
  133. img[self.size*y:self.size*(y+1),self.size*x:self.size*(x+1),:] = (0,1.0,0)
  134. if self.matrix[x,y] == Board.Cell.empty and Q is not None:
  135. p = probs(Q[x,y])
  136. dx,dy = 0,0
  137. for i,(ddx,ddy) in enumerate([(-1,0),(1,0),(0,-1),(0,1)]):
  138. dx += ddx*p[i]
  139. dy += ddy*p[i]
  140. l = draw_line(dx,dy,self.size)
  141. img[self.size*y+2:self.size*y+l.shape[0]+2,self.size*x+2:self.size*x+2+l.shape[1],:] = l
  142. # Draw grid
  143. for i in range(self.height+1):
  144. img[:,i*self.size] = 0.3
  145. #cv2.line(img,(0,i*self.size),(self.width*self.size,i*self.size), self.grid_color, self.grid_thickness,lineType=self.grid_line_type)
  146. for j in range(self.width+1):
  147. img[j*self.size,:] = 0.3
  148. #cv2.line(img,(j*self.size,0),(j*self.size,self.height*self.size), self.grid_color, self.grid_thickness,lineType=self.grid_line_type)
  149. return img
  150. def plot(self,Q=None):
  151. plt.figure(figsize=(11,6))
  152. plt.imshow(self.image(Q),interpolation='hanning')
  153. def saveimage(self,filename,Q=None):
  154. cv2.imwrite(filename,255*self.image(Q)[...,::-1])
  155. def walk(self,policy,save_to=None,start=None):
  156. n = 0
  157. if start:
  158. self.human = start
  159. else:
  160. self.random_start()
  161. while True:
  162. if save_to:
  163. self.saveimage(save_to.format(self.frame_no))
  164. self.frame_no+=1
  165. if self.at() == Board.Cell.apple:
  166. return n # success!
  167. if self.at() in [Board.Cell.wolf, Board.Cell.water]:
  168. return -1 # eaten by wolf or drowned
  169. while True:
  170. a = policy(self)
  171. new_pos = self.move_pos(self.human,a)
  172. if self.is_valid(new_pos) and self.at(new_pos)!=Board.Cell.water:
  173. self.move(a) # do the actual move
  174. break
  175. n+=1
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...