source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs @ 17758

Last change on this file since 17758 was 17758, checked in by gkronber, 20 months ago

#3073: corrected bug in previous commit

File size: 25.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Collections.ObjectModel;
27using System.Linq;
28using HEAL.Attic;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
33using HeuristicLab.Parameters;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [StorableType("DE6C1E1E-D7C1-4070-847E-63B68562B10C")]
37  [Item("IntervalInterpreter", "Interpreter for calculation of intervals of symbolic models.")]
38  public sealed class IntervalInterpreter : ParameterizedNamedItem, IStatefulItem {
39    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
40    private const string MinSplittingWidthParameterName = "MinSplittingWidth";
41    private const string MaxSplittingDepthParameterName = "MaxSplittingDepth";
42    private const string UseIntervalSplittingParameterName = "UseIntervalSplitting";
43
44    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter =>
45      (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName];
46
47    public IFixedValueParameter<IntValue> MinSplittingWithParameter =>
48      (IFixedValueParameter<IntValue>)Parameters[MinSplittingWidthParameterName];
49
50    public IFixedValueParameter<IntValue> MaxSplittingDepthParameter =>
51      (IFixedValueParameter<IntValue>)Parameters[MaxSplittingDepthParameterName];
52
53    public IFixedValueParameter<BoolValue> UseIntervalSplittingParameter =>
54      (IFixedValueParameter<BoolValue>)Parameters[UseIntervalSplittingParameterName];
55
56    public int MinSplittingWidth {
57      get => MinSplittingWithParameter.Value.Value;
58      set => MinSplittingWithParameter.Value.Value = value;
59    }
60
61    public int MaxSplittingDepth {
62      get => MaxSplittingDepthParameter.Value.Value;
63      set => MaxSplittingDepthParameter.Value.Value = value;
64    }
65
66    public bool UseIntervalSplitting {
67      get => UseIntervalSplittingParameter.Value.Value;
68      set => UseIntervalSplittingParameter.Value.Value = value;
69    }
70
71    public int EvaluatedSolutions {
72      get => EvaluatedSolutionsParameter.Value.Value;
73      set => EvaluatedSolutionsParameter.Value.Value = value;
74    }
75
76    [StorableConstructor]
77    private IntervalInterpreter(StorableConstructorFlag _) : base(_) { }
78
79    private IntervalInterpreter(IntervalInterpreter original, Cloner cloner)
80      : base(original, cloner) { }
81
82    public IntervalInterpreter()
83      : base("IntervalInterpreter", "Interpreter for calculation of intervals of symbolic models.") {
84      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName,
85        "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
86      Parameters.Add(new FixedValueParameter<IntValue>(MinSplittingWidthParameterName,
87        "Minimum interval width until splitting is stopped", new IntValue(0)));
88      Parameters.Add(new FixedValueParameter<IntValue>(MaxSplittingDepthParameterName,
89        "Maximum recursion depth of the splitting", new IntValue(5)));
90      Parameters.Add(new FixedValueParameter<BoolValue>(UseIntervalSplittingParameterName, "", new BoolValue(false)));
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new IntervalInterpreter(this, cloner);
95    }
96
97    private readonly object syncRoot = new object();
98
99    #region IStatefulItem Members
100
101    public void InitializeState() {
102      EvaluatedSolutions = 0;
103    }
104
105    public void ClearState() { }
106
107    #endregion
108
109    public Interval GetSymbolicExpressionTreeInterval(
110      ISymbolicExpressionTree tree, IDataset dataset,
111      IEnumerable<int> rows = null) {
112      var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
113      return GetSymbolicExpressionTreeInterval(tree, variableRanges);
114    }
115
116    public Interval GetSymbolicExpressionTreeIntervals(
117      ISymbolicExpressionTree tree, IDataset dataset,
118      out IDictionary<ISymbolicExpressionTreeNode, Interval>
119        nodeIntervals, IEnumerable<int> rows = null) {
120      var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
121      return GetSymbolicExpressionTreeIntervals(tree, variableRanges, out nodeIntervals);
122    }
123
124    public Interval GetSymbolicExpressionTreeInterval(
125      ISymbolicExpressionTree tree,
126      IReadOnlyDictionary<string, Interval> variableRanges) {
127      lock (syncRoot) {
128        EvaluatedSolutions++;
129      }
130
131      Interval outputInterval;
132
133      if (UseIntervalSplitting) {
134        outputInterval = GetSymbolicExpressionTreeIntervals(tree, variableRanges,
135          out var _);
136      } else {
137        var instructionCount = 0;
138        var instructions = PrepareInterpreterState(tree, variableRanges);
139        outputInterval = Evaluate(instructions, ref instructionCount);
140      }
141
142      return outputInterval.LowerBound <= outputInterval.UpperBound
143        ? outputInterval
144        : new Interval(outputInterval.UpperBound, outputInterval.LowerBound);
145    }
146
147
148    public Interval GetSymbolicExpressionTreeIntervals(
149      ISymbolicExpressionTree tree,
150      IReadOnlyDictionary<string, Interval> variableRanges,
151      out IDictionary<ISymbolicExpressionTreeNode, Interval>
152        nodeIntervals) {
153      lock (syncRoot) {
154        EvaluatedSolutions++;
155      }
156
157      var intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
158      var instructions = PrepareInterpreterState(tree, variableRanges);
159
160      Interval outputInterval;
161      if (UseIntervalSplitting) {
162        var variables = tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(x => x.VariableName).Distinct()
163                            .ToList();
164        var containsDependencyProblem = ContainsVariableMultipleTimes(tree);
165
166        if (containsDependencyProblem) {
167          var currIndex = 0;
168          var currDepth = 0;
169          IDictionary<string, Interval> writeableVariableRanges =
170            variableRanges.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
171          //outputInterval = EvaluateRecursive(instructions, intervals, writeableVariableRanges, variables, MinSplittingWidth, MaxSplittingDepth,
172          //  ref currIndex, ref currDepth, tree);
173          outputInterval = EvaluateWithSplitting(instructions, intervals, writeableVariableRanges);
174        } else {
175          var instructionCount = 0;
176          outputInterval = Evaluate(instructions, ref instructionCount, intervals);
177        }
178      } else {
179        var instructionCount = 0;
180        outputInterval = Evaluate(instructions, ref instructionCount, intervals);
181      }
182
183      nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
184      foreach (var kvp in intervals) {
185        var interval = kvp.Value;
186        if (interval.IsInfiniteOrUndefined || interval.LowerBound <= interval.UpperBound)
187          nodeIntervals.Add(kvp.Key, interval);
188        else
189          nodeIntervals.Add(kvp.Key, new Interval(interval.UpperBound, interval.LowerBound));
190      }
191
192      // because of numerical errors the bounds might be incorrect
193      if (outputInterval.IsInfiniteOrUndefined || outputInterval.LowerBound <= outputInterval.UpperBound)
194        return outputInterval;
195
196      return new Interval(outputInterval.UpperBound, outputInterval.LowerBound);
197    }
198
199
200    private static Instruction[] PrepareInterpreterState(
201      ISymbolicExpressionTree tree,
202      IReadOnlyDictionary<string, Interval> variableRanges) {
203      if (variableRanges == null)
204        throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges));
205
206      //Check if all variables used in the tree are present in the dataset
207      foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName)
208                                   .Distinct())
209        if (!variableRanges.ContainsKey(variable))
210          throw new InvalidOperationException($"No ranges for variable {variable} is present");
211
212      var code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
213      foreach (var instr in code.Where(i => i.opCode == OpCodes.Variable)) {
214        var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
215        instr.data = variableRanges[variableTreeNode.VariableName];
216      }
217
218      return code;
219    }
220
221    public static Interval EvaluateWithSplitting(Instruction[] instructions,
222                                                 IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals,
223                                                 IDictionary<string, Interval> variableIntervals) {
224      var savedIntervals = variableIntervals.ToDictionary(entry => entry.Key, entry => entry.Value);
225      var min = FindBound(instructions, nodeIntervals, variableIntervals, minimization: true);
226      var max = FindBound(instructions, nodeIntervals, savedIntervals, minimization: false);
227
228      return new Interval(min, max);
229    }
230
231
232    private static double FindBound(Instruction[] instructions,
233                                    IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals,
234                                    IDictionary<string, Interval> variableIntervals, bool minimization = true) {
235      SortedSet<BoxBound> prioQ = new SortedSet<BoxBound>();
236
237      var ic = 0;
238      //Calculate full box
239      IReadOnlyDictionary<string, Interval> readonlyRanges = variableIntervals.ToDictionary(k => k.Key, k => k.Value);
240      var interval = Evaluate(instructions, ref ic, nodeIntervals, readonlyRanges);
241      // the order of keys in a dictionary is guaranteed to be the same order as values in a dictionary
242      // https://docs.microsoft.com/en-us/dotnet/api/system.collections.idictionary.keys?view=netcore-3.1#remarks
243      var box = variableIntervals.Values;
244      if (minimization) {
245        prioQ.Add(new BoxBound(box, interval.LowerBound));
246      } else {
247        prioQ.Add(new BoxBound(box, -interval.UpperBound));
248      }
249
250      // TODO a fixed limit for depth?!
251      for (var depth = 0; depth < 200; ++depth) {
252        var currentBound = prioQ.Min;
253        prioQ.Remove(currentBound);
254
255        var newBoxes = Split(currentBound.box);
256
257        foreach (var newBox in newBoxes) {
258          var intervalEnum = newBox.GetEnumerator();
259          var keyEnum = readonlyRanges.Keys.GetEnumerator();
260          while (intervalEnum.MoveNext() & keyEnum.MoveNext()) {
261            variableIntervals[keyEnum.Current] = intervalEnum.Current;
262          }
263
264          ic = 0;
265          var res = Evaluate(instructions, ref ic, nodeIntervals,
266            new ReadOnlyDictionary<string, Interval>(variableIntervals));
267          if (minimization) {
268            prioQ.Add(new BoxBound(newBox, interval.LowerBound));
269          } else {
270            prioQ.Add(new BoxBound(newBox, -interval.UpperBound));
271          }
272        }
273      }
274
275      return minimization ?
276        prioQ.First().bound :
277        -prioQ.First().bound;
278    }
279
280    private static IEnumerable<IEnumerable<Interval>> Split(List<Interval> box) {
281      var boxes = box.Select(region => region.Split()).Select(split => new List<Interval> { split.Item1, split.Item2 })
282                     .ToList();
283
284      return boxes.CartesianProduct();
285    }
286
287    // a multi-dimensional box with an associated bound
288    // boxbounds are ordered first by bound (smaller first), then by size of box (larger first) then by distance of bottom left corner to origin
289    private class BoxBound : IComparable<BoxBound> {
290      public List<Interval> box;
291      public double bound;
292      public BoxBound(IEnumerable<Interval> box, double bound) {
293        this.box = new List<Interval>(box);
294        this.bound = bound;
295      }
296      public int CompareTo(BoxBound other) {
297        if (bound != other.bound) return bound.CompareTo(other.bound);
298
299        var thisSize = box.Aggregate(1.0, (current, dimExtent) => current * dimExtent.Width);
300        var otherSize = other.box.Aggregate(1.0, (current, dimExtent) => current * dimExtent.Width);
301        if (thisSize != otherSize) return -thisSize.CompareTo(otherSize);
302
303        var thisDist = box.Sum(dimExtent => dimExtent.LowerBound * dimExtent.LowerBound);
304        var otherDist = other.box.Sum(dimExtent => dimExtent.LowerBound * dimExtent.LowerBound);
305        if (thisDist != otherDist) return thisDist.CompareTo(otherDist);
306
307        // which is smaller first along the dimensions?
308        for (int i = 0; i < box.Count; i++) {
309          if (box[i].LowerBound != other.box[i].LowerBound) return box[i].LowerBound.CompareTo(other.box[i].LowerBound);
310        }
311
312        return 0;
313      }
314    }
315
316    public static Interval EvaluateRecursive(
317      Instruction[] instructions,
318      IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals,
319      IDictionary<string, Interval> variableIntervals, IList<string> variables,
320      double minWidth, int maxDepth, ref int currIndex, ref int currDepth,
321      ISymbolicExpressionTree tree) {
322      Interval evaluate() {
323        var ic = 0;
324        IReadOnlyDictionary<string, Interval> readonlyRanges =
325          new ReadOnlyDictionary<string, Interval>(variableIntervals);
326        return Evaluate(instructions, ref ic, nodeIntervals, readonlyRanges);
327      }
328
329      Interval recurse(ref int idx, ref int depth) {
330        return EvaluateRecursive(instructions, nodeIntervals, variableIntervals, variables, minWidth, maxDepth, ref idx,
331          ref depth, tree);
332      }
333
334
335      var v = variables[currIndex];
336      var x = variableIntervals[v];
337      if (x.Width < minWidth || currDepth == maxDepth || !MultipleTimes(tree, v)) {
338        if (currIndex + 1 < variables.Count) {
339          currDepth = 0;
340          currIndex++;
341          var z = recurse(ref currIndex, ref currDepth);
342          currIndex--;
343          return z;
344        }
345
346        return evaluate();
347      }
348
349      var t = x.Split();
350      var xa = t.Item1;
351      var xb = t.Item2;
352      var d = currDepth;
353      currDepth = d + 1;
354      variableIntervals[v] = xa;
355      var ya = recurse(ref currIndex, ref currDepth);
356      currDepth = d + 1;
357      variableIntervals[v] = xb;
358      var yb = recurse(ref currIndex, ref currDepth);
359      variableIntervals[v] = x; // restore interval
360      return ya | yb;
361    }
362
363    public static Interval Evaluate(
364      Instruction[] instructions, ref int instructionCounter,
365      IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null,
366      IReadOnlyDictionary<string, Interval> variableIntervals = null) {
367      var currentInstr = instructions[instructionCounter];
368      //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side
369      //Update instructionCounter, whenever Evaluate is called
370      instructionCounter++;
371      Interval result = null;
372
373      switch (currentInstr.opCode) {
374        //Variables, Constants, ...
375        case OpCodes.Variable: {
376            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
377            var weightInterval = new Interval(variableTreeNode.Weight, variableTreeNode.Weight);
378            //var variableInterval = (Interval)currentInstr.data;
379
380            Interval variableInterval;
381            if (variableIntervals != null && variableIntervals.ContainsKey(variableTreeNode.VariableName))
382              variableInterval = variableIntervals[variableTreeNode.VariableName];
383            else
384              variableInterval = (Interval)currentInstr.data;
385
386            result = Interval.Multiply(variableInterval, weightInterval);
387            break;
388          }
389        case OpCodes.Constant: {
390            var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
391            result = new Interval(constTreeNode.Value, constTreeNode.Value);
392            break;
393          }
394        //Elementary arithmetic rules
395        case OpCodes.Add: {
396            //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
397            result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
398            for (var i = 1; i < currentInstr.nArguments; i++) {
399              //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
400              var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
401              result = Interval.Add(result, argumentInterval);
402            }
403
404            break;
405          }
406        case OpCodes.Sub: {
407            //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
408            result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
409            if (currentInstr.nArguments == 1)
410              result = Interval.Multiply(new Interval(-1, -1), result);
411
412            for (var i = 1; i < currentInstr.nArguments; i++) {
413              //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
414              var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
415              result = Interval.Subtract(result, argumentInterval);
416            }
417
418            break;
419          }
420        case OpCodes.Mul: {
421            //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
422            result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
423            for (var i = 1; i < currentInstr.nArguments; i++) {
424              var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
425              //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
426              result = Interval.Multiply(result, argumentInterval);
427            }
428
429            break;
430          }
431        case OpCodes.Div: {
432            //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
433            result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
434            if (currentInstr.nArguments == 1)
435              result = Interval.Divide(new Interval(1, 1), result);
436
437            for (var i = 1; i < currentInstr.nArguments; i++) {
438              //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
439              var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
440              result = Interval.Divide(result, argumentInterval);
441            }
442
443            break;
444          }
445        //Trigonometric functions
446        case OpCodes.Sin: {
447            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
448            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
449            result = Interval.Sine(argumentInterval);
450            break;
451          }
452        case OpCodes.Cos: {
453            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
454            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
455            result = Interval.Cosine(argumentInterval);
456            break;
457          }
458        case OpCodes.Tan: {
459            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
460            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
461            result = Interval.Tangens(argumentInterval);
462            break;
463          }
464        case OpCodes.Tanh: {
465            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
466            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
467            result = Interval.HyperbolicTangent(argumentInterval);
468            break;
469          }
470        //Exponential functions
471        case OpCodes.Log: {
472            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
473            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
474            result = Interval.Logarithm(argumentInterval);
475            break;
476          }
477        case OpCodes.Exp: {
478            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
479            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
480            result = Interval.Exponential(argumentInterval);
481            break;
482          }
483        case OpCodes.Square: {
484            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
485            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
486            result = Interval.Square(argumentInterval);
487            break;
488          }
489        case OpCodes.SquareRoot: {
490            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
491            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
492            result = Interval.SquareRoot(argumentInterval);
493            break;
494          }
495        case OpCodes.Cube: {
496            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
497            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
498            result = Interval.Cube(argumentInterval);
499            break;
500          }
501        case OpCodes.CubeRoot: {
502            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
503            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
504            result = Interval.CubicRoot(argumentInterval);
505            break;
506          }
507        case OpCodes.Absolute: {
508            //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
509            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
510            result = Interval.Absolute(argumentInterval);
511            break;
512          }
513        case OpCodes.AnalyticQuotient: {
514            //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
515            result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
516            for (var i = 1; i < currentInstr.nArguments; i++) {
517              //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
518              var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals);
519              result = Interval.AnalyticalQuotient(result, argumentInterval);
520            }
521
522            break;
523          }
524        default:
525          throw new NotSupportedException($"The tree contains the unknown symbol {currentInstr.dynamicNode.Symbol}");
526      }
527
528      if (!(nodeIntervals == null || nodeIntervals.ContainsKey(currentInstr.dynamicNode)))
529        nodeIntervals.Add(currentInstr.dynamicNode, result);
530
531      return result;
532    }
533
534    private static bool MultipleTimes(ISymbolicExpressionTree tree, string variable) {
535      var varlist = tree.IterateNodesPrefix().OfType<VariableTreeNode>().GroupBy(x => x.VariableName);
536      var group = varlist.Select(x => x.Key == variable).Count();
537
538      return group > 1;
539    }
540
541    private static bool ContainsVariableMultipleTimes(ISymbolicExpressionTree tree) {
542      var varlist = tree.IterateNodesPrefix().OfType<VariableTreeNode>().GroupBy(x => x.VariableName);
543      return varlist.Any(group => group.Count() > 1);
544    }
545
546
547    public static bool IsCompatible(ISymbolicExpressionTree tree) {
548      var containsUnknownSymbols = (
549        from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
550        where
551          !(n.Symbol is Variable) &&
552          !(n.Symbol is Constant) &&
553          !(n.Symbol is StartSymbol) &&
554          !(n.Symbol is Addition) &&
555          !(n.Symbol is Subtraction) &&
556          !(n.Symbol is Multiplication) &&
557          !(n.Symbol is Division) &&
558          !(n.Symbol is Sine) &&
559          !(n.Symbol is Cosine) &&
560          !(n.Symbol is Tangent) &&
561          !(n.Symbol is HyperbolicTangent) &&
562          !(n.Symbol is Logarithm) &&
563          !(n.Symbol is Exponential) &&
564          !(n.Symbol is Square) &&
565          !(n.Symbol is SquareRoot) &&
566          !(n.Symbol is Cube) &&
567          !(n.Symbol is CubeRoot) &&
568          !(n.Symbol is Absolute) &&
569          !(n.Symbol is AnalyticQuotient)
570        select n).Any();
571      return !containsUnknownSymbols;
572    }
573  }
574}
Note: See TracBrowser for help on using the repository browser.