public static void SimulateOneStep(MLPDll controller, IModelSimulator model, CarModelState state, out CarModelInput outInput, out CarModelState outState)
        {
            double[] inputs = new double[4];
            inputs[0] = ComMath.Normal(state.Position.X, CarModelState.MIN_POS_X, CarModelState.MAX_POS_X, POSITION_SCALE * MIN_NEURON_VALUE, POSITION_SCALE * MAX_NEURON_VALUE);
            inputs[1] = ComMath.Normal(state.Position.Y, CarModelState.MIN_POS_Y, CarModelState.MAX_POS_Y, POSITION_SCALE * MIN_NEURON_VALUE, POSITION_SCALE * MAX_NEURON_VALUE);
            inputs[2] = ComMath.Normal(state.Orientation.X, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
            inputs[3] = ComMath.Normal(state.Orientation.Y, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
            double[] controllerOutputs = controller.Output(inputs);

            if (INPUT_TYPE == inputType.wheelAngle)
            {
                outInput       = new CarModelInput();
                outInput.Angle = ComMath.Normal(controllerOutputs[0], MIN_NEURON_VALUE, MAX_NEURON_VALUE, CarModelInput.MIN_ANGLE, CarModelInput.MAX_ANGLE);
            }
            else if (INPUT_TYPE == inputType.wheelSpeed)
            {
                outInput = new CarModelInput(ComMath.Normal(controllerOutputs[0], MIN_NEURON_VALUE, MAX_NEURON_VALUE, CarModelInput.MIN_SPEED, CarModelInput.MAX_SPEED),
                                             ComMath.Normal(controllerOutputs[1], MIN_NEURON_VALUE, MAX_NEURON_VALUE, CarModelInput.MIN_SPEED, CarModelInput.MAX_SPEED));
                //********
                //hatrafele tilos mennie
                if (outInput.LeftSpeed < 0)
                {
                    outInput.LeftSpeed = 0;
                }
                if (outInput.RightSpeed < 0)
                {
                    outInput.RightSpeed = 0;
                }
                //********
            }

            model.SimulateModel(state, outInput, out outState);
        }
        //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");
            
        }
Exemple #3
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");
        }
        public static void SimulateOneStep(MLPDll controller, IModelSimulator model, CarModelState state, out CarModelInput outInput, out CarModelState outState)
        {
            double[] inputs = new double[4];            
            inputs[0] = ComMath.Normal(state.Position.X, CarModelState.MIN_POS_X, CarModelState.MAX_POS_X, POSITION_SCALE * MIN_NEURON_VALUE, POSITION_SCALE * MAX_NEURON_VALUE);
            inputs[1] = ComMath.Normal(state.Position.Y, CarModelState.MIN_POS_Y, CarModelState.MAX_POS_Y, POSITION_SCALE * MIN_NEURON_VALUE, POSITION_SCALE * MAX_NEURON_VALUE);
            inputs[2] = ComMath.Normal(state.Orientation.X, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
            inputs[3] = ComMath.Normal(state.Orientation.Y, CarModelState.MIN_OR_XY, CarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE);
            double[] controllerOutputs = controller.Output(inputs);            
                        
            if (INPUT_TYPE == inputType.wheelAngle)
            {                
                outInput = new CarModelInput();
                outInput.Angle = ComMath.Normal(controllerOutputs[0], MIN_NEURON_VALUE, MAX_NEURON_VALUE, CarModelInput.MIN_ANGLE, CarModelInput.MAX_ANGLE);
            }
            else if (INPUT_TYPE == inputType.wheelSpeed)
            {
                outInput = new CarModelInput(ComMath.Normal(controllerOutputs[0], MIN_NEURON_VALUE, MAX_NEURON_VALUE, CarModelInput.MIN_SPEED, CarModelInput.MAX_SPEED),
                                             ComMath.Normal(controllerOutputs[1], MIN_NEURON_VALUE, MAX_NEURON_VALUE, CarModelInput.MIN_SPEED, CarModelInput.MAX_SPEED));
                //********
                //hatrafele tilos mennie
                if (outInput.LeftSpeed < 0) outInput.LeftSpeed = 0;
                if (outInput.RightSpeed < 0) outInput.RightSpeed = 0;
                //********
            }

            model.SimulateModel(state, outInput, out outState);
        }
Exemple #5
0
 public void SimulateModel(CarModelInput input, IModelSimulator simulator, double timeStep)
 {
     simulator.SimulateModel(this.state, input, timeStep, out this.state);
 }
Exemple #6
0
 public void SimulateModel(CarModelInput input, IModelSimulator simulator)
 {
     simulator.SimulateModel(this.state, input, out this.state);
 }
Exemple #7
0
 public void SimulateModel(CarModelInput input, IModelSimulator simulator, double timeStep)
 {
     simulator.SimulateModel(this.state, input, timeStep, out this.state);
 }
Exemple #8
0
 public void SimulateModel(CarModelInput input, IModelSimulator simulator)
 {            
     simulator.SimulateModel(this.state, input, out this.state);
 }