1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Algorithms.Bandits.BanditPolicies;
|
---|
5 | using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
|
---|
6 | using HeuristicLab.Algorithms.GeneticProgramming;
|
---|
7 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
8 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
9 | using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
|
---|
10 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
|
---|
13 |
|
---|
14 | [TestClass]
|
---|
15 | public class RunMctsExperiments {
|
---|
16 | private readonly static int randSeed = 31415;
|
---|
17 |
|
---|
18 | internal class Configuration {
|
---|
19 | public ISymbolicExpressionTreeProblem Problem;
|
---|
20 | public int MaxSize;
|
---|
21 | public int RandSeed;
|
---|
22 |
|
---|
23 | public override string ToString() {
|
---|
24 | return string.Format("{0} {1} {2}", RandSeed, Problem, MaxSize);
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | #region artificial ant
|
---|
30 | [TestMethod]
|
---|
31 | public void RunSeqSolvFuncApproxArtificialAntProblem() {
|
---|
32 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
33 | {
|
---|
34 | (randSeed) => (ISymbolicExpressionTreeProblem) new SantaFeAntProblem(),
|
---|
35 | };
|
---|
36 |
|
---|
37 | var maxSizes = new int[] { 17 }; // size of sequential representation is 17
|
---|
38 | int nReps = 30;
|
---|
39 | int maxIterations = 100000; // randomsearch finds the optimum almost always for 100000 evals
|
---|
40 | foreach (var instanceFactory in instanceFactories) {
|
---|
41 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
42 | RunMctsForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region symb-reg-poly-10
|
---|
50 | [TestMethod]
|
---|
51 | public void RunSeqSolvFuncApproxPoly10Problem() {
|
---|
52 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
53 | {
|
---|
54 | (randSeed) => (ISymbolicExpressionTreeProblem) new SymbolicRegressionPoly10Problem(),
|
---|
55 | };
|
---|
56 |
|
---|
57 | var maxSizes = new int[] { 23 }; // size of sequential representation is 23
|
---|
58 | int nReps = 30;
|
---|
59 | int maxIterations = 200000; // sequentialsearch should find the optimum within 100000 evals
|
---|
60 | foreach (var instanceFactory in instanceFactories) {
|
---|
61 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
|
---|
62 | RunMctsForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.MaxSize);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | #region helpers
|
---|
70 | private IEnumerable<Configuration> GenerateConfigurations(Func<int, ISymbolicExpressionTreeProblem> problemFactory,
|
---|
71 | int nReps,
|
---|
72 | IEnumerable<int> maxSizes
|
---|
73 | ) {
|
---|
74 | var seedRand = new Random(randSeed);
|
---|
75 | // the problem seed is the same for all configuratons
|
---|
76 | // this guarantees that we solve the _same_ problem each time
|
---|
77 | // with different solvers and multiple repetitions
|
---|
78 | var problemSeed = randSeed;
|
---|
79 | for (int i = 0; i < nReps; i++) {
|
---|
80 | // in each repetition use the same random seed for all solver configuratons
|
---|
81 | // do nReps with different seeds for each configuration
|
---|
82 | var solverSeed = seedRand.Next();
|
---|
83 | foreach (var maxSize in maxSizes) {
|
---|
84 | yield return new Configuration {
|
---|
85 | MaxSize = maxSize,
|
---|
86 | Problem = problemFactory(problemSeed),
|
---|
87 | RandSeed = solverSeed
|
---|
88 | };
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static void RunMctsForProblem(
|
---|
94 | int randSeed,
|
---|
95 | IProblem problem,
|
---|
96 | int maxIters,
|
---|
97 | int maxSize
|
---|
98 | ) {
|
---|
99 | var solver = new SequentialSearch(problem, maxSize, new Random(randSeed), 0,
|
---|
100 | new GenericGrammarPolicy(problem, new UCB1Policy(), false));
|
---|
101 | var problemName = problem.GetType().Name;
|
---|
102 | var bestKnownQuality = problem.BestKnownQuality(maxSize);
|
---|
103 | RunSolver(solver, problemName, bestKnownQuality, maxIters, maxSize);
|
---|
104 | }
|
---|
105 |
|
---|
106 | private static void RunSolver(ISolver solver, string problemName, double bestKnownQuality, int maxIters, int maxSize) {
|
---|
107 | int iterations = 0;
|
---|
108 | var globalStatistics = new SentenceSetStatistics(bestKnownQuality);
|
---|
109 | var gpName = solver.GetType().Name;
|
---|
110 | solver.SolutionEvaluated += (sentence, quality) => {
|
---|
111 | iterations++;
|
---|
112 | globalStatistics.AddSentence(sentence, quality);
|
---|
113 |
|
---|
114 | if (iterations % 1000 == 0) {
|
---|
115 | Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" {3}", gpName, maxSize, problemName, globalStatistics);
|
---|
116 | }
|
---|
117 | };
|
---|
118 |
|
---|
119 | solver.Run(maxIters);
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 | }
|
---|
123 | }
|
---|