Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/22/14 18:09:44 (11 years ago)
Author:
gkronber
Message:

#2026 fixed random search code generation

File:
1 edited

Legend:

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

    r10338 r10384  
    3333
    3434  // generic interface for communication from problem interpretation to solver
    35   public interface ISolverState {
    36     void Reset();
    37     int PeekNextAlternative(); // in alternative nodes returns the index of the alternative that should be followed
    38     void Follow(int idx); // next: derive the NT symbol with index=idx;
    39     void Unwind(); // finished with deriving the NT symbol
    40   }
     35  // public interface ISolverState {
     36  //   void Reset();
     37  //   int PeekNextAlternative(); // in alternative nodes returns the index of the alternative that should be followed
     38  //   void Follow(int idx); // next: derive the NT symbol with index=idx;
     39  //   void Unwind(); // finished with deriving the NT symbol
     40  // }
    4141
    4242  public sealed class ?IDENT?Problem {
     
    5353    }
    5454
    55     private ISolverState _state;
    56     public double Evaluate(ISolverState _state) {
    57       this._state = _state;
     55    private Tree _t;
     56    public double Evaluate(Tree _t) {
     57      this._t = _t;
    5858#region objective function (MINIMIZE / MAXIMIZE section)
    5959?FITNESSFUNCTION?
     
    216216      sb.AppendFormat("public class {0}Tree : Tree {{", terminal.Ident).BeginBlock();
    217217      foreach (var att in terminal.FieldDefinitions) {
    218         sb.AppendFormat("{0} {1};", att.Type, att.Identifier).AppendLine();
     218        sb.AppendFormat("public {0} {1};", att.Type, att.Identifier).AppendLine();
    219219      }
    220220      sb.AppendFormat(" public {0}Tree(Random random, ?IDENT?Problem problem) : base() {{", terminal.Ident).BeginBlock();
     
    222222        if (constr.Type == ConstraintNodeType.Set) {
    223223          sb.AppendLine("{").BeginBlock();
    224           sb.AppendFormat(" var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine();
     224          sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine();
    225225          sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine();
    226226          sb.AppendLine("}").EndBlock();
     
    229229          sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine();
    230230          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();
     231          sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min;", constr.Ident).AppendLine();
    232232          sb.AppendLine("}").EndBlock();
    233233        }
     
    268268      else
    269269        actualParameter = string.Empty;
    270       sb.AppendLine("_state.Reset();");
    271       sb.AppendFormat("{0}(_state, {1});", s.Name, actualParameter).AppendLine();
     270      sb.AppendFormat("{0}(_t, {1});", s.Name, actualParameter).AppendLine();
    272271      sb.AppendLine("}").EndBlock();
    273272    }
     
    275274    private void GenerateInterpreterMethod(AttributedGrammar g, ISymbol s, SourceBuilder sb) {
    276275      if (!s.Attributes.Any())
    277         sb.AppendFormat("private void {0}(ISolverState _state) {{", s.Name).BeginBlock();
     276        sb.AppendFormat("private void {0}(Tree _t) {{", s.Name).BeginBlock();
    278277      else
    279         sb.AppendFormat("private void {0}(ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock();
     278        sb.AppendFormat("private void {0}(Tree _t, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock();
    280279
    281280      // generate local definitions
     
    297296
    298297    private void GenerateSwitchStatement(IEnumerable<Sequence> alts, SourceBuilder sb) {
    299       sb.Append("switch(_state.PeekNextAlternative()) {").BeginBlock();
     298      sb.Append("switch(_t.altIdx) {").BeginBlock();
    300299      // generate a case for each alternative
    301300      int i = 0;
     
    321320        sb.Append(action.Code + ";");
    322321      else if (!s.Attributes.Any())
    323         sb.AppendFormat("_state.Follow({0}); {1}(_state); _state.Unwind();", idx, s.Name);
    324       else sb.AppendFormat("_state.Follow({0}); {1}(_state, {2}); _state.Unwind();", idx, s.Name, s.GetAttributeString());
     322        sb.AppendFormat("{1}(_t.subtrees[{0}]);", idx, s.Name);
     323      else sb.AppendFormat("{1}(_t.subtrees[{0}], {2}); ", idx, s.Name, s.GetAttributeString());
    325324      sb.AppendLine();
    326325    }
     
    329328      // if the terminal symbol has attributes then we must samples values for these attributes
    330329      if (!s.Attributes.Any())
    331         sb.AppendFormat("private void {0}(ISolverState _state) {{", s.Name).BeginBlock();
     330        sb.AppendFormat("private void {0}(Tree _t) {{", s.Name).BeginBlock();
    332331      else
    333         sb.AppendFormat("private void {0}(ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock();
     332        sb.AppendFormat("private void {0}(Tree _t, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock();
    334333
    335334      // each field must match a formal parameter, assign a value for each parameter
    336335      int i = 0;
    337336      foreach (var element in s.Attributes) {
    338         sb.AppendFormat("_state.Follow({0});", i++).AppendLine();
    339         sb.AppendFormat("{0} = Get{1}_{0}(_state);", element.Name, s.Name).AppendLine();
    340         sb.AppendFormat("_state.Unwind();").AppendLine();
     337        sb.AppendFormat("{0} = (_t as {1}Tree).{0};", element.Name, s.Name).AppendLine();
    341338      }
    342339      sb.Append("}").EndBlock();
Note: See TracChangeset for help on using the changeset viewer.