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.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(2.8334909529803554E-08, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
55 | Assert.AreEqual(128.08680460446624, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
56 | Assert.AreEqual(713.67728101375587, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
57 | Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
|
---|
58 | } else {
|
---|
59 | Assert.AreEqual(2.8334909529803554E-08, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
|
---|
60 | Assert.AreEqual(128.08680460446624, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
61 | Assert.AreEqual(713.67728101375587, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
62 | Assert.AreEqual(200, 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 = 1.1;
|
---|
83 | pso.MaxIterations.Value = 200;
|
---|
84 | pso.NeighborBestAttraction.Value = 1;
|
---|
85 | pso.PersonalBestAttraction.Value = 1;
|
---|
86 | pso.SwarmSize.Value = 40;
|
---|
87 |
|
---|
88 | var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
|
---|
89 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
90 | .Single();
|
---|
91 | inertiaUpdater.EndValueParameter.Value = new DoubleValue(0.721);
|
---|
92 | pso.InertiaUpdater = inertiaUpdater;
|
---|
93 |
|
---|
94 | pso.TopologyInitializer = null;
|
---|
95 | pso.TopologyUpdater = null;
|
---|
96 | pso.Seed.Value = 0;
|
---|
97 | pso.SetSeedRandomly.Value = true;
|
---|
98 | #endregion
|
---|
99 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
100 | return pso;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|