コード例 #1
0
def mouse_test(maze, mouse, mode):
    
     
    if (mode != 'complete') and (mode != 'incomplete'):
        raise Exception('Argument Error!')

    testmaze = maze
    testmouse = mouse

     
    starting = [0,0]
    destination_final = testmaze.destinations
    location_real = starting[:]
    orientation_real = 'up'
     
    location_reference = starting[:]
    orientation_reference = orientation_real
     
    location_last = starting[:]

     

     
    num_actions_1 = 0
    length_movement_1 = 0
    num_actions_2 = 0
    length_movement_2 = 0
    num_actions_3 = 0
    length_movement_3 = 0
    exploration_time = 0
    maze_visited_observed = np.zeros((testmaze.dim_x, testmaze.dim_y), dtype = np.int32)
    maze_visited_observed[tuple(starting)] = 1
    percentage_maze_visited_observed = float(np.sum(maze_visited_observed))/(maze_visited_observed.shape[0] * maze_visited_observed.shape[1])

     

     
     

    exploration_start = time.time()

    while ((testmouse.percentage_visited < 1.0) if (mode == 'complete') else (testmouse.found_destination == False)):

         
        destination_best, direction_list, movement_list, path_list = testmouse.mouse_action(maze = testmaze, location_real = location_real, orientation_real = orientation_real)

         
         
        direction_list_observed = list()
        for direction in direction_list:
            direction_list_observed.append(orientation_observed(reference_mouse = testmouse.orientation_reference, reference_observed = orientation_reference, orientation_mouse = direction))
         
        movement_list_observed = movement_list[:]
        
         
        location_real = coordinate_observed(reference_mouse = testmouse.location_reference, reference_observed = location_reference, coordinate_mouse = testmouse.location_defined)

         
        orientation_real = orientation_observed(reference_mouse = testmouse.orientation_reference, reference_observed = orientation_reference, orientation_mouse = testmouse.orientation)

         
        location_expected = destination_expectation(maze = testmaze, starting = location_last, direction_list = direction_list_observed, movement_list = movement_list_observed)

         
        if location_real != location_expected:
            print('Warning: location_expected did not match location_real.')

         
        maze_visited_observed[tuple(location_real)] = 1
        percentage_maze_visited_observed = float(np.sum(maze_visited_observed))/(maze_visited_observed.shape[0] * maze_visited_observed.shape[1])

         
        if location_real in destination_final:
            testmouse.found_destination = True
            testmouse.destinations.append(testmouse.location_defined)

         
        num_actions_1 += (len(path_list) - 1)

         
        length_movement_1 += length_count(path_list = path_list)

         
        location_last = location_real

         
         
         
         
    
    exploration_end = time.time()
    exploration_time = exploration_end - exploration_start


     
     

    direction_list, movement_list, path_list = testmouse.return_origin()

     
     
    direction_list_observed = list()
    for direction in direction_list:
        direction_list_observed.append(orientation_observed(reference_mouse = testmouse.orientation_reference, reference_observed = orientation_reference, orientation_mouse = direction))
     
    movement_list_observed = movement_list[:]

     
    location_real = coordinate_observed(reference_mouse = testmouse.location_reference, reference_observed = location_reference, coordinate_mouse = testmouse.location_defined)

     
    orientation_real = orientation_observed(reference_mouse = testmouse.orientation_reference, reference_observed = orientation_reference, orientation_mouse = testmouse.orientation)

     
    location_expected = destination_expectation(maze = testmaze, starting = location_last, direction_list = direction_list_observed, movement_list = movement_list_observed)

     
    if location_real != location_expected:
        print('Warning: location_expected did not match location_real.')
    
     
    location_last = location_real

     
    num_actions_2 += (len(path_list) - 1)

     
    length_movement_2 += length_count(path_list = path_list)
    
     

    direction_list, movement_list, path_list = testmouse.go_destinations()

     
     
    direction_list_observed = list()
    for direction in direction_list:
        direction_list_observed.append(orientation_observed(reference_mouse = testmouse.orientation_reference, reference_observed = orientation_reference, orientation_mouse = direction))
     
    movement_list_observed = movement_list[:]

     
    location_real = coordinate_observed(reference_mouse = testmouse.location_reference, reference_observed = location_reference, coordinate_mouse = testmouse.location_defined)

     
    orientation_real = orientation_observed(reference_mouse = testmouse.orientation_reference, reference_observed = orientation_reference, orientation_mouse = testmouse.orientation)

     
    location_expected = destination_expectation(maze = testmaze, starting = location_last, direction_list = direction_list_observed, movement_list = movement_list_observed)

     
    if location_real != location_expected:
        print('Warning: location_expected did not match location_real.')
    
     
    location_last = location_real  
    
     
    num_actions_3 += (len(path_list) - 1)

     
    length_movement_3 += length_count(path_list = path_list)

     
     
     

    score = num_actions_3 + 1./30 * (num_actions_1 + num_actions_2)

    return (num_actions_1, length_movement_1, num_actions_2, length_movement_2, num_actions_3, length_movement_3, percentage_maze_visited_observed, score, exploration_time)
コード例 #2
0
           (testmouse.found_destination == False)):
        print(testmouse.percentage_visited)

        # Action parameters of mouse
        destination_best, direction_list, movement_list, path_list = testmouse.mouse_action(
            maze=testmaze,
            location_real=location_real,
            orientation_real=orientation_real)

        # Action parameters observed
        # Directions
        direction_list_observed = list()
        for direction in direction_list:
            direction_list_observed.append(
                orientation_observed(
                    reference_mouse=testmouse.orientation_reference,
                    reference_observed=orientation_reference,
                    orientation_mouse=direction))
        # Movements
        movement_list_observed = movement_list[:]

        # Current mouse location observed
        location_real = coordinate_observed(
            reference_mouse=testmouse.location_reference,
            reference_observed=location_reference,
            coordinate_mouse=testmouse.location_defined)

        # Current mouse orientation observed
        orientation_real = orientation_observed(
            reference_mouse=testmouse.orientation_reference,
            reference_observed=orientation_reference,
            orientation_mouse=testmouse.orientation)
コード例 #3
0
def mouse_test(maze, mouse, mode):

    # Check arguments
    if (mode != 'complete') and (mode != 'incomplete'):
        raise Exception('Argument Error!')

    testmaze = maze
    testmouse = mouse

    # Initialize the micromouse
    starting = [0, 0]
    destination_final = testmaze.destinations
    location_real = starting[:]
    orientation_real = 'up'
    # Set references
    location_reference = starting[:]
    orientation_reference = orientation_real
    # Set last location
    location_last = starting[:]

    # Stage 1: Mouse exploration

    # Statistics
    num_actions_1 = 0
    length_movement_1 = 0
    num_actions_2 = 0
    length_movement_2 = 0
    num_actions_3 = 0
    length_movement_3 = 0
    exploration_time = 0
    maze_visited_observed = np.zeros((testmaze.dim_x, testmaze.dim_y),
                                     dtype=np.int32)
    maze_visited_observed[tuple(starting)] = 1
    percentage_maze_visited_observed = float(np.sum(maze_visited_observed)) / (
        maze_visited_observed.shape[0] * maze_visited_observed.shape[1])

    # Explore the maze

    # If argument 1 is 'incomplete', return when mouse found destination
    # If argument 2 is 'complete', return when mouse visited all the grids reachable

    exploration_start = time.time()

    while ((testmouse.percentage_visited < 1.0) if (mode == 'complete') else
           (testmouse.found_destination == False)):

        # Action parameters of mouse
        destination_best, direction_list, movement_list, path_list = testmouse.mouse_action(
            maze=testmaze,
            location_real=location_real,
            orientation_real=orientation_real)

        # Action parameters observed
        # Directions
        direction_list_observed = list()
        for direction in direction_list:
            direction_list_observed.append(
                orientation_observed(
                    reference_mouse=testmouse.orientation_reference,
                    reference_observed=orientation_reference,
                    orientation_mouse=direction))
        # Movements
        movement_list_observed = movement_list[:]

        # Current mouse location observed
        location_real = coordinate_observed(
            reference_mouse=testmouse.location_reference,
            reference_observed=location_reference,
            coordinate_mouse=testmouse.location_defined)

        # Current mouse orientation observed
        orientation_real = orientation_observed(
            reference_mouse=testmouse.orientation_reference,
            reference_observed=orientation_reference,
            orientation_mouse=testmouse.orientation)

        # Expected mouse location based on the directions and movements
        location_expected = destination_expectation(
            maze=testmaze,
            starting=location_last,
            direction_list=direction_list_observed,
            movement_list=movement_list_observed)

        # Check whether location_real and location_expected match
        if location_real != location_expected:
            print('Warning: location_expected did not match location_real.')

        # Update observed maze_visited
        maze_visited_observed[tuple(location_real)] = 1
        percentage_maze_visited_observed = float(
            np.sum(maze_visited_observed)) / (maze_visited_observed.shape[0] *
                                              maze_visited_observed.shape[1])

        # Check if the mouse reached final destination
        if location_real in destination_final:
            testmouse.found_destination = True
            testmouse.destinations.append(testmouse.location_defined)

        # Count number of actions
        num_actions_1 += (len(path_list) - 1)

        # Count the length of movements
        length_movement_1 += length_count(path_list=path_list)

        # Update location_last
        location_last = location_real

        #print('location_real',location_real)
        #print('total num_actions',num_actions_1)
        #print('length_movement',length_movement_1)
        #print('coverage',percentage_maze_visited_observed)

    exploration_end = time.time()
    exploration_time = exploration_end - exploration_start

    # Stage 2: Mouse goes back to starting point
    # The actions of mouse in this stage were not accounted in the total actions

    direction_list, movement_list, path_list = testmouse.return_origin()

    # Action parameters observed
    # Directions
    direction_list_observed = list()
    for direction in direction_list:
        direction_list_observed.append(
            orientation_observed(
                reference_mouse=testmouse.orientation_reference,
                reference_observed=orientation_reference,
                orientation_mouse=direction))
    # Movements
    movement_list_observed = movement_list[:]

    # Current mouse location observed
    location_real = coordinate_observed(
        reference_mouse=testmouse.location_reference,
        reference_observed=location_reference,
        coordinate_mouse=testmouse.location_defined)

    # Current mouse orientation observed
    orientation_real = orientation_observed(
        reference_mouse=testmouse.orientation_reference,
        reference_observed=orientation_reference,
        orientation_mouse=testmouse.orientation)

    # Expected mouse location based on the directions and movements
    location_expected = destination_expectation(
        maze=testmaze,
        starting=location_last,
        direction_list=direction_list_observed,
        movement_list=movement_list_observed)

    # Check whether location_real and location_expected match
    if location_real != location_expected:
        print('Warning: location_expected did not match location_real.')

    # Update location_last
    location_last = location_real

    # Count number of actions
    num_actions_2 += (len(path_list) - 1)

    # Count the length of movements
    length_movement_2 += length_count(path_list=path_list)

    # Stage 3: Mouse goes to the destination again

    direction_list, movement_list, path_list = testmouse.go_destinations()

    # Action parameters observed
    # Directions
    direction_list_observed = list()
    for direction in direction_list:
        direction_list_observed.append(
            orientation_observed(
                reference_mouse=testmouse.orientation_reference,
                reference_observed=orientation_reference,
                orientation_mouse=direction))
    # Movements
    movement_list_observed = movement_list[:]

    # Current mouse location observed
    location_real = coordinate_observed(
        reference_mouse=testmouse.location_reference,
        reference_observed=location_reference,
        coordinate_mouse=testmouse.location_defined)

    # Current mouse orientation observed
    orientation_real = orientation_observed(
        reference_mouse=testmouse.orientation_reference,
        reference_observed=orientation_reference,
        orientation_mouse=testmouse.orientation)

    # Expected mouse location based on the directions and movements
    location_expected = destination_expectation(
        maze=testmaze,
        starting=location_last,
        direction_list=direction_list_observed,
        movement_list=movement_list_observed)

    # Check whether location_real and location_expected match
    if location_real != location_expected:
        print('Warning: location_expected did not match location_real.')

    # Update location_last
    location_last = location_real

    # Count number of actions
    num_actions_3 += (len(path_list) - 1)

    # Count the length of movements
    length_movement_3 += length_count(path_list=path_list)

    #print('total num_actions',num_actions_2)
    #print('length_movement',length_movement_2)
    #print('Final score', num_actions_2 + 1./30 * num_actions_1)

    score = num_actions_3 + 1. / 30 * (num_actions_1 + num_actions_2)

    return (num_actions_1, length_movement_1, num_actions_2, length_movement_2,
            num_actions_3, length_movement_3, percentage_maze_visited_observed,
            score, exploration_time)