Changeset 10387
- Timestamp:
- 01/22/14 21:05:46 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL/CodeGenerator
- Files:
-
- 2 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) { -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs
r10386 r10387 33 33 // terminals 34 34 if(Grammar.subtreeCount[state] == 0) { 35 t = Grammar.CreateTerminalNode(state, random, problem);35 t = CreateTerminalNode(state, random, problem); 36 36 } else { 37 37 // if the symbol has alternatives then we must choose one randomly (only one sub-tree in this case) … … 52 52 curDepth -=1; 53 53 return t; 54 } 55 56 public static Tree CreateTerminalNode(int state, Random random, ?IDENT?Problem problem) { 57 switch(state) { 58 ?CREATETERMINALNODECODE? 59 default: { throw new ArgumentException(""Unknown state index"" + state); } 60 } 54 61 } 55 62 … … 144 151 }"; 145 152 146 public void Generate(IGrammar grammar, bool maximization, SourceBuilder problemSourceCode) {153 public void Generate(IGrammar grammar, IEnumerable<TerminalNode> terminals, bool maximization, SourceBuilder problemSourceCode) { 147 154 var solverSourceCode = new SourceBuilder(); 148 155 solverSourceCode.Append(solverTemplate) 149 156 .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant()) 150 157 .Replace("?SAMPLEALTERNATIVECODE?", GenerateSampleAlternativeSource(grammar)) 158 .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar, terminals)) 151 159 ; 152 160 … … 188 196 return sb.ToString(); 189 197 } 198 private string GenerateCreateTerminalCode(IGrammar grammar, IEnumerable<TerminalNode> terminals) { 199 Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol)); 200 var sb = new SourceBuilder(); 201 var allSymbols = grammar.Symbols.ToList(); 202 foreach (var s in grammar.Symbols) { 203 if (grammar.IsTerminal(s)) { 204 sb.AppendFormat("case {0}: {{", allSymbols.IndexOf(s)).BeginBlock(); 205 sb.AppendFormat("var t = new {0}Tree();", s.Name).AppendLine(); 206 var terminal = terminals.Single(t => t.Ident == s.Name); 207 foreach (var constr in terminal.Constraints) { 208 if (constr.Type == ConstraintNodeType.Set) { 209 sb.Append("{").BeginBlock(); 210 sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 211 sb.AppendFormat("t.{0} = elements[random.Next(elements.Length)]; ", constr.Ident).EndBlock(); 212 sb.AppendLine("}"); 213 } else { 214 sb.Append("{").BeginBlock(); 215 sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 216 sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 217 sb.AppendFormat("t.{0} = random.NextDouble() * (max - min) + min;", constr.Ident).EndBlock(); 218 sb.AppendLine("}"); 219 } 220 } 221 sb.AppendLine("return t;").EndBlock(); 222 sb.Append("}"); 223 } 224 } 225 return sb.ToString(); 226 } 190 227 private void GenerateReturnStatement(IEnumerable<int> idxs, SourceBuilder sb) { 191 228 if (idxs.Count() == 1) {
Note: See TracChangeset
for help on using the changeset viewer.