예제 #1
0
        public void Update()
        {
            if (Dots.All(d => d.IsDead || d.ReachedGoal))
            {
                // all dots are dead, let's evaluate population
                _score.CalculateFitness();

                // get best dot and make make an evolution of it
                var newDots = new Dot[_size];
                newDots[0] = _score.BestDot.Clone();

                // generate rest of the dots
                for (var i = 1; i < _size; i++)
                {
                    var parent = SelectParent();
                    newDots[i] = parent.Clone();

                    // mutate dot's brain
                    newDots[i].Brain.Mutate();
                }
                _generation++;

                newDots.CopyTo(Dots, 0);
                _score = new PopulationScore(_goal, Dots);
            }

            foreach (var dot in Dots)
            {
                if (_score.MaxSteps.HasValue && _currentStep > _score.MaxSteps)
                {
                    dot.SetIsDead();
                }
                else
                {
                    dot.Update();
                }

                if (HasCollision(dot))
                {
                    dot.SetIsDead();
                }

                if (HaveReachedGoal(dot))
                {
                    // this is the first dot
                    dot.SetReachedGoal();

                    // kill all the others
                    foreach (var d in Dots.Where(d => d != dot))
                    {
                        d.SetIsDead();
                    }

                    // jump out of the loop
                    continue;
                }
            }

            _currentStep++;
        }
예제 #2
0
        public Population(int size, StartPoint start, GoalPoint goal, List <Obstacle> obstacles)
        {
            _size      = size;
            _start     = start;
            _goal      = goal;
            _obstacles = obstacles;

            Dots = new Dot[_size];
            for (var i = 0; i < size; i++)
            {
                Dots[i] = new Dot(_start);
            }

            _score = new PopulationScore(goal, Dots);
        }