Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2796 worked on MCTS (removing constraint handling and introducing state unification instead)

File size: 1.9 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.Diagnostics.Contracts;
23
24namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
25  public class CodeGenerator {
26
27    public const int MaxCodeLength = 10000;
28    public const int MaxConstants = 100;
29
30    private readonly byte[] codeArray = new byte[MaxCodeLength];
31    private int pc;
32    private int nParams;
33
34    // for retrieving and restoring the PC
35    internal int ProgramCounter {
36      get { return pc; }
37      set { pc = value; }
38    }
39
40    public void Reset() {
41      pc = 0;
42      nParams = 0;
43    }
44
45    public void Emit1(OpCodes code) {
46      codeArray[pc++] = (byte)code;
47      if (code == OpCodes.LoadParamN) nParams++;
48    }
49
50    public void Emit2(OpCodes code, short arg) {
51      Contract.Assert(code == OpCodes.LoadVar); // only loadVar opcode has params
52      Emit1(code);
53      codeArray[pc++] = (byte)(arg >> 8);
54      codeArray[pc++] = (byte)(arg & 0xFF);
55    }
56
57    public void GetCode(out byte[] code, out int nParams) {
58      code = codeArray;
59      nParams = this.nParams;
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.