[11450] | 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 | [TestClass]
|
---|
| 35 | public class PsoSchwefelSampleTest {
|
---|
[11514] | 36 | private const string SampleFileName = "PSO_Schwefel";
|
---|
[11450] | 37 |
|
---|
| 38 | [TestMethod]
|
---|
| 39 | [TestCategory("Samples.Create")]
|
---|
| 40 | [TestProperty("Time", "medium")]
|
---|
| 41 | public void CreatePsoSchwefelSampleTest() {
|
---|
| 42 | var pso = CreatePsoSchwefelSample();
|
---|
[11514] | 43 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
| 44 | XmlGenerator.Serialize(pso, path);
|
---|
[11450] | 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.Value = 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.EvaluatorParameter.Value = new SchwefelEvaluator();
|
---|
| 74 | problem.Maximization.Value = false;
|
---|
| 75 | problem.ProblemSize.Value = 2;
|
---|
| 76 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
| 77 | #endregion
|
---|
| 78 | #region Algorithm Configuration
|
---|
| 79 | pso.Name = "Particle Swarm Optimization - Schwefel";
|
---|
| 80 | 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)";
|
---|
| 81 | pso.Problem = problem;
|
---|
| 82 | pso.Inertia.Value = 10;
|
---|
| 83 | pso.MaxIterations.Value = 1000;
|
---|
| 84 | pso.NeighborBestAttraction.Value = 0.5;
|
---|
| 85 | pso.PersonalBestAttraction.Value = -0.01;
|
---|
| 86 | pso.SwarmSize.Value = 50;
|
---|
| 87 |
|
---|
| 88 | var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
|
---|
| 89 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
| 90 | .Single();
|
---|
| 91 | inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
|
---|
| 92 | inertiaUpdater.EndValueParameter.Value = new DoubleValue(1);
|
---|
| 93 | pso.InertiaUpdater = inertiaUpdater;
|
---|
| 94 |
|
---|
| 95 | pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
|
---|
| 96 | .OfType<RealVectorParticleCreator>()
|
---|
| 97 | .Single();
|
---|
| 98 | var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
|
---|
| 99 | .OfType<RealVectorSwarmUpdater>()
|
---|
| 100 | .Single();
|
---|
| 101 | swarmUpdater.VelocityBoundsIndexParameter.ActualName = "Iterations";
|
---|
| 102 | swarmUpdater.VelocityBoundsParameter.Value = new DoubleMatrix(new double[,] { { -10, 10 } });
|
---|
| 103 | swarmUpdater.VelocityBoundsStartValueParameter.Value = new DoubleValue(10.0);
|
---|
| 104 | swarmUpdater.VelocityBoundsEndValueParameter.Value = new DoubleValue(1.0);
|
---|
| 105 | swarmUpdater.VelocityBoundsScalingOperatorParameter.Value = swarmUpdater.VelocityBoundsScalingOperatorParameter.ValidValues
|
---|
| 106 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
| 107 | .Single();
|
---|
| 108 |
|
---|
| 109 | pso.TopologyInitializer = null;
|
---|
| 110 | pso.TopologyUpdater = null;
|
---|
| 111 | pso.SwarmUpdater = swarmUpdater;
|
---|
| 112 | pso.Seed.Value = 0;
|
---|
| 113 | pso.SetSeedRandomly.Value = true;
|
---|
| 114 | #endregion
|
---|
| 115 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
| 116 | return pso;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|