def super_path(function, args, params): N = int(params['N']) if WIDE: g = make_grid(N * 3, N) else: g = make_grid(N, N) fn_params = {} p_params = {} for param, value in params.iteritems(): if param in args: fn_params[param] = value for (i, j) in g: if WIDE: g[(i, j)] = function(i, j, dx=-3 * N / 2, dy=-N / 2, **fn_params) else: g[(i, j)] = function(i, j, dx=-N / 2, dy=-N / 2, **fn_params) radius = N / 2 - N * FRACTION angle = params.get('angle', 0) s_angle = radians(angle) e_angle = radians(angle + 180) sx = int(N / 2 - cos(s_angle) * radius) sy = int(N / 2 + sin(s_angle) * radius) if WIDE: ex = 3 * N - N * FRACTION - 1 else: ex = int(N / 2 - cos(e_angle) * radius) ey = int(N / 2 + sin(e_angle) * radius) (score, path) = djikstra(g, (sx, sy), (ex, ey), constant=params['P'], neighbors=params['eight']) return g, score, path
def super_path(function, args, params): N = int( params['N'] ) if WIDE: g = make_grid(N*3, N) else: g = make_grid(N,N) fn_params = {} p_params = {} for param, value in params.iteritems(): if param in args: fn_params[param] = value for (i,j) in g: if WIDE: g[ (i,j) ] = function(i,j,dx=-3*N/2, dy=-N/2, **fn_params) else: g[ (i,j) ] = function(i,j,dx=-N/2, dy=-N/2, **fn_params) radius = N/2-N*FRACTION angle = params.get('angle', 0) s_angle = radians(angle) e_angle = radians(angle + 180) sx = int(N/2 - cos( s_angle ) * radius) sy = int(N/2 + sin( s_angle ) * radius) if WIDE: ex = 3*N-N*FRACTION-1 else: ex = int(N/2 - cos( e_angle) * radius) ey = int(N/2 + sin( e_angle ) * radius) (score, path) = djikstra(g, (sx,sy), (ex,ey), constant=params['P'], neighbors=params['eight']) return g, score, path
def test_djikstra(): answers = [float("Inf"), 0, 2, 3, 4, 6] source = 1 n = 5 graph = { 1: [(2, 2), (3, 4)], 2: [(3, 1), (4, 2)], 3: [(5, 4)], 4: [(5, 2)], 5: [] } shortest_paths = djikstra(graph, n, source) for i in range(n + 1): assert shortest_paths[i] == answers[i]
def test_multiple_poly_djikstra(self): robot = Object(Point(20, 40), [Polygon([Point(0, 0), Point(30, 30), Point(10, -20)]), Polygon([Point(0, 0), Point(10, -20), Point(10, -30), Point(0, -30)])]) obs = Object(Point(20,20), [Polygon([Point(100, 100), Point(100, 200), Point(300, 200), Point(300, 100)])]) cspace_obj = obs.cspace_object(robot) goal = Point(300, 300) world = World(robot, [obs], goal, self.window) world.build_visibility_graph() graph = world.graph_to_weighted_graph() (cost, edges) = djikstra(graph, robot.ref_point, goal) self.assertAlmostEqual(cost, 424.94202022) if DRAW: world.djikstra() world.display_graph()
def test_djikstra(self): robot = Object(Point(20, 40), [Polygon([Point(0, 0), Point(30, 30), Point(10, -20)])]) obs = Object(Point(20,20), [Polygon([Point(100, 100), Point(100, 200), Point(300, 200), Point(300, 100)])]) goal = Point(300, 300) cspace_obj = obs.cspace_object(robot) world = World(robot, [obs], goal) world.build_visibility_graph() graph = world.graph_to_weighted_graph() (cost, edges) = djikstra(graph, robot.ref_point, goal) if DRAW == True: world.display_graph() for e in edges: e.draw(self.window, color='green') raw_input('Press Enter to close window') expected = Object(Point(20,20), [Polygon([Point(70 , 70), Point(270 , 70) , Point(300 , 100) , Point(300 , 200) , Point(290 , 220) , Point(90 , 220) , Point(70 , 170) , Point(70 , 70)])]) self.assertEqual(cspace_obj, expected)
def super_path(function, args, params): N = int( params['N'] ) if WIDE: g = make_grid(N*3, N) else: g = make_grid(N,N) fn_params = {} p_params = {} for param, value in params.iteritems(): if param in args: fn_params[param] = value angle = params.get('angle', 0) sx, sy = fractional_coordinates(N, params.get('start', .1), angle) ex, ey = fractional_coordinates(N, params.get('goal', .1), angle+180) dx, dy = fractional_coordinates(N, params.get('obs', 0.0), angle) for (i,j) in g: g[ (i,j) ] = function(i,j,dx=-dx, dy=-dy, **fn_params) (score, path) = djikstra(g, (sx,sy), (ex,ey), constant=params['P'], neighbors=params['eight']) return g, score, path
def super_path(function, args, params): N = int(params['N']) if WIDE: g = make_grid(N * 3, N) else: g = make_grid(N, N) fn_params = {} p_params = {} for param, value in params.iteritems(): if param in args: fn_params[param] = value angle = params.get('angle', 0) sx, sy = fractional_coordinates(N, params.get('start', .1), angle) ex, ey = fractional_coordinates(N, params.get('goal', .1), angle + 180) dx, dy = fractional_coordinates(N, params.get('obs', 0.0), angle) for (i, j) in g: g[(i, j)] = function(i, j, dx=-dx, dy=-dy, **fn_params) (score, path) = djikstra(g, (sx, sy), (ex, ey), constant=params['P'], neighbors=params['eight']) return g, score, path
from parser import parse args = parse() N = args.size fne, argmap = args.fmap g = make_grid(N, N) xs = range(1,100) ys = [] for x in xs: A = x for (i,j) in g: g[ (i,j) ] = gaussian(i,j, dy=-N/2, varx=varx, vary=vary, A=x) c,p = djikstra(g, (0,0), (0,N-1), constant=path_constant) m = max([a[0] for a in p]) print "%d\t%d"%(x, m) ys.append(m) import pylab pylab.plot(xs,ys) pylab.show() g = make_grid(N, N) for (i,j) in g: g[ (i,j) ] = fne(i,j,dx=-N/2, dy=-N/2) plan(g, (N*3/4,0), (N/4,N-1), constant=args.pathconstant, **{})
for (weight_migration, weight_overload) in costs: i = 0 print("Migration Weight:", weight_migration, "Overload Weight:", weight_overload, " : ") for (edges, vertices) in graphs: currentEdges = []; currentVertices = [] currentEdges = deepcopy(edges) currentVertices = deepcopy(vertices) nbVertices = len(edges) #start = input('Start cell : (From 0 to ' + str(nbVertices-1) + '): ') #target = input('End cell : (From 0 to ' + str(nbVertices-1) + '): ') start = 0 target = 67 previous = [] previous, probabilities = djikstra(currentVertices, currentEdges, start, weight_migration, weight_overload) path, distance, nbMigrations, nbOverloadedCells = shortest(currentEdges, previous, start, target, weight_migration, weight_overload, probabilities) print("Graph:", i , "Path:", path, "Distance:", distance, "Nb Migrations:", nbMigrations, "Nb Overloaded cells:", nbOverloadedCells) i = i + 1 #print(probabilities) """ nbVertices = len(edges_6) start = input('Start cell : (From 0 to ' + str(nbVertices-1) + '): ') target = input('End cell : (From 0 to ' + str(nbVertices-1) + '): ') start = int(start) target = int(target) previous = [] previous = djikstra(vertices_6, edges_6, start)
print(f"{m} edges in format (source, target, cost)") for _ in range(m): edge = list(map(float, input().split())) if int(edge[0]) in vertices and int(edge[1]) in vertices: graph[int(edge[0]), int(edge[1])] = edge[2] else: print(f"Edge coordinates should be from range[1, {n}]") sys.exit(1) source = int(input("source:")) #uruchomienie algorytmu start = time.time() distance, predecessor = djikstra(graph, source, n) end = time.time() print("\ntarget \t distance") for i in range(n): print(f"{i+1} \t {distance[i+1]}") print(f"\nTime: {(end-start)/1000}ms", file=sys.stderr) print("\nPaths:", file=sys.stderr) for i in range(n): target = i + 1 print(f"target: {target}", file=sys.stderr) while predecessor[target] > 0: print( f"\t({predecessor[target]}, {target}) = {graph[predecessor[target], target]}",
def djikstra(self, color='green'): graph = self.graph_to_weighted_graph() (cost, edges) = djikstra(graph, self.robot.ref_point, self.goal) for e in edges: e.draw(self.window, color=color)
start_needed = True while start_needed == True: startx, starty = input("Enter the start position 'x y':\n").split() startx = int(startx) starty = int(starty) if startx > mazewidth or starty > mazeheight: print( "That start position is outside of the maze. The maze you selected has dimensions (" + str(mazewidth) + "," + str(mazeheight) + "). Try again.") else: start_needed = False goal_needed = True while goal_needed == True: goalx, goaly = input("Enter the goal position 'x y':\n").split() goalx = int(goalx) goaly = int(goaly) if goalx > mazewidth or goaly > mazeheight: print( "That goal position is outside of the maze. The maze you selected has dimensions (" + str(mazewidth) + "," + str(mazeheight) + "). Try again.") elif goalx == startx and goaly == starty: print( "That goal is the same as the start position! Try something harder." ) else: goal_needed = False djikstra(robot_type, mazetype, startx, starty, goalx, goaly, diameter, clearance)