Exemplo n.º 1
0
        public void BatchBackPropogate(DataSet[] dataSet, int iterations, double leanrnignRate, double momentum = 0, BackgroundWorker worker = null)
        {
            int y =0;
            for (int i = 0; i < iterations; i++)
            {
                foreach (DataSet data in dataSet)
                {
                    BackPropogate(data, leanrnignRate, momentum);
                    y++;

                    if (worker != null)
                    {
                        worker.ReportProgress((int)(((double)y / (double)(iterations * dataSet.Count())) *100));
                    }
                }
            }
        }
Exemplo n.º 2
0
        public List<List<List<double>>> BackPropogate(DataSet datain, double learninnRate, double momentum = 0)
        {
            if (PrevDW == null)
            {
                PrevDW = new List<List<List<double>>>();
                deltaArr = new List<List<double>>();
                for (int i = 0; i < NumLayers; i++)
                {
                    deltaArr.Add(new List<double>());
                    PrevDW.Add(new List<List<double>>());

                    for (int n = 0; n < Layers[i].NumNeurons; n++)
                    {
                        deltaArr[i].Add(0);
                        PrevDW[i].Add(new List<double>());

                        IEnumerable<Connection> outConn = Layers[i].Neurons[n].Connections.Where(r => r.toNeuron == Layers[i].Neurons[n]);

                        int d = 0;
                        foreach (Connection c in outConn)
                        {
                            PrevDW[i][n].Add(0);
                            d++;
                        }
                    }
                }
            }

            ApplyInput(datain.Inputs);
            CalculateOutput();

            Layer currentLayer = Layers[OutputIndex];
            while (currentLayer != Layers[InputIndex])
            {
                Parallel.ForEach(currentLayer.Neurons, new Action<Neuron>((n) =>
                {
                    double error = 0;

                    if (currentLayer == Layers[OutputIndex])
                    {
                        error = datain.Outputs[n.Index] - n.Value;
                    }

                    else
                    {
                        foreach (Connection c in n.Connections.Where(r => r.fromNeuron == n))
                        {
                            error += c.Weight * deltaArr[c.toNeuron.SelfLayer.Index][c.toNeuron.Index];
                        }

                    }

                    error = error * n.Value * (1 - n.Value);

                    deltaArr[currentLayer.Index][n.Index] = error;

                }));

                currentLayer = Layers[currentLayer.Index - 1];
            }

            currentLayer = Layers[OutputIndex];
            while (currentLayer != Layers[InputIndex])
            {

                for (int i = 0; i < currentLayer.NumNeurons; i++)
                {

                    Neuron n = currentLayer.Neurons[i];

                    foreach (Connection c in n.Connections.Where(r => r.toNeuron == n))
                    {
                        double dw = (deltaArr[c.toNeuron.SelfLayer.Index][c.toNeuron.Index] * learninnRate * c.fromNeuron.Value) + (momentum * PrevDW[currentLayer.Index][i][n.Connections.IndexOf(c)]);
                        c.Weight += dw;
                        PrevDW[currentLayer.Index][i][n.Connections.IndexOf(c)] = dw;

                    }

                    n.Bias += deltaArr[currentLayer.Index][i] * learninnRate;
                }

                currentLayer = Layers[currentLayer.Index - 1];
            }

            return PrevDW;
        }
    public void think()
    {
        ClosestFriend = get_friends_informaion();

        ClosestFoe = get_foe_informaion();

        Enviroment = get_evironment_information();

        ClosestFriend2 = sort_distance(ClosestFriend);

        #if DebugA
        GD.Print("Network input Objects");
        GD.Print(ClosestFriend);
        GD.Print(ClosestFoe);
        GD.Print(Enviroment);
        GD.Print(ClosestFriend2);
        GD.Print(ClosestFoe[0].Position);
        GD.Print(ClosestFoe[0].Position.x);
        GD.Print(ClosestFoe[0].Position.y);
        #endif

        #if DebugB
        GD.Print("+++ClosestFrient+++");
        GD.Print(ClosestFriend);
        GD.Print("---ClosestFrient2---");
        GD.Print(ClosestFriend2);
        #endif


        data_in[0] = ClosestFoe[0].Position.x;
        data_in[1] = ClosestFoe[0].Position.y;
        data_in[2] = ClosestFoe[1].Position.x;
        data_in[3] = ClosestFoe[1].Position.y;
        data_in[4] = ClosestFoe[2].Position.x;
        data_in[5] = ClosestFoe[2].Position.y;
        data_in[6] = ClosestFriend[0].Position.x;
        data_in[7] = ClosestFriend[0].Position.y;
        data_in[8] = ClosestFriend[1].Position.x;
        data_in[9] = ClosestFriend[1].Position.y;
        //data_in[10] = ClosestFriend[2].Position.x;
        //data_in[11] = ClosestFriend[2].Position.y;
        data_in[12] = Enviroment[0].Position.x;
        data_in[13] = Enviroment[0].Position.y;
        data_in[14] = Enviroment[1].Position.x;
        data_in[15] = Enviroment[1].Position.y;
        data_in[16] = Enviroment[2].Position.x;
        data_in[17] = Enviroment[2].Position.y;

        FirstData.Inputs[0] = ClosestFoe[0].Position.x;

        #if (DebugA)
        MyString = "";
        foreach (double Anumber in data_in)
        {
            MyString += Anumber.ToString() + ",";
        }
        GD.Print("Objects Position Data");
        GD.Print(MyString);
        #endif

        normalize(data_in);
        MoveShootNet.ApplyInput(data_in);
        MoveShootNet.CalculateOutput();
        data_out2 = MoveShootNet.ReadOutput();
        data_out2 = denormalize(data_out2);



        data_out = new double[] { data_in[0], data_in[1], data_in[0], data_in[1] };
        //normalize(data_out);

        Training = new NeuralNetworks.DataSet {
            Outputs = data_out, Inputs = data_in
        };
        //Training = new NeuralNetworks.DataSet() {(double[]) data_out2, Inputs = data_in };
        TrainingData.Add(Training);

        #if DebugA
        MyString = "";
        foreach (double Anumber in data_out2)
        {
            MyString += Anumber.ToString() + ",";
        }
        GD.Print("Output of Network");
        GD.Print(MyString);


        //data_out = (double[]) data_out2;
        GD.Print("Has Current Move ", Parent.Get("CurrentMove"));
        GD.Print("Has Side ", Parent.Get("Side"));
        #endif

        Acount = 0;
        foreach (double Anumber in data_out2)
        {
            data_out3[Acount] = (float)Anumber;
            Acount++;
        }



        Parent.Set("CurrentMove", Parent.ToLocal(new Godot.Vector2(data_out3[2], data_out3[3])));
        Parent.Set("CurrentTarget", new Godot.Vector2(data_out3[0], data_out3[1]));
    }