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 |
|
---|
22 | using System;
|
---|
23 | using System.IO;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Algorithms.ParticleSwarmOptimization;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
28 | using HeuristicLab.Optimization.Operators;
|
---|
29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
30 | using HeuristicLab.Problems.TestFunctions;
|
---|
31 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
32 |
|
---|
33 | namespace 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 = 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.TestFunction = new Schwefel();
|
---|
74 | problem.ProblemSize = 2;
|
---|
75 | problem.Encoding.SolutionCreator = 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 = 10;
|
---|
82 | pso.MaxIterations.Value = 1000;
|
---|
83 | pso.NeighborBestAttraction.Value = 0.5;
|
---|
84 | pso.PersonalBestAttraction.Value = -0.01;
|
---|
85 | pso.SwarmSize.Value = 50;
|
---|
86 |
|
---|
87 | var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
|
---|
88 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
89 | .Single();
|
---|
90 | inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
|
---|
91 | inertiaUpdater.EndValueParameter.Value = new DoubleValue(1);
|
---|
92 | pso.InertiaUpdater = inertiaUpdater;
|
---|
93 |
|
---|
94 | pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
|
---|
95 | .OfType<RealVectorParticleCreator>()
|
---|
96 | .Single();
|
---|
97 | var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
|
---|
98 | .OfType<RealVectorSwarmUpdater>()
|
---|
99 | .Single();
|
---|
100 | swarmUpdater.VelocityBoundsIndexParameter.ActualName = "Iterations";
|
---|
101 | swarmUpdater.VelocityBoundsParameter.Value = new DoubleMatrix(new double[,] { { -10, 10 } });
|
---|
102 | swarmUpdater.VelocityBoundsStartValueParameter.Value = new DoubleValue(10.0);
|
---|
103 | swarmUpdater.VelocityBoundsEndValueParameter.Value = new DoubleValue(1.0);
|
---|
104 | swarmUpdater.VelocityBoundsScalingOperatorParameter.Value = swarmUpdater.VelocityBoundsScalingOperatorParameter.ValidValues
|
---|
105 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
106 | .Single();
|
---|
107 |
|
---|
108 | pso.TopologyInitializer = null;
|
---|
109 | pso.TopologyUpdater = null;
|
---|
110 | pso.SwarmUpdater = swarmUpdater;
|
---|
111 | pso.Seed.Value = 0;
|
---|
112 | pso.SetSeedRandomly.Value = true;
|
---|
113 | #endregion
|
---|
114 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
115 | return pso;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|