public void Reproduction()
        {
            Pair <int, double, double[]>[] tempPool;
            tempPool = new Pair <int, double, double[]> [popSize];

            for (int i = 0; i < popSize; i++)
            {
                int[] selectedList = DifferentialSelection(i);
                var   newSolution  = DifferentialCrossover(selectedList, i);
                FitnessCal(newSolution);
                tempPool[i]       = newSolution.Item1 > pool[i].Item1 ? newSolution : Copy.DeepCopy(pool[i]);
                tempPool[i].Item0 = i;
            }

            Array.Clear(pool, 0, pool.Length);
            pool = tempPool;
        }
Beispiel #2
0
        public double ProbabilityFinder(double[] probabilityArray, int index, double entropy)
        {
            //Stop when 1. no remain prob. return 1;
            //2. number of labels reached the max return 1;
            //3. if it can't find the valid prob,ie. in the below range and timeout
            //   return -1
            // The generated probability should make entropy - newEntropy > 0
            // Should no smaller than maximum of the sum of the rest infromation
            // After successfully generate a prob., call itself and pass in next index
            int numOfLabels = probabilityArray.Length;

            while (true)
            {
                if (index == numOfLabels - 1)
                {
                    double probLeft = 1 - probabilityArray.Sum();
                    probabilityArray[index] += probLeft;
                    return(1.0);
                }
                double sum = probabilityArray.Sum();
                if (Math.Round(sum, 4) == 1)
                {
                    return(1.0);
                }
                int    retryTimes = 2000;
                bool   success    = false;
                double rnd        = -1;
                while (retryTimes > 0)
                {
                    retryTimes -= 1;
                    rnd         = GlobalVar.rnd.NextDouble() * (1 - sum);
                    double[] tmpProbList = Copy.DeepCopy(
                        probabilityArray);
                    tmpProbList[index] += rnd;
                    double   testEntropy          = EntropyCalculation(tmpProbList);
                    double   remainProb           = 1 - sum - rnd;
                    double[] arrayOfProbabilities = Enumerable.Repeat(
                        remainProb / (numOfLabels - index - 1), numOfLabels - index - 1).ToArray();
                    var tmpProbList2 = tmpProbList.Select((x, i) =>
                    {
                        if (i > index)
                        {
                            return(x + arrayOfProbabilities[i - index - 1]);
                        }
                        return(x);
                    }).ToArray();
                    double maxEntTest = EntropyCalculation(tmpProbList2);

                    if (entropy > testEntropy && maxEntTest > entropy)
                    {
                        probabilityArray[index] += rnd;
                        success = true;
                        break;
                    }
                }
                double ret = -2;
                if (success == true)
                {
                    Console.WriteLine(index);
                    ret = ProbabilityFinder(probabilityArray, index + 1, entropy);
                    if (ret == 1.0)
                    {
                        return(1.0);
                    }
                    else
                    {
                        probabilityArray[index] -= rnd;
                    }
                }
                else
                {
                    return(-1);
                }
            }
        }