protected AEvolutionEnvironment(PopulationParameters populationParameters, IEnvironmentParameters environmentParameters, AbstractPopulation <T> gradedPopulation, AbstractPopulation <T> solution, AbstractPopulation <T> parents, AbstractPopulation <T> children)
 {
     _populationParameters  = populationParameters;
     _environmentParameters = environmentParameters;
     _gradedPopulation      = gradedPopulation;
     _solution = solution;
     _parents  = parents;
     _children = children;
     _rand     = new Random(42);
 }
        public Tuple <AbstractPopulation <T>, float, AbstractPopulation <T> > EvolvePopulation(AbstractPopulation <T> population)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            var rawGradedPopulation = GradePopulation(population);

            _gradedPopulation.CreaturePopulation.Clear();
            float averageGrade = 0;

            averageGrade = ComputeAverageGrade(rawGradedPopulation);

            if (_solution.CreaturePopulation.Count > 0)
            {
                return(new Tuple <AbstractPopulation <T>, float, AbstractPopulation <T> >(population, averageGrade, _solution));
            }
            _parents.CreaturePopulation.Clear();
            //TOP Creature
            _parents.CreaturePopulation = FindTopCreatures(rawGradedPopulation);

            //Genetic diversity
            for (int i = _populationParameters.GradedIndividualRetainCount; i < _gradedPopulation.CreaturePopulation.Count; ++i)
            {
                if (_rand.Next(1, 100) < _populationParameters.ChanceRetainNonGraded)
                {
                    var creature = _gradedPopulation.CreaturePopulation.ToArray()[i];
                    _parents.CreaturePopulation.Add(creature);
                }
            }

            //Mutation
            _parents = MutatePopulation();

            //Crossover parents
            _children = CrossOverPopulation();

            _parents.CreaturePopulation.AddRange(_children.CreaturePopulation);
            return(new Tuple <AbstractPopulation <T>, float, AbstractPopulation <T> >(_parents, averageGrade, _solution));
        }
        public virtual List <Tuple <T, IFitness> > GradePopulation(AbstractPopulation <T> population)
        {
            var tupleList = population.CreaturePopulation.Select(x => new Tuple <T, IFitness>(x, x.GetIndividualFitness(_environmentParameters))).ToList();

            return(tupleList.OrderByDescending(x => x.Item2.GetGrade()).ToList());
        }
        public virtual float AveragePopulationGrade(AbstractPopulation <T> population)
        {
            float total = population.CreaturePopulation.Sum(creature => creature.GetIndividualFitness(_environmentParameters).GetGrade());

            return(total / _populationParameters.PopulationCount);
        }