1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 |
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HEAL.Attic;
|
---|
11 |
|
---|
12 | using static HeuristicLab.Problems.DataAnalysis.Symbolic.BatchOperations;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
15 | [Item("SymbolicDataAnalysisExpressionTreeBatchInterpreter", "An interpreter that uses batching and vectorization techniques to achieve faster performance.")]
|
---|
16 | [StorableType("BEB15146-BB95-4838-83AC-6838543F017B")]
|
---|
17 | public class SymbolicDataAnalysisExpressionTreeBatchInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
18 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
19 |
|
---|
20 | #region parameters
|
---|
21 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
22 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
23 | }
|
---|
24 | #endregion
|
---|
25 |
|
---|
26 | #region properties
|
---|
27 | public int EvaluatedSolutions {
|
---|
28 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
29 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
30 | }
|
---|
31 | #endregion
|
---|
32 |
|
---|
33 | public void ClearState() { }
|
---|
34 |
|
---|
35 | public SymbolicDataAnalysisExpressionTreeBatchInterpreter() {
|
---|
36 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(StorableConstructorFlag _) : base(_) { }
|
---|
41 | protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(SymbolicDataAnalysisExpressionTreeBatchInterpreter original, Cloner cloner) : base(original, cloner) {
|
---|
42 | }
|
---|
43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
44 | return new SymbolicDataAnalysisExpressionTreeBatchInterpreter(this, cloner);
|
---|
45 | }
|
---|
46 |
|
---|
47 | private void LoadData(BatchInstruction instr, int[] rows, int rowIndex, int batchSize) {
|
---|
48 | for (int i = 0; i < batchSize; ++i) {
|
---|
49 | var row = rows[rowIndex] + i;
|
---|
50 | instr.buf[i] = instr.weight * instr.data[row];
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void Evaluate(BatchInstruction[] code, int[] rows, int rowIndex, int batchSize) {
|
---|
55 | for (int i = code.Length - 1; i >= 0; --i) {
|
---|
56 | var instr = code[i];
|
---|
57 | var c = instr.childIndex;
|
---|
58 | var n = instr.narg;
|
---|
59 |
|
---|
60 | switch (instr.opcode) {
|
---|
61 | case OpCodes.Variable: {
|
---|
62 | LoadData(instr, rows, rowIndex, batchSize);
|
---|
63 | break;
|
---|
64 | }
|
---|
65 |
|
---|
66 | case OpCodes.Add: {
|
---|
67 | Load(instr.buf, code[c].buf);
|
---|
68 | for (int j = 1; j < n; ++j) {
|
---|
69 | Add(instr.buf, code[c + j].buf);
|
---|
70 | }
|
---|
71 | break;
|
---|
72 | }
|
---|
73 |
|
---|
74 | case OpCodes.Sub: {
|
---|
75 | if (n == 1) {
|
---|
76 | Neg(instr.buf, code[c].buf);
|
---|
77 | } else {
|
---|
78 | Load(instr.buf, code[c].buf);
|
---|
79 | for (int j = 1; j < n; ++j) {
|
---|
80 | Sub(instr.buf, code[c + j].buf);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | break;
|
---|
84 | }
|
---|
85 |
|
---|
86 | case OpCodes.Mul: {
|
---|
87 | Load(instr.buf, code[c].buf);
|
---|
88 | for (int j = 1; j < n; ++j) {
|
---|
89 | Mul(instr.buf, code[c + j].buf);
|
---|
90 | }
|
---|
91 | break;
|
---|
92 | }
|
---|
93 |
|
---|
94 | case OpCodes.Div: {
|
---|
95 | if (n == 1) {
|
---|
96 | Inv(instr.buf, code[c].buf);
|
---|
97 | } else {
|
---|
98 | Load(instr.buf, code[c].buf);
|
---|
99 | for (int j = 1; j < n; ++j) {
|
---|
100 | Div(instr.buf, code[c + j].buf);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | break;
|
---|
104 | }
|
---|
105 |
|
---|
106 | case OpCodes.Square: {
|
---|
107 | Square(instr.buf, code[c].buf);
|
---|
108 | break;
|
---|
109 | }
|
---|
110 |
|
---|
111 | case OpCodes.Root: {
|
---|
112 | Load(instr.buf, code[c].buf);
|
---|
113 | Root(instr.buf, code[c + 1].buf);
|
---|
114 | break;
|
---|
115 | }
|
---|
116 |
|
---|
117 | case OpCodes.SquareRoot: {
|
---|
118 | Sqrt(instr.buf, code[c].buf);
|
---|
119 | break;
|
---|
120 | }
|
---|
121 |
|
---|
122 | case OpCodes.Cube: {
|
---|
123 | Cube(instr.buf, code[c].buf);
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | case OpCodes.CubeRoot: {
|
---|
127 | CubeRoot(instr.buf, code[c].buf);
|
---|
128 | break;
|
---|
129 | }
|
---|
130 |
|
---|
131 | case OpCodes.Power: {
|
---|
132 | Load(instr.buf, code[c].buf);
|
---|
133 | Pow(instr.buf, code[c + 1].buf);
|
---|
134 | break;
|
---|
135 | }
|
---|
136 |
|
---|
137 | case OpCodes.Exp: {
|
---|
138 | Exp(instr.buf, code[c].buf);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 |
|
---|
142 | case OpCodes.Log: {
|
---|
143 | Log(instr.buf, code[c].buf);
|
---|
144 | break;
|
---|
145 | }
|
---|
146 |
|
---|
147 | case OpCodes.Sin: {
|
---|
148 | Sin(instr.buf, code[c].buf);
|
---|
149 | break;
|
---|
150 | }
|
---|
151 |
|
---|
152 | case OpCodes.Cos: {
|
---|
153 | Cos(instr.buf, code[c].buf);
|
---|
154 | break;
|
---|
155 | }
|
---|
156 |
|
---|
157 | case OpCodes.Tan: {
|
---|
158 | Tan(instr.buf, code[c].buf);
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case OpCodes.Tanh: {
|
---|
162 | Tanh(instr.buf, code[c].buf);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | case OpCodes.Absolute: {
|
---|
166 | Absolute(instr.buf, code[c].buf);
|
---|
167 | break;
|
---|
168 | }
|
---|
169 |
|
---|
170 | case OpCodes.AnalyticQuotient: {
|
---|
171 | Load(instr.buf, code[c].buf);
|
---|
172 | AnalyticQuotient(instr.buf, code[c + 1].buf);
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | private readonly object syncRoot = new object();
|
---|
180 |
|
---|
181 | [ThreadStatic]
|
---|
182 | private Dictionary<string, double[]> cachedData;
|
---|
183 |
|
---|
184 | [ThreadStatic]
|
---|
185 | private IDataset dataset;
|
---|
186 |
|
---|
187 | private void InitCache(IDataset dataset) {
|
---|
188 | this.dataset = dataset;
|
---|
189 | cachedData = new Dictionary<string, double[]>();
|
---|
190 | foreach (var v in dataset.DoubleVariables) {
|
---|
191 | cachedData[v] = dataset.GetDoubleValues(v).ToArray();
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void InitializeState() {
|
---|
196 | cachedData = null;
|
---|
197 | dataset = null;
|
---|
198 | EvaluatedSolutions = 0;
|
---|
199 | }
|
---|
200 |
|
---|
201 | private double[] GetValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
|
---|
202 | if (cachedData == null || this.dataset != dataset) {
|
---|
203 | InitCache(dataset);
|
---|
204 | }
|
---|
205 |
|
---|
206 | var code = Compile(tree, dataset, OpCodes.MapSymbolToOpCode);
|
---|
207 | var remainingRows = rows.Length % BATCHSIZE;
|
---|
208 | var roundedTotal = rows.Length - remainingRows;
|
---|
209 |
|
---|
210 | var result = new double[rows.Length];
|
---|
211 |
|
---|
212 | for (int rowIndex = 0; rowIndex < roundedTotal; rowIndex += BATCHSIZE) {
|
---|
213 | Evaluate(code, rows, rowIndex, BATCHSIZE);
|
---|
214 | Array.Copy(code[0].buf, 0, result, rowIndex, BATCHSIZE);
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (remainingRows > 0) {
|
---|
218 | Evaluate(code, rows, roundedTotal, remainingRows);
|
---|
219 | Array.Copy(code[0].buf, 0, result, roundedTotal, remainingRows);
|
---|
220 | }
|
---|
221 |
|
---|
222 | // when evaluation took place without any error, we can increment the counter
|
---|
223 | lock (syncRoot) {
|
---|
224 | EvaluatedSolutions++;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return result;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
|
---|
231 | return GetValues(tree, dataset, rows);
|
---|
232 | }
|
---|
233 |
|
---|
234 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
235 | return GetSymbolicExpressionTreeValues(tree, dataset, rows.ToArray());
|
---|
236 | }
|
---|
237 |
|
---|
238 | private BatchInstruction[] Compile(ISymbolicExpressionTree tree, IDataset dataset, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
|
---|
239 | var root = tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
240 | var code = new BatchInstruction[root.GetLength()];
|
---|
241 | if (root.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
|
---|
242 | int c = 1, i = 0;
|
---|
243 | foreach (var node in root.IterateNodesBreadth()) {
|
---|
244 | if (node.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
|
---|
245 | code[i] = new BatchInstruction {
|
---|
246 | opcode = opCodeMapper(node),
|
---|
247 | narg = (ushort)node.SubtreeCount,
|
---|
248 | buf = new double[BATCHSIZE],
|
---|
249 | childIndex = c
|
---|
250 | };
|
---|
251 | if (node is VariableTreeNode variable) {
|
---|
252 | code[i].weight = variable.Weight;
|
---|
253 | if (cachedData.ContainsKey(variable.VariableName)) {
|
---|
254 | code[i].data = cachedData[variable.VariableName];
|
---|
255 | } else {
|
---|
256 | code[i].data = dataset.GetReadOnlyDoubleValues(variable.VariableName).ToArray();
|
---|
257 | cachedData[variable.VariableName] = code[i].data;
|
---|
258 | }
|
---|
259 | } else if (node is ConstantTreeNode constant) {
|
---|
260 | code[i].value = constant.Value;
|
---|
261 | for (int j = 0; j < BATCHSIZE; ++j)
|
---|
262 | code[i].buf[j] = code[i].value;
|
---|
263 | }
|
---|
264 | c += node.SubtreeCount;
|
---|
265 | ++i;
|
---|
266 | }
|
---|
267 | return code;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|