当前位置: 首页>>代码示例>>Python>>正文


Python graph.AdjacencyList类代码示例

本文整理汇总了Python中pelita.graph.AdjacencyList的典型用法代码示例。如果您正苦于以下问题:Python AdjacencyList类的具体用法?Python AdjacencyList怎么用?Python AdjacencyList使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了AdjacencyList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_pos_within

    def test_pos_within(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())
        free = set(pos for pos, val in universe.maze.items() if not val)

        self.assertFalse((0, 0) in al)
        self.assertRaises(NoPathException, al.pos_within, (0, 0), 0)
        self.assertFalse((6, 2) in al)
        self.assertRaises(NoPathException, al.pos_within, (6, 2), 0)

        self.assertTrue((1, 1) in al)
        self.assertEqual(set([(1, 1)]), al.pos_within((1, 1), 0))
        target = set([(1, 1), (1, 2), (1,3), (2, 3), (3, 3), (3, 3)])
        self.assertEqual(target, al.pos_within((1, 1), 5))
        # assuming a_star is working properly
        for pos in target:
            self.assertTrue(len(al.a_star((1, 1), pos)) < 5)
        for pos in free.difference(target):
            self.assertTrue(len(al.a_star((1, 1), pos)) >= 5)
开发者ID:pombredanne,项目名称:pelita,代码行数:25,代码来源:test_graph.py

示例2: test_bfs_to_self

 def test_bfs_to_self(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     assert [] == al.bfs((1,1), [(1, 1), (2, 1)])
开发者ID:,项目名称:,代码行数:8,代码来源:

示例3: test_bfs_to_self

 def test_bfs_to_self(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = create_CTFUniverse(test_layout, 2)
     al = AdjacencyList(universe)
     self.assertEqual([], al.bfs((1,1), [(1, 1), (2, 1)]))
开发者ID:FrancescAlted,项目名称:pelita,代码行数:8,代码来源:test_graph.py

示例4: test_pos_within

    def test_pos_within(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
        universe = CTFUniverse.create(test_layout, 4)
        al = AdjacencyList(universe.free_positions())
        free = {pos for pos, val in universe.maze.items() if not val}

        assert not ((0, 0) in al)
        with pytest.raises(NoPathException):
            al.pos_within((0, 0), 0)
        assert not ((6, 2) in al)
        with pytest.raises(NoPathException):
            al.pos_within((6, 2), 0)

        assert (1, 1) in al
        unittest.TestCase().assertCountEqual([(1, 1)], al.pos_within((1, 1), 0))
        target = [(1, 1), (1, 2), (1,3), (2, 3), (3, 3)]
        unittest.TestCase().assertCountEqual(target, al.pos_within((1, 1), 5))
        # assuming a_star is working properly
        for pos in target:
            assert len(al.a_star((1, 1), pos)) < 5
        for pos in free.difference(target):
            assert len(al.a_star((1, 1), pos)) >= 5
开发者ID:,项目名称:,代码行数:27,代码来源:

示例5: test_path_to_same_position

 def test_path_to_same_position(self):
     test_layout = (
     """ ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     assert [] == al.a_star((1, 1), (1, 1))
     assert [] == al.bfs((1, 1), [(1, 1)])
开发者ID:,项目名称:,代码行数:11,代码来源:

示例6: test_a_star

 def test_a_star(self):
     test_layout = (
     """ ##################
         #0#.  .  # .     #
         #2#####    #####1#
         #     . #  .  .#3#
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     # just a simple smoke test
     self.assertEqual(14, len(al.a_star((1, 1), (3, 1))))
开发者ID:pombredanne,项目名称:pelita,代码行数:11,代码来源:test_graph.py

示例7: test_extended_adjacency_list

    def test_extended_adjacency_list(self):
        test_layout = (
        """ ##################
            #0#.  .  # .     #
            # #####    ##### #
            #     . #  .  .#1#
            ################## """)
        universe = CTFUniverse.create(test_layout, 2)
        al = AdjacencyList(universe.free_positions())

        adjacency_target = {(7, 3): [(7, 2), (7, 3), (6, 3)],
         (1, 3): [(1, 2), (2, 3), (1, 3)],
         (12, 1): [(13, 1), (12, 1), (11, 1)],
         (16, 2): [(16, 3), (16, 1), (16, 2)],
         (15, 1): [(16, 1), (15, 1), (14, 1)],
         (5, 1): [(6, 1), (5, 1), (4, 1)],
         (10, 3): [(10, 2), (11, 3), (10, 3), (9, 3)],
         (7, 2): [(7, 3), (7, 1), (8, 2), (7, 2)],
         (1, 2): [(1, 3), (1, 1), (1, 2)],
         (3, 3): [(4, 3), (3, 3), (2, 3)],
         (13, 3): [(14, 3), (13, 3), (12, 3)],
         (8, 1): [(8, 2), (8, 1), (7, 1)],
         (16, 3): [(16, 2), (16, 3)],
         (6, 3): [(7, 3), (6, 3), (5, 3)],
         (14, 1): [(15, 1), (14, 1), (13, 1)],
         (11, 1): [(12, 1), (11, 1), (10, 1)],
         (4, 1): [(5, 1), (4, 1), (3, 1)],
         (1, 1): [(1, 2), (1, 1)],
         (12, 3): [(13, 3), (12, 3), (11, 3)],
         (8, 2): [(8, 1), (9, 2), (8, 2), (7, 2)],
         (7, 1): [(7, 2), (8, 1), (7, 1), (6, 1)],
         (9, 3): [(9, 2), (10, 3), (9, 3)],
         (2, 3): [(3, 3), (2, 3), (1, 3)],
         (10, 1): [(10, 2), (11, 1), (10, 1)],
         (5, 3): [(6, 3), (5, 3), (4, 3)],
         (13, 1): [(14, 1), (13, 1), (12, 1)],
         (9, 2): [(9, 3), (10, 2), (9, 2), (8, 2)],
         (6, 1): [(7, 1), (6, 1), (5, 1)],
         (3, 1): [(4, 1), (3, 1)],
         (11, 3): [(12, 3), (11, 3), (10, 3)],
         (16, 1): [(16, 2), (16, 1), (15, 1)],
         (4, 3): [(5, 3), (4, 3), (3, 3)],
         (14, 3): [(14, 3), (13, 3)],
         (10, 2): [(10, 3), (10, 1), (10, 2), (9, 2)]}

        for val in al.values():
            val.sort()

        for val in adjacency_target.values():
            val.sort()

        assert adjacency_target == al
开发者ID:,项目名称:,代码行数:52,代码来源:

示例8: test_a_star2

 def test_a_star2(self):
     test_layout = (
         """ ########
             #1#    #
             # # #0 #
             #      #
             ######## """ )
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     #Test distance to middle from both sides
     print(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     print(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
     assert 7 == len(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
     assert 7 == len(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
开发者ID:,项目名称:,代码行数:14,代码来源:

示例9: test_a_star

 def test_a_star(self):
     test_layout = (
     """ ##################
         #02.# .  # .  #  #
         #   ###    ####1 #
         # ### . #  .  ##3#
         #                #
         ################## """)
     universe = CTFUniverse.create(test_layout, 4)
     al = AdjacencyList(universe.free_positions())
     #Test distance to middle from both sides
     assert 11 == len(al.a_star((1, 1), (7, 2)))
     assert 12 == len(al.a_star((2, 1), (7, 2)))
     assert 14 == len(al.a_star((16, 1), (7, 2)))
     assert 15 == len(al.a_star((15, 1), (7, 2)))    
开发者ID:,项目名称:,代码行数:15,代码来源:

示例10: SmartEatingPlayer

class SmartEatingPlayer(AbstractPlayer):
    def set_initial(self):
        self.adjacency = AdjacencyList(self.current_uni.reachable([self.initial_pos]))
        self.next_food = None

    def goto_pos(self, pos):
        return self.adjacency.a_star(self.current_pos, pos)[-1]

    def get_move(self):
        # check, if food is still present
        if (self.next_food is None
                or self.next_food not in self.enemy_food):
            if not self.enemy_food:
                # all food has been eaten? ok. i’ll stop
                return datamodel.stop
            self.next_food = self.rnd.choice(self.enemy_food)

        try:
            dangerous_enemy_pos = [bot.current_pos
                                   for bot in self.enemy_bots if bot.is_destroyer]

            next_pos = self.goto_pos(self.next_food)
            # check, if the next_pos has an enemy on it
            if next_pos in dangerous_enemy_pos:
                # whoops, better wait this round and take another food next time
                self.next_food = None
                return datamodel.stop

            move = diff_pos(self.current_pos, next_pos)
            return move
        except NoPathException:
            return datamodel.stop
开发者ID:jakobj,项目名称:pelita,代码行数:32,代码来源:SmartEatingPlayer.py

示例11: BorderPlayer

class BorderPlayer(AbstractPlayer):
    """ A player that makes moves at random. """

    def find_path(self, thingslist):
        """ finds the path to the nearest object
        *thingslist* - list of tuples with objects positions
        """
        self.adjacency = AdjacencyList(self.current_uni.free_positions())
        try:
            pathd =  self.adjacency.bfs(self.current_pos, thingslist)
        except NoPathException:
            return None
        return pathd

    def read_score(self):
        self.score_history[0, self.round_index] = self.current_uni.teams[0].score
        self.score_history[1, self.round_index] = self.current_uni.teams[1].score

    def get_move(self):
        border_path =  self.find_path(self.team_border)
        self.say("Border!!!!")
        if len(border_path)==0:
            return stop
        if border_path==None:
            return stop
        return diff_pos(self.current_pos, border_path.pop())
开发者ID:dokato,项目名称:pelita,代码行数:26,代码来源:our_player.py

示例12: attack_move

 def attack_move(self):
     self.adjacency = AdjacencyList(self.current_uni.free_positions())
     attackpath = []
     if self.tracking_idx is not None:
         # if the enemy is no longer in our zone
         if not self.team.in_zone(self.tracking_target.current_pos):
             self.tracking_idx = None
             return  self.go_for_food()
         # otherwise update the path to the target
         else:
             attackpath = self.path_to_target
     if self.tracking_idx is None:
         # check the enemy positions
         possible_targets = [enemy for enemy in self.enemy_bots
                 if self.team.in_zone(enemy.current_pos)]
         if possible_targets:
             # get the path to the closest one
             try:
                 possible_paths = [(enemy, 
                     self.adjacency.a_star(self.current_pos, enemy.current_pos))
                                   for enemy in possible_targets]
             except NoPathException:
                 return None
         else:
             return None
         if possible_paths:
             closest_enemy, path = min(possible_paths,
                                       key=lambda enemy_path: len(enemy_path[1]))
             self.tracking_idx = closest_enemy.index
     if len(attackpath)==0:
         return self.random_move()
     if len(attackpath)>0 and self.round_index%20==0:
         return self.random_move()
     return diff_pos(self.current_pos, attackpath.pop())
开发者ID:dokato,项目名称:pelita,代码行数:34,代码来源:our_player.py

示例13: set_initial

 def set_initial(self):
     # Now ``self.current_uni`` and ``self.current_state`` are known.
     # ``set_initial`` is always called before ``get_move``, so we can do some
     # additional initialization here
     self.adjacency = AdjacencyList(self.current_uni.reachable([self.initial_pos]))
     # Just printing the universe to give you an idea, please remove all
     # print statements in the final player.
     print self.current_uni.pretty
开发者ID:veronikam,项目名称:pelita_g2,代码行数:8,代码来源:franzi_player.py

示例14: path_to_target

 def path_to_target(self):
     """ Path to the target we are currently tracking. """
     self.adjacency = AdjacencyList(self.current_uni.free_positions())
     try:
         return self.adjacency.a_star(self.current_pos,
                 self.tracking_target.current_pos)
     except NoPathException:
         return None
开发者ID:dokato,项目名称:pelita,代码行数:8,代码来源:our_player.py

示例15: test_bfs_exceptions

 def test_bfs_exceptions(self):
     test_layout = (
     """ ############
         #0.     #.1#
         ############ """)
     universe = CTFUniverse.create(test_layout, 2)
     al = AdjacencyList(universe.free_positions())
     with pytest.raises(NoPathException):
         al.bfs((1, 1), [(10, 1)])
     with pytest.raises(NoPathException):
         al.bfs((1, 1), [(10, 1), (9, 1)])
     with pytest.raises(NoPathException):
         al.bfs((0, 1), [(10, 1)])
     with pytest.raises(NoPathException):
         al.bfs((1, 1), [(11, 1)])
开发者ID:,项目名称:,代码行数:15,代码来源:


注:本文中的pelita.graph.AdjacencyList类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。