Exemplo n.º 1
0
def get_interception_point(steps_o, InitPosX, InitPosY, agentO: Agent):
    ip_x = -1
    ip_y = -1

    steps_counter = 0
    g = get_graph_from_board(agentO.gameBoard)
    start = Slot(InitPosX, InitPosY)
    path_to = []
    for step in steps_o:
        steps_counter += 1

        # computing the distance should be done using dijkstra, since now we allow obstacles
        # create graph from Board (with obstacles)

        dijkstra(g, g.get_vertex(start), g.get_vertex(step))

        target = g.get_vertex(step)
        path = [target.get_id()]
        shortest(target, path)
        # print('The shortest path : %s' % (path[::-1]))

        distance_from_i_r = len(path[::-1])
        if steps_counter > distance_from_i_r:
            ip_x, ip_y = step.row, step.col
            path_to = path[::-1]
            break

    assert (ip_x != -1 and ip_y != -1)

    return Slot(ip_x, ip_y), len(path_to), path_to
    def get_steps(self, agent_r, board_size=50, agent_o=None):
        """
            This function returns the coverage self.steps,when covering knowing io, and start covering vertically from the farthest
            corner.
            :param self:
            :param board_size:
            :param agent_o:
            :return: the coverage self.steps.
            """
        assert agent_o is not None

        # go to the farthest corner
        self.steps.extend(
            Strategy.go_from_a_to_b(a=Slot(agent_r.InitPosX, agent_r.InitPosY),
                                    b=Strategy.get_farthest_corner(
                                        a=Slot(agent_o.InitPosX,
                                               agent_o.InitPosY),
                                        board_size=board_size)))

        # from there, cover vertically
        current_slot = self.steps[-1]
        v_dir = 'u' if current_slot.row == board_size - 1 else 'd'
        h_dir = 'l' if current_slot.col == board_size - 1 else 'r'
        counter = 1
        while counter <= board_size * board_size:
            if v_dir == 'u':
                while current_slot.row > 0:
                    current_slot = current_slot.go_north()
                    self.steps.append(current_slot)
                    counter += 1

                if counter == board_size * board_size:
                    break

                current_slot = current_slot.go_west(
                ) if h_dir == 'l' else current_slot.go_east()
                v_dir = 'd'
                self.steps.append(current_slot)
                counter += 1
                continue
            else:
                while current_slot.row < board_size - 1:
                    current_slot = current_slot.go_south()
                    self.steps.append(current_slot)
                    counter += 1

                if counter == board_size * board_size:
                    break

                current_slot = current_slot.go_west(
                ) if h_dir == 'l' else current_slot.go_east()
                v_dir = 'u'
                self.steps.append(current_slot)
                counter += 1
                continue

        return self.steps
Exemplo n.º 3
0
    def get_steps(self, agent_r, board_size = 50, agent_o = None):
        next_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)
        flag = (agent_r.InitPosX == board_size - 1)
        counter = 0

        # print "init pos {},{}: ".format(agent_r.InitPosX, agent_r.InitPosY)

        while True:
            counter += 1
            if counter > board_size * board_size:
                break

            self.steps.append(next_slot)
            # in the middle of moving from bottom rpw to top row
            if flag:
                if next_slot.col == board_size - 1:
                    flag = False
                    next_slot = next_slot.go_north()
                else:
                    next_slot = next_slot.go_east()
                if next_slot == Slot(agent_r.InitPosX, agent_r.InitPosY):
                    break
                continue
            # check if in last position, and start moving from last row to top row
            elif next_slot.row == board_size - 1 - 1 and next_slot.col == 0:
                flag = True
                next_slot = next_slot.go_south()

                if next_slot == Slot(agent_r.InitPosX, agent_r.InitPosY):
                    break
                continue
            # update next slot
            elif next_slot.col % 2 != 0:
                if next_slot.row == 0:
                    next_slot = next_slot.go_west()
                else:
                    next_slot = next_slot.go_north()
            else:
                if next_slot.row == board_size - 1 - 1:
                    next_slot = next_slot.go_west()
                else:
                    next_slot = next_slot.go_south()
                    if next_slot.row > board_size - 1:
                        print("+1")

            if next_slot == Slot(agent_r.InitPosX, agent_r.InitPosY):
                break

        return self.steps
    def get_steps(self, agent_r, board_size=50, agent_o=None):
        """
        This function returns the coverage steps, choosing to start from the initial position of the opponent, io, and from
        there cover the world circling out.
        This function perform spiraling out simply: doesn't take steps in alternate sizes, and limiting the available slots
        to be in range
        :param self: the agent
        :param board_size:
        :param agent_o:
        :return:
        """

        fixed_io = Slot(agent_o.InitPosX, agent_o.InitPosY)
        self.steps.extend(
            Strategy.go_from_a_to_b(Slot(agent_r.InitPosX, agent_r.InitPosY),
                                    fixed_io))

        # cover the world, circling from this position out.
        current_slot = self.steps[-1]
        step_size = 1
        counter = 1

        b = [[0 for i in xrange(board_size)] for j in xrange(board_size)]
        b[current_slot.row][current_slot.col] = 1

        while sum(map(sum, b)) < board_size * board_size:
            # go horizontally right then vertically up
            for dir in ['r', 'u']:
                for _ in xrange(step_size):
                    current_slot = current_slot.go(dir)
                    counter += 1
                    if 0 <= current_slot.row < board_size and 0 <= current_slot.col < board_size:
                        self.steps.append(current_slot)
                        b[current_slot.row][current_slot.col] = 1

            step_size += 1
            # go horizontally left then vertically down
            for dir in ['l', 'd']:
                for _ in xrange(step_size):
                    current_slot = current_slot.go(dir)
                    counter += 1
                    if 0 <= current_slot.row < board_size and 0 <= current_slot.col < board_size:
                        self.steps.append(current_slot)
                        b[current_slot.row][current_slot.col] = 1
            step_size += 1
        return self.steps
Exemplo n.º 5
0
    def get_steps(self, agent_r, board_size=50, agent_o=None):
        next_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)

        flag = (agent_r.InitPosY == board_size - 1
                and not (agent_r.InitPosX == 0))

        steps = []
        counter = 0

        while True:
            counter += 1
            if counter > 1000000000000:
                break

            steps.append(next_slot)
            # in the middle of moving from bottom row to top row
            if flag:
                next_slot = next_slot.go_north()
                if next_slot.row == 0:
                    flag = False
                if next_slot == Slot(agent_r.InitPosX, agent_r.InitPosY):
                    break
                continue
            # check if in last position, and start moving from last row to top row
            elif next_slot.row == board_size - 1 and next_slot.col == board_size - 1 - 1:
                flag = True
                next_slot = next_slot.go_east()
                if next_slot == Slot(agent_r.InitPosX, agent_r.InitPosY):
                    break
                continue
            # update next slot
            elif next_slot.row % 2 != 0:
                if next_slot.col == board_size - 1 - 1:
                    next_slot = next_slot.go_south()
                else:
                    next_slot = next_slot.go_east()
            else:
                if next_slot.col == 0:
                    next_slot = next_slot.go_south()
                else:
                    next_slot = next_slot.go_west()

            if next_slot == Slot(agent_r.InitPosX, agent_r.InitPosY):
                break

        return steps
    def get_steps(self, agent_r: Agent, board_size=50, agent_o: Agent = None):
        covered_cells = []
        current_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)
        covered_cells.append(current_slot)
        all_slots = [
            Slot(i, j) for i in range(board_size) for j in range(board_size)
        ]
        while not sublist(all_slots, covered_cells):
            # randomly select move
            new_slot = current_slot
            while not (0 <= new_slot.row < board_size and 0 <= new_slot.col <
                       board_size) or new_slot == current_slot:
                new_slot = current_slot.go(random.choice(['r', 'l', 'u', 'd']))

            # update accordingly
            covered_cells.append(new_slot)
            current_slot = new_slot
            print(len(set(covered_cells)) / (board_size * board_size * 1.0))
        print(covered_cells)
        print(len(covered_cells))
        exit(2)
        return covered_cells
Exemplo n.º 7
0
def cover_current_level(level, current: Slot, board: Board, leveled_slots):
    slots = [current]
    current_slot = current
    level_amount = len([i for i in leveled_slots.values() if i == level])

    # print("covering level %d" % leveled_slots.get(current_slot))

    leveled_neighbors = lambda slot: [
        i for i in current_slot.get_inbound_neighbors(board)
        if leveled_slots.get(i) == level
    ]
    nonpresent_leveled_neighbors = lambda slot, l: [
        i for i in leveled_neighbors(slot) if i not in l
    ]

    # go toward a neighbor of the same level, covering until having a single neighbor with level value
    uncovered_inbound_neighbors = nonpresent_leveled_neighbors(
        current_slot, slots)
    while uncovered_inbound_neighbors:
        current_slot = uncovered_inbound_neighbors.pop()
        slots.append(current_slot)
        uncovered_inbound_neighbors = nonpresent_leveled_neighbors(
            current_slot, slots)

    # if not covered all of this level, go the the opposite direction and cover until all level is covered
    doubly_covered_slots = []
    # if reached to this point, go until there is a cell with uncovered neighbor, and from there cover until done.
    # if len(set(slots)) < level_amount:
    # go until at a cell with uncovered neighbor
    previous_slot = Slot(-1, -1)
    while len(set(slots)) < level_amount:
        uncovered_leveled_slots = nonpresent_leveled_neighbors(
            current_slot, doubly_covered_slots)
        if not uncovered_leveled_slots:
            break

        temp = current_slot
        l = [
            i for i in uncovered_leveled_slots if i != previous_slot
            and not is_slot_shallow_obstacle(i, board.Obstacles)
        ]
        if not l:
            return slots[1::]
        current_slot = l[0]
        previous_slot = temp
        doubly_covered_slots.append(current_slot)
        slots.append(current_slot)

    return slots[1:]
Exemplo n.º 8
0
def test_positions(positions):
    ir=Slot(positions[0][0], positions[0][1])
    io=Slot(positions[1][0], positions[1][1])
    run_game(ir, io)
    def get_steps(self, agent_r, board_size=50, agent_o=None):
        """
            This function return the agent_r steps, when deciding to cover the world, spiraling from inside to outside.
            Note: this coverage method is not optimal!
            :param agent_r: the agent covering the world
            :param board_size: self explanatory
            :return: list of steps
            """

        self.steps.extend(
            self.go_from_a_to_b(Slot(agent_r.InitPosX, agent_r.InitPosY),
                                Slot(board_size / 2, board_size / 2)))

        next_slot = self.steps[-1]
        # next_slot = Slot(36,6)

        # start by going toward the center
        if next_slot.row < board_size / 2:
            while next_slot.row < board_size / 2:
                next_slot = next_slot.go_south()
                self.steps.append(next_slot)
                continue
        else:
            while next_slot.row > board_size / 2:
                next_slot = next_slot.go_north()
                self.steps.append(next_slot)
                continue

        if next_slot.col < board_size / 2:
            while next_slot.col < board_size / 2:
                next_slot = next_slot.go_east()
                self.steps.append(next_slot)
                continue
        else:
            while next_slot.col > board_size / 2:
                next_slot = next_slot.go_west()
                self.steps.append(next_slot)
                continue

        # after reaching the center, start covering, counter clockwise
        circ = 0
        counter = 1
        while circ < board_size:
            circ += 1
            if circ < board_size:
                for _ in range(circ):
                    next_slot = next_slot.go_west()
                    self.steps.append(next_slot)
                    counter += 1
            if circ < board_size:
                for _ in range(circ):
                    next_slot = next_slot.go_north()
                    self.steps.append(next_slot)
                    counter += 1

            circ += 1
            if circ < board_size:
                for _ in range(circ):
                    next_slot = next_slot.go_east()
                    self.steps.append(next_slot)
                    counter += 1
            if circ < board_size:
                for _ in range(circ):
                    next_slot = next_slot.go_south()
                    self.steps.append(next_slot)
                    counter += 1

        for step in self.steps:
            if step.row > board_size - 1 or step.col > board_size - 1 or step.row < 0 or step.col < 0:
                print(
                    "Error while circling outside from board center: reached unavailable position"
                )
                break
        return self.steps
Exemplo n.º 10
0
    def get_steps(self, agent_r, board_size=50, agent_o=None):
        """
        This function returns the coverage self.steps, when covering knowing io, starting from the a corner adjacent to
        io, and covering semi-cyclic - covering the closer layers first.
        :param self:
        :param board_size:
        :param agent_o:
        :return: the coverage self.steps.
        """
        assert agent_o is not None

        # go to the adjacent corner
        steps_to_start = Strategy.go_from_a_to_b(
            a=Slot(agent_r.InitPosX, agent_r.InitPosY),
            b=Strategy.get_adjacent_corner(a=Slot(agent_o.InitPosX,
                                                  agent_o.InitPosY),
                                           board_size=board_size,
                                           first_option=self.first_option))
        for i in steps_to_start:
            self.add_step(i)

        # from there, cover semi-cyclic
        current_slot = self.steps[-1]
        v_dir = 'u' if current_slot.row == board_size - 1 else 'd'
        h_dir = 'r' if current_slot.col == board_size - 1 else 'l'
        start_vertical = True
        distance = 1
        counter = 1

        # initial horizontal step
        current_slot = current_slot.go_west(
        ) if h_dir == 'r' else current_slot.go_east()
        self.add_step(current_slot)

        counter += 1

        while counter <= board_size * board_size and distance < board_size:
            if start_vertical:
                # going vertically
                for _ in range(distance):
                    current_slot = current_slot.go_north(
                    ) if v_dir == 'u' else current_slot.go_south()
                    self.add_step(current_slot)
                    counter += 1

                # going horizontally
                for _ in range(distance):
                    current_slot = current_slot.go_west(
                    ) if h_dir == 'l' else current_slot.go_east()
                    self.add_step(current_slot)
                    counter += 1

                # final vertical step
                if counter < board_size * board_size:
                    current_slot = current_slot.go_north(
                    ) if v_dir == 'u' else current_slot.go_south()
                    self.add_step(current_slot)
                    counter += 1

            else:
                # going horizontally
                for _ in range(distance):
                    current_slot = current_slot.go_west(
                    ) if h_dir == 'l' else current_slot.go_east()
                    self.add_step(current_slot)
                    counter += 1

                # going vertically
                for _ in range(distance):
                    current_slot = current_slot.go_north(
                    ) if v_dir == 'u' else current_slot.go_south()
                    self.add_step(current_slot)
                    counter += 1

                # final horizontal step
                if counter < board_size * board_size:
                    current_slot = current_slot.go_west(
                    ) if h_dir == 'l' else current_slot.go_east()
                    self.add_step(current_slot)
                    counter += 1

            start_vertical = not start_vertical
            h_dir = 'r' if h_dir == 'l' else 'l'
            v_dir = 'u' if v_dir == 'd' else 'd'

            distance += 1

        return self.steps
Exemplo n.º 11
0
    def get_steps(self, agent_r, board_size = 50, agent_o = None):
        """
            This function return the agent steps, when deciding to cover the world, spiraling from outside to inside.
            Note: this coverage method is not optimal!
            :param agent: the agent covering the world
            :param board_size: self explanatory
            :return: list of steps
            """

        next_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)
        # todo: impreve performance by using extending of lists
        # start by going toward the closest corner
        if next_slot.row < board_size / 2:
            while next_slot.row > 0:
                self.steps.append(next_slot)
                next_slot = next_slot.go_north()
                continue
        else:
            while next_slot.row < board_size - 1:
                self.steps.append(next_slot)
                next_slot = next_slot.go_south()
                continue

        if next_slot.col < board_size / 2:
            while next_slot.col > 0:
                self.steps.append(next_slot)
                next_slot = next_slot.go_west()
                continue
        else:
            while next_slot.col < board_size - 1:
                self.steps.append(next_slot)
                next_slot = next_slot.go_east()
                continue
        self.steps.append(next_slot)

        # after reaching the closest-to-start corner, start covering the world, counter clockwise
        shallow_slots = [[0 for x in range(board_size)] for y in range(board_size)]
        shallow_slots[next_slot.row][next_slot.col] = 1
        dist_from_edge = 0

        while dist_from_edge < board_size / 2:
            if next_slot.row + dist_from_edge < board_size - 1 and next_slot.col == dist_from_edge:
                direction = 's'
            elif next_slot.row + dist_from_edge == board_size - 1 and next_slot.col + dist_from_edge < board_size - 1:
                direction = 'e'
            elif next_slot.row > dist_from_edge and next_slot.col + dist_from_edge == board_size - 1:
                direction = 'n'
            elif next_slot.row == dist_from_edge and next_slot.col + dist_from_edge >= board_size - 1:
                direction = 'w'

            if direction == 's':
                new_slot = next_slot.go_south()
            elif direction == 'e':
                new_slot = next_slot.go_east()
            elif direction == 'n':
                new_slot = next_slot.go_north()
            elif direction == 'w':
                new_slot = next_slot.go_west()

            if shallow_slots[new_slot.row][new_slot.col] == 1:
                dist_from_edge += 1
                continue
            else:
                next_slot = new_slot

            self.steps.append(next_slot)
            shallow_slots[next_slot.row][next_slot.col] = 1

        return self.steps
Exemplo n.º 12
0
    def get_steps(self, agent_r: Agent, board_size=50, agent_o: Agent = None):
        assert agent_o is not None

        # 1. perform bfs and set LEVEL values
        # 2. go to cell with highest LEVEL value
        # 3. while not all cells covered:
        #   3.1. cover current LEVEL
        #   3.2. if next level adjacent, go there
        #   3.3  if not all cells are covered, go to next level (search)

        covered_slots = []

        # get the 'farthest' cell from R's initial location
        initial_level_assignment = assign_level_to_slots(
            board=agent_o.gameBoard,
            init=Slot(agent_o.InitPosX, agent_o.InitPosY),
            levelType=4)
        farthest_slot = max(initial_level_assignment.items(),
                            key=operator.itemgetter(1))[0]

        # 1. perform bfs
        board = agent_o.gameBoard
        leveled_slots = assign_level_to_slots(board, farthest_slot)

        # define lambda
        def there_are_cells_to_cover():
            return len(set(self.steps)) + len(
                board.get_shallow_obstacles()) < board.Rows * board.Cols

        def distance(a, b):
            return fabs(a.row - b.row) + fabs(a.col - b.col)

        def edges_and_distance_score(slot: Slot):
            return len(slot.get_inbound_neighbors(board)) * 10 + distance(
                slot, Slot(agent_r.InitPosX, agent_r.InitPosY))

        max_level = min(leveled_slots.values())
        max_leveled_slots = [
            i for i in leveled_slots if leveled_slots[i] == max_level
        ]
        ordered_max_leveled_slots = sorted(max_leveled_slots,
                                           key=edges_and_distance_score)

        # 2. go to cell with highest LEVEL value
        # max_level_slot = max(leveled_slots.items(), key=operator.itemgetter(1))
        best_max_level_slot = ordered_max_leveled_slots[0]
        path_to_max_slot = Strategy.go_from_a_to_b_dijkstra(
            a=Slot(agent_r.InitPosX, agent_r.InitPosY),
            b=best_max_level_slot,
            board=board)
        self.steps.extend(path_to_max_slot)
        current_slot = best_max_level_slot
        covered_slots.append(current_slot)
        # covered_slots.extend(path_to_max_slot)

        # 3. while not all cells covered:
        while there_are_cells_to_cover():
            #   3.1. cover current LEVEL
            level_steps = cover_current_level(
                level=leveled_slots[current_slot],
                current=current_slot,
                board=board,
                leveled_slots=leveled_slots)
            self.steps.extend(level_steps)
            covered_slots.extend(level_steps)

            if level_steps:
                current_slot = level_steps[-1]

            #   3.2. if next level adjacent, go there
            preferred_n = Slot(-1, -1)
            current_slot_neighbors = [
                n for n in current_slot.get_inbound_neighbors(board)
                if not is_slot_shallow_obstacle(n, board.Obstacles)
            ]

            if any([
                (leveled_slots.get(i) == leveled_slots.get(current_slot) + 1)
                    for i in current_slot_neighbors if i not in covered_slots
            ]):
                for n in current_slot_neighbors:
                    if leveled_slots[n] == leveled_slots[current_slot] + 1:
                        if preferred_n == Slot(-1, -1) or len(
                                n.get_inbound_neighbors(board)) < len(
                                    preferred_n.get_inbound_neighbors(board)):
                            preferred_n = n

                if preferred_n != Slot(-1, -1):
                    if preferred_n.row == -1:
                        pass
                    current_slot = preferred_n
                    covered_slots.append(current_slot)
                    self.steps.append(current_slot)
                    # break
                else:
                    raise Exception("Unhandled Code! Slot: %s" % current_slot)
            else:
                # reaching this case means all current slot's neighbors are already covered. either coverage is done,
                # or we have reached a dead end, and should look for the closest uncovered cell.
                if not there_are_cells_to_cover():
                    break

                #   3.3  if next level not adjacent, and process not finished, search for next level
                #   (higher than 0) and go there
                #   3.4 from there, cover with decreasing level values until no more slots are to cover

                # find_closest_uncovered_slot
                cus = min(
                    [i for i in leveled_slots.keys() if i not in self.steps],
                    key=lambda a: distance(a, current_slot))
                current_slot = cus
                self.steps.append(current_slot)
                covered_slots.append(current_slot)
                continue
                # print("closest ucs is: %s" %cus)

        return self.steps
Exemplo n.º 13
0
def take_snapshots():
    import seaborn as sb
    num_samples = 100

    random_results = []
    steps_times_o = {}
    steps_times_r = {}
    for i in range(1024):
        steps_times_o[i] = []
    for i in range(1056):
        steps_times_r[i] = []

    for _ in tqdm(range(num_samples)):
        b = Board(32, 32)
        agentO = Agent("O", StrategyEnum.RandomSTC, 31, 31, board=b)
        agentR = Agent("R",
                       StrategyEnum.LONGEST_TO_REACH,
                       0,
                       0,
                       board=b,
                       agent_o=agentO)
        game = Game(agentR, agentO)
        game.run_game(enforce_paths_length=False)

        for os in range(len(agentO.steps)):
            step = agentO.steps[os]
            steps_times_o[os].append(step)

        random_results.append(game.get_r_gain())

    b = Board(32, 32)
    agentO = Agent("O", StrategyEnum.RandomSTC, 31, 31, board=b)
    agentR = Agent("R",
                   StrategyEnum.LONGEST_TO_REACH,
                   0,
                   0,
                   board=b,
                   agent_o=agentO)
    for os in range(len(agentR.steps)):
        step = agentR.steps[os]
        steps_times_r[os].append(step)

    from collections import Counter
    iterations = float(num_samples)
    to_t = []

    # for cell c, how much strategies cover it by time t\
    for t in tqdm(range(1024)):
        to_t.extend(steps_times_o[t])
        c = Counter(to_t)
        probes = [[c[Slot(i, j)] / iterations for j in range(32)]
                  for i in range(32)]
        probes.reverse()
        sb.heatmap(probes,
                   yticklabels=False,
                   xticklabels=False,
                   vmin=0,
                   vmax=1)
        plt.savefig('data_O/time_%s' % t)
        plt.close()

    for t in tqdm(range(1056)):
        to_t.extend(steps_times_r[t])
        c = Counter(to_t)
        probes = [[c[Slot(i, j)] / iterations for j in range(32)]
                  for i in range(32)]
        probes.reverse()
        sb.heatmap(probes, yticklabels=False, xticklabels=False, vmin=1)
        plt.savefig('data_R/time_%s' % t)
        plt.close()
Exemplo n.º 14
0
    def get_steps(self, agent_r: Agent, board_size=50, agent_o: Agent = None):
        assert agent_o is not None

        # go to the farthest corner
        self.steps.extend(
            Strategy.go_from_a_to_b(a=Slot(agent_r.InitPosX, agent_r.InitPosY),
                                    b=Slot(agent_o.InitPosX,
                                           agent_o.InitPosY)))

        # from there, cover semi-cyclic
        current_slot = self.steps[-1]
        v_dir = 'u' if current_slot.row == board_size - 1 else 'd'
        h_dir = 'r' if current_slot.col == board_size - 1 else 'l'
        start_vertical = True
        distance = 1
        counter = 1

        # initial horizontal step
        current_slot = current_slot.go_west(
        ) if h_dir == 'r' else current_slot.go_east()
        self.steps.append(current_slot)
        counter += 1

        while counter <= board_size * board_size and distance < board_size:
            if start_vertical:
                # going vertically
                for _ in range(distance):
                    current_slot = current_slot.go_north(
                    ) if v_dir == 'u' else current_slot.go_south()
                    self.steps.append(current_slot)
                    counter += 1

                # going horizontally
                for _ in range(distance):
                    current_slot = current_slot.go_west(
                    ) if h_dir == 'l' else current_slot.go_east()
                    self.steps.append(current_slot)
                    counter += 1

                # final vertical step
                if counter < board_size * board_size:
                    current_slot = current_slot.go_north(
                    ) if v_dir == 'u' else current_slot.go_south()
                    self.steps.append(current_slot)
                    counter += 1

            else:
                # going horizontally
                for _ in range(distance):
                    current_slot = current_slot.go_west(
                    ) if h_dir == 'l' else current_slot.go_east()
                    self.steps.append(current_slot)
                    counter += 1

                # going vertically
                for _ in range(distance):
                    current_slot = current_slot.go_north(
                    ) if v_dir == 'u' else current_slot.go_south()
                    self.steps.append(current_slot)
                    counter += 1

                # final horizontal step
                if counter < board_size * board_size:
                    current_slot = current_slot.go_west(
                    ) if h_dir == 'l' else current_slot.go_east()
                    self.steps.append(current_slot)
                    counter += 1

            start_vertical = not start_vertical
            h_dir = 'r' if h_dir == 'l' else 'l'
            v_dir = 'u' if v_dir == 'd' else 'd'

            distance += 1

        return self.steps
Exemplo n.º 15
0
    def get_steps(self, agent_r, board_size=50, agent_o=None):
        # This coverage strategy covers first the top left, then top right, then bottom left, then bottom right quarters of
        # the area.
        # While building this function, we assumed 100X100 dimensions

        next_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)
        # flag = (agent_r.InitPosY == boardSizeY - 1 and not (agent_r.InitPosX == 0))

        steps = []
        counter = 0

        while True:
            counter += 1

            if counter > 100000:
                print("Something is wrong, counter is too big!")
                print(steps)
                break

            if counter > 1 and (next_slot.row,
                                next_slot.col) == (agent_r.InitPosX,
                                                   agent_r.InitPosY):
                break

            steps.append(next_slot)

            # TL Quarter
            if 0 <= next_slot.row < board_size / 2 and 0 <= next_slot.col < board_size / 2:
                if (next_slot.row, next_slot.col) == (board_size / 2 - 1,
                                                      board_size / 2 - 1):
                    next_slot = next_slot.go_east()
                    continue
                if next_slot.col == 0:
                    next_slot = next_slot.go_north(
                    ) if next_slot.row > 0 else next_slot.go_east()
                    continue
                if next_slot.row % 2 == 0 and next_slot.row < board_size / 2 - 2:  # An even line, not the last one
                    next_slot = next_slot.go_east(
                    ) if not next_slot.col == board_size / 2 - 1 else next_slot.go_south(
                    )
                    continue
                elif next_slot.row % 2 != 0 and not next_slot.row == board_size / 2 - 1:  # An odd line, not before last
                    next_slot = next_slot.go_west(
                    ) if not next_slot.col == 1 else next_slot.go_south()
                    continue
                elif next_slot.row % 2 == 0 and next_slot.row == board_size / 2 - 2:  # An even line, the last one
                    next_slot = next_slot.go_south(
                    ) if next_slot.col % 2 != 0 else next_slot.go_east()
                elif next_slot.row % 2 != 0 and next_slot.row == board_size / 2 - 1:  # An odd line, last line
                    next_slot = next_slot.go_east(
                    ) if next_slot.col % 2 != 0 else next_slot.go_north()
                else:
                    print("TL: Error occurred! Should not reach here!")
                continue

            # TR Quarter
            elif 0 <= next_slot.row < board_size / 2 and board_size / 2 <= next_slot.col < board_size:
                if (next_slot.row, next_slot.col) == (board_size / 2 - 1,
                                                      board_size - 1):
                    next_slot = next_slot.go_south()
                    continue
                elif next_slot.col % 2 == 0:
                    next_slot = next_slot.go_north(
                    ) if next_slot.row > 0 else next_slot.go_east()
                    continue
                elif next_slot.col % 2 != 0:
                    next_slot = next_slot.go_south(
                    ) if next_slot.row < board_size / 2 - 1 else next_slot.go_east(
                    )
                    continue
                else:
                    print("TR: Error occurred! Should not reach here!")
                continue

            # BL Quarter
            elif board_size / 2 <= next_slot.row < board_size and 0 <= next_slot.col < board_size / 2:
                if (next_slot.row,
                        next_slot.col) == (board_size / 2,
                                           0):  # last cell of quarter
                    next_slot = next_slot.go_north()
                    continue
                elif next_slot.col % 2 == 0:  # an even column
                    next_slot = next_slot.go_north(
                    ) if next_slot.row > board_size / 2 else next_slot.go_west(
                    )
                    continue
                elif next_slot.col % 2 != 0:  # An odd line
                    next_slot = next_slot.go_south(
                    ) if next_slot.row < board_size - 1 else next_slot.go_west(
                    )
                    continue
                else:
                    print("BL: Error occurred! Should not reach here!")
                continue

            # BR Quarter
            else:
                if (next_slot.row,
                        next_slot.col) == (board_size / 2, board_size /
                                           2):  # last cell of quarter
                    next_slot = next_slot.go_west()
                    continue
                elif next_slot.col == board_size - 1:
                    next_slot = next_slot.go_south(
                    ) if not next_slot.row == board_size - 1 else next_slot.go_west(
                    )
                    continue
                elif next_slot.row % 2 != 0 and not next_slot.row == board_size / 2 + 1:  # and odd line, not before last
                    next_slot = next_slot.go_west(
                    ) if next_slot.col > board_size / 2 else next_slot.go_north(
                    )
                    continue
                elif next_slot.row % 2 != 0 and next_slot.row == board_size / 2 + 1:  # and odd line, DO before last
                    next_slot = next_slot.go_north(
                    ) if next_slot.col % 2 == 0 else next_slot.go_west()
                    continue
                elif next_slot.row % 2 == 0 and not next_slot.row == board_size / 2:  # an even line, not last one
                    next_slot = next_slot.go_east(
                    ) if next_slot.col < board_size - 2 else next_slot.go_north(
                    )
                    continue
                elif next_slot.row % 2 == 0 and next_slot.row == board_size / 2:  # an even line, INDEED last one
                    next_slot = next_slot.go_west(
                    ) if next_slot.col % 2 == 0 else next_slot.go_south()
                    continue
                else:
                    print("BR: Error occurred! Should not reach here!")
                continue

        return steps
Exemplo n.º 16
0
    def get_steps(self, agent_r, board_size = 50, agent_o = None):
        """
            Returns a non-circular vertical coverage, starting from agent_r's initial position to top-right position, then cover
            all cells from top-right to bottom-left, without repeating cells (there are some assumption regarding the initial
            position
            :param agent_r: the given agent_r
            :param board_size: board's width
            :param board_size: board's height
            :return: a set of steps covering the whole board
            """
        steps = []
        next_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)
        turning_slot = Slot(agent_r.InitPosX, agent_r.InitPosY)
        reaching_to_farthest_corner = True
        up_or_down = 'd'

        # assert call
        assert agent_r.InitPosX % 2 == 0
        while True:
            steps.append(next_slot)
            if len(steps) >= board_size * board_size:
                break

            # Check if we agent_r reached the farthest corner of the board
            if next_slot == Slot(board_size - 1, board_size - 1):
                reaching_to_farthest_corner = False

            if reaching_to_farthest_corner:
                if next_slot.row < board_size - 1:
                    next_slot = next_slot.go_south()
                    continue
                elif next_slot.col < board_size - 1:
                    next_slot = next_slot.go_east()
                    continue
            else:
                if next_slot.col > agent_r.InitPosY:
                    if next_slot.col % 2 != 0:
                        next_slot = next_slot.go_north() if next_slot.row > 0 else next_slot.go_west()
                    else:
                        next_slot = next_slot.go_south() if next_slot.row < board_size - 2 else next_slot.go_west()
                    continue
                else:
                    if up_or_down == 'd':
                        if next_slot.go_south() != turning_slot:
                            if next_slot.row < board_size - 1:
                                next_slot = next_slot.go_south()
                            else:
                                next_slot = next_slot.go_west()
                                up_or_down = 'u'
                            continue
                        else:
                            turning_slot = turning_slot.go_west().go_west().go_north().go_north()
                            steps.append(next_slot.go_west())
                            steps.append(next_slot.go_west().go_south())
                            next_slot = next_slot.go_west().go_south().go_south()
                            continue
                    else:
                        if next_slot != turning_slot:
                            if next_slot.row > 0:
                                next_slot = next_slot.go_north()
                            else:
                                next_slot = next_slot.go_west()
                                up_or_down = 'd'
                            continue
                        else:
                            steps.append(next_slot.go_east())
                            steps.append(next_slot.go_east().go_north())
                            next_slot = next_slot.go_east().go_north().go_north()
                            continue

        return steps[:board_size * board_size]
 def get_steps(self, agent_r, board_size = 50, agent_o = None,):
     return SpanningTreeCoverage.get_random_coverage_strategy(size=board_size,
                                                              i_r=Slot(agent_r.InitPosX, agent_r.InitPosY),
                                                              print_mst=True,
                                                              obstacles=agent_r.gameBoard.Obstacles)
Exemplo n.º 18
0
 def edges_and_distance_score(slot: Slot):
     return len(slot.get_inbound_neighbors(board)) * 10 + distance(
         slot, Slot(agent_r.InitPosX, agent_r.InitPosY))