예제 #1
0
    def run(self, Reversed = True, xrobot = None):
        if Reversed:
            if xrobot is None:
                self.x0 = tuple(self.env.goal)
                self.xt = tuple(self.env.start)
            else:
                self.x0 = tuple(self.env.goal)
                self.xt = xrobot
            xnew = self.env.goal
        else:
            xnew = self.env.start
        self.V.append(self.x0)
        while self.ind < self.maxiter:
            xrand = sampleFree(self)
            xnearest = nearest(self, xrand)
            xnew = steer(self, xnearest, xrand)
            collide, _ = isCollide(self, xnearest, xnew)
            if not collide:
                self.V.append(xnew)  # add point
                if self.Flag is not None:
                    self.Flag[xnew] = 'Valid'
                self.wireup(xnew, xnearest)

                if getDist(xnew, self.xt) <= self.stepsize:
                    self.wireup(self.xt, xnew)
                    self.Path, D = path(self)
                    print('Total distance = ' + str(D))
                    if self.Flag is not None:
                        self.Flag[self.xt] = 'Valid'
                    break
                # visualization(self)
                self.i += 1
            self.ind += 1
예제 #2
0
    def run(self):
        self.V.append(tuple(self.env.start))
        self.ind = 0
        self.fig = plt.figure(figsize=(10, 8))
        xnew = self.env.start
        while self.ind < self.maxiter and getDist(
                xnew, self.env.goal) > self.stepsize:
            xrand = sampleFree(self)
            xnearest = nearest(self, xrand)
            xnew = steer(self, xnearest, xrand)
            collide, _ = isCollide(self, xnearest, xnew)
            if not collide:
                self.V.append(xnew)  # add point
                self.wireup(xnew, xnearest)

                if getDist(xnew, self.env.goal) <= self.stepsize:
                    goal = tuple(self.env.goal)
                    self.wireup(goal, xnew)
                    self.Path, D = path(self)
                    print('Total distance = ' + str(D))
                # visualization(self)
                self.i += 1
            self.ind += 1
            # if the goal is really reached

        self.done = True
        visualization(self)
        plt.show()
예제 #3
0
 def run(self):
     xnew = self.x0
     print('start rrt*... ')
     self.fig = plt.figure(figsize=(10, 8))
     while self.ind < self.maxiter:
         xrand = sampleFree(self)
         xnearest = nearest(self, xrand)
         xnew, dist = steer(self, xnearest, xrand)
         collide, _ = isCollide(self, xnearest, xnew, dist=dist)
         if not collide:
             Xnear = near(self, xnew)
             self.V.append(xnew)  # add point
             # visualization(self)
             # minimal path and minimal cost
             xmin, cmin = xnearest, cost(self, xnearest) + getDist(
                 xnearest, xnew)
             # connecting along minimal cost path
             Collide = []
             for xnear in Xnear:
                 xnear = tuple(xnear)
                 c1 = cost(self, xnear) + getDist(xnew, xnear)
                 collide, _ = isCollide(self, xnew, xnear)
                 Collide.append(collide)
                 if not collide and c1 < cmin:
                     xmin, cmin = xnear, c1
             self.wireup(xnew, xmin)
             # rewire
             for i in range(len(Xnear)):
                 collide = Collide[i]
                 xnear = tuple(Xnear[i])
                 c2 = cost(self, xnew) + getDist(xnew, xnear)
                 if not collide and c2 < cost(self, xnear):
                     # self.removewire(xnear)
                     self.wireup(xnear, xnew)
             self.i += 1
         self.ind += 1
     # max sample reached
     self.reached()
     print('time used = ' + str(time.time() - starttime))
     print('Total distance = ' + str(self.D))
     visualization(self)
     plt.show()
예제 #4
0
 def run(self):
     self.V.append(self.env.start)
     self.ind = 0
     self.fig = plt.figure(figsize=(10, 8))
     xnew = self.env.start
     while self.ind < self.maxiter and getDist(xnew, self.env.goal) > 1:
         xrand = sampleFree(self)
         xnearest = nearest(self, xrand)
         xnew = steer(self, xnearest, xrand)
         if not isCollide(self, xnearest, xnew):
             self.V.append(xnew)  # add point
             self.wireup(xnew, xnearest)
             visualization(self)
             self.i += 1
         self.ind += 1
         if getDist(xnew, self.env.goal) <= 1:
             self.wireup(self.env.goal, xnew)
             self.Path, D = path(self)
             print('Total distance = ' + str(D))
     self.done = True
     visualization(self)
     plt.show()
예제 #5
0
 def run(self):
     self.V.append(self.env.start)
     self.ind = 0
     xnew = self.env.start
     print('start rrt*... ')
     self.fig = plt.figure(figsize=(10, 8))
     while self.ind < self.maxiter:
         xrand = sampleFree(self)
         xnearest = nearest(self, xrand)
         xnew = steer(self, xnearest, xrand)
         if not isCollide(self, xnearest, xnew):
             Xnear = near(self, xnew)
             self.V.append(xnew)  # add point
             visualization(self)
             # minimal path and minimal cost
             xmin, cmin = xnearest, cost(self, xnearest) + getDist(
                 xnearest, xnew)
             # connecting along minimal cost path
             for xnear in Xnear:
                 c1 = cost(self, xnear) + getDist(xnew, xnear)
                 if not isCollide(self, xnew, xnear) and c1 < cmin:
                     xmin, cmin = xnear, c1
             self.wireup(xnew, xmin)
             # rewire
             for xnear in Xnear:
                 c2 = cost(self, xnew) + getDist(xnew, xnear)
                 if not isCollide(self, xnew,
                                  xnear) and c2 < cost(self, xnear):
                     self.removewire(xnear)
                     self.wireup(xnear, xnew)
             self.i += 1
         self.ind += 1
     # max sample reached
     self.reached()
     print('time used = ' + str(time.time() - starttime))
     print('Total distance = ' + str(self.D))
     visualization(self)
     plt.show()
예제 #6
0
    def run(self):
        self.V.append(self.x0)
        while self.ind < self.maxiter:
            xrand = sampleFree(self)
            xnearest = nearest(self, xrand)
            xnew, dist = steer(self, xnearest, xrand)
            collide, _ = isCollide(self, xnearest, xnew, dist=dist)
            if not collide:
                self.V.append(xnew)  # add point
                self.wireup(xnew, xnearest)

                if getDist(xnew, self.xt) <= self.stepsize:
                    self.wireup(self.xt, xnew)
                    self.Path, D = path(self)
                    print('Total distance = ' + str(D))
                    break
                # visualization(self)
                self.i += 1
            self.ind += 1
            # if the goal is really reached

        self.done = True
        visualization(self)
        plt.show()
예제 #7
0
 def Nearest(self, target):
     # TODO use kdTree to speed up search
     return nearest(self, target, isset=True)
예제 #8
0
    def Informed_rrt(self):
        self.V = [self.xstart]
        self.E = set()
        self.Xsoln = set()
        self.T = (self.V, self.E)

        c = 1
        while self.ind <= self.N:
            print(self.ind)
            self.visualization()
            # print(self.i)
            if len(self.Xsoln) == 0:
                cbest = np.inf
            else:
                cbest = min({self.cost(xsln) for xsln in self.Xsoln})
            xrand = self.Sample(self.xstart, self.xgoal, cbest)
            xnearest = nearest(self, xrand)
            xnew, dist = steer(self, xnearest, xrand)
            # print(xnew)
            collide, _ = isCollide(self, xnearest, xnew, dist=dist)
            if not collide:
                self.V.append(xnew)
                Xnear = near(self, xnew)
                xmin = xnearest
                cmin = self.cost(xmin) + c * self.line(xnearest, xnew)
                for xnear in Xnear:
                    xnear = tuple(xnear)
                    cnew = self.cost(xnear) + c * self.line(xnear, xnew)
                    if cnew < cmin:
                        collide, _ = isCollide(self, xnear, xnew)
                        if not collide:
                            xmin = xnear
                            cmin = cnew
                self.E.add((xmin, xnew))
                self.Parent[xnew] = xmin

                for xnear in Xnear:
                    xnear = tuple(xnear)
                    cnear = self.cost(xnear)
                    cnew = self.cost(xnew) + c * self.line(xnew, xnear)
                    # rewire
                    if cnew < cnear:
                        collide, _ = isCollide(self, xnew, xnear)
                        if not collide:
                            xparent = self.Parent[xnear]
                            self.E.difference_update((xparent, xnear))
                            self.E.add((xnew, xnear))
                            self.Parent[xnear] = xnew
                self.i += 1
                if self.InGoalRegion(xnew):
                    print('reached')
                    self.done = True
                    self.Parent[self.xgoal] = xnew
                    self.Path, _ = path(self)
                    self.Xsoln.add(xnew)
            # update path
            if self.done:
                self.Path, _ = path(self, Path=[])
            self.ind += 1
        # return tree
        return self.T