- Timestamp:
- 01/22/14 21:05:46 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10386 r10387 83 83 public static readonly string[] symb = new string[] { ?SYMBOLNAMES? }; 84 84 85 public static Tree CreateTerminalNode(int state, Random random, ?IDENT?Problem problem) {86 switch(state) {87 ?CREATETERMINALNODECODE?88 default: { throw new ArgumentException(""Unknown state index"" + state); }89 }90 }91 85 } 92 86 #endregion … … 128 122 .Replace("?SYMBOLNAMES?", grammar.Symbols.Select(s => s.Name).Aggregate(string.Empty, (str, symb) => str + "\"" + symb + "\", ")) 129 123 .Replace("?TRANSITIONTABLE?", GenerateTransitionTable(grammar)) 130 .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar))131 124 .Replace("?SUBTREECOUNTTABLE?", GenerateSubtreeCountTable(grammar)) 132 125 ; … … 136 129 var grammar = CreateGrammarFromAst(ast); 137 130 var randomSearchCodeGen = new RandomSearchCodeGen(); 138 randomSearchCodeGen.Generate(grammar, ast. FitnessFunctionNode.Maximization, solverSourceCode);131 randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode); 139 132 //var bruteForceSearchCodeGen = new BruteForceCodeGen(); 140 133 //bruteForceSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode); … … 239 232 sb.AppendFormat("public {0} {1};", att.Type, att.Identifier).AppendLine(); 240 233 } 241 sb.AppendFormat(" public {0}Tree(Random random, ?IDENT?Problem problem) : base() {{", terminal.Ident).BeginBlock(); 242 foreach (var constr in terminal.Constraints) { 243 if (constr.Type == ConstraintNodeType.Set) { 244 sb.Append("{").BeginBlock(); 245 sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 246 sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine(); 247 sb.Append("}").EndBlock(); 248 } else { 249 sb.Append("{").BeginBlock(); 250 sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 251 sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 252 sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min;", constr.Ident).EndBlock(); 253 sb.Append("}").EndBlock(); 254 } 255 } 256 sb.Append("}").EndBlock(); 234 sb.AppendFormat(" public {0}Tree() : base() {{ }}", terminal.Ident).AppendLine(); 257 235 sb.AppendLine("}"); 258 236 } … … 362 340 } 363 341 364 private string GenerateCreateTerminalCode(IGrammar grammar) { 365 Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol)); 366 var sb = new SourceBuilder(); 367 var allSymbols = grammar.Symbols.ToList(); 368 foreach (var s in grammar.Symbols) { 369 if (grammar.IsTerminal(s)) { 370 sb.AppendFormat("case {0}: {{ return new {1}Tree(random, problem); }}", allSymbols.IndexOf(s), s.Name).AppendLine(); 371 } 372 } 373 return sb.ToString(); 374 } 342 375 343 376 344 private string GenerateTransitionTable(IGrammar grammar) {
Note: See TracChangeset
for help on using the changeset viewer.