当前位置: 首页>>代码示例>>Python>>正文


Python Planet.distance方法代码示例

本文整理汇总了Python中planetwars.planet.Planet.distance方法的典型用法代码示例。如果您正苦于以下问题:Python Planet.distance方法的具体用法?Python Planet.distance怎么用?Python Planet.distance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在planetwars.planet.Planet的用法示例。


在下文中一共展示了Planet.distance方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MyBot

# 需要导入模块: from planetwars.planet import Planet [as 别名]
# 或者: from planetwars.planet.Planet import distance [as 别名]
class MyBot(BaseBot):

    def zeros(self,rows,cols):
        row = []
        data = []
        for i in range(cols):
            row.append(0)
        for i in range(rows):
            data.append(row[:])
        return data

    # v = list of item values or profit
    # w = list of item weight or cost
    # W = max weight or max cost for the knapsack
    def zeroOneKnapsack(self, v, w, W):
        # c is the cost matrix
        c = []
        n = len(v)
        c = self.zeros(n,W+1)
        for i in range(0,n):
            #for ever possible weight
            for j in range(0,W+1):
                        #can we add this item to this?
                if (w[i] > j):
                    c[i][j] = c[i-1][j]
                else:
                    c[i][j] = max(c[i-1][j],v[i] +c[i-1][j-w[i]])
        return [c[n-1][W], self.getUsedItems(w,c)]

    # w = list of item weight or cost
    # c = the cost matrix created by the dynamic programming solution
    def getUsedItems(self,w,c):
        # item count
        i = len(c)-1
        currentW =  len(c[0])-1
        # set everything to not marked
        marked = []
        for i in range(i+1):
            marked.append(0)
        while (i >= 0 and currentW >=0):
            if (i==0 and c[i][currentW] >0 )or c[i][currentW] != c[i-1][currentW]:
                marked[i] =1
                currentW = currentW-w[i]
            i = i-1
        return marked

    def total_fleet_ship_count(self, owner):
        return sum( [ fleet.ship_count for fleet in self.universe.find_fleets(owner) ] )

    def closest_enemy_planet_distance(self, p):
        return min((lambda ep:ep.distance(p))(ep) for ep in self.enemy_planets)

    def my_fleets_attacking(self, planet):
        return sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner == player.ME] )

    def closest_to_enemy_neutral_under_my_attack(self):
        best_distance = 1000000
        result_planet = None
        for planet in self.nobodies_planets:
            if self.my_fleets_attacking(planet) > 0:
                distance = self.enemy_com.distance(planet)
                if distance < best_distance:
                    best_distance = distance
                    result_planet = planet
        return result_planet

    def get_attack_ship_count_first_turn(self, planet_to_attack, my_home, enemy_home):
        my_dist = my_home.distance(planet_to_attack)
        enemy_dist = enemy_home.distance(planet_to_attack)
        #log.info("Distances for %s are %s %s" % (planet_to_attack, my_dist, enemy_dist))
        if my_dist < enemy_dist:
            return planet_to_attack.ship_count+1
        if my_dist == enemy_dist and planet_to_attack.ship_count <= planet_to_attack.growth_rate:
            return planet_to_attack.ship_count+1
        return 1000000

    def get_neutrals_under_enemy_attack(self):
        result = []
        for planet in self.nobodies_planets:
            if sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner in player.NOT_ME ] ) > 0:
                result.append(planet)
        return result

    def get_neutrals_under_my_attack(self):
        result = []
        for planet in self.nobodies_planets:
            if sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner == player.ME ] ) > 0:
                result.append(planet)
        return result

    def get_sum_enemy_growth_within_dist(self, planet, distance):
        result = 0
        for p in self.enemy_planets:
            if p.id != planet.id and p.distance(planet) <= distance:
                result += p.growth_rate
        return result

    def doPrep(self):
        log.info("Prep phase")

#.........这里部分代码省略.........
开发者ID:apinkin,项目名称:google_ai_bot,代码行数:103,代码来源:MyBot_117.py

示例2: MyBot

# 需要导入模块: from planetwars.planet import Planet [as 别名]
# 或者: from planetwars.planet.Planet import distance [as 别名]
class MyBot(BaseBot):

    def __init__(self, universe):
        self.universe = universe
        self.scheduled_moves_at_turn= {}

    def total_fleet_ship_count(self, owner):
        return sum( [ fleet.ship_count for fleet in self.universe.find_fleets(owner) ] )

    def get_neutrals_under_player_attack(self, player):
        result = []
        for planet in self.nobodies_planets:
            if sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner == player ] ) > 0:
                result.append(planet)
        return result

    def get_available_ships_within_distance(self, planet_to_attack, player, distance):
        result = 0
        for planet in self.universe.find_planets(player):
            if planet.id != planet_to_attack.id and planet.distance(planet_to_attack) <= distance and self.ships_needed[planet] == 0:
                ships_avail = self.ships_available[planet]
                if len(planet.attacking_fleets) == 0 and len(self.get_scheduled_fleets_from(planet)) == 0:
                    ships_avail += (distance-planet.distance(planet_to_attack)) * planet.growth_rate
                result += ships_avail
        return result

    def get_attack_score(self, planet_to_attack, future_owner, distance):
        attack_score = (self.max_distance_between_planets - distance + 40) * planet_to_attack.growth_rate
        if future_owner in player.ENEMIES:
            attack_score *= 2
        return attack_score

    def get_scheduled_fleets_to(self, planet):
        result = []
        for moves in self.scheduled_moves_at_turn.values():
            for move in moves:
                if move.target == planet:
                    distance = move.source.distance(move.target)
                    turns_remaining = distance + (move.turn - self.universe.game.turn_count)
                    fleet = Fleet(self.universe,12345,1, move.ship_count, move.source.id, move.target.id, distance, turns_remaining)
                    result.append(fleet)
        return result

    def get_scheduled_fleets_from(self, planet):
        result = []
        for moves in self.scheduled_moves_at_turn.values():
            for move in moves:
                if move.source == planet:
                    turns_remaining = move.turn - self.universe.game.turn_count
                    fleet = Fleet(self.universe,12345,1, move.ship_count, move.source.id, move.target.id, turns_remaining, turns_remaining)
                    result.append(fleet)
        return result

    def get_attack_ship_count_first_turn(self, planet_to_attack, my_home, enemy_home):
        my_dist = my_home.distance(planet_to_attack)
        enemy_dist = enemy_home.distance(planet_to_attack)
        if my_dist < enemy_dist:
            return planet_to_attack.ship_count+1
        if my_dist == enemy_dist and planet_to_attack.ship_count <= planet_to_attack.growth_rate:
            return planet_to_attack.ship_count+1
        return 1000000

    def closest_enemy_planet_distance(self, p):
        return min((lambda ep:ep.distance(p))(ep) for ep in self.enemy_planets)

    def my_fleets_attacking(self, planet):
        return sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner == player.ME] )

    def closest_to_enemy_neutral_under_my_attack(self):
        best_distance = 1000000
        result_planet = None
        for planet in self.nobodies_planets:
            if self.my_fleets_attacking(planet) > 0:
                distance = self.enemy_com.distance(planet)
                if distance < best_distance:
                    best_distance = distance
                    result_planet = planet
        return result_planet


    def doScheduled(self):
        log.info("Scheduled move phase")
        # execute delayed moves first
        if self.scheduled_moves_at_turn.has_key(self.current_turn):
            for move in self.scheduled_moves_at_turn[self.current_turn]:
                if move.ship_count <= move.source.ship_count and move.ship_count > 0 and move.source.owner == PLAYER1 and self.ships_available[move.source] >= move.ship_count:
                    move.source.send_fleet(move.target, move.ship_count)
                    self.ships_available[move.source] -= move.ship_count
                else:
                    log.info("Can't execute move: %s,  ships avail: %s" % (move, self.ships_available[move.source]))


    def doPrep(self):
        log.info("Prep phase")

        self.max_distance_between_planets = 0
        for p1 in self.all_planets:
            for p2 in self.all_planets:
                self.max_distance_between_planets = max(self.max_distance_between_planets, p1.distance(p2))
        #log.info("Max distance: %s" % self.max_distance_between_planets)
#.........这里部分代码省略.........
开发者ID:apinkin,项目名称:google_ai_bot,代码行数:103,代码来源:MyBot_200.py

示例3: MyBot

# 需要导入模块: from planetwars.planet import Planet [as 别名]
# 或者: from planetwars.planet.Planet import distance [as 别名]
class MyBot(BaseBot):

    def __init__(self, universe):
        self.universe = universe
        self.scheduled_moves_at_turn= {}

    def total_fleet_ship_count(self, owner):
        return sum( [ fleet.ship_count for fleet in self.universe.find_fleets(owner) ] )

    def get_neutrals_under_player_attack(self, player):
        result = []
        for planet in self.nobodies_planets:
            if sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner == player ] ) > 0:
                result.append(planet)
        return result

    def get_available_ships_within_distance(self, planet_to_attack, player, distance):
        result = 0
        for planet in (list(self.universe.find_planets(player)) + self.get_neutrals_under_player_attack(player)):
            if planet.id != planet_to_attack.id and planet.distance(planet_to_attack) <= distance and self.ships_needed[planet] == 0:
                ships_avail = self.ships_available_at_turn[planet][distance-planet.distance(planet_to_attack)]
#                if planet_to_attack.id == 0:
#                    log.info("get avail from %s = %s" % (planet, ships_avail))
                result += ships_avail
        return result

    def get_attack_score(self, planet_to_attack, future_owner, distance):
        turns = self.max_distance_between_planets - distance + HORIZON
        attack_score = turns * planet_to_attack.growth_rate
        if future_owner in player.ENEMIES:
            attack_score *= ATTACK_SCORE_ENEMY_MULTIPLIER
        return attack_score

    def get_attack_score_200(self, planet_to_attack, future_owner, distance):
        profit_turns = max(200 - self.current_turn - distance, 0)
        attack_score = profit_turns * planet_to_attack.growth_rate
        if future_owner in player.ENEMIES:
            attack_score *= 2
        return attack_score

    def get_scheduled_fleets_to(self, planet):
        result = []
        for moves in self.scheduled_moves_at_turn.values():
            for move in moves:
                if move.target == planet:
                    distance = move.source.distance(move.target)
                    turns_remaining = distance + (move.turn - self.universe.game.turn_count)
                    fleet = Fleet(self.universe,random.randint(1,1000000),1, move.ship_count, move.source.id, move.target.id, distance, turns_remaining)
                    result.append(fleet)
        return result

    def get_scheduled_fleets_from(self, planet):
        result = []
        for moves in self.scheduled_moves_at_turn.values():
            for move in moves:
                if move.source == planet:
                    turns_remaining = move.turn - self.universe.game.turn_count
                    fleet = Fleet(self.universe,random.randint(1,1000000),1, move.ship_count, move.source.id, move.target.id, turns_remaining, turns_remaining)
                    result.append(fleet)
        return result

    def get_scheduled_fleets_shipcount_from_within_distance(self, planet, turns):
        result = 0
        for moves in self.scheduled_moves_at_turn.values():
            for move in moves:
                if move.source == planet:
                    turns_remaining = move.turn - self.universe.game.turn_count
                    if turns_remaining == turns:
                        result += move.ship_count
        return result

    def get_attack_ship_count_first_turn(self, planet_to_attack, my_home, enemy_home):
        my_dist = my_home.distance(planet_to_attack)
        enemy_dist = enemy_home.distance(planet_to_attack)
        if my_dist < enemy_dist:
            return planet_to_attack.ship_count+1
        if my_dist == enemy_dist and planet_to_attack.ship_count <= planet_to_attack.growth_rate:
            return planet_to_attack.ship_count+1
        return 1000000

    def closest_enemy_planet(self, p):
        if len(self.enemy_planets) == 0:
            return None

        sorted_planets = sorted(self.enemy_planets, key=lambda ep : p.distance(ep) + ep.id/1000000.0)
        return sorted_planets[0]

    def closest_enemy_planet_distance(self, p):
        return min((lambda ep:ep.distance(p))(ep) for ep in self.enemy_planets)

    def my_fleets_attacking(self, planet):
        return sum( [ 1 for fleet in planet.attacking_fleets if fleet.owner == player.ME] )

    def closest_to_enemy_neutral_under_my_attack(self):
        best_distance = 1000000
        result_planet = None
        for planet in self.nobodies_planets:
            if self.my_fleets_attacking(planet) > 0:
                distance = self.enemy_com.distance(planet)
                if distance < best_distance:
#.........这里部分代码省略.........
开发者ID:apinkin,项目名称:google_ai_bot,代码行数:103,代码来源:MyBot_212.py

示例4: MyBot

# 需要导入模块: from planetwars.planet import Planet [as 别名]
# 或者: from planetwars.planet.Planet import distance [as 别名]
class MyBot(BaseBot):

    def total_fleet_ship_count(self, owner):
        return sum( [ fleet.ship_count for fleet in self.universe.find_fleets(owner) ] )

    def closest_enemy_planet_distance(self, p):
        return min((lambda ep:ep.distance(p))(ep) for ep in self.universe.enemy_planets)

    def enemy_ships_reinforcing(self, planet, turn):
        return sum( [ fleet.ship_count for fleet in planet.reinforcement_fleets if fleet.owner in player.NOT_ME and fleet.turns_remaining <= turn ] )

    def doPrep(self):
        log.info("Prep phase")

        self.max_distance_between_planets = 0
        for p1 in self.universe.all_planets:
            for p2 in self.universe.all_planets:
                self.max_distance_between_planets = max(self.max_distance_between_planets, p1.distance(p2))
        #log.info("Max distance: %s" % self.max_distance_between_planets)


        # calculate current high level metrics
        self.my_total_ships_available = 0
        self.my_total_ships = 0
        self.my_total_growth_rate = 0
        self.enemy_total_ships_available = 0
        self.enemy_total_ships = 0
        self.enemy_total_growth_rate = 0
        self.ships_available = {}
        self.ships_needed = {}
        self.planet_timeline = {}
        for planet in self.universe.all_planets:
            if len(planet.attacking_fleets) == 0:
                self.ships_available[planet] = planet.ship_count
                self.ships_needed[planet] = 0
                simulation_distance = self.max_distance_between_planets
                self.planet_timeline[planet] = planet.in_future_timeline(simulation_distance)
            else:
                simulation_distance = self.max_distance_between_planets
                self.planet_timeline[planet] = planet.in_future_timeline(simulation_distance)
                max_needed = 0
                min_available = 1000000
                #log.info("timeline for %s: %s" % (planet, self.planet_timeline[planet]))
                for step in self.planet_timeline[planet]:
                    owner = step[0]
                    ship_count = step[1]
                    if owner != planet.owner:
                        max_needed = max(max_needed, ship_count)
                    else:
                        min_available = min(min_available, ship_count)
                if max_needed > 0:
                    # do we bail if we are going to lose this planet anyway?
                    self.ships_available[planet] = 0
                    self.ships_needed[planet] = max_needed
                else:
                    self.ships_available[planet] = min_available
                    self.ships_needed[planet] = 0

            if (planet.owner == player.ME):
                self.my_total_ships_available += self.ships_available[planet]
                self.my_total_growth_rate += planet.growth_rate
                self.my_total_ships += planet.ship_count
            else:
                self.enemy_total_ships_available += self.ships_available[planet]
                self.enemy_total_growth_rate += planet.growth_rate
                self.enemy_total_ships += planet.ship_count
            #log.info("avail ships for %s: %s" % (planet, self.ships_available[planet]))

        # prevent initial overexpansion
        if self.universe.game.turn_count <= 2:
            for my_planet in self.universe.my_planets:
                for enemy_planet in self.universe.enemy_planets:
                    max_enemy_fleet = self.ships_available[enemy_planet]
                    distance = my_planet.distance(enemy_planet)
                    ships_needed_for_safety = max_enemy_fleet-distance*my_planet.growth_rate
                    if ships_needed_for_safety > (my_planet.ship_count - self.ships_available[my_planet]):
                        deficit = ships_needed_for_safety - (my_planet.ship_count - self.ships_available[my_planet])
                        #log.info("deficit for %s: %s" % (my_planet, deficit))
                        if deficit > self.ships_available[my_planet]:
                            deficit = self.ships_available[my_planet]

                        self.ships_available[my_planet] -= deficit
                        self.my_total_ships_available -= deficit
            
        self.my_total_ships += self.total_fleet_ship_count(player.ME)
        self.enemy_total_ships += self.total_fleet_ship_count(player.NOT_ME)

        # calculate enemy's center of mass
        weighted_x = 0
        weighted_y = 0
        div = 0
        for planet in self.universe.enemy_planets:
            weighted_x += planet.position.x * (self.ships_available[planet] + planet.growth_rate)
            weighted_y += planet.position.y * (self.ships_available[planet] + planet.growth_rate)
            div += self.ships_available[planet] + planet.growth_rate
        if div == 0:
            div = 1

        self.enemy_com = Planet(self.universe, 666, weighted_x/div, weighted_y/div, 2, 0, 0)

#.........这里部分代码省略.........
开发者ID:apinkin,项目名称:google_ai_bot,代码行数:103,代码来源:MyBot_100.py


注:本文中的planetwars.planet.Planet.distance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。