Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SimulatedAnnealingRastriginSampleTest.cs @ 11655

Last change on this file since 11655 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.5 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 System.Linq;
24using HeuristicLab.Algorithms.SimulatedAnnealing;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Persistence.Default.Xml;
29using HeuristicLab.Problems.TestFunctions;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
32namespace HeuristicLab.Tests {
33  /// <summary>
34  /// Summary description for SimulatedAnnealingRastriginSampleTest
35  /// </summary>
36  [TestClass]
37  public class SimulatedAnnealingRastriginSampleTest {
38    private const string samplesDirectory = SamplesUtils.Directory;
39    [ClassInitialize]
40    public static void MyClassInitialize(TestContext testContext) {
41      if (!Directory.Exists(samplesDirectory))
42        Directory.CreateDirectory(samplesDirectory);
43    }
44
45    [TestMethod]
46    [TestCategory("Samples.Create")]
47    [TestProperty("Time", "medium")]
48    public void CreateSimulatedAnnealingRastriginSampleTest() {
49      var sa = CreateSimulatedAnnealingRastriginSample();
50      XmlGenerator.Serialize(sa, @"Samples\SA_Rastrigin.hl");
51    }
52    [TestMethod]
53    [TestCategory("Samples.Execute")]
54    [TestProperty("Time", "medium")]
55    public void RunSimulatedAnnealingRastriginSampleTest() {
56      var sa = CreateSimulatedAnnealingRastriginSample();
57      sa.SetSeedRandomly.Value = false;
58      SamplesUtils.RunAlgorithm(sa);
59      Assert.AreEqual(0.00014039606034543795, SamplesUtils.GetDoubleResult(sa, "BestQuality"));
60      Assert.AreEqual(5000, SamplesUtils.GetIntResult(sa, "EvaluatedMoves"));
61    }
62    private SimulatedAnnealing CreateSimulatedAnnealingRastriginSample() {
63      SimulatedAnnealing sa = new SimulatedAnnealing();
64      #region Problem Configuration
65      var problem = new SingleObjectiveTestFunctionProblem();
66      problem.BestKnownQuality.Value = 0.0;
67      problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 0, 0 });
68      problem.Bounds = new DoubleMatrix(new double[,] { { -5.12, 5.12 } });
69      problem.EvaluatorParameter.Value = new RastriginEvaluator();
70      problem.Maximization.Value = false;
71      problem.ProblemSize.Value = 2;
72      problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
73      #endregion
74      #region Algorithm Configuration
75      sa.Name = "Simulated Annealing - Rastrigin";
76      sa.Description = "A simulated annealing algorithm that solves the 2-dimensional Rastrigin test function";
77      sa.Problem = problem;
78      var annealingOperator = sa.AnnealingOperatorParameter.ValidValues
79        .OfType<ExponentialDiscreteDoubleValueModifier>()
80        .Single();
81      annealingOperator.StartIndexParameter.Value = new IntValue(0);
82      sa.AnnealingOperator = annealingOperator;
83
84      sa.EndTemperature.Value = 1E-6;
85      sa.InnerIterations.Value = 50;
86      sa.MaximumIterations.Value = 100;
87      var moveEvaluator = sa.MoveEvaluatorParameter.ValidValues
88        .OfType<RastriginAdditiveMoveEvaluator>()
89        .Single();
90      moveEvaluator.A.Value = 10;
91      sa.MoveEvaluator = moveEvaluator;
92
93      var moveGenerator = sa.MoveGeneratorParameter.ValidValues
94        .OfType<StochasticNormalMultiMoveGenerator>()
95        .Single();
96      moveGenerator.SigmaParameter.Value = new DoubleValue(1);
97      sa.MoveGenerator = moveGenerator;
98
99      sa.MoveMaker = sa.MoveMakerParameter.ValidValues
100        .OfType<AdditiveMoveMaker>()
101        .Single();
102
103      sa.Seed.Value = 0;
104      sa.SetSeedRandomly.Value = true;
105      sa.StartTemperature.Value = 1;
106      #endregion
107      sa.Engine = new ParallelEngine.ParallelEngine();
108      return sa;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.