Pair <int, double, double[]> DifferentialCrossover(int[] selectedList, int sIndex)
        {
            double F  = 1.5; //[0,2]
            var    s1 = pool[selectedList[0]].Item2;
            var    s2 = pool[selectedList[1]].Item2;
            var    s3 = pool[selectedList[2]].Item2;

            var solutionCopy = Copy.DeepCopy(pool[sIndex]);
            int R            = GlobalVar.rnd.Next(1, numOfLabel);

            for (int i = 0; i < numOfLabel - 1; i++)
            {
                double r = GlobalVar.rnd.NextDouble();
                if (r <= 0.9 || i == R)
                {
                    solutionCopy.Item2[i] = s1[i] + F * (s2[i] - s3[i]);
                }
            }

            solutionCopy.Item2[numOfLabel - 1] =
                1 - (solutionCopy.Item2.Sum()
                     - solutionCopy.Item2[numOfLabel - 1]);

            return(solutionCopy);
        }
Example #2
0
        void Reproduction()
        {
            Pair <int, double, Pair <int, int, double[]>[]>[] tempPool = new Pair <int, double, Pair <int, int, double[]>[]> [populationSize];
            var bestSolution = pool.Where(x => x.Item1 == pool.Max(y => y.Item1)).ToList().First();

            bestSolution.Item0 = 0;
            tempPool[0]        = Copy.DeepCopy(bestSolution);

            for (int i = 1; i < populationSize; i++)
            {
                int[] selectedList = FitnessPropotionateSampling();
                var   child        = Copy.DeepCopy(pool[selectedList[0]]);
                if (GlobalVar.rnd.NextDouble() <= crRate)
                {
                    child.Item2 = TwoPointCrossOver(pool[selectedList[0]].Item2, pool[selectedList[1]].Item2);
                }
                if (GlobalVar.rnd.NextDouble() <= mtRate)
                {
                    Mutation(child.Item2);
                }
                SolutionRepair(child.Item2);
                child.Item0 = i;
                tempPool[i] = child;
            }
            Array.Clear(pool, 0, pool.Length);
            pool = tempPool;
        }
Example #3
0
        public double SelfAdaptivePenaltyFunction(double[] probabilities, double cost)
        {
            int beta           = 1;
            int searchBoundary = 200;

            double[] tempProbs = Copy.DeepCopy(probabilities);
            tempProbs = tempProbs.Select(x =>
            {
                double y = x * -1 - 0;
                return(Math.Pow((Math.Max(0, y)), beta));
            }).ToArray();
            double totalInfeasiability = tempProbs.Sum() > searchBoundary * numOfLabels ? 100 : tempProbs.Sum() / searchBoundary * numOfLabels;
            double normalizedCost      = cost / numOfLabels;
            bool   infeasiablePool     = coverPointSetProb.All(x => x.All(y => y < 0) == true) == true ? true : false;
            double distance            = infeasiablePool == true ? totalInfeasiability
                : Math.Sqrt(Math.Pow(normalizedCost, 2) + Math.Pow(totalInfeasiability, 2));
            double X = infeasiablePool == true ? 0 : totalInfeasiability;
            double Y = probabilities.All(x => x > 0) ? 0 : normalizedCost;
            double proportionOfFeaisableSol = coverPointSetProb
                                              .Count(x => x.All(y => y > 0)) * 1.0 / populationSize;
            double adjustedFitness = distance + (1 - proportionOfFeaisableSol) * X
                                     + proportionOfFeaisableSol * Y;

            return(adjustedFitness);
        }
Example #4
0
        public double GoodnessOfFit(Pair <int, double, Pair <int, int, double[]>[]> solution)
        {
            Matrix <double> Amatrix = Matrix <double> .Build.Dense(numOfLabels, numOfLabels - 1);

            double[]        lastCoverElementSet = new double[numOfLabels];
            Vector <double> bVector             = Vector <double> .Build.Dense(expTriProb);

            for (int i = 0; i < numOfLabels; i++)
            {
                if (solution.Item2.Where(x => x.Item1 == numOfLabels).Count() == 0)
                {
                    Console.WriteLine();
                }
                lastCoverElementSet[i] = solution.Item2.Where(x => x.Item1 == numOfLabels).Sum(x => x.Item2[i])
                                         / solution.Item2.Where(x => x.Item1 == numOfLabels).Count();
            }
            bVector = bVector.Subtract(Vector <double> .Build.Dense(lastCoverElementSet));

            for (int i = 0; i < numOfLabels; i++)
            {
                for (int j = 0; j < numOfLabels - 1; j++)
                {
                    if (solution.Item2.Where(x => x.Item1 == j + 1).Count() == 0)
                    {
                        Console.WriteLine();
                    }
                    Amatrix[i, j]  = solution.Item2.Where(x => x.Item1 == j + 1).Sum(x => x.Item2[i]);
                    Amatrix[i, j] /= solution.Item2.Where(x => x.Item1 == j + 1).Count();
                    Amatrix[i, j] -= lastCoverElementSet[i];
                }
            }

            double error = 0;

            for (int row = 0; row < numOfLabels; row++)
            {
                error += Math.Pow((Amatrix.Row(row).ToRowMatrix()
                                   .Multiply(Vector <double> .Build.Dense(coverPointSetProb[solution.Item0])
                                             .SubVector(0, coverPointSetProb[solution.Item0].Length - 1))[0] - bVector[row]), 2);
            }

            int beta = 1;

            double[] tempProbs = Copy.DeepCopy(coverPointSetProb[solution.Item0]);
            tempProbs = tempProbs.Select(x =>
            {
                double y = x * -1 - 0;
                return(Math.Pow((Math.Max(0, y)), beta));
            }).ToArray();
            double totalInfeasiability = tempProbs.Sum() / numOfLabels;
            double normalizedCost      = error / numOfLabels;
            double goodnessOfFit       = Math.Sqrt(Math.Pow(normalizedCost, 2) + Math.Pow(totalInfeasiability, 2));

            return(goodnessOfFit);
        }
Example #5
0
        Pair <int, int, double[]>[] TwoPointCrossOver(Pair <int, int, double[]>[] s1, Pair <int, int, double[]>[] s2)
        {
            var a1               = s1.Select(x => x.Item1).ToList();
            var a2               = s2.Select(x => x.Item1).ToList();
            int po1              = GlobalVar.rnd.Next(0, a1.Count - 1);
            int po2              = GlobalVar.rnd.Next(po1 + 1, a1.Count);
            var part1            = a1.Select((value, i) => { return(i < po1 ? value : -1); }).Where(x => x != -1);
            var part2            = a2.Select((value, i) => { return(i >= po1 && i <= po2 ? value : -1); }).Where(x => x != -1);
            var part3            = a1.Select((value, i) => { return(i > po2 ? value : -1); }).Where(x => x != -1);
            var newLabelSettings = part1.Concat(part2).Concat(part3).ToArray();
            var newBins          = Copy.DeepCopy(sut.bins);

            for (int i = 0; i < newBins.Length; i++)
            {
                newBins[i].Item1 = newLabelSettings[i];
            }
            return(newBins);
        }
Example #6
0
        public void GAInitialization()
        {
            pool = new Pair <int, double, Pair <int, int, double[]>[]> [populationSize];
            coverPointSetProb = new double[populationSize][];

            for (int i = 0; i < populationSize; i++)
            {
                pool[i]       = new Pair <int, double, Pair <int, int, double[]>[]>();
                pool[i].Item0 = i;
                pool[i].Item1 = -1;
                pool[i].Item2 = Copy.DeepCopy(sut.bins);
                for (int j = 0; j < pool[i].Item2.Length; j++)
                {
                    pool[i].Item2[j].Item1 = GlobalVar.rnd.Next(1, numOfLabels + 1);
                }
                SolutionRepair(pool[i].Item2);
            }
        }
Example #7
0
        public async void FitnessEvaluation(int[] token)
        {
            List <Task> lTasks  = new List <Task>();
            Mutex       mutex_K = new Mutex(false, "lockFork");

            for (int k = 0; k < populationSize;)
            {
                lTasks.Add(Task.Run(() =>
                {
                    mutex_K.WaitOne();
                    int id = k;
                    k      = k + 1;
                    mutex_K.ReleaseMutex();

                    //Console.Write("task #{0}", id);
                    Matrix <double> Amatrix      = Matrix <double> .Build.Dense(numOfLabels, numOfLabels - 1);
                    double[] lastCoverElementSet = new double[numOfLabels];
                    Vector <double> bVector      = Vector <double> .Build.Dense(expTriProb);

                    for (int i = 0; i < numOfLabels; i++)
                    {
                        if (pool[id].Item2.Where(x => x.Item1 == numOfLabels).Count() == 0)
                        {
                            Console.WriteLine();
                        }
                        lastCoverElementSet[i] = pool[id].Item2.Where(x => x.Item1 == numOfLabels).Sum(x => x.Item2[i])
                                                 / pool[id].Item2.Where(x => x.Item1 == numOfLabels).Count();
                    }
                    bVector = bVector.Subtract(Vector <double> .Build.Dense(lastCoverElementSet));

                    for (int i = 0; i < numOfLabels; i++)
                    {
                        for (int j = 0; j < numOfLabels - 1; j++)
                        {
                            if (pool[id].Item2.Where(x => x.Item1 == j + 1).Count() == 0)
                            {
                                Console.WriteLine();
                            }
                            Amatrix[i, j]  = pool[id].Item2.Where(x => x.Item1 == j + 1).Sum(x => x.Item2[i]);
                            Amatrix[i, j] /= pool[id].Item2.Where(x => x.Item1 == j + 1).Count();
                            Amatrix[i, j] -= lastCoverElementSet[i];
                        }
                    }
                    double[] probabilities = Accord.Math.Matrix.Solve(
                        Amatrix.ToArray(), bVector.ToArray(), leastSquares: true);

                    double[] probForCESets = new double[numOfLabels];
                    for (int i = 0; i < numOfLabels - 1; i++)
                    {
                        probForCESets[i] = probabilities[i];
                    }

                    probForCESets[numOfLabels - 1] = 1 - probForCESets.Sum();
                    coverPointSetProb[id]          = Copy.DeepCopy(probForCESets);
                    double error = 0;
                    for (int row = 0; row < numOfLabels; row++)
                    {
                        error += Math.Pow((Amatrix.Row(row).ToRowMatrix().Multiply(Vector <double> .Build.Dense(probForCESets).SubVector(0, probForCESets.Length - 1))[0] - bVector[row]), 2);
                    }

                    pool[id].Item1 = error;
                }));

                if (lTasks.Count == numberOfConcurrentTasks || (populationSize - k - 1 < numberOfConcurrentTasks))
                {
                    for (int i = 0; i < lTasks.Count; i++)
                    {
                        await lTasks[i];
                    }
                    lTasks.Clear();
                }
            }

            for (int k = 0; k < populationSize; k++)
            {
                double adjustFitness = SelfAdaptivePenaltyFunction(coverPointSetProb[k], pool[k].Item1);
                pool[k].Item1 = 1.0 / adjustFitness;
            }

            token[0] = 1;
            //Console.WriteLine("Fitness Evaluation Done");
        }
Example #8
0
        public void FitnessEvaluationNoParll(int[] token)
        {
            for (int k = 0; k < populationSize; k++)
            {
                //Console.Write("task #{0}", id);
                Matrix <double> Amatrix = Matrix <double> .Build.Dense(numOfLabels, numOfLabels - 1);

                double[]        lastCoverElementSet = new double[numOfLabels];
                Vector <double> bVector             = Vector <double> .Build.Dense(expTriProb);

                for (int i = 0; i < numOfLabels; i++)
                {
                    if (pool[k].Item2.Where(x => x.Item1 == numOfLabels).Count() == 0)
                    {
                        Console.WriteLine();
                    }
                    lastCoverElementSet[i] = pool[k].Item2.Where(x => x.Item1 == numOfLabels).Sum(x => x.Item2[i])
                                             / pool[k].Item2.Where(x => x.Item1 == numOfLabels).Count();
                }
                bVector = bVector.Subtract(Vector <double> .Build.Dense(lastCoverElementSet));

                for (int i = 0; i < numOfLabels; i++)
                {
                    for (int j = 0; j < numOfLabels - 1; j++)
                    {
                        if (pool[k].Item2.Where(x => x.Item1 == j + 1).Count() == 0)
                        {
                            Console.WriteLine();
                        }
                        Amatrix[i, j]  = pool[k].Item2.Where(x => x.Item1 == j + 1).Sum(x => x.Item2[i]);
                        Amatrix[i, j] /= pool[k].Item2.Where(x => x.Item1 == j + 1).Count();
                        Amatrix[i, j] -= lastCoverElementSet[i];
                    }
                }
                double[] probabilities = Accord.Math.Matrix.Solve(
                    Amatrix.ToArray(), bVector.ToArray(), leastSquares: true);

                double[] probForCESets = new double[numOfLabels];
                for (int i = 0; i < numOfLabels - 1; i++)
                {
                    probForCESets[i] = probabilities[i];
                }

                probForCESets[numOfLabels - 1] = 1 - probForCESets.Sum();
                coverPointSetProb[k]           = Copy.DeepCopy(probForCESets);
                double error = 0;
                for (int row = 0; row < numOfLabels; row++)
                {
                    error += Math.Pow((Amatrix.Row(row).ToRowMatrix().Multiply(Vector <double> .Build.Dense(probForCESets).SubVector(0, probForCESets.Length - 1))[0] - bVector[row]), 2);
                }

                pool[k].Item1 = error;
            }

            for (int k = 0; k < populationSize; k++)
            {
                double adjustFitness = SelfAdaptivePenaltyFunction(coverPointSetProb[k], pool[k].Item1);
                pool[k].Item1 = 1.0 / adjustFitness;
            }
            token[0] = 1;
            //Console.WriteLine("Fitness Evaluation Done");
        }
Example #9
0
        public void AlgorithmStart(record record)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            int[] token = new int[1];
            FitnessEvaluationNoParll(token);
            while (token[0] == 0)
            {
                ;
            }
            token[0] = 0;

            Queue <double> last20fitness = new Queue <double>();

            while (generation < maxGen)
            {
                Reproduction();
                FitnessEvaluationNoParll(token);
                while (token[0] == 0)
                {
                    ;
                }
                token[0] = 0;

                // Start Recording
                watch.Stop();
                var orderedSolutions = pool.OrderByDescending(x => x.Item1);
                int index            = -1;
                Pair <int, double, Pair <int, int, double[]>[]> bestSolution = null;
                double[] probabilityset = null;

                for (int i = 0; i < populationSize; i++)
                {
                    index          = orderedSolutions.ElementAt(i).Item0;
                    probabilityset = coverPointSetProb[index];
                    if (probabilityset.All(x => x >= 0))
                    {
                        bestSolution = orderedSolutions.ElementAt(i);
                        break;
                    }
                }

                if (bestSolution == null)
                {
                    Console.WriteLine("Current Task ID #{0}", Task.CurrentId);
                    Console.WriteLine("No Feasiable Solution, # Of negative Values {0}",
                                      coverPointSetProb[orderedSolutions
                                                        .First().Item0].Sum(x =>
                    {
                        if (x < 0)
                        {
                            return(Math.Abs(x));
                        }
                        else
                        {
                            return(0);
                        }
                    }));
                }
                else
                {
                    double[] estTriProbabilities = new double[numOfLabels];
                    for (int i = 0; i < numOfLabels; i++)
                    {
                        double[] triProbsCoverSet = new double[numOfLabels];
                        for (int j = 0; j < numOfLabels; j++)
                        {
                            triProbsCoverSet[j]  = bestSolution.Item2.Where(x => x.Item1 == j + 1).Sum(x => x.Item2[i]);
                            triProbsCoverSet[j] /= bestSolution.Item2.Where(x => x.Item1 == j + 1).Count();
                        }
                        estTriProbabilities[i] = Vector <double> .Build.Dense(triProbsCoverSet).ToRowMatrix().
                                                 Multiply(Vector <double> .Build.Dense(coverPointSetProb[index]))[0];
                    }
                    Console.WriteLine("Estimated Triggering Probabilities {0}", estTriProbabilities.Min());
                }
                record.fitnessGen[generation]       = 1.0 / orderedSolutions.First().Item1;
                record.goodnessOfFitGen[generation] = GoodnessOfFit(orderedSolutions.First());

                Console.WriteLine("Gen #{0} Fitness: {1}", generation, record.goodnessOfFitGen[generation]);
                watch.Start();

                if (last20fitness.Count == 100)
                {
                    last20fitness.Dequeue();
                    last20fitness.Enqueue(1.0 / orderedSolutions.First().Item1);
                    if (last20fitness.All(x => x == last20fitness.First()))
                    {
                        break;
                    }
                }
                else
                {
                    last20fitness.Enqueue(1.0 / orderedSolutions.First().Item1);
                }

                generation += 1;
            }

            watch.Stop();

            var rankedOneSolution = pool.OrderByDescending(x => x.Item1).First();

            record.bestSolution.binSetup = Copy.DeepCopy(rankedOneSolution.Item2.Select(x => x.Item1).ToArray());
            var totalRuningTime = watch.ElapsedMilliseconds;

            record.bestSolution.totalRunTime     = totalRuningTime;
            record.bestSolution.setProbabilities = Copy.DeepCopy(
                coverPointSetProb[rankedOneSolution.Item0]);
        }
        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;
        }
Example #11
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);
                }
            }
        }