views:

1934

answers:

4

Hello!

I want to program a chess engine which learns to make good moves and win against other players. I've already coded a representation of the chess board and a function which outputs all possible moves. So I only need an evaluation function which says how good a given situation of the board is. Therefore, I would like to use an artificial neural network which should then evaluate a given position. The output should be a numerical value. The higher the value is, the better is the position for the white player.

My approach is to build a network of 385 neurons: There are six unique chess pieces and 64 fields on the board. So for every field we take 6 neurons (1 for every piece). If there is a white piece, the input value is 1. If there is a black piece, the value is -1. And if there is no piece of that sort on that field, the value is 0. In addition to that there should be 1 neuron for the player to move. If it is White's turn, the input value is 1 and if it's Black's turn, the value is -1.

I think that configuration of the neural network is quite good. But the main part is missing: How can I implement this neural network into a coding language (e.g. Delphi)? I think the weights for each neuron should be the same in the beginning. Depending on the result of a match, the weights should then be adjusted. But how? I think I should let 2 computer players (both using my engine) play against each other. If White wins, Black gets the feedback that its weights aren't good.

So it would be great if you could help me implementing the neural network into a coding language (best would be Delphi, otherwise pseudo-code). Thanks in advance!

+1  A: 

What you need to train a ANN is either something like backpropagation learning or some form of a genetic algorithm. But chess is such an complex game that it is unlikly that a simple ANN will learn to play it - even more if the learning process is unsupervised.

Further, your question does not say anything about the number of layers. You want to use 385 input neurons to encode the current situation. But how do you want to decide what to do? On neuron per field? Highest excitation wins? But there is often more than one possible move.

Further you will need several hidden layers - the functions that can be represented with an input and an output layer without hidden layer are really limited.

So I do not want to prevent you from trying it, but chances for a successful implemenation and training within say one year or so a practically zero.

I tried to build and train an ANN to play Tic-tac-toe when I was 16 years or so ... and I failed. I would suggest to try such an simple game first.

Daniel Brückner
The neural network should only evaluate a position. The other functions compute all possible moves. Then for every move, the resulting position is given to the neural network which gives a numerical value as the evaluation. For example, White would rather take a move leading to 4.5 than -6.2.
As Varkhan pointed out, that score function will be very wavy and is very hard to represent with an ANN.
Daniel Brückner
I cannot program Tic-tac-toe, either. I lack the know-how. Therefore I asked here how to implement such a neural network. In my opinion, a neural network is a quite abstract thing. I can imagine how it would work, but I have no idea how to code that. So I hope someone here can help me.
+5  A: 

Been there, done that. Since there is no continuity in your problem (the value of a position is not closely related to an other position with only 1 change in the value of one input), there is very little chance a NN would work. And it never did in my experiments.

I would rather see a simulated annealing system with an ad-hoc heuristic (of which there are plenty out there) to evaluate the value of the position...

However, if you are set on using a NN, is is relatively easy to represent. A general NN is simply a graph, with each node being a neuron. Each neuron has a current activation value, and a transition formula to compute the next activation value, based on input values, i.e. activation values of all the nodes that have a link to it.

A more classical NN, that is with an input layer, an output layer, identical neurons for each layer, and no time-dependency, can thus be represented by an array of input nodes, an array of output nodes, and a linked graph of nodes connecting those. Each node possesses a current activation value, and a list of nodes it forwards to. Computing the output value is simply setting the activations of the input neurons to the input values, and iterating through each subsequent layer in turn, computing the activation values from the previous layer using the transition formula. When you have reached the last (output) layer, you have your result.

Varkhan
But TD-Gammon learnt to play Backgammon using only a neural network, too. So it has to work somehow, hasn't it?
Blackgammon is a very different game from Chess... it replaces rule complexity and wide branching of possibilities with randomness. But NN are very good at dealing with statistical prediction, not so at pruning the tree of possible solutions.
Varkhan
Continuity wouldnt be a problem with enough internal nodes (for a back-prop NN) and training data. The trouble is that the number of nodes and amount of training required would make it infeasible. I agree that a NN is a bad solution to the problem.
geofftnz
So you would say that it isn't possible to play chess using artificial neural networks, at least using my network configuration?
Let's say, I would be surprised if you managed to make it play properly. NN are very well suited to numeric filtering tasks, where you need to take decisions based on some data that is numeric, and semi-continuous. They do not perform well on symbolic, discrete data like board configurations.
Varkhan
OK, thanks! Thus it would probably not work well. But I would like to test it, though. So I would be very happy about some coding tips or examples.
A: 

The main problem I see here is one of training. You say you want your ANN to take the current board position and evaluate how good it is for a player. (I assume you will take every possible move for a player, apply it to the current board state, evaluate via the ANN and then take the one with the highest output - ie: hill climbing)

Your options as I see them are:

  • Develop some heuristic function to evaluate the board state and train the network off that. But that begs the question of why use an ANN at all, when you could just use your heuristic.

  • Use some statistical measure such as "How many games were won by white or black from this board configuration?", which would give you a fitness value between white or black. The difficulty with that is the amount of training data required for the size of your problem space.

With the second option you could always feed it board sequences from grandmaster games and hope there is enough coverage for the ANN to develop a solution.

Due to the complexity of the problem I'd want to throw the largest network (ie: lots of internal nodes) at it as I could without slowing down the training too much.

geofftnz
Thanks. I've already tested that. It would work but you would need an unimaginable amount of training data - as you already wrote. In chess, there are about 2,28x10^46 possible positions, so you would never have enough training data for each position.
Yes, although the idea of a neural network is that it should be able to generalise given a limited training set. A lot depends on the complexity of the function you are trying to fit, which in the case of chess will be insane.
geofftnz
OK, you've convinced me. But how could you code it, though? I would love to test it, although I know now that my engine will never be a pro player.
A: 

I don't see why you can't have a neural net for a static evaluator if you also do some classic mini-max lookahead with alpha-beta pruning. Lots of Chess engines use minimax with a braindead static evaluator that just adds up the pieces or something; it doesn't matter so much if you have enough levels of minimax. I don't know how much of an improvement the net would make but there's little to lose. Training it would be tricky though. I'd suggest using an engine that looks ahead many moves (and takes loads of CPU etc) to train the evaluator for an engine that looks ahead fewer moves. That way you end up with an engine that doesn't take as much CPU (hopefully).

Silas S. Brown