Пример #1
0
 def path(self, s_start=None):
     '''After ComputeShortestPath()
     returns, one can then follow a shortest path from x_init to
     x_goal by always moving from the current vertex s, starting
     at x_init. , to any successor s' that minimizes cBest(s,s') + g(s')
     until x_goal is reached (ties can be broken arbitrarily).'''
     path = []
     s_goal = self.xt
     s = self.x0
     ind = 0
     while getDist(s, s_goal) > self.env.resolution:
         if s == self.x0:
             children = [
                 i for i in self.CLOSED
                 if getDist(s, i) <= self.env.resolution * np.sqrt(3)
             ]
         else:
             children = list(self.CHILDREN[s])
         snext = children[np.argmin(
             [self.getcost(s, s_p) + self.getg(s_p) for s_p in children])]
         path.append([s, snext])
         s = snext
         if ind > 100:
             break
         ind += 1
     return path
Пример #2
0
    def main(self):
        s_last = self.x0
        s_start = self.x0
        print('first run ...')
        self.ComputeShortestPath()
        self.Path = self.path()
        self.done = True
        visualization(self)
        plt.pause(0.5)
        # plt.show()
        # change the environment
        print('running with map update ...')
        for i in range(100):
            range_changed1 = self.env.move_block(a=[0, 0, -0.1],
                                                 s=0.5,
                                                 block_to_move=0,
                                                 mode='translation')
            range_changed2 = self.env.move_block(a=[0.1, 0, 0],
                                                 s=0.5,
                                                 block_to_move=1,
                                                 mode='translation')
            range_changed3 = self.env.move_block(a=[0, 0.1, 0],
                                                 s=0.5,
                                                 block_to_move=2,
                                                 mode='translation')
            #range_changed = self.env.move_block(a=[0.1, 0, 0], s=0.5, block_to_move=1, mode='translation')
            #   update the edge cost of c(u,v)
            CHANGED1 = self.updatecost(range_changed1)
            CHANGED2 = self.updatecost(range_changed2)
            CHANGED3 = self.updatecost(range_changed3)
            CHANGED2 = CHANGED2.union(CHANGED1)
            CHANGED = CHANGED3.union(CHANGED2)
            while getDist(s_start, self.xt) > 2 * self.env.resolution:
                if s_start == self.x0:
                    children = [
                        i for i in self.CLOSED
                        if getDist(s_start, i) <= self.env.resolution *
                        np.sqrt(3)
                    ]
                else:
                    children = list(self.CHILDREN[s_start])
                s_start = children[np.argmin([
                    cost(self, s_start, s_p) + self.g[s_p] for s_p in children
                ])]

                #   for all directed edges (u,v) with changed costs
                if CHANGED:
                    self.km = self.km + heuristic_fun(self, s_start, s_last)
                    for u in CHANGED:
                        self.UpdateVertex(u)
                    s_last = s_start
                    self.ComputeShortestPath()
            self.Path = self.path()
            visualization(self)
        plt.show()
Пример #3
0
 def isCollide(self, x, child):
     ray, dist = getRay(x, child), getDist(x, child)
     if not isinbound(self.env.boundary, child):
         return True, dist
     for i in self.env.AABB_pyrr:
         shot = pyrr.geometric_tests.ray_intersect_aabb(ray, i)
         if shot is not None:
             dist_wall = getDist(x, shot)
             if dist_wall <= dist:  # collide
                 return True, dist
     for i in self.env.balls:
         if isinball(i, child):
             return True, dist
         shot = pyrr.geometric_tests.ray_intersect_sphere(ray, i)
         if shot != []:
             dists_ball = [getDist(x, j) for j in shot]
             if all(dists_ball <= dist):  # collide
                 return True, dist
     return False, dist
Пример #4
0
 def UpdateVertex(self, u):
     # if still in the hunt
     if not getDist(self.xt,
                    u) <= self.env.resolution:  # originally: u != s_goal
         self.rhs[u] = min([
             self.getcost(s, u) + self.getg(s) for s in self.getchildren(u)
         ])
     # if u is in OPEN, remove it
     self.OPEN.check_remove(u)
     # if rhs(u) not equal to g(u)
     if self.getg(u) != self.getrhs(u):
         self.OPEN.put(u, self.CalculateKey(u))
Пример #5
0
    def run(self, N=None):
        xt = self.xt
        xi = self.x0
        while self.OPEN:  # while xt not reached and open is not empty
            xi = self.OPEN.get()
            if xi not in self.CLOSED:
                self.V.append(np.array(xi))
            self.CLOSED.add(xi)  # add the point in CLOSED set
            if getDist(xi, xt) < self.env.resolution:
                break
            # visualization(self)
            for xj in children(self, xi):
                # if xj not in self.CLOSED:
                if xj not in self.g:
                    self.g[xj] = np.inf
                else:
                    pass
                a = self.g[xi] + cost(self, xi, xj)
                if a < self.g[xj]:
                    self.g[xj] = a
                    self.Parent[xj] = xi
                    # if (a, xj) in self.OPEN.enumerate():
                    # update priority of xj
                    self.OPEN.put(xj, a + 1 * heuristic_fun(self, xj))
                    # else:
                    # add xj in to OPEN set
                    # self.OPEN.put(xj, a + 1 * heuristic_fun(self, xj))
            # For specified expanded nodes, used primarily in LRTA*
            if N:
                if len(self.CLOSED) % N == 0:
                    break
            if self.ind % 100 == 0:
                print('number node expanded = ' + str(len(self.V)))
            self.ind += 1

        self.lastpoint = xi
        # if the path finding is finished
        if self.lastpoint in self.CLOSED:
            self.done = True
            self.Path = self.path()
            if N is None:
                #visualization(self)
                plt.show()
            return True

        return False
Пример #6
0
    def ComputeorImprovePath(self):
        while self.OPEN.top_key() < self.key(self.x0, self.epsilon) or self.rhs[self.x0] != self.g[self.x0]:
            s = self.OPEN.get()

            if getDist(s, tuple(self.env.start)) < self.env.resolution:
                break

            if self.g[s] > self.rhs[s]:
                self.g[s] = self.rhs[s]
                self.CLOSED.add(s)
                self.V.add(s)
                for s_p in self.getchildren(s):
                    self.UpdateState(s_p)
            else:
                self.g[s] = np.inf
                self.UpdateState(s)
                for s_p in self.getchildren(s):
                    self.UpdateState(s_p)
            self.ind += 1
Пример #7
0
 def ComputeShortestPath(self):
     while self.OPEN.top_key() < self.CalculateKey(self.x0) or self.getrhs(
             self.x0) != self.getg(self.x0):
         kold = self.OPEN.top_key()
         u = self.OPEN.get()
         self.V.add(u)
         self.CLOSED.add(u)
         if getDist(self.x0, u) <= self.env.resolution:
             break
         # visualization(self)
         if kold < self.CalculateKey(u):
             self.OPEN.put(u, self.CalculateKey(u))
         if self.getg(u) > self.getrhs(u):
             self.g[u] = self.rhs[u]
         else:
             self.g[u] = np.inf
             self.UpdateVertex(u)
         for s in self.getchildren(u):
             self.UpdateVertex(s)
         self.ind += 1
Пример #8
0
 def ComputeShortestPath(self):
     while self.OPEN.top_key() < self.CalculateKey(self.x0) or self.getrhs(
             self.x0) != self.getg(self.x0):
         kold = self.OPEN.top_key()
         u = self.OPEN.get()
         self.V.add(u)
         self.CLOSED.add(u)
         if not self.done:  # first time running, we need to stop on this condition
             if getDist(self.x0, u) < 1 * self.env.resolution:
                 self.x0 = u
                 break
         if kold < self.CalculateKey(u):
             self.OPEN.put(u, self.CalculateKey(u))
         if self.getg(u) > self.getrhs(u):
             self.g[u] = self.rhs[u]
         else:
             self.g[u] = np.inf
             self.UpdateVertex(u)
         for s in self.getchildren(u):
             self.UpdateVertex(s)
         # visualization(self)
         self.ind += 1
Пример #9
0
    def main(self):
        s_last = self.x0
        print('first run ...')
        self.ComputeShortestPath()
        self.Path = self.path()
        self.done = True
        visualization(self)
        plt.pause(0.5)
        # plt.show()
        print('running with map update ...')
        t = 0  # count time
        ischanged = False
        self.V = set()
        while getDist(self.x0, self.xt) > 2 * self.env.resolution:
            #---------------------------------- at specific times, the environment is changed and Cost is updated
            if t % 2 == 0:
                new0, old0 = self.env.move_block(a=[-0.1, 0, -0.2],
                                                 s=0.5,
                                                 block_to_move=1,
                                                 mode='translation')
                new1, old1 = self.env.move_block(a=[0, 0, -0.2],
                                                 s=0.5,
                                                 block_to_move=0,
                                                 mode='translation')
                new2, old2 = self.env.move_block(theta=[0, 0, 0.1 * t],
                                                 mode='rotation')
                #new2,old2 = self.env.move_block(a=[-0.3, 0, -0.1], s=0.5, block_to_move=1, mode='translation')
                ischanged = True
                self.Path = []
            #----------------------------------- traverse the route as originally planned
            if t == 0:
                children_new = [
                    i for i in self.CLOSED
                    if getDist(self.x0, i) <= self.env.resolution * np.sqrt(3)
                ]
            else:
                children_new = list(children(self, self.x0))
            self.x0 = children_new[np.argmin([
                self.getcost(self.x0, s_p) + self.getg(s_p)
                for s_p in children_new
            ])]
            # TODO add the moving robot position codes
            self.env.start = self.x0
            # ---------------------------------- if any Cost changed, update km, reset slast,
            #                                    for all directed edgees (u,v) with  chaged edge costs,
            #                                    update the edge Cost cBest(u,v) and update vertex u. then replan
            if ischanged:
                self.km += heuristic_fun(self, self.x0, s_last)
                s_last = self.x0
                CHANGED = self.updatecost(True, new0, old0)
                CHANGED1 = self.updatecost(True, new1, old1)
                CHANGED2 = self.updatecost(True, new2, old2, mode='obb')
                CHANGED = CHANGED.union(CHANGED1, CHANGED2)
                # self.V = set()
                for u in CHANGED:
                    self.UpdateVertex(u)
                self.ComputeShortestPath()

                ischanged = False
            self.Path = self.path(self.x0)
            visualization(self)
            t += 1
        plt.show()