Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15214 was 15214, checked in by abeham, 7 years ago

#2797:

  • Fixed adaptive random topology updater
  • Adapted default values of the best attraction parameters
  • Changed code of the new topology initializer
  • Fixed the parameters of the SPSO particle updaters (c parameter is actually (personal|neighbor)bestattraction), reordered the method signature and provided defaults
  • Removed the max beyond parameter again
  • Updated the sample and updated the unit test
    • In the sample no inertia updating is used, but the topology initializers / updaters of SPSO are used
File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Persistence.Default.Xml;
29using HeuristicLab.Problems.TestFunctions;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
32namespace HeuristicLab.Tests {
33  [TestClass]
34  public class PsoSchwefelSampleTest {
35    private const string SampleFileName = "PSO_Schwefel";
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreatePsoSchwefelSampleTest() {
41      var pso = CreatePsoSchwefelSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
43      XmlGenerator.Serialize(pso, path);
44    }
45    [TestMethod]
46    [TestCategory("Samples.Execute")]
47    [TestProperty("Time", "medium")]
48    public void RunPsoSchwefelSampleTest() {
49      var pso = CreatePsoSchwefelSample();
50      pso.SetSeedRandomly.Value = false;
51      SamplesUtils.RunAlgorithm(pso);
52      if (Environment.Is64BitProcess) {
53        Assert.AreEqual(-1.4779288903810084E-12, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
54        Assert.AreEqual(189.28837949705971, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
55        Assert.AreEqual(1195.4166822158872, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
56        Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
57      } else {
58        Assert.AreEqual(-1.4779288903810084E-12, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
59        Assert.AreEqual(189.28837949705971, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
60        Assert.AreEqual(1195.4166822158873, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
61        Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
62      }
63    }
64
65    private ParticleSwarmOptimization CreatePsoSchwefelSample() {
66      ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
67      #region Problem Configuration
68      var problem = new SingleObjectiveTestFunctionProblem();
69      problem.BestKnownQuality.Value = 0.0;
70      problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 420.968746, 420.968746 });
71      problem.Bounds = new DoubleMatrix(new double[,] { { -500, 500 } });
72      problem.EvaluatorParameter.Value = new SchwefelEvaluator();
73      problem.Maximization.Value = false;
74      problem.ProblemSize.Value = 2;
75      problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
76      #endregion
77      #region Algorithm Configuration
78      pso.Name = "Particle Swarm Optimization - Schwefel";
79      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)";
80      pso.Problem = problem;
81      pso.Inertia.Value = 0.721;
82      pso.MaxIterations.Value = 200;
83      pso.NeighborBestAttraction.Value = 1.193;
84      pso.PersonalBestAttraction.Value = 1.193;
85      pso.SwarmSize.Value = 40;
86           
87      pso.TopologyInitializer = pso.TopologyInitializerParameter.ValidValues.OfType<SPSORandomTopologyInitializer>().First();
88      pso.TopologyUpdater = pso.TopologyUpdaterParameter.ValidValues.OfType<SPSOAdaptiveRandomTopologyUpdater>().First();
89      pso.Seed.Value = 0;
90      pso.SetSeedRandomly.Value = true;
91      #endregion
92      pso.Engine = new ParallelEngine.ParallelEngine();
93      return pso;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.