Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SamplesUtils.cs @ 18126

Last change on this file since 18126 was 18002, checked in by jkarder, 3 years ago

#3017: missed null-conditional operator

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