Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12136 was 11907, checked in by jkarder, 10 years ago

#2211: merged r11450, r11466, r11483, r11514, r11515 and r11890 into stable
#2234: merged r11308, r11309, r11326, r11337, r11340, r11339, r11342, r11361, r11427, r11447, r11464, r11542, r11544, r11545, r11547, r11548 into stable
#2239: merged r11437, r11439 and r11472 into stable
#2262: merged r11436, r11440, r11471, r11474, r11477, r11479, r11480, r11605, r11657, r11721, r11734, r11735, r11787, r11788, r11789 and r11826 into stable

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