Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/PsoSchwefelSampleTest.cs @ 13229

Last change on this file since 13229 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 5.4 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;
23using System.IO;
24using System.Linq;
25using HeuristicLab.Algorithms.ParticleSwarmOptimization;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Persistence.Default.Xml;
30using HeuristicLab.Problems.TestFunctions;
31using Microsoft.VisualStudio.TestTools.UnitTesting;
32
33namespace HeuristicLab.Tests {
34  [TestClass]
35  public class PsoSchwefelSampleTest {
36    private const string SampleFileName = "PSO_Schwefel";
37
38    [TestMethod]
39    [TestCategory("Samples.Create")]
40    [TestProperty("Time", "medium")]
41    public void CreatePsoSchwefelSampleTest() {
42      var pso = CreatePsoSchwefelSample();
43      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
44      XmlGenerator.Serialize(pso, path);
45    }
46    [TestMethod]
47    [TestCategory("Samples.Execute")]
48    [TestProperty("Time", "medium")]
49    public void RunPsoSchwefelSampleTest() {
50      var pso = CreatePsoSchwefelSample();
51      pso.SetSeedRandomly.Value = false;
52      SamplesUtils.RunAlgorithm(pso);
53      if (!Environment.Is64BitProcess) {
54        Assert.AreEqual(118.44027985932837, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
55        Assert.AreEqual(140.71570105946438, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
56        Assert.AreEqual(220.956806502853, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
57        Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
58      } else {
59        Assert.AreEqual(118.43958282879345, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
60        Assert.AreEqual(139.43946864779372, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
61        Assert.AreEqual(217.14654589055152, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
62        Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
63      }
64    }
65
66    private ParticleSwarmOptimization CreatePsoSchwefelSample() {
67      ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
68      #region Problem Configuration
69      var problem = new SingleObjectiveTestFunctionProblem();
70      problem.BestKnownQuality.Value = 0.0;
71      problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 420.968746, 420.968746 });
72      problem.Bounds = new DoubleMatrix(new double[,] { { -500, 500 } });
73      problem.EvaluatorParameter.Value = new SchwefelEvaluator();
74      problem.Maximization.Value = false;
75      problem.ProblemSize.Value = 2;
76      problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
77      #endregion
78      #region Algorithm Configuration
79      pso.Name = "Particle Swarm Optimization - Schwefel";
80      pso.Description = "A particle swarm optimization algorithm which solves the 2-dimensional Schwefel test function (based on the description in Pedersen, M.E.H. (2010). PhD thesis. University of Southampton)";
81      pso.Problem = problem;
82      pso.Inertia.Value = 10;
83      pso.MaxIterations.Value = 1000;
84      pso.NeighborBestAttraction.Value = 0.5;
85      pso.PersonalBestAttraction.Value = -0.01;
86      pso.SwarmSize.Value = 50;
87
88      var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
89        .OfType<ExponentialDiscreteDoubleValueModifier>()
90        .Single();
91      inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
92      inertiaUpdater.EndValueParameter.Value = new DoubleValue(1);
93      pso.InertiaUpdater = inertiaUpdater;
94
95      pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
96        .OfType<RealVectorParticleCreator>()
97        .Single();
98      var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
99        .OfType<RealVectorSwarmUpdater>()
100        .Single();
101      swarmUpdater.VelocityBoundsIndexParameter.ActualName = "Iterations";
102      swarmUpdater.VelocityBoundsParameter.Value = new DoubleMatrix(new double[,] { { -10, 10 } });
103      swarmUpdater.VelocityBoundsStartValueParameter.Value = new DoubleValue(10.0);
104      swarmUpdater.VelocityBoundsEndValueParameter.Value = new DoubleValue(1.0);
105      swarmUpdater.VelocityBoundsScalingOperatorParameter.Value = swarmUpdater.VelocityBoundsScalingOperatorParameter.ValidValues
106        .OfType<ExponentialDiscreteDoubleValueModifier>()
107        .Single();
108
109      pso.TopologyInitializer = null;
110      pso.TopologyUpdater = null;
111      pso.SwarmUpdater = swarmUpdater;
112      pso.Seed.Value = 0;
113      pso.SetSeedRandomly.Value = true;
114      #endregion
115      pso.Engine = new ParallelEngine.ParallelEngine();
116      return pso;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.