Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10338


Ignore:
Timestamp:
01/14/14 15:22:06 (11 years ago)
Author:
gkronber
Message:

#2026 changing the way terminal symbols are handled (work in progress)

Location:
branches/HeuristicLab.Problems.GPDL
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs

    r10335 r10338  
    1818    private const string problemTemplate = @"
    1919namespace ?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?
    2033
    2134  // generic interface for communication from problem interpretation to solver
     
    94107        .Replace("?INTERPRETERSOURCE?", GenerateInterpreterSource(grammar))
    95108        .Replace("?CONSTRAINTSSOURCE?", GenerateConstraintMethods(ast.Terminals))
    96           ;
     109        .Replace("?TERMINALNODECLASSDEFINITIONS?", GenerateTerminalNodeClassDefinitions(ast.Terminals.OfType<TerminalNode>()))
     110       ;
    97111    }
    98112
     
    100114      var grammar = CreateGrammarFromAst(ast);
    101115      var randomSearchCodeGen = new RandomSearchCodeGen();
    102       randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
     116      randomSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode);
    103117      //var bruteForceSearchCodeGen = new BruteForceCodeGen();
    104118      //bruteForceSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode);
     
    190204    #endregion
    191205
     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
    192239    private string GenerateInterpreterSource(AttributedGrammar grammar) {
    193240      var sb = new SourceBuilder();
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs

    r10335 r10338  
    2626   
    2727    public sealed class SolverState : ISolverState {
    28       private class Tree {
    29         public int altIdx;
    30         // public string symbol; // for debugging
    31         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 
    4028      public int curDepth;
    4129      public int steps;
     
    6351        steps += 1;
    6452        depth = Math.Max(depth, curDepth);
    65         var t = new Tree(state, altIdx);
     53        var t = new Tree(state, altIdx, subtreeCount[state]);
    6654
    6755        // t.symbol = symb.Length > state ? symb[state] : ""TERM"";
     
    200188}";
    201189
    202     public void Generate(IGrammar grammar, IEnumerable<TerminalNode> terminals, bool maximization, SourceBuilder problemSourceCode) {
     190    public void Generate(IGrammar grammar, bool maximization, SourceBuilder problemSourceCode) {
    203191      var solverSourceCode = new SourceBuilder();
    204192      solverSourceCode.Append(solverTemplate)
    205193        // .Replace("?TERMINALFIELDS?", GenerateTerminalFields(grammar))
    206         .Replace("?TERMINALNODEDEFINITIONS?", GenerateTerminalNodeDefinitions(terminals))
    207194        // .Replace("?CONSTRUCTORCODE?", GenerateConstructorCode(grammar))
    208195        .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant())
     
    217204    }
    218205
    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     }
    253206
    254207    //private string GenerateSampleTerminalSource(IGrammar grammar) {
  • branches/HeuristicLab.Problems.GPDL/Examples/symbreg Koza.txt

    r10099 r10338  
    106106    | Multiplication<<row, out val>>
    107107    | Var<<out varName>>                                   SEM << val = GetValue(x, varName, row); >>
    108     /* | ERC<<out val>> */
     108    | ERC<<out val>>
    109109  .
    110110
  • branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.sln

    r10333 r10338  
    11
    22Microsoft Visual Studio Solution File, Format Version 12.00
    3 # Visual Studio 2012
     3# Visual Studio 2013
     4VisualStudioVersion = 12.0.21005.1
     5MinimumVisualStudioVersion = 10.0.40219.1
    46Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3768D612-38EB-47D8-9E79-75D8E5AB00A8}"
    57  ProjectSection(SolutionItems) = preProject
     
    115117    {E2060931-6700-464B-9E82-50846D7AE4E9} = {3768D612-38EB-47D8-9E79-75D8E5AB00A8}
    116118  EndGlobalSection
    117   GlobalSection(Performance) = preSolution
    118     HasPerformanceSessions = true
    119   EndGlobalSection
    120119EndGlobal
Note: See TracChangeset for help on using the changeset viewer.