Changeset 10426
- Timestamp:
- 01/29/14 17:10:03 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL/CodeGenerator
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/BruteForceCodeGen.cs
r10425 r10426 12 12 13 13 namespace ?PROBLEMNAME? { 14 public sealed class ?IDENT? Solver {14 public sealed class ?IDENT?BruteForceSolver { 15 15 private static int maxDepth = 20; 16 16 … … 73 73 } 74 74 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) { 82 76 var maxDepthRegex = new Regex(@""--maxDepth=(?<d>.+)""); 83 77 … … 97 91 } 98 92 } 99 private staticvoid PrintUsage() {93 private void PrintUsage() { 100 94 Console.WriteLine(""Find a solution using brute force tree search.""); 101 95 Console.WriteLine(); … … 106 100 107 101 108 public ?IDENT?Solver(?IDENT?Problem problem) { 102 103 public ?IDENT?BruteForceSolver(?IDENT?Problem problem, string[] args) { 104 if(args.Length>1) ParseArguments(args); 109 105 this.problem = problem; 110 106 this.random = new Random(); 111 107 } 112 108 113 p rivatevoid Start() {109 public void Start() { 114 110 var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity; 115 111 int n = 0; -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/MonteCarloTreeSearchCodeGen.cs
r10415 r10426 17 17 public bool done; 18 18 public SearchTreeNode[] children; 19 // only for debugging 19 20 public double[] Ucb { 20 21 get { 21 22 return (from c in children 22 select ?IDENT? Solver.UCB(this, c)23 select ?IDENT?MonteCarloTreeSearchSolver.UCB(this, c) 23 24 ).ToArray(); 24 25 } … … 28 29 } 29 30 30 public sealed class ?IDENT?Solver { 31 public sealed class ?IDENT?MonteCarloTreeSearchSolver { 32 private int maxDepth = 20; 31 33 32 34 private readonly ?IDENT?Problem problem; … … 34 36 private SearchTreeNode searchTree = new SearchTreeNode(); 35 37 36 private Tree SampleTree( ) {37 var extensionsStack = new Stack<Tuple<Tree, int, int >>(); // the unfinished tree, the state, and the index of the extensionpoint38 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 38 40 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)); 40 42 SampleTree(searchTree, extensionsStack); 41 43 return t.subtrees[0]; 42 44 } 43 45 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) { 45 47 const int RANDOM_TRIES = 1000; 46 48 if(extensionPoints.Count == 0) { … … 52 54 int state = extensionPoint.Item2; 53 55 int subtreeIdx = extensionPoint.Item3; 56 int maxDepth = extensionPoint.Item4; 57 Debug.Assert(maxDepth >= 1); 54 58 Tree t = null; 55 59 if(searchTree.tries < RANDOM_TRIES || Grammar.subtreeCount[state] == 0) { … … 81 85 var altIdx = SelectAlternative(searchTree); 82 86 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)); 84 88 SampleTree(searchTree.children[altIdx], extensionPoints); 85 89 } else { … … 88 92 t = new Tree(-1, subtrees); 89 93 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)); 91 95 } 92 96 SampleTree(searchTree, extensionPoints); … … 215 219 } 216 220 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 ""); 227 223 this.problem = problem; 228 224 this.random = new Random(); 229 225 } 230 226 231 p rivatevoid Start() {227 public void Start() { 232 228 Console.ReadLine(); 233 229 var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity; … … 241 237 242 238 int steps, depth; 243 var _t = SampleTree( );239 var _t = SampleTree(maxDepth); 244 240 // _t.PrintTree(0); Console.WriteLine(); 245 241 … … 247 243 steps = _t.GetSize(); 248 244 depth = _t.GetDepth(); 245 Debug.Assert(depth <= maxDepth); 249 246 var f = problem.Evaluate(_t); 250 247 if(?MAXIMIZATION?) -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10425 r10426 1 using System.Collections.Generic; 1 using System; 2 using System.Collections.Generic; 2 3 using System.Diagnostics; 3 4 using System.IO; … … 19 20 private const string problemTemplate = @" 20 21 namespace ?PROBLEMNAME? { 22 23 24 25 21 26 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 } 22 32 23 33 public ?IDENT?Problem() { … … 153 163 private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) { 154 164 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 } 161 188 } 162 189 -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs
r10424 r10426 11 11 private string solverTemplate = @" 12 12 namespace ?PROBLEMNAME? { 13 public sealed class ?IDENT? Solver {14 private staticdouble baseTerminalProbability = 0.05; // 5% of all samples are only a terminal node15 private staticdouble 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% 16 16 private static int maxDepth = 20; 17 17 … … 77 77 } 78 78 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) { 87 80 var baseTerminalProbabilityRegex = new Regex(@""--terminalProbBase=(?<prob>.+)""); 88 81 var terminalProbabilityIncRegex = new Regex(@""--terminalProbInc=(?<prob>.+)""); … … 111 104 } 112 105 } 113 private staticvoid PrintUsage() {106 private void PrintUsage() { 114 107 Console.WriteLine(""Find a solution using random tree search.""); 115 108 Console.WriteLine(); … … 121 114 122 115 123 public ?IDENT?Solver(?IDENT?Problem problem) { 116 public ?IDENT?RandomSearchSolver(?IDENT?Problem problem, string[] args) { 117 if(args.Length >= 1) ParseArguments(args); 118 124 119 this.problem = problem; 125 120 this.random = new Random(); 126 121 } 127 122 128 p rivatevoid Start() {123 public void Start() { 129 124 var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity; 130 125 int n = 0;
Note: See TracChangeset
for help on using the changeset viewer.