def astar(graph, start, goal): # Fringe. Nodes not visited yet openList = PriorityQueue() # Visited Nodes. Each one will store it's parent position closedList = {} node = Node(start) openList.push(node) while openList: node, _ = openList.pop() position, cost = node.position, node.cost if position in closedList: # Oops. Already expanded. continue # Save the node in the closed list, and keep track of its parent closedList[position] = node.parent if position == goal: break for childPosition, actionCost in graph.getChildren(position): # Only add to the open list if it's not expanded yet if not childPosition in closedList: childNode = Node(childPosition, cost + actionCost, position) openList.push( childNode, childNode.cost + graph.getHCost(childPosition, goal)) path = [] if position == goal: # Ensure a path has been found path.insert(0, position) while position and position != start: position = closedList[position] path.insert(0, position) return path
class WaitingQueue: def __init__(self): self.queue = PriorityQueue() self.log = [] def push(self, current_time, p): self.queue.push([p.healthy, current_time], p) self.log.append((Log.ENTER, p.healthy, current_time)) def empty(self): return self.queue.empty() def pop(self, current_time): p = self.queue.pop()[-1] self.log.append((Log.EXIT, p.healthy, current_time)) return p def remove(self, current_time, p): self.queue.remove(p) self.log.append((Log.EXIT, p.healthy, current_time)) def size(self): return self.queue.size()
def astar(graph, start, goal): # Fringe. Nodes not visited yet openList = PriorityQueue() # Visited Nodes. Each one will store it's parent position closedList = {} node = Node(start) openList.push(node) while openList: node, _ = openList.pop() position, cost = node.position, node.cost if position in closedList: # Oops. Already expanded. continue # Save the node in the closed list, and keep track of its parent closedList[position] = node.parent if position == goal: break for childPosition, actionCost in graph.getChildren(position): # Only add to the open list if it's not expanded yet if not childPosition in closedList: childNode = Node(childPosition, cost + actionCost, position) openList.push(childNode, childNode.cost + graph.getHCost(childPosition, goal)) path = [] if position == goal: # Ensure a path has been found path.insert(0, position) while position and position != start: position = closedList[position] path.insert(0, position) return path
def astarItemsMultiPath(graph, start, goal, itemsAvailable, initialRaftState, illegalEdges): #itemsAvailable = intialItems.copy() #print("initial state") print(itemsAvailable) # Fringe. Nodes not visited yet openList = PriorityQueue() # Visited Nodes. Each one will store it's parent position closedList = {} nodeTable = {} # assign some start and goal points # and push start point as a node to openList. start_tuple = tuple(start) goal_tuple = tuple(goal) node = PathNode(start_tuple, itemsAvailable.copy()) node.setRaftState(initialRaftState) openList.push(node) while openList: # if openList does not contain any node # we will break. curr_node, _ = openList.pop() if not curr_node: break position, cost = curr_node.position, curr_node.cost if position in closedList: # Oops. Already expanded. continue # Save the node in the closed list, and keep track of its parent if curr_node.parent != None: closedList[position] = curr_node.parent.position else: closedList[position] = curr_node.position #track of its items remainings nodeTable[position] = curr_node # arrived to goal so save it as goal node. if position == goal_tuple: goalNode = curr_node break # copy some parent properties curr_available = curr_node.getItemAvailable() raftState = curr_node.getRaftState() parentStonePlace = curr_node.getStonePlaced().copy() # for each legal child for current parent, for childPosition, actionCost, itemRequired, element in graph.getChildren( position, curr_available, raftState, parentStonePlace): copyStonePlaced = parentStonePlace.copy() # if illegal edges were given # and current one is illegal, ignore current child. pos1 = list(position).copy() pos2 = childPosition if illegalEdges and ([pos1, pos2] in illegalEdges or [pos2, pos1] in illegalEdges): continue # Only add to the open list if it's not expanded yet if not tuple(childPosition) in closedList: # copyAvailableItem copy_available_item = curr_available.copy() # deduct from item list if it was required if itemRequired: copy_available_item = deductItem(copy_available_item, itemRequired) childRaftSate = raftState # updating child raftstate based on where current child is # and the item required to get to the child. if itemRequired == 'r': childRaftSate = 1 elif childRaftSate == 1 and element != '~': childRaftSate = 0 # updating the stone coordinate that was used for child. # append to the parent ones. stoneCoord = [] if itemRequired == 'o': stoneCoord = [childPosition] #print(stoneCoord) copyStonePlaced += stoneCoord # creating new child node and push to the openlist childNode = PathNode(tuple(childPosition), copy_available_item, cost + actionCost, curr_node, childRaftSate) childNode.addStonePlaced(copyStonePlaced) openList.push( childNode, childNode.cost + graph.getHCost(childPosition, goal)) # intialize the returning variables path = [] totalItemUsed = [] totalNewItemCollected = [] itemUsedState = False finalItemList = itemsAvailable finalRaftState = 1 finalStonePlace = [] # if we arrive goal, we are updating those variables. if position == goal_tuple: finalItemList = goalNode.getItemAvailable() itemUsedState = confirmItemUsed(finalItemList, itemsAvailable) finalRaftState = goalNode.getRaftState() finalStonePlace = goalNode.getStonePlaced() # Ensure a path has been found position_list = list(position) path.insert(0, position_list) while position and position != start_tuple: position = closedList[position] position_list = list(position) path.insert(0, position_list) return [ path, itemUsedState, finalItemList, finalRaftState, finalStonePlace, nodeTable ] #finalItemList, ]
def get_ready_queue(job_list, pattern, time_quantum): # obtain job key function as key for priority queue comparison job_key = job_keys[pattern] if job_key == round_robin: job_list.sort(key=first_come_first_serve) i = 0 while i < len(job_list): job = job_list[i] job.instance_id = i if job.duration > time_quantum: split_job = Job(job.arrival + time_quantum, job.duration - time_quantum, job.priority, job.id, True) job.terminate = False job.duration = time_quantum job_list.append(split_job) i += 1 global last_index last_index.clear() # prepare job queue job_queue = PriorityQueue(job_list, key=job_key) # prepare the ready queue ready_queue = [] global time_elapsed time_elapsed = 0 global idd idd = 0 def schedule(job): # schedule a job to the ready queue if ready_queue and ready_queue[-1].id == job.id: # merge with the previous job because same ID # this happens when pre-emptive ready_queue[-1].duration += job.duration ready_queue[-1].terminate = job.terminate else: # append new job ready_queue.append(job) # update time elapsed after scheduling global time_elapsed time_elapsed = ready_queue[-1].end() global last_index, idd last_index[job.id] = idd idd += 1 # simulate scheduling algorithm while job_queue: # get the next job job = job_queue.pop() global last_index if job.idd != (last_index[job.id] if job.id in last_index else -1): job.idd = (last_index[job.id] if job.id in last_index else -1) job_queue.push(job) elif job.arrival < time_elapsed: # this job will arrive later, repush to the queue job.arrival = time_elapsed #job.instance_id = Job.instance_id #Job.instance_id += 1 job_queue.push(job) elif job_key == round_robin and job.duration > time_quantum: # split job into two for round robin part_two = Job( arrival = job.arrival + time_quantum, duration = job.duration - time_quantum, priority = job.priority, job_id = job.id, terminate = job.terminate ) # part_two.instance_id = job.instance_id + len(job_list) job_queue.push(part_two) # repush part 2 # process part 1 job.duration = time_quantum job.terminate = False job_queue.push(job) # schedule(job) elif not pre_emptive(job_key): # schedule immediately schedule(job) else: # pre_emptive # special case: find possible jobs that can interrupt put_back = [] interrupted = False while job_queue and job_queue.top().arrival < job.end(): # check if the next job is better than having the other end of the current job interrupt = job_queue.top() split_job = Job( arrival = interrupt.arrival, duration = job.end() - interrupt.arrival, priority = job.priority, job_id = job.id, terminate = job.terminate ) # print interrupt, 'wants to interrupt', split_job # print job_key(interrupt), job_key(split_job) if job_key(interrupt) < job_key(split_job): # job has been interrupted! split then repush into the queue job.duration -= split_job.duration job.terminate = False # push new parts of the job job_queue.push(job) job_queue.push(split_job) interrupted = True break put_back.append(job_queue.pop()) # return tested jobs back into queue for tested_job in put_back: job_queue.push(tested_job) if not interrupted: # finalize job schedule schedule(job) return ready_queue
def is_bounded(x): return x[0] >= 0 and x[0] <= 100 and x[1] >= 0 and x[1] <= 100 path = [] start = (0,50) while in_circle(start): start = (0,np.random.randint(101)) frontier = PriorityQueue() frontier.insert(start,0) visited = set() while frontier.heap: dist,node = frontier.pop() path.append(node) if node[0] == 100: break visited.add(node) up = (node[0], node[1] + d) up_right = (node[0] + d, node[1] + d) right = (node[0] + d + 1, node[1]) down_right = (node[0] + d, node[1] - d) down = (node[0], node[1] - d) for n in [up,up_right,right,down_right,down]: if n not in visited and not in_circle(n) and is_bounded(n):
def get_ready_queue(job_list, pattern, time_quantum): # obtain job key function as key for priority queue comparison job_key = job_keys[pattern] if job_key == round_robin: job_list.sort(key=first_come_first_serve) i = 0 while i < len(job_list): job = job_list[i] job.instance_id = i if job.duration > time_quantum: split_job = Job(job.arrival + time_quantum, job.duration - time_quantum, job.priority, job.id, True) job.terminate = False job.duration = time_quantum job_list.append(split_job) i += 1 global last_index last_index.clear() # prepare job queue job_queue = PriorityQueue(job_list, key=job_key) # prepare the ready queue ready_queue = [] global time_elapsed time_elapsed = 0 global idd idd = 0 def schedule(job): # schedule a job to the ready queue if ready_queue and ready_queue[-1].id == job.id: # merge with the previous job because same ID # this happens when pre-emptive ready_queue[-1].duration += job.duration ready_queue[-1].terminate = job.terminate else: # append new job ready_queue.append(job) # update time elapsed after scheduling global time_elapsed time_elapsed = ready_queue[-1].end() global last_index, idd last_index[job.id] = idd idd += 1 # simulate scheduling algorithm while job_queue: # get the next job job = job_queue.pop() global last_index if job.idd != (last_index[job.id] if job.id in last_index else -1): job.idd = (last_index[job.id] if job.id in last_index else -1) job_queue.push(job) elif job.arrival < time_elapsed: # this job will arrive later, repush to the queue job.arrival = time_elapsed #job.instance_id = Job.instance_id #Job.instance_id += 1 job_queue.push(job) elif job_key == round_robin and job.duration > time_quantum: # split job into two for round robin part_two = Job(arrival=job.arrival + time_quantum, duration=job.duration - time_quantum, priority=job.priority, job_id=job.id, terminate=job.terminate) # part_two.instance_id = job.instance_id + len(job_list) job_queue.push(part_two) # repush part 2 # process part 1 job.duration = time_quantum job.terminate = False job_queue.push(job) # schedule(job) elif not pre_emptive(job_key): # schedule immediately schedule(job) else: # pre_emptive # special case: find possible jobs that can interrupt put_back = [] interrupted = False while job_queue and job_queue.top().arrival < job.end(): # check if the next job is better than having the other end of the current job interrupt = job_queue.top() split_job = Job(arrival=interrupt.arrival, duration=job.end() - interrupt.arrival, priority=job.priority, job_id=job.id, terminate=job.terminate) # print interrupt, 'wants to interrupt', split_job # print job_key(interrupt), job_key(split_job) if job_key(interrupt) < job_key(split_job): # job has been interrupted! split then repush into the queue job.duration -= split_job.duration job.terminate = False # push new parts of the job job_queue.push(job) job_queue.push(split_job) interrupted = True break put_back.append(job_queue.pop()) # return tested jobs back into queue for tested_job in put_back: job_queue.push(tested_job) if not interrupted: # finalize job schedule schedule(job) return ready_queue