-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlocked_rooms.py
114 lines (98 loc) · 3.01 KB
/
Blocked_rooms.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
#Importing modules
import random
import time
def Blocked_room_sequence(n):
#Setting the parameters
iterations=1000
lr=0.8
#n = int(input('\nEnter number of rooms: '))
#Initializing Q and reward matrix
Q = [[0 for x in range(n)] for y in range(n)]
R = [[-1 for x in range(n)] for y in range(n)]
source=input('\nEnter Starting Room: ')
destination = input('Enter Destination Room: ')
dest = ord(destination)-65
maintenance = input('Is any room under maintenance? (Y?N): ')
if maintenance == 'Y':
blocked_rooms = [x for x in input('\nEnter Blocked Rooms: ').strip()]
for x in blocked_rooms:
R[ord(x)-65][ord(x)-65]=-100
for i in range(n):
node = [x for x in input('Enter rooms connected to '+str(chr(i+65))+': ').strip()]
#print(node)
for col in node:
idx = ord(col)-65
if col==chr(dest+65):
R[i][idx]=100
elif col in blocked_rooms:
R[i][ord(col)-65]=-100
else:
R[i][idx]=0
else:
R[dest][dest] = 100
print('\n\n')
for i in range(n):
node = [x for x in input('Enter rooms connected to '+str(chr(i+65))+': ').strip()]
#print(node)
for col in node:
idx = ord(col)-65
if col==chr(dest+65):
R[i][idx]=100
else:
R[i][idx]=0
t1 = time.time()
#Training begins
for s in range(0,iterations):
starter=[]
for i in range(0,n):
starter.append(chr(i+65))
start=random.choice(starter)
k=ord(start)-65
randomizer_array=[]
for j in range(0,n):
if R[k][j]>-1:
randomizer_array.append(j)
next=random.choice(randomizer_array)
largest=[]
for x in range(0,n):
if R[next][x]>-1:
largest.append(Q[next][x])
p=max(largest)
Q[k][next]=R[k][next]+lr*p
k=next
for i in range(0, n):
for j in range(0, n):
Q[i][j]=int(Q[i][j])
print(time.time()-t1)
'''
print('\nTrained Q matrix for the map is: \n')
for i in range(0, n):
for j in range(0, n):
print(Q[i][j],end=' ')
print('\n'.strip())
'''
#Testing
#print('\nHow to get out: ',end='')
track=[]
seq = [source]
u=ord(source)-65
#print(source,end='')
#print('->',end='')
while(u!=dest):
for j in range(0, n):
if Q[u][j]>0:
track.append(Q[u][j])
t=max(track)
tx=[]
for y in range(0,n):
if Q[u][y]==t:
tx.append(y)
tind=random.choice(tx)
#print(chr(tind+65),end='')
seq.append(chr(tind+65))
u=tind
if u==dest:
break
return seq
n = int(input('\nEnter number of rooms: '))
print(Blocked_room_sequence(n))