예제 #1
0
        public void SmacOptimizer_Optimize()
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0.0, 100.0, Transform.Linear)
            };

            var sut = new SmacOptimizer(parameters,
                                        iterations: 80,
                                        randomStartingPointCount: 20,
                                        functionEvaluationsPerIterationCount: 1,
                                        localSearchPointCount: 10,
                                        randomSearchPointCount: 1000,
                                        epsilon: 0.00001,
                                        seed: 42);

            var actual = sut.Optimize(MinimizeWeightFromHeight);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 90.513222660177 }, 114559.431919558),
                new OptimizerResult(new double[] { 41.8333740634068 }, 806.274612132759),
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), Delta);
        }
예제 #2
0
        private IEnumerable <IExpressionToken> Compile(OptimizerResult optimizeResult)
        {
            var result = new List <IExpressionToken>();

            foreach (var token in optimizeResult.ExpressionTokens)
            {
                if (!(token is RawToken))
                {
                    result.Add(token);
                }
                else
                {
                    var rawTokenString = token.GetStringRepresentation();
                    var functionTokens = new List <FunctionToken>();

                    foreach (var compiledToken in CompileStringValue(rawTokenString))
                    {
                        if (compiledToken is VariableToken variableToken && optimizeResult.VariableTokens.ContainsKey(variableToken.Text))
                        {
                            var variableOptimizer       = optimizeResult.VariableTokens[variableToken.Text];
                            var variableOptimizerResult = Compile(variableOptimizer);
                            result.AddRange(variableOptimizerResult);
                            //i need to set new function tokens count
                            foreach (var item in functionTokens)
                            {
                                item.FunctionTokensCount += variableOptimizerResult.Count() - 1;
                            }
                            continue;
                        }
예제 #3
0
        public void GridSearchOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new GridParameterSpec[]
            {
                new GridParameterSpec(10.0, 20.0, 30.0, 35.0, 37.5, 40.0, 50.0, 60.0)
            };

            var sut = maxDegreeOfParallelism.HasValue ?
                      new GridSearchOptimizer(parameters, true, maxDegreeOfParallelism.Value) :
                      new GridSearchOptimizer(parameters);

            var actual = sut.Optimize(MinimizeWeightFromHeight);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 10 }, 31638.9579),
                new OptimizerResult(new double[] { 60 }, 20500.6279)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(),
                            actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(),
                            actual.Last().ParameterSet.First(), Delta);
        }
        public void ParticleSwarmOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0.0, 100.0, Transform.Linear)
            };

            var sut = CreateSut(maxDegreeOfParallelism, parameters);

            var results = sut.Optimize(MinimizeWeightFromHeight);

            var actual = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 38.1151505704492 }, 115.978346548015),
                new OptimizerResult(new double[] { 37.2514904205637 }, 118.093289672808),
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(),
                            actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(),
                            actual.Last().ParameterSet.First(), Delta);
        }
예제 #5
0
        public void GlobalizedBoundedNelderMeadOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0.0, 100.0, Transform.Linear)
            };

            var sut = CreateSut(maxDegreeOfParallelism, parameters);

            var results = sut.Optimize(MinimizeWeightFromHeight);
            var actual  = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.71314634450421 }, 109.3438139631394),
                new OptimizerResult(new double[] { 37.713142445047254 }, 109.34381396345546)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(),
                            actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(),
                            actual.Last().ParameterSet.First(), Delta);
        }
        public void GlobalizedBoundedNelderMeadOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0.0, 100.0, Transform.Linear)
            };

            var sut = maxDegreeOfParallelism.HasValue ?
                      new GlobalizedBoundedNelderMeadOptimizer(parameters, 5, 1e-5, 10,
                                                               maxDegreeOfParallelism: maxDegreeOfParallelism.Value) :
                      new GlobalizedBoundedNelderMeadOptimizer(parameters, 5, 1e-5, 10);

            var results = sut.Optimize(MinimizeWeightFromHeight);
            var actual  = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.71314535727786 }, 109.34381396310141),
                new OptimizerResult(new double[] { 37.7131485180996 }, 109.34381396350526)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(),
                            actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(),
                            actual.Last().ParameterSet.First(), Delta);
        }
예제 #7
0
        public void HyperbandOptimizer_OptimizeBest()
        {
            var parameters = new IParameterSpec[]
            {
                new MinMaxParameterSpec(min: 80, max: 300, transform: Transform.Linear),   // iterations
                new MinMaxParameterSpec(min: 0.02, max:  0.2, transform: Transform.Log10), // learning rate
                new MinMaxParameterSpec(min: 8, max: 15, transform: Transform.Linear),     // maximumTreeDepth
            };

            var random = new Random(343);
            HyperbandObjectiveFunction minimize = (p, r) =>
            {
                var error = random.NextDouble();
                return(new OptimizerResult(p, error));
            };

            var sut = new HyperbandOptimizer(
                parameters,
                maximumUnitsOfCompute: 81,
                eta: 5,
                skipLastIterationOfEachRound: false,
                seed: 34);

            var actual   = sut.OptimizeBest(minimize);
            var expected = new OptimizerResult(new[] { 278.337940, 0.098931, 13.177449 }, 0.009549);

            AssertOptimizerResult(expected, actual);
        }
        public void BayesianOptimizer_Optimize()
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0.0, 100.0, Transform.Linear)
            };

            var sut = new BayesianOptimizer(parameters,
                                            iterations: 120,
                                            randomStartingPointCount: 5,
                                            functionEvaluationsPerIterationCount: 1,
                                            randomSearchPointCount: 1000,
                                            seed: 42,
                                            runParallel: false); // Note, since the returned results are not ordered on error,
                                                                 // running with parallel computations will not return reproducible order of results,
                                                                 // so runParallel must be false for this test.

            var results = sut.Optimize(MinimizeWeightFromHeight);
            var actual  = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 43.216748276360683 }, 1352.8306605984087),
                new OptimizerResult(new double[] { 38.201425707992833 }, 119.1316225267316)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), Delta);
        }
        public void RandomSearchOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(10.0, 37.5, Transform.Linear)
            };

            var sut = maxDegreeOfParallelism.HasValue ?
                      new RandomSearchOptimizer(parameters, 2, 42, true, maxDegreeOfParallelism.Value) :
                      new RandomSearchOptimizer(parameters, 2);

            var actual = sut.Optimize(MinimizeWeightFromHeight);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 13.8749507052707 }, 23438.2157641635),
                new OptimizerResult(new double[] { 28.3729278125674 }, 3690.81119818742),
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(),
                            actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(),
                            actual.Last().ParameterSet.First(), Delta);
        }
예제 #10
0
        public void GridSearchOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new GridParameterSpec[]
            {
                new GridParameterSpec(10.0, 37.5)
            };

            var sut = maxDegreeOfParallelism.HasValue ?
                      new GridSearchOptimizer(parameters, true, maxDegreeOfParallelism.Value) :
                      new GridSearchOptimizer(parameters);

            var actual = sut.Optimize(Minimize);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.5 }, 111.20889999999987),
                new OptimizerResult(new double[] { 10 }, 31638.9579)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
예제 #11
0
        public void ParticleSwarmOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0.0, 100.0, Transform.Linear)
            };

            var sut = maxDegreeOfParallelism.HasValue ?
                      new ParticleSwarmOptimizer(parameters, 100, maxDegreeOfParallelism: maxDegreeOfParallelism.Value) :
                      new ParticleSwarmOptimizer(parameters, 100);

            var results = sut.Optimize(Minimize2);

            var actual = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 38.1151505704492 }, 115.978346548015),
                new OptimizerResult(new double[] { 37.2514904205637 }, 118.093289672808),
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
예제 #12
0
        public void RandomSearchOptimizer_Optimize(int?maxDegreeOfParallelism)
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(10.0, 37.5, Transform.Linear)
            };

            var sut = maxDegreeOfParallelism.HasValue ?
                      new RandomSearchOptimizer(parameters, 100, 42, true, maxDegreeOfParallelism.Value) :
                      new RandomSearchOptimizer(parameters, 100);

            var actual = sut.Optimize(MinimizeWeightFromHeight);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 28.3729278125674 }, 3690.81119818742),
                new OptimizerResult(new double[] { 19.1529422843144 }, 14251.396910816733),
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, Delta);
            Assert.AreEqual(expected.First().ParameterSet.First(),
                            actual.First().ParameterSet.First(), Delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, Delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(),
                            actual.Last().ParameterSet.First(), Delta);
        }
        public virtual OptimizerResult Minimize(double[] p, Chromosome configChromosome)
        {
            var id = Guid.NewGuid().ToString("N");

            try
            {
                StringBuilder output = new StringBuilder();
                var           list   = configChromosome.ToDictionary();

                list.Add("Id", id);
                output.Append("Id: " + id + ", ");

                var isActual = false;
                lock (Locker)
                {
                    isActual     = !HasRunActual ? true : false;
                    HasRunActual = true;
                }

                for (int i = 0; i < Config.Genes.Count(); i++)
                {
                    var key       = Config.Genes.ElementAt(i).Key;
                    var precision = Config.Genes.ElementAt(i).Precision ?? 0;

                    if (isActual)
                    {
                        p[i] = Config.Genes[i].Actual ?? p[i];
                    }

                    var value = Math.Round(p[i], precision);
                    list[key] = value;

                    output.Append(key + ": " + value.ToString() + ", ");
                }
                isActual = false;

                if (Config.StartDate.HasValue && Config.EndDate.HasValue)
                {
                    output.AppendFormat("Start: {0}, End: {1}, ", Config.StartDate, Config.EndDate);
                }

                var score   = GetScore(list, Config);
                var fitness = CalculateFitness(score);

                output.AppendFormat("{0}: {1}", Name, fitness.Value.ToString("0.##"));
                LogProvider.OptimizerLogger.Info(output);

                var result = new OptimizerResult(p, fitness.Fitness);
                _resultIndex.Add(result, id);
                return(result);
            }
            catch (Exception ex)
            {
                LogProvider.ErrorLogger.Error(ex, $"Id: {id}, Iteration failed.");

                var result = new OptimizerResult(p, ErrorFitness);
                _resultIndex.Add(result, id);
                return(result);
            }
        }
예제 #14
0
        public void HyperbandOptimizer_OptimizeBest()
        {
            var parameters = new IParameterSpec[]
            {
                new MinMaxParameterSpec(min: 80, max: 300, transform: Transform.Linear),
                new MinMaxParameterSpec(min: 0.02, max:  0.2, transform: Transform.Log10),
                new MinMaxParameterSpec(min: 8, max: 15, transform: Transform.Linear),
            };

            var random = new Random(343);

            OptimizerResult Minimize(double[] p, double r)
            {
                var error = random.NextDouble();

                return(new OptimizerResult(p, error));
            }

            var sut = new HyperbandOptimizer(
                parameters,
                maximumBudget: 81,
                eta: 5,
                skipLastIterationOfEachRound: false,
                seed: 34);

            var actual   = sut.OptimizeBest(Minimize);
            var expected = new OptimizerResult(new[] { 278.337940, 0.098931, 13.177449 }, 0.009549);

            AssertOptimizerResult(expected, actual);
        }
예제 #15
0
        public void SmacOptimizer_OptimizeBest_MultipleParameters_Open_Loop()
        {
            var emptyResults = new List <OptimizerResult>();

            OptimizerResult actual = RunOpenLoopOptimizationTest(emptyResults);

            Assert.AreEqual(-0.964878416222769, actual.Error, Delta);
            Assert.AreEqual(actual.ParameterSet.Length, 3);

            Assert.AreEqual(-7.8487638560350819, actual.ParameterSet[0], Delta);
            Assert.AreEqual(6.2840940040927826, actual.ParameterSet[1], Delta);
            Assert.AreEqual(0.036385473812179825, actual.ParameterSet[2], Delta);
        }
        public void BayesianOptimizer_OptimizeBest_MultipleParameters_Open_Loop()
        {
            var emptyResults = new List <OptimizerResult>();

            OptimizerResult actual = RunOpenLoopOptimizationTest(emptyResults);

            Assert.AreEqual(-0.72886252184027234, actual.Error, Delta);
            Assert.AreEqual(actual.ParameterSet.Length, 3);

            Assert.AreEqual(2.0474983901006638, actual.ParameterSet[0], Delta);
            Assert.AreEqual(-9.8598778060916246, actual.ParameterSet[1], Delta);
            Assert.AreEqual(-0.10546011389487475, actual.ParameterSet[2], Delta);
        }
예제 #17
0
        static void AssertOptimizerResult(OptimizerResult expected, OptimizerResult actual)
        {
            Assert.AreEqual(expected.Error, actual.Error, Delta);

            var expectedParameterSet = expected.ParameterSet;
            var actualParameterSet   = actual.ParameterSet;

            Assert.AreEqual(expectedParameterSet.Length, actualParameterSet.Length);

            for (int i = 0; i < expectedParameterSet.Length; i++)
            {
                Assert.AreEqual(expectedParameterSet[i], actualParameterSet[i], Delta);
            }
        }
예제 #18
0
        private void CheckRecursiveExpressions(int recursionCount, OptimizerResult result)
        {
            if (recursionCount == 0)
            {
                return;
            }

            for (var i = 0; i < recursionCount; i++)
            {
                Assert.AreEqual(result.ExpressionTokens.Count, 1);
                Assert.AreEqual(result.VariableTokens.Count, 1);
                CheckRecursiveExpressions(recursionCount - 1, result.VariableTokens.First().Value);
            }
        }
예제 #19
0
        public void BayesianOptimizer_OptimizeNonDeterministicInParallel()
        {
            var parameters = new MinMaxParameterSpec[]
            {
                new MinMaxParameterSpec(0, 1, Transform.Linear, ParameterType.Discrete)
            };
            var sut = new BayesianOptimizer(parameters, iterations: 240, randomStartingPointCount: 5, functionEvaluationsPerIteration: 5,
                                            seed: Seed, maxDegreeOfParallelism: -1, allowMultipleEvaluations: true);
            var results = sut.Optimize(p => MinimizeNonDeterministic(p, Random));
            var actual = new OptimizerResult[] { results.First(), results.Last() }.OrderByDescending(o => o.Error);

            Assert.AreEqual(1, actual.First().Error);
            Assert.AreEqual(1, (int)actual.First().ParameterSet[0]);
        }
예제 #20
0
        private IChromosome ToChromosome(OptimizerResult result, IChromosome source)
        {
            var destination = (Chromosome)source;

            destination.Id = _resultIndex.GetValue(result, (k) => Guid.NewGuid().ToString("N"));

            var list = destination.ToDictionary();

            for (int i = 0; i < Config.Genes.Count(); i++)
            {
                var pair = (KeyValuePair <string, object>)destination.GetGene(i).Value;
                destination.ReplaceGene(i, new Gene(new KeyValuePair <string, object>(pair.Key, result.ParameterSet[i])));
            }

            destination.Fitness = result.Error;
            return(destination);
        }
        public void BayesianOptimizer_OptimizeBest_MultipleParameters_Open_Loop_Using_PreviousResults()
        {
            var previousResults = new List <OptimizerResult>()
            {
                new OptimizerResult(new[] { -6.83357586936726, 6.0834837966056, -0.0766206300242906 }, -0.476143174040315),
                new OptimizerResult(new[] { -7.29391428515963, 6.0834837966056, 1.01057317620636 }, -0.41300737879641),
                new OptimizerResult(new[] { -7.29391428515963, 6.0834837966056, 1.01057317620636 }, -0.41300737879641),
                new OptimizerResult(new[] { -8.05557010604794, -5.14662256238359, 0.0363854738121798 }, -0.397724266113204),
                new OptimizerResult(new[] { -8.06241082868651, 5.88012208038947, -1.5210571566229 }, -0.356975377788698),
                new OptimizerResult(new[] { 4.42408777513732, 0.472018332440413, 1.7076749781648 }, -0.315360461074171),
                new OptimizerResult(new[] { -8.14483470197061, 7.54724840519356, 0.0363854738121798 }, -0.279108605472165),
                new OptimizerResult(new[] { -6.64746686660101, 6.7109944004151, -0.214493549528761 }, -0.266917186594653),
                new OptimizerResult(new[] { 5.34224593795009, -6.45170816986435, -2.1147669628797 }, -0.255769932489526),
                new OptimizerResult(new[] { -7.84876385603508, 6.28409400409278, 3.1447921661403 }, -0.241263236969342),
                new OptimizerResult(new[] { -7.84876385603508, 4.96554990995934, -0.0766206300242906 }, -0.232637166385485),
                new OptimizerResult(new[] { -8.14041409554911, 7.16927772256047, 1.75166608381628 }, -0.220476103560048),
                new OptimizerResult(new[] { -7.84876385603508, 6.0834837966056, -3.60210045874217 }, -0.212970686239402),
                new OptimizerResult(new[] { -7.29391428515963, 5.22505613752876, 1.01057317620636 }, -0.206689239504653),
                new OptimizerResult(new[] { -9.20479206331297, 6.0834837966056, -0.0766206300242906 }, -0.198657722521128),
                new OptimizerResult(new[] { -8.25145286426481, 5.27274844947865, -1.82163462593296 }, -0.17367847378187),
                new OptimizerResult(new[] { -7.84876385603508, 6.0834837966056, 5.3824106023565 }, -0.153564625328103),
                new OptimizerResult(new[] { -1.37364300497511, -1.35665034472786, -0.585322245296707 }, -0.131453543138338),
                new OptimizerResult(new[] { -7.84876385603508, 7.74187722138216, -0.0766206300242906 }, -0.103906821017427),
                new OptimizerResult(new[] { 9.20868899636375, -9.38389458664874, 1.51842798642741 }, -0.0850657757130275),
                new OptimizerResult(new[] { -7.72406242681856, 5.70825177044992, 9.95585092341334 }, -0.0759553721161318),
                new OptimizerResult(new[] { 1.65093947744506, -4.37866264692445, -4.29402069854272 }, -0.0616761163702651),
                new OptimizerResult(new[] { -9.37414173938993, 6.28409400409278, 0.0363854738121798 }, -0.0488375857853505),
                new OptimizerResult(new[] { 3.38691201684387, 5.42095644186295, -5.71318443664964 }, -0.0235423806080941),
                new OptimizerResult(new[] { -6.48224856540665, -7.13935053774125, 7.05507751417117 }, -0.0160884883078408),
                new OptimizerResult(new[] { -9.68539061941457, 7.96346846873102, -0.990608674935348 }, -0.0141441279734299),
                new OptimizerResult(new[] { -9.41382774124566, 5.12580713030221, 0.630654976996897 }, -0.00269773409680873),
                new OptimizerResult(new[] { 6.7694738305963, 1.56629731485913, -2.12145430600338 }, 0.000673595210828553),
                new OptimizerResult(new[] { -0.0282478006688169, 2.87566112022645, -4.84997700660023 }, 0.00465834522866944),
                new OptimizerResult(new[] { 3.50054986472267, 8.01269467827524, 7.36471213277649 }, 0.00663762309484885),
                new OptimizerResult(new[] { 3.05129390817662, -6.16640157819092, 7.49125691013935 }, 0.0105475373675896),
            };

            OptimizerResult actual = RunOpenLoopOptimizationTest(previousResults);

            Assert.AreEqual(-0.8720142225794828, actual.Error, Delta);
            Assert.AreEqual(actual.ParameterSet.Length, 3);

            Assert.AreEqual(-7.8218983383019918, actual.ParameterSet[0], Delta);
            Assert.AreEqual(-6.0818001516544262, actual.ParameterSet[1], Delta);
            Assert.AreEqual(-0.12301634537196549, actual.ParameterSet[2], Delta);
        }
예제 #22
0
        protected OptimizerResult Minimize(double[] p, Chromosome configChromosome)
        {
            var id = Guid.NewGuid().ToString("N");

            try
            {
                StringBuilder output = new StringBuilder();
                var           list   = configChromosome.ToDictionary();

                list.Add("Id", id);
                output.Append("Id: " + id + ", ");

                for (int i = 0; i < Config.Genes.Count(); i++)
                {
                    var key       = Config.Genes.ElementAt(i).Key;
                    var precision = Config.Genes.ElementAt(i).Precision ?? 0;
                    var value     = Math.Round(p[i], precision);
                    list[key] = value;

                    output.Append(key + ": " + value.ToString() + ", ");
                }

                if (Config.StartDate.HasValue && Config.EndDate.HasValue)
                {
                    output.AppendFormat("Start: {0}, End: {1}, ", Config.StartDate, Config.EndDate);
                }

                var score   = GetScore(list, Config);
                var fitness = CalculateFitness(score);

                output.AppendFormat("{0}: {1}", Name, fitness.Value.ToString("0.##"));
                Program.Logger.Info(output);

                var result = new OptimizerResult(p, fitness.Fitness);
                _resultIndex.Add(result, id);
                return(result);
            }
            catch (Exception)
            {
                Program.Logger.Error($"Id: {id}, Iteration failed.");

                var result = new OptimizerResult(p, ErrorFitness);
                _resultIndex.Add(result, id);
                return(result);
            }
        }
예제 #23
0
        public void RandomSearchOptimizer_Optimize()
        {
            var parameters = new double[][] { new double[] { 10.0, 37.5 } };
            var sut        = new RandomSearchOptimizer(parameters, 2);
            var actual     = sut.Optimize(Minimize);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 28.372927812567415 }, 3690.8111981874217),
                new OptimizerResult(new double[] { 13.874950705270725 }, 23438.215764163542)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
예제 #24
0
        public void GridSearchOptimizer_Optimize()
        {
            var parameters = new double[][] { new double[] { 10.0, 37.5 } };
            var sut        = new GridSearchOptimizer(parameters);
            var actual     = sut.Optimize(Minimize);

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.5 }, 111.20889999999987),
                new OptimizerResult(new double[] { 10 }, 31638.9579)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
        public void SequentialModelBasedOptimizer_Optimize()
        {
            var parameters = new double[][] { new double[] { 0.0, 100.0 } };
            var sut        = new SequentialModelBasedOptimizer(parameters, 20, 5);
            var results    = sut.Optimize(Minimize2);
            var actual     = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.712345696964071 }, 109.34384021647092),
                new OptimizerResult(new double[] { 66.810646591154239 }, 34867.634010511123)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
예제 #26
0
        public void BayesianOptimizer_Optimize()
        {
            var parameters = new double[][] { new double[] { 0.0, 100.0 } };
            var sut        = new BayesianOptimizer(parameters, 120, 5, 1);
            var results    = sut.Optimize(Minimize2);
            var actual     = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.710969353891429 }, 109.34400835405613),
                new OptimizerResult(new double[] { 99.646240426062718 }, 157577.44222424511)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
예제 #27
0
        public void GlobalizedBoundedNelderMeadOptimizer_Optimize()
        {
            var parameters = new double[][] { new double[] { 0.0, 100.0 } };
            var sut        = new GlobalizedBoundedNelderMeadOptimizer(parameters, 5, 1e-5, 10);
            var results    = sut.Optimize(Minimize2);
            var actual     = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.71314535727786 }, 109.34381396310141),
                new OptimizerResult(new double[] { 37.7131485180996 }, 109.34381396350526)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
        public void ParticleSwarmOptimizer_Optimize()
        {
            var parameters = new double[][] { new double[] { 0.0, 100.0 } };
            var sut        = new ParticleSwarmOptimizer(parameters, 100);
            var results    = sut.Optimize(Minimize2);
            var actual     = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.635959742891046 }, 109.58839344747747),
                new OptimizerResult(new double[] { 38.891904333594624 }, 166.38611320480345)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }
예제 #29
0
        public void BayesianOptimizer_Optimize()
        {
            var parameters = new ParameterBounds[]
            {
                new ParameterBounds(0.0, 100.0, Transform.Linear)
            };
            var sut     = new BayesianOptimizer(parameters, 120, 5, 1);
            var results = sut.Optimize(Minimize2);
            var actual  = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 42.323589763754789 }, 981.97873691815118),
                new OptimizerResult(new double[] { 99.110398813667885 }, 154864.41962974239)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, m_delta);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), m_delta);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, m_delta);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), m_delta);
        }
        public void ParticleSwarmOptimizer_Optimize()
        {
            var parameters = new ParameterBounds[]
            {
                new ParameterBounds(0.0, 100.0, Transform.Linear)
            };
            var sut     = new ParticleSwarmOptimizer(parameters, 100);
            var results = sut.Optimize(Minimize2);
            var actual  = new OptimizerResult[] { results.First(), results.Last() };

            var expected = new OptimizerResult[]
            {
                new OptimizerResult(new double[] { 37.660092259635064 }, 109.45936368750877),
                new OptimizerResult(new double[] { 39.038272502859328 }, 181.43166846962754)
            };

            Assert.AreEqual(expected.First().Error, actual.First().Error, 0.0001);
            Assert.AreEqual(expected.First().ParameterSet.First(), actual.First().ParameterSet.First(), 0.0001);

            Assert.AreEqual(expected.Last().Error, actual.Last().Error, 0.0001);
            Assert.AreEqual(expected.Last().ParameterSet.First(), actual.Last().ParameterSet.First(), 0.0001);
        }