示例#1
0
        public Individual[] Operate(Individual[] parents)
        {
            Individual[] offspring = new Individual[parents.Length];

            for (int i = 0; i < parents.Length; i++)
            {
                IntegerIndividual p1 = (IntegerIndividual)parents[i];
                IntegerIndividual o1 = (IntegerIndividual)p1.Clone();

                if (Rnd.NextDouble() < MutationProbability)
                {
                    for (int j = 0; j < o1.Genes.Length; j++)
                    {
                        if (Rnd.NextDouble() < GeneChangeProbability)
                        {
                            o1.Genes[j] = Rnd.Next(o1.Max - o1.Min) + o1.Min;
                        }
                    }
                }

                offspring[i] = o1;
            }

            return(offspring);
        }
示例#2
0
        public Individual[] Operate(Individual[] parents)
        {
            Individual[] offspring = new Individual[parents.Length];

            for (int i = 0; i < parents.Length / 2; i++)
            {
                IntegerIndividual p1 = (IntegerIndividual)parents[2 * i];
                IntegerIndividual p2 = (IntegerIndividual)parents[2 * i + 1];

                IntegerIndividual o1 = (IntegerIndividual)p1.Clone();
                IntegerIndividual o2 = (IntegerIndividual)p2.Clone();

                if (Rnd.NextDouble() < XOverProbability)
                {
                    int point = Rnd.Next(p1.Genes.Length);

                    for (int j = point; j < p1.Genes.Length; j++)
                    {
                        int tmp = o1.Genes[j];
                        o1.Genes[j] = o2.Genes[j];
                        o2.Genes[j] = tmp;
                    }
                }

                offspring[2 * i]     = o1;
                offspring[2 * i + 1] = o2;
            }

            return(offspring);
        }
示例#3
0
        public Individual[] Operate(Individual[] parents)
        {
            Individual[] offspring = new Individual[parents.Length];

            for (int i = 0; i < parents.Length; i++)
            {
                IntegerIndividual p1 = (IntegerIndividual)parents[i];
                IntegerIndividual o1 = (IntegerIndividual)p1.Clone();

                if (Rnd.NextDouble() < MutationProbability)
                {
                    int i1 = 0, i2 = 0;
                    int size = o1.Genes.Length;

                    while (i1 == i2)
                    {
                        i1 = Rnd.Next(size);
                        i2 = Rnd.Next(size);
                    }

                    int tmp = o1.Genes[i1];
                    o1.Genes[i1] = o1.Genes[i2];
                    o1.Genes[i2] = tmp;
                }

                offspring[i] = o1;
            }

            return(offspring);
        }
        public Individual[] Operate(Individual[] parents)
        {
            Individual[] offspring = new Individual[parents.Length];

            for (int i = 0; i < parents.Length; i++)
            {
                IntegerIndividual p1 = (IntegerIndividual)parents[i];
                IntegerIndividual o1 = (IntegerIndividual)p1.Clone();

                if (Rnd.NextDouble() < MutationProbability)
                {
                    int    iH = -1, iL = -1;
                    double heavyBinSum = 0, lightBinSum = double.PositiveInfinity;

                    for (int j = 0; j < Bins; j++)
                    {
                        double sum = getTotalWeight(o1, j);

                        if (heavyBinSum < sum)
                        {
                            heavyBinSum = sum;
                            iH          = j;
                        }
                        else if (lightBinSum > sum)
                        {
                            lightBinSum = sum;
                            iL          = j;
                        }
                    }

                    while (heavyBinSum > lightBinSum)
                    {
                        for (int j = 0; j < o1.Genes.Length; j++)
                        {
                            if ((int)o1.Genes[j] == iH)
                            {
                                o1.Genes[j] = iL;

                                heavyBinSum -= Weights[j];
                                lightBinSum += Weights[j];

                                break;
                            }
                        }
                    }
                }

                offspring[i] = o1;
            }

            return(offspring);
        }