Esempio n. 1
0
def aStarSearch(graph, start, goal, heuristic):
	# initialize Priority Queue
	frontier = PriorityQueue()
	frontier.put(start, 0)
	previous = {}
	currentCost = {}
	previous[start] = None
	currentCost[start] = 0
	counter = 1
	space = 0
	# while frontier is not empty
	while not frontier.empty():
		current = frontier.get()
		if current == goal:
			break
		for next in graph.neighbors(current):
			# determine A* cost
			new_cost = currentCost[current] + graph.distanceToDistination(graph.getWeight(current), graph.getWeight(next))
			# check if the cost has gone down since last time we visited to determine if location has already been visited
			if next not in currentCost or new_cost < currentCost[next]:
			 	currentCost[next] = new_cost
				# determine which heuristic to use
			 	if heuristic == 1:
			 		heuristicValue = heuristicEuclidean(graph.getWeight(current), graph.getWeight(goal))
			 	else:
			 		heuristicValue = heuristicChebyshev(graph.getWeight(current), graph.getWeight(goal))
			 	# add heuristic cose to A* cost
			 	priority = new_cost + heuristicValue
			 	# add path with it's priority
			 	frontier.put(next, priority)
			 	previous[next] = current
			 	counter = counter + 1
			 	space = max(space, frontier.size())
	return previous, currentCost, counter, space
Esempio n. 2
0
def greedyFirstSearch(graph, start, goal, heuristic):
	# initialize priority queue
	frontier = PriorityQueue()
	frontier.put(start, 0)
	previous = {}
	previous[start] = None
	counter = 1
	space = 0
	# if frontier isn't empty
	while not frontier.empty():
		current = frontier.get()
		# check if current is the goal
		if current == goal:
			break
		for next in graph.neighbors(current):
			if next not in previous:
				# Greedy Best First Search will only use the heuristic to determine the path to choose
				if heuristic == 1:
			 		heuristicValue = heuristicEuclidean(graph.getWeight(current), graph.getWeight(goal))
			 	else:
			 		heuristicValue = heuristicChebyshev(graph.getWeight(current), graph.getWeight(goal))
				priority = heuristicValue
				frontier.put(next, priority)
				counter = counter + 1
				previous[next] = current
			space = max(space, frontier.size())
	return previous, counter, space
class PQueueTestCase(unittest.TestCase):
    '''Test creation and use of a priority queue.'''
    
    def setUp(self):
        self.pq = PriorityQueue()
        self.r1 = Rectangle(0, 150, 200, 50, media.forestgreen, 1)
        self.o1= Oval(120, 160, 60, 60, media.white, 4)
        self.o2 = Oval(120, 115, 45, 45, media.yellow, 3)
        self.o3 = Oval(120, 80, 30, 30, media.orange, 2)

    def tearDown(self):
        self.pq = None
        self.r1 = None
        self.o1 = None
        self.o2 = None
        self.o3 = None
            
    def testSize(self):
        assert self.pq.size() == 0, 'mismatch in predicted pqueue size'
        
    def testEnqueueSize(self):
        self.pq.enqueue(self.r1)
        assert self.pq.size() == 1, \
               'mismatch in pqueue size after enqueue'

    def testDequeue(self):
        self.pq.enqueue(self.r1)
        result = self.pq.dequeue()
        assert result is self.r1, \
               'mismatch in dequeued value'

    def testMultipleEnqueueSize(self):
        self.pq.enqueue(self.o1)
        self.pq.enqueue(self.o2)
        self.pq.enqueue(self.o3)
        assert self.pq.size() == 3, \
               'mismatch in pqueue size after enqueues'

    def testMultipleDequeue(self):
        self.pq.enqueue(self.o1)
        self.pq.enqueue(self.o2)
        self.pq.enqueue(self.o3)
        result = self.pq.dequeue()
        assert result is self.o3, 'mismatch in dequeuing correct value'
Esempio n. 4
0
def main():
    maze = Maze("maze2.png")
    paths = PriorityQueue()

    start = maze.getStart()
    end = maze.getEnd()
    paths.insert(PathHead(start[0], start[1], calcDistance(start, end)))
    current = paths.minimum()

    while paths.size() > 0 and current.getDistance() != 0.0:
        current = paths.extractMin()
        maze.setNodeVisited(current.getCords())
        surroundings = maze.checkSurroundings(current.getCords())
        insertSurroundings(paths, surroundings, current, end)

    solved = maze.getMaze()
    solved = numpy.array(solved)
    img = Image.fromarray(solved.astype('uint8'), 'RGB')
    img.save('solved.png')
Esempio n. 5
0
import priorityqueue
from priorityqueue import PriorityQueue
import numpy as np

pq = PriorityQueue(0, 20)
ints = np.random.randint(1, 100, size=20)
print("Inserting 20 integers into pq: {0}".format(ints))
[pq.insert(i) for i in ints]
print("pq is full: {0}".format(pq.isFull()))
print("pq size: {0}".format(pq.size()))
print("Deleting 20 integers from pq: {0}".format(
    [pq.delMin() for i in range(20)], sep=','))
print("pq is empty: {0}".format(pq.isEmpty()))
print("pq size: {0}".format(pq.size()))
Esempio n. 6
0
import priorityqueue
from priorityqueue import PriorityQueue
import numpy as np

pq = PriorityQueue(0,20)
ints = np.random.randint(1,100, size=20)
print("Inserting 20 integers into pq: {0}".format(ints))
[pq.insert(i) for i in ints]
print("pq is full: {0}".format(pq.isFull()))
print("pq size: {0}".format(pq.size()))
print("Deleting 20 integers from pq: {0}".format([pq.delMin() for i in range(20)], sep=','))
print("pq is empty: {0}".format(pq.isEmpty()))
print("pq size: {0}".format(pq.size()))