Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SimulatedAnnealingRastriginSampleTest.cs @ 17983

Last change on this file since 17983 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.3 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.Value = 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.EvaluatorParameter.Value = new RastriginEvaluator();
64      problem.Maximization.Value = false;
65      problem.ProblemSize.Value = 2;
66      problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
67      #endregion
68      #region Algorithm Configuration
69      sa.Name = "Simulated Annealing - Rastrigin";
70      sa.Description = "A simulated annealing algorithm that solves the 2-dimensional Rastrigin test function";
71      sa.Problem = problem;
72      var annealingOperator = sa.AnnealingOperatorParameter.ValidValues
73        .OfType<ExponentialDiscreteDoubleValueModifier>()
74        .Single();
75      annealingOperator.StartIndexParameter.Value = new IntValue(0);
76      sa.AnnealingOperator = annealingOperator;
77
78      sa.EndTemperature.Value = 1E-6;
79      sa.InnerIterations.Value = 50;
80      sa.MaximumIterations.Value = 100;
81      var moveEvaluator = sa.MoveEvaluatorParameter.ValidValues
82        .OfType<RastriginAdditiveMoveEvaluator>()
83        .Single();
84      moveEvaluator.A.Value = 10;
85      sa.MoveEvaluator = moveEvaluator;
86
87      var moveGenerator = sa.MoveGeneratorParameter.ValidValues
88        .OfType<StochasticNormalMultiMoveGenerator>()
89        .Single();
90      moveGenerator.SigmaParameter.Value = new DoubleValue(1);
91      sa.MoveGenerator = moveGenerator;
92
93      sa.MoveMaker = sa.MoveMakerParameter.ValidValues
94        .OfType<AdditiveMoveMaker>()
95        .Single();
96
97      sa.Seed.Value = 0;
98      sa.SetSeedRandomly.Value = true;
99      sa.StartTemperature.Value = 1;
100      #endregion
101      sa.Engine = new ParallelEngine.ParallelEngine();
102      return sa;
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.