[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;
|
---|
[16565] | 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.")]
|
---|
[16565] | 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]
|
---|
[16565] | 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 | }
|
---|
[16722] | 53 | private void SubTreeEvoluate(BatchInstruction instr, int[] rows, int rowIndex, int batchSize) {
|
---|
| 54 | for (int i = 0; i < batchSize; ++i) {
|
---|
| 55 | var row = rows[rowIndex] + i;
|
---|
| 56 | instr.buf[i] = instr.data[row]; // не забыть заполнить
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
[16285] | 59 | private void Evaluate(BatchInstruction[] code, int[] rows, int rowIndex, int batchSize) {
|
---|
| 60 | for (int i = code.Length - 1; i >= 0; --i) {
|
---|
| 61 | var instr = code[i];
|
---|
| 62 | var c = instr.childIndex;
|
---|
| 63 | var n = instr.narg;
|
---|
| 64 |
|
---|
| 65 | switch (instr.opcode) {
|
---|
[16899] | 66 | case OpCode.Variable: {
|
---|
[16285] | 67 | LoadData(instr, rows, rowIndex, batchSize);
|
---|
| 68 | break;
|
---|
| 69 | }
|
---|
[16899] | 70 | case OpCode.TreeModel: {
|
---|
[16722] | 71 | SubTreeEvoluate(instr, rows, rowIndex, batchSize);
|
---|
| 72 | break;
|
---|
| 73 | }
|
---|
[16293] | 74 |
|
---|
[16899] | 75 | case OpCode.Add: {
|
---|
[16285] | 76 | Load(instr.buf, code[c].buf);
|
---|
| 77 | for (int j = 1; j < n; ++j) {
|
---|
| 78 | Add(instr.buf, code[c + j].buf);
|
---|
| 79 | }
|
---|
| 80 | break;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[16899] | 83 | case OpCode.Sub: {
|
---|
[16285] | 84 | if (n == 1) {
|
---|
| 85 | Neg(instr.buf, code[c].buf);
|
---|
| 86 | } else {
|
---|
| 87 | Load(instr.buf, code[c].buf);
|
---|
| 88 | for (int j = 1; j < n; ++j) {
|
---|
| 89 | Sub(instr.buf, code[c + j].buf);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
[16293] | 92 | break;
|
---|
[16285] | 93 | }
|
---|
| 94 |
|
---|
[16899] | 95 | case OpCode.Mul: {
|
---|
[16285] | 96 | Load(instr.buf, code[c].buf);
|
---|
| 97 | for (int j = 1; j < n; ++j) {
|
---|
| 98 | Mul(instr.buf, code[c + j].buf);
|
---|
| 99 | }
|
---|
| 100 | break;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[16899] | 103 | case OpCode.Div: {
|
---|
[16285] | 104 | if (n == 1) {
|
---|
| 105 | Inv(instr.buf, code[c].buf);
|
---|
| 106 | } else {
|
---|
| 107 | Load(instr.buf, code[c].buf);
|
---|
| 108 | for (int j = 1; j < n; ++j) {
|
---|
| 109 | Div(instr.buf, code[c + j].buf);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[16293] | 112 | break;
|
---|
[16285] | 113 | }
|
---|
| 114 |
|
---|
[16899] | 115 | case OpCode.Square: {
|
---|
[16293] | 116 | Square(instr.buf, code[c].buf);
|
---|
| 117 | break;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[16899] | 120 | case OpCode.Root: {
|
---|
[16356] | 121 | Load(instr.buf, code[c].buf);
|
---|
| 122 | Root(instr.buf, code[c + 1].buf);
|
---|
[16293] | 123 | break;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[16899] | 126 | case OpCode.SquareRoot: {
|
---|
[16293] | 127 | Sqrt(instr.buf, code[c].buf);
|
---|
| 128 | break;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[16899] | 131 | case OpCode.Cube: {
|
---|
[16356] | 132 | Cube(instr.buf, code[c].buf);
|
---|
| 133 | break;
|
---|
| 134 | }
|
---|
[16899] | 135 | case OpCode.CubeRoot: {
|
---|
[16356] | 136 | CubeRoot(instr.buf, code[c].buf);
|
---|
| 137 | break;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[16899] | 140 | case OpCode.Power: {
|
---|
[16356] | 141 | Load(instr.buf, code[c].buf);
|
---|
| 142 | Pow(instr.buf, code[c + 1].buf);
|
---|
[16293] | 143 | break;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[16899] | 146 | case OpCode.Exp: {
|
---|
[16285] | 147 | Exp(instr.buf, code[c].buf);
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[16899] | 151 | case OpCode.Log: {
|
---|
[16285] | 152 | Log(instr.buf, code[c].buf);
|
---|
| 153 | break;
|
---|
| 154 | }
|
---|
[16293] | 155 |
|
---|
[16899] | 156 | case OpCode.Sin: {
|
---|
[16293] | 157 | Sin(instr.buf, code[c].buf);
|
---|
| 158 | break;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[16899] | 161 | case OpCode.Cos: {
|
---|
[16293] | 162 | Cos(instr.buf, code[c].buf);
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[16899] | 166 | case OpCode.Tan: {
|
---|
[16293] | 167 | Tan(instr.buf, code[c].buf);
|
---|
| 168 | break;
|
---|
| 169 | }
|
---|
[16899] | 170 | case OpCode.Tanh: {
|
---|
[16722] | 171 | Tanh(instr.buf, code[c].buf);
|
---|
| 172 | break;
|
---|
| 173 | }
|
---|
[16899] | 174 | case OpCode.Absolute: {
|
---|
[16356] | 175 | Absolute(instr.buf, code[c].buf);
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[16899] | 179 | case OpCode.AnalyticQuotient: {
|
---|
[16356] | 180 | Load(instr.buf, code[c].buf);
|
---|
| 181 | AnalyticQuotient(instr.buf, code[c + 1].buf);
|
---|
| 182 | break;
|
---|
| 183 | }
|
---|
[16285] | 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[16378] | 188 | private readonly object syncRoot = new object();
|
---|
| 189 |
|
---|
[16296] | 190 | [ThreadStatic]
|
---|
| 191 | private Dictionary<string, double[]> cachedData;
|
---|
| 192 |
|
---|
[16378] | 193 | [ThreadStatic]
|
---|
| 194 | private IDataset dataset;
|
---|
| 195 |
|
---|
[16296] | 196 | private void InitCache(IDataset dataset) {
|
---|
[16378] | 197 | this.dataset = dataset;
|
---|
[16296] | 198 | cachedData = new Dictionary<string, double[]>();
|
---|
| 199 | foreach (var v in dataset.DoubleVariables) {
|
---|
[16301] | 200 | cachedData[v] = dataset.GetDoubleValues(v).ToArray();
|
---|
[16296] | 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | public void InitializeState() {
|
---|
| 205 | cachedData = null;
|
---|
[16378] | 206 | dataset = null;
|
---|
[16296] | 207 | EvaluatedSolutions = 0;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[16285] | 210 | private double[] GetValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
|
---|
[16378] | 211 | if (cachedData == null || this.dataset != dataset) {
|
---|
| 212 | InitCache(dataset);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[16899] | 215 | var code = Compile(tree, dataset, OpCode.MapSymbolToOpCode, rows);
|
---|
[16285] | 216 | var remainingRows = rows.Length % BATCHSIZE;
|
---|
| 217 | var roundedTotal = rows.Length - remainingRows;
|
---|
| 218 |
|
---|
| 219 | var result = new double[rows.Length];
|
---|
| 220 |
|
---|
| 221 | for (int rowIndex = 0; rowIndex < roundedTotal; rowIndex += BATCHSIZE) {
|
---|
| 222 | Evaluate(code, rows, rowIndex, BATCHSIZE);
|
---|
| 223 | Array.Copy(code[0].buf, 0, result, rowIndex, BATCHSIZE);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | if (remainingRows > 0) {
|
---|
| 227 | Evaluate(code, rows, roundedTotal, remainingRows);
|
---|
| 228 | Array.Copy(code[0].buf, 0, result, roundedTotal, remainingRows);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[16378] | 231 | // when evaluation took place without any error, we can increment the counter
|
---|
| 232 | lock (syncRoot) {
|
---|
| 233 | EvaluatedSolutions++;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[16285] | 236 | return result;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[16293] | 239 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, int[] rows) {
|
---|
| 240 | return GetValues(tree, dataset, rows);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[16285] | 243 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
[16296] | 244 | return GetSymbolicExpressionTreeValues(tree, dataset, rows.ToArray());
|
---|
[16285] | 245 | }
|
---|
| 246 |
|
---|
[16722] | 247 | private BatchInstruction[] Compile(ISymbolicExpressionTree tree, IDataset dataset, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, int[] rows) {
|
---|
[16285] | 248 | var root = tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 249 | var code = new BatchInstruction[root.GetLength()];
|
---|
| 250 | if (root.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
|
---|
| 251 | int c = 1, i = 0;
|
---|
[16722] | 252 | var allRows = Enumerable.Range(0, dataset.Rows).ToArray();
|
---|
[16285] | 253 | foreach (var node in root.IterateNodesBreadth()) {
|
---|
[16296] | 254 | if (node.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
|
---|
| 255 | code[i] = new BatchInstruction {
|
---|
| 256 | opcode = opCodeMapper(node),
|
---|
| 257 | narg = (ushort)node.SubtreeCount,
|
---|
| 258 | buf = new double[BATCHSIZE],
|
---|
| 259 | childIndex = c
|
---|
| 260 | };
|
---|
[16285] | 261 | if (node is VariableTreeNode variable) {
|
---|
| 262 | code[i].weight = variable.Weight;
|
---|
[16296] | 263 | if (cachedData.ContainsKey(variable.VariableName)) {
|
---|
| 264 | code[i].data = cachedData[variable.VariableName];
|
---|
| 265 | } else {
|
---|
| 266 | code[i].data = dataset.GetReadOnlyDoubleValues(variable.VariableName).ToArray();
|
---|
| 267 | cachedData[variable.VariableName] = code[i].data;
|
---|
| 268 | }
|
---|
[16285] | 269 | } else if (node is ConstantTreeNode constant) {
|
---|
| 270 | code[i].value = constant.Value;
|
---|
[16287] | 271 | for (int j = 0; j < BATCHSIZE; ++j)
|
---|
| 272 | code[i].buf[j] = code[i].value;
|
---|
[16722] | 273 | } else if (node is TreeModelTreeNode subtree) {
|
---|
| 274 | code[i].data = GetValues(subtree.Tree, dataset, allRows);
|
---|
| 275 | EvaluatedSolutions--;
|
---|
[16285] | 276 | }
|
---|
| 277 | c += node.SubtreeCount;
|
---|
| 278 | ++i;
|
---|
| 279 | }
|
---|
| 280 | return code;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|