Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SimulatedAnnealingRastriginSampleTest.cs @ 17709

Last change on this file since 17709 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Value = 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.EvaluatorParameter.Value = new RastriginEvaluator();
63      problem.Maximization.Value = false;
64      problem.ProblemSize.Value = 2;
65      problem.SolutionCreatorParameter.Value = 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 moveEvaluator = sa.MoveEvaluatorParameter.ValidValues
81        .OfType<RastriginAdditiveMoveEvaluator>()
82        .Single();
83      moveEvaluator.A.Value = 10;
84      sa.MoveEvaluator = moveEvaluator;
85
86      var moveGenerator = sa.MoveGeneratorParameter.ValidValues
87        .OfType<StochasticNormalMultiMoveGenerator>()
88        .Single();
89      moveGenerator.SigmaParameter.Value = new DoubleValue(1);
90      sa.MoveGenerator = moveGenerator;
91
92      sa.MoveMaker = sa.MoveMakerParameter.ValidValues
93        .OfType<AdditiveMoveMaker>()
94        .Single();
95
96      sa.Seed.Value = 0;
97      sa.SetSeedRandomly.Value = true;
98      sa.StartTemperature.Value = 1;
99      #endregion
100      sa.Engine = new ParallelEngine.ParallelEngine();
101      return sa;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.