Halite Home

Tips for making Python bots faster



I started making bots for this game a couple of days ago in the Python programming language.
However, I noticed that as it became more complex, it started to slow down significantly, to the point where it cannot finish a game on a 50x50 playing field.

An example of such a game:

If it is needed, I can upload the code as well.

Do any of you have any tips for making the bot faster so that it won't time out?




First of all, the game's timeout code appears to be slightly wonky in practice - you'll have to experiment to find out just how close you can cut it if your bot is close to a second.

Most importantly, you'll want to profile your bot code to see where it's spending it's time, and then speed that up. Look into cprofile and friends. Here's blog post with a few different ways to profile: https://julien.danjou.info/blog/2015/guide-to-python-profiling-cprofile-concrete-case-carbonara


All my timeout issues went away when I rewrote the basic functions in the starter package. Published it as an alt-starter-package here: https://2016.forums.halite.io/t/alternative-halite-starter-package-for-python3/539


Moving as much spatial logic as you can into numpy array operations will cause some crazy gains. A lot of the computation in Halite is very well suited to matrices.


The other suggestions are very good and if the replay you show is typical of your time outs,

Another couple of things I would do, is keep an eye on the time you took on the precious frame, and once it reaches a threshold (say 0.8 of a second), let the cells stand still longer, rather than 4*production, try 5 or 6*production will give you a significant reduction in computations with little loss when you are already ahead, then slowly increase it as required.

the second thing to do would be to record how long you have been moving a cell in the same direction, and continue to send the cell this direction, only calculating every second or third time (10-18 frames).

the third thing to do would be to look at the ambiturner code and use this heuristic when you are running out of time, perhaps work out how to sort your cells so you calculate the contested areas first then after you have used 1/2 the time finish with the ambiturner heuristic.