1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Algorithms.Bandits;
|
---|
8 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
9 | using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
|
---|
10 | using HeuristicLab.Algorithms.Bandits.Models;
|
---|
11 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
12 | using HeuristicLab.Common;
|
---|
13 | using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
|
---|
14 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
15 | using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy;
|
---|
16 |
|
---|
17 | namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
|
---|
18 | [TestClass]
|
---|
19 | public class TestTunedSettings {
|
---|
20 | private const int randSeed = 31415;
|
---|
21 | internal class Configuration {
|
---|
22 | public IProblem Problem;
|
---|
23 | public int MaxSize;
|
---|
24 | public int RandSeed;
|
---|
25 |
|
---|
26 | public override string ToString() {
|
---|
27 | return string.Format("{0} {1} {2}", RandSeed, Problem, MaxSize);
|
---|
28 | }
|
---|
29 | }
|
---|
30 | [TestMethod]
|
---|
31 | [Timeout(1000 * 60 * 60 * 12)] // 12 hours
|
---|
32 | public void TestAllPoliciesArtificialAnt() {
|
---|
33 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
34 |
|
---|
35 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
36 | {
|
---|
37 | (randSeed) => (ISymbolicExpressionTreeProblem)new SantaFeAntProblem(),
|
---|
38 | };
|
---|
39 |
|
---|
40 | var policyFactories = new Func<IBanditPolicy>[]
|
---|
41 | {
|
---|
42 | () => new RandomPolicy(),
|
---|
43 | () => new ActiveLearningPolicy(),
|
---|
44 | () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
45 | () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
46 | () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
47 | () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
48 | //() => new GaussianThompsonSamplingPolicy(),
|
---|
49 | () => new GaussianThompsonSamplingPolicy(true),
|
---|
50 | () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
|
---|
51 | () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
|
---|
52 | //() => new BernoulliThompsonSamplingPolicy(),
|
---|
53 | () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
|
---|
54 | () => new EpsGreedyPolicy(0.01),
|
---|
55 | () => new EpsGreedyPolicy(0.05),
|
---|
56 | () => new EpsGreedyPolicy(0.1),
|
---|
57 | () => new EpsGreedyPolicy(0.2),
|
---|
58 | () => new EpsGreedyPolicy(0.5),
|
---|
59 | () => new UCTPolicy(0.01),
|
---|
60 | () => new UCTPolicy(0.05),
|
---|
61 | () => new UCTPolicy(0.1),
|
---|
62 | () => new UCTPolicy(0.5),
|
---|
63 | () => new UCTPolicy(1),
|
---|
64 | () => new UCTPolicy(2),
|
---|
65 | () => new UCTPolicy( 5),
|
---|
66 | () => new UCTPolicy( 10),
|
---|
67 | () => new ModifiedUCTPolicy(0.01),
|
---|
68 | () => new ModifiedUCTPolicy(0.05),
|
---|
69 | () => new ModifiedUCTPolicy(0.1),
|
---|
70 | () => new ModifiedUCTPolicy(0.5),
|
---|
71 | () => new ModifiedUCTPolicy(1),
|
---|
72 | () => new ModifiedUCTPolicy(2),
|
---|
73 | () => new ModifiedUCTPolicy( 5),
|
---|
74 | () => new ModifiedUCTPolicy( 10),
|
---|
75 | () => new UCB1Policy(),
|
---|
76 | () => new UCB1TunedPolicy(),
|
---|
77 | () => new UCBNormalPolicy(),
|
---|
78 | () => new BoltzmannExplorationPolicy(1),
|
---|
79 | () => new BoltzmannExplorationPolicy(10),
|
---|
80 | () => new BoltzmannExplorationPolicy(20),
|
---|
81 | () => new BoltzmannExplorationPolicy(100),
|
---|
82 | () => new BoltzmannExplorationPolicy(200),
|
---|
83 | () => new BoltzmannExplorationPolicy(500),
|
---|
84 | () => new ChernoffIntervalEstimationPolicy( 0.01),
|
---|
85 | () => new ChernoffIntervalEstimationPolicy( 0.05),
|
---|
86 | () => new ChernoffIntervalEstimationPolicy( 0.1),
|
---|
87 | () => new ChernoffIntervalEstimationPolicy( 0.2),
|
---|
88 | () => new ThresholdAscentPolicy(5, 0.01),
|
---|
89 | () => new ThresholdAscentPolicy(5, 0.05),
|
---|
90 | () => new ThresholdAscentPolicy(5, 0.1),
|
---|
91 | () => new ThresholdAscentPolicy(5, 0.2),
|
---|
92 | () => new ThresholdAscentPolicy(10, 0.01),
|
---|
93 | () => new ThresholdAscentPolicy(10, 0.05),
|
---|
94 | () => new ThresholdAscentPolicy(10, 0.1),
|
---|
95 | () => new ThresholdAscentPolicy(10, 0.2),
|
---|
96 | () => new ThresholdAscentPolicy(50, 0.01),
|
---|
97 | () => new ThresholdAscentPolicy(50, 0.05),
|
---|
98 | () => new ThresholdAscentPolicy(50, 0.1),
|
---|
99 | () => new ThresholdAscentPolicy(50, 0.2),
|
---|
100 | () => new ThresholdAscentPolicy(100, 0.01),
|
---|
101 | () => new ThresholdAscentPolicy(100, 0.05),
|
---|
102 | () => new ThresholdAscentPolicy(100, 0.1),
|
---|
103 | () => new ThresholdAscentPolicy(100, 0.2),
|
---|
104 | () => new ThresholdAscentPolicy(500, 0.01),
|
---|
105 | () => new ThresholdAscentPolicy(500, 0.05),
|
---|
106 | () => new ThresholdAscentPolicy(500, 0.1),
|
---|
107 | () => new ThresholdAscentPolicy(500, 0.2),
|
---|
108 | () => new ThresholdAscentPolicy(5000, 0.01),
|
---|
109 | () => new ThresholdAscentPolicy(10000, 0.01),
|
---|
110 | };
|
---|
111 | var maxSizes = new int[] { 17 }; // necessary size for ant programm
|
---|
112 | int nReps = 20;
|
---|
113 | int maxIterations = 100000;
|
---|
114 | foreach (var instanceFactory in instanceFactories) {
|
---|
115 | var sumBestQ = 0.0;
|
---|
116 | var sumItersToBest = 0;
|
---|
117 | double fractionSolved = 0.0;
|
---|
118 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
119 | foreach (var policy in policyFactories) {
|
---|
120 | var prob = conf.Problem;
|
---|
121 | var maxLen = conf.MaxSize;
|
---|
122 | var rand = new Random(conf.RandSeed);
|
---|
123 |
|
---|
124 | var solver = new SequentialSearch(prob, maxLen, rand, 0, new GenericGrammarPolicy(prob, policy(), true));
|
---|
125 | var problemName = prob.GetType().Name;
|
---|
126 | var policyName = policy().ToString();
|
---|
127 | double bestQ; int itersToBest;
|
---|
128 | RunSolver(solver, problemName, policyName, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
|
---|
129 | sumBestQ += bestQ;
|
---|
130 | sumItersToBest += itersToBest;
|
---|
131 | if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | // Assert.AreEqual(0.85, fractionSolved, 1E-6);
|
---|
135 | // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
|
---|
136 | // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | [TestMethod]
|
---|
141 | [Timeout(1000 * 60 * 60 * 30)] // 30 hours
|
---|
142 | public void TestAllPoliciesPoly10() {
|
---|
143 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
144 |
|
---|
145 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
146 | {
|
---|
147 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionPoly10Problem(),
|
---|
148 | };
|
---|
149 |
|
---|
150 | var policyFactories = new Func<IBanditPolicy>[]
|
---|
151 | {
|
---|
152 | () => new RandomPolicy(),
|
---|
153 | () => new ActiveLearningPolicy(),
|
---|
154 | () => new EpsGreedyPolicy(0.01, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
155 | () => new EpsGreedyPolicy(0.05, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
156 | () => new EpsGreedyPolicy(0.1, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
157 | () => new EpsGreedyPolicy(0.2, (aInfo)=> aInfo.MaxReward, "max"),
|
---|
158 | //() => new GaussianThompsonSamplingPolicy(),
|
---|
159 | () => new GaussianThompsonSamplingPolicy(true),
|
---|
160 | () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1)),
|
---|
161 | () => new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)),
|
---|
162 | //() => new BernoulliThompsonSamplingPolicy(),
|
---|
163 | () => new GenericThompsonSamplingPolicy(new BernoulliModel(1, 1)),
|
---|
164 | () => new EpsGreedyPolicy(0.01),
|
---|
165 | () => new EpsGreedyPolicy(0.05),
|
---|
166 | () => new EpsGreedyPolicy(0.1),
|
---|
167 | () => new EpsGreedyPolicy(0.2),
|
---|
168 | () => new EpsGreedyPolicy(0.5),
|
---|
169 | () => new UCTPolicy(0.01),
|
---|
170 | () => new UCTPolicy(0.05),
|
---|
171 | () => new UCTPolicy(0.1),
|
---|
172 | () => new UCTPolicy(0.5),
|
---|
173 | () => new UCTPolicy(1),
|
---|
174 | () => new UCTPolicy(2),
|
---|
175 | () => new UCTPolicy( 5),
|
---|
176 | () => new UCTPolicy( 10),
|
---|
177 | () => new ModifiedUCTPolicy(0.01),
|
---|
178 | () => new ModifiedUCTPolicy(0.05),
|
---|
179 | () => new ModifiedUCTPolicy(0.1),
|
---|
180 | () => new ModifiedUCTPolicy(0.5),
|
---|
181 | () => new ModifiedUCTPolicy(1),
|
---|
182 | () => new ModifiedUCTPolicy(2),
|
---|
183 | () => new ModifiedUCTPolicy( 5),
|
---|
184 | () => new ModifiedUCTPolicy( 10),
|
---|
185 | () => new UCB1Policy(),
|
---|
186 | () => new UCB1TunedPolicy(),
|
---|
187 | () => new UCBNormalPolicy(),
|
---|
188 | () => new BoltzmannExplorationPolicy(1),
|
---|
189 | () => new BoltzmannExplorationPolicy(10),
|
---|
190 | () => new BoltzmannExplorationPolicy(20),
|
---|
191 | () => new BoltzmannExplorationPolicy(100),
|
---|
192 | () => new BoltzmannExplorationPolicy(200),
|
---|
193 | () => new BoltzmannExplorationPolicy(500),
|
---|
194 | () => new ChernoffIntervalEstimationPolicy( 0.01),
|
---|
195 | () => new ChernoffIntervalEstimationPolicy( 0.05),
|
---|
196 | () => new ChernoffIntervalEstimationPolicy( 0.1),
|
---|
197 | () => new ChernoffIntervalEstimationPolicy( 0.2),
|
---|
198 | () => new ThresholdAscentPolicy(5, 0.01),
|
---|
199 | () => new ThresholdAscentPolicy(5, 0.05),
|
---|
200 | () => new ThresholdAscentPolicy(5, 0.1),
|
---|
201 | () => new ThresholdAscentPolicy(5, 0.2),
|
---|
202 | () => new ThresholdAscentPolicy(10, 0.01),
|
---|
203 | () => new ThresholdAscentPolicy(10, 0.05),
|
---|
204 | () => new ThresholdAscentPolicy(10, 0.1),
|
---|
205 | () => new ThresholdAscentPolicy(10, 0.2),
|
---|
206 | () => new ThresholdAscentPolicy(50, 0.01),
|
---|
207 | () => new ThresholdAscentPolicy(50, 0.05),
|
---|
208 | () => new ThresholdAscentPolicy(50, 0.1),
|
---|
209 | () => new ThresholdAscentPolicy(50, 0.2),
|
---|
210 | () => new ThresholdAscentPolicy(100, 0.01),
|
---|
211 | () => new ThresholdAscentPolicy(100, 0.05),
|
---|
212 | () => new ThresholdAscentPolicy(100, 0.1),
|
---|
213 | () => new ThresholdAscentPolicy(100, 0.2),
|
---|
214 | () => new ThresholdAscentPolicy(500, 0.01),
|
---|
215 | () => new ThresholdAscentPolicy(500, 0.05),
|
---|
216 | () => new ThresholdAscentPolicy(500, 0.1),
|
---|
217 | () => new ThresholdAscentPolicy(500, 0.2),
|
---|
218 | () => new ThresholdAscentPolicy(5000, 0.01),
|
---|
219 | () => new ThresholdAscentPolicy(10000, 0.01),
|
---|
220 | };
|
---|
221 | var maxSizes = new int[] { 23 }; // necessary size symb reg poly 10
|
---|
222 | int nReps = 20;
|
---|
223 | int maxIterations = 100000;
|
---|
224 | foreach (var instanceFactory in instanceFactories) {
|
---|
225 | var sumBestQ = 0.0;
|
---|
226 | var sumItersToBest = 0;
|
---|
227 | double fractionSolved = 0.0;
|
---|
228 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
229 | foreach (var policy in policyFactories) {
|
---|
230 | var prob = conf.Problem;
|
---|
231 | var maxLen = conf.MaxSize;
|
---|
232 | var rand = new Random(conf.RandSeed);
|
---|
233 |
|
---|
234 | var solver = new SequentialSearch(prob, maxLen, rand, 0, new GenericGrammarPolicy(prob, policy(), true));
|
---|
235 | var problemName = prob.GetType().Name;
|
---|
236 | var policyName = policy().ToString();
|
---|
237 | double bestQ; int itersToBest;
|
---|
238 | RunSolver(solver, problemName, policyName, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
|
---|
239 | sumBestQ += bestQ;
|
---|
240 | sumItersToBest += itersToBest;
|
---|
241 | if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | // Assert.AreEqual(0.85, fractionSolved, 1E-6);
|
---|
245 | // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
|
---|
246 | // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | [TestMethod]
|
---|
251 | [Timeout(1000 * 60 * 60 * 30)] // 30 hours
|
---|
252 | public void TestAllSymbolicRegression() {
|
---|
253 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
254 |
|
---|
255 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
256 | {
|
---|
257 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Nguyen F7", true),
|
---|
258 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Keijzer 6", true),
|
---|
259 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Vladislavleva-4", true),
|
---|
260 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Spatial", true),
|
---|
261 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Friedman - II", true),
|
---|
262 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Tower", true),
|
---|
263 | };
|
---|
264 |
|
---|
265 | var policyFactories = new Func<IBanditPolicy>[]
|
---|
266 | {
|
---|
267 | () => new UCTPolicy(0.05),
|
---|
268 | () => new UCTPolicy(0.1),
|
---|
269 | () => new ModifiedUCTPolicy(0.01),
|
---|
270 | () => new ModifiedUCTPolicy(0.05),
|
---|
271 | () => new UCB1Policy(),
|
---|
272 | () => new UCB1TunedPolicy(),
|
---|
273 | };
|
---|
274 | var maxSizes = new int[] { 20 }; // default limit for all problems
|
---|
275 | int nReps = 20;
|
---|
276 | int maxIterations = 10000;
|
---|
277 | foreach (var instanceFactory in instanceFactories) {
|
---|
278 | var sumBestQ = 0.0;
|
---|
279 | var sumItersToBest = 0;
|
---|
280 | double fractionSolved = 0.0;
|
---|
281 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
282 | foreach (var policy in policyFactories) {
|
---|
283 | var prob = conf.Problem;
|
---|
284 | var maxLen = conf.MaxSize;
|
---|
285 | var rand = new Random(conf.RandSeed);
|
---|
286 |
|
---|
287 | var solver = new SequentialSearch(prob, maxLen, rand, 0, new GenericGrammarPolicy(prob, policy(), true));
|
---|
288 | var problemName = prob.Name;
|
---|
289 | var policyName = policy().ToString();
|
---|
290 | double bestQ; int itersToBest;
|
---|
291 | RunSolver(solver, problemName, policyName, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
|
---|
292 | sumBestQ += bestQ;
|
---|
293 | sumItersToBest += itersToBest;
|
---|
294 | if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | // Assert.AreEqual(0.85, fractionSolved, 1E-6);
|
---|
298 | // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
|
---|
299 | // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | [TestMethod]
|
---|
304 | [Timeout(1000 * 60 * 60 * 12)] // 12 hours
|
---|
305 | // this configuration worked especially well in the experiments
|
---|
306 | public void TestPoly10WithOutConstantOpt() {
|
---|
307 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
308 |
|
---|
309 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
310 | {
|
---|
311 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionPoly10Problem(),
|
---|
312 | };
|
---|
313 |
|
---|
314 | var maxSizes = new int[] { 23 };
|
---|
315 | int nReps = 20;
|
---|
316 | int maxIterations = 100000;
|
---|
317 | foreach (var instanceFactory in instanceFactories) {
|
---|
318 | var sumBestQ = 0.0;
|
---|
319 | var sumItersToBest = 0;
|
---|
320 | double fractionSolved = 0.0;
|
---|
321 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
322 | var prob = conf.Problem;
|
---|
323 | var maxLen = conf.MaxSize;
|
---|
324 | var rand = new Random(conf.RandSeed);
|
---|
325 |
|
---|
326 | var solver = new SequentialSearch(prob, maxLen, rand, 0,
|
---|
327 | new GenericFunctionApproximationGrammarPolicy(prob, true));
|
---|
328 |
|
---|
329 | var problemName = prob.GetType().Name;
|
---|
330 | double bestQ; int itersToBest;
|
---|
331 | RunSolver(solver, problemName, string.Empty, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
|
---|
332 | sumBestQ += bestQ;
|
---|
333 | sumItersToBest += itersToBest;
|
---|
334 | if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
|
---|
335 | }
|
---|
336 | // Assert.AreEqual(0.85, fractionSolved, 1E-6);
|
---|
337 | // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
|
---|
338 | // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | [TestMethod]
|
---|
343 | [Timeout(1000 * 60 * 60 * 12)] // 12 hours
|
---|
344 | // this configuration worked especially well in the experiments
|
---|
345 | public void TestPoly10WithConstantOpt() {
|
---|
346 | CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
---|
347 |
|
---|
348 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
349 | {
|
---|
350 | (randSeed) => (ISymbolicExpressionTreeProblem)new SymbolicRegressionProblem(new Random(randSeed), "Poly-10", true ),
|
---|
351 | };
|
---|
352 |
|
---|
353 | var maxSizes = new int[] { 23 };
|
---|
354 | int nReps = 20;
|
---|
355 | int maxIterations = 100000;
|
---|
356 | foreach (var instanceFactory in instanceFactories) {
|
---|
357 | var sumBestQ = 0.0;
|
---|
358 | var sumItersToBest = 0;
|
---|
359 | double fractionSolved = 0.0;
|
---|
360 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
361 | var prob = conf.Problem;
|
---|
362 | var maxLen = conf.MaxSize;
|
---|
363 | var rand = new Random(conf.RandSeed);
|
---|
364 |
|
---|
365 | var solver = new SequentialSearch(prob, maxLen, rand, 0,
|
---|
366 | new GenericFunctionApproximationGrammarPolicy(prob, true));
|
---|
367 |
|
---|
368 | var problemName = prob.GetType().Name;
|
---|
369 | double bestQ; int itersToBest;
|
---|
370 | RunSolver(solver, problemName, string.Empty, 1.0, maxIterations, maxLen, out bestQ, out itersToBest);
|
---|
371 | sumBestQ += bestQ;
|
---|
372 | sumItersToBest += itersToBest;
|
---|
373 | if (bestQ.IsAlmost(1.0)) fractionSolved += 1.0 / nReps;
|
---|
374 | }
|
---|
375 | // Assert.AreEqual(0.85, fractionSolved, 1E-6);
|
---|
376 | // Assert.AreEqual(0.99438202247191, sumBestQ / nReps, 1E-6);
|
---|
377 | // Assert.AreEqual(5461.7, sumItersToBest / (double)nReps, 1E-6);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | private IEnumerable<Configuration> GenerateConfigurations(Func<int, ISymbolicExpressionTreeProblem> problemFactory,
|
---|
382 | int nReps,
|
---|
383 | IEnumerable<int> maxSizes
|
---|
384 | ) {
|
---|
385 | var seedRand = new Random(randSeed);
|
---|
386 | // the problem seed is the same for all configuratons
|
---|
387 | // this guarantees that we solve the _same_ problem each time
|
---|
388 | // with different solvers and multiple repetitions
|
---|
389 | var problemSeed = randSeed;
|
---|
390 | for (int i = 0; i < nReps; i++) {
|
---|
391 | // in each repetition use the same random seed for all solver configuratons
|
---|
392 | // do nReps with different seeds for each configuration
|
---|
393 | var solverSeed = seedRand.Next();
|
---|
394 | foreach (var maxSize in maxSizes) {
|
---|
395 | yield return new Configuration {
|
---|
396 | MaxSize = maxSize,
|
---|
397 | Problem = problemFactory(problemSeed),
|
---|
398 | RandSeed = solverSeed
|
---|
399 | };
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 |
|
---|
406 |
|
---|
407 | private static void RunSolver(ISolver solver, string problemName, string policyName, double bestKnownQuality, int maxIters, int maxSize,
|
---|
408 | out double bestQ, out int itersToBest) {
|
---|
409 | int iterations = 0;
|
---|
410 | var globalStatistics = new SentenceSetStatistics(bestKnownQuality);
|
---|
411 | var solverName = solver.GetType().Name;
|
---|
412 | double bestQuality = double.NegativeInfinity;
|
---|
413 | int iterationsToBest = -1;
|
---|
414 | solver.SolutionEvaluated += (sentence, quality) => {
|
---|
415 | iterations++;
|
---|
416 | globalStatistics.AddSentence(sentence, quality);
|
---|
417 | if (quality > bestQuality) {
|
---|
418 | bestQuality = quality;
|
---|
419 | iterationsToBest = iterations;
|
---|
420 | }
|
---|
421 | if (iterations % 1000 == 0) {
|
---|
422 | Console.WriteLine("\"{0,25}\" \"{1,25}\" {2} \"{3,25}\" {4}", solverName, policyName, maxSize, problemName, globalStatistics);
|
---|
423 | }
|
---|
424 | };
|
---|
425 |
|
---|
426 |
|
---|
427 | solver.Run(maxIters);
|
---|
428 |
|
---|
429 | bestQ = bestQuality;
|
---|
430 | itersToBest = iterationsToBest;
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|