Example #1
0
        /// <summary>
        /// Creates the child.
        /// </summary>
        /// <param name="leftParent">Left parent.</param>
        /// <param name="rightParent">Right parent</param>
        /// <param name="firstSwapPoint">he index of the swap point one gene.</param>
        /// <param name="secondSwapPoint">he index of the swap point two gene.</param>
        /// <returns>The child.</returns>
        protected IIndividual CreateChild(IIndividual leftParent, IIndividual rightParent, int firstSwapPoint, int secondSwapPoint)
        {
            var firstCutGenesCount  = firstSwapPoint + 1;
            var secondCutGenesCount = secondSwapPoint + 1;
            var child = leftParent.CreateNew();

            child.ReplaceGenes(0, leftParent.GetGenes().Take(firstCutGenesCount).ToArray());
            child.ReplaceGenes(firstCutGenesCount, rightParent.GetGenes().Skip(firstCutGenesCount).Take(secondCutGenesCount - firstCutGenesCount).ToArray());
            child.ReplaceGenes(secondCutGenesCount, leftParent.GetGenes().Skip(secondCutGenesCount).ToArray());

            return(child);
        }
Example #2
0
        /// <summary>
        /// Creates the child.
        /// </summary>
        /// <param name="leftParent">Left parent.</param>
        /// <param name="rightParent">Right parent</param>
        /// <param name="swapPoint">The index of the swap point.</param>
        /// <returns>The child.</returns>
        protected virtual IIndividual CreateChild(IIndividual leftParent, IIndividual rightParent, int swapPoint)
        {
            var cutGenesCount = swapPoint + 1;
            var child         = leftParent.CreateNew();

            child.ReplaceGenes(0, leftParent.GetGenes().Take(cutGenesCount).ToArray());
            child.ReplaceGenes(cutGenesCount, rightParent.GetGenes().Skip(cutGenesCount).ToArray());

            return(child);
        }
Example #3
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;

            var genes = individual.GetGenes();

            for (int i = 0; i < this.targetBitmapPixelsCount; i++)
            {
                fitness += PixelDifference(this.targetBitmapPixels[i], (Color)genes[i].Value);
            }
            return(1 / (1 + fitness));
        }
Example #4
0
        public CMAParameters(IIndividual mean, double sigma, Matrix <double> bounds, int nMaxResampling, double epsilon = 1e-8)
        {
            ValidateSigma(sigma);

            // problem dimensions
            nDim = ValidateNDim(mean.Length);

            // population size is automatic generate by problem dimesnion eq.
            PopSize = 4 + (int)Math.Floor(3 * Math.Log(nDim));

            // inicialze mu coef.
            mu = PopSize / 2;

            Vector <double> weightsPrime = Vector <double> .Build.Dense(PopSize);

            for (int i = 0; i < PopSize; i++)
            {
                weightsPrime[i] = Math.Log((PopSize + 1) / (double)2) - Math.Log(i + 1);
            }

            Vector <double> weightsPrimeMuEff = Vector <double> .Build.Dense(weightsPrime.Take(mu).ToArray());

            mu_eff = Math.Pow(weightsPrimeMuEff.Sum(), 2) / Math.Pow(weightsPrimeMuEff.L2Norm(), 2);

            Vector <double> weightsPrimeMuEffMinus = Vector <double> .Build.Dense(weightsPrime.Skip(mu).ToArray());

            double muEffMinus = Math.Pow(weightsPrimeMuEffMinus.Sum(), 2) / Math.Pow(weightsPrimeMuEffMinus.L2Norm(), 2);

            int alphacCov = 2;

            c1 = alphacCov / (Math.Pow(nDim + 1.3, 2) + mu_eff);


            cmu = Math.Min(1 - c1, alphacCov * (mu_eff - 2 + (1 / mu_eff)) / (Math.Pow(nDim + 2, 2) + (alphacCov * mu_eff / 2)));

            ValidateRateRankOneUpdate();
            ValidateRateRankMu();

            double minAlpha = Math.Min(1 + (c1 / cmu), Math.Min(1 + (2 * muEffMinus / (mu_eff + 2)), (1 - c1 - cmu) / (nDim * cmu)));

            double positiveSum = weightsPrime.Where(x => x > 0).Sum();
            double negativeSum = Math.Abs(weightsPrime.Where(x => x < 0).Sum());

            Vector <double> weights = Vector <double> .Build.Dense(weightsPrime.Count);

            weightsPrime.CopyTo(weights);
            bool[] weightsIsNotNegative = weightsPrime.Select(x => x >= 0).ToArray();
            for (int i = 0; i < weights.Count; i++)
            {
                weights[i] = weightsIsNotNegative[i] ? 1 / positiveSum * weightsPrime[i] : minAlpha / negativeSum * weightsPrime[i];
            }

            cm = 1;

            c_sigma = (mu_eff + 2) / (nDim + mu_eff + 5);
            d_sigma = 1 + (2 * Math.Max(0, Math.Sqrt((mu_eff - 1) / (nDim + 1)) - 1)) + c_sigma;

            StepSizeControl();

            cc = (4 + (mu_eff / nDim)) / (nDim + 4 + (2 * mu_eff / nDim));

            ValidationRankOneUpdateSecond();

            chi_n = Math.Sqrt(nDim) * (1.0 - (1.0 / (4.0 * nDim)) + 1.0 / (21.0 * (Math.Pow(nDim, 2))));

            _weights = weights;

            p_sigma = Vector <double> .Build.Dense(nDim, 0);

            pc = Vector <double> .Build.Dense(nDim, 0);

            _mean = Vector <double> .Build.DenseOfArray(mean.GetGenes());

            C = Matrix <double> .Build.DenseIdentity(nDim, nDim);

            this.sigma = sigma;

            validateBounds(bounds);

            this.bounds      = bounds;
            n_max_resampling = nMaxResampling;
            this.epsilon     = epsilon;
        }
    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;
    }