def simulation(numSeconds, numStudents, pagesPerMinute): labprinter = Printer(pagesPerMinute) printQueue = Queue() waitingtimes = [] for currentSecond in range(numSeconds): if newPrintTask(numStudents): task = Task(currentSecond) printQueue.enqueue(task) if (not labprinter.busy()) and (not printQueue.isEmpty()): nexttask = printQueue.dequeue() waitingtimes.append(nexttask.waitTime(currentSecond)) labprinter.startNext(nexttask) labprinter.tick() averageWait = sum(waitingtimes) / len(waitingtimes) print("Average Wait %6.2f secs %3d tasks remaining." % (averageWait, printQueue.size())) return averageWait
def single_bfs(big_list, current): queue = Queue() explored = Queue() newExplored = Stack() seen = set() pointer = current queue.enqueue(pointer) #Add the our initial position to the explored.enqueue(pointer) #queue tra_model = Tra_model() while True: if queue.isEmpty(): break else: pointer = queue.dequeue() # First move to the right and check what it is pointer = tra_model.move_right(pointer) #make the first move x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location queue.enqueue(pointer) explored.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_left(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_left(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break # second move to the bottom and check what it is pointer = tra_model.move_down(pointer) x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location explored.enqueue(pointer) queue.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_up(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_up(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break # Third move to the left and check what it is pointer = tra_model.move_left(pointer) x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location explored.enqueue(pointer) queue.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_right(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_right(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break # Fourth move to the left and check what it is pointer = tra_model.move_up(pointer) x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location explored.enqueue(pointer) queue.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_down(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_down(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break expanded = 0 for i in explored.items: expanded += 1 steps = 0 for item in explored.items: t = tuple(item) if t not in seen: steps += 1 newExplored.push(item) seen.add(t) return newExplored, big_list, steps, expanded