Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/Test/RunSequentialSolverFuncApproxExperiments.cs @ 13401

Last change on this file since 13401 was 11981, checked in by gkronber, 10 years ago

#2283: cleanup and included HeuristicLab.dlls to create a self-contained branch

File size: 4.6 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
5using HeuristicLab.Algorithms.GeneticProgramming;
6using HeuristicLab.Algorithms.GrammaticalOptimization;
7using HeuristicLab.Problems.GrammaticalOptimization;
8using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10
11namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
12  // only supported for Artificial Ant and Symbolic Regression up to now
13
14  [TestClass]
15  public class RunSequentialSolverFuncApproximationExperiments {
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 = 20;
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          RunSeqSolvFuncApproxForProblem(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 = 20;
59      int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
60      foreach (var instanceFactory in instanceFactories) {
61        foreach (var conf in GenerateConfigurations(instanceFactory, nReps, maxSizes)) {
62          RunSeqSolvFuncApproxForProblem(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 RunSeqSolvFuncApproxForProblem(
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 GenericFunctionApproximationGrammarPolicy(problem, true));
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}
Note: See TracBrowser for help on using the repository browser.