Changeset 16328


Ignore:
Timestamp:
11/23/18 17:16:51 (4 weeks ago)
Author:
mkommend
Message:

#2966: Reordered methods in interval interpreter, added IStatefulItem interface, sealed class.

File:
1 edited

Legend:

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

    r16326 r16328  
    1010using HeuristicLab.Problems.DataAnalysis.Symbolic;
    1111
    12 namespace HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Interpreter {
     12namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
    1313  [StorableClass]
    1414  [Item("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.")]
    15   public class IntervalInterpreter : ParameterizedNamedItem{
     15  public sealed class IntervalInterpreter : ParameterizedNamedItem, IStatefulItem {
     16
    1617    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
    1718    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     
    2425    }
    2526
    26     public void ClearState() { }
    27 
    28     protected IntervalInterpreter(bool deserializing) : base(deserializing) { }
    29 
    30     protected IntervalInterpreter(IntervalInterpreter original,
    31         Cloner cloner)
     27    private IntervalInterpreter(bool deserializing) : base(deserializing) { }
     28    private IntervalInterpreter(IntervalInterpreter original, Cloner cloner)
    3229        : base(original, cloner) { }
    3330
     
    3936    }
    4037
     38    #region IStatefulItem Members
     39    public void InitializeState() {
     40      EvaluatedSolutions = 0;
     41    }
     42    public void ClearState() { }
     43    #endregion
     44
     45
     46    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IEnumerable<int> rows, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
     47      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
     48      var state = PrepareInterpreterState(tree, rows);
     49      var x = Evaluate(state, intervals);
     50
     51      return x;
     52    }
     53
     54    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
     55      Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
     56      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
     57      var state = PrepareInterpreterState(tree, rows, dataset, customIntervals);
     58      var x = Evaluate(state, intervals);
     59
     60      return x;
     61    }
     62
     63    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
     64      out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
     65      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
     66      var state = PrepareInterpreterState(tree, rows, dataset);
     67      var x = Evaluate(state, intervals);
     68
     69      return x;
     70    }
     71
    4172    private readonly object syncRoot = new object();
    4273
    43     private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, 
     74    private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree,
    4475      IEnumerable<int> rows, IDataset dataset = null, Dictionary<string, Interval> customIntervals = null) {
    4576      Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
    4677      int necessaryArgStackSize = 0;
    47      
    48       foreach(Instruction instr in code) {
     78
     79      foreach (Instruction instr in code) {
    4980        if (instr.opCode == OpCodes.Variable) {
    5081          var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
     
    5485            if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {
    5586              instr.data = customIntervals[variableTreeNode.VariableName];
    56             } else {
     87            }
     88            else {
    5789              foreach (var rowEnum in rows) {
    5890                values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
     
    6092              instr.data = new Interval(values.Min(), values.Max());
    6193            }
    62           } else if (dataset != null) {
    63              foreach (var rowEnum in rows) {
     94          }
     95          else if (dataset != null) {
     96            foreach (var rowEnum in rows) {
    6497              values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
    6598            }
    6699            instr.data = new Interval(values.Min(), values.Max());
    67           } else if (customIntervals != null) {
    68             if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {
     100          }
     101          else if (customIntervals != null) {
     102            if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {
    69103              instr.data = customIntervals[variableTreeNode.VariableName];
    70104            }
    71           } else {
     105          }
     106          else {
    72107            throw new Exception("No valid input for variables!");
    73108          }
     
    182217      }
    183218    }
    184 
    185     public void InitializeState() {
    186       EvaluatedSolutions = 0;
    187     }
    188 
    189     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IEnumerable<int> rows, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
    190       intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    191       var state = PrepareInterpreterState(tree, rows);
    192       var x = Evaluate(state, intervals);
    193 
    194       return x;
    195     }
    196 
    197     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
    198       Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
    199       intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    200       var state = PrepareInterpreterState(tree, rows, dataset, customIntervals);
    201       var x = Evaluate(state, intervals);
    202 
    203       return x;
    204     }
    205 
    206     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
    207       out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
    208       intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    209       var state = PrepareInterpreterState(tree, rows, dataset);
    210       var x = Evaluate(state, intervals);
    211      
    212       return x;
    213     }
    214219  }
    215220}
Note: See TracChangeset for help on using the changeset viewer.