Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SamplesUtils.cs @ 17399

Last change on this file since 17399 was 13237, checked in by gkronber, 9 years ago

#2478: created a sample unit test for OSES

File size: 8.9 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Algorithms.ALPS;
5using HeuristicLab.Algorithms.EvolutionStrategy;
6using HeuristicLab.Algorithms.GeneticAlgorithm;
7using HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy;
8using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
9using HeuristicLab.Data;
10using HeuristicLab.Optimization;
11using HeuristicLab.Selection;
12using Microsoft.VisualStudio.TestTools.UnitTesting;
13
14namespace HeuristicLab.Tests {
15  public static class SamplesUtils {
16    public const string SamplesDirectory = @"Samples\";
17    public const string SampleFileExtension = ".hl";
18
19    public static void RunAlgorithm(IAlgorithm a) {
20      var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
21      Exception ex = null;
22      a.Stopped += (src, e) => { trigger.Set(); };
23      a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
24      a.Prepare();
25      a.Start();
26      trigger.WaitOne();
27
28      Assert.AreEqual(ex, null);
29    }
30
31    public static double GetDoubleResult(IAlgorithm a, string resultName) {
32      return ((DoubleValue)a.Results[resultName].Value).Value;
33    }
34
35    public static int GetIntResult(IAlgorithm a, string resultName) {
36      return ((IntValue)a.Results[resultName].Value).Value;
37    }
38
39    public static void ConfigureEvolutionStrategyParameters<R, M, SC, SR, SM>(EvolutionStrategy es, int popSize, int children, int parentsPerChild, int maxGens, bool plusSelection)
40      where R : ICrossover
41      where M : IManipulator
42      where SC : IStrategyParameterCreator
43      where SR : IStrategyParameterCrossover
44      where SM : IStrategyParameterManipulator {
45      es.PopulationSize.Value = popSize;
46      es.Children.Value = children;
47      es.ParentsPerChild.Value = parentsPerChild;
48      es.MaximumGenerations.Value = maxGens;
49      es.PlusSelection.Value = false;
50
51      es.Seed.Value = 0;
52      es.SetSeedRandomly.Value = true;
53
54      es.Recombinator = es.RecombinatorParameter.ValidValues
55        .OfType<R>()
56        .Single();
57
58      es.Mutator = es.MutatorParameter.ValidValues
59        .OfType<M>()
60        .Single();
61
62      es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues
63        .OfType<SC>()
64        .Single();
65      es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues
66        .OfType<SR>()
67        .Single();
68      es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues
69        .OfType<SM>()
70        .Single();
71      es.Engine = new ParallelEngine.ParallelEngine();
72    }
73
74    public static void ConfigureOffspringSelectionEvolutionStrategyParameters<R, M, SC, SR, SM>(OffspringSelectionEvolutionStrategy es, int popSize,
75      double successRatio, double comparisonFactor, double maxSelPres, int parentsPerChild, int maxGens, bool plusSelection)
76      where R : ICrossover
77      where M : IManipulator
78      where SC : IStrategyParameterCreator
79      where SR : IStrategyParameterCrossover
80      where SM : IStrategyParameterManipulator {
81      es.PopulationSize.Value = popSize;
82      es.ComparisonFactor.Value = comparisonFactor;
83      es.SuccessRatio.Value = successRatio;
84      es.MaximumSelectionPressure.Value = maxSelPres;
85      es.ParentsPerChild.Value = parentsPerChild;
86      es.SelectedParents.Value = popSize * parentsPerChild;
87      es.MaximumGenerations.Value = maxGens;
88      es.PlusSelection.Value = false;
89
90      es.Seed.Value = 0;
91      es.SetSeedRandomly.Value = true;
92
93      es.Recombinator = es.RecombinatorParameter.ValidValues
94        .OfType<R>()
95        .Single();
96
97      es.Mutator = es.MutatorParameter.ValidValues
98        .OfType<M>()
99        .Single();
100
101      es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues
102        .OfType<SC>()
103        .Single();
104      es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues
105        .OfType<SR>()
106        .Single();
107      es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues
108        .OfType<SM>()
109        .Single();
110      es.Engine = new ParallelEngine.ParallelEngine();
111    }
112
113    public static void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0)
114      where S : ISelector
115      where C : ICrossover
116      where M : IManipulator {
117      ga.Elites.Value = elites;
118      ga.MaximumGenerations.Value = maxGens;
119      ga.MutationProbability.Value = mutationRate;
120      ga.PopulationSize.Value = popSize;
121      ga.Seed.Value = 0;
122      ga.SetSeedRandomly.Value = true;
123      ga.Selector = ga.SelectorParameter.ValidValues
124        .OfType<S>()
125        .First();
126
127      ga.Crossover = ga.CrossoverParameter.ValidValues
128        .OfType<C>()
129        .First();
130
131      ga.Mutator = ga.MutatorParameter.ValidValues
132        .OfType<M>()
133        .First();
134
135      var tSelector = ga.Selector as TournamentSelector;
136      if (tSelector != null) {
137        tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
138      }
139      ga.Engine = new ParallelEngine.ParallelEngine();
140    }
141
142    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)
143      where S : ISelector
144      where C : ICrossover
145      where M : IManipulator {
146      ga.Elites.Value = elites;
147      ga.MaximumGenerations.Value = maxGens;
148      ga.MutationProbability.Value = mutationRate;
149      ga.PopulationSize.Value = popSize;
150      ga.MaximumSelectionPressure.Value = maxSelPres;
151      ga.Seed.Value = 0;
152      ga.SetSeedRandomly.Value = true;
153      ga.ComparisonFactorLowerBound.Value = 1;
154      ga.ComparisonFactorUpperBound.Value = 1;
155
156      ga.Selector = ga.SelectorParameter.ValidValues
157        .OfType<S>()
158        .First();
159
160      ga.Crossover = ga.CrossoverParameter.ValidValues
161        .OfType<C>()
162        .First();
163
164      ga.Mutator = ga.MutatorParameter.ValidValues
165        .OfType<M>()
166        .First();
167
168      var tSelector = ga.Selector as TournamentSelector;
169      if (tSelector != null) {
170        tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
171      }
172      ga.Engine = new ParallelEngine.ParallelEngine();
173    }
174
175    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)
176      where S : ISelector
177      where C : ICrossover
178      where M : IManipulator
179      where Mi : IMigrator
180      where MiS : ISelector
181      where MiR : IReplacer {
182      ga.Elites.Value = elites;
183      ga.MaximumGenerations.Value = maxGens;
184      ga.MutationProbability.Value = mutationRate;
185      ga.PopulationSize.Value = popSize;
186      ga.NumberOfIslands.Value = numberOfIslands;
187      ga.MigrationInterval.Value = migrationInterval;
188      ga.MigrationRate.Value = migrationRate;
189      ga.Seed.Value = 0;
190      ga.SetSeedRandomly.Value = true;
191      ga.Selector = ga.SelectorParameter.ValidValues
192        .OfType<S>()
193        .Single();
194
195      ga.Crossover = ga.CrossoverParameter.ValidValues
196        .OfType<C>()
197        .Single();
198
199      ga.Mutator = ga.MutatorParameter.ValidValues
200        .OfType<M>()
201        .Single();
202      ga.Migrator = ga.MigratorParameter.ValidValues
203        .OfType<Mi>()
204        .Single();
205      ga.EmigrantsSelector = ga.EmigrantsSelectorParameter.ValidValues
206        .OfType<MiS>()
207        .Single();
208      ga.ImmigrationReplacer = ga.ImmigrationReplacerParameter.ValidValues
209        .OfType<MiR>()
210        .Single();
211      ga.Engine = new ParallelEngine.ParallelEngine();
212    }
213
214    public static void ConfigureAlpsGeneticAlgorithmParameters<S, C, M>(AlpsGeneticAlgorithm ga, int numberOfLayers, int popSize, double mutationRate, int elites, bool plusSelection, AgingScheme agingScheme, int ageGap, double ageInheritance, int maxGens)
215      where S : ISelector
216      where C : ICrossover
217      where M : IManipulator {
218      ga.Seed.Value = 0;
219      ga.SetSeedRandomly.Value = true;
220
221      ga.NumberOfLayers.Value = numberOfLayers;
222      ga.PopulationSize.Value = popSize;
223
224      ga.Selector = ga.SelectorParameter.ValidValues.OfType<S>().Single();
225      ga.Crossover = ga.CrossoverParameter.ValidValues.OfType<C>().Single();
226      ga.Mutator = ga.MutatorParameter.ValidValues.OfType<M>().Single();
227      ga.MutationProbability.Value = mutationRate;
228      ga.Elites.Value = elites;
229      ga.PlusSelection = plusSelection;
230
231      ga.AgingScheme = new EnumValue<AgingScheme>(agingScheme);
232      ga.AgeGap.Value = ageGap;
233      ga.AgeInheritance.Value = ageInheritance;
234
235      ga.MaximumGenerations = maxGens;
236
237      ga.Engine = new ParallelEngine.ParallelEngine();
238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.