Ejemplo n.º 1
0
        public void Think(Transform transform, Obstacle obstacle, Action callback)
        {
            if (obstacle != null)
            {
                float[] inputs = new float[]
                {
                    transform.position.y,
                    obstacle.top.position.y,
                    obstacle.bottom.position.y,
                    obstacle.transform.position.x
                };

                float[] output = network.FeedForward(inputs);

                if (output[0] > output[1])
                {
                    //Spacebar pressed
                    if (callback != null)
                    {
                        callback();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Matrix.Matrix output  = new Matrix.Matrix(1, 1);
            double        sum     = 0;
            double        sumTest = 0;
            int           epochs  = 0;
            int           epoch   = 5000;


            LoadFromFile(data, target);
            LoadFromFileTest(dataTest, targetTest);
            SortX(data, target);
            this.Controls.Add(pv);
            this.Controls.Add(pv2);
            PlotModel pm = new PlotModel();

            pv.Model = pm;
            PlotModel pm2 = new PlotModel();

            pv2.Model = pm2;

            LineSeries punktySerii = new LineSeries
            {
                LineStyle    = LineStyle.None,
                MarkerType   = MarkerType.Diamond,
                MarkerSize   = 2,
                MarkerStroke = OxyColors.Red,
                Title        = "Punkty treningowe"
            };
            LineSeries punktySeriiTestu = new LineSeries
            {
                LineStyle    = LineStyle.None,
                MarkerType   = MarkerType.Circle,
                MarkerSize   = 2,
                MarkerStroke = OxyColors.Red,
                Title        = "Punkty wejściowe"
            };

            LineSeries punktySieci = new LineSeries()
            {
                LineStyle    = LineStyle.None,
                MarkerType   = MarkerType.Plus,
                MarkerSize   = 3,
                MarkerStroke = OxyColors.Blue,
                Title        = "Punkty wyjściowe"
            };

            LineSeries punktyBledu = new LineSeries()
            {
                LineStyle    = LineStyle.Solid,
                MarkerType   = MarkerType.Diamond,
                Color        = OxyColors.Red,
                MarkerSize   = 1,
                MarkerStroke = OxyColors.Red,
                Title        = "Zbior treningowy"
            };

            LineSeries punktyBleduTestowego = new LineSeries()
            {
                LineStyle    = LineStyle.Solid,
                MarkerType   = MarkerType.Diamond,
                Color        = OxyColors.Blue,
                MarkerSize   = 1,
                MarkerStroke = OxyColors.Blue,
                Title        = "Zbior testowy"
            };

            LineSeries punktyKresek = new LineSeries
            {
                Color           = OxyColors.Blue,
                StrokeThickness = 1
            };

            /*
             * for (int i = 0; i < data.Length; i++)
             * {
             *  if (data[i] >= 1)
             *      target[i] = data[i] * -2;
             * }
             */

            for (int i = 0; i < data.Length; i++)
            {
                punktySerii.Points.Add(new DataPoint(data[i], target[i]));
            }

            for (int i = 0; i < dataTest.Length; i++)
            {
                punktySeriiTestu.Points.Add(new DataPoint(dataTest[i], targetTest[i]));
            }



            Matrix.Matrix outputFF         = new Matrix.Matrix(1, 1);
            double[]      d                = new double[1];
            NeuralNetwork.NeuralNetwork nn = new NeuralNetwork.NeuralNetwork(1, 10, 1, true);

            //uczenie

            for (int i = 0; i < epoch; ++i)
            {
                sum     = 0;
                sumTest = 0;

                foreach (int j in Enumerable.Range(0, 81).OrderBy(x => rnd.Next()))
                {
                    d[0] = data[j];
                    double[] y = new double[1] {
                        target[j]
                    };
                    nn.Train(d, y);
                    // Console.Write("x: " + data[j]+ "y= ");
                    // nn.FeedForward(d).DisplayMatrix();
                    // Console.WriteLine();
                }
                Console.WriteLine("epoka: " + epochs);



                for (int j = 0; j < data.Length; j++)
                {
                    d[0]     = data[j];
                    outputFF = nn.FeedForward(d);
                    sum     += (outputFF.tab[0, 0] - target[j]) * (outputFF.tab[0, 0] - target[j]) / 2;
                }

                for (int j = 0; j < dataTest.Length; j++)
                {
                    d[0]     = dataTest[j];
                    outputFF = nn.FeedForward(d);
                    sumTest += (outputFF.tab[0, 0] - targetTest[j]) * (outputFF.tab[0, 0] - targetTest[j]) / 2;
                }

                EpochErrorTest.Add(new DataPoint(epochs, sumTest / dataTest.Length));
                EpochError.Add(new DataPoint(epochs, sum / data.Length));
                ++epochs;
            }

            /*
             * for (int i = 0; i < data.Length; ++i)
             * {
             *  d[0] = data[i];
             *  outputFF = nn.FeedForward(d);
             *  punktySieci.Points.Add(new DataPoint(d[0], outputFF.tab[0, 0]));
             *  outerSpace[i] = outputFF.tab[0, 0];
             *  //Console.WriteLine("x: " + d[0] + " y: " + outputFF.tab[0, 0]);
             *  //outputFF.DisplayMatrix();
             * }
             */

            for (int i = 0; i < dataTest.Length; ++i)
            {
                d[0]     = dataTest[i];
                outputFF = nn.FeedForward(d);
                punktySieci.Points.Add(new DataPoint(d[0], outputFF.tab[0, 0]));
                outerSpaceTest[i] = outputFF.tab[0, 0];
            }

            SortX(data, outerSpace);
            SortX(dataTest, outerSpaceTest);

            /*
             * for(int i = 0; i < data.Length; ++i)
             * {
             *  punktyKresek.Points.Add(new DataPoint(data[i], outerSpace[i]));
             * }
             *
             * for (int i = 0; i < dataTest.Length; ++i)
             * {
             *  punktyKresek.Points.Add(new DataPoint(dataTest[i], outerSpaceTest[i]));
             * }
             */
            //for error:

            for (int i = 0; i < epoch; ++i)
            {
                punktyBleduTestowego.Points.Add(EpochErrorTest[i]);
                punktyBledu.Points.Add(EpochError[i]);
            }

            pm2.Series.Add(punktyBleduTestowego);
            pm2.Series.Add(punktyBledu);
            pm2.Axes.Add(new OxyPlot.Axes.LinearAxis {
                Position = OxyPlot.Axes.AxisPosition.Bottom, MinimumPadding = 0.1, MaximumPadding = 0.1, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Title = "Epoka"
            });
            pm2.Axes.Add(new OxyPlot.Axes.LinearAxis {
                Position = OxyPlot.Axes.AxisPosition.Left, MinimumPadding = 0.1, MaximumPadding = 0.1, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Title = "Błąd"
            });

            //for points

            pm.Series.Add(punktySerii);
            pm.Series.Add(punktySieci);
            pm.Series.Add(punktySeriiTestu);
            //pm.Series.Add(punktyKresekTestu);
            //pm.Series.Add(punktyKresek);
        }