-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskeleton.py
197 lines (188 loc) · 5.45 KB
/
skeleton.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 12 10:52:56 2018
@author: jess
"""
from enum import Enum
import numpy as np
from random import randrange
import string
import random
class Direction(Enum):
up=0
down=1
left=2
right=3
stepsTaken=0
nbMaxSteps=500
Q = {}
gamma=0.95
strat=1
epsilon=0.99
maze=[]
penalty=0
#values of each movement
step=-1
stepTrap=-20
stepExit=500
stepWall=-100
#current position of the robot
position=[0, 0]
#funciton that checks if a certain place in the Q matrix is empty, returns 1 if it is
def currentQEmpty():
global Q
global position
moves=[]
if (position[0]!=0):
moves.append(Direction.left)
if (position[0]!=cols-1):
moves.append(Direction.right)
if (position[1]!=0):
moves.append(Direction.down)
if (position[1]!=rows-1):
moves.append(Direction.up)
for d in moves:
if (Q.get((position[0],position[1],d),'A')=='A'):
return 1
return 0
#intialise the Q matrix
cols=10
rows=10
values=np.zeros((rows,cols))
for x in range(rows):
for y in range(cols):
for dir in Direction:
Q[(x, y, dir)] = 0
#fills the Q matrix (replaces empty values only)
def QFill(moves):
global maze
global position
global Q
global step
global stepTrap
global stepWall
global stepExit
global gamma
for d in moves:
reward=0
newpos=position
if d==Direction.up:
newpos=[position[0], position[1]+1]
if d==Direction.down:
newpos=[position[0], position[1]-1]
if d==Direction.left:
newpos=[position[0]-1, position[1]]
if d==Direction.right:
newpos=[position[0]+1, position[1]]
reward=reward+values[newpos[0],newpos[1]]
if(Q.get((position[0],position[1],d),0)==0):
Q[position[0],position[1],d]=reward
#Qmove: decides which move to make depending on current Q values
#this is where the issue is!
def Qmove(moves):
global position
global Q
global step
global stepTrap
global stepWall
global stepExit
global gamma
bestd=0
newd=moves[random.randint(0,len(moves)-1)]
for d in moves:
newpos=position
if d==Direction.up:
newpos=[position[0], position[1]+1]
if d==Direction.down:
newpos=[position[0], position[1]-1]
if d==Direction.left:
newpos=[position[0]-1, position[1]]
if d==Direction.right:
newpos=[position[0]+1, position[1]]
#update value to best value of new position
if Q.get((newpos[0],newpos[1],d),0)>=Q.get((newpos[0],newpos[1],bestd),0):
bestd=d
Q[position[0],position[1],d]=Q.get((position[0],position[1],d),0)+ (values[newpos[0]][newpos[1]] + gamma * Q.get((newpos[0],newpos[1],bestd),1) - Q.get((position[0],position[1],d),0))
#update arrow
if Q.get((position[0],position[1],d),0)>Q.get((position[0],position[1],newd),0):
newd=d
return newd
#create maze
ch=['0', '1', '3']
for i in range(cols):
maze.append([0]*(cols))
for j in range(cols):
random_index = randrange(0,len(ch))
maze[i][j]=ch[random_index]
if i==cols-1 and j==cols-1:
maze[i][j]='5'
if i==0 and j==0:
maze[i][j]='0'
if(maze[i][j]=="1"):
values[i][j]=step
elif(maze[i][j]=="0"):
values[i][j]=stepWall
elif(maze[i][j]=="3"):
values[i][j]=stepTrap
else:
values[i][j]=stepExit
#move
while(stepsTaken<nbMaxSteps):
moves=[]
#if he finishes he starts over
if(position[0]==rows-1 and position[1]==cols-1):
position[0]=0
position[1]=0
penalty=0
#identify the moves he can legally make
if (position[0]!=0):
moves.append(Direction.left)
if (position[0]!=cols-1):
moves.append(Direction.right)
d=moves[0]
if (position[1]!=0):
moves.append(Direction.down)
if (position[1]!=rows-1):
moves.append(Direction.up)
dest=[]
#choose epsilon value
rand=random.uniform(0, 1)
if(rand<epsilon**stepsTaken):
strat=1
#explore
else:
strat=2
#exploit
#print(epsilon**stepsTaken)
if(currentQEmpty() or strat==1):
QFill(moves)
d=moves[random.randint(0,len(moves)-1)]#how and why he moves
print('dumb')
else:
d=Qmove(moves)
print('smart')
if(d==Direction.left):
dest.append(position[0]-1) #x decreases by 1 place
dest.append(position[1]) #y does not change
if(d==Direction.right):
dest.append(position[0]+1) #x increases by 1 place
dest.append(position[1]) #y does not change
if(d==Direction.up):
dest.append(position[0]) #x does not change&&
dest.append(position[1]+1) #y increases by 1
if(d==Direction.down):
dest.append(position[0]) #x does not change
dest.append(position[1]-1) #y decreases by 1
#penalty is calculated
penalty=penalty+values[dest[0]][dest[1]]
if(maze[dest[0]][dest[1]]!='0'): #not a wall
position=dest
stepsTaken=stepsTaken+1
#show Q matrix
x=position[0]
y=position[1]
print("x:",x," y:",y)
print(" UP:%s" % Q.get((x,y, Direction.up)))
print(" DOWN:%s" % Q.get((x,y, Direction.down)))
print(" LEFT:%s" % Q.get((x,y, Direction.left)))
print(" RIGHT:%s\n" % Q.get((x,y, Direction.right)))