-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimple-agent-robot.py
More file actions
95 lines (80 loc) · 2.14 KB
/
simple-agent-robot.py
File metadata and controls
95 lines (80 loc) · 2.14 KB
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
import matplotlib.pyplot as plt
import random
# 0 -> clean
# 1 -> wall
# 2 -> dirt
matrix = [
[1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1]
]
# Actions Matrix -> represents the action for each position
# Actions = up (0), down (1), left (2), right (3), clean(4), end (5)
actionsMatrix = [
[9, 9, 9, 9, 9, 9],
[9, 1, 3, 1, 5, 9],
[9, 1, 0, 1, 0, 9],
[9, 1, 0, 1, 0, 9],
[9, 3, 0, 3, 0, 9],
[9, 9, 9, 9, 9, 9]
]
# The robot always starts at matrix[1][1]
currLine = 1
currCol = 1
def renderMatrix(matrix):
plt.imshow(matrix, 'pink')
plt.show(block=False)
plt.plot(currCol, currLine, '*r', 'LineWidth', 5)
plt.pause(0.5)
plt.clf()
def createWorld(m):
for mI in range(1, 5):
for aI in range(1, 5):
number = random.randint(0, 3)
m[mI][aI] = 2 if number == 1 else 0
renderMatrix(matrix)
def findNextAction(x, y):
return actionsMatrix[x][y]
# decides which action will be done
# Actions = up (0), down (1), left (2), right (3), clean(4)
def simpleAgentRobot(x, y):
if (matrix[x][y] == 2): # if it's dirty, return the clean action
return 4
return findNextAction(x, y)
def main():
createWorld(matrix)
global currLine
global currCol
while True:
action = simpleAgentRobot(currLine, currCol)
if (action == 0): # go up
print("up")
currLine = currLine - 1 # remove 1 line
renderMatrix(matrix)
elif (action == 1): # go down
print("down")
currLine = currLine + 1
renderMatrix(matrix)
elif (action == 2): # go left
print("left")
currCol = currCol - 1
renderMatrix(matrix)
elif (action == 3): # go right
print("right")
currCol = currCol + 1
renderMatrix(matrix)
elif (action == 4): # clean
print("clean")
matrix[currLine][currCol] = 0
renderMatrix(matrix)
else:
print("end")
break
for i in range(1, len(matrix) - 1):
for j in range(i, len(matrix[i]) - 1):
print(matrix[i][j]) # verifies if the matrix is empty
if __name__ == "__main__":
main()