Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code an artificial neural network (Tic-tac-toe)? [closed]

I want to play Tic-tac-toe using an artificial neural network. My configuration for the network is as follows: For each of the 9 fields, I use 2 input neuron. So I have 18 input neurons, of course. For every field, I have 1 input neuron for a piece of Player 1 and 1 neuron for a piece of Player 2. In addition to that, I have 1 output neuron which gives an evaluation of the current board position. The higher the output value is, the better is the position for Player 1. The lower it is, the better is it for Player 2.

But my problem is: How could I code that neural network? My idea was to use an Array[1-18] for the input neurons. The values of this array are the input weights. The I would walk through the array using a loop. Whenever there is a neuron to be activated, I add the weight to the output value. So the output value is the sum of the weights of the activated input neurons:

Output = SUM(ActivatedInputNeurons)

Do you think this is a good way of programming the network? Do you have better ideas?

I hope you can help me. Thanks in advance!

like image 801
caw Avatar asked Apr 17 '09 16:04

caw


People also ask

What is the most appropriate formal model to represent the game of tic tac toe?

This paper presents the design model of Tic tac toe Game using Multi-Tape Turing Machine in which both player choose input randomly and result of the game is declared. The computational Model of Tic tac toe is used to describe it in a formal manner.

What is a neural network in coding?

What is a neural network ? Based on nature, neural networks are the usual representation we make of the brain : neurons interconnected to other neurons which forms a network. A simple information transits in a lot of them before becoming an actual thing, like “move the hand to pick up this pencil”.


3 Answers

Well, you have an input layer of 18 neurons, and an output layer of 1 neuron. That's OK. However, you need to give your neural net the opportunity to put the inputs into relation. For that, you need at least one intermediate layer. I would propose to use 9 neurons in the intermediate layer. Each of these should be connected to each input neuron, and the output neuron should be connected to each intermediate. Each such connection has a weight, and each neuron has an activation level.

Then, you go through all neurons, a layer at a time. The input layer is just activated with the board state. For all further neurons, you go through all its respective connections and sum over the product of the connected neuron's activation level and the weight of the connection. Finally, you calculate the activation level by applying a sigmoid function on this sum.

This is the working principle. Now, you need to train this net to get better results. There are several algorithms for this, you will have to do some googling and reading. Finally, you might want to adjust the number of neurons and layers when the results don't get convincing fast enough. For example, you could reduce the input layer to 9 neurons and activate them with +1 for an X and -1 for an O. Perhaps adding another intermediate layer yields better results, or increasing the number of neurons of a layer.

like image 154
Svante Avatar answered Oct 15 '22 17:10

Svante


I don't particularly understand how you expect to get a meaningful summary of the board situation out of one output neuron. I would more look at having:

    I I I             O O O
    I I I      x      O O O
    I I I             O O O
9 input neurons  9 output neurons

in a fully connected network, i.e. 81 weights. Then train the output neurons for the relative desirability of playing in that position.

like image 33
chaos Avatar answered Oct 15 '22 17:10

chaos


Have a look at my Tic project. I've solved this problem with both neural network and genetic algorithm. The source code is freely available.

http://www.roncemer.com/tic-tac-toe-an-experiment-in-machine-learning

like image 34
Ron Avatar answered Oct 15 '22 18:10

Ron