Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15420 was 15420, checked in by gkronber, 5 years ago

#2796: debugging

File size: 17.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
24using System.Diagnostics;
25using System.IO;
26using System.Linq;
27
28namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
29  // this is the core class for generating expressions.
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  //
44  internal class Automaton {
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
69    public const int StateExpFStart = 15;
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;
81    public const int FirstDynamicState = 25;
82    // more states for individual variables are created dynamically
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;
92    private IConstraintHandler constraintHandler;
93
94    public Automaton(double[][] vars, IConstraintHandler constraintHandler,
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();
103      this.constraintHandler = constraintHandler;
104      BuildAutomaton(nVars, allowProdOfVars, allowExp, allowLog, allowInv, allowMultipleTerms);
105
106      Reset();
107#if DEBUG
108      PrintAutomaton();
109#endif
110    }
111
112    // postfix notation
113    // Expr -> 0 Term { '+' Term } '+' 'exit'
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
136      // Expr -> 0 Term { '+' Term } '+' 'exit'
137      AddTransition(StateExpr, StateTermStart, () => {
138        codeGenerator.Reset();
139        codeGenerator.Emit1(OpCodes.LoadConst0);
140        constraintHandler.Reset();
141      }, "0");
142      AddTransition(StateTermEnd, StateExprEnd, () => {
143        codeGenerator.Emit1(OpCodes.Add);
144        codeGenerator.Emit1(OpCodes.Exit);
145      }, "+ exit");
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");
158      AddTransition(StateFactorEnd, StateTermEnd,
159        () => {
160          codeGenerator.Emit1(OpCodes.Mul);
161          constraintHandler.EndTerm();
162        },
163        "*");
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        }, "");
175      if (allowExp)
176        AddTransition(StateFactorStart, StateExpFactorStart, () => {
177          constraintHandler.StartFactor(StateExpFactorStart);
178        }, "");
179      if (allowLog)
180        AddTransition(StateFactorStart, StateLogFactorStart, () => {
181          constraintHandler.StartFactor(StateLogFactorStart);
182        }, "");
183      if (allowInv)
184        AddTransition(StateFactorStart, StateInvFactorStart, () => {
185          constraintHandler.StartFactor(StateInvFactorStart);
186        }, "");
187      AddTransition(StateVariableFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "");
188      AddTransition(StateExpFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "");
189      AddTransition(StateLogFactorEnd, StateFactorEnd, () => { constraintHandler.EndFactor(); }, "");
190      AddTransition(StateInvFactorEnd, StateFactorEnd, () => { constraintHandler.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 + "");
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        },
221        "c*exp");
222      AddTransition(StateExpFEnd, StateExpFStart,
223        () => { },
224        "");
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 + "");
237        AddTransition(curDynVarState, StateExpFEnd,
238          () => {
239            codeGenerator.Emit1(OpCodes.Mul);
240          }, "*");
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);
249          constraintHandler.StartNewTermInPoly();
250        },
251        "0");
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 + "");
292        AddTransition(curDynVarState, StateLogTFEnd);
293        curDynVarState++;
294      }
295
296      // InvFact -> 1 InvT { '+' InvT } '+' 'inv'
297      AddTransition(StateInvFactorStart, StateInvTStart,
298        () => {
299          codeGenerator.Emit1(OpCodes.LoadConst1);
300          constraintHandler.StartNewTermInPoly();
301        },
302        "c");
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 + "");
341        AddTransition(curDynVarState, StateInvTFEnd);
342        curDynVarState++;
343      }
344
345      followStates[StateExprEnd] = new List<int>(); // no follow states
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      }
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      Debug.Assert(followStates[CurrentState].Contains(targetState));
388      if (actions[CurrentState, targetState] != null)
389        actions[CurrentState, targetState].ForEach(a => a()); // execute all actions
390      CurrentState = targetState;
391    }
392
393    public bool IsFinalState(int s) {
394      return s == StateExprEnd && !constraintHandler.IsInvalidExpression;
395    }
396
397    public bool IsEvalState(int v) {
398      return v == StateFactorEnd ||
399        v == StateLogTFEnd ||
400        v == StateInvTFEnd ||
401        v == StateExpFEnd
402        ;
403    }
404
405
406    // Always returns valid code.
407    // If the method is called in an intermediate state the expression is completed by
408    // taking the shortest route to the final state.
409    // After that state of the automaton is restored to the current state.
410    public void GetCode(out byte[] code, out int nParams) {
411      IConstraintHandler storedConstraintHandler = null;
412      int storedState = CurrentState;
413      int storedPC = codeGenerator.ProgramCounter;
414
415      if (!IsFinalState(CurrentState)) {
416        // save state and code,
417        // constraints are ignored while completing the expression
418        storedConstraintHandler = constraintHandler;
419        constraintHandler = new EmptyConstraintHandler();
420        storedState = CurrentState;
421        storedPC = codeGenerator.ProgramCounter;
422
423        // take shortest route to final state (smaller state values are closer to the final state)
424        while (!IsFinalState(CurrentState)) {
425          Debug.Assert(followStates[CurrentState][0] == followStates[CurrentState].Min());
426          var nextState = followStates[CurrentState][0];
427          Goto(nextState);
428        }
429      }
430
431      codeGenerator.GetCode(out code, out nParams);
432
433      // restore
434      if (storedConstraintHandler != null) {
435        constraintHandler = storedConstraintHandler;
436        CurrentState = storedState;
437        codeGenerator.ProgramCounter = storedPC;
438      }
439    }
440
441    public void Reset() {
442      CurrentState = StartState;
443      codeGenerator.Reset();
444      constraintHandler.Reset();
445    }
446
447    internal string GetActionString(int fromState, int toState) {
448      return actionStrings[fromState,toState] != null ? string.Join(" , ", actionStrings[fromState, toState]) : "";
449    }
450
451#if DEBUG
452    public void PrintAutomaton() {
453      using (var writer = new StreamWriter("automaton.gv")) {
454        writer.WriteLine("digraph {");
455        // writer.WriteLine("rankdir=LR");
456        for (int s = StartState; s < stateNames.Count; s++) {
457          for (int i = 0; i < followStates[s].Count; i++) {
458            if (followStates[s][i] <= 0) continue;
459            var followS = followStates[s][i];
460            var label = actionStrings[s, followS] != null ? string.Join(" , ", actionStrings[s, followS]) : "";
461            writer.WriteLine("{0} -> {1} [ label = \"{2}\" ];", stateNames[s], stateNames[followS], label);
462          }
463        }
464        writer.WriteLine("}");
465      }
466    }
467
468#endif
469  }
470}
Note: See TracBrowser for help on using the repository browser.