Changeset 10338
- Timestamp:
- 01/14/14 15:22:06 (11 years ago)
- Location:
- branches/HeuristicLab.Problems.GPDL
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10335 r10338 18 18 private const string problemTemplate = @" 19 19 namespace ?PROBLEMNAME? { 20 public class Tree { 21 public int altIdx; 22 public List<Tree> subtrees; 23 protected Tree() { 24 // leave subtrees uninitialized 25 } 26 public Tree(int state, int altIdx, int numSubTrees) { 27 subtrees = new List<Tree>(); 28 this.altIdx = altIdx; 29 } 30 } 31 32 ?TERMINALNODECLASSDEFINITIONS? 20 33 21 34 // generic interface for communication from problem interpretation to solver … … 94 107 .Replace("?INTERPRETERSOURCE?", GenerateInterpreterSource(grammar)) 95 108 .Replace("?CONSTRAINTSSOURCE?", GenerateConstraintMethods(ast.Terminals)) 96 ; 109 .Replace("?TERMINALNODECLASSDEFINITIONS?", GenerateTerminalNodeClassDefinitions(ast.Terminals.OfType<TerminalNode>())) 110 ; 97 111 } 98 112 … … 100 114 var grammar = CreateGrammarFromAst(ast); 101 115 var randomSearchCodeGen = new RandomSearchCodeGen(); 102 randomSearchCodeGen.Generate(grammar, ast. Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);116 randomSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode); 103 117 //var bruteForceSearchCodeGen = new BruteForceCodeGen(); 104 118 //bruteForceSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode); … … 190 204 #endregion 191 205 206 private string GenerateTerminalNodeClassDefinitions(IEnumerable<TerminalNode> terminals) { 207 var sb = new SourceBuilder(); 208 209 foreach (var terminal in terminals) { 210 GenerateTerminalNodeClassDefinitions(terminal, sb); 211 } 212 return sb.ToString(); 213 } 214 215 private void GenerateTerminalNodeClassDefinitions(TerminalNode terminal, SourceBuilder sb) { 216 sb.AppendFormat("public class {0}Tree : Tree {{", terminal.Ident).BeginBlock(); 217 foreach (var att in terminal.FieldDefinitions) { 218 sb.AppendFormat("{0} {1};", att.Type, att.Identifier).AppendLine(); 219 } 220 sb.AppendFormat(" public {0}Tree(Random random, ?IDENT?Problem problem) : base() {{", terminal.Ident).BeginBlock(); 221 foreach (var constr in terminal.Constraints) { 222 if (constr.Type == ConstraintNodeType.Set) { 223 sb.AppendLine("{").BeginBlock(); 224 sb.AppendFormat(" var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 225 sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine(); 226 sb.AppendLine("}").EndBlock(); 227 } else { 228 sb.AppendLine("{").BeginBlock(); 229 sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 230 sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 231 sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min ", constr.Ident).AppendLine(); 232 sb.AppendLine("}").EndBlock(); 233 } 234 } 235 sb.AppendLine("}").EndBlock(); 236 sb.AppendLine("}").EndBlock(); 237 } 238 192 239 private string GenerateInterpreterSource(AttributedGrammar grammar) { 193 240 var sb = new SourceBuilder(); -
branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs
r10335 r10338 26 26 27 27 public sealed class SolverState : ISolverState { 28 private class Tree {29 public int altIdx;30 // public string symbol; // for debugging31 public List<Tree> subtrees;32 public Tree(int state, int altIdx) {33 subtrees = new List<Tree>(subtreeCount[state]);34 this.altIdx = altIdx;35 }36 }37 38 ?TERMINALNODEDEFINITIONS?39 40 28 public int curDepth; 41 29 public int steps; … … 63 51 steps += 1; 64 52 depth = Math.Max(depth, curDepth); 65 var t = new Tree(state, altIdx );53 var t = new Tree(state, altIdx, subtreeCount[state]); 66 54 67 55 // t.symbol = symb.Length > state ? symb[state] : ""TERM""; … … 200 188 }"; 201 189 202 public void Generate(IGrammar grammar, IEnumerable<TerminalNode> terminals,bool maximization, SourceBuilder problemSourceCode) {190 public void Generate(IGrammar grammar, bool maximization, SourceBuilder problemSourceCode) { 203 191 var solverSourceCode = new SourceBuilder(); 204 192 solverSourceCode.Append(solverTemplate) 205 193 // .Replace("?TERMINALFIELDS?", GenerateTerminalFields(grammar)) 206 .Replace("?TERMINALNODEDEFINITIONS?", GenerateTerminalNodeDefinitions(terminals))207 194 // .Replace("?CONSTRUCTORCODE?", GenerateConstructorCode(grammar)) 208 195 .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant()) … … 217 204 } 218 205 219 private string GenerateTerminalNodeDefinitions(IEnumerable<TerminalNode> terminals) {220 var sb = new SourceBuilder();221 222 foreach (var terminal in terminals) {223 GenerateTerminalNodeDefinitions(terminal, sb);224 }225 return sb.ToString();226 }227 228 private void GenerateTerminalNodeDefinitions(TerminalNode terminal, SourceBuilder sb) {229 sb.AppendFormat("private class {0}Tree {{", terminal.Ident).BeginBlock();230 foreach (var att in terminal.FieldDefinitions) {231 sb.AppendFormat("{0} {1};", att.Type, att.Identifier).AppendLine();232 }233 sb.AppendFormat(" static {0}Tree Create(Random random, ?IDENT?Problem problem) {{", terminal.Ident).BeginBlock();234 sb.AppendFormat("var t = new {0}Tree();", terminal.Ident);235 foreach (var constr in terminal.Constraints) {236 if (constr.Type == ConstraintNodeType.Set) {237 sb.AppendLine("{").BeginBlock();238 sb.AppendFormat(" var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident);239 sb.AppendFormat("t.{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine();240 sb.AppendLine("}").EndBlock();241 } else {242 sb.AppendLine("{").BeginBlock();243 sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident);244 sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident);245 sb.AppendFormat("t.{0} = random.NextDouble() * (max - min) + min ", constr.Ident).AppendLine();246 sb.AppendLine("}").EndBlock();247 }248 }249 sb.AppendLine("return t;");250 sb.AppendLine("}").EndBlock();251 sb.AppendLine("}").EndBlock();252 }253 206 254 207 //private string GenerateSampleTerminalSource(IGrammar grammar) { -
branches/HeuristicLab.Problems.GPDL/Examples/symbreg Koza.txt
r10099 r10338 106 106 | Multiplication<<row, out val>> 107 107 | Var<<out varName>> SEM << val = GetValue(x, varName, row); >> 108 /* | ERC<<out val>> */108 | ERC<<out val>> 109 109 . 110 110 -
branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.sln
r10333 r10338 1 1 2 2 Microsoft Visual Studio Solution File, Format Version 12.00 3 # Visual Studio 2012 3 # Visual Studio 2013 4 VisualStudioVersion = 12.0.21005.1 5 MinimumVisualStudioVersion = 10.0.40219.1 4 6 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3768D612-38EB-47D8-9E79-75D8E5AB00A8}" 5 7 ProjectSection(SolutionItems) = preProject … … 115 117 {E2060931-6700-464B-9E82-50846D7AE4E9} = {3768D612-38EB-47D8-9E79-75D8E5AB00A8} 116 118 EndGlobalSection 117 GlobalSection(Performance) = preSolution118 HasPerformanceSessions = true119 EndGlobalSection120 119 EndGlobal
Note: See TracChangeset
for help on using the changeset viewer.