Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/EsGriewankSampleTest.cs @ 11465

Last change on this file since 11465 was 11450, checked in by bburlacu, 10 years ago

#2211: Separated samples class into separate test classes. Added scripts unit tests (grid search classification/regression).

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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  /// <summary>
32  /// Summary description for EsGriewankSampleTest
33  /// </summary>
34  [TestClass]
35  public class EsGriewankSampleTest {
36    private const string samplesDirectory = SamplesUtils.Directory;
37
38    [ClassInitialize]
39    public static void MyClassInitialize(TestContext testContext) {
40      if (!Directory.Exists(samplesDirectory))
41        Directory.CreateDirectory(samplesDirectory);
42    }
43
44    [TestMethod]
45    [TestCategory("Samples.Create")]
46    [TestProperty("Time", "medium")]
47    public void CreateEsGriewankSampleTest() {
48      var es = CreateEsGriewankSample();
49      XmlGenerator.Serialize(es, @"Samples\ES_Griewank.hl");
50    }
51
52    [TestMethod]
53    [TestCategory("Samples.Execute")]
54    [TestProperty("Time", "long")]
55    public void RunEsGriewankSampleTest() {
56      var es = CreateEsGriewankSample();
57      es.SetSeedRandomly.Value = false;
58      SamplesUtils.RunAlgorithm(es);
59      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "BestQuality"));
60      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentAverageQuality"));
61      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentWorstQuality"));
62      Assert.AreEqual(100020, SamplesUtils.GetIntResult(es, "EvaluatedSolutions"));
63    }
64
65    private EvolutionStrategy CreateEsGriewankSample() {
66      EvolutionStrategy es = new EvolutionStrategy();
67
68      #region Problem Configuration
69
70      SingleObjectiveTestFunctionProblem problem = new SingleObjectiveTestFunctionProblem();
71
72      problem.ProblemSize.Value = 10;
73      problem.EvaluatorParameter.Value = new GriewankEvaluator();
74      problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
75      problem.Maximization.Value = false;
76      problem.Bounds = new DoubleMatrix(new double[,] { { -600, 600 } });
77      problem.BestKnownQuality.Value = 0;
78      problem.BestKnownSolutionParameter.Value = new RealVector(10);
79      problem.Name = "Single Objective Test Function";
80      problem.Description = "Test function with real valued inputs and a single objective.";
81
82      #endregion
83
84      #region Algorithm Configuration
85
86      es.Name = "Evolution Strategy - Griewank";
87      es.Description = "An evolution strategy which solves the 10-dimensional Griewank test function";
88      es.Problem = problem;
89      SamplesUtils.ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
90        StdDevStrategyVectorCreator, StdDevStrategyVectorCrossover, StdDevStrategyVectorManipulator>(
91          es, 20, 500, 2, 200, false);
92
93      StdDevStrategyVectorCreator strategyCreator = (StdDevStrategyVectorCreator)es.StrategyParameterCreator;
94      strategyCreator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1, 20 } });
95
96      StdDevStrategyVectorManipulator strategyManipulator =
97        (StdDevStrategyVectorManipulator)es.StrategyParameterManipulator;
98      strategyManipulator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1E-12, 30 } });
99      strategyManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.22360679774997896);
100      strategyManipulator.LearningRateParameter.Value = new DoubleValue(0.39763536438352531);
101
102      #endregion
103
104      return es;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.