Beispiel #1
0
        //public NeuralModelSimulator(String filename)
        //{
        //    mlp = new MLPDll(filename);
        //}

        public void Train(IModelSimulator sourceSimulator, double treshold)
        {
            Random r = new Random();
            double mu = 0.0001;
            long   count = 0;
            double errors = 0, errors2 = double.MaxValue;

            double[] error = new double[4];
            do
            {
                for (int i2 = 0; i2 < EPOCH_COUNT; ++i2)
                {
                    double        angle    = r.NextDouble() * 2 * Math.PI;//veletlen szog
                    CarModelState carstate = new CarModelState(new PointD(ComMath.Normal(r.NextDouble(), 0, 1, CarModelState.MIN_POS_X, CarModelState.MAX_POS_X),
                                                                          ComMath.Normal(r.NextDouble(), 0, 1, CarModelState.MIN_POS_Y, CarModelState.MAX_POS_Y)),
                                                               new PointD(ComMath.Normal(Math.Cos(angle), -1, 1, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY),
                                                                          ComMath.Normal(Math.Sin(angle), -1, 1, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY)));

                    CarModelInput carinput = new CarModelInput();
                    //= new CarModelInput(ComMath.Normal(r.NextDouble(), 0, 1, CarModelInput.MIN_SPEED, CarModelInput.MAX_SPEED),
                    //                                           ComMath.Normal(r.NextDouble(), 0, 1, CarModelInput.MIN_SPEED, CarModelInput.MAX_SPEED));
                    carinput.Angle = ComMath.Normal(r.NextDouble(), 0, 1, CarModelInput.MIN_ANGLE, CarModelInput.MAX_ANGLE);


                    CarModelState state, state2;
                    double[]      output;
                    sourceSimulator.SimulateModel(carstate, carinput, out state);
                    this.SimulateModel(carstate, carinput, out state2, out output);


                    error    = new double[4];
                    error[0] = -output[0] + ComMath.Normal(state.Position.X, CarModelState.MIN_POS_X, CarModelState.MAX_POS_X, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
                    error[1] = -output[1] + ComMath.Normal(state.Position.Y, CarModelState.MIN_POS_Y, CarModelState.MAX_POS_Y, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
                    error[2] = -output[2] + ComMath.Normal(state.Orientation.X, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
                    error[3] = -output[3] + ComMath.Normal(state.Orientation.Y, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
                    count++;
                    mlp.Train(mu, error);
                    errors += error[0] * error[0] + error[1] * error[1] + error[2] * error[2] + error[3] * error[3];
                }
                errors /= EPOCH_COUNT;
                //if (errors2 < errors) mu *= 0.75;
                errors2 = errors;
                System.Console.WriteLine(errors.ToString());
            } while (errors > treshold);
            // mlp.SaveNN("neuralmodel.mlp");
        }
Beispiel #2
0
 public double Train(double[] inputValues)
 {
     if (inputValues.Length >= dimension * (inputLength + 1))
     {
         double error = 0;
         for (int i = inputLength; i < inputValues.Length / dimension; ++i)
         {
             double[] input = new double[dimension * inputLength];
             int      i3    = 0;
             for (int i2 = (i - inputLength) * dimension; i2 < i * dimension; ++i2)
             {
                 input[i3] = inputValues[i2];
                 i3++;
             }
             double[] outp = mlp.Output(input);
             double[] err  = new double[dimension];
             for (int i2 = 0; i2 < dimension; ++i2)
             {
                 err[i2] = inputValues[i * dimension + i2] - outp[i2];
                 error  += err[i2] * err[i2];
             }
             mlp.Train(mu, err);
             trainCount++;
         }
         //if (error > 1.2 * preverror)
         //{
         //    if (trainCount > 20000) mu *= 0.2;
         //}
         //else if (error < 0.8 * preverror)
         //{
         //    mu *= 1.2;
         //}
         preverror = error / inputValues.Length * dimension;
         return(preverror);
     }
     else
     {
         throw new Exception("Not enough data!");
     }
 }