Exemple #1
0
def plot_single_run():
    height = 10
    width = 10

    m = mapgrid.generate_map(width=width, height=height)
    v = vehicle.Vehicle(m, 10)

    plot = plotter.LocatorPlot(v)
    plot.plot_map()

    # The placement of the vehicle need not be shown
    # plot.plot_vehicle()

    log.info("Start location:", str(v.location()))
    log.info("Start color:", v.get_color())

    for i in range(10):
        v.move_unbound()
        plot.plot_vehicle()

    # found, x, y = locator.locate(v.map(), v.history(), v)
    num_matches, x, y, possible_loc = locator.locate(v.map(), v.history())

    log.info("End location:", v.location())
    log.info("End color:", v.get_color())

    if num_matches == 1:
        loc_x, loc_y, dir = v.location()
        log.info(
            "Found on location", x, y, " and its correctness is " + str(
                (num_matches == 1) and (x == loc_x) and (y == loc_y)))
    else:
        log.warning("Location not found!")

    # Graph testing
    """
    history_graphs = []
    for i in range(4):
        history_graph = graph_utils.history_to_digraph(v.history(), i)
        history_graphs.append(history_graph)

        graph_utils.digraph_history_analyzer(history_graph)

    map_graph = graph_utils.map_to_digraph(m)

    for i in range(4):
        match = graph_utils.graph_matcher(map_graph, history_graphs[i])
        print("Start direction", direction.Direction(i), "graph match:", match)
    """

    # History error handling test
    locator.locate_with_one_possible_error(v.map(), v.history())
    locator.locate_with_one_possible_error(v.map(), v.history_error_one())
    locator.locate_with_error_fallback(v.map(), v.history())
    locator.locate_with_error_fallback(v.map(), v.history_error_one())
    # locator.locate_with_movement_and_error_fallback(v.map(), v.history(), v, retries=10)
    # locator.locate_with_movement_and_error_fallback(v.map(), v.history_error_one(), v, retries=10)

    plot.show()
    def test_history_error_generation(self):
        m = mapgrid.generate_map(10, 10)
        v = vehicle.Vehicle(m)

        history_colors = v.history()[:, 1]
        for i in range(10):
            history_err_colors = v.history_error_one()[:, 1]
            diff = np.sum(history_colors != history_err_colors)
            self.assertEqual(diff, 1)
Exemple #3
0
def run_MC_2(gridsize: int,
             steps: int,
             MC_iterations: int,
             error=0.001) -> np.ndarray:
    """
    Does Monte Carlo-simulation with given parametes.
    Sensitivity and specificity, confusion 4-matrix,
    
    return 2*2*2 np.ndarray, in which [0,:,:] is mean values and [1,:,:] is error
    """

    # all confusion matrices (like ...0100,0010...)
    results_mat = np.zeros((MC_iterations, 2, 2), dtype=int)

    height = gridsize
    width = gridsize

    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()

        num_found_exact, x, y = locator.locate(
            v.map(), v.history_error(iteration_for_seed=run_idx,
                                     error=error))[0:3]
        num_found_inextact, x, y = locator.locate(v.map(), v.history())[0:3]

        true_pos = 0
        true_neg = 0
        false_pos = 0
        false_neg = 0

        if (num_found_exact == 1) and (num_found_inextact == 1):
            true_pos += 1
        elif (num_found_exact != 1) and (num_found_inextact != 1):
            true_neg += 1
        elif (num_found_exact == 1) and (num_found_inextact != 1):
            false_neg += 1
        elif (num_found_exact != 1) and (num_found_inextact == 1):
            false_pos += 1

        confusion_mat = np.array(
            ((true_pos, false_neg), (false_pos, true_neg)))
        results_mat[run_idx, :, :] = confusion_mat

    #confusion mat
    mean_mat = np.mean(results_mat, axis=0)
    err_mat = np.std(results_mat, axis=0) / np.sqrt(MC_iterations)

    return np.array((mean_mat, err_mat))
Exemple #4
0
def movement_monte_carlo_plot_movenents():
    start_time = time.perf_counter()

    height = 20
    width = 20
    iterations = 10000
    min_moves = 10
    max_moves = 20

    success = 0
    wrong = 0
    fail = 0
    required_moves = np.zeros(max_moves + 1, dtype=int)

    for i in range(iterations):
        if i % 100 == 0:
            log.debug("Running iteration", i)

        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m, 10)
        num_matches, x, y, possible_loc, movements = \
            locator.locate_with_movement_and_error_fallback(m, v, max_moves, min_moves=min_moves)

        if num_matches == 1:
            correct_x, correct_y, direction = v.location()
            if x == correct_x and y == correct_y:
                success += 1
                required_moves[movements] += 1
            else:
                wrong += 1
        else:
            fail += 1

    #print("Success:", success)
    #print("Wrong:", wrong)
    #print("Fail", fail)
    #print("Required moves", required_moves)
    #print("Total time", time.perf_counter() - start_time)

    x_axis = np.arange(1, 8)
    y_axis = (required_moves / iterations)[10:17]

    plotter.plot_histogram(x_axis,
                           y_axis,
                           xlabel="lisäaskelten lukumäärä",
                           ylim=None,
                           savename="lisa-askelten_lkm")
Exemple #5
0
def run_MC(gridsize: int,
           steps: int,
           MC_iterations: int,
           error=False) -> np.ndarray:
    """
    Does Monte Carlo-simulation with given parametes.
    """

    numFound_correct = 0
    numFound_incorrect = 0
    numNotFound = 0

    height = gridsize
    width = gridsize

    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()

        if error:
            num_found, x, y = locator.locate(
                v.map(), v.history_error(iteration_for_seed=run_idx))[0:3]
        else:
            num_found, x, y = locator.locate(v.map(), v.history())[0:3]

        if num_found == 1:
            loc_x, loc_y, dir = v.location()
            if ((x == loc_x) and (y == loc_y)):
                numFound_correct += 1
            else:
                numFound_incorrect += 1
        elif num_found == 0:
            numNotFound += 1
            if not error:
                log.warning("Not found!, gridsize:", gridsize, "steps", steps,
                            "error", error)


    log.debug("    correct match:{:7.3f}".format(numFound_correct/(MC_iterations)) + \
              ",  incorrect match:{:7.3f}".format(numFound_incorrect/(MC_iterations)) )

    return np.array(
        (numFound_correct / MC_iterations, numFound_incorrect / MC_iterations,
         numNotFound / MC_iterations))
Exemple #6
0
def movement_monte_carlo():
    start_time = time.perf_counter()

    height = 10
    width = 10
    iterations = 10000
    min_moves = 9
    max_moves = 30

    success = 0
    wrong = 0
    fail = 0
    required_moves = np.zeros(max_moves + 1, dtype=int)

    for i in range(iterations):
        if i % 100 == 0:
            log.debug("Running iteration", i)

        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m, 10)
        num_matches, x, y, possible_loc, movements = \
            locator.locate_with_movement_and_error_fallback(m, v, max_moves, min_moves=min_moves)

        if num_matches == 1:
            correct_x, correct_y, direction = v.location()
            if x == correct_x and y == correct_y:
                success += 1
                required_moves[movements] += 1
            else:
                wrong += 1
        else:
            fail += 1

    print("Success:", success)
    print("Wrong:", wrong)
    print("Fail", fail)
    print("Required moves", required_moves)
    print("Total time", time.perf_counter() - start_time)
Exemple #7
0
def movement_single_run():
    height = 10
    width = 10

    m = mapgrid.generate_map(width=width, height=height)
    v = vehicle.Vehicle(m, 10)

    plot = plotter.LocatorPlot(v)
    plot.plot_map()

    num_matches, x, y, possible_loc, movements = locator.locate_with_movement_and_error_fallback(
        m, v, 20, plot)

    if num_matches == 1:
        loc_x, loc_y, dir = v.location()
        log.info(
            "Found on location", x, y, " and its correctness is " + str(
                (num_matches == 1) and (x == loc_x) and (y == loc_y)))
        log.info("Required movements:", movements)
    else:
        log.warning("Location not found!")

    plot.show()
Exemple #8
0
def run_MC_3(gridsize: int, steps: int, MC_iterations: int, error=0.001) -> np.ndarray:
    """
    Does Monte Carlo-simulation with given parametes.
    Meant fo error correction and error analysis
    
    This time this function returns full array of results, so that can be used again
    
    return 
        results_mat (N*2*2 np.ndarray)   in which [n,:,:] one confusion 4-matrix of single MC_simulation n
        error_info   
            #   0: number of found locations without error, 
            #   1: number of found locations with error, 
            #   2: if two both of those are equal to 1, 
            #           1 if the answer in error is correct, 0 else
        results_mat_corr with error correction
    """
    
    # all confusion matrices (like ...0100,0010...)
    results_mat = np.zeros((MC_iterations, 2, 2), dtype=int)
    
    error_info = np.zeros((MC_iterations, 3), dtype=int) 
    
    results_mat_corr = np.zeros((MC_iterations, 2, 2), dtype=int)
            
    
    height = gridsize
    width = gridsize
    
    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()
        
        
        err_hist = v.history_error(iteration_for_seed=run_idx, error=error)
        
        num_found_exact, x_ext, y_ext = locator.locate(v.map(), v.history())[0:3]
        num_found_inexact, x_err, y_err = locator.locate(v.map(), err_hist)[0:3]
        num_found_corrected, x_corr, y_corr = locator.locate_with_error_fallback(v.map(), err_hist)[0:3]
        
        true_pos = 0
        true_neg = 0
        false_pos = 0
        false_neg = 0
        
        true_pos_corr = 0
        true_neg_corr = 0
        false_pos_corr = 0
        false_neg_corr = 0
        
        error_info[run_idx, 0] = num_found_exact
        error_info[run_idx, 1] = num_found_inexact
        
        if (num_found_exact == 1) and (num_found_inexact == 1):
            true_pos += 1
            if not (x_err == x_ext) and (y_err == y_ext):
                error_info[run_idx, 2] = 1
                log.debug("Incorrect TP! args:", gridsize,steps,error)
                
        elif (num_found_exact != 1) and (num_found_inexact != 1):
            true_neg += 1
        elif (num_found_exact == 1) and (num_found_inexact != 1):
            false_neg += 1
        elif (num_found_exact != 1) and (num_found_inexact == 1):
            false_pos += 1
        
        # corrected
        if (num_found_exact == 1) and (num_found_corrected == 1):
            true_pos_corr += 1
        elif (num_found_exact != 1) and (num_found_corrected != 1):
            true_neg_corr += 1
        elif (num_found_exact == 1) and (num_found_corrected != 1):
            false_neg_corr += 1
        elif (num_found_exact != 1) and (num_found_corrected == 1):
            false_pos_corr += 1
        
        confusion_mat = np.array(((true_pos, false_neg), (false_pos, true_neg)))
        results_mat[run_idx,:,:] = confusion_mat
        
        conf_mat_corr = np.array(((true_pos_corr, false_neg_corr), (false_pos_corr, true_neg_corr)))
        results_mat_corr[run_idx,:,:] = confusion_mat

    return results_mat, error_info, results_mat_corr
Exemple #9
0
def run_MC_4(gridsize: int, steps: int, MC_iterations: int, error=0.001) -> np.ndarray:
    """
    Yet another way to run calculations. This time thi just returns all values
    """
    # num found vector for every simulation in MC
    num_found_exact    = np.zeros((MC_iterations), dtype=int)
    num_found_inexact   = np.zeros((MC_iterations), dtype=int)
    num_found_corrected = np.zeros((MC_iterations), dtype=int)
    num_found_corrected_move = np.zeros((MC_iterations), dtype=int)
    
    # if TP then if location correct
    found_correct_exact    = np.zeros((MC_iterations), dtype=int)
    found_correct_inexact   = np.zeros((MC_iterations), dtype=int)
    found_correct_corrected = np.zeros((MC_iterations), dtype=int)
    found_correct_corrected_move = np.zeros((MC_iterations), dtype=int)
    
    
    
    
    min_moves = 9
    max_moves = 30
    required_moves = np.zeros(max_moves+1, dtype=int)

                
    
    height = gridsize
    width = gridsize
    
    # Runs simulation in loop
    for run_idx in range(MC_iterations):
        m = mapgrid.generate_map(width=width, height=height)
        v = vehicle.Vehicle(m)
        for i in range(steps):
            v.move_unbound()
        
        
        err_hist = v.history_error(iteration_for_seed=run_idx, error=error)
        
        num_found_exact[run_idx], x_ext, y_ext = locator.locate(v.map(), v.history())[0:3]
        num_found_inexact[run_idx], x_err, y_err = locator.locate(v.map(), err_hist)[0:3]
        num_found_corrected[run_idx], x_corr, y_corr = locator.locate_with_error_fallback(v.map(), err_hist)[0:3]
        
        if num_found_exact[run_idx] == 1:
            found_correct_exact[run_idx] = 1
        
        if num_found_inexact[run_idx] == 1:
            if x_err == x_ext and y_err == y_ext:
                found_correct_inexact[run_idx] = 1
        
        if num_found_corrected[run_idx] == 1:
            if x_corr == x_ext and y_corr == y_ext:
                found_correct_corrected[run_idx] = 1
                
    
        # now the moving error correction from 'movement.py'
        
        v2 = vehicle.Vehicle(m, 10, error=error)
        num_found_corrected_move[run_idx], x_mov, y_mov, possible_loc, movements = \
            locator.locate_with_movement_and_error_fallback(m, v2, max_moves, min_moves=min_moves)

        if num_found_corrected_move[run_idx] == 1:
            correct_x, correct_y, direction = v2.location()
            if x_mov == correct_x and y_mov == correct_y:
                found_correct_corrected_move[run_idx] = 1
                required_moves[movements] += 1
        
    return num_found_exact, num_found_inexact, num_found_corrected, num_found_corrected_move, \
            found_correct_exact, found_correct_inexact, found_correct_corrected, found_correct_corrected_move, required_moves