Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/06/19 14:20:06 (5 years ago)
Author:
msemenki
Message:

#2988: New version of class structure.

Location:
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs

    r16722 r16899  
    6969    #endregion
    7070
    71     public Interval GetSymbolicExressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {
     71    public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {
    7272      var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
    73       return GetSymbolicExressionTreeInterval(tree, variableRanges);
    74     }
    75 
    76     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
    77       out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {
     73      return GetSymbolicExpressionTreeInterval(tree, variableRanges);
     74    }
     75
     76    public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
     77      out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {
    7878      var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
    79       return GetSymbolicExressionTreeIntervals(tree, variableRanges, out nodeIntervals);
    80     }
    81 
    82     public Interval GetSymbolicExressionTreeInterval(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) {
     79      return GetSymbolicExpressionTreeIntervals(tree, variableRanges, out nodeIntervals);
     80    }
     81
     82    public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) {
    8383      lock (syncRoot) {
    8484        EvaluatedSolutions++;
     
    9696
    9797
    98     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree,
    99       Dictionary<string, Interval> variableRanges, out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) {
     98    public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree,
     99      IDictionary<string, Interval> variableRanges, out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) {
    100100      lock (syncRoot) {
    101101        EvaluatedSolutions++;
     
    108108      // fix incorrect intervals if necessary (could occur because of numerical errors)
    109109      nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    110       foreach(var kvp in intervals) {
     110      foreach (var kvp in intervals) {
    111111        var interval = kvp.Value;
    112112        if (interval.IsInfiniteOrUndefined || interval.LowerBound <= interval.UpperBound)
     
    124124
    125125
    126     private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) {
     126    private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) {
    127127      if (variableRanges == null)
    128128        throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges));
     
    133133      }
    134134
    135       Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
    136       foreach (Instruction instr in code.Where(i => i.opCode == OpCodes.Variable)) {
     135      Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCode.MapSymbolToOpCode);
     136      foreach (Instruction instr in code.Where(i => i.opCode == OpCode.Variable)) {
    137137        var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
    138138        instr.data = variableRanges[variableTreeNode.VariableName];
     
    141141    }
    142142
    143     private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {
     143    private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {
    144144      Instruction currentInstr = instructions[instructionCounter];
    145145      //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side
     
    150150      switch (currentInstr.opCode) {
    151151        //Variables, Constants, ...
    152         case OpCodes.Variable: {
     152        case OpCode.Variable: {
    153153            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
    154154            var weightInterval = new Interval(variableTreeNode.Weight, variableTreeNode.Weight);
     
    158158            break;
    159159          }
    160         case OpCodes.Constant: {
     160        case OpCode.Constant: {
    161161            var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
    162162            result = new Interval(constTreeNode.Value, constTreeNode.Value);
     
    164164          }
    165165        //Elementary arithmetic rules
    166         case OpCodes.Add: {
     166        case OpCode.Add: {
    167167            result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    168168            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    172172            break;
    173173          }
    174         case OpCodes.Sub: {
     174        case OpCode.Sub: {
    175175            result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    176176            if (currentInstr.nArguments == 1)
     
    183183            break;
    184184          }
    185         case OpCodes.Mul: {
     185        case OpCode.Mul: {
    186186            result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    187187            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    191191            break;
    192192          }
    193         case OpCodes.Div: {
     193        case OpCode.Div: {
    194194            result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    195195            if (currentInstr.nArguments == 1)
     
    203203          }
    204204        //Trigonometric functions
    205         case OpCodes.Sin: {
     205        case OpCode.Sin: {
    206206            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    207207            result = Interval.Sine(argumentInterval);
    208208            break;
    209209          }
    210         case OpCodes.Cos: {
     210        case OpCode.Cos: {
    211211            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    212212            result = Interval.Cosine(argumentInterval);
    213213            break;
    214214          }
    215         case OpCodes.Tan: {
     215        case OpCode.Tan: {
    216216            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    217217            result = Interval.Tangens(argumentInterval);
    218218            break;
    219219          }
     220        case OpCode.Tanh: {
     221            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
     222            result = Interval.HyperbolicTangent(argumentInterval);
     223            break;
     224          }
    220225        //Exponential functions
    221         case OpCodes.Log: {
     226        case OpCode.Log: {
    222227            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    223228            result = Interval.Logarithm(argumentInterval);
    224229            break;
    225230          }
    226         case OpCodes.Exp: {
     231        case OpCode.Exp: {
    227232            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    228233            result = Interval.Exponential(argumentInterval);
    229234            break;
    230235          }
    231         case OpCodes.Power: {
     236        case OpCode.Power: {
    232237            result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    233238            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    237242            break;
    238243          }
    239         case OpCodes.Square: {
     244        case OpCode.Square: {
    240245            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    241246            result = Interval.Square(argumentInterval);
    242247            break;
    243248          }
    244         case OpCodes.Root: {
     249        case OpCode.Root: {
    245250            result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    246251            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    250255            break;
    251256          }
    252         case OpCodes.SquareRoot: {
     257        case OpCode.SquareRoot: {
    253258            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    254259            result = Interval.SquareRoot(argumentInterval);
Note: See TracChangeset for help on using the changeset viewer.