Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/27/14 10:02:58 (10 years ago)
Author:
mkommend
Message:

#1638: Extracted samples unit test into separate folder, extracted utility methods and added a unit test for the multiplexer problem.

Location:
trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples
Files:
3 added
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SamplesTest.cs

    r11050 r11051  
    3737using HeuristicLab.Data;
    3838using HeuristicLab.Encodings.BinaryVectorEncoding;
    39 using HeuristicLab.Encodings.IntegerVectorEncoding;
    4039using HeuristicLab.Encodings.PermutationEncoding;
    4140using HeuristicLab.Encodings.RealVectorEncoding;
     
    7271  [TestClass]
    7372  public class SamplesTest {
    74     private const string samplesDirectory = @"Samples\";
     73    private const string samplesDirectory = SamplesUtils.Directory;
    7574
    7675    [ClassInitialize]
     
    9594      var ga = CreateGaTspSample();
    9695      ga.SetSeedRandomly.Value = false;
    97       RunAlgorithm(ga);
    98       Assert.AreEqual(12332, GetDoubleResult(ga, "BestQuality"));
    99       Assert.AreEqual(13123.2, GetDoubleResult(ga, "CurrentAverageQuality"));
    100       Assert.AreEqual(14538, GetDoubleResult(ga, "CurrentWorstQuality"));
    101       Assert.AreEqual(99100, GetIntResult(ga, "EvaluatedSolutions"));
     96      SamplesUtils.RunAlgorithm(ga);
     97      Assert.AreEqual(12332, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
     98      Assert.AreEqual(13123.2, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
     99      Assert.AreEqual(14538, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
     100      Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
    102101    }
    103102
     
    115114      ga.Description = "A genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
    116115      ga.Problem = tspProblem;
    117       ConfigureGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator>(
     116      SamplesUtils.ConfigureGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator>(
    118117        ga, 100, 1, 1000, 0.05);
    119118      #endregion
     
    136135      var ga = CreateGaVrpSample();
    137136      ga.SetSeedRandomly.Value = false;
    138       RunAlgorithm(ga);
    139       Assert.AreEqual(1828.9368669428338, GetDoubleResult(ga, "BestQuality"));
    140       Assert.AreEqual(1830.1444308908331, GetDoubleResult(ga, "CurrentAverageQuality"));
    141       Assert.AreEqual(1871.7128510304112, GetDoubleResult(ga, "CurrentWorstQuality"));
    142       Assert.AreEqual(99100, GetIntResult(ga, "EvaluatedSolutions"));
     137      SamplesUtils.RunAlgorithm(ga);
     138      Assert.AreEqual(1828.9368669428338, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
     139      Assert.AreEqual(1830.1444308908331, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
     140      Assert.AreEqual(1871.7128510304112, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
     141      Assert.AreEqual(99100, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
    143142    }
    144143
     
    167166      ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
    168167      ga.Problem = vrpProblem;
    169       ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
     168      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
    170169        ga, 100, 1, 1000, 0.05, 3);
    171170
     
    210209      var ga = CreateGpArtificialAntSample();
    211210      ga.SetSeedRandomly.Value = false;
    212       RunAlgorithm(ga);
    213       Assert.AreEqual(81, GetDoubleResult(ga, "BestQuality"));
    214       Assert.AreEqual(48.19, GetDoubleResult(ga, "CurrentAverageQuality"));
    215       Assert.AreEqual(0, GetDoubleResult(ga, "CurrentWorstQuality"));
    216       Assert.AreEqual(50950, GetIntResult(ga, "EvaluatedSolutions"));
     211      SamplesUtils.RunAlgorithm(ga);
     212      Assert.AreEqual(81, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
     213      Assert.AreEqual(48.19, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
     214      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
     215      Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
    217216    }
    218217
     
    232231      ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
    233232      ga.Problem = antProblem;
    234       ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
     233      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
    235234        ga, 1000, 1, 50, 0.15, 5);
    236235      var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
     
    265264      var ga = CreateGpSymbolicRegressionSample();
    266265      ga.SetSeedRandomly.Value = false;
    267       RunAlgorithm(ga);
    268       Assert.AreEqual(0.858344291534625, GetDoubleResult(ga, "BestQuality"), 1E-8);
    269       Assert.AreEqual(0.56758466520692641, GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
    270       Assert.AreEqual(0, GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
    271       Assert.AreEqual(50950, GetIntResult(ga, "EvaluatedSolutions"));
     266      SamplesUtils.RunAlgorithm(ga);
     267      Assert.AreEqual(0.858344291534625, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
     268      Assert.AreEqual(0.56758466520692641, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
     269      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
     270      Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
    272271      var bestTrainingSolution = (IRegressionSolution)ga.Results["Best training solution"].Value;
    273272      Assert.AreEqual(0.85504801557844745, bestTrainingSolution.TrainingRSquared, 1E-8);
     
    347346      ga.Name = "Genetic Programming - Symbolic Regression";
    348347      ga.Description = "A standard genetic programming algorithm to solve a symbolic regression problem (tower dataset)";
    349       ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
     348      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
    350349        ga, 1000, 1, 50, 0.15, 5);
    351350      var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
     
    380379      var ga = CreateGpSymbolicClassificationSample();
    381380      ga.SetSeedRandomly.Value = false;
    382       RunAlgorithm(ga);
    383       Assert.AreEqual(0.141880203907627, GetDoubleResult(ga, "BestQuality"), 1E-8);
    384       Assert.AreEqual(4.3246992327753295, GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
    385       Assert.AreEqual(100.62175156249987, GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
    386       Assert.AreEqual(100900, GetIntResult(ga, "EvaluatedSolutions"));
     381      SamplesUtils.RunAlgorithm(ga);
     382      Assert.AreEqual(0.141880203907627, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
     383      Assert.AreEqual(4.3246992327753295, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
     384      Assert.AreEqual(100.62175156249987, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
     385      Assert.AreEqual(100900, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
    387386      var bestTrainingSolution = (IClassificationSolution)ga.Results["Best training solution"].Value;
    388387      Assert.AreEqual(0.80875, bestTrainingSolution.TrainingAccuracy, 1E-8);
     
    459458      ga.Name = "Genetic Programming - Symbolic Classification";
    460459      ga.Description = "A standard genetic programming algorithm to solve a classification problem (Mammographic+Mass dataset)";
    461       ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
     460      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
    462461        ga, 1000, 1, 100, 0.15, 5
    463462        );
     
    486485      var ga = CreateGpLawnMowerSample();
    487486      ga.SetSeedRandomly.Value = false;
    488       RunAlgorithm(ga);
     487      SamplesUtils.RunAlgorithm(ga);
    489488    }
    490489
     
    498497      ga.Description = "A standard genetic programming algorithm to solve the lawn mower problem";
    499498      ga.Problem = problem;
    500       ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
     499      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
    501500        ga, 1000, 1, 50, 0.25, 5);
    502501      var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
     
    525524      var es = CreateEsGriewankSample();
    526525      es.SetSeedRandomly.Value = false;
    527       RunAlgorithm(es);
    528       Assert.AreEqual(0, GetDoubleResult(es, "BestQuality"));
    529       Assert.AreEqual(0, GetDoubleResult(es, "CurrentAverageQuality"));
    530       Assert.AreEqual(0, GetDoubleResult(es, "CurrentWorstQuality"));
    531       Assert.AreEqual(100020, GetIntResult(es, "EvaluatedSolutions"));
     526      SamplesUtils.RunAlgorithm(es);
     527      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "BestQuality"));
     528      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentAverageQuality"));
     529      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(es, "CurrentWorstQuality"));
     530      Assert.AreEqual(100020, SamplesUtils.GetIntResult(es, "EvaluatedSolutions"));
    532531    }
    533532
     
    551550      es.Description = "An evolution strategy which solves the 10-dimensional Griewank test function";
    552551      es.Problem = problem;
    553       ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
     552      SamplesUtils.ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
    554553        StdDevStrategyVectorCreator, StdDevStrategyVectorCrossover, StdDevStrategyVectorManipulator>(
    555554        es, 20, 500, 2, 200, false);
     
    583582      var ga = CreateIslandGaTspSample();
    584583      ga.SetSeedRandomly.Value = false;
    585       RunAlgorithm(ga);
    586       Assert.AreEqual(9918, GetDoubleResult(ga, "BestQuality"));
    587       Assert.AreEqual(10324.64, GetDoubleResult(ga, "CurrentAverageQuality"));
    588       Assert.AreEqual(11823, GetDoubleResult(ga, "CurrentWorstQuality"));
    589       Assert.AreEqual(495500, GetIntResult(ga, "EvaluatedSolutions"));
     584      SamplesUtils.RunAlgorithm(ga);
     585      Assert.AreEqual(9918, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
     586      Assert.AreEqual(10324.64, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
     587      Assert.AreEqual(11823, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
     588      Assert.AreEqual(495500, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
    590589    }
    591590
     
    603602      ga.Description = "An island genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
    604603      ga.Problem = tspProblem;
    605       ConfigureIslandGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator,
     604      SamplesUtils.ConfigureIslandGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator,
    606605        UnidirectionalRingMigrator, BestSelector, WorstReplacer>(
    607606        ga, 100, 1, 1000, 0.05, 5, 50, 0.25);
     
    627626      var ls = CreateLocalSearchKnapsackSample();
    628627      ls.SetSeedRandomly.Value = false;
    629       RunAlgorithm(ls);
    630       Assert.AreEqual(345, GetDoubleResult(ls, "BestQuality"));
    631       Assert.AreEqual(340.70731707317071, GetDoubleResult(ls, "CurrentAverageQuality"));
    632       Assert.AreEqual(337, GetDoubleResult(ls, "CurrentWorstQuality"));
    633       Assert.AreEqual(82000, GetIntResult(ls, "EvaluatedMoves"));
     628      SamplesUtils.RunAlgorithm(ls);
     629      Assert.AreEqual(345, SamplesUtils.GetDoubleResult(ls, "BestQuality"));
     630      Assert.AreEqual(340.70731707317071, SamplesUtils.GetDoubleResult(ls, "CurrentAverageQuality"));
     631      Assert.AreEqual(337, SamplesUtils.GetDoubleResult(ls, "CurrentWorstQuality"));
     632      Assert.AreEqual(82000, SamplesUtils.GetIntResult(ls, "EvaluatedMoves"));
    634633    }
    635634
     
    692691      var pso = CreatePsoSchwefelSample();
    693692      pso.SetSeedRandomly.Value = false;
    694       RunAlgorithm(pso);
     693      SamplesUtils.RunAlgorithm(pso);
    695694      if (!Environment.Is64BitProcess) {
    696         Assert.AreEqual(118.44027985932837, GetDoubleResult(pso, "BestQuality"));
    697         Assert.AreEqual(140.71570105946438, GetDoubleResult(pso, "CurrentAverageQuality"));
    698         Assert.AreEqual(220.956806502853, GetDoubleResult(pso, "CurrentWorstQuality"));
    699         Assert.AreEqual(1000, GetIntResult(pso, "Iterations"));
     695        Assert.AreEqual(118.44027985932837, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
     696        Assert.AreEqual(140.71570105946438, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
     697        Assert.AreEqual(220.956806502853, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
     698        Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
    700699      } else {
    701         Assert.AreEqual(118.43958282879345, GetDoubleResult(pso, "BestQuality"));
    702         Assert.AreEqual(139.43946864779372, GetDoubleResult(pso, "CurrentAverageQuality"));
    703         Assert.AreEqual(217.14654589055152, GetDoubleResult(pso, "CurrentWorstQuality"));
    704         Assert.AreEqual(1000, GetIntResult(pso, "Iterations"));
     700        Assert.AreEqual(118.43958282879345, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
     701        Assert.AreEqual(139.43946864779372, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
     702        Assert.AreEqual(217.14654589055152, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
     703        Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
    705704      }
    706705    }
     
    775774      var sa = CreateSimulatedAnnealingRastriginSample();
    776775      sa.SetSeedRandomly.Value = false;
    777       RunAlgorithm(sa);
    778       Assert.AreEqual(0.00014039606034543795, GetDoubleResult(sa, "BestQuality"));
    779       Assert.AreEqual(5000, GetIntResult(sa, "EvaluatedMoves"));
     776      SamplesUtils.RunAlgorithm(sa);
     777      Assert.AreEqual(0.00014039606034543795, SamplesUtils.GetDoubleResult(sa, "BestQuality"));
     778      Assert.AreEqual(5000, SamplesUtils.GetIntResult(sa, "EvaluatedMoves"));
    780779    }
    781780    private SimulatedAnnealing CreateSimulatedAnnealingRastriginSample() {
     
    845844      var ts = CreateTabuSearchTspSample();
    846845      ts.SetSeedRandomly.Value = false;
    847       RunAlgorithm(ts);
    848       Assert.AreEqual(6294, GetDoubleResult(ts, "BestQuality"));
    849       Assert.AreEqual(7380.0386666666664, GetDoubleResult(ts, "CurrentAverageQuality"));
    850       Assert.AreEqual(8328, GetDoubleResult(ts, "CurrentWorstQuality"));
    851       Assert.AreEqual(750000, GetIntResult(ts, "EvaluatedMoves"));
     846      SamplesUtils.RunAlgorithm(ts);
     847      Assert.AreEqual(6294, SamplesUtils.GetDoubleResult(ts, "BestQuality"));
     848      Assert.AreEqual(7380.0386666666664, SamplesUtils.GetDoubleResult(ts, "CurrentAverageQuality"));
     849      Assert.AreEqual(8328, SamplesUtils.GetDoubleResult(ts, "CurrentWorstQuality"));
     850      Assert.AreEqual(750000, SamplesUtils.GetIntResult(ts, "EvaluatedMoves"));
    852851    }
    853852
     
    916915      var vrp = CreateTabuSearchVrpSample();
    917916      vrp.SetSeedRandomly.Value = false;
    918       RunAlgorithm(vrp);
    919       Assert.AreEqual(1473, GetDoubleResult(vrp, "BestQuality"));
    920       Assert.AreEqual(2102.1192622950812, GetDoubleResult(vrp, "CurrentAverageQuality"));
    921       Assert.AreEqual(4006, GetDoubleResult(vrp, "CurrentWorstQuality"));
    922       Assert.AreEqual(119072, GetIntResult(vrp, "EvaluatedMoves"));
     917      SamplesUtils.RunAlgorithm(vrp);
     918      Assert.AreEqual(1473, SamplesUtils.GetDoubleResult(vrp, "BestQuality"));
     919      Assert.AreEqual(2102.1192622950812, SamplesUtils.GetDoubleResult(vrp, "CurrentAverageQuality"));
     920      Assert.AreEqual(4006, SamplesUtils.GetDoubleResult(vrp, "CurrentWorstQuality"));
     921      Assert.AreEqual(119072, SamplesUtils.GetIntResult(vrp, "EvaluatedMoves"));
    923922    }
    924923
     
    988987      var vns = CreateVnsTspSample();
    989988      vns.SetSeedRandomly = false;
    990       RunAlgorithm(vns);
    991       Assert.AreEqual(867, GetDoubleResult(vns, "BestQuality"));
    992       Assert.AreEqual(867, GetDoubleResult(vns, "CurrentAverageQuality"));
    993       Assert.AreEqual(867, GetDoubleResult(vns, "CurrentWorstQuality"));
    994       Assert.AreEqual(12975173, GetIntResult(vns, "EvaluatedSolutions"));
     989      SamplesUtils.RunAlgorithm(vns);
     990      Assert.AreEqual(867, SamplesUtils.GetDoubleResult(vns, "BestQuality"));
     991      Assert.AreEqual(867, SamplesUtils.GetDoubleResult(vns, "CurrentAverageQuality"));
     992      Assert.AreEqual(867, SamplesUtils.GetDoubleResult(vns, "CurrentWorstQuality"));
     993      Assert.AreEqual(12975173, SamplesUtils.GetIntResult(vns, "EvaluatedSolutions"));
    995994    }
    996995
     
    10701069      gpr.SetSeedRandomly = false;
    10711070      gpr.Seed = 1618551877;
    1072       RunAlgorithm(gpr);
    1073       Assert.AreEqual(-940.48768748097029, GetDoubleResult(gpr, "NegativeLogLikelihood"));
    1074       Assert.AreEqual(0.99561947047986976, GetDoubleResult(gpr, "Training R²"));
     1071      SamplesUtils.RunAlgorithm(gpr);
     1072      Assert.AreEqual(-940.48768748097029, SamplesUtils.GetDoubleResult(gpr, "NegativeLogLikelihood"));
     1073      Assert.AreEqual(0.99561947047986976, SamplesUtils.GetDoubleResult(gpr, "Training R²"));
    10751074    }
    10761075
     
    11131112      var ss = CreateScatterSearchVRPSample();
    11141113      ss.SetSeedRandomly.Value = false;
    1115       RunAlgorithm(ss);
    1116       Assert.AreEqual(828.93686694283383, GetDoubleResult(ss, "BestQuality"));
    1117       Assert.AreEqual(868.63623986983077, GetDoubleResult(ss, "CurrentAverageQuality"));
    1118       Assert.AreEqual(1048.8333559209832, GetDoubleResult(ss, "CurrentWorstQuality"));
    1119       Assert.AreEqual(262622, GetIntResult(ss, "EvaluatedSolutions"));
     1114      SamplesUtils.RunAlgorithm(ss);
     1115      Assert.AreEqual(828.93686694283383, SamplesUtils.GetDoubleResult(ss, "BestQuality"));
     1116      Assert.AreEqual(868.63623986983077, SamplesUtils.GetDoubleResult(ss, "CurrentAverageQuality"));
     1117      Assert.AreEqual(1048.8333559209832, SamplesUtils.GetDoubleResult(ss, "CurrentWorstQuality"));
     1118      Assert.AreEqual(262622, SamplesUtils.GetIntResult(ss, "EvaluatedSolutions"));
    11201119    }
    11211120
     
    11731172      var rapga = CreateRAPGASchedulingSample();
    11741173      rapga.SetSeedRandomly.Value = false;
    1175       RunAlgorithm(rapga);
    1176       Assert.AreEqual(988.00, GetDoubleResult(rapga, "BestQuality"));
    1177       Assert.AreEqual(988.00, GetDoubleResult(rapga, "CurrentAverageQuality"));
    1178       Assert.AreEqual(988.00, GetDoubleResult(rapga, "CurrentWorstQuality"));
    1179       Assert.AreEqual(27100, GetIntResult(rapga, "EvaluatedSolutions"));
     1174      SamplesUtils.RunAlgorithm(rapga);
     1175      Assert.AreEqual(988.00, SamplesUtils.GetDoubleResult(rapga, "BestQuality"));
     1176      Assert.AreEqual(988.00, SamplesUtils.GetDoubleResult(rapga, "CurrentAverageQuality"));
     1177      Assert.AreEqual(988.00, SamplesUtils.GetDoubleResult(rapga, "CurrentWorstQuality"));
     1178      Assert.AreEqual(27100, SamplesUtils.GetIntResult(rapga, "EvaluatedSolutions"));
    11801179    }
    11811180
     
    12151214      var ga = CreateGeArtificialAntSample();
    12161215      ga.SetSeedRandomly.Value = false;
    1217       RunAlgorithm(ga);
     1216      SamplesUtils.RunAlgorithm(ga);
    12181217    }
    12191218
     
    12271226      ga.Description = "Grammatical evolution algorithm for solving a artificial ant problem";
    12281227      ga.Problem = problem;
    1229       ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
     1228      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
    12301229        ga, 200, 1, 50, 0.05, 200);
    12311230      #endregion
     
    12501249      var ga = CreateGeSymbolicRegressionSample();
    12511250      ga.SetSeedRandomly.Value = false;
    1252       RunAlgorithm(ga);
     1251      SamplesUtils.RunAlgorithm(ga);
    12531252    }
    12541253
     
    12711270      }
    12721271
    1273       ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
     1272      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
    12741273        ga, 1000, 1, 50, 0.05, 200);
    12751274      #endregion
     
    12791278
    12801279    #region Helpers
    1281     private void ConfigureEvolutionStrategyParameters<R, M, SC, SR, SM>(EvolutionStrategy es, int popSize, int children, int parentsPerChild, int maxGens, bool plusSelection)
    1282       where R : ICrossover
    1283       where M : IManipulator
    1284       where SC : IStrategyParameterCreator
    1285       where SR : IStrategyParameterCrossover
    1286       where SM : IStrategyParameterManipulator {
    1287       es.PopulationSize.Value = popSize;
    1288       es.Children.Value = children;
    1289       es.ParentsPerChild.Value = parentsPerChild;
    1290       es.MaximumGenerations.Value = maxGens;
    1291       es.PlusSelection.Value = false;
    1292 
    1293       es.Seed.Value = 0;
    1294       es.SetSeedRandomly.Value = true;
    1295 
    1296       es.Recombinator = es.RecombinatorParameter.ValidValues
    1297         .OfType<R>()
    1298         .Single();
    1299 
    1300       es.Mutator = es.MutatorParameter.ValidValues
    1301         .OfType<M>()
    1302         .Single();
    1303 
    1304       es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues
    1305         .OfType<SC>()
    1306         .Single();
    1307       es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues
    1308         .OfType<SR>()
    1309         .Single();
    1310       es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues
    1311         .OfType<SM>()
    1312         .Single();
    1313       es.Engine = new ParallelEngine.ParallelEngine();
    1314     }
    1315 
    1316     private void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0)
    1317       where S : ISelector
    1318       where C : ICrossover
    1319       where M : IManipulator {
    1320       ga.Elites.Value = elites;
    1321       ga.MaximumGenerations.Value = maxGens;
    1322       ga.MutationProbability.Value = mutationRate;
    1323       ga.PopulationSize.Value = popSize;
    1324       ga.Seed.Value = 0;
    1325       ga.SetSeedRandomly.Value = true;
    1326       ga.Selector = ga.SelectorParameter.ValidValues
    1327         .OfType<S>()
    1328         .First();
    1329 
    1330       ga.Crossover = ga.CrossoverParameter.ValidValues
    1331         .OfType<C>()
    1332         .First();
    1333 
    1334       ga.Mutator = ga.MutatorParameter.ValidValues
    1335         .OfType<M>()
    1336         .First();
    1337 
    1338       var tSelector = ga.Selector as TournamentSelector;
    1339       if (tSelector != null) {
    1340         tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
    1341       }
    1342       ga.Engine = new ParallelEngine.ParallelEngine();
    1343     }
    1344 
    1345     private void ConfigureOsGeneticAlgorithmParameters<S, C, M>(OffspringSelectionGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate=0.05, double maxSelPres=100, int tournGroupSize = 0)
    1346       where S : ISelector
    1347       where C : ICrossover
    1348       where M : IManipulator {
    1349       ga.Elites.Value = elites;
    1350       ga.MaximumGenerations.Value = maxGens;
    1351       ga.MutationProbability.Value = mutationRate;
    1352       ga.PopulationSize.Value = popSize;
    1353       ga.MaximumSelectionPressure.Value = maxSelPres;
    1354       ga.Seed.Value = 0;
    1355       ga.SetSeedRandomly.Value = true;
    1356       ga.ComparisonFactorLowerBound.Value = 1;
    1357       ga.ComparisonFactorUpperBound.Value = 1;
    1358 
    1359       ga.Selector = ga.SelectorParameter.ValidValues
    1360         .OfType<S>()
    1361         .First();
    1362 
    1363       ga.Crossover = ga.CrossoverParameter.ValidValues
    1364         .OfType<C>()
    1365         .First();
    1366 
    1367       ga.Mutator = ga.MutatorParameter.ValidValues
    1368         .OfType<M>()
    1369         .First();
    1370 
    1371       var tSelector = ga.Selector as TournamentSelector;
    1372       if (tSelector != null) {
    1373         tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
    1374       }
    1375       ga.Engine = new ParallelEngine.ParallelEngine();
    1376     }
    1377 
    1378     private void ConfigureIslandGeneticAlgorithmParameters<S, C, M, Mi, MiS, MiR>(IslandGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int numberOfIslands, int migrationInterval, double migrationRate)
    1379       where S : ISelector
    1380       where C : ICrossover
    1381       where M : IManipulator
    1382       where Mi : IMigrator
    1383       where MiS : ISelector
    1384       where MiR : IReplacer {
    1385       ga.Elites.Value = elites;
    1386       ga.MaximumGenerations.Value = maxGens;
    1387       ga.MutationProbability.Value = mutationRate;
    1388       ga.PopulationSize.Value = popSize;
    1389       ga.NumberOfIslands.Value = numberOfIslands;
    1390       ga.MigrationInterval.Value = migrationInterval;
    1391       ga.MigrationRate.Value = migrationRate;
    1392       ga.Seed.Value = 0;
    1393       ga.SetSeedRandomly.Value = true;
    1394       ga.Selector = ga.SelectorParameter.ValidValues
    1395         .OfType<S>()
    1396         .Single();
    1397 
    1398       ga.Crossover = ga.CrossoverParameter.ValidValues
    1399         .OfType<C>()
    1400         .Single();
    1401 
    1402       ga.Mutator = ga.MutatorParameter.ValidValues
    1403         .OfType<M>()
    1404         .Single();
    1405       ga.Migrator = ga.MigratorParameter.ValidValues
    1406         .OfType<Mi>()
    1407         .Single();
    1408       ga.EmigrantsSelector = ga.EmigrantsSelectorParameter.ValidValues
    1409         .OfType<MiS>()
    1410         .Single();
    1411       ga.ImmigrationReplacer = ga.ImmigrationReplacerParameter.ValidValues
    1412         .OfType<MiR>()
    1413         .Single();
    1414       ga.Engine = new ParallelEngine.ParallelEngine();
    1415     }
    1416 
    1417 
    1418     private void RunAlgorithm(IAlgorithm a) {
    1419       var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
    1420       Exception ex = null;
    1421       a.Stopped += (src, e) => { trigger.Set(); };
    1422       a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
    1423       a.Prepare();
    1424       a.Start();
    1425       trigger.WaitOne();
    1426 
    1427       Assert.AreEqual(ex, null);
    1428     }
    1429 
    1430     private double GetDoubleResult(IAlgorithm a, string resultName) {
    1431       return ((DoubleValue)a.Results[resultName].Value).Value;
    1432     }
    1433     private int GetIntResult(IAlgorithm a, string resultName) {
    1434       return ((IntValue)a.Results[resultName].Value).Value;
    1435     }
     1280
    14361281    #endregion
    14371282  }
Note: See TracChangeset for help on using the changeset viewer.