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