Пример #1
0
def findPath(gdict, fig, ax, limits, startPoint, endPoint, line, type):
	'''findPath
		Finds the shortest path with the algorithm specified by 'type'.

		calls:
			Helper.findClosestNode()	finds the valid node closest to specified start/end points
			PathOptimizer.*()		path optimization algorithm

		args:
			gdict 		 		edge graph with distances
			fig					a figure
			ax					an axis
			limits				the limits of the bounding box
			startPoint			starting point of the path
			endPoint				end point of the path
			line					2d matplotlib line object
			type					algorithm type, accepts 'd[ijkstra]' or 'a[star]'
		return:
			None

	'''

	#The big ugly function gets the keys into a list of tuples [(1,2),(3,4),...]
	r1 = Helper.findClosestNode(list(zip(*zip(*gdict.keys()))), startPoint)
	r2 = Helper.findClosestNode(list(zip(*zip(*gdict.keys()))), endPoint)

	sys.setrecursionlimit(1500)

	try:
		t1 = time.time()
		if (type.lower().startswith('a')):
			sp = PathOptimization.AStar(gdict, r1, r2)
		elif (type.lower().startswith('d')):
			sp = PathOptimization.dijkstra(gdict, r1, r2)
		pass
		t2 = time.time()

		print(sp[1])
		print(r1)
		print(r2)

		pathlength = sp[0]
		foundPath = sp[1]
		foundPath.insert(0,startPoint)
		foundPath.append(endPoint)

		print(foundPath)

		print("Time taken: {0}s".format(t2 - t1))

		# sp contains sp[0] - the path length, sp[1] the nodes taken; we set line data to the nodes
		line.set_xdata([p[0] for p in foundPath])
		line.set_ydata([p[1] for p in foundPath])
	except Exception as e:
		print("Not a valid path optimizer!")
		print("We will still return though.")
		print(str(e))
	pass
Пример #2
0
def plotPathWithResults(pack,pdict,startPoint,endPoint,line=None):
	'''plotPathWithResults
		Uses results from findAllPaths() to plot the shortest path between two points

		args:
			pack			needed for the figure axis, returned by findAllPaths()
			pdict		the primary results from findAllPaths(); hashtable of all paths
			startPoint	starting point of your path
			endPoint		end point of your path
		
		kwargs:
			line			a line object returned by this method. If you want subsequent calls to 
						this function to overwrite the last found paths then input the line 
						object returned by this method.

		return:
			line			the line object created by this method. Used to overwrite past lines
						in subsequent calls.
	
	'''

	if not line:
		line = lines.Line2D([], [], lw=2, c='red')
		ax = pack[2]
		ax.add_line(line)
	pass

	r1 = Helper.findClosestNode(list(zip(*zip(*pdict.keys()))), startPoint)
	r2 = Helper.findClosestNode(list(zip(*zip(*pdict.keys()))), endPoint)

	foundPath = []

	if r1 != r2:
		foundPath = pdict[r1][r2]
	pass

	foundPath.insert(0,startPoint)
	foundPath.append(endPoint)

	line.set_xdata([p[0] for p in foundPath])
	line.set_ydata([p[1] for p in foundPath])

	return line