Esempio n. 1
0
    def test_4(self):
        gs = GrahamScan()
        gs.add_point(Point(0.0, 0.0))
        gs.add_point(Point(1.0, 1.0))
        gs.add_point(Point(2.0, 2.0))

        res = gs.calculate()

        self.assertTrue(len(res) == 3)
Esempio n. 2
0
 def generateObstacles(self, count, theta):
     obstacles = []
     for _ in range(count):
         obstacle = Polygon(self.size.x, self.size.y, theta,
                            self.random(Point(0, 0), self.size))
         while self.first.intersects(obstacle) or self.final.intersects(
                 obstacle):
             obstacle = Polygon(self.size.x, self.size.y, theta,
                                self.random(Point(0, 0), self.size))
         obstacles.append(obstacle)
     return obstacles
Esempio n. 3
0
 def generateBoundaries(self):
     dimension = min(self.size.x, self.size.y)
     return [
         Polygon(self.width, dimension, 0.0,
                 Point(self.minimum.x, self.maximum.y)),
         Polygon(self.width, dimension, 0.0,
                 Point(self.minimum.x, self.minimum.y - dimension)),
         Polygon(dimension, self.height + dimension * 2.0, 0.0,
                 Point(self.maximum.x, self.minimum.y - dimension)),
         Polygon(
             dimension, self.height + dimension * 2.0, 0.0,
             Point(self.minimum.x - dimension, self.minimum.y - dimension))
     ]
Esempio n. 4
0
    def test_2(self):
        gs = GrahamScan()
        gs.add_point(Point(0.0, 0.0))
        gs.add_point(Point(1.0, 0.0))
        gs.add_point(Point(1.0, 1.0))
        gs.add_point(Point(0.0, 1.0))

        p5 = Point(0.5, 0.5)
        gs.add_point(p5)

        res = gs.calculate()

        self.assertTrue(len(res) == 4)
        self.assertTrue(p5 not in res)
Esempio n. 5
0
 def random(self, offset, size):
     return Point(
         np.random.randint(self.minimum.x + offset.x,
                           self.maximum.x - offset.x - size.x + 1),
         np.random.randint(self.minimum.y + offset.y,
                           self.maximum.y - offset.y - size.y + 1),
     )
Esempio n. 6
0
def individual(grid, interpolation, segments):
    points = [grid.first]

    for s in range(segments):
        nextPoint = grid.random(grid.size, Point(0, 0)) if s < segments-1 else grid.final
        segment = Line(points[-1], nextPoint)
        points.extend(segment.divide(interpolation))

    return points
Esempio n. 7
0
def bezierCurve(points, samples):
    t = np.linspace(0.0, 1.0, samples)

    px = [p.x for p in points]
    py = [p.y for p in points]

    polynomials = [
        bernsteinPolynomial(len(points) - 1, i, t) for i in range(len(points))
    ]

    vx = np.dot(px, polynomials)
    vy = np.dot(py, polynomials)

    return [Point(vx[s], vy[s]) for s in range(samples)]
Esempio n. 8
0
def evolve(population, grid, count, chance):
    children = []
    while len(children) < count:
        parentA = np.random.randint(0, len(population))
        parentB = np.random.randint(0, len(population))
        if parentA != parentB:
            pathA = population[parentA].points
            pathB = population[parentB].points
            crossoverPosition = len(pathA) // 2
            child = pathA[:crossoverPosition] + pathB[crossoverPosition:]
            if np.random.random() <= chance:
                mutationPosition = np.random.randint(0, len(child))
                child[mutationPosition] = grid.random(grid.size, Point(0, 0))
            children.append(child)
    return children
Esempio n. 9
0
    def test_3(self):
        gs = GrahamScan()
        gs.add_point(Point(0.0, 0.0))
        gs.add_point(Point(5.0, 0.0))
        gs.add_point(Point(4.0, -1.0))
        gs.add_point(Point(4.0, 5.0))
        gs.add_point(Point(0.0, 5.0))

        p5 = Point(2.5, 2.5)
        gs.add_point(p5)

        p6 = Point(1.0, 1.0)
        gs.add_point(p6)

        res = gs.calculate()

        self.assertTrue(len(res) == 5)
        self.assertTrue(p5 not in res)
        self.assertTrue(p6 not in res)
Esempio n. 10
0
def parse(filename):
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-v", "--verbose", action="store_true",
        help="enables additional logging with extra information"
    )

    parser.add_argument(
        "-lx", "--leftx", type=int,
        help="x-coordinate of the bottom-left point of the grid"
    )

    parser.add_argument(
        "-ly", "--lefty", type=int,
        help="y-coordinate of the bottom-left point of the grid"
    )

    parser.add_argument(
        "-rx", "--rightx", type=int,
        help="x-coordinate of the top-right point of the grid"
    )

    parser.add_argument(
        "-ry", "--righty", type=int,
        help="y-coordinate of the top-right point of the grid"
    )

    parser.add_argument(
        "-sx", "--startx", type=int,
        help="x-coordinate of the start point of the vehicle"
    )

    parser.add_argument(
        "-sy", "--starty", type=int,
        help="y-coordinate of the start point of the vehicle"
    )

    parser.add_argument(
        "-ex", "--endx", type=int,
        help="x-coordinate of the end point of the vehicle"
    )

    parser.add_argument(
        "-ey", "--endy", type=int,
        help="y-coordinate of the end point of the vehicle"
    )

    parser.add_argument(
        "-ox", "--sizex", type=int,
        help="size of objects along the x-axis"
    )

    parser.add_argument(
        "-oy", "--sizey", type=int,
        help="size of objects along the y-axis"
    )

    parser.add_argument(
        "-oc", "--obstaclecount", type=int,
        help="the number of obstacles in the environment"
    )

    parser.add_argument(
        "-ot", "--obstacletheta", type=int,
        help="the orientation of obstacles (in degrees)"
    )

    if filename == "genetic-algorithm":
        parser.add_argument(
            "-pc", "--populationcount", type=int,
            help="the size of the population"
        )

        parser.add_argument(
            "-in", "--interpolation", type=int,
            help="the number of intermediary points along a line segment"
        )

        parser.add_argument(
            "-ps", "--pathsegments", type=int,
            help="the number of segments in a given path"
        )

        parser.add_argument(
            "-cs", "--curvesamples", type=int,
            help="the number of samples to use when path smoothing"
        )

        parser.add_argument(
            "-mc", "--mutationchance", type=float,
            help="the probability that mutation will occur [0.0, 1.0]"
        )

        parser.add_argument(
            "-ev", "--evolutionmax", type=int,
            help="the number of evolutions to carry out"
        )

    args = parser.parse_args()

    arguments = {
        "verbose": args.verbose
    }

    if args.leftx is not None and args.rightx is not None and args.leftx >= args.rightx:
        raise Exception("x-coordinate of the bottom-left point cannot be greater than or equal to the top-right point")

    if args.lefty is not None and args.righty is not None and args.lefty >= args.righty:
        raise Exception("y-coordinate of the bottom-left point cannot be greater than or equal to the top-right point")

    if args.leftx is not None and args.rightx is not None and args.rightx - args.leftx < 8:
        raise Exception("the width of the grid should not be less than 8 units")

    if args.lefty is not None and args.righty is not None and args.righty - args.lefty < 8:
        raise Exception("the height of the grid should not be less than 8 units")

    arguments["gridMinimum"] = Point(
        args.leftx if args.leftx is not None else (args.rightx - 20.0 if args.rightx is not None else 0.0),
        args.lefty if args.lefty is not None else (args.righty - 20.0 if args.righty is not None else 0.0)
    )

    arguments["gridMaximum"] = Point(
        args.rightx if args.rightx is not None else (args.leftx + 20.0 if args.leftx is not None else 20.0),
        args.righty if args.righty is not None else (args.lefty + 20.0 if args.lefty is not None else 20.0)
    )

    if args.startx is not None and (args.startx < arguments["gridMinimum"].x or args.startx > arguments["gridMaximum"].x):
        raise Exception("x-coordinate of the start point of the vehicle must reside within the grid")

    if args.starty is not None and (args.starty < arguments["gridMinimum"].y or args.starty > arguments["gridMaximum"].y):
        raise Exception("y-coordinate of the start point of the vehicle must reside within the grid")

    if args.endx is not None and (args.endx < arguments["gridMinimum"].x or args.endx > arguments["gridMaximum"].x):
        raise Exception("x-coordinate of the end point of the vehicle must reside within the grid")

    if args.endy is not None and (args.endy < arguments["gridMinimum"].y or args.endy > arguments["gridMaximum"].y):
        raise Exception("y-coordinate of the end point of the vehicle must reside within the grid")

    arguments["vehicleFirst"] = Point(
        args.startx if args.startx is not None else arguments["gridMinimum"].x + 2,
        args.starty if args.starty is not None else arguments["gridMinimum"].y + 2
    )

    arguments["vehicleFinal"] = Point(
        args.endx if args.endx is not None else arguments["gridMaximum"].x - 2,
        args.endy if args.endy is not None else arguments["gridMaximum"].y - 2
    )

    if args.sizex is not None and args.sizex <= 0.0:
        raise Exception("the size of objects along the x-axis should not be less than or equal to zero")

    if args.sizey is not None and args.sizey <= 0.0:
        raise Exception("the size of objects along the y-axis should not be less than or equal to zero")

    arguments["objectSize"] = Point(
        args.sizex if args.sizex is not None else 1.0,
        args.sizey if args.sizey is not None else 1.0
    )

    arguments["obstacleCount"] = args.obstaclecount if args.obstaclecount is not None else 40

    arguments["obstacleTheta"] = args.obstacletheta if args.obstacletheta is not None else 0.0

    if filename == "genetic-algorithm":
        if args.populationcount is not None and args.populationcount < 10:
            raise Exception("the population size should not be less than 10")

        if args.pathsegments is not None and args.pathsegments < 2:
            raise Exception("the value for path segments cannot be less than 2, as that implies a straight line")

        if args.mutationchance is not None and (args.mutationchance < 0.0 or args.mutationchance > 1.0):
            raise Exception("the probability that mutation will occur must be between 0.0 and 1.0 (inclusive)")

        arguments["populationCount"] = args.populationcount if args.populationcount is not None else 80
        arguments["interpolation"] = args.interpolation if args.interpolation is not None else 8
        arguments["pathSegments"] = args.pathsegments if args.pathsegments is not None else 2
        arguments["curveSamples"] = args.curvesamples if args.curvesamples is not None else 16
        arguments["mutationChance"] = args.mutationchance if args.mutationchance is not None else 0.04
        arguments["evolutionMax"] = args.evolutionmax if args.evolutionmax is not None else 10

    return arguments
Esempio n. 11
0
    def test_internals3(self):
        p1 = Point(1.0, 1.0)
        p2 = Point(1.0, 1.0)

        self.assertTrue(not p1 != p2)
        self.assertTrue(p1 == p2)
Esempio n. 12
0
    def test_point4(self):
        p1 = Point(1.0, 1.0)
        p2 = Point(1.0, 0.0)

        self.assertTrue(p2.angle_x(p1) - 90.0 <= self.epsilon)
Esempio n. 13
0
    def test_point(self):
        p1 = Point(1.0, 1.0)
        p2 = Point(0.0, 0.0)

        self.assertTrue(p1.angle_x(p2) - 45.0 <= self.epsilon)