Changeset 16364


Ignore:
Timestamp:
12/11/18 14:54:35 (7 months ago)
Author:
chaider
Message:

#2966

  • Changed signature of GetSymbolicExressionTreeIntervals methods
  • Changed PrepareInterpreterState (removed optinal parameters, takes Dictionary<string, Interval> as input and no dataset anymore)
  • Added optional parameter (rows) to GetVariableBoundaries method (allows to use just parts from dataset as input e.g. training/test indices)
Location:
branches/2966_interval_calculation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs

    r16331 r16364  
    7878        ResetInstrucitonCount();
    7979      }
    80       var instructions = PrepareInterpreterState(tree, rows, dataset);
     80      var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows);
     81      var instructions = PrepareInterpreterState(tree, intervalBoundaries);
    8182      var x = Evaluate(instructions);
    82 
    83       return x;
    84     }
    85 
    86     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals, IEnumerable<int> rows = null) {
    87       lock (syncRoot) {
    88         EvaluatedSolutions++;
    89         ResetInstrucitonCount();
    90       }
    91       var instructions = PrepareInterpreterState(tree, rows, null, customIntervals);
    92       var x = Evaluate(instructions);
    93 
    94       return x;
    95     }
    96 
    97     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
    98       Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) {
    99       lock (syncRoot) {
    100         EvaluatedSolutions++;
    101         ResetInstrucitonCount();
    102       }
    103       intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    104       var instructions = PrepareInterpreterState(tree, rows, dataset, customIntervals);
    105       var x = Evaluate(instructions, intervals);
    10683
    10784      return x;
     
    11087    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
    11188      out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) {
    112       lock(syncRoot) {
    113         EvaluatedSolutions++;
    114         ResetInstrucitonCount();
    115       }
     89      lock (syncRoot) {
     90        EvaluatedSolutions++;
     91        ResetInstrucitonCount();
     92      }
     93      var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows);
    11694      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    117       var instructions = PrepareInterpreterState(tree, rows, dataset);
     95      var instructions = PrepareInterpreterState(tree, intervalBoundaries);
    11896      var x = Evaluate(instructions, intervals);
    11997
     
    12199    }
    122100
    123     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals,
    124       out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) {
     101    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals) {
     102      lock (syncRoot) {
     103        EvaluatedSolutions++;
     104        ResetInstrucitonCount();
     105      }
     106      var instructions = PrepareInterpreterState(tree, customIntervals);
     107      var x = Evaluate(instructions);
     108
     109      return x;
     110    }
     111
     112
     113    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree,
     114      Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
    125115      lock (syncRoot) {
    126116        EvaluatedSolutions++;
     
    128118      }
    129119      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    130       var instructions = PrepareInterpreterState(tree, rows, null, customIntervals);
     120      var instructions = PrepareInterpreterState(tree, customIntervals);
    131121      var x = Evaluate(instructions, intervals);
    132122
     
    134124    }
    135125
    136     private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree,
    137       IEnumerable<int> rows = null, IDataset dataset = null, Dictionary<string, Interval> customIntervals = null) {
     126
     127    private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals) {
    138128      Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
    139129
    140       if (dataset == null && customIntervals == null)
    141         throw new Exception("No dataset or ranges for intervals are given!");
    142 
    143       if(rows == null)
    144         rows = Enumerable.Range(0, dataset.Rows);
    145 
    146       foreach (Instruction instr in code) {
    147         if (instr.opCode == OpCodes.Variable) {
    148           var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
    149           IList<double> values = new List<double>();
    150 
    151           if (customIntervals != null) {
    152             if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {
    153               instr.data = customIntervals[variableTreeNode.VariableName];
    154             }
    155           } else {
    156             foreach (var rowEnum in rows) {
    157               values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
    158             }
    159             instr.data = new Interval(values.Min(), values.Max());
    160           }
    161         }
     130      if (customIntervals == null)
     131        throw new ArgumentException("No interval ranges are present!", nameof(customIntervals));
     132
     133      foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName).Distinct()) {
     134        if (!customIntervals.ContainsKey(variable)) throw new InvalidOperationException($"No ranges for variable {variable} is present");
     135      }
     136
     137      foreach (Instruction instr in code.Where(i => i.opCode == OpCodes.Variable)) {
     138        var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
     139        instr.data = customIntervals[variableTreeNode.VariableName];
    162140      }
    163141      return code;
    164142    }
    165143
    166     private Interval Evaluate(Instruction[] instructions, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals = null) {
     144    private Interval Evaluate(Instruction[] instructions, Dictionary<ISymbolicExpressionTreeNode,
     145      Interval> intervals = null) {
    167146      Instruction currentInstr = instructions[InstructionCount++];
    168147      Interval intermediate = null;
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs

    r16363 r16364  
    9393    }
    9494
    95     public static Dictionary<string, Interval> GetVariableBoundaries(IDataset dataset) {
     95    public static Dictionary<string, Interval> GetVariableBoundaries(IDataset dataset, IEnumerable<int> rows = null) {
    9696      Dictionary<string, Interval> variableBoundaries = new Dictionary<string, Interval>();
    9797
     
    100100        var max = double.MinValue;
    101101
    102         foreach (var val in dataset.GetDoubleValues(variable)) {
    103           if (val < min) min = val;
    104           if (val > max) max = val;
     102        if (rows != null) {
     103          foreach (int row in rows) {
     104            var val = dataset.GetDoubleValue(variable, row);
     105            if (val < min) min = val;
     106            if (val > max) max = val;
     107          }
     108        } else {
     109          foreach (var val in dataset.GetDoubleValues(variable)) {
     110            if (val < min) min = val;
     111            if (val > max) max = val;
     112          }
    105113        }
    106114        variableBoundaries.Add(variable, new Interval(min, max));
Note: See TracChangeset for help on using the changeset viewer.