Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SamplesUtils.cs @ 11868

Last change on this file since 11868 was 11051, checked in by mkommend, 10 years ago

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

File size: 6.1 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Algorithms.EvolutionStrategy;
5using HeuristicLab.Algorithms.GeneticAlgorithm;
6using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
7using HeuristicLab.Data;
8using HeuristicLab.Optimization;
9using HeuristicLab.Selection;
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11
12namespace HeuristicLab.Tests {
13  public static class SamplesUtils {
14    public const string Directory = @"Samples\";
15
16    public static void RunAlgorithm(IAlgorithm a) {
17      var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
18      Exception ex = null;
19      a.Stopped += (src, e) => { trigger.Set(); };
20      a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
21      a.Prepare();
22      a.Start();
23      trigger.WaitOne();
24
25      Assert.AreEqual(ex, null);
26    }
27
28    public static double GetDoubleResult(IAlgorithm a, string resultName) {
29      return ((DoubleValue)a.Results[resultName].Value).Value;
30    }
31
32    public static int GetIntResult(IAlgorithm a, string resultName) {
33      return ((IntValue)a.Results[resultName].Value).Value;
34    }
35
36    public static void ConfigureEvolutionStrategyParameters<R, M, SC, SR, SM>(EvolutionStrategy es, int popSize, int children, int parentsPerChild, int maxGens, bool plusSelection)
37      where R : ICrossover
38      where M : IManipulator
39      where SC : IStrategyParameterCreator
40      where SR : IStrategyParameterCrossover
41      where SM : IStrategyParameterManipulator {
42      es.PopulationSize.Value = popSize;
43      es.Children.Value = children;
44      es.ParentsPerChild.Value = parentsPerChild;
45      es.MaximumGenerations.Value = maxGens;
46      es.PlusSelection.Value = false;
47
48      es.Seed.Value = 0;
49      es.SetSeedRandomly.Value = true;
50
51      es.Recombinator = es.RecombinatorParameter.ValidValues
52        .OfType<R>()
53        .Single();
54
55      es.Mutator = es.MutatorParameter.ValidValues
56        .OfType<M>()
57        .Single();
58
59      es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues
60        .OfType<SC>()
61        .Single();
62      es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues
63        .OfType<SR>()
64        .Single();
65      es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues
66        .OfType<SM>()
67        .Single();
68      es.Engine = new ParallelEngine.ParallelEngine();
69    }
70
71    public static void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0)
72      where S : ISelector
73      where C : ICrossover
74      where M : IManipulator {
75      ga.Elites.Value = elites;
76      ga.MaximumGenerations.Value = maxGens;
77      ga.MutationProbability.Value = mutationRate;
78      ga.PopulationSize.Value = popSize;
79      ga.Seed.Value = 0;
80      ga.SetSeedRandomly.Value = true;
81      ga.Selector = ga.SelectorParameter.ValidValues
82        .OfType<S>()
83        .First();
84
85      ga.Crossover = ga.CrossoverParameter.ValidValues
86        .OfType<C>()
87        .First();
88
89      ga.Mutator = ga.MutatorParameter.ValidValues
90        .OfType<M>()
91        .First();
92
93      var tSelector = ga.Selector as TournamentSelector;
94      if (tSelector != null) {
95        tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
96      }
97      ga.Engine = new ParallelEngine.ParallelEngine();
98    }
99
100    public static void ConfigureOsGeneticAlgorithmParameters<S, C, M>(OffspringSelectionGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate = 0.05, double maxSelPres = 100, int tournGroupSize = 0)
101      where S : ISelector
102      where C : ICrossover
103      where M : IManipulator {
104      ga.Elites.Value = elites;
105      ga.MaximumGenerations.Value = maxGens;
106      ga.MutationProbability.Value = mutationRate;
107      ga.PopulationSize.Value = popSize;
108      ga.MaximumSelectionPressure.Value = maxSelPres;
109      ga.Seed.Value = 0;
110      ga.SetSeedRandomly.Value = true;
111      ga.ComparisonFactorLowerBound.Value = 1;
112      ga.ComparisonFactorUpperBound.Value = 1;
113
114      ga.Selector = ga.SelectorParameter.ValidValues
115        .OfType<S>()
116        .First();
117
118      ga.Crossover = ga.CrossoverParameter.ValidValues
119        .OfType<C>()
120        .First();
121
122      ga.Mutator = ga.MutatorParameter.ValidValues
123        .OfType<M>()
124        .First();
125
126      var tSelector = ga.Selector as TournamentSelector;
127      if (tSelector != null) {
128        tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
129      }
130      ga.Engine = new ParallelEngine.ParallelEngine();
131    }
132
133    public static void ConfigureIslandGeneticAlgorithmParameters<S, C, M, Mi, MiS, MiR>(IslandGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int numberOfIslands, int migrationInterval, double migrationRate)
134      where S : ISelector
135      where C : ICrossover
136      where M : IManipulator
137      where Mi : IMigrator
138      where MiS : ISelector
139      where MiR : IReplacer {
140      ga.Elites.Value = elites;
141      ga.MaximumGenerations.Value = maxGens;
142      ga.MutationProbability.Value = mutationRate;
143      ga.PopulationSize.Value = popSize;
144      ga.NumberOfIslands.Value = numberOfIslands;
145      ga.MigrationInterval.Value = migrationInterval;
146      ga.MigrationRate.Value = migrationRate;
147      ga.Seed.Value = 0;
148      ga.SetSeedRandomly.Value = true;
149      ga.Selector = ga.SelectorParameter.ValidValues
150        .OfType<S>()
151        .Single();
152
153      ga.Crossover = ga.CrossoverParameter.ValidValues
154        .OfType<C>()
155        .Single();
156
157      ga.Mutator = ga.MutatorParameter.ValidValues
158        .OfType<M>()
159        .Single();
160      ga.Migrator = ga.MigratorParameter.ValidValues
161        .OfType<Mi>()
162        .Single();
163      ga.EmigrantsSelector = ga.EmigrantsSelectorParameter.ValidValues
164        .OfType<MiS>()
165        .Single();
166      ga.ImmigrationReplacer = ga.ImmigrationReplacerParameter.ValidValues
167        .OfType<MiR>()
168        .Single();
169      ga.Engine = new ParallelEngine.ParallelEngine();
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.