Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.Tests/HeuristicLab-3.3/Samples/EsGriewankSampleTest.cs @ 14877

Last change on this file since 14877 was 13469, checked in by mkommend, 9 years ago

#2521: Refactored problem base classes and adapted scheduling encoding, scheduling problem and unit tests.

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Algorithms.EvolutionStrategy;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using HeuristicLab.Persistence.Default.Xml;
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    [TestMethod]
36    [TestCategory("Samples.Create")]
37    [TestProperty("Time", "medium")]
38    public void CreateEsGriewankSampleTest() {
39      var es = CreateEsGriewankSample();
40      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
41      XmlGenerator.Serialize(es, path);
42    }
43
44    [TestMethod]
45    [TestCategory("Samples.Execute")]
46    [TestProperty("Time", "long")]
47    public void RunEsGriewankSampleTest() {
48      var es = CreateEsGriewankSample();
49      es.SetSeedRandomly.Value = false;
50      SamplesUtils.RunAlgorithm(es);
51      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "BestQuality"));
52      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentAverageQuality"));
53      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentWorstQuality"));
54      Assert.AreEqual(100020, SamplesUtils.GetIntResult(es, "EvaluatedSolutions"));
55    }
56
57    private EvolutionStrategy CreateEsGriewankSample() {
58      EvolutionStrategy es = new EvolutionStrategy();
59
60      #region Problem Configuration
61
62      SingleObjectiveTestFunctionProblem problem = new SingleObjectiveTestFunctionProblem();
63
64      problem.ProblemSize = 10;
65      problem.TestFunction = new Griewank();
66      problem.Encoding.SolutionCreator = new UniformRandomRealVectorCreator();
67      problem.Bounds = new DoubleMatrix(new double[,] { { -600, 600 } });
68      problem.BestKnownQuality = 0;
69      problem.BestKnownSolutionParameter.Value = new RealVector(10);
70      problem.Name = "Single Objective Test Function";
71      problem.Description = "Test function with real valued inputs and a single objective.";
72
73      #endregion
74
75      #region Algorithm Configuration
76
77      es.Name = "Evolution Strategy - Griewank";
78      es.Description = "An evolution strategy which solves the 10-dimensional Griewank test function";
79      es.Problem = problem;
80      SamplesUtils.ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
81        StdDevStrategyVectorCreator, StdDevStrategyVectorCrossover, StdDevStrategyVectorManipulator>(
82          es, 20, 500, 2, 200, false);
83
84      StdDevStrategyVectorCreator strategyCreator = (StdDevStrategyVectorCreator)es.StrategyParameterCreator;
85      strategyCreator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1, 20 } });
86
87      StdDevStrategyVectorManipulator strategyManipulator =
88        (StdDevStrategyVectorManipulator)es.StrategyParameterManipulator;
89      strategyManipulator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1E-12, 30 } });
90      strategyManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.22360679774997896);
91      strategyManipulator.LearningRateParameter.Value = new DoubleValue(0.39763536438352531);
92
93      #endregion
94
95      return es;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.