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