Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2796_SymbReg/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Disassembler.cs @ 17511

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

#2796 comments, simplifications, reviewed tests for structure enumeration (not working)

File size: 3.0 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.Text;
23
24namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
25  internal class Disassembler {
26    public static string CodeToString(byte[] code, double[] consts) {
27      var sb = new StringBuilder();
28      int pc = 0;
29      int nextParamIdx = -1;
30      while (code[pc] != (byte)OpCodes.Exit) {
31        var op = code[pc++];
32        switch (op) {
33          case (byte)OpCodes.Add: sb.Append(" + "); break;
34          case (byte)OpCodes.Mul: sb.Append(" * "); break;
35          case (byte)OpCodes.LoadConst1: sb.Append(" 1 "); break;
36          case (byte)OpCodes.LoadConst0: sb.Append(" 0 "); break;
37          case (byte)OpCodes.LoadParamN: sb.AppendFormat(" {0:N3} ", consts[++nextParamIdx]); break;
38          case (byte)OpCodes.LoadVar:
39          {
40              short arg = (short)((code[pc] << 8) | code[pc + 1]);
41              pc += 2;
42            sb.AppendFormat(" var{0} ", arg); break;
43          }
44          case (byte)OpCodes.Exp: sb.Append(" exp "); break;
45          case (byte)OpCodes.Log: sb.Append(" log "); break;
46          case (byte)OpCodes.Inv: sb.Append(" inv "); break;         
47        }
48      }
49      return sb.ToString();
50    }
51
52    public static string CodeToString(byte[] code) {
53      var sb = new StringBuilder();
54      int pc = 0;
55      int nextParamIdx = -1;
56      while (code[pc] != (byte)OpCodes.Exit) {
57        var op = code[pc++];
58        switch (op) {
59          case (byte)OpCodes.Add: sb.Append(" + "); break;
60          case (byte)OpCodes.Mul: sb.Append(" * "); break;
61          case (byte)OpCodes.LoadConst1: sb.Append(" 1 ");  break;
62          case (byte)OpCodes.LoadConst0: sb.Append(" 0 "); break;
63          case (byte)OpCodes.LoadParamN: sb.Append(" c "); break;
64          case (byte)OpCodes.LoadVar: {
65              short arg = (short)((code[pc] << 8) | code[pc + 1]);
66              pc += 2;
67              sb.AppendFormat("{0}", (char)('a'+arg)); break;
68            }
69          case (byte)OpCodes.Exp: sb.Append(" exp "); break;
70          case (byte)OpCodes.Log: sb.Append(" log "); break;
71          case (byte)OpCodes.Inv: sb.Append(" inv "); break;
72        }
73      }
74      return sb.ToString();
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.