def __init__(self, point_free_cb, line_free_cb, dimensions): """ Construct a randomized roadmap planner. 'point_free_cb' is a function that accepts a point (two-tuple) and outputs a boolean value indicating whether the point is in free space. 'line_free_cb' is a function that accepts two points (the start and the end of a line segment) and outputs a boolen value indicating whether the line segment is free from obstacles. 'dimensions' is a tuple of tuples that define the x and y dimensions of the world, e.g. ((-8, 8), (-8, 8)). It should be used when generating random points. """ self.point_free_cb = point_free_cb self.line_free_cb = line_free_cb self.dimensions = dimensions self.search_algorithm = euclidean() self.number_of_trials = 500 self.search_result = False self.output_path = None self.id_generator = 10000 self.graph = graph()
def __init__(self): self.date = [0,0,0] self.dt = 0.0 self.modules = {} self.unit_types = {} self.building_types = {} self.cities = {} self.areas_id = {} self.units = [] self.groups = {} self.players = {} self.countries = {} self.fights = [] #graph way search self.areas_graph = digraph() self.heuristic = euclidean() self.countries_colors = {None: 0xe2f0e1} self.globalClock = ClockObject.getGlobalClock() self.last_t = self.globalClock.getRealTime() self.dt = 0 self.t = 0
def __init__(self, point_free_cb, line_free_cb, dimensions): """ Construct a randomized roadmap planner. 'point_free_cb' is a function that accepts a point (two-tuple) and outputs a boolean value indicating whether the point is in free space. 'line_free_cb' is a function that accepts two points (the start and the end of a line segment) and outputs a boolen value indicating whether the line segment is free from obstacles. 'dimensions' is a tuple of tuples that define the x and y dimensions of the world, e.g. ((-8, 8), (-8, 8)). It should be used when generating random points. """ rospy.loginfo('Initializing RandomizedRoadmapPlanner') self.point_free_cb = point_free_cb self.line_free_cb = line_free_cb self.dimensions = dimensions self.node_list = [] self.graph = graph() self.heuristic = euclidean() self.node_ids = []
def test_euclidean(self): heuristic = euclidean() heuristic.optimize(self.G) result = pygraph.algorithms.minmax.heuristic_search(self.G, 'A', 'C', heuristic ) assert result == ['A', 'D', 'C']
def heuristic(self): heuristic = euclidean() heuristic.optimize(self.graph) return heuristic
from pygraph.classes.digraph import digraph from pygraph.algorithms.heuristics.chow import chow from pygraph.algorithms.heuristics.euclidean import euclidean from pygraph.algorithms.minmax import * from moga_taskmapping_v2 import * g = digraph() g.add_nodes(['ALU0', 'SE0', 'ALU1', 'SE1']) g.add_node_attribute('ALU0', ('position', (0, 0))) g.add_node_attribute('SE0', ('position', (0, 0))) g.add_node_attribute('ALU1', ('position', (1, 0))) g.add_node_attribute('SE1', ('position', (1, 0))) g.add_edge(('ALU0', 'SE0'), wt=1) g.add_edge(('SE0', 'SE1'), wt=1) g.add_edge(('SE1', 'SE0'), wt=1) g.add_edge(('SE0', 'ALU1'), wt=1) g.add_edge(('ALU1', 'SE1'), wt=1) h = euclidean() h.optimize(g) path = heuristic_search(g, 'ALU0', 'ALU1', h) print(path) path_evaluation(g, path)
def test_euclidean(self): heuristic = euclidean() heuristic.optimize(self.G) result = pygraph.algorithms.minmax.heuristic_search( self.G, 'A', 'C', heuristic) assert result == ['A', 'D', 'C']
def plan(self, point1, point2): """ Plan a path which connects the two given 2D points. The points are represented by tuples of two numbers (x, y). Return a list of tuples where each tuple represents a point in the planned path, the first point is the start point, and the last point is the end point. If the planning algorithm failed the returned list should be empty. """ path = [] #Add start and end self.node_counter += 1 start = self.node_counter self.graph.add_node(self.node_counter, attrs=[('position', (point1[0], point1[1]))]) self.node_counter += 1 end = self.node_counter self.graph.add_node(self.node_counter, attrs=[('position', (point2[0], point2[1]))]) new_node_counter = 0 #Create nodes until a solution is found, limit = self.node_limit while new_node_counter < self.node_limit: #Add 5 new random nodes each iteration for i in range(self.number_nodes): position_x = random.uniform(self.dimensions_x[0], self.dimensions_x[1]) position_y = random.uniform(self.dimensions_y[0], self.dimensions_y[1]) if self.point_free_cb((position_x, position_y)): self.node_counter += 1 new_node_counter += 1 self.graph.add_node(self.node_counter, attrs=[('position', (position_x, position_y))]) #Add edges for nid1, attr in self.graph.node_attr.iteritems(): position1 = attr[0][1] for nid2, attr2 in self.graph.node_attr.iteritems(): position2 = attr2[0][1] distance = self.distance(position1, position2) #Avoid self edges and repetitivity if distance > 0.0 and self.graph.has_edge( (nid1, nid2)) == False: if self.line_free_cb(position1, position2): self.graph.add_edge((nid1, nid2), wt=distance) #A* search try: heuristic = euclidean() heuristic.optimize(self.graph) id_path = heuristic_search(self.graph, start, end, heuristic) for member in id_path: attributes = self.graph.node_attributes(member) path.append(attributes[0][1]) except NodeUnreachable: rospy.logwarn("Path unreachable, not enough nodes") else: return path #If node limit is passed, then no path is found rospy.logwarn("Node limit reached, path is not connected") return path
# tasks_graph = digraph() # tasks_graph.add_nodes(list(range(8))) # tasks_graph.add_edge((0,1)) # tasks_graph.add_edge((1,2)) # tasks_graph.add_edge((1,3)) # tasks_graph.add_edge((3,4)) # tasks_graph.add_edge((4,5)) # tasks_graph.add_edge((4,6)) # tasks_graph.add_edge((5,7)) # tasks_graph.add_edge((6,7)) # tasks, tasks_graph = af_graph() # test_platform_graph = [[0 for i in range(12)] for j in range(8)] # only size required? platform_graph, SE_graph, ALU_graph = generate_cma_graph(PE_row, PE_col) # CMA graph platform_search_graph = euclidean() SE_search_graph = euclidean() ALU_search_graph = euclidean() platform_search_graph.optimize(platform_graph) SE_search_graph.optimize(SE_graph) ALU_search_graph.optimize(ALU_graph) # mapping1 = random_generator(tasks_graph,test_platform_graph) # mapping2 = random_generator(tasks_graph,test_platform_graph) # print(mapping1) # print(mapping2) # val, totval = objectives_eval(tasks_graph,mapping1) # print(val) # print(totval)
def plan(self, point1, point2): """ Plan a path which connects the two given 2D points. The points are represented by tuples of two numbers (x, y). Return a list of tuples where each tuple represents a point in the planned path, the first point is the start point, and the last point is the end point. If the planning algorithm failed the returned list should be empty. """ print "PLANNING METHOD CALLED" if not self.point_free_cb(point1): rospy.logwarn("START POINT UNAVAILIBLE") return [] if not self.point_free_cb(point2): rospy.logwarn("END POINT UNAVAILIBLE") return [] if self.line_free_cb(point1, point2): print "STRAIGHT LINE ROUTE FOUND" return [point1, point2] else: print "EXPANDING MAP" start_id = self.add_node_to_graph(point1, force=True) finish_id = self.add_node_to_graph(point2, force=True) h = euclidean() while True: # No DO..UNTIL loops in python. Condition check below path = [] try: h.optimize(self.graph) path = heuristic_search(self.graph, start_id, finish_id, h) except NodeUnreachable: # add another node new_node_added = False while not new_node_added: randomPoint = (uniform(*self.dimensions[0]), uniform(*self.dimensions[1])) if self.point_free_cb(randomPoint): if self.add_node_to_graph(randomPoint): new_node_added = True # DO..UNTIL emulation this way if len(path) > 0 or self.nodecount >= RandomizedRoadmapPlanner.MAX_NODES_NUM: print "Break loop" break # print path if len(path) > 0: found_path = [] rospy.loginfo("PATH FOUND") for n in path: found_path.append(self.graph.node_attr.get(n)[0][1]) return found_path elif self.nodecount >= RandomizedRoadmapPlanner.MAX_NODES_NUM: rospy.logwarn("MAX NUM NODES ALLOWED EXCEEDED!") rospy.logwarn("PATH NOT FOUND!") return []