Changeset 10425
- Timestamp:
- 01/29/14 16:37:50 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL/CodeGenerator
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/BruteForceCodeGen.cs
r10400 r10425 13 13 namespace ?PROBLEMNAME? { 14 14 public sealed class ?IDENT?Solver { 15 private static int maxDepth = 20; 15 16 16 17 private readonly ?IDENT?Problem problem; … … 73 74 74 75 public static void Main(string[] args) { 76 if(args.Length > 0) ParseArguments(args); 75 77 var problem = new ?IDENT?Problem(); 76 78 var solver = new ?IDENT?Solver(problem); 77 79 solver.Start(); 78 80 } 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 79 107 80 108 public ?IDENT?Solver(?IDENT?Problem problem) { … … 84 112 85 113 private void Start() { 86 const int MAX_SECONDS = 100000;87 114 var bestF = ?MAXIMIZATION? ? double.NegativeInfinity : double.PositiveInfinity; 88 115 int n = 0; … … 90 117 long sumSize = 0; 91 118 var sumF = 0.0; 92 var terminationTimer = new Stopwatch();93 119 var sw = new System.Diagnostics.Stopwatch(); 94 120 sw.Start(); 95 terminationTimer.Start(); 96 for(int depth = 1; terminationTimer.Elapsed.TotalSeconds < MAX_SECONDS; depth ++) { 121 for(int depth = 1; depth < maxDepth; depth ++) { 97 122 Console.WriteLine(""Depth {0}:"", depth); 98 123 foreach(var t in GenerateTrees(0, depth)) { 99 124 var f = problem.Evaluate(t); 125 // t.PrintTree(0); Console.WriteLine(); 100 126 var size = t.GetSize(); 101 127 sumSize += size; … … 104 130 sumF += f; 105 131 if (problem.IsBetter(f, bestF)) { 106 t.PrintTree(0); Console.WriteLine();107 132 bestF = f; 108 133 t.PrintTree(0); Console.WriteLine(); … … 111 136 if (n % 1000 == 0) { 112 137 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); 115 140 sumSize = 0; 116 141 sumDepth = 0; 117 142 sumF = 0.0; 118 143 sw.Restart(); 119 if(terminationTimer.Elapsed.TotalSeconds >= MAX_SECONDS) break;120 144 } 121 145 } -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10424 r10425 153 153 private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) { 154 154 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); 159 159 // var mctsCodeGen = new MonteCarloTreeSearchCodeGen(); 160 160 // mctsCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
Note: See TracChangeset
for help on using the changeset viewer.