Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10387


Ignore:
Timestamp:
01/22/14 21:05:46 (11 years ago)
Author:
gkronber
Message:

#2025 reintegrated initialization of terminal values into the random search solver.

Location:
branches/HeuristicLab.Problems.GPDL/CodeGenerator
Files:
2 edited

Legend:

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

    r10386 r10387  
    8383    public static readonly string[] symb = new string[] { ?SYMBOLNAMES? };
    8484   
    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     }
    9185  } 
    9286#endregion
     
    128122        .Replace("?SYMBOLNAMES?", grammar.Symbols.Select(s => s.Name).Aggregate(string.Empty, (str, symb) => str + "\"" + symb + "\", "))
    129123        .Replace("?TRANSITIONTABLE?", GenerateTransitionTable(grammar))
    130         .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar))
    131124        .Replace("?SUBTREECOUNTTABLE?", GenerateSubtreeCountTable(grammar))
    132125       ;
     
    136129      var grammar = CreateGrammarFromAst(ast);
    137130      var randomSearchCodeGen = new RandomSearchCodeGen();
    138       randomSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode);
     131      randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
    139132      //var bruteForceSearchCodeGen = new BruteForceCodeGen();
    140133      //bruteForceSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode);
     
    239232        sb.AppendFormat("public {0} {1};", att.Type, att.Identifier).AppendLine();
    240233      }
    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();
    257235      sb.AppendLine("}");
    258236    }
     
    362340    }
    363341
    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   
    375343
    376344    private string GenerateTransitionTable(IGrammar grammar) {
  • branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs

    r10386 r10387  
    3333      // terminals
    3434      if(Grammar.subtreeCount[state] == 0) {
    35         t = Grammar.CreateTerminalNode(state, random, problem);
     35        t = CreateTerminalNode(state, random, problem);
    3636      } else {
    3737        // if the symbol has alternatives then we must choose one randomly (only one sub-tree in this case)
     
    5252      curDepth -=1;
    5353      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      }
    5461    }
    5562
     
    144151}";
    145152
    146     public void Generate(IGrammar grammar, bool maximization, SourceBuilder problemSourceCode) {
     153    public void Generate(IGrammar grammar, IEnumerable<TerminalNode> terminals, bool maximization, SourceBuilder problemSourceCode) {
    147154      var solverSourceCode = new SourceBuilder();
    148155      solverSourceCode.Append(solverTemplate)
    149156        .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant())
    150157        .Replace("?SAMPLEALTERNATIVECODE?", GenerateSampleAlternativeSource(grammar))
     158        .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar, terminals))
    151159      ;
    152160
     
    188196      return sb.ToString();
    189197    }
     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    }
    190227    private void GenerateReturnStatement(IEnumerable<int> idxs, SourceBuilder sb) {
    191228      if (idxs.Count() == 1) {
Note: See TracChangeset for help on using the changeset viewer.