Free cookie consent management tool by TermsFeed Policy Generator

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.

File:
1 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) {
Note: See TracChangeset for help on using the changeset viewer.