How to make ship scan its environment?


#1

Hey there, I am an absolute beginner in AI. I just completed a beginners course in Web Dev, but that about it. Anyone know how I woud make the ship scan say a 5 *5 area around it and then go where it has the max amount of hailte and then return?


#2

To my knowledge, there is no direct way. However, you can still do it the hard way:

pos = ship.position
max = 0
for i in range(pos.x - 2, pos.x + 2):
for j in range(pos.y - 2, pos.y + 2):
if game_map[Position(i,j)].halite_amout > max:
max = game_map[Position(i,j)].halite_amount
max_pos = Position(i,j)

That should gives you the maximum halite and the position of this cell. You can then navigate to it using:

naive_navigate(ship, max_pos)

I hope this helped. Maybe the codes contains some errors, I wrote them on the fly, but try to fix it and the logic should be correct.


#3

Adding onto cxpox’s answer you can add more artificial intelligence to your search by adding a heuristic search element to each spot’s amount, for example by adding the cost of movement to spot, how much it would cost to return from that spot to the nearest drop off. This can be used to approximate the opportunity cost of moving to one square vs another, as the full equation of the expected cost of each square for each ship might take more than the 2 seconds allow.


#4

You can use something like:

def get_search_radius(ship, radius):
    """ returns a list of all positions within square radius of a ship """
    return [ship.position+Position(i, j) for i in range(-radius, radius+1) for j in range(-radius, radius+1)]

to return a list of all positions within a radius*radius grid. Then you can use whatever fitness or heuristic you want on the grid.

edit: You can also change the function to normalize the positions by using

def get_search_radius(ship, radius, game_map):
    """ returns a list of all positions within square radius of a ship """
    return [game_map.normalize(ship.position+Position(i, j)) for i in range(-radius, radius+1) for j in range(-radius, radius+1)]```