Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/EsGriewankSampleTest.cs @ 17226

Last change on this file since 17226 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.IO;
23using HEAL.Attic;
24using HeuristicLab.Algorithms.EvolutionStrategy;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Problems.TestFunctions;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab.Tests {
31  [TestClass]
32  public class EsGriewankSampleTest {
33    private const string SampleFileName = "ES_Griewank";
34
35    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateEsGriewankSampleTest() {
41      var es = CreateEsGriewankSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
43      serializer.Serialize(es, path);
44    }
45
46    [TestMethod]
47    [TestCategory("Samples.Execute")]
48    [TestProperty("Time", "long")]
49    public void RunEsGriewankSampleTest() {
50      var es = CreateEsGriewankSample();
51      es.SetSeedRandomly.Value = false;
52      SamplesUtils.RunAlgorithm(es);
53      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "BestQuality"));
54      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentAverageQuality"));
55      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentWorstQuality"));
56      Assert.AreEqual(100020, SamplesUtils.GetIntResult(es, "EvaluatedSolutions"));
57    }
58
59    private EvolutionStrategy CreateEsGriewankSample() {
60      EvolutionStrategy es = new EvolutionStrategy();
61
62      #region Problem Configuration
63
64      SingleObjectiveTestFunctionProblem problem = new SingleObjectiveTestFunctionProblem();
65
66      problem.ProblemSize = 10;
67      problem.TestFunction = new Griewank();
68      problem.Encoding.SolutionCreator = new UniformRandomRealVectorCreator();
69      problem.Bounds = new DoubleMatrix(new double[,] { { -600, 600 } });
70      problem.BestKnownQuality = 0;
71      problem.BestKnownSolutionParameter.Value = new RealVector(10);
72      problem.Name = "Single Objective Test Function";
73      problem.Description = "Test function with real valued inputs and a single objective.";
74
75      #endregion
76
77      #region Algorithm Configuration
78
79      es.Name = "Evolution Strategy - Griewank";
80      es.Description = "An evolution strategy which solves the 10-dimensional Griewank test function";
81      es.Problem = problem;
82      SamplesUtils.ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
83        StdDevStrategyVectorCreator, StdDevStrategyVectorCrossover, StdDevStrategyVectorManipulator>(
84          es, 20, 500, 2, 200, false);
85
86      StdDevStrategyVectorCreator strategyCreator = (StdDevStrategyVectorCreator)es.StrategyParameterCreator;
87      strategyCreator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1, 20 } });
88
89      StdDevStrategyVectorManipulator strategyManipulator =
90        (StdDevStrategyVectorManipulator)es.StrategyParameterManipulator;
91      strategyManipulator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1E-12, 30 } });
92      strategyManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.22360679774997896);
93      strategyManipulator.LearningRateParameter.Value = new DoubleValue(0.39763536438352531);
94
95      #endregion
96
97      return es;
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.