예제 #1
0
 //called after the game
 public void trainNeuralNetwork(int result, int[] usedBy)
 {
     oldLayers     = layers;
     layersOfNN[1] = layers;
     double[] spotSuccess = new double[usedBy.Length];
     if (result != 0)
     {
         for (int i = 0; i < usedBy.Length; i++)
         {
             if (usedBy[i] == result)
             {
                 //good, improve weight
                 spotSuccess[i] = 1;
             }
             else if (usedBy[i] != 0)
             {
                 //bad, decrease weights, also makes sure not an unfilled spot
                 spotSuccess[i] = 0;
             }
             else
             {
                 spotSuccess[i] = -1;
             }
         }
         for (int i = 0; i < layers[layers.Count - 1].Neurons.Count; i++)
         {
             //finds valuue needs to change by for baddies
             if (spotSuccess[i] != -1)
             {
                 Neuron n = layers[layers.Count - 1].Neurons[i];
                 n.Delta = n.Value * (1 - n.Value) * (spotSuccess[i] - n.Value);
                 //goes back layer by layer while excluding output and input
                 for (int j = layers.Count - 2; j > 2; j--)
                 {
                     for (int k = 0; k < layers[j].Neurons.Count; k++)
                     {
                         Neuron n2 = layers[j].Neurons[k];
                         n.Delta = n.Value * (1 - n.Value) * layers[j + 1].Neurons[i].Dendrites[k].Weight * layers[j + 1].Neurons[i].Delta;
                     }
                 }
             }
         }
         //goes through each layer except input and reassigns values
         for (int i = layers.Count - 1; i >= 1; i--)
         {
             for (int j = 0; j < layers[i].NeuronCount; j++)
             {
                 Neuron n = layers[i].Neurons[j];
                 n.Bias = n.Bias + (learningRate * n.Delta);
                 for (int k = 0; k < n.DendriteCount; k++)
                 {
                     n.Dendrites[k].Weight = n.Dendrites[k].Weight + (learningRate * layers[i - 1].Neurons[k].Value * n.Delta);
                 }
             }
         }
         if (layers == oldLayers)
         {
             resetOldLayer();
         }
         saveToDatabase();
     }
     else
     {
         resetOldLayer();
     }
 }
예제 #2
0
        public double[] getMoveValues(int[] usedBy, bool PlayerOne)
        {
            double[] inputs = new double[usedBy.Length];

            double[] outputs = new double[usedBy.Length];
            //assign weights
            for (int i = 0; i < usedBy.Length; i++)
            {
                inputs[i] = usedBy[i];
            }
            //goes through each layer

            /*  for (int i = 0; i < layers.Count; i++)
             * {
             *    Layer lay = layers[i];
             *
             *    for (int n = 0; n < lay.NeuronCount; n++)
             *    {
             *        Neuron neuron = lay.Neurons[n];
             *        //sets first layer values to be input
             *        if (i == 0)
             *        {
             *            neuron.Value = inputs[n];
             *        }
             *        else
             *        {
             *            //does the calculations for layers other than 1
             *            neuron.Value = 0;
             *            for (int np = 0; np < layers[i-1].Neurons.Count; np++)
             *
             *            {
             *                neuron.Value = neuron.Value + this.layers[1 - 1].Neurons[np].Value * neuron.Dendrites[np].Weight;
             *                neuron.Value = sigmoid(neuron.Value + neuron.Bias);
             *            }
             *        }
             *    }
             * }
             * Layer lastLayer = layers[layers.Count - 1];
             * for (int i = 0; i < outputs.Length; i++)
             * {
             *    outputs[i] = lastLayer.Neurons[i].Value;
             * }*/
            if (PlayerOne)
            {
                for (int i = 0; i < layersOfNN[0].Count; i++)
                {
                    Layer lay = layersOfNN[0][i];

                    for (int n = 0; n < lay.NeuronCount; n++)
                    {
                        Neuron neuron = lay.Neurons[n];
                        //sets first layer values to be input
                        if (i == 0)
                        {
                            neuron.Value = inputs[n];
                        }
                        else
                        {
                            //does the calculations for layers other than 1
                            neuron.Value = 0;
                            for (int np = 0; np < layersOfNN[0][i - 1].Neurons.Count; np++)

                            {
                                neuron.Value = neuron.Value + layersOfNN[0][i - 1].Neurons[np].Value * neuron.Dendrites[np].Weight;
                                neuron.Value = sigmoid(neuron.Value + neuron.Bias);
                            }
                        }
                    }
                }
                int index = 0;
                foreach (Neuron item in layersOfNN[0][3].Neurons)
                {
                    outputs[index] = item.Value;
                    index++;
                }
            }
            else
            {
                for (int i = 0; i < layersOfNN[1].Count; i++)
                {
                    Layer lay = layersOfNN[1][i];

                    for (int n = 0; n < lay.NeuronCount; n++)
                    {
                        Neuron neuron = lay.Neurons[n];
                        //sets first layer values to be input
                        if (i == 0)
                        {
                            neuron.Value = inputs[n];
                        }
                        else
                        {
                            //does the calculations for layers other than 1
                            neuron.Value = 0;
                            for (int np = 0; np < layersOfNN[1][i - 1].Neurons.Count; np++)

                            {
                                neuron.Value = neuron.Value + layersOfNN[1][i - 1].Neurons[np].Value * neuron.Dendrites[np].Weight;
                                neuron.Value = sigmoid(neuron.Value + neuron.Bias);
                            }
                        }
                    }
                }
                int index = 0;
                foreach (Neuron item in layersOfNN[1][3].Neurons)
                {
                    outputs[index] = item.Value;
                    index++;
                }
            }

            return(outputs);
        }