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 |
|
---|
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]
|
---|
33 | public class PsoRastriginSampleTest {
|
---|
34 | private const string SampleFileName = "PSO_Rastrigin";
|
---|
35 |
|
---|
36 | [TestMethod]
|
---|
37 | [TestCategory("Samples.Create")]
|
---|
38 | [TestProperty("Time", "medium")]
|
---|
39 | public void CreatePsoRastriginSampleTest() {
|
---|
40 | var pso = CreatePsoRastriginSample();
|
---|
41 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
42 | XmlGenerator.Serialize(pso, path);
|
---|
43 | }
|
---|
44 | [TestMethod]
|
---|
45 | [TestCategory("Samples.Execute")]
|
---|
46 | [TestProperty("Time", "medium")]
|
---|
47 | public void RunPsoRastriginSampleTest() {
|
---|
48 | var pso = CreatePsoRastriginSample();
|
---|
49 | pso.SetSeedRandomly.Value = false;
|
---|
50 | SamplesUtils.RunAlgorithm(pso);
|
---|
51 | if (Environment.Is64BitProcess) {
|
---|
52 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
53 | Assert.AreEqual(3.9649516110677525, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"), 1e-08);
|
---|
54 | Assert.AreEqual(25.566430359483757, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"), 1e-08);
|
---|
55 | Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
56 | } else {
|
---|
57 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
58 | Assert.AreEqual(3.3957460831564048, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"), 1e-08);
|
---|
59 | Assert.AreEqual(34.412788077766145, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"), 1e-08);
|
---|
60 | Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | private ParticleSwarmOptimization CreatePsoRastriginSample() {
|
---|
65 | ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
---|
66 | #region Problem Configuration
|
---|
67 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
68 | var provider = new SOTFInstanceProvider();
|
---|
69 | problem.Load(provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name == "Rastrigin Function")));
|
---|
70 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
71 | #endregion
|
---|
72 | #region Algorithm Configuration
|
---|
73 | pso.Name = "Particle Swarm Optimization - Rastrigin";
|
---|
74 | pso.Description = "A particle swarm optimization algorithm which solves the 2-dimensional Rastrigin test function.";
|
---|
75 | pso.Problem = problem;
|
---|
76 | pso.Inertia.Value = 0.721;
|
---|
77 | pso.MaxIterations.Value = 200;
|
---|
78 | pso.NeighborBestAttraction.Value = 1.193;
|
---|
79 | pso.PersonalBestAttraction.Value = 1.193;
|
---|
80 | pso.SwarmSize.Value = 40;
|
---|
81 |
|
---|
82 | pso.TopologyInitializer = pso.TopologyInitializerParameter.ValidValues.OfType<SPSORandomTopologyInitializer>().First();
|
---|
83 | pso.TopologyUpdater = pso.TopologyUpdaterParameter.ValidValues.OfType<SPSOAdaptiveRandomTopologyUpdater>().First();
|
---|
84 | pso.Seed.Value = 0;
|
---|
85 | pso.SetSeedRandomly.Value = true;
|
---|
86 | #endregion
|
---|
87 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
88 | return pso;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|