Exemplo n.º 1
0
    def get_top_N_averages(self):
        """
        POPULATE WHERE TOP N PLACES ARE, BASED ON HIGHEST AVERAGE CELL MANHATTAN
        THEN GET HIGHEST HALITE AMOUNT WITHIN THAT AREA
        BECAUSE THE HIGHEST CELL AVERAGE IS NOT ALWAYS THE HIGHEST HALITE CELL IN THAT AREA
        """
        ## NEW WAY (SHOULD BE MORE EFFICIENT AND BETTER POSITIONING)
        average_manhattan = copy.deepcopy(self.myMatrix.cell_average.halite)

        ## POPULATE UNAVAILABLE AREA CLOSE TO SHIPYARD
        self.populate_shipyards_unavailable()

        ## GET INDEXES OF TOP N AVERAGES
        for _ in range(MyConstants.build.top_n):
            keep_looking = True
            quit = False

            while keep_looking:
                ## GET TOP AVERAGE LOCATION
                value_top_ave, indexes = get_n_max_values(average_manhattan)
                index_top_ave = self.get_closest_to_shipyard(indexes)

                loc_top_ave = (index_top_ave[0], index_top_ave[1])
                pos_top_ave = Position(loc_top_ave[1],
                                       loc_top_ave[0])  ## Position(x, y)

                if self.unavailable_area[pos_top_ave.y][
                        pos_top_ave.x] != Matrix_val.UNAVAILABLE:
                    ## SET THE SURROUNDING TO UNAVAILABLE
                    populate_manhattan(self.unavailable_area,
                                       Matrix_val.UNAVAILABLE, pos_top_ave,
                                       MyConstants.build.min_dist_btw_docks,
                                       Option.REPLACE)

                    ## TOP N IS THE HIGHEST HALITE WITHIN THE HIGHEST AVERAGE
                    ## GET TOP HALITE WITHIN THIS AREA
                    loc_top_halite_normalized, pos_top_halite_normalized = self.get_closest_top_halite(
                        loc_top_ave, pos_top_ave)

                    ## POPULATE TOP N POSITIONS IN cell_average.top_N
                    self.myMatrix.cell_average.top_N[loc_top_halite_normalized[
                        0]][loc_top_halite_normalized[1]] = value_top_ave

                    ## TOP N IS THE HIGHEST AVERAGE
                    # self.myMatrix.cell_average.top_N[pos_top_ave.y][pos_top_ave.x] = value_top_ave

                    keep_looking = False

                ## CHANGE THIS TO ZERO SO IT WONT BE TAKEN AS HIGHEST AVERAGE LATER
                average_manhattan[pos_top_ave.y][
                    pos_top_ave.x] = Matrix_val.ZERO

                ## WHEN TOP AVERAGE IS BELOW THE TOTAL AVERAGE, WILL EXIT FOR LOOP
                if value_top_ave < self.myVars.average_halite:
                    quit = True
                    break

            if quit: break
Exemplo n.º 2
0
    def building_now(self):
        """
        MOVE SHIPS BUILDING NOW (CURRENTLY AT DOCK POSITION)
        """
        r, c = np.where(self.data.myMatrix.docks.manhattan ==
                        MyConstants.build.dock_manhattan)
        ships_on_docks = OrderedSet(self.data.myMatrix.locations.myShipsID[r,
                                                                           c])
        ships_building = ships_on_docks & self.data.mySets.ships_to_move

        for ship_id in sorted(ships_building):
            ship = self.data.game.me._ships.get(ship_id)
            cell_halite_amount = self.data.myMatrix.halite.amount[
                ship.position.y][ship.position.x]

            if self.withinLimit_ships():
                self.data.myVars.isBuilding = True
                if ship.halite_amount + self.data.game.me.halite_amount + cell_halite_amount >= 4000:
                    ## HAVE ENOUGH HALITE TO BUILD DOCK
                    self.data.game.me.halite_amount -= (4000 -
                                                        ship.halite_amount -
                                                        cell_halite_amount)
                    logging.debug(
                        "Shid id: {} building dock at position: {}".format(
                            ship.id, ship.position))
                    self.data.halite_stats.record_spent(BuildType.DOCK)
                    command = ship.make_dropoff()
                    self.data.command_queue.append(command)

                    ## CLEAR DOCK AREA, SO THAT OTHER SHIPS WILL NOT TRY TO BUILD ON IT
                    populate_manhattan(self.data.myMatrix.docks.manhattan,
                                       Matrix_val.ZERO, ship.position,
                                       MyConstants.build.dock_manhattan,
                                       Option.REPLACE)

                    self.data.init_data.myMatrix.docks.placement[
                        ship.position.y][ship.position.x] = Matrix_val.ZERO
                    self.data.init_data.myMatrix.docks.order[ship.position.y][
                        ship.position.x] = Matrix_val.NINETY
                else:
                    ## NOT ENOUGH HALITE YET, STAY STILL
                    self.data.myVars.isSaving = True  ## PREVENT SPAWNING SHIPS
                    direction = Direction.Still
                    command = ship.move(direction)
                    self.data.command_queue.append(command)

                ## RECORD INFO ALSO SHIP COUNTER PER DOCK
                dock_coord = (ship.position.y, ship.position.x)
                self.data.myDicts.ships_building_dock.setdefault(
                    dock_coord, set())
                self.data.myDicts.ships_building_dock[dock_coord].add(ship.id)
                self.mark_unsafe(ship, ship.position)
                self.data.mySets.ships_to_move.remove(ship.id)
Exemplo n.º 3
0
    def populate_dock_manhattan(self):
        if getattr(self, 'init_data', None):
            indexes = np.argwhere(
                self.init_data.myMatrix.docks.placement == Matrix_val.ONE)
        else:
            indexes = np.argwhere(
                self.myMatrix.docks.placement == Matrix_val.ONE)

        for y, x in indexes:
            for i in range(0, MyConstants.build.dock_manhattan):
                position = Position(x, y)
                populate_manhattan(self.myMatrix.docks.manhattan,
                                   Matrix_val.ONE, position, i,
                                   Option.CUMMULATIVE)
Exemplo n.º 4
0
    def populate_shipyards_unavailable(self):
        """
        POPULATE UNAVAILABLE AREA CLOSE TO SHIPYARD
        SO NO DOCK WILL BE TOO CLOSE TO SHIPYARD
        """
        ## POPULATE AROUND MY SHIPYARD
        populate_manhattan(self.unavailable_area, Matrix_val.UNAVAILABLE,
                           self.game.me.shipyard.position,
                           MyConstants.build.min_dist_btw_docks,
                           Option.REPLACE)

        ## POPULATE AROUND ENEMY SHIPYARD
        for id, player in self.game.players.items():
            if id != self.game.me.id:
                populate_manhattan(self.unavailable_area,
                                   Matrix_val.UNAVAILABLE,
                                   player.shipyard.position,
                                   MyConstants.build.min_dist_btw_enemy_docks,
                                   Option.REPLACE)
Exemplo n.º 5
0
    def populate_myShips(self):
        """
        POPULATE MATRIX LOCATIONS OF MY SHIP AND ITS IDs
        GATHER HALITE INFO AS WELL
        POPULATE SHIP CARGO
        POPULATE POTENTIAL COLLISION MATRIX
        POPULATE STUCK SHIPS
        """
        self.myDicts.players_info[self.game.my_id] = PlayerInfo()
        self.myDicts.players_info[
            self.game.my_id].halite_amount = self.game.me.halite_amount
        self.myDicts.players_info[self.game.my_id].num_ships = len(
            self.game.me.get_ships())

        for ship in self.game.me.get_ships():
            self.myDicts.players_info[
                self.game.my_id].halite_carried += ship.halite_amount
            self.myMatrix.locations.myShips[ship.position.y][
                ship.position.x] = Matrix_val.ONE
            self.myMatrix.locations.myShipsID[ship.position.y][
                ship.position.x] = ship.id
            self.myMatrix.locations.occupied[ship.position.y][
                ship.position.x] = Matrix_val.OCCUPIED
            self.myMatrix.locations.shipsCargo[ship.position.y][
                ship.position.x] = ship.halite_amount
            populate_manhattan(
                self.myMatrix.locations.potential_ally_collisions,
                Matrix_val.POTENTIAL_COLLISION, ship.position,
                MyConstants.DIRECT_NEIGHBOR_DISTANCE, Option.CUMMULATIVE)

            ## POPULATE STUCK SHIPS
            if self.myMatrix.halite.cost[ship.position.y][
                    ship.position.x] > ship.halite_amount:
                self.myMatrix.locations.stuck[ship.position.y][
                    ship.position.x] = Matrix_val.ONE

        if self.prev_data:
            self.myMatrix.sunken_ships = mySunkenShips(
                self.prev_data.matrix.locations.updated_myShipsID,
                self.myMatrix.locations.myShipsID)
Exemplo n.º 6
0
    def final_dock_placement(self):
        """
        POPULATE DOCK PLACEMENT
        """
        shipyard = self.game.me.shipyard
        matrix = copy.deepcopy(self.myMatrix.cell_average.top_N)

        ## OLD WAY
        print_matrix("Average: top N", self.myMatrix.cell_average.top_N)

        ## ELIMINATE TOP N CLOSE TO SHIPYARD
        populate_manhattan(matrix, Matrix_val.ZERO, shipyard.position,
                           MyConstants.build.min_dist_btw_docks,
                           Option.REPLACE)

        print_matrix("Eliminate close to shipyard: top N", matrix)

        ## ELIMINATE TOP N CLOSE TO ENEMY SHIPYARD
        for id, player in self.game.players.items():
            if id != self.game.me.id:
                enemyShipyard_position = player.shipyard.position
                populate_manhattan(matrix, Matrix_val.ZERO,
                                   enemyShipyard_position,
                                   MyConstants.build.min_dist_btw_docks,
                                   Option.REPLACE)

        print_matrix("Eliminate close to enemy shipyard: top N", matrix)

        ## GET COORD OF HIGHEST VALUE IN MATRIX
        ## LOCATED ON HIGHEST HALITE (WITH HIGHEST AVERAGE VALUE FROM THAT SECTION)
        curr_cell = (shipyard.position.y, shipyard.position.x)
        coord, distance, val = get_coord_closest(
            matrix.max(), matrix, self.myMatrix.distances.cell[curr_cell],
            Inequality.EQUAL)
        while val > 1:
            ## ELIMINATE TOP N CLOSE TO THIS AREA
            position = Position(coord[1], coord[0])
            populate_manhattan(matrix, Matrix_val.ONE, position,
                               MyConstants.build.min_dist_btw_docks,
                               Option.REPLACE)

            ## POPULATE DOCK PLACEMENT
            # # self.myMatrix.locations.dock_placement[position.y][position.x] = Matrix_val.ONE
            # for i in range(0, MyConstants.build.dock_manhattan):
            #     populate_manhattan(self.myMatrix.docks.placement, Matrix_val.ONE, position, i, Option.CUMMULATIVE)
            self.myMatrix.docks.placement[position.y][
                position.x] = Matrix_val.ONE

            ## GET COORD OF NEXT HIGHEST VALUE IN MATRIX
            coord, distance, val = get_coord_closest(
                matrix.max(), matrix, self.myMatrix.distances.cell[curr_cell],
                Inequality.EQUAL)

        print_matrix("Final dock placement", self.myMatrix.docks.placement)
Exemplo n.º 7
0
    def populate_enemyShips_influenced(self):
        """
        POPULATE MATRIX LOCATION OF ENEMY SHIPS AND ITS IDs
        GATHER HALITE INFO AS WELL
        POPULATE MATRIX WITH ENEMY CARGO
        POPULATE MATRIX WITH ENEMY INFLUENCE
        POPULATE MATRIX WITH POTENTIAL ENEMY COLLISIONS
        POPULATE MATRIX WITH ENGAGE ENEMY (CLOSE TO ENEMY)
        """
        enemy_id = None

        for id, player in self.game.players.items():
            if id != self.game.me.id:
                enemy_id = id
                self.myDicts.players_info[id] = PlayerInfo()
                self.myDicts.players_info[
                    id].halite_amount = player.halite_amount
                self.myDicts.players_info[id].num_ships = len(
                    player.get_ships())

                for ship in player.get_ships():
                    self.myDicts.players_info[
                        id].halite_carried += ship.halite_amount
                    self.myMatrix.locations.enemyShips[ship.position.y][
                        ship.position.x] = Matrix_val.ONE
                    self.myMatrix.locations.enemyShipsID[ship.position.y][
                        ship.position.x] = ship.id
                    self.myMatrix.locations.enemyShipsOwner[ship.position.y][
                        ship.position.x] = id
                    self.myMatrix.locations.shipsCargo[ship.position.y][
                        ship.position.x] = ship.halite_amount
                    self.myMatrix.halite.enemyCargo[ship.position.y][
                        ship.position.x] = ship.halite_amount
                    self.myMatrix.halite.updated_enemyCargo[ship.position.y][
                        ship.position.x] = ship.halite_amount

                    populate_manhattan(self.myMatrix.locations.influenced,
                                       Matrix_val.ONE, ship.position,
                                       constants.INSPIRATION_RADIUS,
                                       Option.CUMMULATIVE)

                    populate_manhattan(
                        self.myMatrix.locations.potential_enemy_collisions,
                        Matrix_val.POTENTIAL_COLLISION, ship.position,
                        MyConstants.DIRECT_NEIGHBOR_DISTANCE,
                        Option.CUMMULATIVE)

                    populate_manhattan(
                        self.myMatrix.locations.potential_enemy_cargo,
                        ship.halite_amount, ship.position,
                        MyConstants.DIRECT_NEIGHBOR_DISTANCE, Option.MINIMUM)

                    populate_manhattan(
                        self.myMatrix.locations.engage_influence,
                        Matrix_val.ONE, ship.position,
                        MyConstants.influence.engage_distance, Option.REPLACE)

                    for dist in range(
                            1, MyConstants.attack.engage_enemy_distance + 1):
                        populate_manhattan(
                            self.myMatrix.locations.engage_enemy[dist],
                            Matrix_val.ONE, ship.position, dist,
                            Option.REPLACE)

        ## CHECK IF KILLING SPREE SHOULD BE ENABLED
        if self.myDicts.players_info[enemy_id].num_ships * MyConstants.snipe.killing_spree_halite_ratio <= self.myDicts.players_info[self.game.me.id].num_ships \
            and self.myVars.ratio_left_halite <= MyConstants.snipe.killing_spree_halite_left \
            and len(self.game.players) == 2:
            self.myVars.on_killing_spree = True
Exemplo n.º 8
0
    def get_dock_order(self):
        """
        GET ORDER OF WHAT DOCKS TO BUILD
        """
        order_num = 1
        shipyard = self.game.me.shipyard
        shipyard_coord = (shipyard.position.y, shipyard.position.x)
        value_matrix = copy.deepcopy(self.myMatrix.cell_average.top_N)
        distance_coords = {shipyard_coord}

        ## ELIMINATE TOP N CLOSE TO SHIPYARD
        populate_manhattan(value_matrix, Matrix_val.ZERO, shipyard.position,
                           MyConstants.build.min_dist_btw_docks,
                           Option.REPLACE)

        print_matrix("Eliminate close to shipyard: top N", value_matrix)

        ## ELIMINATE TOP N CLOSE TO ENEMY SHIPYARD
        for id, player in self.game.players.items():
            if id != self.game.me.id:
                enemyShipyard_position = player.shipyard.position
                populate_manhattan(value_matrix, Matrix_val.ZERO,
                                   enemyShipyard_position,
                                   MyConstants.MIN_DIST_BTW_DOCKS,
                                   Option.REPLACE)

        print_matrix("Eliminate close to enemy shipyard: top N", value_matrix)

        ratio, distances = self.get_ratio(value_matrix, distance_coords)

        print_matrix("ratio order_num {}".format(order_num), ratio)

        ## GET HIGHEST RATIO
        coord, distance, val = get_coord_closest(ratio.max(), ratio, distances,
                                                 Inequality.EQUAL)

        while val > 0:
            ## POPULATE ORDER MATRIX
            position = Position(coord[1], coord[0])
            self.myMatrix.docks.order[position.y][position.x] = order_num
            order_num += 1

            ## ELIMINATE TOP N CLOSE TO THIS AREA
            populate_manhattan(value_matrix, Matrix_val.ZERO, position,
                               MyConstants.build.min_dist_btw_docks,
                               Option.REPLACE)

            print_matrix("order matrix", self.myMatrix.docks.order)

            ## CLEAR THAT LOCATION
            value_matrix[position.y][position.x] = Matrix_val.ZERO

            ## ADD THIS COORD TO DISTANCES TO TAKE INTO ACCOUNT
            distance_coords.add(coord)

            ratio, distances = self.get_ratio(value_matrix, distance_coords)

            print_matrix("ratio order_num {}".format(order_num), ratio)

            ## GET HIGHEST RATIO
            coord, distance, val = get_coord_closest(ratio.max(), ratio,
                                                     distances,
                                                     Inequality.EQUAL)