Halite Al python。我的机器人无法运行

时间:2018-01-07 06:49:46

标签: python

这是我的机器人代码。我希望机器人停靠在不在地图中心行星上的最近行星上。所以我找到了arr_distances()中的距离,它返回了它的键是planet.id的dict,它的值是距离然后我订购了它基于值

    """
Welcome to your first Halite-II bot!

This bot's name is Settler. It's purpose is simple (don't expect it to win complex games :) ):
1. Initialize game
2. If a ship is not docked and there are unowned planets
2.a. Try to Dock in the planet if close enough
2.b If not, go towards the planet

Note: Please do not place print statements here as they are used to communicate with the Halite engine. If you need
to log anything use the logging module.
"""
# Let's start by importing the Halite Starter Kit so we can interface with the Halite engine
import hlt
# Then let's import the logging module so we can print out information
import logging
from collections import OrderedDict
import math

# GAME START
# Here we define the bot's name as Settler and initialize the game, including communication with the Halite engine.
game = hlt.Game("Settler")
# Then we print our start message to the logs
logging.info("Starting my Settler bot!")

def arr_distances(ship, game_map):
    p_distances = {}
    for planet  in game_map.all_planets():
        distance = math.sqrt((float(planet.x) - float(ship.x)) ** 2.0 + (float(planet.y) - float(ship.y)) ** 2.0)
        p_distances[planet.id] = distance
    p_distances = OrderedDict(sorted(p_distances.items(), key = lambda t: t[1]))
    return p_distances

while True:
    # TURN START
    # Update the map for the new turn and get the latest version
    game_map = game.update_map()

    # Here we define the set of commands to be sent to the Halite engine at the end of the turn
    command_queue = []
    # For every ship that I control
    for ship in game_map.get_me().all_ships():
        distances = arr_distances(ship, game_map)
        # If the ship is docked
        if ship.docking_status != ship.DockingStatus.UNDOCKED:
            # Skip this ship
            continue

        # For each planet in the game (only non-destroyed planets are included)
        for planet in distances:
            # If the planet is owned
            if planet.is_owned():
                # Skip this planet
                continue

            # If we can dock, let's (try to) dock. If two ships try to dock at once, neither will be able to.
            if ship.can_dock(planet):
                # We add the command by appending it to the command_queue
                command_queue.append(ship.dock(planet))
            else:
                # If we can't dock, we move towards the closest empty point near this planet (by using closest_point_to)
                # with constant speed. Don't worry about pathfinding for now, as the command will do it for you.
                # We run this navigate command each turn until we arrive to get the latest move.
                # Here we move at half our maximum speed to better control the ships
                # In order to execute faster we also choose to ignore ship collision calculations during navigation.
                # This will mean that you have a higher probability of crashing into ships, but it also means you will
                # make move decisions much quicker. As your skill progresses and your moves turn more optimal you may
                # wish to turn that option off.
                navigate_command = ship.navigate(
                    ship.closest_point_to(planet),
                    game_map,
                    speed=int(hlt.constants.MAX_SPEED),
                    ignore_ships=True)
                # If the move is possible, add it to the command_queue (if there are too many obstacles on the way
                # or we are trapped (or we reached our destination!), navigate_command will return null;
                # don't fret though, we can run the command again the next turn)
                if navigate_command:
                    command_queue.append(navigate_command)
            break

    # Send our set of commands to the Halite engine for this turn
    game.send_command_queue(command_queue)
    # TURN END
# GAME END

并且cmd上此bot的输出是:

E:\halite competition\mybot\Halite2_Python3_Windows>halite.exe -d "240 160" "python MyBot.py" "python MyBot.py"
Game constants: all default
Seed: 197516812 Dimensions: 240x160
Init Message sent to player 0.
Init Message sent to player 1.
Init Message received from player 0, Settler.
Init Message received from player 1, Settler.
Turn 1
ERROR: Bot #0: Received invalid character 'Traceback (most recent call last):T'. (at character 1.)
Input received from bot:
Traceback (most recent call last):
^
ERROR: Bot #1: Received invalid character 'Traceback (most recent call last):T'. (at character 1.)
Input received from bot:
Traceback (most recent call last):
^
Player 0 is dead
Bot 0 was killed.
Here is the rest of its output (if any):
 "MyBot.py", line 58, in <module>
    if planet.is_owned():
AttributeError: 'int' object has no attribute 'is_owned'
--- End bot output ---
Player 1 is dead
Skipping replay (bot errored on first turn).
Player #0, Settler, came in rank #2 and was last alive on frame #0, producing 0 ships and dealing 0 damage!
Player #1, Settler, came in rank #1 and was last alive on frame #0, producing 0 ships and dealing 0 damage!

我不知道为什么机器人无法工作。

3 个答案:

答案 0 :(得分:0)

我认为将代码中的块更改为如下将修复该错误:

distances

这是因为arr_distancesarr_distances设置为OrderedDict返回的对象。从代码段中的函数来看,arr_distances似乎返回list

或者,您可以更改p_distances = sorted(p_distances.items(), key = lambda t: t[1]) return p_distances 以返回public class KreatorZamowien { public int Id { get; set; } public int NumerZamowienia { get; set; } public virtual ICollection<Nabywca> Nabywcy { get; set; } = new Hashset<Nabywca>(); } public class Nabywca { public int NabywcaId { get; set; } public string Nazwa { get; set; } public virtual ICollection<KreatorZamowien> KreatoryZamowien { get; set; } = new Hashset<KreatorZamowien>(); } public ActionResult Create(KreatorZamowienNabywca model) { model.KreatorZamowien.Nabywcy.Add(model.Nabywca); model.Nabywca.KreatoryZamowien.Add(model.KreatorZamowien); db.KreatoryZamowien.Add(model.KreatorZamowien); db.Nabywcy.Add(model.Nabywca); db.SaveChanges(); }

{{1}}

希望有所帮助。

答案 1 :(得分:0)

你可以使用行星的ID获取或锁定一个行星 PLANET的ID是一个整数,你需要找到ID ==你的P.DISTANCE的行星[KEY] 那么DOCK PLANET

for planet in game_map.all_planets():
for i in distances:
    if planet.id == i:
        if planet.is_owned():
            continue

        # If we can dock, let's (try to) dock. If two ships try to dock at once, neither will be able to.
        if ship.can_dock(planet):
            # We add the command by appending it to the command_queue
            command_queue.append(ship.dock(planet))
        else:
            # If we can't dock, we move towards the closest empty point near this planet (by using closest_point_to)
            # with constant speed. Don't worry about pathfinding for now, as the command will do it for you.
            # We run this navigate command each turn until we arrive to get the latest move.
            # Here we move at half our maximum speed to better control the ships
            # In order to execute faster we also choose to ignore ship collision calculations during navigation.
            # This will mean that you have a higher probability of crashing into ships, but it also means you will
            # make move decisions much quicker. As your skill progresses and your moves turn more optimal you may
            # wish to turn that option off.
            navigate_command = ship.navigate(
                ship.closest_point_to(planet),
                game_map,
                speed=int(hlt.constants.MAX_SPEED),
                ignore_ships=True)
            # If the move is possible, add it to the command_queue (if there are too many obstacles on the way
            # or we are trapped (or we reached our destination!), navigate_command will return null;
            # don't fret though, we can run the command again the next turn)
            if navigate_command:
                command_queue.append(navigate_command)
        break

答案 2 :(得分:0)

所以我查看了你的代码,只是从地球上删除了id属性,但你究竟想要你的机器人做什么?因为当我运行这个你的机器人直接到中心行星,而不是在附近的任何其他地方

    # Let's start by importing the Halite Starter Kit so we can interface with 
    the Halite engine
    import hlt
# Then let's import the logging module so we can print out information
import logging
from collections import OrderedDict
import math

# GAME START
# Here we define the bot's name as Settler and initialize the game, including communication with the Halite engine.
game = hlt.Game("Settler")
# Then we print our start message to the logs
logging.info("Starting my Settler bot!")

def arr_distances(ship, game_map):
    p_distances = {}
    for planet  in game_map.all_planets():
        distance = math.sqrt((float(planet.x) - float(ship.x)) ** 2.0 + (float(planet.y) - float(ship.y)) ** 2.0)
        p_distances[planet] = distance
    p_distances = OrderedDict(sorted(p_distances.items(), key = lambda t: t[1]))
    return p_distancese Halite engine at the end of the turn