예제 #1
0
        private long[][] selection()
        {
            switch (SelectionType)
            {
            case SelectionType.PROPORTIONAL:
            {
                long[] f    = new long[this.IndividualCount];
                long   fSum = 0;
                for (int i = 0; i < this.IndividualCount; i++)
                {
                    f[i]  = _fitnessFunction.run(_parentPopulation[i]);
                    fSum += f[i];
                }

                double[] fDeals             = new double[IndividualCount];
                double[] fN                 = new double[IndividualCount];
                int[]    numberOfIndividual = new int[IndividualCount];

                int secondaryArraySize = 0;
                for (int i = 0; i < this.IndividualCount; i++)
                {
                    fDeals[i]             = f[i] / fSum;
                    fN[i]                 = fDeals[i] * IndividualCount;
                    numberOfIndividual[i] = (int)Math.Floor(fN[i]);
                    double randomValue = random.NextDouble();

                    if (randomValue <= fN[i] - numberOfIndividual[i])
                    {
                        numberOfIndividual[i]++;
                    }

                    secondaryArraySize += numberOfIndividual[i];
                }

                long[][] secondaryArray = new long[secondaryArraySize][];
                int      k = 0;
                for (int i = 0; i < IndividualCount; i++)
                {
                    for (int j = 0; j < numberOfIndividual[i]; j++)
                    {
                        secondaryArray[k] = _parentPopulation[i];
                        k++;
                    }
                }

                return(secondaryArray);
            }

            case SelectionType.TOURNEY:
            {
                for (int i = 0; i < this.IndividualCount; i++)
                {
                    int index1 = random.Next(IndividualCount);
                    int index2 = random.Next(IndividualCount);

                    //long ffTime = System.currentTimeMillis(); // time

                    long fr1 = this.getFitnessFunctionResult(index1);
                    long fr2 = this.getFitnessFunctionResult(index2);

                    //this.timeToFF += (System.currentTimeMillis() - ffTime); // time

                    this._childPopulation[i] = fr1 > fr2 ? (long[])this._parentPopulation[index1].Clone() : (long[])this._parentPopulation[index2].Clone();
                }
                break;
            }

            default:
                throw new NotImplementedException();
            }
        }