Position.directional_offset doesn't normalize resultant position, and probably should


#1

The code in the halite docks is as such:

return self + Position(*direction)

where “add” is:

return Position(self.x + other.x, self.y + other.y)

so in the end the resultant position is not normalized. I really think this should be the default, as an unnormalized position is useless.


#2

I can not agree with you. Position __add__ method is uses quite often and should be fast. Also normalization requires map size constants which is not available for Position instance. And GameMap.__getitem__ do normalization by default (source). So it is useless only if your store positions in list or other container. (by default Position can not be used as dict key or as hashed set element)


#3

I agree with the OP - one of the first things I did was to normalise this function (and make it rely on a map), because having unnormalised positions floating around your main code is a recipe for bugs, for example when comparing for equality. Normalising is hardly slow; I’d be rather surprised if it is relevant to the performance of any real bot.


#4

I doubt normalization would ever be a factor in time, it’s time complexity is O(1). And storing positions in a list is pretty much fundamental to all bots, and because nearly all other functions normalize, I expected this position to normalize, but it doesn’t, so it took a good few hours of searching to figure out why it wasn’t working.

Updating the docs to have this function be normalized wouldn’t affect any bot except for the better either, as any bot that already has “normalize” around this position will just do it twice, meaning there are no negative implications I can think of.


#5

There are still problem with access to map size constans, impelementing this requires changing scopes of responsibility of hlt.positionals and hlt.game_map. I am not very happy with map sizes constants as GameMap attributes but not constants package ones. However I don’t think anyone will change it now.
P.S. For my bot I rewrite about half of the hlt code (I even don’t have Position.directional_offset method) so there definelty a lot thing that can be improved. But many of them are controversial or can break someone’s bots.