Exemple #1
0
def main():
    seed()
    steps = []
    stepsSlow = []
    for i in range(1):
        width = randrange(33, 33 + 1)
        height = randrange(33, 33 + 1)
        bomb_x = randrange(0, width)
        bomb_y = randrange(0, height)
        robots = []
        grid = Grid(width, height)
        grid.init_bomb(bomb_x, bomb_y, 10)
        for i in range(0, 3):
            temp = Robot(i, grid, [0, int(i * height / 3)], False)
            robots.append(temp)
        controller = RobotController(grid, robots, False, True)
        controller.print_grid = False
        temp = controller.go()
        steps.append(temp)

    x_bar = steps[0]
    # sigma = stdev(steps, x_bar)
    print('Phylogenetic Algorithm')
    print('----------------------')
    print('Mean Steps: ', x_bar)
Exemple #2
0
def single():
    """
    Single runs a single run with no graphics.
    You can configure each parameter with the menu system
    :return: None
    """
    choice = input('Brute Force (1) or Phylogenetic(2): ')
    temp = int(choice)
    slow = False
    if temp == 1:
        slow = True
    choice = input('Enter the Grid Width : ')
    width = int(choice)
    choice = input('Grid Height : ')
    height = int(choice)
    choice = input('Enter the Bomb Location in X: ')
    bomb_x = int(choice)
    choice = input('in Y: ')
    bomb_y = int(choice)
    choice = input('Enter the number of robots: ')
    num_robots = int(choice)
    choice = input('Use Kriging to Guess 1 Yes 2 No: ')
    temp = int(choice)
    guess = True
    if temp == 2:
        guess = False
    choice = input('Print Grid 1 Yes 2 No: ')
    temp = int(choice)
    print_grid = False
    if temp == 1:
        print_grid = True
    robots = []

    choice = input('EM Grid 1 Yes 2 No: ')
    temp = int(choice)
    grid = Grid(width, height)
    if temp == 1:
        grid = EmGrid(width, height)
    grid.init_bomb(bomb_x, bomb_y, 10)
    print(grid)

    for i in range(0, num_robots):
        starting_x = 0
        direction = 1
        if i % 2 == 1:
            starting_x = width - 1
            direction = -1
        temp = Robot(i, grid, [starting_x, int(i * height / num_robots)], slow)
        temp.row_direction = direction
        temp.x_direction = direction
        robots.append(temp)

    controller = RobotController(grid, robots, False, True, guess)
    controller.print_grid = print_grid
    controller.go()
Exemple #3
0
def single_with_graphics():
    """
    Runs a single run with graphics.
    You can configure each parameter with the menu system
    :return: None
    """
    choice = input('Brute Force (1) or Phylogenic(2): ')
    temp = int(choice)
    slow = False
    if temp == 1:
        slow = True
    choice = input('Enter the Grid Width : ')
    width = int(choice)
    choice = input('Grid Height : ')
    height = int(choice)
    choice = input('Enter the Bomb Location in X: ')
    bomb_x = int(choice)
    choice = input('in Y: ')
    bomb_y = int(choice)
    choice = input('Enter the number of robots: ')
    num_robots = int(choice)
    robots = []

    grid = Grid(width, height)
    grid.init_bomb(bomb_x, bomb_y, 10)

    for i in range(0, num_robots):
        starting_x = 0
        direction = 1
        if i % 2 == 1:
            starting_x = width - 1
            direction = -1
        temp = Robot(i, grid, [starting_x, int(i * height / num_robots)], slow)
        temp.row_direction = direction
        temp.x_direction = direction
        robots.append(temp)

    controller = RobotController(grid, robots, True, True)
    controller.print_grid = False
    controller.go()
Exemple #4
0
def main():
    seed()
    steps = []
    stepsSlow = []
    for i in range(1, 11):
        width = i * 100
        height = i * 100
        bomb_x = randrange(0, width)
        bomb_y = randrange(0, height)
        robots = []
        grid = Grid(width, height)
        grid.init_bomb(bomb_x, bomb_y, 10)
        for i in range(0, 2):
            temp = Robot(i, grid, [0, int(i * height / 2)], False)
            robots.append(temp)
        controller = RobotController(grid, robots, False, False)
        tempFast = controller.go()
        steps.append(tempFast)

        robotsSlow = []
        gridSlow = Grid(width, height)
        gridSlow.init_bomb(bomb_x, bomb_y, 10)
        for j in range(0, 2):
            temp = Robot(j, gridSlow, [0, int(j * height / 2)], True)
            robotsSlow.append(temp)
        controllerSlow = RobotController(gridSlow, robotsSlow, False, False)
        tempSlow = controllerSlow.go()
        stepsSlow.append(tempSlow)
        print(width, 'x', height, tempFast, 'vs', tempSlow,
              str("%.1f" % (tempSlow / tempFast)))
        print(str("%.1f Steps Per Cell" % (tempFast / width)))
        print(str("%.1f Steps Per Cell" % (tempSlow / width)))

    x_bar = mean(steps)
    sigma = stdev(steps, x_bar)
    print('Phylogenetic Algorithm')
    print('----------------------')
    print('Mean Steps: ', x_bar)
    print('Std Dev Steps: ', sigma)

    x_barSlow = mean(stepsSlow)
    sigmaSlow = stdev(stepsSlow, x_barSlow)
    print()
    print('Brute Force')
    print('----------------------')
    print('Mean Steps: ', x_barSlow)
    print('Std Dev Steps: ', sigmaSlow)

    print(x_barSlow / x_bar, 'Increase')
Exemple #5
0
def main():
    seed()
    steps = []
    stepsSlow = []
    for i in range(1):
        width = randrange(16, 25)
        height = randrange(16, 25)
        bomb_x = randrange(0, width)
        bomb_y = randrange(0, height)
        robots = []
        grid = Grid(width, height)
        grid.init_bomb(bomb_x, bomb_y, 10)
        for i in range(0, 2):
            temp = Robot(i, grid, [0, int(i * height / 2)], False)
            robots.append(temp)
        controller = RobotController(grid, robots, True, True)
        temp = controller.go()
        steps.append(temp)

        robotsSlow = []
        gridSlow = Grid(width, height)
        gridSlow.init_bomb(bomb_x, bomb_y, 10)
        for j in range(0, 2):
            temp = Robot(j, grid, [0, int(j * height / 2)], True)
            robotsSlow.append(temp)
        controllerSlow = RobotController(gridSlow, robotsSlow, True, True)
        tempSlow = controllerSlow.go()
        stepsSlow.append(tempSlow)

    x_bar = steps[0]
    # sigma = stdev(steps, x_bar)
    print('Phylogenetic Algorithm')
    print('----------------------')
    print('Mean Steps: ', x_bar)
    # print('Std Dev Steps: ', sigma)

    x_barSlow = stepsSlow[0]
    # sigmaSlow = stdev(stepsSlow, x_barSlow)
    print()
    print('Brute Force')
    print('----------------------')
    print('Mean Steps: ', x_barSlow)
    # print('Std Dev Steps: ', sigmaSlow)

    print(x_barSlow / x_bar, 'Increase')
 def __init__(self, grid, robots, ui=True, asci=True, guess=True):
     self.id = id
     self.grid = grid
     self.MAX_STEPS = grid.width * grid.height
     self.heat_map = Grid(grid.width, grid.height)
     self.estimate_generator = EstimateGenerator(self.heat_map, self.grid.bomb_location)
     self.robots = robots
     self.steps = 0
     self.interval_steps = 0
     self.ui = ui
     self.ascii = asci
     self.done = False
     self.estimates = []
     self.diff = 0
     self.print_grid = False
     self.print_all_estimates = False
     self.guess = guess
     self.best_z = []
     self.last_z = []
     # self.interval = max([int((grid.width * grid.height) / 250), 3])
     self.interval = 3
     if self.ui:
         self.graphics = Graphics(self.heat_map, self.robots)
Exemple #7
0
                return True
            return True

    def get_estimate(self, x, y):
        self.calculate_prediction_point(x, y)
        self.calculate_sv_pp()
        if self.calculate_weights():
            self.calculate_z()
            return [self.pp_z, self.pp_error]
        else:
            return []


if __name__ == "__main__":
    np.set_printoptions(linewidth=300, precision=1)
    heat_map = Grid(16, 16)
    heat_map.init_bomb(3, 3, 10)
    heat_map.cells[3][3] = 0
    # heat_map.cells[0][0] = 1
    # heat_map.cells[1][0] = 2
    # heat_map.cells[2][0] = 4
    # heat_map.cells[0][1] = 5
    # heat_map.cells[0][2] = 6
    # heat_map.cells[2][2] = 27

    for x in range(16, 32):
        for y in range(16, 32):
            heat_map = Grid((x), (y))
            bombX = int(heat_map.width / 2)
            bombY = int(heat_map.height / 2)
            heat_map.init_bomb(bombX, bombY)
Exemple #8
0
def mc_no_kriging():
    """
    Runs a number of iterations of each of the 4 algorithm
    combinations. No graphics and no output.
    You can configure each parameter with the menu system
    :return: Writes out a CSV File of results for each run
    """
    min_width = 0
    min_height = 0
    max_width = 0
    max_height = 0
    steps_bf = []
    steps_bf_k = []
    steps_ph = []
    steps_ph_k = []
    choice = input('Do you want to Vary Grid Size (Y/N): ')
    if choice == 'Y' or choice == 'y':
        choice = input('Minimum Grid Width : ')
        min_width = int(choice)
        choice = input('Maximum Grid Width : ')
        max_width = int(choice)
        choice = input('Minimum Grid Height : ')
        min_height = int(choice)
        choice = input('Maximum Grid Height : ')
        max_height = int(choice)
    else:
        choice = input('Grid Width : ')
        min_width = int(choice)
        max_width = min_width + 1
        choice = input('Grid Height : ')
        min_height = int(choice)
        max_height = min_height + 1

    vary_bomb = False
    bomb_x = 0
    bomb_y = 0
    choice = input('Do you want to Vary Bomb Location (Y/N): ')
    if choice == 'Y' or choice == 'y':
        vary_bomb = True
    else:
        vary_bomb = False
        choice = input('Enter the Bomb Location in X: ')
        bomb_x = int(choice)
        choice = input('in Y: ')
        bomb_y = int(choice)

    choice = input('Enter the number of robots: ')
    num_robots = int(choice)
    robots = []

    choice = input('Number of Iterations: ')
    iterations = int(choice)

    choice = input('File To Save Results (default is 100by100.csv): ')
    filename = choice
    if filename == '':
        filename = '100by100.csv'

    with open(filename, 'w', newline='') as csvfile:
        csvwriter = csv.writer(csvfile,
                               delimiter=',',
                               quotechar='|',
                               quoting=csv.QUOTE_MINIMAL)
        header = [
            'Algorithm', 'Kriging', 'Grid Width', 'Grid Height',
            'Bomb X Location', 'Bomb Y Location', 'Number of Robots', 'Steps'
        ]
        csvwriter.writerow(header)
        seed()
        for iteration in range(iterations):
            width = randrange(min_width, max_width)
            height = randrange(min_height, max_height)
            if vary_bomb:
                bomb_x = randrange(0, width)
                bomb_y = randrange(0, height)
            grid = Grid(width, height)
            grid.init_bomb(bomb_x, bomb_y, 10)

            robots_bf = []
            for i in range(0, num_robots):
                temp = Robot(i, grid, [0, int(i * height / num_robots)], True)
                robots_bf.append(temp)
            controller = RobotController(grid, robots_bf, False, False, False)
            temp = controller.go()
            steps_bf.append(temp[0])
            print('BF Iteration', iteration)
            list = [
                'BF', 'No', width, height, bomb_x, bomb_y, num_robots, temp
            ]
            csvwriter.writerow(list)

            robots_ph = []
            for i in range(0, num_robots):
                temp = Robot(i, grid, [0, int(i * height / num_robots)], False)
                robots_ph.append(temp)
            controller = RobotController(grid, robots_ph, False, False, False)
            temp = controller.go()
            steps_ph.append(temp[0])
            print('PH Iteration', iteration)
            list = [
                'PH', 'No', width, height, bomb_x, bomb_y, num_robots, temp
            ]
            csvwriter.writerow(list)

    x_bar_bf = mean(steps_bf)
    sigma_bf = stdev(steps_bf, x_bar_bf)
    print()
    print('Brute Force')
    print('----------------------')
    print('Mean Steps: ', x_bar_bf)
    print('Std Dev Steps: ', sigma_bf)

    x_bar_ph = mean(steps_ph)
    sigma_ph = stdev(steps_ph, x_bar_ph)
    print()
    print('Phylogenetic')
    print('----------------------')
    print('Mean Steps: ', x_bar_ph)
    print('Std Dev Steps: ', sigma_ph)
Exemple #9
0
def mc():
    """
    Runs a number of iterations of each of the 4 algorithm
    combinations. No graphics and no output.
    You can configure each parameter with the menu system
    :return: Writes out a CSV File of results for each run
    """
    min_width = 0
    min_height = 0
    max_width = 0
    max_height = 0
    steps_bf = []
    steps_bf_k = []
    steps_ph = []
    steps_ph_k = []
    choice = input('Do you want to Vary Grid Size (Y/N): ')
    if choice == 'Y' or choice == 'y':
        choice = input('Minimum Grid Width : ')
        min_width = int(choice)
        choice = input('Maximum Grid Width : ')
        max_width = int(choice)
        choice = input('Minimum Grid Height : ')
        min_height = int(choice)
        choice = input('Maximum Grid Height : ')
        max_height = int(choice)
    else:
        choice = input('Grid Width : ')
        min_width = int(choice)
        max_width = min_width + 1
        choice = input('Grid Height : ')
        min_height = int(choice)
        max_height = min_height + 1

    vary_bomb = False
    bomb_x = 0
    bomb_y = 0
    choice = input('Do you want to Vary Bomb Location (Y/N): ')
    if choice == 'Y' or choice == 'y':
        vary_bomb = True
    else:
        vary_bomb = False
        choice = input('Enter the Bomb Location in X: ')
        bomb_x = int(choice)
        choice = input('in Y: ')
        bomb_y = int(choice)

    choice = input('Enter the number of robots: ')
    num_robots = int(choice)
    robots = []

    choice = input('Number of Iterations: ')
    iterations = int(choice)

    filename = str(min_width) + 'by' + str(min_height) + '.csv'
    choice = input('File To Save Results (default is ' + filename + '): ')
    if choice is not '':
        filename = choice

    with open(filename, 'w', newline='') as csvfile:
        csvwriter = csv.writer(csvfile,
                               delimiter=',',
                               quotechar='|',
                               quoting=csv.QUOTE_MINIMAL)
        header = [
            'Algorithm', 'Kriging', 'Grid Width', 'Grid Height',
            'Bomb X Location', 'Bomb Y Location', 'Number of Robots', 'Steps',
            'Finish', 'Best Bomb Guess X', 'Best Bomb Guess Y',
            'Guess at Step', 'Last Bomb Guess X', 'Last Bomb Guess Y'
        ]
        csvwriter.writerow(header)
        seed()
        for iteration in range(iterations):
            width = randrange(min_width, max_width)
            height = randrange(min_height, max_height)
            if vary_bomb:
                bomb_x = randrange(0, width)
                bomb_y = randrange(0, height)
            grid = Grid(width, height)
            grid.init_bomb(bomb_x, bomb_y, 10)
            print('Bomb at', bomb_x, bomb_y)

            robots_bf = []
            for i in range(0, num_robots):
                starting_x = 0
                direction = 1
                if i % 2 == 1:
                    starting_x = width - 1
                    direction = -1
                temp = Robot(
                    i, grid,
                    [starting_x, int(i * height / num_robots)], True)
                temp.row_direction = direction
                temp.x_direction = direction
                robots_bf.append(temp)
            controller = RobotController(grid, robots_bf, False, False, False)
            temp = controller.go()
            steps_bf.append(temp[0])
            print('BF Iteration', iteration)
            name = 'BF'
            kriging = 'No'
            steps = temp[0]
            finished = temp[1]
            if len(temp[2]) >= 5:
                best_bomb_guess_x = temp[2][0]
                best_bomb_guess_y = temp[2][1]
                guess_at_step = temp[2][4]
            else:
                best_bomb_guess_x = ''
                best_bomb_guess_y = ''
                guess_at_step = ''
            if len(temp[3]) >= 2:
                last_bomb_guess_x = temp[3][0]
                last_bomb_guess_y = temp[3][1]
            else:
                last_bomb_guess_x = ''
                last_bomb_guess_y = ''
            list = [
                name, kriging, width, height, bomb_x, bomb_y, num_robots,
                steps, finished, best_bomb_guess_x, best_bomb_guess_y,
                guess_at_step, last_bomb_guess_x, last_bomb_guess_y
            ]
            csvwriter.writerow(list)

            robots_bf_k = []
            for i in range(0, num_robots):
                starting_x = 0
                direction = 1
                if i % 2 == 1:
                    starting_x = width - 1
                    direction = -1
                temp = Robot(
                    i, grid,
                    [starting_x, int(i * height / num_robots)], True)
                temp.row_direction = direction
                temp.x_direction = direction
                robots_bf_k.append(temp)
            controller = RobotController(grid, robots_bf_k, False, False, True)
            temp = controller.go()
            steps_bf_k.append(temp[0])
            print('BFK Iteration', iteration)
            name = 'BF'
            kriging = 'Yes'
            steps = temp[0]
            finished = temp[1]
            if len(temp[2]) >= 5:
                best_bomb_guess_x = temp[2][0]
                best_bomb_guess_y = temp[2][1]
                guess_at_step = temp[2][4]
            else:
                best_bomb_guess_x = ''
                best_bomb_guess_y = ''
                guess_at_step = ''
            if len(temp[3]) >= 2:
                last_bomb_guess_x = temp[3][0]
                last_bomb_guess_y = temp[3][1]
            else:
                last_bomb_guess_x = ''
                last_bomb_guess_y = ''
            list = [
                name, kriging, width, height, bomb_x, bomb_y, num_robots,
                steps, finished, best_bomb_guess_x, best_bomb_guess_y,
                guess_at_step, last_bomb_guess_x, last_bomb_guess_y
            ]

            csvwriter.writerow(list)

            robots_ph = []
            for i in range(0, num_robots):
                starting_x = 0
                direction = 1
                if i % 2 == 1:
                    starting_x = width - 1
                    direction = -1
                temp = Robot(
                    i, grid,
                    [starting_x, int(i * height / num_robots)], False)
                temp.row_direction = direction
                temp.x_direction = direction
                robots_ph.append(temp)
            controller = RobotController(grid, robots_ph, False, False, False)
            temp = controller.go()
            steps_ph.append(temp[0])
            print('PH Iteration', iteration)
            name = 'PH'
            kriging = 'No'
            steps = temp[0]
            finished = temp[1]
            if len(temp[2]) >= 5:
                best_bomb_guess_x = temp[2][0]
                best_bomb_guess_y = temp[2][1]
                guess_at_step = temp[2][4]
            else:
                best_bomb_guess_x = ''
                best_bomb_guess_y = ''
                guess_at_step = ''
            if len(temp[3]) >= 2:
                last_bomb_guess_x = temp[3][0]
                last_bomb_guess_y = temp[3][1]
            else:
                last_bomb_guess_x = ''
                last_bomb_guess_y = ''
            list = [
                name, kriging, width, height, bomb_x, bomb_y, num_robots,
                steps, finished, best_bomb_guess_x, best_bomb_guess_y,
                guess_at_step, last_bomb_guess_x, last_bomb_guess_y
            ]
            csvwriter.writerow(list)

            robots_ph_k = []
            for i in range(0, num_robots):
                starting_x = 0
                direction = 1
                if i % 2 == 1:
                    starting_x = width - 1
                    direction = -1
                temp = Robot(
                    i, grid,
                    [starting_x, int(i * height / num_robots)], False)
                temp.row_direction = direction
                temp.x_direction = direction
                robots_ph_k.append(temp)
            controller = RobotController(grid, robots_ph_k, False, False, True)
            temp = controller.go()
            steps_ph_k.append(temp[0])
            print('PHK Iteration', iteration)
            name = 'PH'
            kriging = 'Yes'
            steps = temp[0]
            finished = temp[1]
            if len(temp[2]) >= 5:
                best_bomb_guess_x = temp[2][0]
                best_bomb_guess_y = temp[2][1]
                guess_at_step = temp[2][4]
            else:
                best_bomb_guess_x = ''
                best_bomb_guess_y = ''
                guess_at_step = ''
            if len(temp[3]) >= 2:
                last_bomb_guess_x = temp[3][0]
                last_bomb_guess_y = temp[3][1]
            else:
                last_bomb_guess_x = ''
                last_bomb_guess_y = ''
            list = [
                name, kriging, width, height, bomb_x, bomb_y, num_robots,
                steps, finished, best_bomb_guess_x, best_bomb_guess_y,
                guess_at_step, last_bomb_guess_x, last_bomb_guess_y
            ]
            csvwriter.writerow(list)

    x_bar_bf = mean(steps_bf)
    sigma_bf = stdev(steps_bf, x_bar_bf)
    print()
    print('Brute Force')
    print('----------------------')
    print('Mean Steps: ', x_bar_bf)
    print('Std Dev Steps: ', sigma_bf)

    x_bar_bf_k = mean(steps_bf_k)
    sigma_bf_k = stdev(steps_bf_k, x_bar_bf_k)
    print()
    print('Brute Force + Kriging')
    print('----------------------')
    print('Mean Steps: ', x_bar_bf_k)
    print('Std Dev Steps: ', sigma_bf_k)

    x_bar_ph = mean(steps_ph)
    sigma_ph = stdev(steps_ph, x_bar_ph)
    print()
    print('Phylogenetic')
    print('----------------------')
    print('Mean Steps: ', x_bar_ph)
    print('Std Dev Steps: ', sigma_ph)

    x_bar_ph_k = mean(steps_ph_k)
    sigma_ph_k = stdev(steps_ph_k, x_bar_ph_k)
    print()
    print('Phylogenetic + Kriging')
    print('----------------------')
    print('Mean Steps: ', x_bar_ph_k)
    print('Std Dev Steps: ', sigma_ph_k)
            self.weights.append(weight)

        print(total)

    def calculate_z(self):
        for index in range(len(self.points)):
            temp = self.weights[index] * self.points[index][2]
            # print(self.points[index][2], '->', temp)
            self.estimate += temp
            # print(self.estimate)
            # print(self.weights[index], '->', self.points[index][2])


if __name__ == "__main__":
    np.set_printoptions(linewidth=300, precision=3)
    heat_map = Grid(26, 26)
    heat_map.init_bomb(2, 3, 10)
    # heat_map.cells[10][0] = 5
    # heat_map.cells[7][1] = 6
    # heat_map.cells[13][1] = 6
    # heat_map.cells[9][3] = 8
    # heat_map.cells[13][2] = 7
    # heat_map.cells[4][0] = 4
    # heat_map.cells[8][8] = 8
    # heat_map.cells[12][8] = 8
    # heat_map.cells[10][9] = 7

    k = EasyKriging(heat_map, 2, 3)
    k.get_points()
    print(k.points)
    k.calculate_weights()