Ignore:
Timestamp:
06/25/17 15:59:39 (2 years ago)
Author:
gkronber
Message:

#2581: merged r13645,r13648,r13650,r13651,r13652,r13654,r13657,r13658,r13659,r13661,r13662,r13669,r13708,r14142 from trunk to stable (to be deleted in the next commit)

Location:
stable
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs

    r13645 r15060  
    22/* HeuristicLab
    33 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    4  * and the BEACON Center for the Study of Evolution in Action.
    54 *
    65 * This file is part of HeuristicLab.
     
    2726namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
    2827  // this is the core class for generating expressions.
    29   // the automaton determines which expressions are allowed
     28  // it represents a finite state automaton, each state transition can be associated with an action (e.g. to produce code).
     29  // the automaton determines the possible structures for expressions.
     30  //
     31  // to understand this code it is worthwile to generate a graphical visualization of the automaton (see PrintAutomaton).
     32  // If the code is compiled in debug mode the automaton produces a Graphviz file into the folder of the application
     33  // whenever an instance of the automaton is constructed.
     34  //
     35  // This class relies on two other classes:
     36  // - CodeGenerator to produce code for a stack-based evaluator and
     37  // - ConstraintHandler to restrict the allowed set of expressions.
     38  //
     39  // The ConstraintHandler extends the automaton and adds semantic restrictions for expressions produced by the automaton.
     40  //
     41  //
    3042  internal class Automaton {
    3143    public const int StateExpr = 1;
     
    5365    public const int StateInvTFStart = 23;
    5466    public const int StateInvTFEnd = 24;
    55     private const int FirstDynamicState = 25;
     67    public const int FirstDynamicState = 25;
     68    // more states for individual variables are created dynamically
    5669
    5770    private const int StartState = StateExpr;
     
    222235        () => {
    223236          codeGenerator.Emit1(OpCodes.LoadConst0);
    224         },
    225         "0");
     237          constraintHandler.StartNewTermInPoly();
     238        },
     239        "0, StartTermInPoly");
    226240      AddTransition(StateLogTEnd, StateLogFactorEnd,
    227241        () => {
     
    272286        () => {
    273287          codeGenerator.Emit1(OpCodes.LoadConst1);
    274         },
    275         "c");
     288          constraintHandler.StartNewTermInPoly();
     289        },
     290        "c, StartTermInPoly");
    276291      AddTransition(StateInvTEnd, StateInvFactorEnd,
    277292        () => {
     
    338353    private readonly int[] followStatesBuf = new int[1000];
    339354    public void FollowStates(int state, out int[] buf, out int nElements) {
    340       // return followStates[state]
    341       //   .Where(s => s < FirstDynamicState || s >= minVarIdx) // for variables we only allow non-decreasing state sequences
    342       //   // the following states imply an additional variable being added to the expression
    343       //   // F, Sum, Prod
    344       //   .Where(s => (s != StateF && s != StateSum && s != StateProd) || variablesRemaining > 0);
    345 
    346355      // for loop instead of where iterator
    347356      var fs = followStates[state];
    348357      int j = 0;
    349       //Console.Write(stateNames[CurrentState] + " allowed: ");
    350358      for (int i = 0; i < fs.Count; i++) {
    351359        var s = fs[i];
    352360        if (constraintHandler.IsAllowedFollowState(state, s)) {
    353           //Console.Write(s + " ");
    354361          followStatesBuf[j++] = s;
    355362        }
    356363      }
    357       //Console.WriteLine();
    358364      buf = followStatesBuf;
    359365      nElements = j;
     
    362368
    363369    public void Goto(int targetState) {
    364       //Console.WriteLine("->{0}", stateNames[targetState]);
    365       // Contract.Assert(FollowStates(CurrentState).Contains(targetState));
    366 
    367370      if (actions[CurrentState, targetState] != null)
    368371        actions[CurrentState, targetState].ForEach(a => a()); // execute all actions
     
    371374
    372375    public bool IsFinalState(int s) {
    373       return s == StateExprEnd;
     376      return s == StateExprEnd && !constraintHandler.IsInvalidExpression;
    374377    }
    375378
     
    389392        writer.WriteLine("digraph {");
    390393        // writer.WriteLine("rankdir=LR");
    391         int[] fs;
    392         int nFs;
    393394        for (int s = StartState; s < stateNames.Count; s++) {
    394395          for (int i = 0; i < followStates[s].Count; i++) {
Note: See TracChangeset for help on using the changeset viewer.