Changeset 10426


Ignore:
Timestamp:
01/29/14 17:10:03 (7 years ago)
Author:
gkronber
Message:

#2026 generate code for all solvers

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

Legend:

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

    r10425 r10426  
    1212
    1313namespace ?PROBLEMNAME? {
    14   public sealed class ?IDENT?Solver {
     14  public sealed class ?IDENT?BruteForceSolver {
    1515    private static int maxDepth = 20;
    1616
     
    7373    }
    7474
    75     public static void Main(string[] args) {
    76       if(args.Length > 0) ParseArguments(args);
    77       var problem = new ?IDENT?Problem();
    78       var solver = new ?IDENT?Solver(problem);
    79       solver.Start();
    80     }
    81     private static void ParseArguments(string[] args) {
     75    private void ParseArguments(string[] args) {
    8276      var maxDepthRegex = new Regex(@""--maxDepth=(?<d>.+)"");
    8377
     
    9791      }
    9892    }
    99     private static void PrintUsage() {
     93    private void PrintUsage() {
    10094      Console.WriteLine(""Find a solution using brute force tree search."");
    10195      Console.WriteLine();
     
    106100
    107101
    108     public ?IDENT?Solver(?IDENT?Problem problem) {
     102
     103    public ?IDENT?BruteForceSolver(?IDENT?Problem problem, string[] args) {
     104      if(args.Length>1) ParseArguments(args);
    109105      this.problem = problem;
    110106      this.random = new Random();
    111107    }
    112108
    113     private void Start() {
     109    public void Start() {
    114110      var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity;
    115111      int n = 0;
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/MonteCarloTreeSearchCodeGen.cs

    r10415 r10426  
    1717    public bool done;
    1818    public SearchTreeNode[] children;
     19    // only for debugging
    1920    public double[] Ucb {
    2021      get {
    2122        return (from c in children
    22                 select ?IDENT?Solver.UCB(this, c)
     23                select ?IDENT?MonteCarloTreeSearchSolver.UCB(this, c)
    2324               ).ToArray();
    2425      }
     
    2829  }
    2930
    30   public sealed class ?IDENT?Solver {
     31  public sealed class ?IDENT?MonteCarloTreeSearchSolver {
     32    private int maxDepth = 20;
    3133
    3234    private readonly ?IDENT?Problem problem;
     
    3436    private SearchTreeNode searchTree = new SearchTreeNode();
    3537   
    36     private Tree SampleTree() {
    37       var extensionsStack = new Stack<Tuple<Tree, int, int>>(); // the unfinished tree, the state, and the index of the extension point
     38    private Tree SampleTree(int maxDepth) {
     39      var extensionsStack = new Stack<Tuple<Tree, int, int, int>>(); // the unfinished tree, the state, the index of the extension point and the maximal depth of a tree inserted at that point
    3840      var t = new Tree(-1, new Tree[1]);
    39       extensionsStack.Push(Tuple.Create(t, 0, 0));
     41      extensionsStack.Push(Tuple.Create(t, 0, 0, maxDepth));
    4042      SampleTree(searchTree, extensionsStack);
    4143      return t.subtrees[0];
    4244    }
    4345
    44     private void SampleTree(SearchTreeNode searchTree, Stack<Tuple<Tree, int, int>> extensionPoints) {
     46    private void SampleTree(SearchTreeNode searchTree, Stack<Tuple<Tree, int, int, int>> extensionPoints) {
    4547      const int RANDOM_TRIES = 1000;
    4648      if(extensionPoints.Count == 0) {
     
    5254      int state = extensionPoint.Item2;
    5355      int subtreeIdx = extensionPoint.Item3;
     56      int maxDepth = extensionPoint.Item4;
     57      Debug.Assert(maxDepth >= 1);
    5458      Tree t = null;
    5559      if(searchTree.tries < RANDOM_TRIES || Grammar.subtreeCount[state] == 0) {
     
    8185          var altIdx = SelectAlternative(searchTree);
    8286          t = new Tree(altIdx, new Tree[1]);
    83           extensionPoints.Push(Tuple.Create(t, Grammar.transition[state][altIdx], 0));
     87          extensionPoints.Push(Tuple.Create(t, Grammar.transition[state][altIdx], 0, maxDepth - 1));
    8488          SampleTree(searchTree.children[altIdx], extensionPoints);
    8589        } else {
     
    8892          t = new Tree(-1, subtrees);
    8993          for(int i = subtrees.Length - 1; i >= 0; i--) {
    90             extensionPoints.Push(Tuple.Create(t, Grammar.transition[state][i], i));
     94            extensionPoints.Push(Tuple.Create(t, Grammar.transition[state][i], i, maxDepth - 1));
    9195          }
    9296          SampleTree(searchTree, extensionPoints);         
     
    215219    }
    216220
    217 
    218     public static void Main(string[] args) {
    219       // if(args.Length >= 1) ParseArguments(args);
    220 
    221       var problem = new ?IDENT?Problem();
    222       var solver = new ?IDENT?Solver(problem);
    223       solver.Start();
    224     }
    225 
    226     public ?IDENT?Solver(?IDENT?Problem problem) {
     221    public ?IDENT?MonteCarloTreeSearchSolver(?IDENT?Problem problem, string[] args) {
     222      if(args.Length > 0 ) throw new ArgumentException(""Arguments ""+args.Aggregate("""", (str, s) => str+ "" "" + s)+"" are not supported "");
    227223      this.problem = problem;
    228224      this.random = new Random();
    229225    }
    230226
    231     private void Start() {
     227    public void Start() {
    232228      Console.ReadLine();
    233229      var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity;
     
    241237
    242238        int steps, depth;
    243         var _t = SampleTree();
     239        var _t = SampleTree(maxDepth);
    244240        //  _t.PrintTree(0); Console.WriteLine();
    245241
     
    247243        steps = _t.GetSize();
    248244        depth = _t.GetDepth();
     245        Debug.Assert(depth <= maxDepth);
    249246        var f = problem.Evaluate(_t);
    250247        if(?MAXIMIZATION?)
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs

    r10425 r10426  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using System.Diagnostics;
    34using System.IO;
     
    1920    private const string problemTemplate = @"
    2021namespace ?PROBLEMNAME? {
     22
     23
     24
     25
    2126  public sealed class ?IDENT?Problem {
     27    public static void Main(string[] args) {
     28      var problem = new ?IDENT?Problem();
     29      var solver = new ?IDENT?RandomSearchSolver(problem, args);
     30      solver.Start();
     31    }
    2232   
    2333   public ?IDENT?Problem() {
     
    153163    private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) {
    154164      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);
    159       // var mctsCodeGen = new MonteCarloTreeSearchCodeGen();
    160       // mctsCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
     165      try {
     166        var randomSearchCodeGen = new RandomSearchCodeGen();
     167        randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization,
     168                                     solverSourceCode);
     169      }
     170      catch (Exception e) {
     171        Console.WriteLine(e.Message);
     172      }
     173      try {
     174        var bruteForceSearchCodeGen = new BruteForceCodeGen();
     175        bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
     176      }
     177      catch (Exception e) {
     178        Console.WriteLine(e.Message);
     179      }
     180      try {
     181        var mctsCodeGen = new MonteCarloTreeSearchCodeGen();
     182        mctsCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization,
     183                             solverSourceCode);
     184      }
     185      catch (Exception e) {
     186        Console.WriteLine(e.Message);
     187      }
    161188    }
    162189
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs

    r10424 r10426  
    1111    private string solverTemplate = @"
    1212namespace ?PROBLEMNAME? {
    13   public sealed class ?IDENT?Solver {
    14     private static double baseTerminalProbability = 0.05; // 5% of all samples are only a terminal node
    15     private static double terminalProbabilityInc = 0.05; // for each level the probability to sample a terminal grows by 5%
     13  public sealed class ?IDENT?RandomSearchSolver {
     14    private double baseTerminalProbability = 0.05; // 5% of all samples are only a terminal node
     15    private double terminalProbabilityInc = 0.05; // for each level the probability to sample a terminal grows by 5%
    1616    private static int maxDepth = 20;
    1717
     
    7777    }
    7878
    79     public static void Main(string[] args) {
    80       if(args.Length >= 1) ParseArguments(args);
    81 
    82       var problem = new ?IDENT?Problem();
    83       var solver = new ?IDENT?Solver(problem);
    84       solver.Start();
    85     }
    86     private static void ParseArguments(string[] args) {
     79    private void ParseArguments(string[] args) {
    8780      var baseTerminalProbabilityRegex = new Regex(@""--terminalProbBase=(?<prob>.+)"");
    8881      var terminalProbabilityIncRegex = new Regex(@""--terminalProbInc=(?<prob>.+)"");
     
    111104      }
    112105    }
    113     private static void PrintUsage() {
     106    private void PrintUsage() {
    114107      Console.WriteLine(""Find a solution using random tree search."");
    115108      Console.WriteLine();
     
    121114
    122115
    123     public ?IDENT?Solver(?IDENT?Problem problem) {
     116    public ?IDENT?RandomSearchSolver(?IDENT?Problem problem, string[] args) {
     117      if(args.Length >= 1) ParseArguments(args);
     118
    124119      this.problem = problem;
    125120      this.random = new Random();
    126121    }
    127122
    128     private void Start() {
     123    public void Start() {
    129124      var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity;
    130125      int n = 0;
Note: See TracChangeset for help on using the changeset viewer.