source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/CodeGenerator.cs @ 13645

Last change on this file since 13645 was 13645, checked in by gkronber, 3 years ago

#2581: added an MCTS for symbolic regression models

File size: 1.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System.Diagnostics.Contracts;
24
25namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
26  internal class CodeGenerator {
27
28    public const int MaxCodeLength = 10000;
29    public const int MaxConstants = 100;
30
31    private readonly byte[] codeArray = new byte[MaxCodeLength];
32    private int pc;
33    private int nParams;
34
35    public void Reset() {
36      pc = 0;
37      nParams = 0;
38    }
39
40    public void Emit1(OpCodes code) {
41      codeArray[pc++] = (byte)code;
42      if (code == OpCodes.LoadParamN) nParams++;
43    }
44
45    public void Emit2(OpCodes code, short arg) {
46      Contract.Assert(code == OpCodes.LoadVar); // only loadVar opcode has params
47      Emit1(code);
48      codeArray[pc++] = (byte)(arg >> 8);
49      codeArray[pc++] = (byte)(arg & 0xFF);
50    }
51
52    public void GetCode(out byte[] code, out int nParams) {
53      code = codeArray;
54      nParams = this.nParams;
55    }
56  }
57}
Note: See TracBrowser for help on using the repository browser.