コード例 #1
0
 def test_gfx(self):
     board = [[0, 8, 16, 2048], [0, 4, 32, 1024], [8192, 2, 64, 512],
              [4096, 0, 128, 256]]
     size = 4
     gfx = Gfx(size, size, fps=4)
     gfx.draw(board)
     sleep(2)
コード例 #2
0
    def test_gfx(self):
        my_grid = Grid()
        my_gfx = Gfx(fps=4)
        my_agent = Agent()
        my_agent.set_grid(my_grid)
        for i in range(15):
            r = random.randint(0, 2)
            relative_direction = (r - 1) * 90
            my_agent.move(relative_direction)

            my_gfx.draw(my_grid, my_agent)
コード例 #3
0
ファイル: main.py プロジェクト: afcarl/it3105
    def __init__(self):
        arg_parser = argparse.ArgumentParser()
        arg_parser.add_argument('--disable-gfx',
                                nargs='?',
                                dest='disable_gfx',
                                const=True,
                                required=False,
                                default=False)
        arg_parser.add_argument(
            '--print-execution-time',
            nargs='?',
            dest='print_execution_time',
            help='At the end of the run, print the execution time',
            const=True,
            required=False,
            default=False)
        arg_parser.add_argument('--max-depth',
                                dest='max_depth',
                                type=int,
                                choices=[2, 3, 4],
                                required=False,
                                default=3)
        args = arg_parser.parse_args()

        if args.disable_gfx:
            from board_printer import BoardPrinter
            self.gfx = BoardPrinter()
        else:
            from gfx import Gfx
            self.gfx = Gfx(grid_width=self.size,
                           grid_height=self.size,
                           fps=30.0)

        Node.max_depth = args.max_depth

        if args.print_execution_time:
            self.start_time = time.time()

        self.run()

        if args.print_execution_time:
            print "execution time: %s seconds" % (time.time() -
                                                  self.start_time)
コード例 #4
0
    def __init__(self):
        arg_parser = argparse.ArgumentParser()
        arg_parser.add_argument('-i',
                                '--input',
                                dest='filename',
                                type=str,
                                help='The name of the input file',
                                required=True)
        arg_parser.add_argument('--mode',
                                dest='mode',
                                type=str,
                                choices=['astar', 'bfs', 'dfs'],
                                required=False,
                                default="astar")
        arg_parser.add_argument('--fps',
                                dest='fps',
                                type=float,
                                required=False,
                                default=16.0)
        arg_parser.add_argument(
            '--draw-every',
            dest='draw_every',
            help=
            'Use this argument to skip frames when visualizing large and complex problems',
            type=int,
            required=False,
            default=1)
        arg_parser.add_argument('--disable-gfx',
                                nargs='?',
                                dest='disable_gfx',
                                const=True,
                                required=False,
                                default=False)
        arg_parser.add_argument(
            '--print-execution-time',
            nargs='?',
            dest='print_execution_time',
            help=
            'At the end of the run, print the execution time of the A* algorithm. Useful for'
            ' testing the performance of the algorithm while gfx is disabled.',
            const=True,
            required=False,
            default=False)
        arg_parser.add_argument(
            '--sleep-afterwards',
            nargs='?',
            dest='sleep_afterwards',
            help=
            'At the end of the run, sleep for a couple of seconds. This gives you time to'
            ' take a screenshot of the solution, for example',
            const=True,
            required=False,
            default=False)
        args = arg_parser.parse_args()
        self.sleep_afterwards = args.sleep_afterwards

        if args.mode == 'bfs':
            CspNode.H_MULTIPLIER = 0
        elif args.mode == 'dfs':
            CspNode.H_MULTIPLIER = 0
            CspNode.ARC_COST_MULTIPLIER = 0

        f = open(args.filename)
        lines = []
        for line in f:
            lines.append(line.strip())
        f.close()

        num_cols, num_rows, row_segments, col_segments = self.parse_lines(
            lines)

        # initialize constraint network
        self.constraint_network = NgConstraintNetwork(
            num_cols=num_cols,
            num_rows=num_rows,
            row_segments=row_segments,
            col_segments=col_segments)

        if not args.disable_gfx:
            from gfx import Gfx
            self.gfx = Gfx(grid_width=num_cols,
                           grid_height=num_rows,
                           fps=args.fps)

        self.a_star = AStar(
            draw=self.gfx.draw if not args.disable_gfx else lambda _: 0,
            disable_gfx=args.disable_gfx,
            draw_every=args.draw_every,
            print_path=False)

        if args.print_execution_time:
            self.start_time = time.time()

        self.run()

        if args.print_execution_time:
            print "execution time: %s seconds" % (time.time() -
                                                  self.start_time)
コード例 #5
0
    def __init__(self):
        arg_parser = argparse.ArgumentParser()
        arg_parser.add_argument('-i',
                                '--input',
                                dest='filename',
                                type=str,
                                help='The name of the input file',
                                required=True)
        arg_parser.add_argument('--mode',
                                dest='mode',
                                type=str,
                                choices=['astar', 'bfs', 'dfs'],
                                required=False,
                                default="astar")
        arg_parser.add_argument(
            '-k',
            '--num-colors',
            dest='num_colors',
            type=int,
            choices=[2, 3, 4, 5, 6, 7, 8],
            required=True,
        )
        arg_parser.add_argument('--fps',
                                dest='fps',
                                type=float,
                                required=False,
                                default=16.0)
        arg_parser.add_argument(
            '--draw-every',
            dest='draw_every',
            help=
            'Use this argument to skip frames when visualizing large and complex problems',
            type=int,
            required=False,
            default=1)
        arg_parser.add_argument('--disable-gfx',
                                nargs='?',
                                dest='disable_gfx',
                                const=True,
                                required=False,
                                default=False)

        arg_parser.add_argument(
            '--print-execution-time',
            nargs='?',
            dest='print_execution_time',
            help=
            'At the end of the run, print the execution time of the A* algorithm. Useful for'
            ' testing the performance of the algorithm while gfx is disabled.',
            const=True,
            required=False,
            default=False)
        args = arg_parser.parse_args()

        if args.mode == 'bfs':
            VcCspNode.H_MULTIPLIER = 0
        elif args.mode == 'dfs':
            VcCspNode.H_MULTIPLIER = 0
            VcCspNode.ARC_COST_MULTIPLIER = 0

        f = open(args.filename)
        lines = []
        for line in f:
            lines.append(line.strip())
        f.close()

        num_vertices, num_edges, vertices, edges = self.parse_lines(lines)

        # initialize constraint network
        initial_domain = range(args.num_colors)
        self.constraint_network = VertexColorConstraintNetwork(
            vertices=vertices, edges=edges, initial_domain=initial_domain)

        if not args.disable_gfx:
            from gfx import Gfx
            self.gfx = Gfx(fps=args.fps)

        self.a_star = AStar(
            draw=self.gfx.draw if not args.disable_gfx else lambda _: 0,
            disable_gfx=args.disable_gfx,
            draw_every=args.draw_every,
            print_path=False)

        if args.print_execution_time:
            self.start_time = time.time()

        self.run()

        if args.print_execution_time:
            print "execution time: %s seconds" % (time.time() -
                                                  self.start_time)
コード例 #6
0
ファイル: main.py プロジェクト: afcarl/it3105
    def __init__(self):
        """
        Parse command line arguments, read input file, set up board and call run()
        """

        arg_parser = argparse.ArgumentParser()
        arg_parser.add_argument(
            '-i',
            '--input',
            dest='filename',
            type=str,
            help='The name of the input file',
            required=True
        )
        arg_parser.add_argument(
            '--mode',
            dest='mode',
            type=str,
            choices=['astar', 'bfs', 'dfs'],
            required=False,
            default="astar"
        )
        arg_parser.add_argument(
            '--fps',
            dest='fps',
            type=float,
            required=False,
            default=16.0
        )
        arg_parser.add_argument(
            '--draw-every',
            dest='draw_every',
            help='Use this argument to skip frames when visualizing large and complex problems',
            type=int,
            required=False,
            default=1
        )
        arg_parser.add_argument(
            '--disable-gfx',
            nargs='?',
            dest='disable_gfx',
            const=True,
            required=False,
            default=False
        )
        arg_parser.add_argument(
            '--print-path',
            nargs='?',
            dest='print_path',
            help='If a solution is found, print the backtracked nodes that led to the solution',
            const=True,
            required=False,
            default=False
        )
        arg_parser.add_argument(
            '--print-execution-time',
            nargs='?',
            dest='print_execution_time',
            help='At the end of the run, print the execution time of the A* algorithm. Useful for'
                 ' testing the performance of the algorithm while gfx is disabled.',
            const=True,
            required=False,
            default=False
        )
        args = arg_parser.parse_args()

        if args.mode == 'bfs':
            NavNode.H_MULTIPLIER = 0
        elif args.mode == 'dfs':
            NavNode.H_MULTIPLIER = 0
            NavNode.ARC_COST_MULTIPLIER = 0

        f = open(args.filename)
        lines = []
        for line in f:
            lines.append(line.strip())
        f.close()

        self.disable_gfx = args.disable_gfx
        self.print_path = args.print_path
        self.print_execution_time = args.print_execution_time
        self.draw_every = args.draw_every

        dimensions, start, goal, barriers = self.parse_lines(lines)
        self.board = Board(dimensions, start, goal, barriers)
        NavNode.board = self.board

        if not self.disable_gfx:
            from gfx import Gfx
            self.gfx = Gfx(board=self.board, fps=args.fps)

        if self.print_execution_time:
            self.start_time = time.time()
        self.run()
        if self.print_execution_time:
            print "execution time: %s seconds" % (time.time() - self.start_time)