Exemplo n.º 1
0
        void GenBasis(int DIM)
        {
            if (B != null ? B.Length == DIM : false)
            {
                return;
            }

            double sp;
            int    i, j, k;

            /* generate orthogonal basis */
            B = new double[DIM][];
            for (int l = 0; l < B.Length; l++)
            {
                B[l] = new double[DIM];
            }


            for (i = 0; i < DIM; ++i)
            {
                /* sample components gaussian */
                for (j = 0; j < DIM; ++j)
                {
                    B[i][j] = CMAEvolutionStrategy.nextGaussian(rand);
                }
                /* substract projection of previous vectors */
                for (j = i - 1; j >= 0; --j)
                {
                    for (sp = 0.0, k = 0; k < DIM; ++k)
                    {
                        sp += B[i][k] * B[j][k]; /* scalar product */
                    }
                    for (k = 0; k < DIM; ++k)
                    {
                        B[i][k] -= sp * B[j][k]; /* substract */
                    }
                }
                /* normalize */
                for (sp = 0.0, k = 0; k < DIM; ++k)
                {
                    sp += B[i][k] * B[i][k]; /* squared norm */
                }
                for (k = 0; k < DIM; ++k)
                {
                    B[i][k] /= Math.Sqrt(sp);
                }
            }
        }
Exemplo n.º 2
0
 public Timing()
 {
     birth = CMAEvolutionStrategy.CurrentTimeMillis();
     start = birth; // on the save side
 }
Exemplo n.º 3
0
        public override double valueOf(double[] x)
        {
            double res = CMAEvolutionStrategy.nextDouble(rand);

            return(res);
        }
Exemplo n.º 4
0
        public void Run()
        {
            #region cma settings (auto generated)
            CMAEvolutionStrategy cma = new CMAEvolutionStrategy(this.taskType);

            //number of dimension (only those who have candidate value counted)
            int dimension = evalHelper.Dimensions();

            //initial X and STD (only those who have candidate value counted)
            List <double> initialX   = new List <double>();
            List <double> initialStd = new List <double>();

            foreach (Parameter parameter in evalHelper.Parameters)
            {
                if (parameter.Source == ParameterSource.CandidateValue)
                {
                    initialX.Add((parameter.MaxCandidateValue() + parameter.MinCandidateValue()) / 2);
                    initialStd.Add((parameter.MaxCandidateValue() - parameter.MinCandidateValue()) / 3);
                }
            }

            double[] fitness = cma.init(dimension, initialX.ToArray(), initialStd.ToArray());

            #endregion

            while (cma.getNumber() == 0)
            {
                double[][] pop = cma.samplePopulation();
                for (int i = 0; i < pop.Length; ++i)
                {
                    //break the loop if canceled by user while in process
                    if (evalHelper.IsCanceled())
                    {
                        evalHelper.OnCanceled(EventArgs.Empty);
                        return;
                    }

                    while (!evalHelper.IsFeasible(pop[i]))
                    {
                        pop[i] = cma.resampleSingle(i);
                    }
                    fitness[i] = evalHelper.ValueOf(pop[i]);
                }
                cma.updateDistribution(fitness);

                //stopping criteria.
                cma.options.stopTolFun     = 0.000001 * cma.getBestFunctionValue();//default:0.02
                cma.options.stopTolFunHist = 0.1 * cma.options.stopTolFun;
            }
            cma.setFitnessOfMeanX(evalHelper.ValueOf(cma.getMeanX()));

            //final output
            string simpleTextReport;
            simpleTextReport = "Terminated due to" + Environment.NewLine;
            foreach (String s in cma.getMessages())
            {
                simpleTextReport = simpleTextReport + ("  " + s + Environment.NewLine);
            }
            simpleTextReport = simpleTextReport + "Best function value " + cma.getBestFunctionValue();

            evalHelper.OnOptimizationComplete(new OptimizationCompleteEventArgs {
                SimpleTextReport = simpleTextReport
            });
        }