[16276] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Runtime.InteropServices;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[16277] | 28 | using HeuristicLab.Data;
|
---|
[16276] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[16277] | 30 | using HeuristicLab.Parameters;
|
---|
[16276] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | [Item("SymbolicDataAnalysisExpressionTreeNativeInterpreter", "An interpreter that wraps a native dll")]
|
---|
| 36 | public class SymbolicDataAnalysisExpressionTreeNativeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
[16277] | 37 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
[16276] | 38 |
|
---|
[16277] | 39 | #region parameters
|
---|
| 40 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 41 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
| 42 | }
|
---|
| 43 | #endregion
|
---|
| 44 |
|
---|
| 45 | #region properties
|
---|
| 46 | public int EvaluatedSolutions {
|
---|
| 47 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
| 48 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
| 49 | }
|
---|
| 50 | #endregion
|
---|
| 51 |
|
---|
[16276] | 52 | public void ClearState() { }
|
---|
| 53 |
|
---|
[16277] | 54 | public SymbolicDataAnalysisExpressionTreeNativeInterpreter() {
|
---|
| 55 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 56 | }
|
---|
[16276] | 57 |
|
---|
| 58 | [StorableConstructor]
|
---|
| 59 | protected SymbolicDataAnalysisExpressionTreeNativeInterpreter(bool deserializing) : base(deserializing) { }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | protected SymbolicDataAnalysisExpressionTreeNativeInterpreter(SymbolicDataAnalysisExpressionTreeNativeInterpreter original, Cloner cloner) : base(original, cloner) {
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 66 | return new SymbolicDataAnalysisExpressionTreeNativeInterpreter(this, cloner);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | private NativeInstruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {
|
---|
| 70 | var root = tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 71 | var code = new NativeInstruction[root.GetLength()];
|
---|
| 72 | if (root.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
|
---|
| 73 | code[0] = new NativeInstruction { narg = (ushort)root.SubtreeCount, opcode = opCodeMapper(root) };
|
---|
| 74 | int c = 1, i = 0;
|
---|
| 75 | foreach (var node in root.IterateNodesBreadth()) {
|
---|
| 76 | for (int j = 0; j < node.SubtreeCount; ++j) {
|
---|
| 77 | var s = node.GetSubtree(j);
|
---|
| 78 | if (s.SubtreeCount > ushort.MaxValue) throw new ArgumentException("Number of subtrees is too big (>65.535)");
|
---|
| 79 | code[c + j] = new NativeInstruction { narg = (ushort)s.SubtreeCount, opcode = opCodeMapper(s) };
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (node is VariableTreeNode variable) {
|
---|
| 83 | code[i].weight = variable.Weight;
|
---|
| 84 | code[i].data = cachedData[variable.VariableName].AddrOfPinnedObject();
|
---|
| 85 | } else if (node is ConstantTreeNode constant) {
|
---|
| 86 | code[i].value = constant.Value;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | code[i].childIndex = c;
|
---|
| 90 | c += node.SubtreeCount;
|
---|
| 91 | ++i;
|
---|
| 92 | }
|
---|
| 93 | return code;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private readonly object syncRoot = new object();
|
---|
| 97 |
|
---|
| 98 | [ThreadStatic]
|
---|
| 99 | private static Dictionary<string, GCHandle> cachedData;
|
---|
| 100 |
|
---|
| 101 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
| 102 | if (!rows.Any()) return Enumerable.Empty<double>();
|
---|
| 103 |
|
---|
| 104 | lock (syncRoot) {
|
---|
| 105 | EvaluatedSolutions++; // increment the evaluated solutions counter
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | if (cachedData == null) {
|
---|
| 109 | InitCache(dataset);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | var code = Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
| 113 |
|
---|
| 114 | var rowsArray = rows.ToArray();
|
---|
| 115 | var result = new double[rowsArray.Length];
|
---|
| 116 |
|
---|
| 117 | NativeWrapper.GetValuesVectorized(code, code.Length, rowsArray, rowsArray.Length, result);
|
---|
| 118 | return result;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private void InitCache(IDataset dataset) {
|
---|
| 122 | cachedData = new Dictionary<string, GCHandle>();
|
---|
| 123 | foreach (var v in dataset.DoubleVariables) {
|
---|
| 124 | var values = dataset.GetDoubleValues(v).ToArray();
|
---|
| 125 | var gch = GCHandle.Alloc(values, GCHandleType.Pinned);
|
---|
| 126 | cachedData[v] = gch;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public void InitializeState() {
|
---|
| 131 | if (cachedData != null) {
|
---|
| 132 | foreach (var gch in cachedData.Values) {
|
---|
| 133 | gch.Free();
|
---|
| 134 | }
|
---|
| 135 | cachedData = null;
|
---|
| 136 | }
|
---|
| 137 | EvaluatedSolutions = 0;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|