# GANN-EWN / Part 2 / Building a Hand-Crafted Player For EWN

Welcome to this second part of the GANN-EWN (Genetic Algorithms for Neural Networks applied to the game “Einstein Würfelt Nicht”) series! If you haven’t read it yet, it’s probably time to read the first part.

The game is quite simple: at the beginning of the game every player has 6 pieces labeled from 1 to 6, and every player throws a die at every turn. Depending on the result of the throw, the player may move one or two of his own pieces. The goal is to reach the opposite side of the board (diagonally) or capture all opponent’s pieces.

The rules with the dice are simple as well. If the die corresponds exactly to one of his pieces, he has to play that piece in any direction forward (the three squares: up, up left, or left), like in here where the die shows “5”:

If no piece with the same number are on the board, then one of the two pieces that are the closest to the die can be played. For instance, if the die was “2”, then the “1” and “3” can be played here:

Again, if the die is “3” then on this board the “2” or the “5” can be played:

Note that if there was no “2” on this board, then only the “5” could be played since there is no lower number than “3” on the board.

The first thing to do was to code a simple board implementing the rules of the game, which is quite straightforward. I chose Java, which is probably the programming language I’m most comfortable with, but also for another reason, which I will develop later.

To have something to compare my neural networks to, and also to test my implementation of the EWN Board and game logic, my first first step was to implement a simple (and I can call it quite “naive”) implementation of a player, based on my very limited experience of the game. Remember that I just discovered EWN, so after having a look at some games from the best players, I realized that one common strategy is to “eat up” some of your own pieces early in the game, to give momentum to your remaining pieces. Although this is a “classic” strategy, it does have one drawback: you opponent may be able to take all your remaining pieces and you lose. Nevertheless, I decided to implement this rule, along with a few others. Here is the list of rules that are hard-coded in this first player:

• if one move is a winning move (capture the last opponent’s piece or reach the opposite side of the board), then play it,
• if an opponent piece is close to winning (basically at a certain distance from the goal), always take it,
• always take your own pieces if they are within a certain number (typically from 2 to 5) if you have more than a certain amount of pieces (you don’t want to take your pieces if you have too few of them, that could be suicide in some situations),
• move forward always the most advanced piece (rushing towards the goal),
• preferably move forward-up rather than simply forward or up.

And that’s it!

So yes, this is a very simple player, but one funny fact is that this player beats me consistently! That’s how weak I am at EWN as a player.

But as those rules can have parameters (how far must an adversary piece so that we capture it? etc.), I ran a simple computer simulation using a range of possible parameters (including totally disabling a rule). And those simulation showed me that, by far, the best parameters are:

• capture pieces that are at most at a distance of 2 from the goal, but actually this rule is not very important,
• take your own pieces from 2 to 5 until you have only 2 pieces left,
• move always forward-up,
• do advance the most advanced piece.

Of course, every single rule taken on its own may have some exceptions, but my “simple player” doesn’t go that far.

I also created a “random” player which picks moves at random, again to have something to compare to that doesn’t vary much in its crappiness. 🙂 And indeed my simple player was definitely better than the random one, at least.

I published this first player on the littlegolem site, with a different user than my regular user.

This is when I discovered that the site was actually crawling with bots playing EWN! Nice, the challenge was getting interesting! That’s when I met with a German researcher who also wrote a bot to play EWN on littlegolem. He even made part of his PhD on this project, so needless to say that my own simple player was quite ridiculous compared to his, and got a very bad beating!

This is when I realized that EWN is actually a statistics-driven game, since you make a choice depending on the probabilities of the next dice throws. You have a “2” ahead and want to give it momentum to reach the opposite side of the board faster? Capture your own “1”, “3” and possibly “4” and “5” pieces. The enemy has one piece slightly ahead but that piece has a 1/6 chance to be picked by the dice? Just ignore it. But you will capture that piece if it has a 1/2 chance of being played. As the Wikipedia page points out, one of the first approach is to calculate probability tables for each piece to reach the goal. All the dynamics of that game are based on probabilities. Well, almost. So that makes it a very good candidate for a Monte-Carlo type approach.

As a reminder, the general principle behind the Monte-Carlo approach is: “at a given turn, for each possible move play as many random games as possible and play the one that leads to victory more often than the others.”

Brutal. But very effective and the easiest “AI” to program. I guess. At least on a CPU.

So I implemented a simple Monte-Carlo (that I will abbreviate as “MC” now on) algorithm and launched it against my simple player. And sure enough, the MC player won, even if it was just playing 1000 games with random moves to decide which move to pick. And I’m not speaking about a “little better” here. The MC player won almost 70 games out of 100.

Then I thought: picking random moves is not really a likely outcome for a normal game, so the results for the MC player are somehow skewed. What if I use my simple player instead of a random player to play the games?

No surprise, using the simple player within the MC player improved the results against the simple player by quite a bit. But against the random MC player, the difference was not that big, it just improved it slightly, but not really significantly, only by 2.5 to 3% which is not bad but not as much as I would have expected. Note that running this simulation on a CPU is costing a lot of cycles. 1000 games with each roughly 15 to 20 moves. And at each turn, play 1000 games for every possible move (there are 1 to 6 possible moves). That’s a lot of moves to run! I’ve accelerated that to run on a GPU, but that will come later.

And still, the MC player lost to the German PhD’s carefully hand-crafted player. Well if a simple MC approach would do it, that’s what he would have picked in the first place!

This is it for this part. Next, we’ll start building a Neural Network with its associated Genetic Algorithm.

## 3 thoughts on “GANN-EWN / Part 2 / Building a Hand-Crafted Player For EWN”

1. Interesting article. But if I may add, you crossover & mutation parameters seems a bit bizarre. *Generally*, one expects xover to be over 06% (0.6) and mutation to be roughly between 1/L (length of your chromosome) and 10% (0.1). As for the elitism parameter, it’s a trial an error or a question of personal taste. I always prefer a slower convergence (thus a very small elite) over orienting the search directly into a local maximum/minimum.

Obviously, there is no “magical setting” that fits all problems. But if I recall correctly, I remember reading about your quest (optimizing a NN with GA) and mutation was always in the 1-5 % and crossover was mostly always greater than 0.75.

What’s your selection policy and crossover type?

Btw, I’m also into GA (https://endormitoire.wordpress.com/?s=Freewill) and I’m also on Little Golem (https://www.littlegolem.net/jsp/info/player.jsp?plid=41994) playing EWN.

And this series of article just gave me the push I needed to code my own EWN bot now!

Wanna exchange ideas? Je parle aussi français en passant!

Bonjour du Québec cher ami! 😉

1. Jean-Yves Toumit says:

It is great that I can be a catalyst for others to try out their own solutions. 🙂

The selection policy is basically the players that win most games against others, but I’m planning to write (many) more articles on the subject where I give more details. The fact is that my population is evolving very slowly when it reaches a given level at the game, because it is required to play so many games to make sure that one player is indeed better than another. The trick could be to retain the memory of past games and instead of playing full games, only play and simulate positions that really matter when it comes to be better or worse.

As for the crossover, it is a simple exchange of bits between one player and another. The problem here is that because it is transferring one region of a network into another network that may have a totally different structure, it is mostly not relevant and I don’t get very good results with crossovers so far. Most of the improvement comes from random mutations. Again, I’ll address those in more articles when I have time – I’m currently working on something else and days only have 24 hours. 🙂