Ejemplo n.º 1
0
        /// <summary>
        /// Swap two genes position.
        /// </summary>
        /// <param name="firstIndex">First gene</param>
        /// <param name="secondIndex">Second gene</param>
        /// <param name="individual">Ind</param>
        protected void SwapGenes(int firstIndex, int secondIndex, IIndividual individual)
        {
            var firstGene  = individual.GetGene(firstIndex);
            var secondGene = individual.GetGene(secondIndex);

            individual.ReplaceGene(firstIndex, secondGene);
            individual.ReplaceGene(secondIndex, firstGene);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Evolved one generation.
        /// </summary>
        /// <param name="population">Curr. pop</param>
        /// <returns>New generation</returns>
        protected override IPopulation EvolvedOneGeneration(IPopulation population)
        {
            List <IIndividual> newGeneration = new List <IIndividual>();

            foreach (var orginal in population.Individuals)
            {
                // generate unique random numbers
                var randomValues = FastRandom.GetUniqueInts(3, 0, population.Size);
                int a            = randomValues[0];
                int b            = randomValues[1];
                int c            = randomValues[2];

                // choose random individuals (agents) from population
                IIndividual individual1 = population.Individuals[a];
                IIndividual individual2 = population.Individuals[b];
                IIndividual individual3 = population.Individuals[c];

                int i = 0;


                int R = FastRandom.GetInt(0, population.Size);

                var candidate = population.CreateEmptyIndividual();
                foreach (var orginalElement in orginal.GetGenes())
                {
                    double probXover = FastRandom.GetDouble();

                    if (probXover < XoverProbability || i == R)
                    {
                        // simple mutation
                        double newElement = individual1.GetGene(i) + F * (individual2.GetGene(i) - individual3.GetGene(i));
                        candidate.ReplaceGene(i, newElement);
                    }
                    else
                    {
                        candidate.ReplaceGene(i, orginalElement);
                    }

                    i++;
                }


                var fit = fitness.Evaluate(candidate);

                if (fit < orginal.Fitness)
                {
                    newGeneration.Add(candidate);
                }
                else
                {
                    newGeneration.Add(orginal);
                }
            }

            CurrentGenerationsNumber++;

            population.Individuals = newGeneration;

            return(population);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Mutate the specified individual.
        /// Shifted line
        /// </summary>
        /// <param name="individual">The individual.</param>
        /// <param name="mutation_probabilty">The probability to mutate each indiviudal.</param>
        public override void Mutate(IIndividual individual, float mutation_probabilty)
        {
            var indexes = RandomizationRnd.GetInts(1, 0, individual.Length);

            foreach (var index in indexes)
            {
                var oldGene = individual.GetGene(index);

                // line endPoints shift
                var shift1 = RandomizationRnd.GetInt(-shift, shift);
                var shift2 = RandomizationRnd.GetInt(-shift, shift);
                var shift3 = RandomizationRnd.GetInt(-shift, shift);
                var shift4 = RandomizationRnd.GetInt(-shift, shift);

                // gene
                LineSegment2D line = (LineSegment2D)oldGene.Value;

                // shifted gene
                var p1 = new Point(line.P1.X + shift1, line.P1.Y + shift2);
                var p2 = new Point(line.P2.X + shift3, line.P2.Y + shift4);

                // sets new gene
                individual.ReplaceGene(index, new Gene(new LineSegment2D(p1, p2)));
            }
        }
        /// <summary>
        /// Mutate one ind.
        /// </summary>
        /// <param name="ind">Individual</param>
        void Mutate(IIndividual ind)
        {
            // last gene is sigma parameter
            // var sigma = 1;
            var sigma = ind.GetGene(ind.Length - 1);


            for (int index = 0; index < ind.Length - 1; index++)
            {
                if (FastRandom.GetDouble() <= mutationProbability)
                {
                    var g = ind.GetGene(index);

                    var s = Normal.Sample(FastRandom.Instance, 0, sigma);

                    ind.ReplaceGene(index, g + s);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Performs the evaluation against the specified individual.
        /// </summary>
        /// <param name="individual">The individual to be evaluated.</param>
        /// <returns>The fitness of the individual.</returns>
        public double Evaluate(IIndividual individual)
        {
            double fitness = 0.0;

            for (var i = 0; i < individual.Length; i++)
            {
                fitness += LinesDifference((LineSegment2D)individual.GetGene(i).Value, targetBitmapLine[i]);
            }

            return(1 / (fitness + 1));
        }
Ejemplo n.º 6
0
        /// <summary>
        ///  Cross the specified parents generating the children (Uniform cross).
        /// </summary>
        /// <param name="firstParent"> First parent</param>
        /// <param name="secondParent">Second parent</param>
        /// <returns>The offspring (children) of the parents.</returns>
        private IList<IIndividual> PerfomCross(IIndividual firstParent, IIndividual secondParent)
        {

            var firstChild = firstParent.CreateNew();
            var secondChild = firstParent.CreateNew();

            // uniform cross by pmixProb
            for (int i = 0; i < firstParent.Length; i++)
            {
                if (FastRandom.GetDouble() < mixProbability)
                {
                    firstChild.ReplaceGene(i, firstParent.GetGene(i));
                    secondChild.ReplaceGene(i, secondParent.GetGene(i));
                }
                else
                {
                    firstChild.ReplaceGene(i, secondParent.GetGene(i));
                    secondChild.ReplaceGene(i, firstParent.GetGene(i));
                }
            }
            return new List<IIndividual> { firstChild, secondChild };
        }
        /// <summary>
        /// Mutate the specified individual.
        /// Shifted line
        /// </summary>
        /// <param name="individual">The individual.</param>
        /// <param name="mutation_probabilty">The probability to mutate each indiviudal.</param>
        public override void Mutate(IIndividual individual, float mutation_probabilty)
        {
            var indexes = FastRandom.GetInts(mutateRate, 0, individual.Length);

            foreach (var index in indexes)
            {
                var oldGene = individual.GetGene(index);

                // line endPoints shift
                var currShift = FastRandom.GetInt(-shift, shift);


                // sets new gene
                individual.ReplaceGene(index, oldGene + currShift);
            }
        }
Ejemplo n.º 8
0
    private IIndividual<Gene> Mutate(IIndividual<Gene> pIndividual)
    {
        Random lvRandom = new Random(DateTime.Now.Millisecond);
        int lvRandomValue = lvRandom.Next(1, 101);
        int lvNewPosition = -1;
        IIndividual<Gene> lvMutatedIndividual = null;
        List<Gene> lvNextMov = null;
        Queue<Gene> lvQueue = new Queue<Gene>();
        Gene lvRefGene = null;
        Gene lvGene = null;
        HashSet<Gene> lvHashSet = new HashSet<Gene>();
        int lvPrevGene = -1;
        int lvNextGene = -1;
        int lvEndIndex = -1;

        if (lvRandomValue <= mMutationRate)
        {
            lvRandomValue = lvRandom.Next(0, pIndividual.Count - 1);
            lvRefGene = pIndividual.GetGene(lvRandomValue);
            while (lvRefGene.HeadWayTime != DateTime.MinValue)
            {
                lvRandomValue++;
                lvRefGene = pIndividual.GetGene(lvRandomValue);

                if (lvRefGene == null)
                {
                    lvRandomValue = pIndividual.Count - 1;
                }
            }

            lvPrevGene = 0;
            for (int i = lvRandomValue - 1; i >= 0; i--)
            {
                lvGene = pIndividual.GetGene(i);

                if ((lvGene.TrainId == lvRefGene.TrainId) && (lvRefGene.HeadWayTime == DateTime.MinValue))
                {
                    lvPrevGene = i + 1;
                    break;
                }
            }

            lvNextGene = pIndividual.Count - 1;
            for (int i = lvRandomValue + 2; i < pIndividual.Count; i++)
            {
                lvGene = pIndividual.GetGene(i);

                if ((lvGene.TrainId == lvRefGene.TrainId) && (lvRefGene.HeadWayTime == DateTime.MinValue))
                {
                    lvNextGene = i - 1;
                    break;
                }
            }

            lvNewPosition = lvRandom.Next(lvPrevGene, lvNextGene + 1);

            if (lvNewPosition != lvRandomValue)
            {
                lvMutatedIndividual = new TrainIndividual(mFitness, mDateRef, null, null);
                if (lvNewPosition < lvRandomValue)
                {
                    lvEndIndex = lvNewPosition - 1;
                }
                else
                {
                    lvEndIndex = lvRandomValue - 1;
                }
                lvMutatedIndividual.AddGenes(pIndividual.GetGenes(0, lvEndIndex));

                for (int i = lvRandomValue + 1; i < lvNewPosition; i++)
                {
                    lvGene = pIndividual.GetGene(i);
                    if (lvGene.HeadWayTime == DateTime.MinValue)
                    {
                        AddFromQueue(lvMutatedIndividual, lvQueue, lvHashSet);
                        lvNextMov = lvMutatedIndividual.GetNextPosition(lvGene, DateTime.MaxValue);
                        if (lvNextMov != null)
                        {
                            lvMutatedIndividual.AddGenes(lvNextMov);
                        }
                        else
                        {
                            lvQueue.Enqueue(lvGene);
                        }
                    }
                }

                /* Insere o Gene na nova posição */
                AddFromQueue(lvMutatedIndividual, lvQueue, lvHashSet);
                lvNextMov = lvMutatedIndividual.GetNextPosition(lvRefGene);
                if (lvNextMov != null)
                {
                    lvMutatedIndividual.AddGenes(lvNextMov);
                }
                else
                {
                    lvQueue.Enqueue(lvGene);
                }

                if (lvNewPosition < lvRandomValue)
                {
                    for (int i = lvNewPosition + 1; i < pIndividual.Count; i++)
                    {
                        lvGene = pIndividual.GetGene(i);
                        if ((lvGene.HeadWayTime == DateTime.MinValue) && (lvGene != lvRefGene))
                        {
                            AddFromQueue(lvMutatedIndividual, lvQueue, lvHashSet);
                            lvNextMov = lvMutatedIndividual.GetNextPosition(lvGene);
                            if (lvNextMov != null)
                            {
                                lvMutatedIndividual.AddGenes(lvNextMov);
                            }
                            else
                            {
                                lvQueue.Enqueue(lvGene);
                            }
                        }
                    }
                }
                else
                {
                    for (int i = lvNewPosition; i < pIndividual.Count; i++)
                    {
                        lvGene = pIndividual.GetGene(i);
                        if (lvGene.HeadWayTime == DateTime.MinValue)
                        {
                            AddFromQueue(lvMutatedIndividual, lvQueue, lvHashSet);
                            lvNextMov = lvMutatedIndividual.GetNextPosition(lvGene);
                            if (lvNextMov != null)
                            {
                                lvMutatedIndividual.AddGenes(lvNextMov);
                            }
                            else
                            {
                                lvQueue.Enqueue(lvGene);
                            }
                            lvMutatedIndividual.AddGenes(lvNextMov);
                        }
                    }
                }
            }

            if (lvQueue.Count > 0)
            {
                AddFromQueue(lvMutatedIndividual, lvQueue, lvHashSet);
            }

            if (lvQueue.Count > 0)
            {
                lvMutatedIndividual = null;
            }
        }

        return lvMutatedIndividual;
    }