Esempio n. 1
0
        public Population Select(int n)
        {
            _controllers.Sort();
            Controller worst = _controllers.First();

            List <Controller> newList = new List <Controller>(_controllers.Count);

            for (int i = 0; i < _controllers.Count; i++)
            {
                List <Controller> challengeList = new List <Controller>(n);
                for (int j = 0; j < n; j++)
                {
                    challengeList.Add(_controllers[random.Next(_controllers.Count)]);
                }
                challengeList.Sort();
                Controller    best       = challengeList.Last();
                NeuralNetwork newNetwork = NNFactory.CreateElmanNN(best.Simulation.SensorStates.Count, 2);
                newNetwork.SetAllWeights(best.NeuralNetwork.GetAllWeights());
                if (gaMode == GA_CONST_START)
                {
                    newList.Add(new Controller(newNetwork));
                }
                else if (gaMode == GA_HYBRID)
                {
                    newList.Add(new Controller(newNetwork, worst.Simulation));
                }
            }

            return(new Population(newList, iteration + 1)
            {
                gaMode = gaMode
            });
        }
Esempio n. 2
0
        public Population(int popSize, int _gaMode)
        {
            _controllers = new List <Controller>(popSize);
            gaMode       = _gaMode;
            //_simulation = Simulation.CloneDefault();
            int inputCount = Simulation.CloneDefault().SensorStates.Count;

            for (int i = 0; i < popSize; i++)
            {
                NeuralNetwork network = NNFactory.CreateElmanNN(inputCount, 2);
                network.RandomizeWeights(-0.5, 0.5);
                _controllers.Add(new Controller(network));
            }
        }