- Timestamp:
- 01/22/14 19:27:04 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10385 r10386 18 18 private const string problemTemplate = @" 19 19 namespace ?PROBLEMNAME? { 20 #region class definitions for tree21 public class Tree {22 public int altIdx;23 public List<Tree> subtrees;24 protected Tree() {25 // leave subtrees uninitialized26 }27 public Tree(int state, int altIdx, int numSubTrees) {28 subtrees = new List<Tree>();29 this.altIdx = altIdx;30 }31 }32 33 ?TERMINALNODECLASSDEFINITIONS?34 #endregion35 36 20 public sealed class ?IDENT?Problem { 37 21 … … 54 38 #endregion 55 39 } 40 public bool IsBetter(double a, double b) { 41 return ?MAXIMIZATION? ? a > b : a < b; 42 } 56 43 57 44 // additional code from the problem definition (CODE section) … … 68 55 #endregion 69 56 } 57 58 #region class definitions for tree 59 public class Tree { 60 public int altIdx; 61 public Tree[] subtrees; 62 protected Tree() { 63 // leave subtrees uninitialized 64 } 65 public Tree(int altIdx, Tree[] subtrees) { 66 this.altIdx = altIdx; 67 this.subtrees = subtrees; 68 } 69 } 70 71 ?TERMINALNODECLASSDEFINITIONS? 72 #endregion 73 74 #region helper class for the grammar representation 75 public class Grammar { 76 public static readonly Dictionary<int, int[]> transition = new Dictionary<int, int[]>() { 77 ?TRANSITIONTABLE? 78 }; 79 public static readonly Dictionary<int, int> subtreeCount = new Dictionary<int, int>() { 80 { -1, 0 }, // terminals 81 ?SUBTREECOUNTTABLE? 82 }; 83 public static readonly string[] symb = new string[] { ?SYMBOLNAMES? }; 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 } 92 #endregion 70 93 }"; 71 94 … … 97 120 .AppendLine(problemTemplate) 98 121 .Replace("?FITNESSFUNCTION?", ast.FitnessFunctionNode.SrcCode) 122 .Replace("?MAXIMIZATION?", ast.FitnessFunctionNode.Maximization.ToString().ToLowerInvariant()) 99 123 .Replace("?INITSOURCE?", ast.InitCodeNode.SrcCode) 100 124 .Replace("?ADDITIONALCODE?", ast.ClassCodeNode.SrcCode) … … 102 126 .Replace("?CONSTRAINTSSOURCE?", GenerateConstraintMethods(ast.Terminals)) 103 127 .Replace("?TERMINALNODECLASSDEFINITIONS?", GenerateTerminalNodeClassDefinitions(ast.Terminals.OfType<TerminalNode>())) 128 .Replace("?SYMBOLNAMES?", grammar.Symbols.Select(s => s.Name).Aggregate(string.Empty, (str, symb) => str + "\"" + symb + "\", ")) 129 .Replace("?TRANSITIONTABLE?", GenerateTransitionTable(grammar)) 130 .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar)) 131 .Replace("?SUBTREECOUNTTABLE?", GenerateSubtreeCountTable(grammar)) 104 132 ; 105 133 } … … 190 218 sb.AppendFormat("public {0} GetMax{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.RangeMaxExpression).AppendLine(); 191 219 sb.AppendFormat("public {0} GetMin{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.RangeMinExpression).AppendLine(); 192 //sb.AppendFormat("public {0} Get{1}_{2}(ISolverState _state) {{ _state. }}", fieldType, t.Ident, c.Ident, )193 220 } else if (c.Type == ConstraintNodeType.Set) { 194 221 sb.AppendFormat("public IEnumerable<{0}> GetAllowed{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.SetExpression).AppendLine(); … … 289 316 } 290 317 291 private void GenerateSwitchStatement(IEnumerable<Sequence> alts, SourceBuilder sb) { 318 private void GenerateSwitchStatement(IEnumerable<Sequence> alts, SourceBuilder sb) 319 { 292 320 sb.Append("switch(_t.altIdx) {").BeginBlock(); 293 321 // generate a case for each alternative 294 int i= 0;322 int altIdx = 0; 295 323 foreach (var alt in alts) { 296 sb.AppendFormat("case {0}: {{ ", i).BeginBlock();324 sb.AppendFormat("case {0}: {{ ", altIdx).BeginBlock(); 297 325 298 326 // this only works for alternatives with a single non-terminal symbol (ignoring semantic symbols)! … … 302 330 GenerateSourceForAction(0, altSymb, sb); // index is always 0 because of the assertion above 303 331 } 304 i++;332 altIdx++; 305 333 sb.AppendLine("break;").Append("}").EndBlock(); 306 334 } … … 333 361 sb.Append("}").EndBlock(); 334 362 } 363 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 } 375 376 private string GenerateTransitionTable(IGrammar grammar) { 377 Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol)); 378 var sb = new SourceBuilder(); 379 380 // state idx = idx of the corresponding symbol in the grammar 381 var allSymbols = grammar.Symbols.ToList(); 382 foreach (var s in grammar.Symbols) { 383 var targetStates = new List<int>(); 384 if (grammar.IsTerminal(s)) { 385 } else { 386 if (grammar.NumberOfAlternatives(s) > 1) { 387 foreach (var alt in grammar.GetAlternatives(s)) { 388 // only single-symbol alternatives are supported 389 Debug.Assert(alt.Count() == 1); 390 targetStates.Add(allSymbols.IndexOf(alt.Single())); 391 } 392 } else { 393 // rule is a sequence of symbols 394 var seq = grammar.GetAlternatives(s).Single(); 395 targetStates.AddRange(seq.Select(symb => allSymbols.IndexOf(symb))); 396 } 397 } 398 399 var targetStateString = targetStates.Aggregate(string.Empty, (str, state) => str + state + ", "); 400 401 var idxOfSourceState = allSymbols.IndexOf(s); 402 sb.AppendFormat("// {0}", s).AppendLine(); 403 sb.AppendFormat("{{ {0} , new int[] {{ {1} }} }},", idxOfSourceState, targetStateString).AppendLine(); 404 } 405 return sb.ToString(); 406 } 407 private string GenerateSubtreeCountTable(IGrammar grammar) { 408 Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol)); 409 var sb = new SourceBuilder(); 410 411 // state idx = idx of the corresponding symbol in the grammar 412 var allSymbols = grammar.Symbols.ToList(); 413 foreach (var s in grammar.Symbols) { 414 int subtreeCount = 0; 415 if (grammar.IsTerminal(s)) { 416 } else { 417 if (grammar.NumberOfAlternatives(s) > 1) { 418 Debug.Assert(grammar.GetAlternatives(s).All(alt => alt.Count() == 1)); 419 subtreeCount = 1; 420 } else { 421 subtreeCount = grammar.GetAlternative(s, 0).Count(); 422 } 423 } 424 425 sb.AppendFormat("// {0}", s).AppendLine(); 426 sb.AppendFormat("{{ {0} , {1} }},", allSymbols.IndexOf(s), subtreeCount).AppendLine(); 427 } 428 429 return sb.ToString(); 430 } 431 335 432 } 336 433 }
Note: See TracChangeset
for help on using the changeset viewer.