-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.py
executable file
·130 lines (98 loc) · 3.11 KB
/
explorer.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
import random
import numpy as np
import matplotlib.pyplot as plt
# Alumno: Alejandro Curbelo Fontelos
# Environment size
width = 5
height = 16
# Actions
num_actions = 4
actions_list = {"UP": 0,
"RIGHT": 1,
"DOWN": 2,
"LEFT": 3
}
actions_vectors = {"UP": (-1, 0),
"RIGHT": (0, 1),
"DOWN": (1, 0),
"LEFT": (0, -1)
}
# Discount factor
discount = 0.8
Q = np.zeros((height * width, num_actions)) # Q matrix
Rewards = np.zeros(height * width) # Reward matrix, it is stored in one dimension
def getState(y, x):
return y * width + x
def getStateCoord(state):
return int(state / width), int(state % width)
def getActions(state):
y, x = getStateCoord(state)
actions = []
if x < width - 1:
actions.append("RIGHT")
if x > 0:
actions.append("LEFT")
if y < height - 1:
actions.append("DOWN")
if y > 0:
actions.append("UP")
return actions
def getRndAction(state):
return random.choice(getActions(state))
def getRndState():
return random.randint(0, height * width - 1)
Rewards[4 * width + 3] = -10000
Rewards[4 * width + 2] = -10000
Rewards[4 * width + 1] = -10000
Rewards[4 * width + 0] = -10000
Rewards[9 * width + 4] = -10000
Rewards[9 * width + 3] = -10000
Rewards[9 * width + 2] = -10000
Rewards[9 * width + 1] = -10000
Rewards[3 * width + 3] = 100
final_state = getState(3, 3)
print np.reshape(Rewards, (height, width))
def qlearning(s1, a, s2):
Q[s1][a] = Rewards[s2] + discount * max(Q[s2])
return
# for n in xrange(100):
nActions=0
# Episodes
for i in xrange(100):
state = getRndState()
while state != final_state:
action = getRndAction(state)
nActions+=1
y = getStateCoord(state)[0] + actions_vectors[action][0]
x = getStateCoord(state)[1] + actions_vectors[action][1]
new_state = getState(y, x)
qlearning(state, actions_list[action], new_state)
state = new_state
print Q
print "Numero promedio de acciones para alcanzar el objetivo: ", nActions/100
# print n+1, " ", nActions/100
# Q = np.zeros((height * width, num_actions))
# Q matrix plot
"""
s = 0
ax = plt.axes()
ax.axis([-1, width + 1, -1, height + 1])
for j in xrange(height):
plt.plot([0, width], [j, j], 'b')
for i in xrange(width):
plt.plot([i, i], [0, height], 'b')
direction = np.argmax(Q[s])
if s != final_state:
if direction == 0:
ax.arrow(i + 0.5, 0.75 + j, 0, -0.35, head_width=0.08, head_length=0.08, fc='k', ec='k')
if direction == 1:
ax.arrow(0.25 + i, j + 0.5, 0.35, 0., head_width=0.08, head_length=0.08, fc='k', ec='k')
if direction == 2:
ax.arrow(i + 0.5, 0.25 + j, 0, 0.35, head_width=0.08, head_length=0.08, fc='k', ec='k')
if direction == 3:
ax.arrow(0.75 + i, j + 0.5, -0.35, 0., head_width=0.08, head_length=0.08, fc='k', ec='k')
s += 1
plt.plot([i+1, i+1], [0, height], 'b')
plt.plot([0, width], [j+1, j+1], 'b')
plt.show()
"""