Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="population"></param>
        /// <param name="size">normally a percentage of population size (less than 40%)</param>
        /// <returns></returns>
        public int Tournament(List <Chromosome> population, int size)
        {
            var selected   = 0;
            var bestFiness = 0.0;
            var positions  = new List <int>();

            for (var i = 0; i < PopulationSize; i++)
            {
                positions.Add(i);
            }

            for (var i = 0; i < size; i++)
            {
                var pos = MyAleatory.Next(positions.Count);
                var ran = positions[pos];
                positions.RemoveAt(pos);

                if (population[ran].Fitness > bestFiness)
                {
                    selected   = ran;
                    bestFiness = population[ran].Fitness;
                }
            }

            return(selected);
        }
Beispiel #2
0
        public List <Cat> Mutate(List <Cat> clones, Cat cat)
        {
            var size           = MyOSSP.NumOperations;
            var mutationLenght = (int)Math.Round(CDC * size);

            for (var i = 0; i < (SPC ? SMP - 1 : SMP); i++)
            {
                SRD = MyAleatory.Next(size) + 1;
                var sum = mutationLenght + SRD;
                if (sum == size)
                {
                    var index = SRD - 1;
                    for (var j = mutationLenght; j > SRD; j--)
                    {
                        var aux = cat.Position[j];
                        cat.Position[j]     = cat.Position[index];
                        cat.Position[index] = aux;
                        index++;
                    }
                }
                else
                {
                    var first = cat.Position[SRD - 1];

                    if (sum < size)
                    {
                        cat.Position[SRD - 1] = cat.Position[sum];
                        cat.Position[sum]     = first;
                    }
                    else
                    {
                        cat.Position[SRD - 1]          = cat.Position[sum - (size + 1)];
                        cat.Position[sum - (size + 1)] = first;
                    }
                }

                cat.Evaluate();
                clones.RemoveAt(i);
                clones.Insert(i, new Cat(cat));
            }
            return(clones);
        }
Beispiel #3
0
        public void DistributeCats()
        {
            List <int> aleatories = new List <int>();
            int        aleatorio;

            for (int i = 0; i < SwarmSize; i++)
            {
                Swarm[i].TMFlag = false;
            }

            for (var i = 0; i < NumberTM; i++)
            {
                do
                {
                    aleatorio = MyAleatory.Next(SwarmSize);
                }while (aleatories.Contains(aleatorio));
                aleatories.Add(aleatorio);
                Swarm[aleatorio].TMFlag = true;
            }
        }
Beispiel #4
0
        public int SelectPosition(List <double> probabilities, List <Cat> clones, bool equal)
        {
            if (equal)
            {
                return(MyAleatory.Next(SMP));
            }

            var chosen   = -1;
            var aleatory = MyAleatory.NextDouble() *
                           (probabilities.Max() - probabilities.Min())
                           + probabilities.Min();

            for (int i = 0; i < clones.Count(); i++)
            {
                if (probabilities[i] >= aleatory)
                {
                    chosen = i;
                    break;
                }
            }
            return(chosen);
        }