コード例 #1
0
 def testUpdateCell(self):
     belief_str = 'F-\n-b'
     grid = problem.parse(belief_str)
     observation_str = '--\nHB'
     observation_dict = problem.parse(observation_str)
     new_grid = update_cell(grid, observation_dict)
     self.assertEquals(new_grid, {(0, 0): 'F',  (1, 1): 'B', (0, 1): 'H'}, new_grid)
     self.assertEquals(grid, {(0, 0): 'F', (1, 1): 'b'}, grid)
コード例 #2
0
 def testUpdateBelief(self):
     belief_str = 'F-\n-b'
     grid = problem.parse(belief_str)
     belief = problem.to_state(grid)
     observation_str = '--\nHB'
     observation_dict = problem.parse(observation_str)
     observation = problem.to_observation(observation_dict)
     new_belief = update_belief(belief, observation)
     self.assertEquals(new_belief.grid, {(0, 0): 'F',  (1, 1): 'B', (0, 1): 'H'}, new_belief.grid)
     self.assertEquals(belief.grid, {(0, 0): 'F', (1, 1): 'b'}, belief.grid)
コード例 #3
0
 def testTransitionHFMaxFood1(self):
     random.seed(1)
     state_str = 'B---\n--HF'
     grid = problem.parse(state_str)
     action = 'HF_3_1'
     harvester_world = problem.to_problem(x=4, y=2, max_food=1)
     distances = problem.distance_to_base(grid, harvester_world)
     distances = problem.add_distance_to_food(grid, distances,
                                              harvester_world)
     belief_state = problem.to_state(grid, distances=distances)
     food_dist = problem.chance_of_food(belief_state, harvester_world)
     future_food = problem.sample_future_food(food_dist, n=1)
     initial_state = problem.to_state(grid,
                                      distances=distances,
                                      future_food=future_food)
     next_state, action_cost = transition(initial_state,
                                          action,
                                          harvester_world,
                                          time_left=1)
     self.assertEquals(next_state.grid, {
         (0, 1): 'F',
         (0, 0): 'B',
         (3, 1): '$',
         (2, 1): None
     }, next_state.grid)
     self.assertEquals(next_state.reward, -1, next_state.reward)
     self.assertEquals(action_cost, 1, action_cost)
     random.seed(None)
コード例 #4
0
 def testListExploredCell(self):
     belief_str = 'F-\n-b'
     grid = problem.parse(belief_str)
     grid[(1, 0)] = '-'
     grid[(0, 1)] = '-'
     explored = list_explored_cell(grid)
     self.assertEquals(explored, {(1, 0): '-',  (0, 1): '-'}, explored)
コード例 #5
0
 def testDelExploredCell(self):
     belief_str = 'F-\n-b'
     grid = problem.parse(belief_str)
     grid[(1, 0)] = '-'
     grid[(0, 1)] = '-'
     new_grid = del_explored_cell(grid)
     self.assertEquals(new_grid, {(0, 0): 'F', (1, 1): 'b'}, new_grid)
     self.assertEquals(grid, {(0, 0): 'F', (1, 1): 'b', (1, 0): '-',  (0, 1): '-'}, grid)
コード例 #6
0
 def testReturnMaxG(self):
     state_str = '-#\n$B'
     grid = problem.parse(state_str)
     harvester_world = problem.to_problem(x=2, y=2)
     distances = problem.distance_to_base(grid, harvester_world)
     distances = problem.add_distance_to_food(grid, distances,
                                              harvester_world)
     initial_state = problem.to_state(grid, distances=distances)
     max_g = search(initial_state, harvester_world, horizon=10)
     self.assertEquals(max_g, 49.0, max_g)
コード例 #7
0
 def testReturnPlan(self):
     state_str = '-#\n$B'
     grid = problem.parse(state_str)
     harvester_world = problem.to_problem(x=2, y=2)
     distances = problem.distance_to_base(grid, harvester_world)
     distances = problem.add_distance_to_food(grid, distances,
                                              harvester_world)
     initial_state = problem.to_state(grid, distances=distances)
     _ = search(initial_state,
                harvester_world,
                horizon=10,
                return_plan=True)
コード例 #8
0
 def testExpand(self):
     state_str = 'H-\n-B'
     base, harvester, food, obstacle, defender, enemy, has_food = problem.parse(
         state_str)
     state = problem.to_state(base, harvester, food, obstacle, defender,
                              enemy, has_food)
     world = problem.to_problem(x=2, y=2)
     open_list = []
     policy = [[None] * world.y for _ in range(world.x)]
     expand(((1, 1), 0), open_list, policy, state, world)
     self.assertEquals(open_list, [((1, 0), 1), ((0, 1), 1)], open_list)
     self.assertEquals(policy, [[None, ((1, 1), 1)], [((1, 1), 1), None]],
                       policy)
コード例 #9
0
 def testDijkstra(self):
     state_str = '#-\n-b'
     base, harvester, food, obstacle, defender, enemy, has_food = problem.parse(
         state_str)
     state = problem.to_state(base, harvester, food, obstacle, defender,
                              enemy, has_food)
     world = problem.to_problem(x=2, y=2)
     policy = dijkstra((1, 1), state, world)
     self.assertEquals(policy, {
         (0, 1): ((1, 1), 1),
         (1, 0): ((1, 1), 1),
         (1, 1): ('*', 0)
     }, policy)
コード例 #10
0
 def testTransitionHB(self):
     state_str = '---$\n---B'
     grid = problem.parse(state_str)
     action = 'HB'
     harvester_world = problem.to_problem(x=4, y=2)
     distances = problem.distance_to_base(grid, harvester_world)
     initial_state = problem.to_state(grid, distances=distances)
     next_state, action_cost = transition(initial_state,
                                          action,
                                          harvester_world,
                                          time_left=1)
     self.assertEquals(next_state.grid, {
         (3, 1): '*',
         (3, 0): None
     }, next_state.grid)
     self.assertEquals(next_state.reward, 49, next_state.reward)
     self.assertEquals(action_cost, 1, action_cost)
コード例 #11
0
 def testTransitionHF(self):
     state_str = 'B---\n--HF'
     grid = problem.parse(state_str)
     action = 'HF_3_1'
     harvester_world = problem.to_problem(x=4, y=2)
     distances = problem.distance_to_base(grid, harvester_world)
     distances = problem.add_distance_to_food(grid, distances,
                                              harvester_world)
     initial_state = problem.to_state(grid, distances=distances)
     next_state, action_cost = transition(initial_state,
                                          action,
                                          harvester_world,
                                          time_left=1)
     self.assertEquals(next_state.grid, {
         (0, 0): 'B',
         (3, 1): '$',
         (2, 1): None
     }, next_state.grid)
     self.assertEquals(next_state.reward, -1, next_state.reward)
     self.assertEquals(action_cost, 1, action_cost)
コード例 #12
0
def init_reality(reality_file_name):
    """Constructs the initial state of the world from a file.
    param: reality_file_name: The path and name of a file illustrating the real world. See problem for format.
    return: Initial state of the world
    return: x, y: Dimensions of the world
    """
    reality_str = ''
    x = 0
    y = 0
    with open(reality_file_name, 'r') as reality_file:
        for line in reality_file:
            reality_str += line
            x = len(line) - 1
            y += 1
    base, harvester, food, obstacle, defender, enemy, has_food, explored = problem.parse(reality_str)
    return problem.to_state(base,
                            harvester,
                            food=food,
                            obstacle=obstacle,
                            defender=defender,
                            enemy=enemy,
                            has_food=has_food,
                            explored=explored), x, y
コード例 #13
0
def init_belief(belief_file_name, future_food=None):
    """Constructs the agent's initial belief about the world from a file.
    param: belief_file_name: The path and name of a file illustrating the agent's belief. See problem for format.
    return: Agent's initial belief state
    """
    belief_str = ''
    x = 0
    y = 0
    with open(belief_file_name, 'r') as belief:
        for line in belief:
            belief_str += line
            x = len(line) - 1
            y += 1
    base, harvester, food, obstacle, defender, enemy, has_food, explored = problem.parse(belief_str)
    return problem.to_state(base,
                            harvester,
                            food=food,
                            obstacle=obstacle,
                            defender=defender,
                            enemy=enemy,
                            has_food=has_food,
                            future_food=future_food,
                            explored=explored), x, y
コード例 #14
0
 def testIsPayDay(self):
     belief_str = 'F-\n-*'
     grid = problem.parse(belief_str)
     pay_day = is_pay_day(grid)
     self.assertEquals(pay_day, True, pay_day)