public List <string> Print_Solution(GAEncoding solution)
        {
            string theta  = null;
            string weight = null;

            for (int i = 0; i < solution.thetaDelta.RowCount; i++)
            {
                string          tmp   = "(";
                Vector <double> vTemp = solution.thetaDelta.Row(i);
                for (int j = 0; j < dimension; j++)
                {
                    tmp = tmp + Math.Round(vTemp[j], 2) + " ";
                }
                tmp.Remove(tmp.Length - 1, 1);
                tmp   = tmp + ")";
                theta = theta + tmp + " ";
            }
            theta.Remove(theta.Length - 1, 1);

            for (int i = 0; i < solution.weightsVect.Count; i++)
            {
                weight = weight + solution.weightsVect[i] + " ";
            }
            weight.Remove(weight.Length - 1, 1);

            return(new List <string>()
            {
                theta, weight
            });
        }
        public async void Reproduction(int[] token)
        {
            int        maxNumOfTasks = 20;
            GAEncoding child         = new GAEncoding(pNumOfParams, dimension, lowbounds, highbounds);
            int        k             = 0;
            var        rankOneSol    = cePool.Where(x => x.Value[0] == cePool.Max(y => y.Value[0])).First();

            tempPool.Add(Copy.DeepCopy(rankOneSol.Key), rankOneSol.Value);

            List <Task> lTasks = new List <Task>();

            if (cePool.Count < 100)
            {
                Console.WriteLine("RRRRRR");
            }

            while (tempPool.Count < popSize)
            {
                lTasks.Add(Task.Run(() =>
                {
                    int[] selectedList = FitnessPropotionateSampling(pPanCombSize);
                    child = Copy.DeepCopy(cePool.ElementAt(selectedList[0]).Key);
                    if (GlobalVar.rnd.NextDouble() <= pmCrossOverRate)
                    {
                        if (cePool.Count < 100)
                        {
                            Console.WriteLine();
                        }

                        child = cePool.ElementAt(0).Key.IntermidinateRecomb(cePool, selectedList);
                        child = child.RealVectMutation();
                        GlobalVar.mutex_K.WaitOne();
                        if (!tempPool.ContainsKey(child))
                        {
                            tempPool.Add(child, new double[] { -1, -1 });
                            k = k + 1;
                        }
                        GlobalVar.mutex_K.ReleaseMutex();
                    }
                }));

                if (lTasks.Count == maxNumOfTasks)
                {
                    for (int i = 0; i < lTasks.Count; i++)
                    {
                        await lTasks[i];
                    }
                    lTasks.Clear();
                    int remainTasks = cePool.Count - k;
                    if (remainTasks <= maxNumOfTasks)
                    {
                        maxNumOfTasks = remainTasks;
                    }
                }
            }

            token[0] = 1;
        }
        public async void Reproduction(int[] token)
        {
            int maxNumOfTasks = 20;
            int k             = 0;

            List <Task> lTasks = new List <Task>();

            while (tempPool.Count < popSize)
            {
                lTasks.Add(Task.Run(() =>
                {
                    GlobalVar.mutex_K.WaitOne();
                    int index = k;
                    k         = k + 1;
                    GlobalVar.mutex_K.ReleaseMutex();
                    GAEncoding child   = Copy.DeepCopy(cePool.ElementAt(index).Key);
                    int[] selectedList = DifferentialSelection(index);
                    child = child.DifferentialCrossover(cePool, selectedList);

                    child.FitnessCal(aMatrix);
                    if (child.entropy > cePool.ElementAt(index).Key.entropy
                        /*&& child.diversity > cePool.ElementAt(index).Key.diversity*/)
                    {
                        GlobalVar.mutex_K.WaitOne();
                        tempPool.Add(child, new double[] { child.entropy, -1 });
                        GlobalVar.mutex_K.ReleaseMutex();
                    }
                    else
                    {
                        GlobalVar.mutex_K.WaitOne();
                        tempPool.Add(cePool.ElementAt(index).Key,
                                     new double[] { cePool.ElementAt(index).Key.entropy, -1 });
                        GlobalVar.mutex_K.ReleaseMutex();
                    }
                }));

                if (lTasks.Count == maxNumOfTasks)
                {
                    for (int i = 0; i < lTasks.Count; i++)
                    {
                        await lTasks[i];
                    }
                    lTasks.Clear();
                    int remainTasks = cePool.Count - k;
                    if (remainTasks <= maxNumOfTasks)
                    {
                        maxNumOfTasks = remainTasks;
                    }
                }
            }

            token[0] = 1;
        }
示例#4
0
        //Simple Mutation Assumes independence of each variable
        internal GAEncoding RealVectMutation()
        {
            GAEncoding newSolution = Copy.DeepCopy(this);

            UpdateID();
            newSolution.FixInputs(_lowbounds, _highbounds);

            //for (int i = 0; i < _numOfParams * _numOfParams; i++)
            //{
            //    if (GlobalVar.rnd.NextDouble()
            //        <= 2.0 / (_numOfParams * _numOfParams))
            //    {
            //        double k_MutPrecision = 1;
            //        double u = GlobalVar.rnd.NextDouble();
            //        int s_Direction = GlobalVar.rnd.NextDouble() <= 0.5 ? -1 : 1;
            //        double a_StepSize1 = 10*u + s_Direction;
            //        double mean = weightsVect.Sum()/(_numOfParams*_numOfParams);
            //        weightsVect[i] = weightsVect[i] * a_StepSize1 + mean * (1 - a_StepSize1);
            //        if (weightsVect[i] <= 0)
            //        {
            //            weightsVect[i] = 0.0001;
            //        }
            //    }
            //}

            if (GlobalVar.rnd.NextDouble() <= 1.0 / 100)
            {
                for (int i = 0; i < _numOfParams * _numOfParams; i++)
                {
                    double k_MutPrecision = 1;
                    double u           = GlobalVar.rnd.NextDouble();
                    int    s_Direction = GlobalVar.rnd.NextDouble() <= 0.5 ? -1 : 1;
                    double a_StepSize1 = 10 * u + s_Direction;
                    double mean        = weightsVect.Sum() / (_numOfParams * _numOfParams);
                    weightsVect[i] = weightsVect[i] * a_StepSize1 + mean * (1 - a_StepSize1);
                    if (weightsVect[i] <= 0)
                    {
                        weightsVect[i] = 0.0001;
                    }
                }
            }

            weightsVect = weightsVect.Normalize(1).Multiply(_numOfParams * _numOfParams);

            double test = weightsVect.Sum();

            if (test > _numOfParams * _numOfParams + 0.001 || test < _numOfParams * _numOfParams - 0.001)
            {
                Console.WriteLine();
            }
            return(newSolution);
        }
 private void GA_Initialization()
 {
     cePool                  = new CEPool();
     tempPool                = new CEPool();
     ceDB                    = new CEPool();
     cumulativeArray         = new double[popSize];
     Console.BackgroundColor = ConsoleColor.Yellow;
     for (int i = 0; i < popSize; i++)
     {
         var newSolution = new GAEncoding(pNumOfParams, dimension, lowbounds, highbounds);
         newSolution.FixInputs(lowbounds, highbounds);
         tempPool.Add(newSolution, new double[] { -1, -1 });
     }
     for (int i = 0; i < pNumOfParams * pNumOfParams; i++)
     {
         dt.Columns.Add(i.ToString(), Type.GetType("System.Double"));
     }
 }
示例#6
0
        // Recombination: create a new solution, call this function;
        // Add the new solution to the population
        internal GAEncoding IntermidinateRecomb(CEPool currentPool, int[] selectedList)
        {
            double     d           = 0.25;
            GAEncoding newSolution = new GAEncoding(_numOfParams, _dimension, _lowbounds, _highbounds);
            double     alpha       = GlobalVar.rnd.NextDouble();

            alpha = (1 + d - -1 * d) * alpha + -1 * d;
            var deltaW = currentPool.ElementAt(selectedList[1]).Key.weightsVect
                         - currentPool.ElementAt(selectedList[0]).Key.weightsVect;

            newSolution.weightsVect =
                deltaW.Multiply(alpha) + currentPool.ElementAt(selectedList[0]).Key.weightsVect;

            double test = newSolution.weightsVect.Sum();

            if (test > _numOfParams * _numOfParams + 0.001 || test < _numOfParams * _numOfParams - 0.001)
            {
                Console.WriteLine();
            }
            newSolution.FixInputs(_lowbounds, _highbounds);
            newSolution.UpdateID();
            return(newSolution);
        }
        public async void GA_FitnessEvaluation(int[] token)
        {
            List <Task> lTasks = new List <Task>();

            // Test inputs Generation
            // Fitness Evaluation
            for (int k = 0; k < tempPool.Count;)
            {
                // Single Thread
                //ga.CalEstFitness(testSetSize, numOfLabel, labelMatrix, lowbounds, highbounds);

                lTasks.Add(Task.Run(() =>
                {
                    GlobalVar.mutex_K.WaitOne();
                    // Console.WriteLine(k);
                    GAEncoding ga = tempPool.ElementAt(k).Key;
                    int id        = k;
                    k             = k + 1;
                    GlobalVar.mutex_K.ReleaseMutex();
                    //ga.CalEstFitness(testSetSize, numOfLabel, labelMatrix, lowbounds, highbounds);
                    //ga.TrueFitnessCal(numOfLabel, labelMatrix);
                    ga.FitnessCal(aMatrix);
                    //Console.WriteLine("{0} job finish",id);
                }));
                if (lTasks.Count == 20 || (tempPool.Count - k - 1 < 20))  //8,8
                {
                    for (int i = 0; i < lTasks.Count; i++)
                    {
                        await lTasks[i];
                    }
                    lTasks.Clear();
                }
            }
            token[0] = 1;
            //Console.WriteLine("Fitness Evaluation Done");
        }