[16285] | 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;
|
---|
[16654] | 10 | using HEAL.Attic;
|
---|
[16285] | 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.")]
|
---|
[16654] | 16 | [StorableType("BEB15146-BB95-4838-83AC-6838543F017B")]
|
---|
[16285] | 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]
|
---|
[16654] | 40 | protected SymbolicDataAnalysisExpressionTreeBatchInterpreter(StorableConstructorFlag _) : base(_) { }
|
---|
[16285] | 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 | }
|
---|
[16293] | 65 |
|
---|
[16285] | 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 | }
|
---|
[16293] | 83 | break;
|
---|
[16285] | 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 | }
|
---|
[16293] | 103 | break;
|
---|
[16285] | 104 | }
|
---|
| 105 |
|
---|
[16293] | 106 | case OpCodes.Square: {
|
---|
| 107 | Square(instr.buf, code[c].buf);
|
---|
| 108 | break;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | case OpCodes.Root: {
|
---|
[16356] | 112 | Load(instr.buf, code[c].buf);
|
---|
| 113 | Root(instr.buf, code[c + 1].buf);
|
---|
[16293] | 114 | break;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | case OpCodes.SquareRoot: {
|
---|
| 118 | Sqrt(instr.buf, code[c].buf);
|
---|
| 119 | break;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[16356] | 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 |
|
---|
[16293] | 131 | case OpCodes.Power: {
|
---|
[16356] | 132 | Load(instr.buf, code[c].buf);
|
---|
| 133 | Pow(instr.buf, code[c + 1].buf);
|
---|
[16293] | 134 | break;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[16285] | 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 | }
|
---|
[16293] | 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 | }
|
---|
[16531] | 161 | case OpCodes.Tanh: {
|
---|
| 162 | Tanh(instr.buf, code[c].buf);
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
[16356] | 165 | case OpCodes.Absolute: {
|
---|
| 166 | Absolute(instr.buf, code[c].buf);
|
---|
| 167 | break;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[16360] | 170 | case OpCodes.AnalyticQuotient: {
|
---|
[16356] | 171 | Load(instr.buf, code[c].buf);
|
---|
| 172 | AnalyticQuotient(instr.buf, code[c + 1].buf);
|
---|
| 173 | break;
|
---|
| 174 | }
|
---|
[16285] | 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[16654] | 179 | private readonly object syncRoot = new object();
|
---|
| 180 |
|
---|
[16296] | 181 | [ThreadStatic]
|
---|
| 182 | private Dictionary<string, double[]> cachedData;
|
---|
| 183 |
|
---|
[16654] | 184 | [ThreadStatic]
|
---|
| 185 | private IDataset dataset;
|
---|
| 186 |
|
---|
[16296] | 187 | private void InitCache(IDataset dataset) {
|
---|
[16654] | 188 | this.dataset = dataset;
|
---|
[16296] | 189 | cachedData = new Dictionary<string, double[]>();
|
---|
| 190 | foreach (var v in dataset.DoubleVariables) {
|
---|
[16301] | 191 | cachedData[v] = dataset.GetDoubleValues(v).ToArray();
|
---|
[16296] | 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | public void InitializeState() {
|
---|
| 196 | cachedData = null;
|
---|
[16654] | 197 | dataset = null;
|
---|
[16296] | 198 | EvaluatedSolutions = 0;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[16285] | 201 | private double[] GetValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
|
---|
[16654] | 202 | if (cachedData == null || this.dataset != dataset) {
|
---|
| 203 | InitCache(dataset);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[16285] | 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 |
|
---|
[16654] | 222 | // when evaluation took place without any error, we can increment the counter
|
---|
| 223 | lock (syncRoot) {
|
---|
| 224 | EvaluatedSolutions++;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[16285] | 227 | return result;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[16293] | 230 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
|
---|
| 231 | return GetValues(tree, dataset, rows);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[16285] | 234 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
[16296] | 235 | return GetSymbolicExpressionTreeValues(tree, dataset, rows.ToArray());
|
---|
[16285] | 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()) {
|
---|
[16296] | 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 | };
|
---|
[16285] | 251 | if (node is VariableTreeNode variable) {
|
---|
| 252 | code[i].weight = variable.Weight;
|
---|
[16296] | 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 | }
|
---|
[16285] | 259 | } else if (node is ConstantTreeNode constant) {
|
---|
| 260 | code[i].value = constant.Value;
|
---|
[16287] | 261 | for (int j = 0; j < BATCHSIZE; ++j)
|
---|
| 262 | code[i].buf[j] = code[i].value;
|
---|
[16285] | 263 | }
|
---|
| 264 | c += node.SubtreeCount;
|
---|
| 265 | ++i;
|
---|
| 266 | }
|
---|
| 267 | return code;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | }
|
---|