How to use newaction method in tox

Best Python code snippet using tox_python

IDS.py

Source:IDS.py Github

copy

Full Screen

1import numpy as np2from collections import deque3import time4import copy56def numberOfRows(n):7 row = 18 for i in n:9 if(i == '\n'):10 row = row +111 return row1213def checkGoalState(map): #map: 2D array14 indexesOfP = np.where(np.array(map) == 'P')15 if(indexesOfP[0].size == 0 and indexesOfP[1].size == 0):16 return True17 return False18192021def expandNeighbors(currentState):22 iA, jA = np.where((np.array(currentState) >= 'A') & (np.array(currentState) != 'P') & (np.array(currentState) != 'AP') & (np.array(currentState) < 'AP'))23 if(iA.size != 0 and jA.size != 0):24 stateOfA = 'A'25 iA = int(iA)26 jA = int(jA)27 else:28 stateOfA = 'AP'29 iA, jA = np.where((np.array(currentState) >= 'AP') & (np.array(currentState) != 'P') & (np.array(currentState) != '#'))30 iA = int(iA)31 jA = int(jA)32 nodes = []33 upNeighbor = currentState[iA-1][jA]34 downNeighbor = currentState[iA + 1][jA]35 rightNeighbor = currentState[iA][jA + 1]36 leftNeighbor = currentState[iA][jA - 1]37 if (stateOfA == 'A'):38 if(len(currentState[iA][jA]) == 1):39 replaceA = ' '40 else:41 replaceA = currentState[iA][jA][1:len(currentState[iA][jA])]42 if(upNeighbor == 'P'):43 newState = copy.deepcopy(currentState)44 newState[iA][jA] = 'AP' + replaceA45 newAction = 'UP'46 nodes.append((newState, newAction))47 return nodes48 if(downNeighbor == 'P'):49 newState = copy.deepcopy(currentState)50 newState[iA][jA] = 'AP' + replaceA51 newAction = 'DOWN'52 nodes.append((newState, newAction))53 return nodes54 if(rightNeighbor == 'P'):55 newState = copy.deepcopy(currentState)56 newState[iA][jA] = 'AP' + replaceA57 newAction = 'RIGHT'58 nodes.append((newState, newAction))59 return nodes60 if(leftNeighbor == 'P'):61 newState = copy.deepcopy(currentState)62 newState[iA][jA] = 'AP' + replaceA63 newAction = 'LEFT'64 nodes.append((newState, newAction))65 return nodes 66 if(upNeighbor != '#'):67 newState = copy.deepcopy(currentState)68 newState[iA][jA] = replaceA69 newState[iA - 1][jA] = 'A' + newState[iA - 1][jA]70 newAction = 'UP'71 nodes.append((newState, newAction)) 72 if(downNeighbor != '#'):73 newState = copy.deepcopy(currentState)74 newState[iA][jA] = replaceA75 newState[iA + 1][jA] = 'A' + newState[iA + 1][jA]76 newAction = 'DOWN'77 nodes.append((newState, newAction)) 7879 if(rightNeighbor != '#'): 80 newState = copy.deepcopy(currentState)81 newState[iA][jA] = replaceA82 newState[iA][jA + 1] = 'A' + newState[iA][jA + 1]83 newAction = 'RIGHT'84 nodes.append((newState, newAction))8586 if(leftNeighbor != '#'):87 newState = copy.deepcopy(currentState)88 newState[iA][jA] = replaceA89 newState[iA][jA - 1] = 'A' + newState[iA][jA - 1]90 newAction = 'LEFT'91 nodes.append((newState, newAction)) 92 9394 elif stateOfA == 'AP':9596 if(len(currentState[iA][jA]) == 2):97 replaceA = ' '98 else:99 replaceA = currentState[iA][jA][2:len(currentState[iA][jA])]100101 if(upNeighbor == 'P'):102 PPosition = 'UP'103 iP = iA - 1104 jP = jA105 elif(downNeighbor == 'P'):106 PPosition = 'DOWN'107 iP = iA + 1108 jP = jA109 elif(leftNeighbor == 'P'):110 PPosition = 'LEFT'111 iP = iA112 jP = jA - 1113 elif(rightNeighbor == 'P'):114 PPosition = 'RIGHT'115 iP = iA116 jP = jA + 1117 elif(downNeighbor != 'P' and upNeighbor != 'P' and leftNeighbor != 'P' and rightNeighbor != 'P'):118 dimUpRightNeighbor = currentState[iA -1][jA + 1]119 dimUpLeftNeighbor = currentState[iA -1][jA - 1]120 dimDownRightNeighbor = currentState[iA + 1][jA + 1]121 dimDownLeftNeighbor = currentState[iA + 1][jA - 1]122 if(dimUpRightNeighbor == 'P'):123 PPosition = 'UPRIGHT'124125 if(dimUpLeftNeighbor == 'P'):126 PPosition = 'UPLEFT'127128 if(dimDownRightNeighbor == 'P'):129 PPosition = 'DOWNRIGHT'130131 if(dimDownLeftNeighbor == 'P'):132 PPosition = 'DOWNLEFT'133134 if(PPosition != 'DOWNLEFT' and PPosition != 'DOWNRIGHT' and PPosition != 'UPLEFT' and PPosition != 'UPRIGHT'):135 upNeighborOfP = currentState[iP-1][jP]136 downNeighborOfP = currentState[iP + 1][jP]137 rightNeighborOfP = currentState[iP][jP + 1]138 leftNeighborOfP = currentState[iP][jP - 1] 139140 if(PPosition == 'UP' and upNeighborOfP.isdigit() and int(upNeighborOfP) > 0):141 newState = copy.deepcopy(currentState)142 newState[iP][jP] = 'A'143 newState[iP - 1][jP] = str(int(upNeighborOfP) - 1)144 newState[iA][jA] = replaceA145 newAction = 'UP'146 nodes.append((newState, newAction)) 147 return nodes 148 if(PPosition == 'DOWN' and downNeighborOfP.isdigit() and int(downNeighborOfP) > 0):149 newState = copy.deepcopy(currentState)150 newState[iP][jP] = 'A'151 newState[iP + 1][jP] = str(int(downNeighborOfP) - 1)152 newState[iA][jA] = replaceA153 newAction = 'DOWN'154 nodes.append((newState, newAction)) 155 return nodes 156 if(PPosition == 'RIGHT' and rightNeighborOfP.isdigit() and int(rightNeighborOfP) > 0):157 newState = copy.deepcopy(currentState)158 newState[iP][jP] = 'A'159 newState[iP][jP + 1] = str(int(rightNeighborOfP) - 1)160 newState[iA][jA] = replaceA161 newAction = 'RIGHT'162 nodes.append((newState, newAction))163 return nodes 164 if(PPosition == 'LEFT' and leftNeighborOfP.isdigit() and int(leftNeighborOfP) > 0):165 #print("::::::::::::HOSPITAL LEFT::::::::::")166 newState = copy.deepcopy(currentState)167 newState[iP][jP] = 'A'168 newState[iP][jP - 1] = str((int(leftNeighborOfP) - 1))169 newState[iA][jA] = replaceA170 newAction = 'LEFT'171 nodes.append((newState, newAction))172 return nodes 173174 if(PPosition == 'UP' and upNeighborOfP != '#' and upNeighborOfP != 'P'):175 newState = copy.deepcopy(currentState)176 newState[iP][jP] = 'AP'177 newState[iP - 1][jP] = 'P'178 newState[iA][jA] = replaceA179 newAction = 'UP'180 nodes.append((newState, newAction)) 181 if(PPosition == 'DOWN' and downNeighborOfP != '#' and downNeighborOfP != 'P'):182 newState = copy.deepcopy(currentState)183 newState[iP][jP] = 'AP'184 newState[iP + 1][jP] = 'P'185 newState[iA][jA] = replaceA186 newAction = 'DOWN'187 nodes.append((newState, newAction))188 if(PPosition == 'RIGHT' and rightNeighborOfP != '#' and rightNeighborOfP != 'P'):189 newState = copy.deepcopy(currentState)190 newState[iP][jP] = 'AP'191 newState[iP][jP + 1] = 'P'192 newState[iA][jA] = replaceA193 newAction = 'RIGHT'194 nodes.append((newState, newAction))195 196 if(PPosition == 'LEFT' and leftNeighborOfP != '#' and leftNeighborOfP != 'P'):197 newState = copy.deepcopy(currentState)198 newState[iP][jP] = 'AP'199 newState[iP][jP - 1] = 'P'200 newState[iA][jA] = replaceA201 newAction = 'LEFT'202 nodes.append((newState, newAction)) 203 204 if((PPosition == 'UP' or PPosition == 'DOWN') and leftNeighbor != '#' and leftNeighbor != 'P'):205 newState = copy.deepcopy(currentState)206 newState[iA][jA] = replaceA207 newState[iA][jA - 1] = 'AP' + newState[iA][jA - 1]208 newAction = 'LEFT'209 nodes.append((newState, newAction)) 210 if((PPosition == 'UP' or PPosition == 'DOWN') and rightNeighbor != '#' and rightNeighbor != 'P'):211 newState = copy.deepcopy(currentState)212 newState[iA][jA] = replaceA213 newState[iA][jA + 1] = 'AP' + newState[iA][jA + 1]214 newAction = 'RIGHT'215 nodes.append((newState, newAction)) 216 if((PPosition == 'RIGHT' or PPosition == 'LEFT') and downNeighbor != '#' and downNeighbor != 'P'):217 newState = copy.deepcopy(currentState)218 newState[iA][jA] = replaceA219 newState[iA + 1][jA] = 'AP' + newState[iA + 1][jA]220 newAction = 'DOWN'221 nodes.append((newState, newAction)) 222 if((PPosition == 'RIGHT' or PPosition == 'LEFT') and upNeighbor != '#' and upNeighbor != 'P'):223 newState = copy.deepcopy(currentState)224 newState[iA][jA] = replaceA225 newState[iA - 1][jA] = 'AP' + newState[iA - 1][jA]226 newAction = 'UP'227 nodes.append((newState, newAction)) 228229 if((PPosition == 'DOWNLEFT' or PPosition == 'DOWNRIGHT') and downNeighbor != '#' and downNeighbor != 'P'):230 newState = copy.deepcopy(currentState)231 newState[iA][jA] = replaceA232 newState[iA + 1][jA] = 'AP' + newState[iA + 1][jA]233 newAction = 'DOWN'234 nodes.append((newState, newAction)) 235236 if((PPosition == 'UPRIGHT' or PPosition == 'DOWNRIGHT') and rightNeighbor != '#' and rightNeighbor != 'P'):237 newState = copy.deepcopy(currentState)238 newState[iA][jA] = replaceA239 newState[iA][jA + 1] = 'AP' + newState[iA][jA + 1]240 newAction = 'RIGHT'241 nodes.append((newState, newAction))242 if((PPosition == 'UPLEFT' or PPosition == 'DOWNLEFT') and leftNeighbor != '#' and leftNeighbor != 'P'):243 newState = copy.deepcopy(currentState)244 newState[iA][jA] = replaceA245 newState[iA][jA - 1] = 'AP' + newState[iA][jA - 1]246 newAction = 'LEFT'247 nodes.append((newState, newAction))248 if((PPosition == 'UPRIGHT' or PPosition == 'UPLEFT') and upNeighbor != '#' and upNeighbor != 'P'):249 newState = copy.deepcopy(currentState)250 newState[iA][jA] = replaceA251 newState[iA - 1][jA] = 'AP' + newState[iA - 1][jA]252 newAction = 'UP'253 nodes.append((newState, newAction)) 254 return nodes255256def checkIfNotInExplored(explored, state):257 notInExplored = True258 for x in explored:259 if(x == state):260 notInExplored = False261 break262 return notInExplored263264def DFS(currentNode, explored, depth, dataArray2D, allStates, uniqueStates):265 currentState, path, currentCost = currentNode266 allStates = allStates + 1267 if(checkGoalState(currentState)):268 return [currentState, path, currentCost, 'SUCCESS', allStates, uniqueStates]269 if checkIfNotInExplored(explored, currentState):270 uniqueStates = uniqueStates + 1271 if(checkGoalState(currentState)):272 return [currentState, path, currentCost, 'SUCCESS', allStates, uniqueStates]273 if depth == 0:274 return [currentState, path, currentCost, 'END', allStates, uniqueStates]275 explored.append(currentState)276 neighbors = expandNeighbors(currentState)277 for state, action in neighbors:278 if checkIfNotInExplored(explored, state):279 cost = currentCost + 1280 path.append(action)281 nextState, nextPath, nextCost, stateOfSearch, allStates, uniqueStates = DFS([state, path, cost], explored, depth - 1, dataArray2D, allStates, uniqueStates)282 if(stateOfSearch == 'SUCCESS'):283 return [nextState, nextPath, nextCost, stateOfSearch, allStates, uniqueStates]284 else:285 continue286 return [currentState, path, currentCost, 'CONTINUE', allStates, uniqueStates]287288289def IDS():290 with open('D:/AI/CA1/test3.txt', 'r') as f:291 data = f.read()292 print(data)293 print("FILE OPENED:::::::::::::::::::")294 dataArray = np.array(list(data))295 numOfRow = numberOfRows(dataArray)296 dataArraydeleted = np.delete(dataArray, np.argwhere(dataArray == '\n'))297 dataArray2D = np.reshape(dataArraydeleted, (numOfRow, -1))298 explored = deque()299 path = []300 cost = 0301 iterate = 0302 depth = -1303 allStates = 0304 uniqueStates = 0305 while True:306 iterate = iterate + 1307 depth = depth + 1308 if(iterate == 246):309 print("::::::::::::::::::CHECK", iterate)310 break311 finalState, finalPath, finalCost, stateOfSearch, allStates, uniqueStates = DFS([dataArray2D.tolist(), path, cost], explored, depth + 1, dataArray2D, allStates, uniqueStates)312 if stateOfSearch == 'SUCCESS':313 print("SUCCESS")314 return [finalState, finalPath, finalCost, allStates, uniqueStates]315 explored = deque()316 cost = cost + 1317 318319320start = time.time()321finalState, finalPath, finalCost, finalAllStates, finalUniqueStates = IDS()322end = time.time()323print("Time: ", end - start)324print("finalState: \n", np.array(finalState))325print("Path Length: ", len(finalPath))326print("Final Cost: ", finalCost)327print("Number of All States: ", finalAllStates) ...

Full Screen

Full Screen

BFS.py

Source:BFS.py Github

copy

Full Screen

1from collections import deque2import numpy as np3import copy4import time5import collections 67def numberOfRows(n):8 row = 19 for i in n:10 if(i == '\n'):11 row = row +112 return row1314def checkGoalState(map): #map: 2D array15 indexesOfP = np.where(np.array(map) == 'P')16 indexesOfAP = np.where(np.array(map) == 'AP')17 if(indexesOfP[0].size == 0 and indexesOfP[1].size == 0 and indexesOfAP[0].size == 0 and indexesOfAP[1].size == 0):18 return True19 l = np.reshape(np.array(map), (1, -1))20 l = l.tolist()21 l = l[0]22 return False2324def isAnyP(array):25 numberOfPs = (array == 'P').sum()26 return numberOfPs2728def expandNeighbors(currentState):29 iA, jA = np.where((np.array(currentState) >= 'A') & (np.array(currentState) != 'P') & (np.array(currentState) != 'AP') & (np.array(currentState) < 'AP'))30 if(iA.size != 0 and jA.size != 0):31 stateOfA = 'A'32 iA = int(iA)33 jA = int(jA)34 else:35 stateOfA = 'AP'36 iA, jA = np.where((np.array(currentState) >= 'AP') & (np.array(currentState) != 'P') & (np.array(currentState) != '#'))37 iA = int(iA)38 jA = int(jA)39 nodes = []40 upNeighbor = currentState[iA-1][jA]41 downNeighbor = currentState[iA + 1][jA]42 rightNeighbor = currentState[iA][jA + 1]43 leftNeighbor = currentState[iA][jA - 1]44 if (stateOfA == 'A'):45 if(len(currentState[iA][jA]) == 1):46 replaceA = ' '47 else:48 replaceA = currentState[iA][jA][1:len(currentState[iA][jA])]49 if(upNeighbor == 'P'):50 newState = copy.deepcopy(currentState)51 newState[iA][jA] = 'AP' + replaceA52 newAction = 'UP'53 nodes.append((newState, newAction))54 return nodes55 if(downNeighbor == 'P'):56 newState = copy.deepcopy(currentState)57 newState[iA][jA] = 'AP' + replaceA58 newAction = 'DOWN'59 nodes.append((newState, newAction))60 return nodes61 if(rightNeighbor == 'P'):62 newState = copy.deepcopy(currentState)63 newState[iA][jA] = 'AP' + replaceA64 newAction = 'RIGHT'65 nodes.append((newState, newAction))66 return nodes67 if(leftNeighbor == 'P'):68 newState = copy.deepcopy(currentState)69 newState[iA][jA] = 'AP' + replaceA70 newAction = 'LEFT'71 nodes.append((newState, newAction))72 return nodes 73 if(upNeighbor != '#'):74 newState = copy.deepcopy(currentState)75 newState[iA][jA] = replaceA76 newState[iA - 1][jA] = 'A' + newState[iA - 1][jA]77 newAction = 'UP'78 nodes.append((newState, newAction)) 79 if(downNeighbor != '#'):80 newState = copy.deepcopy(currentState)81 newState[iA][jA] = replaceA82 newState[iA + 1][jA] = 'A' + newState[iA + 1][jA]83 newAction = 'DOWN'84 nodes.append((newState, newAction)) 85 if(rightNeighbor != '#'):86 newState = copy.deepcopy(currentState)87 newState[iA][jA] = replaceA88 newState[iA][jA + 1] = 'A' + newState[iA][jA + 1]89 newAction = 'RIGHT'90 nodes.append((newState, newAction))91 if(leftNeighbor != '#'):92 newState = copy.deepcopy(currentState)93 newState[iA][jA] = replaceA94 newState[iA][jA - 1] = 'A' + newState[iA][jA - 1]95 newAction = 'LEFT'96 nodes.append((newState, newAction)) 9798 elif stateOfA == 'AP':99 if(len(currentState[iA][jA]) == 2):100 replaceA = ' '101 else:102 replaceA = currentState[iA][jA][2:len(currentState[iA][jA])]103 if(upNeighbor == 'P'):104 PPosition = 'UP'105 iP = iA - 1106 jP = jA107 elif(downNeighbor == 'P'):108 PPosition = 'DOWN'109 iP = iA + 1110 jP = jA111 elif(leftNeighbor == 'P'):112 PPosition = 'LEFT'113 iP = iA114 jP = jA - 1115 elif(rightNeighbor == 'P'):116 PPosition = 'RIGHT'117 iP = iA118 jP = jA + 1119 elif(downNeighbor != 'P' and upNeighbor != 'P' and leftNeighbor != 'P' and rightNeighbor != 'P'):120 dimUpRightNeighbor = currentState[iA -1][jA + 1]121 dimUpLeftNeighbor = currentState[iA -1][jA - 1]122 dimDownRightNeighbor = currentState[iA + 1][jA + 1]123 dimDownLeftNeighbor = currentState[iA + 1][jA - 1]124 if(dimUpRightNeighbor == 'P'):125 PPosition = 'UPRIGHT'126 iP = iA - 1127 jP = jA + 1128 if(dimUpLeftNeighbor == 'P'):129 PPosition = 'UPLEFT'130 iP = iA - 1131 jP = jA - 1132 if(dimDownRightNeighbor == 'P'):133 PPosition = 'DOWNRIGHT'134 iP = iA + 1135 jP = jA + 1136 if(dimDownLeftNeighbor == 'P'):137 PPosition = 'DOWNLEFT'138 iP = iA + 1139 jP = jA - 1140 141 upNeighborOfP = currentState[iP-1][jP]142 downNeighborOfP = currentState[iP + 1][jP]143 rightNeighborOfP = currentState[iP][jP + 1]144 leftNeighborOfP = currentState[iP][jP - 1] 145146 if(PPosition == 'UP' and upNeighborOfP.isdigit() and int(upNeighborOfP) > 0):147 newState = copy.deepcopy(currentState)148 newState[iP][jP] = 'A'149 newState[iP - 1][jP] = str(int(upNeighborOfP) - 1)150 newState[iA][jA] = replaceA151 newAction = 'UP'152 nodes.append((newState, newAction)) 153 return nodes 154 if(PPosition == 'DOWN' and downNeighborOfP.isdigit() and int(downNeighborOfP) > 0):155 newState = copy.deepcopy(currentState)156 newState[iP][jP] = 'A'157 newState[iP + 1][jP] = str(int(downNeighborOfP) - 1)158 newState[iA][jA] = replaceA159 newAction = 'DOWN'160 nodes.append((newState, newAction))161 return nodes 162 if(PPosition == 'RIGHT' and rightNeighborOfP.isdigit() and int(rightNeighborOfP) > 0):163 newState = copy.deepcopy(currentState)164 newState[iP][jP] = 'A'165 newState[iP][jP + 1] = str(int(rightNeighborOfP) - 1)166 newState[iA][jA] = replaceA167 newAction = 'RIGHT'168 nodes.append((newState, newAction))169 return nodes 170 if(PPosition == 'LEFT' and leftNeighborOfP.isdigit() and int(leftNeighborOfP) > 0):171 newState = copy.deepcopy(currentState)172 newState[iP][jP] = 'A'173 newState[iP][jP - 1] = str((int(leftNeighborOfP) - 1))174 newState[iA][jA] = replaceA175 newAction = 'LEFT'176 nodes.append((newState, newAction))177 return nodes 178179 if(PPosition == 'UP' and upNeighborOfP != '#' and upNeighborOfP != 'P'):180 newState = copy.deepcopy(currentState)181 newState[iP][jP] = 'AP'182 newState[iP - 1][jP] = 'P'183 newState[iA][jA] = replaceA184 newAction = 'UP'185 nodes.append((newState, newAction)) 186 if(PPosition == 'DOWN' and downNeighborOfP != '#' and downNeighborOfP != 'P'):187 newState = copy.deepcopy(currentState)188 newState[iP][jP] = 'AP'189 newState[iP + 1][jP] = 'P'190 newState[iA][jA] = replaceA191 newAction = 'DOWN'192 nodes.append((newState, newAction))193 if(PPosition == 'RIGHT' and rightNeighborOfP != '#' and rightNeighborOfP != 'P'):194 newState = copy.deepcopy(currentState)195 newState[iP][jP] = 'AP'196 newState[iP][jP + 1] = 'P'197 newState[iA][jA] = replaceA198 newAction = 'RIGHT'199 nodes.append((newState, newAction))200 201 if(PPosition == 'LEFT' and leftNeighborOfP != '#' and leftNeighborOfP != 'P'):202 newState = copy.deepcopy(currentState)203 newState[iP][jP] = 'AP'204 newState[iP][jP - 1] = 'P'205 newState[iA][jA] = replaceA206 newAction = 'LEFT'207 nodes.append((newState, newAction)) 208 209 if((PPosition == 'UP' or PPosition == 'DOWN') and leftNeighbor != '#' and leftNeighbor != 'P'):210 newState = copy.deepcopy(currentState)211 newState[iA][jA] = replaceA212 newState[iA][jA - 1] = 'AP' + newState[iA][jA - 1]213 newAction = 'LEFT'214 nodes.append((newState, newAction)) 215 if((PPosition == 'UP' or PPosition == 'DOWN') and rightNeighbor != '#' and rightNeighbor != 'P'):216 newState = copy.deepcopy(currentState)217 newState[iA][jA] = replaceA218 newState[iA][jA + 1] = 'AP' + newState[iA][jA + 1]219 newAction = 'RIGHT'220 nodes.append((newState, newAction)) 221 if((PPosition == 'RIGHT' or PPosition == 'LEFT') and downNeighbor != '#' and downNeighbor != 'P'):222 newState = copy.deepcopy(currentState)223 newState[iA][jA] = replaceA224 newState[iA + 1][jA] = 'AP' + newState[iA + 1][jA]225 newAction = 'DOWN'226 nodes.append((newState, newAction)) 227 if((PPosition == 'RIGHT' or PPosition == 'LEFT') and upNeighbor != '#' and upNeighbor != 'P'):228 newState = copy.deepcopy(currentState)229 newState[iA][jA] = replaceA230 newState[iA - 1][jA] = 'AP' + newState[iA - 1][jA]231 newAction = 'UP'232 nodes.append((newState, newAction)) 233234 if((PPosition == 'DOWNLEFT' or PPosition == 'DOWNRIGHT') and downNeighbor != '#' and downNeighbor != 'P'):235 newState = copy.deepcopy(currentState)236 newState[iA][jA] = replaceA237 newState[iA + 1][jA] = 'AP' + newState[iA + 1][jA]238 newAction = 'DOWN'239 nodes.append((newState, newAction)) 240241 if((PPosition == 'UPRIGHT' or PPosition == 'DOWNRIGHT') and rightNeighbor != '#' and rightNeighbor != 'P'):242 newState = copy.deepcopy(currentState)243 newState[iA][jA] = replaceA244 newState[iA][jA + 1] = 'AP' + newState[iA][jA + 1]245 newAction = 'RIGHT'246 nodes.append((newState, newAction))247 if((PPosition == 'UPLEFT' or PPosition == 'DOWNLEFT') and leftNeighbor != '#' and leftNeighbor != 'P'):248 newState = copy.deepcopy(currentState)249 newState[iA][jA] = replaceA250 newState[iA][jA - 1] = 'AP' + newState[iA][jA - 1]251 newAction = 'LEFT'252 nodes.append((newState, newAction))253 if((PPosition == 'UPRIGHT' or PPosition == 'UPLEFT') and upNeighbor != '#' and upNeighbor != 'P'):254 newState = copy.deepcopy(currentState)255 newState[iA][jA] = replaceA256 newState[iA - 1][jA] = 'AP' + newState[iA - 1][jA]257 newAction = 'UP'258 nodes.append((newState, newAction)) 259 return nodes260261def bfs():262 with open('D:/AI/CA1/test1.txt', 'r') as f:263 data = f.read()264 print("FIL OPENED:::::::::::::::::::")265 dataArray = np.array(list(data))266 numOfRow = numberOfRows(dataArray)267 dataArraydeleted = np.delete(dataArray, np.argwhere(dataArray == '\n'))268 dataArray2D = np.reshape(dataArraydeleted, (numOfRow, -1))269 print(dataArray2D)270 frontier = deque()271 explored = deque()272 numberOfStates = 1273 cost = 0274 path = []275 nodeA = [dataArray2D.tolist(), path, cost]276 frontier.append(nodeA)277 path = []278 iterate = 0279 addedToFrontierState = 0280 allStates = 1281 while True:282 iterate = iterate + 1283 if(iterate == 1000):284 break285 if(len(frontier)== 0 ):286 print("LEN OF ZERO")287 break288 currentState, path, currentCost = frontier.popleft()289 explored.append(currentState)290 neighbors = expandNeighbors(currentState)291 for state, action in neighbors:292 allStates = allStates + 1293 numberOfStates = numberOfStates + 1294 path.append(action)295 cost = 1 + currentCost 296 newNode = [state, path, cost]297 if(len(frontier) == 0):298 notInFrontier = True299 else:300 notInFrontier = not newNode in frontier301 for x in explored:302 if(x == state):303 notInExplored = False304 break305 notInExplored = True306307 if((notInFrontier and notInExplored)):308 addedToFrontierState = addedToFrontierState + 1309 if(checkGoalState(state)):310 print("INSIDE GOAL:::::")311 print("success")312 print("Length of frontier list: ", len(frontier))313 print("Length of explored list: ", len(explored))314 return [cost, path, state, addedToFrontierState, allStates, iterate]315 else:316 frontier.append(newNode)317318319cost = 0320path = []321state = [] 322addedToFrontierState = 0 323allStates = 0 324iterate = 0325326start = time.time()327#resultCost, resultPath, resultSatate = bfs()328cost, path, state, addedToFrontierState, allStates, iterate = bfs()329end = time.time()330print("Time: ", end - start)331print("Number of iterations in loop: ", iterate)332print("Cost: ", cost)333print("Goal State: \n", np.array(state))334print("Unique States: ", addedToFrontierState)335print("All seen states: ", allStates)336print("Length of path: ", len(path)) ...

Full Screen

Full Screen

BFS_WithoutPriority.py

Source:BFS_WithoutPriority.py Github

copy

Full Screen

1from collections import deque2import numpy as np3import copy4import time5import collections 67def numberOfRows(n):8 row = 19 for i in n:10 if(i == '\n'):11 row = row +112 return row1314def checkGoalState(map): #map: 2D array15 indexesOfP = np.where(np.array(map) == 'P')16 indexesOfAP = np.where(np.array(map) == 'AP')17 if(indexesOfP[0].size == 0 and indexesOfP[1].size == 0 and indexesOfAP[0].size == 0 and indexesOfAP[1].size == 0):18 return True19 l = np.reshape(np.array(map), (1, -1))20 l = l.tolist()21 l = l[0]22 return False2324def isAnyP(array):25 numberOfPs = (array == 'P').sum()26 return numberOfPs2728def expandNeighbors(currentState):29 iA, jA = np.where((np.array(currentState) >= 'A') & (np.array(currentState) != 'P') & (np.array(currentState) != 'AP') & (np.array(currentState) < 'AP'))30 if(iA.size != 0 and jA.size != 0):31 stateOfA = 'A'32 iA = int(iA)33 jA = int(jA)34 else:35 stateOfA = 'AP'36 iA, jA = np.where((np.array(currentState) >= 'AP') & (np.array(currentState) != 'P') & (np.array(currentState) != '#'))37 iA = int(iA)38 jA = int(jA)39 nodes = []40 upNeighbor = currentState[iA-1][jA]41 downNeighbor = currentState[iA + 1][jA]42 rightNeighbor = currentState[iA][jA + 1]43 leftNeighbor = currentState[iA][jA - 1]44 if (stateOfA == 'A'):45 if(len(currentState[iA][jA]) == 1):46 replaceA = ' '47 else:48 replaceA = currentState[iA][jA][1:len(currentState[iA][jA])]49 if(upNeighbor == 'P'):50 newState = copy.deepcopy(currentState)51 newState[iA][jA] = 'AP' + replaceA52 newAction = 'UP'53 nodes.append((newState, newAction))54 if(downNeighbor == 'P'):55 newState = copy.deepcopy(currentState)56 newState[iA][jA] = 'AP' + replaceA57 newAction = 'DOWN'58 nodes.append((newState, newAction))59 if(rightNeighbor == 'P'):60 newState = copy.deepcopy(currentState)61 newState[iA][jA] = 'AP' + replaceA62 newAction = 'RIGHT'63 nodes.append((newState, newAction))64 if(leftNeighbor == 'P'):65 newState = copy.deepcopy(currentState)66 newState[iA][jA] = 'AP' + replaceA67 newAction = 'LEFT'68 nodes.append((newState, newAction)) 69 if(upNeighbor != '#' and upNeighbor != 'P'):70 newState = copy.deepcopy(currentState)71 newState[iA][jA] = replaceA72 newState[iA - 1][jA] = 'A' + newState[iA - 1][jA]73 newAction = 'UP'74 nodes.append((newState, newAction)) 75 if(downNeighbor != '#' and downNeighbor != 'P'):76 newState = copy.deepcopy(currentState)77 newState[iA][jA] = replaceA78 newState[iA + 1][jA] = 'A' + newState[iA + 1][jA]79 newAction = 'DOWN'80 nodes.append((newState, newAction)) 81 if(rightNeighbor != '#' and rightNeighbor != 'P'):82 newState = copy.deepcopy(currentState)83 newState[iA][jA] = replaceA84 newState[iA][jA + 1] = 'A' + newState[iA][jA + 1]85 newAction = 'RIGHT'86 nodes.append((newState, newAction))87 if(leftNeighbor != '#' and leftNeighbor != 'P'):88 newState = copy.deepcopy(currentState)89 newState[iA][jA] = replaceA90 newState[iA][jA - 1] = 'A' + newState[iA][jA - 1]91 newAction = 'LEFT'92 nodes.append((newState, newAction)) 9394 elif stateOfA == 'AP':95 if(len(currentState[iA][jA]) == 2):96 replaceA = ' '97 else:98 replaceA = currentState[iA][jA][2:len(currentState[iA][jA])]99 if(upNeighbor == 'P'):100 PPosition = 'UP'101 iP = iA - 1102 jP = jA103 elif(downNeighbor == 'P'):104 PPosition = 'DOWN'105 iP = iA + 1106 jP = jA107 elif(leftNeighbor == 'P'):108 PPosition = 'LEFT'109 iP = iA110 jP = jA - 1111 elif(rightNeighbor == 'P'):112 PPosition = 'RIGHT'113 iP = iA114 jP = jA + 1115 elif(downNeighbor != 'P' and upNeighbor != 'P' and leftNeighbor != 'P' and rightNeighbor != 'P'):116 dimUpRightNeighbor = currentState[iA -1][jA + 1]117 dimUpLeftNeighbor = currentState[iA -1][jA - 1]118 dimDownRightNeighbor = currentState[iA + 1][jA + 1]119 dimDownLeftNeighbor = currentState[iA + 1][jA - 1]120 if(dimUpRightNeighbor == 'P'):121 PPosition = 'UPRIGHT'122 iP = iA - 1123 jP = jA + 1124 if(dimUpLeftNeighbor == 'P'):125 PPosition = 'UPLEFT'126 iP = iA - 1127 jP = jA - 1128 if(dimDownRightNeighbor == 'P'):129 PPosition = 'DOWNRIGHT'130 iP = iA + 1131 jP = jA + 1132 if(dimDownLeftNeighbor == 'P'):133 PPosition = 'DOWNLEFT'134 iP = iA + 1135 jP = jA - 1136 137 upNeighborOfP = currentState[iP-1][jP]138 downNeighborOfP = currentState[iP + 1][jP]139 rightNeighborOfP = currentState[iP][jP + 1]140 leftNeighborOfP = currentState[iP][jP - 1] 141142 if(PPosition == 'UP' and upNeighborOfP.isdigit() and int(upNeighborOfP) > 0):143 newState = copy.deepcopy(currentState)144 newState[iP][jP] = 'A'145 newState[iP - 1][jP] = str(int(upNeighborOfP) - 1)146 newState[iA][jA] = replaceA147 newAction = 'UP'148 nodes.append((newState, newAction)) 149 if(PPosition == 'DOWN' and downNeighborOfP.isdigit() and int(downNeighborOfP) > 0):150 newState = copy.deepcopy(currentState)151 newState[iP][jP] = 'A'152 newState[iP + 1][jP] = str(int(downNeighborOfP) - 1)153 newState[iA][jA] = replaceA154 newAction = 'DOWN'155 nodes.append((newState, newAction)) 156 if(PPosition == 'RIGHT' and rightNeighborOfP.isdigit() and int(rightNeighborOfP) > 0):157 newState = copy.deepcopy(currentState)158 newState[iP][jP] = 'A'159 newState[iP][jP + 1] = str(int(rightNeighborOfP) - 1)160 newState[iA][jA] = replaceA161 newAction = 'RIGHT'162 nodes.append((newState, newAction)) 163 if(PPosition == 'LEFT' and leftNeighborOfP.isdigit() and int(leftNeighborOfP) > 0):164 newState = copy.deepcopy(currentState)165 newState[iP][jP] = 'A'166 newState[iP][jP - 1] = str((int(leftNeighborOfP) - 1))167 newState[iA][jA] = replaceA168 newAction = 'LEFT'169 nodes.append((newState, newAction))170171 if(PPosition == 'UP' and upNeighborOfP != '#' and upNeighborOfP != 'P'):172 newState = copy.deepcopy(currentState)173 newState[iP][jP] = 'AP'174 newState[iP - 1][jP] = 'P'175 newState[iA][jA] = replaceA176 newAction = 'UP'177 nodes.append((newState, newAction)) 178 if(PPosition == 'DOWN' and downNeighborOfP != '#' and downNeighborOfP != 'P'):179 newState = copy.deepcopy(currentState)180 newState[iP][jP] = 'AP'181 newState[iP + 1][jP] = 'P'182 newState[iA][jA] = replaceA183 newAction = 'DOWN'184 nodes.append((newState, newAction))185 if(PPosition == 'RIGHT' and rightNeighborOfP != '#' and rightNeighborOfP != 'P'):186 newState = copy.deepcopy(currentState)187 newState[iP][jP] = 'AP'188 newState[iP][jP + 1] = 'P'189 newState[iA][jA] = replaceA190 newAction = 'RIGHT'191 nodes.append((newState, newAction))192 193 if(PPosition == 'LEFT' and leftNeighborOfP != '#' and leftNeighborOfP != 'P'):194 newState = copy.deepcopy(currentState)195 newState[iP][jP] = 'AP'196 newState[iP][jP - 1] = 'P'197 newState[iA][jA] = replaceA198 newAction = 'LEFT'199 nodes.append((newState, newAction)) 200 201 if((PPosition == 'UP' or PPosition == 'DOWN') and leftNeighbor != '#' and leftNeighbor != 'P'):202 newState = copy.deepcopy(currentState)203 newState[iA][jA] = replaceA204 newState[iA][jA - 1] = 'AP' + newState[iA][jA - 1]205 newAction = 'LEFT'206 nodes.append((newState, newAction)) 207 if((PPosition == 'UP' or PPosition == 'DOWN') and rightNeighbor != '#' and rightNeighbor != 'P'):208 newState = copy.deepcopy(currentState)209 newState[iA][jA] = replaceA210 newState[iA][jA + 1] = 'AP' + newState[iA][jA + 1]211 newAction = 'RIGHT'212 nodes.append((newState, newAction)) 213 if((PPosition == 'RIGHT' or PPosition == 'LEFT') and downNeighbor != '#' and downNeighbor != 'P'):214 newState = copy.deepcopy(currentState)215 newState[iA][jA] = replaceA216 newState[iA + 1][jA] = 'AP' + newState[iA + 1][jA]217 newAction = 'DOWN'218 nodes.append((newState, newAction)) 219 if((PPosition == 'RIGHT' or PPosition == 'LEFT') and upNeighbor != '#' and upNeighbor != 'P'):220 newState = copy.deepcopy(currentState)221 newState[iA][jA] = replaceA222 newState[iA - 1][jA] = 'AP' + newState[iA - 1][jA]223 newAction = 'UP'224 nodes.append((newState, newAction)) 225226 if((PPosition == 'DOWNLEFT' or PPosition == 'DOWNRIGHT') and downNeighbor != '#' and downNeighbor != 'P'):227 newState = copy.deepcopy(currentState)228 newState[iA][jA] = replaceA229 newState[iA + 1][jA] = 'AP' + newState[iA + 1][jA]230 newAction = 'DOWN'231 nodes.append((newState, newAction)) 232233 if((PPosition == 'UPRIGHT' or PPosition == 'DOWNRIGHT') and rightNeighbor != '#' and rightNeighbor != 'P'):234 newState = copy.deepcopy(currentState)235 newState[iA][jA] = replaceA236 newState[iA][jA + 1] = 'AP' + newState[iA][jA + 1]237 newAction = 'RIGHT'238 nodes.append((newState, newAction))239 if((PPosition == 'UPLEFT' or PPosition == 'DOWNLEFT') and leftNeighbor != '#' and leftNeighbor != 'P'):240 newState = copy.deepcopy(currentState)241 newState[iA][jA] = replaceA242 newState[iA][jA - 1] = 'AP' + newState[iA][jA - 1]243 newAction = 'LEFT'244 nodes.append((newState, newAction))245 if((PPosition == 'UPRIGHT' or PPosition == 'UPLEFT') and upNeighbor != '#' and upNeighbor != 'P'):246 newState = copy.deepcopy(currentState)247 newState[iA][jA] = replaceA248 newState[iA - 1][jA] = 'AP' + newState[iA - 1][jA]249 newAction = 'UP'250 nodes.append((newState, newAction)) 251 return nodes252253def bfs():254 with open('D:/AI/CA1/test3.txt', 'r') as f:255 data = f.read()256 print("FIL OPENED:::::::::::::::::::")257 dataArray = np.array(list(data))258 numOfRow = numberOfRows(dataArray)259 dataArraydeleted = np.delete(dataArray, np.argwhere(dataArray == '\n'))260 dataArray2D = np.reshape(dataArraydeleted, (numOfRow, -1))261 frontier = deque()262 explored = deque()263 numberOfStates = 1264 cost = 0265 path = []266 nodeA = [dataArray2D.tolist(), path, cost]267 frontier.append(nodeA)268 path = []269 iterate = 0270 addedToFrontierState = 0271 allStates = 1272 while True:273 iterate = iterate + 1274 if(iterate == 2000):275 break276 if(len(frontier)== 0 ):277 print("LEN OF ZERO")278 break279 currentState, path, currentCost = frontier.popleft()280 explored.append(currentState)281 neighbors = expandNeighbors(currentState)282 for state, action in neighbors:283 allStates = allStates + 1284 numberOfStates = numberOfStates + 1285 path.append(action)286 cost = 1 + currentCost 287 newNode = [state, path, cost]288 if(len(frontier) == 0):289 notInFrontier = True290 else:291 notInFrontier = not newNode in frontier292 for x in explored:293 if(x == state):294 notInExplored = False295 break296 notInExplored = True297298 if((notInFrontier and notInExplored)):299 addedToFrontierState = addedToFrontierState + 1300 if(checkGoalState(state)):301 print("INSIDE GOAL:::::")302 print("success")303 print("Length of frontier list: ", len(frontier))304 print("Length of explored list: ", len(explored))305 return [cost, path, state, addedToFrontierState, allStates, iterate]306 else:307 frontier.append(newNode)308309310cost = 0311path = []312state = [] 313addedToFrontierState = 0 314allStates = 0 315iterate = 0316317start = time.time()318#resultCost, resultPath, resultSatate = bfs()319cost, path, state, addedToFrontierState, allStates, iterate = bfs()320end = time.time()321print("Time: ", end - start)322print("Number of iterations in loop: ", iterate)323print("Cost: ", cost)324print("Goal State: \n", np.array(state))325print("Unique States: ", addedToFrontierState)326print("All seen states: ", allStates)327print("Length of path: ", len(path)) ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful