예제 #1
0
        public virtual async Task <List <TIndividual> > Generate()
        {
            var bodies = _storage.Get(0, Algorithm.Parameters.Lambda);
            await Mutator.Mutate(bodies);

            bodies.ForEach(b => b.Fitness = Algorithm.Fitness.Evaluate(b));

            var result = Filter.Filter(bodies);

            _storage.Dump(0, result.Discarded);
            return(result.NextGeneration);
        }
        private void MutationPhase()
        {
            var x      = Algorithm.Population[0];
            var bodies = _storage.Get(-1, _parameters.Lambda);

            _storage.Dump(-1, bodies);

            var mutations = Math.Min(Algorithm.Parameters.GeneCount, Mutations());

            _flips = new int[mutations];
            var currentFlips = new int[mutations];
            var flipped      = new HashSet <int>();

            for (var i = 0; i < bodies.Count; i++)
            {
                flipped.Clear();
                var body = bodies[i];
                x.CopyTo(body);
                for (var m = 0; m < mutations; m++)
                {
                    int r;
                    do
                    {
                        r = _random.Next(_parameters.GeneCount);
                    }while (flipped.Contains(r));
                    flipped.Add(r);
                    body.Flip(r);
                    currentFlips[m] = r;
                }

                body.Fitness = Algorithm.Fitness.Evaluate(body);

                if (i == 0 || body.Fitness > _xMark.Fitness)
                {
                    body.CopyTo(_xMark);
                    Array.Copy(currentFlips, 0, _flips, 0, mutations);
                }
            }
        }