Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs @ 15414

Last change on this file since 15414 was 15414, checked in by gkronber, 7 years ago

#2796 worked on MCTS

File size: 17.3 KB
RevLine 
[13645]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13645]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
[15410]24using System.Diagnostics;
[13645]25using System.IO;
[15410]26using System.Linq;
[13645]27
28namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
29  // this is the core class for generating expressions.
[13651]30  // it represents a finite state automaton, each state transition can be associated with an action (e.g. to produce code).
31  // the automaton determines the possible structures for expressions.
32  //
33  // to understand this code it is worthwile to generate a graphical visualization of the automaton (see PrintAutomaton).
34  // If the code is compiled in debug mode the automaton produces a Graphviz file into the folder of the application
35  // whenever an instance of the automaton is constructed.
36  //
37  // This class relies on two other classes:
38  // - CodeGenerator to produce code for a stack-based evaluator and
39  // - ConstraintHandler to restrict the allowed set of expressions.
40  //
41  // The ConstraintHandler extends the automaton and adds semantic restrictions for expressions produced by the automaton.
42  //
43  //
[13645]44  internal class Automaton {
[15410]45    // TODO: refactor so that State is an enumerable type
46
47    // there is a single final state (ExprEnd)
48    // states with lower values are closer to the final state
49    // (this is helpful when we try to navigate to the final state)
50    public const int StateExprEnd = 1;
51    public const int StateTermEnd = 2;
52    public const int StateFactorEnd = 3;
53
54    public const int StateVariableFactorEnd = 4;
55    public const int StateExpFactorEnd = 5;
56    public const int StateLogFactorEnd = 6;
57    public const int StateInvFactorEnd = 7;
58
59    public const int StateExpFEnd = 8;
60    public const int StateLogTEnd = 9;
61    public const int StateInvTEnd = 10;
62
63    public const int StateLogTFEnd = 11;
64    public const int StateInvTFEnd = 12;
65
66    public const int StateLogTFStart = 13;
67    public const int StateInvTFStart = 14;
68
[13645]69    public const int StateExpFStart = 15;
[15410]70    public const int StateLogTStart = 16;
71    public const int StateInvTStart = 17;
72
73    public const int StateVariableFactorStart = 18;
74    public const int StateExpFactorStart = 19;
75    public const int StateLogFactorStart = 20;
76    public const int StateInvFactorStart = 21;
77
78    public const int StateFactorStart = 22;
79    public const int StateTermStart = 23;
80    public const int StateExpr = 24;
[13651]81    public const int FirstDynamicState = 25;
82    // more states for individual variables are created dynamically
[13645]83
84    private const int StartState = StateExpr;
85    public int CurrentState { get; private set; }
86
87    public readonly List<string> stateNames;
88    private List<int>[] followStates;
89    private List<Action>[,] actions; // not every follow state is possible but this representation should be efficient
90    private List<string>[,] actionStrings; // just for printing
91    private readonly CodeGenerator codeGenerator;
[15410]92    private IConstraintHandler constraintHandler;
[13645]93
[15410]94    public Automaton(double[][] vars, IConstraintHandler constraintHandler,
[13645]95       bool allowProdOfVars = true,
96       bool allowExp = true,
97       bool allowLog = true,
98       bool allowInv = true,
99       bool allowMultipleTerms = false) {
100      int nVars = vars.Length;
101      stateNames = new List<string>() { string.Empty, "Expr", "ExprEnd", "TermStart", "TermEnd", "FactorStart", "FactorEnd", "VarFactorStart", "VarFactorEnd", "ExpFactorStart", "ExpFactorEnd", "LogFactorStart", "LogFactorEnd", "InvFactorStart", "InvFactorEnd", "ExpFStart", "ExpFEnd", "LogTStart", "LogTEnd", "LogTFStart", "LogTFEnd", "InvTStart", "InvTEnd", "InvTFStart", "InvTFEnd" };
102      codeGenerator = new CodeGenerator();
[15410]103      this.constraintHandler = constraintHandler;
[13645]104      BuildAutomaton(nVars, allowProdOfVars, allowExp, allowLog, allowInv, allowMultipleTerms);
105
106      Reset();
107#if DEBUG
108      PrintAutomaton();
109#endif
110    }
111
[15410]112    // postfix notation
[15403]113    // Expr -> 0 Term { '+' Term } '+' 'exit'
[13645]114    // Term -> c Fact { '*' Fact } '*'
115    // Fact -> VarFact | ExpFact | LogFact | InvFact
116    // VarFact -> var_1 ... var_n
117    // ExpFact -> 1 ExpF { '*' ExpF } '*' c '*' 'exp' // c must be at end to allow scaling in evaluator
118    // ExpF    -> var_1 ... var_n
119    // LogFact -> 0 LogT { '+' LogT } '+' c '+' 'log' // c must be at end to allow scaling in evaluator
120    // LogT    -> c LogTF { '*' LogTF } '*'
121    // LogTF   -> var_1 ... var_n
122    // InvFact -> 1 InvT { '+' InvT } '+' 'inv'
123    // InvT    -> (var_1 ... var_n) c '*'
124    private void BuildAutomaton(int nVars,
125      bool allowProdOfVars = true,
126       bool allowExp = true,
127       bool allowLog = true,
128       bool allowInv = true,
129       bool allowMultipleTerms = false) {
130
131      int nStates = FirstDynamicState + 4 * nVars;
132      followStates = new List<int>[nStates];
133      actions = new List<Action>[nStates, nStates];
134      actionStrings = new List<string>[nStates, nStates];
135
[15403]136      // Expr -> 0 Term { '+' Term } '+' 'exit'
[13645]137      AddTransition(StateExpr, StateTermStart, () => {
138        codeGenerator.Reset();
139        codeGenerator.Emit1(OpCodes.LoadConst0);
140        constraintHandler.Reset();
[15403]141      }, "0, Reset");
[13645]142      AddTransition(StateTermEnd, StateExprEnd, () => {
143        codeGenerator.Emit1(OpCodes.Add);
144        codeGenerator.Emit1(OpCodes.Exit);
[15403]145      }, "+ exit");
[13645]146      if (allowMultipleTerms)
147        AddTransition(StateTermEnd, StateTermStart, () => {
148          codeGenerator.Emit1(OpCodes.Add);
149        }, "+");
150
151      // Term -> c Fact { '*' Fact } '*'
152      AddTransition(StateTermStart, StateFactorStart,
153        () => {
154          codeGenerator.Emit1(OpCodes.LoadParamN);
155          constraintHandler.StartTerm();
156        },
157        "c, StartTerm");
158      AddTransition(StateFactorEnd, StateTermEnd,
159        () => {
160          codeGenerator.Emit1(OpCodes.Mul);
161          constraintHandler.EndTerm();
162        },
163        "*, EndTerm");
164
165      AddTransition(StateFactorEnd, StateFactorStart,
166        () => { codeGenerator.Emit1(OpCodes.Mul); },
167        "*");
168
169
170      // Fact -> VarFact | ExpFact | LogFact | InvFact
171      if (allowProdOfVars)
172        AddTransition(StateFactorStart, StateVariableFactorStart, () => {
173          constraintHandler.StartFactor(StateVariableFactorStart);
174        }, "StartFactor");
175      if (allowExp)
176        AddTransition(StateFactorStart, StateExpFactorStart, () => {
177          constraintHandler.StartFactor(StateExpFactorStart);
178        }, "StartFactor");
179      if (allowLog)
180        AddTransition(StateFactorStart, StateLogFactorStart, () => {
181          constraintHandler.StartFactor(StateLogFactorStart);
182        }, "StartFactor");
183      if (allowInv)
184        AddTransition(StateFactorStart, StateInvFactorStart, () => {
185          constraintHandler.StartFactor(StateInvFactorStart);
186        }, "StartFactor");
187      AddTransition(StateVariableFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "EndFactor");
188      AddTransition(StateExpFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "EndFactor");
189      AddTransition(StateLogFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "EndFactor");
190      AddTransition(StateInvFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "EndFactor");
191
192      // VarFact -> var_1 ... var_n
193      // add dynamic states for each variable
194      int curDynVarState = FirstDynamicState;
195      for (int i = 0; i < nVars; i++) {
196        short varIdx = (short)i;
197        var varState = curDynVarState;
198        stateNames.Add("var_1");
199        AddTransition(StateVariableFactorStart, curDynVarState,
200          () => {
201            codeGenerator.Emit2(OpCodes.LoadVar, varIdx);
202            constraintHandler.AddVarToCurrentFactor(varState);
203          },
204          "var_" + varIdx + ", AddVar");
205        AddTransition(curDynVarState, StateVariableFactorEnd);
206        curDynVarState++;
207      }
208
209      // ExpFact -> 1 ExpF { '*' ExpF } '*' c '*' 'exp'
210      AddTransition(StateExpFactorStart, StateExpFStart,
211        () => {
212          codeGenerator.Emit1(OpCodes.LoadConst1);
213        },
214        "1");
215      AddTransition(StateExpFEnd, StateExpFactorEnd,
216        () => {
217          codeGenerator.Emit1(OpCodes.LoadParamN);
218          codeGenerator.Emit1(OpCodes.Mul);
219          codeGenerator.Emit1(OpCodes.Exp);
220        },
[15410]221        "c*exp");
[13645]222      AddTransition(StateExpFEnd, StateExpFStart,
[15410]223        () => { },
224        "");
[13645]225
226      // ExpF    -> var_1 ... var_n
227      for (int i = 0; i < nVars; i++) {
228        short varIdx = (short)i;
229        int varState = curDynVarState;
230        stateNames.Add("var_2");
231        AddTransition(StateExpFStart, curDynVarState,
232          () => {
233            codeGenerator.Emit2(OpCodes.LoadVar, varIdx);
234            constraintHandler.AddVarToCurrentFactor(varState);
235          },
236          "var_" + varIdx + ", AddVar");
[15410]237        AddTransition(curDynVarState, StateExpFEnd,
238          () => {
239            codeGenerator.Emit1(OpCodes.Mul);
240          }, "*");
[13645]241        curDynVarState++;
242      }
243
244      // must have c at end because of adjustment of c in evaluator
245      // LogFact -> 0 LogT { '+' LogT } '+' c '+' 'log'
246      AddTransition(StateLogFactorStart, StateLogTStart,
247        () => {
248          codeGenerator.Emit1(OpCodes.LoadConst0);
[13651]249          constraintHandler.StartNewTermInPoly();
[13645]250        },
[13651]251        "0, StartTermInPoly");
[13645]252      AddTransition(StateLogTEnd, StateLogFactorEnd,
253        () => {
254          codeGenerator.Emit1(OpCodes.Add);
255          codeGenerator.Emit1(OpCodes.LoadParamN);
256          codeGenerator.Emit1(OpCodes.Add);
257          codeGenerator.Emit1(OpCodes.Log);
258        },
259        "+c+log");
260      AddTransition(StateLogTEnd, StateLogTStart,
261        () => { codeGenerator.Emit1(OpCodes.Add); },
262        "+");
263
264      // LogT    -> c LogTF { '*' LogTF } '*'
265      AddTransition(StateLogTStart, StateLogTFStart,
266        () => {
267          codeGenerator.Emit1(OpCodes.LoadParamN);
268        },
269        "c");
270      AddTransition(StateLogTFEnd, StateLogTEnd,
271        () => {
272          codeGenerator.Emit1(OpCodes.Mul);
273        },
274        "*");
275      AddTransition(StateLogTFEnd, StateLogTFStart,
276        () => {
277          codeGenerator.Emit1(OpCodes.Mul);
278        },
279        "*");
280
281      // LogTF   -> var_1 ... var_n
282      for (int i = 0; i < nVars; i++) {
283        short varIdx = (short)i;
284        int varState = curDynVarState;
285        stateNames.Add("var_3");
286        AddTransition(StateLogTFStart, curDynVarState,
287          () => {
288            codeGenerator.Emit2(OpCodes.LoadVar, varIdx);
289            constraintHandler.AddVarToCurrentFactor(varState);
290          },
291          "var_" + varIdx + ", AddVar");
292        AddTransition(curDynVarState, StateLogTFEnd);
293        curDynVarState++;
294      }
295
296      // InvFact -> 1 InvT { '+' InvT } '+' 'inv'
297      AddTransition(StateInvFactorStart, StateInvTStart,
298        () => {
299          codeGenerator.Emit1(OpCodes.LoadConst1);
[13651]300          constraintHandler.StartNewTermInPoly();
[13645]301        },
[13651]302        "c, StartTermInPoly");
[13645]303      AddTransition(StateInvTEnd, StateInvFactorEnd,
304        () => {
305          codeGenerator.Emit1(OpCodes.Add);
306          codeGenerator.Emit1(OpCodes.Inv);
307        },
308        "+inv");
309      AddTransition(StateInvTEnd, StateInvTStart,
310        () => { codeGenerator.Emit1(OpCodes.Add); },
311        "+");
312
313      // InvT    -> c InvTF { '*' InvTF } '*'
314      AddTransition(StateInvTStart, StateInvTFStart,
315        () => {
316          codeGenerator.Emit1(OpCodes.LoadParamN);
317        },
318        "c");
319      AddTransition(StateInvTFEnd, StateInvTEnd,
320        () => {
321          codeGenerator.Emit1(OpCodes.Mul);
322        },
323        "*");
324      AddTransition(StateInvTFEnd, StateInvTFStart,
325        () => {
326          codeGenerator.Emit1(OpCodes.Mul);
327        },
328        "*");
329
330      // InvTF    -> (var_1 ... var_n) c '*'
331      for (int i = 0; i < nVars; i++) {
332        short varIdx = (short)i;
333        int varState = curDynVarState;
334        stateNames.Add("var_4");
335        AddTransition(StateInvTFStart, curDynVarState,
336          () => {
337            codeGenerator.Emit2(OpCodes.LoadVar, varIdx);
338            constraintHandler.AddVarToCurrentFactor(varState);
339          },
340          "var_" + varIdx + ", AddVar");
341        AddTransition(curDynVarState, StateInvTFEnd);
342        curDynVarState++;
343      }
344
345      followStates[StateExprEnd] = new List<int>(); // no follow states
[15410]346
347      // order all followstates (the first follow state leads to the final state)
348      foreach (var list in followStates) {
349        if (list != null)
350          list.Sort();
351      }
[13645]352    }
353
354    private void AddTransition(int fromState, int toState) {
355      if (followStates[fromState] == null) followStates[fromState] = new List<int>();
356      followStates[fromState].Add(toState);
357    }
358    private void AddTransition(int fromState, int toState, Action action, string str) {
359      if (followStates[fromState] == null) followStates[fromState] = new List<int>();
360      followStates[fromState].Add(toState);
361
362      if (actions[fromState, toState] == null) {
363        actions[fromState, toState] = new List<Action>();
364        actionStrings[fromState, toState] = new List<string>();
365      }
366
367      actions[fromState, toState].Add(action);
368      actionStrings[fromState, toState].Add(str);
369    }
370
371    private readonly int[] followStatesBuf = new int[1000];
372    public void FollowStates(int state, out int[] buf, out int nElements) {
373      var fs = followStates[state];
374      int j = 0;
375      for (int i = 0; i < fs.Count; i++) {
376        var s = fs[i];
377        if (constraintHandler.IsAllowedFollowState(state, s)) {
378          followStatesBuf[j++] = s;
379        }
380      }
381      buf = followStatesBuf;
382      nElements = j;
383    }
384
385
386    public void Goto(int targetState) {
387      if (actions[CurrentState, targetState] != null)
388        actions[CurrentState, targetState].ForEach(a => a()); // execute all actions
389      CurrentState = targetState;
390    }
391
392    public bool IsFinalState(int s) {
[13651]393      return s == StateExprEnd && !constraintHandler.IsInvalidExpression;
[13645]394    }
395
[15410]396    public bool IsEvalState(int v) {
397      return v == StateFactorEnd ||
398        v == StateLogTFEnd ||
399        v == StateInvTFEnd ||
[15414]400        v == StateExpFEnd
401        ;
[15410]402    }
403
404
405    // Always returns valid code.
406    // If the method is called in an intermediate state the expression is completed by
407    // taking the shortest route to the final state.
408    // After that state of the automaton is restored to the current state.
[13645]409    public void GetCode(out byte[] code, out int nParams) {
[15410]410      IConstraintHandler storedConstraintHandler = null;
411      int storedState = CurrentState;
412      int storedPC = codeGenerator.ProgramCounter;
413
414      if (!IsFinalState(CurrentState)) {
415        // save state and code,
416        // constraints are ignored while completing the expression
417        storedConstraintHandler = constraintHandler;
418        constraintHandler = new EmptyConstraintHandler();
419        storedState = CurrentState;
420        storedPC = codeGenerator.ProgramCounter;
421
422        // take shortest route to final state (smaller state values are closer to the final state)
423        while (!IsFinalState(CurrentState)) {
424          Debug.Assert(followStates[CurrentState][0] == followStates[CurrentState].Min());
425          var nextState = followStates[CurrentState][0];
426          Goto(nextState);
427        }
428      }
429
[13645]430      codeGenerator.GetCode(out code, out nParams);
[15410]431
432      // restore
433      if (storedConstraintHandler != null) {
434        constraintHandler = storedConstraintHandler;
435        CurrentState = storedState;
436        codeGenerator.ProgramCounter = storedPC;
437      }
[13645]438    }
439
440    public void Reset() {
441      CurrentState = StartState;
442      codeGenerator.Reset();
443      constraintHandler.Reset();
444    }
445
446#if DEBUG
447    public void PrintAutomaton() {
448      using (var writer = new StreamWriter("automaton.gv")) {
449        writer.WriteLine("digraph {");
450        // writer.WriteLine("rankdir=LR");
451        for (int s = StartState; s < stateNames.Count; s++) {
452          for (int i = 0; i < followStates[s].Count; i++) {
453            if (followStates[s][i] <= 0) continue;
454            var followS = followStates[s][i];
455            var label = actionStrings[s, followS] != null ? string.Join(" , ", actionStrings[s, followS]) : "";
456            writer.WriteLine("{0} -> {1} [ label = \"{2}\" ];", stateNames[s], stateNames[followS], label);
457          }
458        }
459        writer.WriteLine("}");
460      }
461    }
[15410]462
[13645]463#endif
464  }
465}
Note: See TracBrowser for help on using the repository browser.