Esempio n. 1
0
        private void Solve(int iteration, ref CMAState cmaState, ref Solution solution, bool isOnce = false)
        {
            //ColumnVector xmean = new ColumnVector(parameters);

            double tolX = this.StandardDeviationToleranceMultiple * this.InitialVarianceEstimate;

            int stopeval = this.StopEvals;

            RectangularMatrix arx       = new RectangularMatrix(cmaState.p.N, cmaState.p.Lambda);
            ColumnVector      arfitness = new ColumnVector(cmaState.p.Lambda);

            bool isRestart    = false;
            bool isGoodEnough = false;
            int  counteval    = 0;

            while (counteval < stopeval)
            {
                // Generate and evaluate lambda offspring
                for (int k = 0; k < cmaState.p.Lambda; k++)
                {
                    double[] sample = cmaState.Sample(arx.Column(k));
                    for (int r = 0; r < cmaState.p.N; r++)
                    {
                        arx[r, k] = sample[r];
                    }

                    arfitness[k] = this.ObjectiveFunction(arx.Column(k).ToArray(), this.UserData);

                    counteval++;
                }

                // Sort by fitness and compute weighted mean into xmean
                // minimization
                int[] arindex;
                arfitness = Utilities.Sort(arfitness, out arindex);                 // one based index

                int mu = cmaState.p.Mu;
                cmaState.ReEstimate(
                    (RectangularMatrix)Utilities.GetColumns(arx, arindex.Take(cmaState.p.Mu)),
                    arfitness.First(),
                    arfitness.SkipWhile((f, i) => i < (mu - 1)).First());

                cmaState.UpdateEigenSystem(1, 0);


                if ((solution.StopReason == StopReason.NotSet) || (arfitness[0] < solution.MinimizedFitness))
                {
                    solution.StopReason       = StopReason.Incomplete;
                    solution.MinimizedFitness = arfitness[0];
                    solution.BestCMAState     = cmaState;
                    solution.BestNumEvals     = counteval;
                    solution.BestNumRestarts  = iteration;
                    solution.BestParameters   = cmaState.mean.ToArray();
                }

                if (arfitness[0] <= this.StopFitnessMinimization)
                {
                    isGoodEnough = true;
                    break;
                }

                if (IsRestart(ref solution, arfitness, tolX, cmaState))
                {
                    isRestart = true;
                    break;
                }

                if (isOnce)
                {
                    break;
                }
            }

            if ((isRestart) && (iteration > this.MaxRestarts))
            {
                solution.StopReason = StopReason.MaxRestarts;
            }
            else if ((isRestart) && ((cmaState.p.Lambda * this.RestartPopulationMultiple) > MAX_POPULATIONSIZE))
            {
                solution.StopReason = StopReason.MaxPopulation;
            }
            else if (isRestart)
            {
                solution.History    = new Queue <double>();
                solution.StopReason = StopReason.Restart;
                int previousLambda = cmaState.p.Lambda;
                cmaState = new CMAState(_initialCmaParams, new ColumnVector(this.InitialParameters), this.InitialVarianceEstimate);
                cmaState.p.UpdateLambda((int)(previousLambda * this.RestartPopulationMultiple));
            }
            else if (isGoodEnough)
            {
                solution.StopReason = StopReason.GoodEnough;
            }
            else
            {
                solution.StopReason = StopReason.MaxIterations;
            }

            solution.LatestCMAState    = cmaState;
            solution.LatestNumEvals    = counteval;
            solution.LatestNumRestarts = iteration;
            solution.LatestParameters  = cmaState.mean.ToArray();
        }