Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SimulatedAnnealingRastriginSampleTest.cs @ 16654

Last change on this file since 16654 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 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  [TestClass]
34  public class SimulatedAnnealingRastriginSampleTest {
35    private const string SampleFileName = "SA_Rastrigin";
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateSimulatedAnnealingRastriginSampleTest() {
41      var sa = CreateSimulatedAnnealingRastriginSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
43      XmlGenerator.Serialize(sa, path);
44    }
45    [TestMethod]
46    [TestCategory("Samples.Execute")]
47    [TestProperty("Time", "medium")]
48    public void RunSimulatedAnnealingRastriginSampleTest() {
49      var sa = CreateSimulatedAnnealingRastriginSample();
50      sa.SetSeedRandomly.Value = false;
51      SamplesUtils.RunAlgorithm(sa);
52      Assert.AreEqual(0.00014039606034543795, SamplesUtils.GetDoubleResult(sa, "BestQuality"));
53      Assert.AreEqual(5000, SamplesUtils.GetIntResult(sa, "EvaluatedMoves"));
54    }
55    private SimulatedAnnealing CreateSimulatedAnnealingRastriginSample() {
56      SimulatedAnnealing sa = new SimulatedAnnealing();
57      #region Problem Configuration
58      var problem = new SingleObjectiveTestFunctionProblem();
59      problem.BestKnownQuality = 0.0;
60      problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 0, 0 });
61      problem.Bounds = new DoubleMatrix(new double[,] { { -5.12, 5.12 } });
62      problem.TestFunction= new Rastrigin();
63      problem.ProblemSize = 2;
64      problem.Encoding.SolutionCreator = new UniformRandomRealVectorCreator();
65      #endregion
66      #region Algorithm Configuration
67      sa.Name = "Simulated Annealing - Rastrigin";
68      sa.Description = "A simulated annealing algorithm that solves the 2-dimensional Rastrigin test function";
69      sa.Problem = problem;
70      var annealingOperator = sa.AnnealingOperatorParameter.ValidValues
71        .OfType<ExponentialDiscreteDoubleValueModifier>()
72        .Single();
73      annealingOperator.StartIndexParameter.Value = new IntValue(0);
74      sa.AnnealingOperator = annealingOperator;
75
76      sa.EndTemperature.Value = 1E-6;
77      sa.InnerIterations.Value = 50;
78      sa.MaximumIterations.Value = 100;
79      var moveEvaluator = sa.MoveEvaluatorParameter.ValidValues
80        .OfType<AdditiveMoveEvaluator>()
81        .Single();
82      sa.MoveEvaluator = moveEvaluator;
83
84      var moveGenerator = sa.MoveGeneratorParameter.ValidValues
85        .OfType<StochasticNormalMultiMoveGenerator>()
86        .Single();
87      moveGenerator.SigmaParameter.Value = new DoubleValue(1);
88      sa.MoveGenerator = moveGenerator;
89
90      sa.MoveMaker = sa.MoveMakerParameter.ValidValues
91        .OfType<AdditiveMoveMaker>()
92        .Single();
93
94      sa.Seed.Value = 0;
95      sa.SetSeedRandomly.Value = true;
96      sa.StartTemperature.Value = 1;
97      #endregion
98      sa.Engine = new ParallelEngine.ParallelEngine();
99      return sa;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.