public GridNeuralController(IGridModelSimulator model, IObstaclePositionProvider obstacle, ICarPositionProvider start, IFinishPositionProvider finish)
 {
     this.model = model;
     
     
     //controller = new MLPDll(new int[] { 40, 1 }, 5, true);//4 bemenet a state, 1 kimenet az input                        
     controllerOriginal = new MLPDll(new int[] { 40, 1 }, 5, false);//4 bemenet a state, 1 kimenet az input                        
     
     obstacleProvider = obstacle;
     carStateProvider = start;
     finishStateProvider = finish;
     trainingStopped = true;
     mu = 0.005;
 }
Beispiel #2
0
        public GridNeuralController(IGridModelSimulator model, IObstaclePositionProvider obstacle, ICarPositionProvider start, IFinishPositionProvider finish)
        {
            this.model = model;


            //controller = new MLPDll(new int[] { 40, 1 }, 5, true);//4 bemenet a state, 1 kimenet az input
            controllerOriginal = new MLPDll(new int[] { 40, 1 }, 5, false);//4 bemenet a state, 1 kimenet az input

            obstacleProvider    = obstacle;
            carStateProvider    = start;
            finishStateProvider = finish;
            trainingStopped     = true;
            mu = 0.005;
        }
 public NeuralController(IModelSimulator model, IObstaclePositionProvider obstacle, ICarPositionProvider start, IFinishPositionProvider finish)
 {
     this.model = model;
     if (INPUT_TYPE == inputType.wheelAngle)
     {
         controller = new MLPDll(new int[] { 20, 1 }, 4);//4 bemenet a state, 1 kimenet az input                        
     }
     else if (INPUT_TYPE == inputType.wheelSpeed)
     {
         controller = new MLPDll(new int[] { 75, 2 }, 4);//4 bemenet a state, 2 kimenet az input                        
     }
     obstacleProvider = obstacle;
     carStateProvider = start;
     finishStateProvider = finish;
     trainingStopped = true;
     mu = 0.005;
 }
 public void SetSimulation(bool b)
 {
     if (b)
     {
         carPositionProvider = simManager;
         finishPositionProvider = simManager;
         obstacleProvider = simManager;
         simulation = true;
     }
     else
     {
         carPositionProvider = cameraCarPos;
         finishPositionProvider = cameraCarPos;
         obstacleProvider = cameraCarPos;
         simulation = false;
     }
 }
 public NeuralController(IModelSimulator model, IObstaclePositionProvider obstacle, ICarPositionProvider start, IFinishPositionProvider finish)
 {
     this.model = model;
     if (INPUT_TYPE == inputType.wheelAngle)
     {
         controller = new MLPDll(new int[] { 20, 1 }, 4);//4 bemenet a state, 1 kimenet az input
     }
     else if (INPUT_TYPE == inputType.wheelSpeed)
     {
         controller = new MLPDll(new int[] { 75, 2 }, 4);//4 bemenet a state, 2 kimenet az input
     }
     obstacleProvider    = obstacle;
     carStateProvider    = start;
     finishStateProvider = finish;
     trainingStopped     = true;
     mu = 0.005;
 }
Beispiel #6
0
 public void SetSimulation(bool b)
 {
     if (b)
     {
         carPositionProvider    = simManager;
         finishPositionProvider = simManager;
         obstacleProvider       = simManager;
         simulation             = true;
     }
     else
     {
         carPositionProvider    = cameraCarPos;
         finishPositionProvider = cameraCarPos;
         obstacleProvider       = cameraCarPos;
         simulation             = false;
     }
 }
        private static double TrainOneEpoch(MLPDll controller, IGridModelSimulator model, ICarPositionProvider cps, IFinishPositionProvider fps, IObstaclePositionProvider ops, double mu, int maxSimCount, out double SumSimCount, out List<GridCarModelState> innerStates)
        {    
            double sumSimCount = 0;
            double error = 0;
            innerStates = new List<GridCarModelState>();
            List<double> deltaws = new List<double>();

            MLPDll[] controllers = new MLPDll[maxSimCount];
            IGridModelSimulator[] models = new IGridModelSimulator[maxSimCount];

            GridCarModelState state = GridCarModelState.FromCarModelState(cps.GetCarState());
            GridCarModelInput input = new GridCarModelInput();


            //kimenet kiszamitasa                    
            int simCount = 0;
            List<double[]> singleErrors = new List<double[]>();
            List<double[]> regularizationErrors = new List<double[]>();
            GridCarModelState laststate;
            bool earlyStop;
            do
            {
                if (simCount == 0) controllers[simCount] = new MLPDll(controller);//lemasoljuk
                else controllers[simCount] = new MLPDll(controllers[simCount - 1]);
                models[simCount] = model.Clone();//a modellt is

                laststate = state;
                GridNeuralController.SimulateOneStep(controllers[simCount], models[simCount], state, out input, out state);//vegigszimulaljuk a simCount darab controlleren es modellen
                innerStates.Add(state);

                //kozbulso hibak kiszamitasa, itt csak az akadalyoktol valo tavolsag "hibajat" vesszuk figyelembe, irany nem szamit -> hibaja 0                    
                regularizationErrors.Add(obstacleFieldErrorGradient(ops, state, simCount));

                //minden pont celtol vett tavolsaga                
                double disterror = ComMath.Normal(state.TargetDist, GridCarModelState.MIN_DIST, GridCarModelState.MAX_DIST, 0, 1);
                double orientationerror = disterror;
                if (orientationerror < 0.2) orientationerror = 0;
                double finishorientationerror = disterror;
                if (finishorientationerror > 0.05) finishorientationerror = 0;
                else finishorientationerror = 1;
                double finishX = Math.Cos(Math.PI - fps.GetFinishState(simCount).Angle);
                double finishY = Math.Sin(Math.PI - fps.GetFinishState(simCount).Angle);

                singleErrors.Add(new double[] { -disterror * MAX_NEURON_VALUE ,                                                                                                 
                                                orientationerror*ComMath.Normal(1 - state.TargetOrientation.X,GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE), 
                                                orientationerror*ComMath.Normal(0 - state.TargetOrientation.Y,GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE),
                                                finishorientationerror*ComMath.Normal(finishX - state.TargetFinishOrientation.X,GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE), 
                                                finishorientationerror*ComMath.Normal(finishY - state.TargetFinishOrientation.Y,GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE) }
                                );
                ++simCount;

                earlyStop = false;
                if (simCount > 3)
                {
                    double[] err1 = singleErrors[simCount - 1];
                    double[] err2 = singleErrors[simCount - 2];
                    double[] err3 = singleErrors[simCount - 3];
                    double error1, error2, error3;
                    error1 = error2 = error3 = 0;
                    for (int i = 0; i < 1; i++)//err1.Length
                    {
                        error1 += err1[i] * err1[i];
                        error2 += err2[i] * err2[i];
                        error3 += err3[i] * err3[i];
                    }
                    earlyStop = ((error1 > error2) && (error3 > error2));

                    if (earlyStop)
                    {
                        //utolso elemet toroljuk
                        singleErrors.RemoveAt(singleErrors.Count - 1);
                        regularizationErrors.RemoveAt(regularizationErrors.Count - 1);
                        innerStates.RemoveAt(innerStates.Count - 1);
                        --simCount;
                    }
                }



            }
            while ((simCount < maxSimCount) && !earlyStop);



            double[] errors = singleErrors[singleErrors.Count - 1];

            sumSimCount += simCount;

            //hibavisszaterjesztes
            for (int i = simCount - 1; i >= 0; --i)
            {
                double[] sensitibility;
                models[i].CalcErrorSensibility(errors, out sensitibility);

                double[] inputSensitibility;

                
                inputSensitibility = new double[1];
                inputSensitibility[0] = sensitibility[5];
                

                double[] sensitibility2;

                controllers[i].SetOutputError(inputSensitibility);
                controllers[i].Backpropagate();
                controllers[i].CalculateDeltaWeights();
                sensitibility2 = controllers[i].SensitibilityD();



                errors[0] = (sensitibility[0] + sensitibility2[0] + 0.1 * singleErrors[i][0]);
                errors[1] = (sensitibility[1] + sensitibility2[1] + 0 * singleErrors[i][1]);
                errors[2] = (sensitibility[2] + sensitibility2[2] + 0 * singleErrors[i][2]);
                errors[3] = (sensitibility[3] + sensitibility2[3] + singleErrors[i][3]);
                errors[4] = (sensitibility[4] + sensitibility2[4] + singleErrors[i][4]);

                //regularizaciobol szarmazo hiba hozzaadasa                    
                errors[0] += regularizationErrors[i][0];
                errors[1] += regularizationErrors[i][1];
                errors[2] += regularizationErrors[i][2];



            }

            controller.ClearDeltaWeights();
            //sulymodositasok osszegzese     
            for (int i2 = 0; i2 < simCount; ++i2)
            {
                controller.AddDeltaWeights(controllers[i2]);
            }
            float maxdw = controller.MaxDeltaWeight();
            //if (maxdw < 50) maxdw = 50;

            controller.ChangeWeights(mu / maxdw);

            ////sulymodositasok osszegzese                    
            //for (int i2 = 0; i2 < simCount; ++i2) //simCount
            //{

            //    int count = 0;
            //    for (int i = 1; i < controllers[i2]; ++i)
            //    {
            //        foreach (INeuron n in controllers[i2].mlp[i])
            //        {
            //            foreach (NeuronInput ni in ((Neuron)n).inputs)
            //            {
            //                if (deltaws.Count <= count) deltaws.Add(ni.deltaw);
            //                else deltaws[count] += ni.deltaw;
            //                ++count;
            //            }
            //        }
            //    }
            //}

            ////legnagyobb sulymodositas ertekenek meghatarozasa, majd ezzel normalas
            //double maxdw = 1;

            //foreach (double dw in deltaws)
            //{
            //    if (Math.Abs(dw) > maxdw) maxdw = Math.Abs(dw);
            //}

            //if (maxdw < 50) maxdw = 50;

            ////sulymodositasok ervenyre juttatasa a controllerben
            //int count2 = 0;

            //for (int i = 1; i < controller.mlp.Count; ++i)
            //{
            //    foreach (INeuron n in controller.mlp[i])
            //    {
            //        foreach (NeuronInput ni in ((Neuron)n).inputs)
            //        {
            //            ni.w += mu * deltaws[count2] / maxdw;

            //            ++count2;
            //        }
            //    }
            //}


            SumSimCount = sumSimCount;
            return error;
        }
Beispiel #8
0
        private static double TrainOneEpoch(MLPDll controller, IGridModelSimulator model, ICarPositionProvider cps, IFinishPositionProvider fps, IObstaclePositionProvider ops, double mu, int maxSimCount, out double SumSimCount, out List <GridCarModelState> innerStates)
        {
            double sumSimCount = 0;
            double error       = 0;

            innerStates = new List <GridCarModelState>();
            List <double> deltaws = new List <double>();

            MLPDll[] controllers         = new MLPDll[maxSimCount];
            IGridModelSimulator[] models = new IGridModelSimulator[maxSimCount];

            GridCarModelState state = GridCarModelState.FromCarModelState(cps.GetCarState());
            GridCarModelInput input = new GridCarModelInput();


            //kimenet kiszamitasa
            int               simCount             = 0;
            List <double[]>   singleErrors         = new List <double[]>();
            List <double[]>   regularizationErrors = new List <double[]>();
            GridCarModelState laststate;
            bool              earlyStop;

            do
            {
                if (simCount == 0)
                {
                    controllers[simCount] = new MLPDll(controller);               //lemasoljuk
                }
                else
                {
                    controllers[simCount] = new MLPDll(controllers[simCount - 1]);
                }
                models[simCount] = model.Clone();//a modellt is

                laststate = state;
                GridNeuralController.SimulateOneStep(controllers[simCount], models[simCount], state, out input, out state);//vegigszimulaljuk a simCount darab controlleren es modellen
                innerStates.Add(state);

                //kozbulso hibak kiszamitasa, itt csak az akadalyoktol valo tavolsag "hibajat" vesszuk figyelembe, irany nem szamit -> hibaja 0
                regularizationErrors.Add(obstacleFieldErrorGradient(ops, state, simCount));

                //minden pont celtol vett tavolsaga
                double disterror        = ComMath.Normal(state.TargetDist, GridCarModelState.MIN_DIST, GridCarModelState.MAX_DIST, 0, 1);
                double orientationerror = disterror;
                if (orientationerror < 0.2)
                {
                    orientationerror = 0;
                }
                double finishorientationerror = disterror;
                if (finishorientationerror > 0.05)
                {
                    finishorientationerror = 0;
                }
                else
                {
                    finishorientationerror = 1;
                }
                double finishX = Math.Cos(Math.PI - fps.GetFinishState(simCount).Angle);
                double finishY = Math.Sin(Math.PI - fps.GetFinishState(simCount).Angle);

                singleErrors.Add(new double[] { -disterror * MAX_NEURON_VALUE,
                                                orientationerror * ComMath.Normal(1 - state.TargetOrientation.X, GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE),
                                                orientationerror * ComMath.Normal(0 - state.TargetOrientation.Y, GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE),
                                                finishorientationerror * ComMath.Normal(finishX - state.TargetFinishOrientation.X, GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE),
                                                finishorientationerror * ComMath.Normal(finishY - state.TargetFinishOrientation.Y, GridCarModelState.MIN_OR_XY, GridCarModelState.MAX_OR_XY, MIN_NEURON_VALUE, MAX_NEURON_VALUE) }
                                 );
                ++simCount;

                earlyStop = false;
                if (simCount > 3)
                {
                    double[] err1 = singleErrors[simCount - 1];
                    double[] err2 = singleErrors[simCount - 2];
                    double[] err3 = singleErrors[simCount - 3];
                    double   error1, error2, error3;
                    error1 = error2 = error3 = 0;
                    for (int i = 0; i < 1; i++)//err1.Length
                    {
                        error1 += err1[i] * err1[i];
                        error2 += err2[i] * err2[i];
                        error3 += err3[i] * err3[i];
                    }
                    earlyStop = ((error1 > error2) && (error3 > error2));

                    if (earlyStop)
                    {
                        //utolso elemet toroljuk
                        singleErrors.RemoveAt(singleErrors.Count - 1);
                        regularizationErrors.RemoveAt(regularizationErrors.Count - 1);
                        innerStates.RemoveAt(innerStates.Count - 1);
                        --simCount;
                    }
                }
            }while ((simCount < maxSimCount) && !earlyStop);



            double[] errors = singleErrors[singleErrors.Count - 1];

            sumSimCount += simCount;

            //hibavisszaterjesztes
            for (int i = simCount - 1; i >= 0; --i)
            {
                double[] sensitibility;
                models[i].CalcErrorSensibility(errors, out sensitibility);

                double[] inputSensitibility;


                inputSensitibility    = new double[1];
                inputSensitibility[0] = sensitibility[5];


                double[] sensitibility2;

                controllers[i].SetOutputError(inputSensitibility);
                controllers[i].Backpropagate();
                controllers[i].CalculateDeltaWeights();
                sensitibility2 = controllers[i].SensitibilityD();



                errors[0] = (sensitibility[0] + sensitibility2[0] + 0.1 * singleErrors[i][0]);
                errors[1] = (sensitibility[1] + sensitibility2[1] + 0 * singleErrors[i][1]);
                errors[2] = (sensitibility[2] + sensitibility2[2] + 0 * singleErrors[i][2]);
                errors[3] = (sensitibility[3] + sensitibility2[3] + singleErrors[i][3]);
                errors[4] = (sensitibility[4] + sensitibility2[4] + singleErrors[i][4]);

                //regularizaciobol szarmazo hiba hozzaadasa
                errors[0] += regularizationErrors[i][0];
                errors[1] += regularizationErrors[i][1];
                errors[2] += regularizationErrors[i][2];
            }

            controller.ClearDeltaWeights();
            //sulymodositasok osszegzese
            for (int i2 = 0; i2 < simCount; ++i2)
            {
                controller.AddDeltaWeights(controllers[i2]);
            }
            float maxdw = controller.MaxDeltaWeight();

            //if (maxdw < 50) maxdw = 50;

            controller.ChangeWeights(mu / maxdw);

            ////sulymodositasok osszegzese
            //for (int i2 = 0; i2 < simCount; ++i2) //simCount
            //{

            //    int count = 0;
            //    for (int i = 1; i < controllers[i2]; ++i)
            //    {
            //        foreach (INeuron n in controllers[i2].mlp[i])
            //        {
            //            foreach (NeuronInput ni in ((Neuron)n).inputs)
            //            {
            //                if (deltaws.Count <= count) deltaws.Add(ni.deltaw);
            //                else deltaws[count] += ni.deltaw;
            //                ++count;
            //            }
            //        }
            //    }
            //}

            ////legnagyobb sulymodositas ertekenek meghatarozasa, majd ezzel normalas
            //double maxdw = 1;

            //foreach (double dw in deltaws)
            //{
            //    if (Math.Abs(dw) > maxdw) maxdw = Math.Abs(dw);
            //}

            //if (maxdw < 50) maxdw = 50;

            ////sulymodositasok ervenyre juttatasa a controllerben
            //int count2 = 0;

            //for (int i = 1; i < controller.mlp.Count; ++i)
            //{
            //    foreach (INeuron n in controller.mlp[i])
            //    {
            //        foreach (NeuronInput ni in ((Neuron)n).inputs)
            //        {
            //            ni.w += mu * deltaws[count2] / maxdw;

            //            ++count2;
            //        }
            //    }
            //}


            SumSimCount = sumSimCount;
            return(error);
        }