Hello and welcome to an introduction to Halite, a competitive open-sourced Artificial Intelligence challenge. While searching for something like OpenAI's gym, but instead hoping for a way for players to compete directly against each other in enviornments, the people at Halite reached out to me.
Halite a multi-player game environment, where you write a bot in just about any major programming language, and you can compete against other people who have done the same thing. In the latest challenge, Halite II, the idea is to "mine planets" with your ships, which allows you to create more ships to further mine more planets. You can also engage in combat with other player's ships and even destroy the planets.
Once you've built an AI that you are proud of, you can submit your bot to the Halite website, where it will begin to compete against other players and earn a rank. You can see replays of your AI's matches, which look something like:
Aside from being an enjoyable challenge, this challenge is put forth by Two Sigma as both a general challenge but also as a means of finding talented programmers. Two Sigma is one of the largest Hedge Funds in the world, focusing on leveraging data and analysis in the field of finance. If you're interested in a career in data analysis, you might want to check out their Two Sigma Recruiting page, even if finance isn't a particularly interesting field to you, since you could easily work at a place like Two Sigma and never touch any finance stuff yourself. You can really think of them more as a data science company.
Alright, let's dive in. To begin, you'll want to head to Halite.io and create an account. Next, download the starter kit. For me, I am going to be working specifically with Python, and I will likely be on multiple operating systems, but, right now, I am on Windows, so I am going to grab the Python 3 version for Windows. Once downloaded, extract those files.
In here, you have the halite.exe
, which you can use to run the game locally to create replay files, along with the run_game.bat
and run_game.sh
. On Windows, you will use the run_game.bat
and run_game.sh
on Linux, for example.
You should also have a MyBot.py
, along with an hlt
directory. The MyBot.py
contains a super simple starter bot, which uses a bunch of utility/helper scripts pre-made for you in the hlt
directory. So, right now, your "bot" consists of your .py file and the hlt
directory since that's also code you're trying to import. Open the MyBot.py
file, and try to read through the logic. The idea here is that first we get all of our ships, then we're looking for any ships not currently docked. Next, we iterate through all of the planets, looking for a planet that doesn't have docked ships at it (making them "owned"). If our ship can dock, we attempt to dock, entering this action into a "command queue." The idea here with the command queue is that you might have many actions you want to take per "turn." You are controlling many ships. If we cannot dock, then we want to start looking for a new planet again, and begin navigating to it as our command. In order to "dock" at a planet, you need to be within close proximity. Now, each game turn takes place within the while True
loop. So you send all of your commands ONCE per iteration of the main game loop (the top-level while True). Once the game ends, the while True
will be broken and the game is over.
While this starter bot isn't going to win you any awards, it does actually work, and is a decent starting point. Let's run a game! To do this on Windows, you can just double click on the run_game.bat
file. You could also open a terminal, and run either run_game.bat
on Windows or run the run_game.sh
shell script on linux. For the curious, you can see what these scripts are doing by editing them:
halite.exe -d "240 160" "python MyBot.py" "python MyBot.py"
So really all this is doing is running a simple command for you, running halite.exe
with the -d
flag, which allows you to set dimensions (240x160 in the case above), and then you can insert players. In this case, we're making MyBot.py
compete against MyBot.py
, but you might find you create many bots, and then might want to run a few competitions between them to see who is better. If you're doing something with reinforcement learning, or genetic algorithms, or anything with degrees of randomness, running the same iteration against itself might prove to be a good way to build training data.
You can learn more about the Halite CLI (command line interface) here. There are a few more flags, and ways you can run even different programming language bots against each other.
Once you've run your bot, you should see a replay
file has been created. You can use this file to visualize how your AI did. You can easily do this by heading to the play page, and then drag-n-drop your file to the "replay a file" side. Once you do that, within a few seconds, you should see the game begins to replay, and you can watch the results.
This is a competition though, so let's say that we're happy with this bot! Now, all we need to do is upload this bot. There is a SUBMIT A BOT
button on the top right of the page, you can click this, and rather than uploading a replay file, we can submit a bot here. That said, we need to first zip it up! What we'll need to do is zip your bot file(s). In our case, we have MyBot.py
and the hlt
directory. See the submit your bot docs for more info, but, we want to just zip these things together, so let's highlight the hlt
directory and python file (MyBot.py
), then right click on one of them, choose send to
(On Windows), and then you can call it submission.zip
. Now, you can drag this into the left hand side of the submit bot page, and your bot will begin playing matches shortly. You can view the results from this page, or any time by clicking on your profile picture and heading to your profile. You can see the results here. At least for me, I was playing and competing in matches within a minute.
Now that you have the basics, you can head back to the starter bot, and begin to make tweaks. It might be wise, for example, to not send all of your ships to the same planet. You may also want to add in other forms of logic like, if you're winning, to begin destroying other planets. I've seen many matches start off with a clear victor, but end with the initial victor losing the lead to someone else. You might also want to prioritize the larger planets that have more resources. You can also engage in ship-to-ship combat, and much more. If you're interested, I will be continuing this series by building on top of the logic here, and then later taking a swing at doing deep learning with Halite. Alright, get to hacking and I'll see you in the next tutorial!