1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 | /// <summary>
|
---|
35 | /// Summary description for PsoSchwefelSampleTest
|
---|
36 | /// </summary>
|
---|
37 | [TestClass]
|
---|
38 | public class PsoSchwefelSampleTest {
|
---|
39 | private const string samplesDirectory = SamplesUtils.Directory;
|
---|
40 | [ClassInitialize]
|
---|
41 | public static void MyClassInitialize(TestContext testContext) {
|
---|
42 | if (!Directory.Exists(samplesDirectory))
|
---|
43 | Directory.CreateDirectory(samplesDirectory);
|
---|
44 | }
|
---|
45 |
|
---|
46 | [TestMethod]
|
---|
47 | [TestCategory("Samples.Create")]
|
---|
48 | [TestProperty("Time", "medium")]
|
---|
49 | public void CreatePsoSchwefelSampleTest() {
|
---|
50 | var pso = CreatePsoSchwefelSample();
|
---|
51 | XmlGenerator.Serialize(pso, @"Samples\PSO_Schwefel.hl");
|
---|
52 | }
|
---|
53 | [TestMethod]
|
---|
54 | [TestCategory("Samples.Execute")]
|
---|
55 | [TestProperty("Time", "medium")]
|
---|
56 | public void RunPsoSchwefelSampleTest() {
|
---|
57 | var pso = CreatePsoSchwefelSample();
|
---|
58 | pso.SetSeedRandomly.Value = false;
|
---|
59 | SamplesUtils.RunAlgorithm(pso);
|
---|
60 | if (!Environment.Is64BitProcess) {
|
---|
61 | Assert.AreEqual(118.44027985932837, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
62 | Assert.AreEqual(140.71570105946438, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
63 | Assert.AreEqual(220.956806502853, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
64 | Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
65 | } else {
|
---|
66 | Assert.AreEqual(118.43958282879345, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
67 | Assert.AreEqual(139.43946864779372, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
68 | Assert.AreEqual(217.14654589055152, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
69 | Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private ParticleSwarmOptimization CreatePsoSchwefelSample() {
|
---|
74 | ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
---|
75 | #region Problem Configuration
|
---|
76 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
77 | problem.BestKnownQuality.Value = 0.0;
|
---|
78 | problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 420.968746, 420.968746 });
|
---|
79 | problem.Bounds = new DoubleMatrix(new double[,] { { -500, 500 } });
|
---|
80 | problem.EvaluatorParameter.Value = new SchwefelEvaluator();
|
---|
81 | problem.Maximization.Value = false;
|
---|
82 | problem.ProblemSize.Value = 2;
|
---|
83 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
84 | #endregion
|
---|
85 | #region Algorithm Configuration
|
---|
86 | pso.Name = "Particle Swarm Optimization - Schwefel";
|
---|
87 | 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)";
|
---|
88 | pso.Problem = problem;
|
---|
89 | pso.Inertia.Value = 10;
|
---|
90 | pso.MaxIterations.Value = 1000;
|
---|
91 | pso.NeighborBestAttraction.Value = 0.5;
|
---|
92 | pso.PersonalBestAttraction.Value = -0.01;
|
---|
93 | pso.SwarmSize.Value = 50;
|
---|
94 |
|
---|
95 | var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
|
---|
96 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
97 | .Single();
|
---|
98 | inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
|
---|
99 | inertiaUpdater.EndValueParameter.Value = new DoubleValue(1);
|
---|
100 | pso.InertiaUpdater = inertiaUpdater;
|
---|
101 |
|
---|
102 | pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
|
---|
103 | .OfType<RealVectorParticleCreator>()
|
---|
104 | .Single();
|
---|
105 | var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
|
---|
106 | .OfType<RealVectorSwarmUpdater>()
|
---|
107 | .Single();
|
---|
108 | swarmUpdater.VelocityBoundsIndexParameter.ActualName = "Iterations";
|
---|
109 | swarmUpdater.VelocityBoundsParameter.Value = new DoubleMatrix(new double[,] { { -10, 10 } });
|
---|
110 | swarmUpdater.VelocityBoundsStartValueParameter.Value = new DoubleValue(10.0);
|
---|
111 | swarmUpdater.VelocityBoundsEndValueParameter.Value = new DoubleValue(1.0);
|
---|
112 | swarmUpdater.VelocityBoundsScalingOperatorParameter.Value = swarmUpdater.VelocityBoundsScalingOperatorParameter.ValidValues
|
---|
113 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
114 | .Single();
|
---|
115 |
|
---|
116 | pso.TopologyInitializer = null;
|
---|
117 | pso.TopologyUpdater = null;
|
---|
118 | pso.SwarmUpdater = swarmUpdater;
|
---|
119 | pso.Seed.Value = 0;
|
---|
120 | pso.SetSeedRandomly.Value = true;
|
---|
121 | #endregion
|
---|
122 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
123 | return pso;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|