Ignore:
Timestamp:
03/31/17 14:49:21 (3 years ago)
Author:
mkommend
Message:

#2442: Merged r12807, r13039, r13139, r13140, r13141, r13222, r13247, r13248, r13251, r13254, r13255, r13256, r1326, r13288, r13313, r13314, r13315, r13318, r14282, r14809, r14810 into stable.

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r14186 r14811  
    3232  [StorableClass]
    3333  [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
    34   public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
     34  public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem,
     35    ISymbolicDataAnalysisExpressionTreeInterpreter {
    3536    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
     37    private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.";
    3638    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
    3739
    38     public override bool CanChangeName { get { return false; } }
    39     public override bool CanChangeDescription { get { return false; } }
     40    public override bool CanChangeName {
     41      get { return false; }
     42    }
     43
     44    public override bool CanChangeDescription {
     45      get { return false; }
     46    }
    4047
    4148    #region parameter properties
    42     public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
    43       get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
    44     }
    45 
    46     public IValueParameter<IntValue> EvaluatedSolutionsParameter {
    47       get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
     49    public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
     50      get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
     51    }
     52
     53    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     54      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
    4855    }
    4956    #endregion
    5057
    5158    #region properties
    52     public BoolValue CheckExpressionsWithIntervalArithmetic {
    53       get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
    54       set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
    55     }
    56 
    57     public IntValue EvaluatedSolutions {
    58       get { return EvaluatedSolutionsParameter.Value; }
    59       set { EvaluatedSolutionsParameter.Value = value; }
     59    public bool CheckExpressionsWithIntervalArithmetic {
     60      get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
     61      set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
     62    }
     63
     64    public int EvaluatedSolutions {
     65      get { return EvaluatedSolutionsParameter.Value.Value; }
     66      set { EvaluatedSolutionsParameter.Value.Value = value; }
    6067    }
    6168    #endregion
     
    6370    [StorableConstructor]
    6471    protected SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
    65     protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
     72
     73    protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original,
     74      Cloner cloner)
     75      : base(original, cloner) { }
     76
    6677    public override IDeepCloneable Clone(Cloner cloner) {
    6778      return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
     
    7081    public SymbolicDataAnalysisExpressionTreeInterpreter()
    7182      : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
    72       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    73       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     83      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     84      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    7485    }
    7586
    7687    protected SymbolicDataAnalysisExpressionTreeInterpreter(string name, string description)
    7788      : base(name, description) {
    78       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    79       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     89      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     90      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    8091    }
    8192
    8293    [StorableHook(HookType.AfterDeserialization)]
    8394    private void AfterDeserialization() {
    84       if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
    85         Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     95      var evaluatedSolutions = new IntValue(0);
     96      var checkExpressionsWithIntervalArithmetic = new BoolValue(false);
     97      if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) {
     98        var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName];
     99        evaluatedSolutions = evaluatedSolutionsParameter.Value;
     100        Parameters.Remove(EvaluatedSolutionsParameterName);
     101      }
     102      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions));
     103      if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) {
     104        var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName];
     105        Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
     106        checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value;
     107      }
     108      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic));
    86109    }
    87110
    88111    #region IStatefulItem
    89112    public void InitializeState() {
    90       EvaluatedSolutions.Value = 0;
    91     }
    92 
    93     public void ClearState() {
    94     }
     113      EvaluatedSolutions = 0;
     114    }
     115
     116    public void ClearState() { }
    95117    #endregion
    96118
    97     public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    98       if (CheckExpressionsWithIntervalArithmetic.Value)
     119    private readonly object syncRoot = new object();
     120    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset,
     121      IEnumerable<int> rows) {
     122      if (CheckExpressionsWithIntervalArithmetic) {
    99123        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    100 
    101       lock (EvaluatedSolutions) {
    102         EvaluatedSolutions.Value++; // increment the evaluated solutions counter
     124      }
     125
     126      lock (syncRoot) {
     127        EvaluatedSolutions++; // increment the evaluated solutions counter
    103128      }
    104129      var state = PrepareInterpreterState(tree, dataset);
     
    131156    }
    132157
    133 
    134158    public virtual double Evaluate(IDataset dataset, ref int row, InterpreterState state) {
    135159      Instruction currentInstr = state.NextInstruction();
     
    147171              s -= Evaluate(dataset, ref row, state);
    148172            }
    149             if (currentInstr.nArguments == 1) s = -s;
     173            if (currentInstr.nArguments == 1) { s = -s; }
    150174            return s;
    151175          }
     
    162186              p /= Evaluate(dataset, ref row, state);
    163187            }
    164             if (currentInstr.nArguments == 1) p = 1.0 / p;
     188            if (currentInstr.nArguments == 1) { p = 1.0 / p; }
    165189            return p;
    166190          }
     
    205229        case OpCodes.Gamma: {
    206230            var x = Evaluate(dataset, ref row, state);
    207             if (double.IsNaN(x)) return double.NaN;
    208             else return alglib.gammafunction(x);
     231            if (double.IsNaN(x)) { return double.NaN; } else { return alglib.gammafunction(x); }
    209232          }
    210233        case OpCodes.Psi: {
     
    216239        case OpCodes.Dawson: {
    217240            var x = Evaluate(dataset, ref row, state);
    218             if (double.IsNaN(x)) return double.NaN;
     241            if (double.IsNaN(x)) { return double.NaN; }
    219242            return alglib.dawsonintegral(x);
    220243          }
    221244        case OpCodes.ExponentialIntegralEi: {
    222245            var x = Evaluate(dataset, ref row, state);
    223             if (double.IsNaN(x)) return double.NaN;
     246            if (double.IsNaN(x)) { return double.NaN; }
    224247            return alglib.exponentialintegralei(x);
    225248          }
     
    349372            int positiveSignals = 0;
    350373            for (int i = 0; i < currentInstr.nArguments; i++) {
    351               if (Evaluate(dataset, ref row, state) > 0.0) positiveSignals++;
     374              if (Evaluate(dataset, ref row, state) > 0.0) { positiveSignals++; }
    352375            }
    353376            return positiveSignals % 2 != 0 ? 1.0 : -1.0;
     
    356379            double x = Evaluate(dataset, ref row, state);
    357380            double y = Evaluate(dataset, ref row, state);
    358             if (x > y) return 1.0;
    359             else return -1.0;
     381            if (x > y) { return 1.0; } else { return -1.0; }
    360382          }
    361383        case OpCodes.LT: {
    362384            double x = Evaluate(dataset, ref row, state);
    363385            double y = Evaluate(dataset, ref row, state);
    364             if (x < y) return 1.0;
    365             else return -1.0;
     386            if (x < y) { return 1.0; } else { return -1.0; }
    366387          }
    367388        case OpCodes.TimeLag: {
     
    437458            var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
    438459            int actualRow = row + laggedVariableTreeNode.Lag;
    439             if (actualRow < 0 || actualRow >= dataset.Rows) return double.NaN;
     460            if (actualRow < 0 || actualRow >= dataset.Rows) { return double.NaN; }
    440461            return ((IList<double>)currentInstr.data)[actualRow] * laggedVariableTreeNode.Weight;
    441462          }
     
    459480            return trueBranch * p + falseBranch * (1 - p);
    460481          }
    461         default: throw new NotSupportedException();
     482        default:
     483          throw new NotSupportedException();
    462484      }
    463485    }
Note: See TracChangeset for help on using the changeset viewer.