Changeset 10425


Ignore:
Timestamp:
01/29/14 16:37:50 (6 years ago)
Author:
gkronber
Message:

#2026 support for max depth in brute force solver

Location:
branches/HeuristicLab.Problems.GPDL/CodeGenerator
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/BruteForceCodeGen.cs

    r10400 r10425  
    1313namespace ?PROBLEMNAME? {
    1414  public sealed class ?IDENT?Solver {
     15    private static int maxDepth = 20;
    1516
    1617    private readonly ?IDENT?Problem problem;
     
    7374
    7475    public static void Main(string[] args) {
     76      if(args.Length > 0) ParseArguments(args);
    7577      var problem = new ?IDENT?Problem();
    7678      var solver = new ?IDENT?Solver(problem);
    7779      solver.Start();
    7880    }
     81    private static void ParseArguments(string[] args) {
     82      var maxDepthRegex = new Regex(@""--maxDepth=(?<d>.+)"");
     83
     84      var helpRegex = new Regex(@""--help|/\?"");
     85     
     86      foreach(var arg in args) {
     87        var maxDepthMatch = maxDepthRegex.Match(arg);
     88        var helpMatch = helpRegex.Match(arg);
     89        if(helpMatch.Success) {
     90          PrintUsage(); Environment.Exit(0);
     91        } else if(maxDepthMatch.Success) {
     92           maxDepth = int.Parse(maxDepthMatch.Groups[""d""].Captures[0].Value, System.Globalization.CultureInfo.InvariantCulture);
     93           if(maxDepth < 1 || maxDepth > 100) throw new ArgumentException(""max depth must lie in range [1 ... 100]"");
     94        } else {
     95           Console.WriteLine(""Unknown switch {0}"", arg); PrintUsage(); Environment.Exit(0);
     96        }
     97      }
     98    }
     99    private static void PrintUsage() {
     100      Console.WriteLine(""Find a solution using brute force tree search."");
     101      Console.WriteLine();
     102      Console.WriteLine(""Parameters:"");
     103      Console.WriteLine(""\t--maxDepth=<depth>\tSets the maximal depth of sampled trees [Default: 20]"");
     104      Console.WriteLine(""\t--help\tPrints this help text."");
     105    }
     106
    79107
    80108    public ?IDENT?Solver(?IDENT?Problem problem) {
     
    84112
    85113    private void Start() {
    86       const int MAX_SECONDS = 100000;
    87114      var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity;
    88115      int n = 0;
     
    90117      long sumSize = 0;
    91118      var sumF = 0.0;
    92       var terminationTimer = new Stopwatch();
    93119      var sw = new System.Diagnostics.Stopwatch();
    94120      sw.Start();
    95       terminationTimer.Start();
    96       for(int depth = 1; terminationTimer.Elapsed.TotalSeconds < MAX_SECONDS; depth ++) {
     121      for(int depth = 1; depth < maxDepth; depth ++) {
    97122        Console.WriteLine(""Depth {0}:"", depth);
    98123        foreach(var t in GenerateTrees(0, depth)) {
    99124          var f = problem.Evaluate(t);
     125          // t.PrintTree(0); Console.WriteLine();
    100126          var size = t.GetSize();
    101127          sumSize += size;
     
    104130          sumF += f;
    105131          if (problem.IsBetter(f, bestF)) {
    106             t.PrintTree(0); Console.WriteLine();
    107132            bestF = f;
    108133            t.PrintTree(0); Console.WriteLine();
     
    111136          if (n % 1000 == 0) {
    112137            sw.Stop();
    113             Console.WriteLine(""{6,5:0.0s}: {0}\tbest: {1:0.000}\t(avg: {2:0.000})\t(avg size: {3:0.0})\t(avg. depth: {4:0.0})\t({5:0.00} sols/ms)"",
    114                               n, bestF, sumF/1000.0, sumSize/1000.0, sumDepth/1000.0, 1000.0 / sw.ElapsedMilliseconds, terminationTimer.Elapsed.TotalSeconds);
     138            Console.WriteLine(""{0}\tbest: {1:0.000}\t(avg: {2:0.000})\t(avg size: {3:0.0})\t(avg. depth: {4:0.0})\t({5:0.00} sols/ms)"",
     139                              n, bestF, sumF/1000.0, sumSize/1000.0, sumDepth/1000.0, 1000.0 / sw.ElapsedMilliseconds);
    115140            sumSize = 0;
    116141            sumDepth = 0;
    117142            sumF = 0.0;
    118143            sw.Restart();
    119             if(terminationTimer.Elapsed.TotalSeconds >= MAX_SECONDS) break;
    120144          }
    121145        }
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs

    r10424 r10425  
    153153    private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) {
    154154      var grammar = CreateGrammarFromAst(ast);
    155       var randomSearchCodeGen = new RandomSearchCodeGen();
    156       randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
    157       // var bruteForceSearchCodeGen = new BruteForceCodeGen();
    158       // bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
     155      // var randomSearchCodeGen = new RandomSearchCodeGen();
     156      // randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
     157      var bruteForceSearchCodeGen = new BruteForceCodeGen();
     158      bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
    159159      // var mctsCodeGen = new MonteCarloTreeSearchCodeGen();
    160160      // mctsCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
Note: See TracChangeset for help on using the changeset viewer.