[11450] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11450] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.IO;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Algorithms.ParticleSwarmOptimization;
|
---|
| 26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 27 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 28 | using HeuristicLab.Problems.TestFunctions;
|
---|
| 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Tests {
|
---|
| 32 | [TestClass]
|
---|
[15228] | 33 | public class PsoRastriginSampleTest {
|
---|
[15223] | 34 | private const string SampleFileName = "PSO_Rastrigin";
|
---|
[11450] | 35 |
|
---|
| 36 | [TestMethod]
|
---|
| 37 | [TestCategory("Samples.Create")]
|
---|
| 38 | [TestProperty("Time", "medium")]
|
---|
[15223] | 39 | public void CreatePsoRastriginSampleTest() {
|
---|
| 40 | var pso = CreatePsoRastriginSample();
|
---|
[11514] | 41 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
| 42 | XmlGenerator.Serialize(pso, path);
|
---|
[11450] | 43 | }
|
---|
| 44 | [TestMethod]
|
---|
| 45 | [TestCategory("Samples.Execute")]
|
---|
| 46 | [TestProperty("Time", "medium")]
|
---|
[15223] | 47 | public void RunPsoRastriginSampleTest() {
|
---|
| 48 | var pso = CreatePsoRastriginSample();
|
---|
[11450] | 49 | pso.SetSeedRandomly.Value = false;
|
---|
| 50 | SamplesUtils.RunAlgorithm(pso);
|
---|
[15092] | 51 | if (Environment.Is64BitProcess) {
|
---|
[15223] | 52 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
[15228] | 53 | Assert.AreEqual(3.9649516110677525, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"), 1e-08);
|
---|
| 54 | Assert.AreEqual(25.566430359483757, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"), 1e-08);
|
---|
[15096] | 55 | Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
[11450] | 56 | } else {
|
---|
[15223] | 57 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
[15228] | 58 | Assert.AreEqual(3.3957460831564048, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"), 1e-08);
|
---|
| 59 | Assert.AreEqual(34.412788077766145, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"), 1e-08);
|
---|
[15096] | 60 | Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
[11450] | 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[15223] | 64 | private ParticleSwarmOptimization CreatePsoRastriginSample() {
|
---|
[11450] | 65 | ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
---|
| 66 | #region Problem Configuration
|
---|
| 67 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
[15223] | 68 | var provider = new SOTFInstanceProvider();
|
---|
| 69 | problem.Load(provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name == "Rastrigin Function")));
|
---|
[11450] | 70 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
| 71 | #endregion
|
---|
| 72 | #region Algorithm Configuration
|
---|
[15223] | 73 | pso.Name = "Particle Swarm Optimization - Rastrigin";
|
---|
| 74 | pso.Description = "A particle swarm optimization algorithm which solves the 2-dimensional Rastrigin test function.";
|
---|
[11450] | 75 | pso.Problem = problem;
|
---|
[15214] | 76 | pso.Inertia.Value = 0.721;
|
---|
[15096] | 77 | pso.MaxIterations.Value = 200;
|
---|
[15214] | 78 | pso.NeighborBestAttraction.Value = 1.193;
|
---|
| 79 | pso.PersonalBestAttraction.Value = 1.193;
|
---|
[15096] | 80 | pso.SwarmSize.Value = 40;
|
---|
[15214] | 81 |
|
---|
| 82 | pso.TopologyInitializer = pso.TopologyInitializerParameter.ValidValues.OfType<SPSORandomTopologyInitializer>().First();
|
---|
| 83 | pso.TopologyUpdater = pso.TopologyUpdaterParameter.ValidValues.OfType<SPSOAdaptiveRandomTopologyUpdater>().First();
|
---|
[11450] | 84 | pso.Seed.Value = 0;
|
---|
| 85 | pso.SetSeedRandomly.Value = true;
|
---|
| 86 | #endregion
|
---|
| 87 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
| 88 | return pso;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|