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


Python Terrain.get_distance方法代码示例

本文整理汇总了Python中terrain.Terrain.get_distance方法的典型用法代码示例。如果您正苦于以下问题:Python Terrain.get_distance方法的具体用法?Python Terrain.get_distance怎么用?Python Terrain.get_distance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在terrain.Terrain的用法示例。


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

示例1: Robot

# 需要导入模块: from terrain import Terrain [as 别名]
# 或者: from terrain.Terrain import get_distance [as 别名]

#.........这里部分代码省略.........
        reverse_direction = dir_reverse[heading]
        index = self.terrain.get_index_of_wall(reverse_direction)
        cell.imaginary_walls[index] = 1

        # 4) Change the value of visited to signify dead end
        cell.visited = 'x'
        cell.distance = WALL_VALUE

        # 5) Update imaginary walls and distances
        self.terrain.update_imaginary_walls(x, y, cell.imaginary_walls)

        return rotation, movement

    def should_end_exploring(self, x, y):
        """
        The robot should end exploring in all these cases:
        - It has already explored more than 90% of the cells
        - It has already taken 30 steps (in the exploration phase)
        - It has reached the center of the maze (again)
        - It has reached the starting location (again)
        """

        # Check for % of cells covered
        if self.terrain.get_percentage_of_maze_explored() > 80:
            return True

        if self.consecutive_explored_cells >= 3:
            return True

        # Check for number of steps
        if self.steps_exploring > 15:
            return True

        # Check for center of the maze:
        if self.is_at_center_of_the_maze(x, y):
            return True

        if self.is_at_starting_position(x, y):
            return True

        return False

    def final_round(self, x, y, heading, sensors):
        """
        Returns the correct rotation and maximum numbers of steps that
         the robot can take to optimize the score while staying on track
        """

        rotation = None
        movement = None
        current_distance = self.terrain.grid[x][y].distance

        adj_distances, adj_visited = \
            self.terrain.get_adj_info(
                x, y, heading, sensors, False)

        # Change sensor info to max allowed moves, when it applies
        sensors = [3 if step > 3 else step for step in sensors]

        for i, steps in enumerate(sensors):

            # If we found a movement, exit and apply it
            if movement is not None:
                break

            # Otherwise, iterate through steps to see if one matches with the
            # next correct and logical distance for that number of steps
            elif self.is_a_possible_move(adj_distances, adj_visited, i):
                for idx in range(steps):
                    step = steps - idx
                    rotation = rotations[str(i)]
                    new_direction = self.get_new_direction(rotation)
                    furthest_distance = self.terrain.get_distance(
                        x, y, new_direction, step)
                    if furthest_distance == current_distance - step:
                        movement = step
                        break

        return rotation, movement

    def is_a_possible_move(self, adj_distances, adj_visited, i):
        """
        Distances are valid if they are not walls, unvisited, or dead ends.
        """
        return (adj_visited[i] is not ''
                and adj_visited[i] is not 'x'
                and adj_distances[i] is not WALL_VALUE)

    def report_results(self):
        distance = self.terrain.grid[0][0].distance
        percentage = self.terrain.get_percentage_of_maze_explored()
        first_round = self.steps_first_round + self.steps_exploring
        final_round = self.steps_final_round
        print('ALGORITHM USED: {}'.format(self.algorithm.name.upper()))
        print('EXPLORING AFTER CENTER: {}'.format(self.explore_after_center))
        print('NUMBER OF MOVES FIRST ROUND: {}'.format(first_round))
        print('PERCENTAGE OF MAZE EXPLORED: {}%'.format(percentage))
        print('DISTANCE TO CENTER: {}'.format(distance))
        print('NUMBER OF MOVES FINAL ROUND: {}'.format(final_round))
        print('********************************')
开发者ID:RodrigoVillatoro,项目名称:machine_learning_projects,代码行数:104,代码来源:robot.py


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