Exemple #1
0
        public void ShouldValidate(bool?input, OptimizationModes expected)
        {
            var target = new DisableBailInput {
                SuppliedInput = input
            };

            var result = target.Validate();

            result.ShouldBe(expected);
        }
Exemple #2
0
        public void init(int dim, int populationSize, double[] initialMean, float initialStepSize, OptimizationModes mode)
        {
            this.dim = dim;
            this.mode = mode;
            int N = dim;
            lambda = populationSize;	// population size, e.g., 4+floor(3*log(N));
            mu = lambda / 2;					// number of parents, e.g., floor(lambda/2);
            sigma = 0.3 * 2.0f;		// initial step-size
            chiN = Math.Sqrt((double)N) * (1.0 - 1.0 / (4.0 * N) + 1 / (21.0 * N * N));

            M = new double[N, N];
            Mmult = new double[N, N];
            ps = new double[N];
            xmean = new double[N];
            xBest = new double[N];
            zmean = new double[N];
            xold = new double[N];
            z = new double[N];
            Mz = new double[N];
            weights = new double[mu];
            arfitness = new double[lambda];
            arindex = new int[lambda];
            arr_tmp = new List<SortedVal>(lambda);
            bestFitness = mode == OptimizationModes.minimize ? double.PositiveInfinity : double.NegativeInfinity;
            for (int i = 0; i < lambda; i++)
            {
                arx.Add(new double[N]);
                arz.Add(new double[N]);
                ard.Add(new double[N]);
            }
            for (int i = 0; i < lambda; i++)
            {
                arr_tmp.Add(new SortedVal());
            }

            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                    if (i == j) M[i, j] = 1.0;
                    else M[i, j] = 0.0;


            double sum_weights = 0;
            for (int i = 0; i < mu; i++)
            {
                weights[i] = Math.Pow(Math.Log(0.5 + (double)mu) - Math.Log((double)(1 + i)), 1.0);
                sum_weights = sum_weights + weights[i];
            }
            mueff = 0;
            for (int i = 0; i < mu; i++)
            {
                weights[i] = weights[i] / sum_weights;
                mueff = mueff + weights[i] * weights[i];
            }
            mueff = 1.0 / mueff;

            for (int i = 0; i < N; i++)
            {
                ps[i] = 0;
            }

            cs = (mueff + 2.0) / (N + mueff + 5.0);
            lcoef = 2.0;
            c1 = lcoef / (Math.Pow(N + 1.3, 2.0) + mueff);
            cmu = minv(1.0 - c1, lcoef * (mueff - 2.0 + 1.0 / mueff) / (Math.Pow(N + 2.0, 2.0) + mueff));

            if (initialMean.Length != dim)
                Debug.LogException(new Exception("Incorrect array length!"));
            Array.Copy(initialMean, xmean, xmean.Length);
            sigma = initialStepSize;
        
        }
Exemple #3
0
        public void init(int dim, int populationSize, double[] initialMean, float initialStepSize, OptimizationModes mode)
        {
            this.dim = dim;
            this.mode = mode;
            int N = dim;
            // set boring parameters
            lambda = populationSize;
            mu = lambda / 2;					// number of parents, e.g., floor(lambda/2);
            sigma = initialStepSize;		// initial step-size
            chiN = Math.Sqrt((double)N) * (1.0 - 1.0 / (4.0 * N) + 1 / (21.0 * N * N));

            // set interesting (to be tuned) parameters
            nvectors = 4 + 3 * (int)Math.Log((double)N);	// number of stored direction vectors, e.g., nvectors = 4+floor(3*log(N))
            cs = 2.0 * ((double)lambda) / N;		// nvectors;			

            // memory allocation
            //ibit = 31;								// for the fast Rademacher sampling

            arx = new List<double[]>();
            arz = new List<double[]>();
            for (int i = 0; i < lambda; i++)
            {
                arx.Add(new double[N]);
                arz.Add(new double[N]);
            }
            ps_arr = new double[nvectors, N];
            ps = new double[N];
            xmean = new double[N];
            xBest = new double[N];
            zmean = new double[N];
            xold = new double[N];
            z = new double[N];
            dz = new double[N];
            Az = new double[N];
            weights = new double[mu];
            arfitness = new double[lambda];
            arindex = new int[lambda];
            arr_tmp = new List<SortedVal>(lambda);
            for (int i = 0; i < lambda; i++)
            {
                arr_tmp.Add(new SortedVal());
            }
            double sum_weights = 0;
            for (int i = 0; i < mu; i++)
            {
                weights[i] = Math.Pow(Math.Log(0.5 + (double)mu) - Math.Log((double)(1 + i)), 1.0);
                sum_weights = sum_weights + weights[i];
            }
            mueff = 0;
            for (int i = 0; i < mu; i++)
            {
                weights[i] = weights[i] / sum_weights;
                mueff = mueff + weights[i] * weights[i];
            }
            mueff = 1.0 / mueff;
            if (N < 32)
                Debug.LogWarning("Warning: LM-MA-ES is not optimal for low-dimensional problems. You should use MA-ES instead");
                //With small N, LMMAES way of computing cs can result in negative or zero values.
                //In this case, we revert to how it's computed in CMA-ES and MA-ES.
                //However, this is a hack to prevent crashes, and one should rather use MA-ES than LM-MA-ES with small N.
                cs = (mueff + 2.0) / (N + mueff + 5.0);  


            for (int i = 0; i < N; i++)
            {
                ps[i] = 0;
            }
            if (initialMean.Length != dim)
                Debug.LogException(new Exception("Incorrect array length!"));
            Array.Copy(initialMean, xmean, xmean.Length);

            for (int i = 0; i < nvectors; i++)
                for (int j = 0; j < N; j++)
                    ps_arr[i, j] = 0;
            bestFitness = mode == OptimizationModes.minimize ? double.PositiveInfinity : double.NegativeInfinity;
            iterator_sz = 0;
        }