Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to train an ANN to play a card game?

I would like to teach an ANN to play Hearts, but I am stuck on how to actually perform the training.

A friend suggested to use weka for the implementation of the actual ANN, but I've never used it, so I'm leaning towards a custom implementation.

I have programmed the rules and I can let the computer play a game, choosing random but legal cards each turn.

Now I am at a loss of what to send to the ANN as input and how to extract output (decreasing amount of cards each turn, so I can't let each output neuron be a possible card) and how to teach it and when to perform teaching.

My guess is to give the ANN as input:

  • The cards that have been played previously, with metadata of which player has played which card
  • The cards on the table for this turn, also with the same metadata
  • The cards in the ANN's hand

And then have the output be 13 neurons (the maximal amount of cards per player), of which I take the most activated of the cards that still are in the ANN's hand.

I also don't really know when to teach it (after each turn or after each game), as it is beneficial to have all the penalty cards, but bad to have all but one penalty card.

Any and all help is appreciated. I don't really know where else to put this question.

I currently have it programmed in Swift, but it's only 200 lines and I know a few other languages, so I can translate it.

like image 246
vrwim Avatar asked Aug 05 '15 23:08

vrwim


People also ask

Can AI play games?

From AI taking on chess grandmasters, to taking part in game shows, the history of AI is littered with game playing. As cool as it is for AI programs to be able to win on Jeopardy!, or to play video games like DOTA 2 and Starcraft, it's not immediately clear on its value.


1 Answers

Note that neural networks might not be the best thing to use here. More on that at the end of the answer, I'll answer your questions first.

Now I am at a loss of what to send to the ANN as input and how to extract output (decreasing amount of cards each turn, so I can't let each output neuron be a possible card) and how to teach it and when to perform teaching.

ANNs require labeled input data. This means a pair (X, y) where X can be whatever (structured) data related to your problem and y is the list of correct answers you expect the ANN to learn for X.

For example, think about how you would learn math in school. The teacher will do a couple of exercises on the blackboard, and you will write those down. This is your training data.

Then, the teacher will invite you to the blackboard to do one on your own. You might not do so well at first, but he/she will guide you in the right direction. This is the training part.

Then, you'll have to do problems on your own, hopefully having learnt how.

The thing is, even this trivial example is much too complex for an ANN. An ANN usually takes in real-valued numbers and outputs one or more real-valued numbers. So it's actually much dumber than a grade schooler who learns about ax + b = 0 type equations.

For your particular problem, it can be hard to see how it fits in this format. As a whole, it doesn't: you can't present the ANN with a game and have it learn the moves, that is much too complex. You need to present it with something for which you have a correct numerical label associated with and you want the ANN to learn the underlying pattern.

To do this, you should break your problem up into subproblems. For example, input the current player's cards and expect as output the correct move.

The cards that have been played previously, with metadata of which player has played which card

The ANN should only care about the current player. I would not use metadata or any other information that identifies the players.

Giving it a history could get complicated. You might want recurrent neural networks for that.

The cards on the table for this turn, also with the same metadata

Yes, but again, I wouldn't use metadata.

The cards in the ANN's hand

Also good.

Make sure you have as many input units as the MAXIMUM number of cards you want to input (2 x total possible cards, for the cards in hand and those on the table). This will be a binary vector where the ith position is true if the card corresponding to that position exists in hand / on the table.

Then do the same for moves: you will have m binary output units, where the ith will be true if the ANN thinks you should do move i, where there are m possible moves in total (pick the max if m depends on stages in the game).

Your training data will also have to be in this format. For simplicity, let's say there can be at most 2 cards in hand and 2 on the table, out of a total of 5 cards, and we can choose from 2 moves (say fold and all in). Then a possible training instance is:

Xi = 1 0 0 1 0 0 0 0 1 1 (meaning cards 1 and 4 in hand, cards 4 and 5 on table) 
yi = 0 1 (meaning you should go all in in this case)

I also don't really know when to teach it (after each turn or after each game), as it is beneficial to have all the penalty cards, but bad to have all but one penalty card.

You should gather a lot of labeled training data in the format I described, train it on that, and then use it. You will need thousands or even tens of thousands of games to see good performance. Teaching it after each turn or game is unlikely to do well.

This will lead to very large neural networks. Another thing that you might try is to predict who will win given a current game configuration. This will significantly reduce the number of output units, making learning easier. For example, given the cards currently on the table and in hand, what is the probability that the current player will win? With enough training data, neural networks can attempt to learn these probabilities.

There are obvious shortcomings: the need for large training data sets. There is no memory of how the game has gone so far (unless you use much more advanced nets).

For games such as these, I suggest you read about reinforcement learning, or dedicated algorithms for your particular game. You're not going to have much luck teaching an ANN to play chess for example, and I doubt you will teaching it to play a card game.

like image 191
IVlad Avatar answered Nov 18 '22 18:11

IVlad