Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17356 was 17356, checked in by abeham, 4 years ago

#2521: fixed some problems in Samples.Create unit tests and updated samples

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