Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/PsoRastriginSampleTest.cs
- Timestamp:
- 03/18/19 17:24:30 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Tests
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/PsoRastriginSampleTest.cs
r16691 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 24 24 using System.Linq; 25 25 using HeuristicLab.Algorithms.ParticleSwarmOptimization; 26 using HeuristicLab.Data;27 26 using HeuristicLab.Encodings.RealVectorEncoding; 28 using HeuristicLab.Optimization.Operators;29 27 using HeuristicLab.Persistence.Default.Xml; 30 28 using HeuristicLab.Problems.TestFunctions; … … 33 31 namespace HeuristicLab.Tests { 34 32 [TestClass] 35 public class Pso SchwefelSampleTest {36 private const string SampleFileName = "PSO_ Schwefel";33 public class PsoRastriginSampleTest { 34 private const string SampleFileName = "PSO_Rastrigin"; 37 35 38 36 [TestMethod] 39 37 [TestCategory("Samples.Create")] 40 38 [TestProperty("Time", "medium")] 41 public void CreatePso SchwefelSampleTest() {42 var pso = CreatePso SchwefelSample();39 public void CreatePsoRastriginSampleTest() { 40 var pso = CreatePsoRastriginSample(); 43 41 string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension); 44 42 XmlGenerator.Serialize(pso, path); … … 47 45 [TestCategory("Samples.Execute")] 48 46 [TestProperty("Time", "medium")] 49 public void RunPso SchwefelSampleTest() {50 var pso = CreatePso SchwefelSample();47 public void RunPsoRastriginSampleTest() { 48 var pso = CreatePsoRastriginSample(); 51 49 pso.SetSeedRandomly.Value = false; 52 50 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(2 20.956806502853, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));57 Assert.AreEqual( 1000, SamplesUtils.GetIntResult(pso, "Iterations"));51 if (Environment.Is64BitProcess) { 52 Assert.AreEqual(0, SamplesUtils.GetDoubleResult(pso, "BestQuality")); 53 Assert.AreEqual(3.9649516110677525, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"), 1e-08); 54 Assert.AreEqual(25.566430359483757, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"), 1e-08); 55 Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations")); 58 56 } 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"));57 Assert.AreEqual(0, SamplesUtils.GetDoubleResult(pso, "BestQuality")); 58 Assert.AreEqual(3.3957460831564048, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"), 1e-08); 59 Assert.AreEqual(34.412788077766145, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"), 1e-08); 60 Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations")); 63 61 } 64 62 } 65 63 66 private ParticleSwarmOptimization CreatePso SchwefelSample() {64 private ParticleSwarmOptimization CreatePsoRastriginSample() { 67 65 ParticleSwarmOptimization pso = new ParticleSwarmOptimization(); 68 66 #region Problem Configuration 69 67 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(); 68 var provider = new SOTFInstanceProvider(); 69 problem.Load(provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name == "Rastrigin Function"))); 70 problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator(); 76 71 #endregion 77 72 #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)";73 pso.Name = "Particle Swarm Optimization - Rastrigin"; 74 pso.Description = "A particle swarm optimization algorithm which solves the 2-dimensional Rastrigin test function."; 80 75 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; 76 pso.Inertia.Value = 0.721; 77 pso.MaxIterations.Value = 200; 78 pso.NeighborBestAttraction.Value = 1.193; 79 pso.PersonalBestAttraction.Value = 1.193; 80 pso.SwarmSize.Value = 40; 81 82 pso.TopologyInitializer = pso.TopologyInitializerParameter.ValidValues.OfType<SPSORandomTopologyInitializer>().First(); 83 pso.TopologyUpdater = pso.TopologyUpdaterParameter.ValidValues.OfType<SPSOAdaptiveRandomTopologyUpdater>().First(); 111 84 pso.Seed.Value = 0; 112 85 pso.SetSeedRandomly.Value = true;
Note: See TracChangeset
for help on using the changeset viewer.