Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18113 for branches


Ignore:
Timestamp:
12/11/21 12:26:27 (3 years ago)
Author:
chaider
Message:

#3140

  • Refactored ConstantOptimization ==> ParameterOptimization
Location:
branches/3140_NumberSymbol
Files:
17 edited
2 moved

Legend:

Unmodified
Added
Removed
  • branches/3140_NumberSymbol/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessCovarianceOptimizationProblem.cs

    r17180 r18113  
    9797
    9898    private const string ProblemDataParameterName = "ProblemData";
    99     private const string ConstantOptIterationsParameterName = "Constant optimization steps";
     99    private const string ParameterOptIterationsParameterName = "Parameter optimization steps";
    100100    private const string RestartsParameterName = "Restarts";
    101101
     
    108108      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
    109109    }
    110     public IFixedValueParameter<IntValue> ConstantOptIterationsParameter {
    111       get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptIterationsParameterName]; }
     110    public IFixedValueParameter<IntValue> ParameterOptIterationsParameter {
     111      get { return (IFixedValueParameter<IntValue>)Parameters[ParameterOptIterationsParameterName]; }
    112112    }
    113113    public IFixedValueParameter<IntValue> RestartsParameter {
     
    124124    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData { get { return ProblemData; } }
    125125
    126     public int ConstantOptIterations {
    127       get { return ConstantOptIterationsParameter.Value.Value; }
    128       set { ConstantOptIterationsParameter.Value.Value = value; }
     126    public int ParameterOptIterations {
     127      get { return ParameterOptIterationsParameter.Value.Value; }
     128      set { ParameterOptIterationsParameter.Value.Value = value; }
    129129    }
    130130
     
    153153      : base() {
    154154      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, "The data for the regression problem", new RegressionProblemData()));
    155       Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptIterationsParameterName, "Number of optimization steps for hyperparameter values", new IntValue(50)));
    156       Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of random restarts for constant optimization.", new IntValue(10)));
     155      Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptIterationsParameterName, "Number of optimization steps for hyperparameter values", new IntValue(50)));
     156      Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of random restarts for parameter optimization.", new IntValue(10)));
    157157      Parameters["Restarts"].Hidden = true;
    158158      var g = new SimpleSymbolicExpressionGrammar();
     
    236236        double epsx = 0;
    237237        double stpmax = 1;
    238         int maxits = ConstantOptIterations;
     238        int maxits = ParameterOptIterations;
    239239        alglib.mincgstate state;
    240240        alglib.mincgreport rep;
     
    384384    [StorableHook(HookType.AfterDeserialization)]
    385385    private void AfterDeserialization() {
     386      if (!Parameters.ContainsKey(ParameterOptIterationsParameterName)) {
     387        if (Parameters.ContainsKey("Constant optimization steps")) {
     388          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptIterationsParameterName, "Number of optimization steps for hyperparameter values", (IntValue)Parameters["Constant optimization steps"].ActualValue));
     389          Parameters.Remove("Constant optimization steps");
     390        } else {
     391          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptIterationsParameterName, "Number of optimization steps for hyperparameter values", new IntValue(50)));
     392        }
     393      }
    386394    }
    387395
  • branches/3140_NumberSymbol/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs

    r18100 r18113  
    263263      }
    264264
    265       if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)) throw new ArgumentException("The optimizer does not support the specified model structure.");
     265      if (!SymbolicRegressionParameterOptimizationEvaluator.CanOptimizeParameters(tree)) throw new ArgumentException("The optimizer does not support the specified model structure.");
    266266
    267267      // initialize constants randomly
     
    275275      var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    276276
    277       SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices,
     277      SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, problemData.TrainingIndices,
    278278        applyLinearScaling: applyLinearScaling, maxIterations: maxIterations,
    279         updateVariableWeights: false, updateConstantsInTree: true);
     279        updateVariableWeights: false, updateParametersInTree: true);
    280280
    281281      var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter.Clone());
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r17430 r18113  
    4242
    4343      var tree = Content?.Model?.SymbolicExpressionTree;
    44       btnOptimizeConstants.Enabled = tree != null && SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree);
     44      btnOptimizeConstants.Enabled = tree != null && SymbolicRegressionParameterOptimizationEvaluator.CanOptimizeParameters(tree);
    4545    }
    4646
     
    6464      do {
    6565        prevResult = result;
    66         result = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter, tree, regressionProblemData, regressionProblemData.TrainingIndices,
     66        result = SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(model.Interpreter, tree, regressionProblemData, regressionProblemData.TrainingIndices,
    6767          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true, lowerEstimationLimit: model.LowerEstimationLimit, upperEstimationLimit: model.UpperEstimationLimit,
    6868          iterationCallback: (args, func, obj) => {
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r18053 r18113  
    129129    <Compile Include="Plugin.cs" />
    130130    <Compile Include="ShapeConstraintsAnalyzer.cs" />
    131     <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" />
     131    <Compile Include="SingleObjective\ParameterOptimizationAnalyzer.cs" />
    132132    <Compile Include="SingleObjective\Evaluators\NMSESingleObjectiveConstraintsEvaluator.cs" />
    133133    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" />
     
    142142    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs" />
    143143    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs" />
    144     <Compile Include="SingleObjective\Evaluators\SymbolicRegressionConstantOptimizationEvaluator.cs" />
     144    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionParameterOptimizationEvaluator.cs" />
    145145    <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveOverfittingAnalyzer.cs" />
    146146    <Compile Include="SymbolicRegressionModel.cs" />
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/NMSEMultiObjectiveConstraintsEvaluator.cs

    r18100 r18113  
    9090      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    9191
    92       if (UseConstantOptimization) {
    93         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, rows,
     92      if (UseParameterOptimization) {
     93        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, rows,
    9494          false,
    95           ConstantOptimizationIterations,
    96           ConstantOptimizationUpdateVariableWeights,
     95          ParameterOptimizationIterations,
     96          ParameterOptimizationUpdateVariableWeights,
    9797          estimationLimits.Lower,
    9898          estimationLimits.Upper);
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs

    r17180 r18113  
    7676      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    7777
    78       if (UseConstantOptimization) {
    79         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
     78      if (UseParameterOptimization) {
     79        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, solution, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    8080      }
    8181
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredNestedTreeSizeEvaluator.cs

    r17180 r18113  
    5454      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    5555
    56       if (UseConstantOptimization) {
    57         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights,lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
     56      if (UseParameterOptimization) {
     57        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, solution, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights,lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    5858      }
    5959
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredNumberOfVariablesEvaluator.cs

    r17180 r18113  
    5454      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    5555
    56       if (UseConstantOptimization) {
    57         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
     56      if (UseParameterOptimization) {
     57        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, solution, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    5858      }
    5959      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredTreeComplexityEvaluator.cs

    r17180 r18113  
    5353      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    5454
    55       if (UseConstantOptimization) {
    56         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
     55      if (UseParameterOptimization) {
     56        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, solution, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    5757      }
    5858      double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling, DecimalPlaces);
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveEvaluator.cs

    r17180 r18113  
    3131  public abstract class SymbolicRegressionMultiObjectiveEvaluator : SymbolicDataAnalysisMultiObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionMultiObjectiveEvaluator {
    3232    private const string DecimalPlacesParameterName = "Decimal Places";
    33     private const string UseConstantOptimizationParameterName = "Use constant optimization";
    34     private const string ConstantOptimizationIterationsParameterName = "Constant optimization iterations";
     33    private const string UseParameterOptimizationParameterName = "Use parameter optimization";
     34    private const string ParameterOptimizationIterationsParameterName = "Parameter optimization iterations";
    3535
    36     private const string ConstantOptimizationUpdateVariableWeightsParameterName =
    37       "Constant optimization update variable weights";
     36    private const string ParameterOptimizationUpdateVariableWeightsParameterName =
     37      "Parameter optimization update variable weights";
    3838
    3939    public IFixedValueParameter<IntValue> DecimalPlacesParameter {
    4040      get { return (IFixedValueParameter<IntValue>)Parameters[DecimalPlacesParameterName]; }
    4141    }
    42     public IFixedValueParameter<BoolValue> UseConstantOptimizationParameter {
    43       get { return (IFixedValueParameter<BoolValue>)Parameters[UseConstantOptimizationParameterName]; }
     42    public IFixedValueParameter<BoolValue> UseParameterOptimizationParameter {
     43      get { return (IFixedValueParameter<BoolValue>)Parameters[UseParameterOptimizationParameterName]; }
    4444    }
    4545
    46     public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
    47       get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
     46    public IFixedValueParameter<IntValue> ParameterOptimizationIterationsParameter {
     47      get { return (IFixedValueParameter<IntValue>)Parameters[ParameterOptimizationIterationsParameterName]; }
    4848    }
    4949
    50     public IFixedValueParameter<BoolValue> ConstantOptimizationUpdateVariableWeightsParameter {
    51       get { return (IFixedValueParameter<BoolValue>)Parameters[ConstantOptimizationUpdateVariableWeightsParameterName]; }
     50    public IFixedValueParameter<BoolValue> ParameterOptimizationUpdateVariableWeightsParameter {
     51      get { return (IFixedValueParameter<BoolValue>)Parameters[ParameterOptimizationUpdateVariableWeightsParameterName]; }
    5252    }
    5353
     
    5656      set { DecimalPlacesParameter.Value.Value = value; }
    5757    }
    58     public bool UseConstantOptimization {
    59       get { return UseConstantOptimizationParameter.Value.Value; }
    60       set { UseConstantOptimizationParameter.Value.Value = value; }
     58    public bool UseParameterOptimization {
     59      get { return UseParameterOptimizationParameter.Value.Value; }
     60      set { UseParameterOptimizationParameter.Value.Value = value; }
    6161    }
    62     public int ConstantOptimizationIterations {
    63       get { return ConstantOptimizationIterationsParameter.Value.Value; }
    64       set { ConstantOptimizationIterationsParameter.Value.Value = value; }
     62    public int ParameterOptimizationIterations {
     63      get { return ParameterOptimizationIterationsParameter.Value.Value; }
     64      set { ParameterOptimizationIterationsParameter.Value.Value = value; }
    6565    }
    66     public bool ConstantOptimizationUpdateVariableWeights {
    67       get { return ConstantOptimizationUpdateVariableWeightsParameter.Value.Value; }
    68       set { ConstantOptimizationUpdateVariableWeightsParameter.Value.Value = value; }
     66    public bool ParameterOptimizationUpdateVariableWeights {
     67      get { return ParameterOptimizationUpdateVariableWeightsParameter.Value.Value; }
     68      set { ParameterOptimizationUpdateVariableWeightsParameter.Value.Value = value; }
    6969    }
    7070
     
    7878      : base() {
    7979      Parameters.Add(new FixedValueParameter<IntValue>(DecimalPlacesParameterName, "The number of decimal places used for rounding the quality values.", new IntValue(5)) { Hidden = true });
    80       Parameters.Add(new FixedValueParameter<BoolValue>(UseConstantOptimizationParameterName, "", new BoolValue(false)));
    81       Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "The number of iterations constant optimization should be applied.", new IntValue(5)));
    82       Parameters.Add(new FixedValueParameter<BoolValue>(ConstantOptimizationUpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized during constant optimization.", new BoolValue(true)) { Hidden = true });
     80      Parameters.Add(new FixedValueParameter<BoolValue>(UseParameterOptimizationParameterName, "", new BoolValue(false)));
     81      Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "The number of iterations parameter optimization should be applied.", new IntValue(5)));
     82      Parameters.Add(new FixedValueParameter<BoolValue>(ParameterOptimizationUpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized during parameter optimization.", new BoolValue(true)) { Hidden = true });
    8383    }
    8484
    8585    [StorableHook(HookType.AfterDeserialization)]
    8686    private void AfterDeserialization() {
    87       if (!Parameters.ContainsKey(UseConstantOptimizationParameterName)) {
    88         Parameters.Add(new FixedValueParameter<BoolValue>(UseConstantOptimizationParameterName, "", new BoolValue(false)));
     87      if (!Parameters.ContainsKey(UseParameterOptimizationParameterName)) {
     88        if (Parameters.ContainsKey("Use constant optimization")) {
     89          Parameters.Add(new FixedValueParameter<BoolValue>(UseParameterOptimizationParameterName, "", (BoolValue)Parameters["Use constant optimization"].ActualValue));
     90          Parameters.Remove("Use constant optimization");
     91        } else {
     92          Parameters.Add(new FixedValueParameter<BoolValue>(UseParameterOptimizationParameterName, "", new BoolValue(false)));
     93        }
    8994      }
     95
    9096      if (!Parameters.ContainsKey(DecimalPlacesParameterName)) {
    9197        Parameters.Add(new FixedValueParameter<IntValue>(DecimalPlacesParameterName, "The number of decimal places used for rounding the quality values.", new IntValue(-1)) { Hidden = true });
    9298      }
    93       if (!Parameters.ContainsKey(ConstantOptimizationIterationsParameterName)) {
    94         Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "The number of iterations constant optimization should be applied.", new IntValue(5)));
     99      if (!Parameters.ContainsKey(ParameterOptimizationIterationsParameterName)) {
     100        if (Parameters.ContainsKey("Constant optimization iterations")) {
     101          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "The number of iterations parameter optimization should be applied.", (IntValue)Parameters["Constant optimization iterations"].ActualValue));
     102          Parameters.Remove("Constant optimization iterations");
     103        } else {
     104          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "The number of iterations parameter optimization should be applied.", new IntValue(5)));
     105        }
    95106      }
    96       if (!Parameters.ContainsKey(ConstantOptimizationUpdateVariableWeightsParameterName)) {
    97         Parameters.Add(new FixedValueParameter<BoolValue>(ConstantOptimizationUpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized during constant optimization.", new BoolValue(true)));
     107      if (!Parameters.ContainsKey(ParameterOptimizationUpdateVariableWeightsParameterName)) {
     108        if (Parameters.ContainsKey("Constant optimization update variable weights")) {
     109          Parameters.Add(new FixedValueParameter<BoolValue>(ParameterOptimizationUpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized during parameter optimization.",
     110            (BoolValue)Parameters["Constant optimization update variable weights"].ActualValue));
     111          Parameters.Remove("Constant optimization update variable weights");
     112        } else {
     113          Parameters.Add(new FixedValueParameter<BoolValue>(ParameterOptimizationUpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized during parameter optimization.", new BoolValue(true)));
     114        }
    98115      }
    99116    }
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs

    r17180 r18113  
    5353      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    5454
    55       if (UseConstantOptimization) {
    56         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
     55      if (UseParameterOptimization) {
     56        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, solution, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    5757      }
    5858
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs

    r17180 r18113  
    5353      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
    5454
    55       if (UseConstantOptimization) {
    56         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
     55      if (UseParameterOptimization) {
     56        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, solution, problemData, rows, applyLinearScaling, ParameterOptimizationIterations, updateVariableWeights: ParameterOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    5757      }
    5858      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NMSESingleObjectiveConstraintsEvaluator.cs

    r18100 r18113  
    4646      (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName];
    4747
    48     public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter =>
     48    public IFixedValueParameter<IntValue> ParameterOptimizationIterationsParameter =>
    4949      (IFixedValueParameter<IntValue>)Parameters[ParameterOptimizationIterationsParameterName];
    5050
     
    6262    }
    6363
    64     public int ConstantOptimizationIterations {
    65       get => ConstantOptimizationIterationsParameter.Value.Value;
    66       set => ConstantOptimizationIterationsParameter.Value.Value = value;
     64    public int ParameterOptimizationIterations {
     65      get => ParameterOptimizationIterationsParameter.Value.Value;
     66      set => ParameterOptimizationIterationsParameter.Value.Value = value;
    6767    }
    6868
     
    109109
    110110    [StorableHook(HookType.AfterDeserialization)]
    111     private void AfterDeserialization() { }
     111    private void AfterDeserialization() {
     112      if (!Parameters.ContainsKey(ParameterOptimizationIterationsParameterName)) {
     113        if (Parameters.ContainsKey("ParameterOptimizationIterations")) {
     114          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "Define how many parameter optimization steps should be performed (default: 10).", (IntValue)Parameters["ParameterOptimizationIterations"].ActualValue));
     115          Parameters.Remove("ParameterOptimizationIterations");
     116        } else {
     117          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "Define how many parameter optimization steps should be performed (default: 10).", new IntValue(10)));
     118        }
     119      }
     120
     121    }
    112122
    113123    public override IDeepCloneable Clone(Cloner cloner) {
     
    126136
    127137      if (OptimizeParameters) {
    128         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, rows,
    129           false, ConstantOptimizationIterations, true,
     138        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, rows,
     139          false, ParameterOptimizationIterations, true,
    130140          estimationLimits.Lower, estimationLimits.Upper);
    131141      } else {
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionParameterOptimizationEvaluator.cs

    r18112 r18113  
    3232
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    34   [Item("Constant Optimization Evaluator", "Calculates Pearson R² of a symbolic regression solution and optimizes the constant used.")]
     34  [Item("Parameter Optimization Evaluator", "Calculates Pearson R² of a symbolic regression solution and optimizes the parameters used.")]
    3535  [StorableType("24B68851-036D-4446-BD6F-3823E9028FF4")]
    36   public class SymbolicRegressionConstantOptimizationEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
    37     private const string ConstantOptimizationIterationsParameterName = "ConstantOptimizationIterations";
    38     private const string ConstantOptimizationImprovementParameterName = "ConstantOptimizationImprovement";
    39     private const string ConstantOptimizationProbabilityParameterName = "ConstantOptimizationProbability";
    40     private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage";
    41     private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree";
     36  public class SymbolicRegressionParameterOptimizationEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
     37    private const string ParameterOptimizationIterationsParameterName = "ParameterOptimizationIterations";
     38    private const string ParameterOptimizationImprovementParameterName = "ParameterOptimizationImprovement";
     39    private const string ParameterOptimizationProbabilityParameterName = "ParameterOptimizationProbability";
     40    private const string ParameterOptimizationRowsPercentageParameterName = "ParameterOptimizationRowsPercentage";
     41    private const string UpdateParametersInTreeParameterName = "UpdateParametersInSymbolicExpressionTree";
    4242    private const string UpdateVariableWeightsParameterName = "Update Variable Weights";
    4343
    44     private const string FunctionEvaluationsResultParameterName = "Constants Optimization Function Evaluations";
    45     private const string GradientEvaluationsResultParameterName = "Constants Optimization Gradient Evaluations";
     44    private const string FunctionEvaluationsResultParameterName = "Parameters Optimization Function Evaluations";
     45    private const string GradientEvaluationsResultParameterName = "Parameters Optimization Gradient Evaluations";
    4646    private const string CountEvaluationsParameterName = "Count Function and Gradient Evaluations";
    4747
    48     public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
    49       get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
    50     }
    51     public IFixedValueParameter<DoubleValue> ConstantOptimizationImprovementParameter {
    52       get { return (IFixedValueParameter<DoubleValue>)Parameters[ConstantOptimizationImprovementParameterName]; }
    53     }
    54     public IFixedValueParameter<PercentValue> ConstantOptimizationProbabilityParameter {
    55       get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationProbabilityParameterName]; }
    56     }
    57     public IFixedValueParameter<PercentValue> ConstantOptimizationRowsPercentageParameter {
    58       get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationRowsPercentageParameterName]; }
    59     }
    60     public IFixedValueParameter<BoolValue> UpdateConstantsInTreeParameter {
    61       get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateConstantsInTreeParameterName]; }
     48    public IFixedValueParameter<IntValue> ParameterOptimizationIterationsParameter {
     49      get { return (IFixedValueParameter<IntValue>)Parameters[ParameterOptimizationIterationsParameterName]; }
     50    }
     51    public IFixedValueParameter<DoubleValue> ParameterOptimizationImprovementParameter {
     52      get { return (IFixedValueParameter<DoubleValue>)Parameters[ParameterOptimizationImprovementParameterName]; }
     53    }
     54    public IFixedValueParameter<PercentValue> ParameterOptimizationProbabilityParameter {
     55      get { return (IFixedValueParameter<PercentValue>)Parameters[ParameterOptimizationProbabilityParameterName]; }
     56    }
     57    public IFixedValueParameter<PercentValue> ParameterOptimizationRowsPercentageParameter {
     58      get { return (IFixedValueParameter<PercentValue>)Parameters[ParameterOptimizationRowsPercentageParameterName]; }
     59    }
     60    public IFixedValueParameter<BoolValue> UpdateParametersInTreeParameter {
     61      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateParametersInTreeParameterName]; }
    6262    }
    6363    public IFixedValueParameter<BoolValue> UpdateVariableWeightsParameter {
     
    7676
    7777
    78     public IntValue ConstantOptimizationIterations {
    79       get { return ConstantOptimizationIterationsParameter.Value; }
    80     }
    81     public DoubleValue ConstantOptimizationImprovement {
    82       get { return ConstantOptimizationImprovementParameter.Value; }
    83     }
    84     public PercentValue ConstantOptimizationProbability {
    85       get { return ConstantOptimizationProbabilityParameter.Value; }
    86     }
    87     public PercentValue ConstantOptimizationRowsPercentage {
    88       get { return ConstantOptimizationRowsPercentageParameter.Value; }
    89     }
    90     public bool UpdateConstantsInTree {
    91       get { return UpdateConstantsInTreeParameter.Value.Value; }
    92       set { UpdateConstantsInTreeParameter.Value.Value = value; }
     78    public IntValue ParameterOptimizationIterations {
     79      get { return ParameterOptimizationIterationsParameter.Value; }
     80    }
     81    public DoubleValue ParameterOptimizationImprovement {
     82      get { return ParameterOptimizationImprovementParameter.Value; }
     83    }
     84    public PercentValue ParameterOptimizationProbability {
     85      get { return ParameterOptimizationProbabilityParameter.Value; }
     86    }
     87    public PercentValue ParameterOptimizationRowsPercentage {
     88      get { return ParameterOptimizationRowsPercentageParameter.Value; }
     89    }
     90    public bool UpdateParametersInTree {
     91      get { return UpdateParametersInTreeParameter.Value.Value; }
     92      set { UpdateParametersInTreeParameter.Value.Value = value; }
    9393    }
    9494
     
    108108
    109109    [StorableConstructor]
    110     protected SymbolicRegressionConstantOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
    111     protected SymbolicRegressionConstantOptimizationEvaluator(SymbolicRegressionConstantOptimizationEvaluator original, Cloner cloner)
     110    protected SymbolicRegressionParameterOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
     111    protected SymbolicRegressionParameterOptimizationEvaluator(SymbolicRegressionParameterOptimizationEvaluator original, Cloner cloner)
    112112      : base(original, cloner) {
    113113    }
    114     public SymbolicRegressionConstantOptimizationEvaluator()
     114    public SymbolicRegressionParameterOptimizationEvaluator()
    115115      : base() {
    116       Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
    117       Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0)) { Hidden = true });
    118       Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1)));
    119       Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1)));
    120       Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)) { Hidden = true });
     116      Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the parameter of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
     117      Parameters.Add(new FixedValueParameter<DoubleValue>(ParameterOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the parameter optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0)) { Hidden = true });
     118      Parameters.Add(new FixedValueParameter<PercentValue>(ParameterOptimizationProbabilityParameterName, "Determines the probability that the parameters are optimized", new PercentValue(1)));
     119      Parameters.Add(new FixedValueParameter<PercentValue>(ParameterOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for parameter optimization", new PercentValue(1)));
     120      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateParametersInTreeParameterName, "Determines if the parameters in the tree should be overwritten by the optimized parameters.", new BoolValue(true)) { Hidden = true });
    121121      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be  optimized.", new BoolValue(true)) { Hidden = true });
    122122
    123123      Parameters.Add(new FixedValueParameter<BoolValue>(CountEvaluationsParameterName, "Determines if function and gradient evaluation should be counted.", new BoolValue(false)));
    124       Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
    125       Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
     124      Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the parameters optimization evaluator", "Results", new IntValue()));
     125      Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the parameters optimization evaluator", "Results", new IntValue()));
    126126    }
    127127
    128128    public override IDeepCloneable Clone(Cloner cloner) {
    129       return new SymbolicRegressionConstantOptimizationEvaluator(this, cloner);
     129      return new SymbolicRegressionParameterOptimizationEvaluator(this, cloner);
    130130    }
    131131
    132132    [StorableHook(HookType.AfterDeserialization)]
    133133    private void AfterDeserialization() {
    134       if (!Parameters.ContainsKey(UpdateConstantsInTreeParameterName))
    135         Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)));
     134      if (!Parameters.ContainsKey(UpdateParametersInTreeParameterName)) {
     135        if (Parameters.ContainsKey("UpdateConstantsInSymbolicExpressionTree")) {
     136          Parameters.Add(new FixedValueParameter<BoolValue>(UpdateParametersInTreeParameterName, "Determines if the parameters in the tree should be overwritten by the optimized parameters.", (BoolValue)Parameters["UpdateConstantsInSymbolicExpressionTree"].ActualValue));
     137          Parameters.Remove("UpdateConstantsInSymbolicExpressionTree");
     138        } else {
     139          Parameters.Add(new FixedValueParameter<BoolValue>(UpdateParametersInTreeParameterName, "Determines if the parameters in the tree should be overwritten by the optimized parameters.", new BoolValue(true)));
     140        }
     141      }
     142
    136143      if (!Parameters.ContainsKey(UpdateVariableWeightsParameterName))
    137144        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be  optimized.", new BoolValue(true)));
     
    140147        Parameters.Add(new FixedValueParameter<BoolValue>(CountEvaluationsParameterName, "Determines if function and gradient evaluation should be counted.", new BoolValue(false)));
    141148
    142       if (!Parameters.ContainsKey(FunctionEvaluationsResultParameterName))
    143         Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
    144       if (!Parameters.ContainsKey(GradientEvaluationsResultParameterName))
    145         Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
     149      if (!Parameters.ContainsKey(FunctionEvaluationsResultParameterName)) {
     150        if (Parameters.ContainsKey("Constants Optimization Function Evaluations")) {
     151          Parameters.Remove("Constants Optimization Function Evaluations");
     152        }
     153        Parameters.Add(new ResultParameter<IntValue>(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the parameters optimization evaluator", "Results", new IntValue()));
     154      }
     155
     156      if (!Parameters.ContainsKey(GradientEvaluationsResultParameterName)) {
     157        if (Parameters.ContainsKey("Constants Optimization Gradient Evaluations")) {
     158          Parameters.Remove("Constants Optimization Gradient Evaluations");
     159        }
     160        Parameters.Add(new ResultParameter<IntValue>(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the parameters optimization evaluator", "Results", new IntValue()));
     161      }
     162
     163      if (!Parameters.ContainsKey(ParameterOptimizationIterationsParameterName)) {
     164        if (Parameters.ContainsKey("ConstantOptimizationIterations")) {
     165          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the parameter of a symbolic expression tree (0 indicates other or default stopping criterion).", (IntValue)Parameters["ConstantOptimizationIterations"].ActualValue));
     166          Parameters.Remove("ConstantOptimizationIterations");
     167        } else {
     168          Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the parameter of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
     169        }
     170      }
     171
     172      if (!Parameters.ContainsKey(ParameterOptimizationImprovementParameterName)) {
     173        if (Parameters.ContainsKey("CosntantOptimizationImprovement")) {
     174          Parameters.Add(new FixedValueParameter<DoubleValue>(ParameterOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the parameter optimization to continue with it (0 indicates other or default stopping criterion).",
     175            (DoubleValue)Parameters["CosntantOptimizationImprovement"].ActualValue) { Hidden = true });
     176          Parameters.Remove("CosntantOptimizationImprovement");
     177        } else {
     178          Parameters.Add(new FixedValueParameter<DoubleValue>(ParameterOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the parameter optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0)) { Hidden = true });
     179        }
     180      }
     181
     182      if (!Parameters.ContainsKey(ParameterOptimizationProbabilityParameterName)) {
     183        if (Parameters.ContainsKey("ConstantOptimizationProbability")) {
     184          Parameters.Add(new FixedValueParameter<PercentValue>(ParameterOptimizationProbabilityParameterName, "Determines the probability that the parameters are optimized",
     185            (PercentValue)Parameters["ConstantOptimizationProbability"].ActualValue));
     186          Parameters.Remove("ConstantOptimizationProbability");
     187        } else {
     188          Parameters.Add(new FixedValueParameter<PercentValue>(ParameterOptimizationProbabilityParameterName, "Determines the probability that the parameters are optimized", new PercentValue(1)));
     189        }
     190      }
     191
     192      if (!Parameters.ContainsKey(ParameterOptimizationRowsPercentageParameterName)) {
     193        if (Parameters.ContainsKey("ConstantOptimizationRowsPercentage")) {
     194          Parameters.Add(new FixedValueParameter<PercentValue>(ParameterOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for parameter optimization", (PercentValue)Parameters["ConstantOptimizationRowsPercentage"].ActualValue));
     195          Parameters.Remove("ConstantOptimizationRowsPercentage");
     196        } else {
     197          Parameters.Add(new FixedValueParameter<PercentValue>(ParameterOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for parameter optimization", new PercentValue(1)));
     198        }
     199       
     200      }
    146201    }
    147202
     
    150205      var solution = SymbolicExpressionTreeParameter.ActualValue;
    151206      double quality;
    152       if (RandomParameter.ActualValue.NextDouble() < ConstantOptimizationProbability.Value) {
    153         IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
     207      if (RandomParameter.ActualValue.NextDouble() < ParameterOptimizationProbability.Value) {
     208        IEnumerable<int> parameterOptimizationRows = GenerateRowsToEvaluate(ParameterOptimizationRowsPercentage.Value);
    154209        var counter = new EvaluationsCounter();
    155         quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
    156            constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree, counter: counter);
    157 
    158         if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
     210        quality = OptimizeParameters(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
     211           parameterOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ParameterOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateParametersInTree: UpdateParametersInTree, counter: counter);
     212
     213        if (ParameterOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
    159214          var evaluationRows = GenerateRowsToEvaluate();
    160215          quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
     
    203258    }
    204259
    205     public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     260    public static double OptimizeParameters(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    206261      ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling,
    207262      int maxIterations, bool updateVariableWeights = true,
    208263      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
    209       bool updateConstantsInTree = true, Action<double[], double, object> iterationCallback = null, EvaluationsCounter counter = null) {
    210 
    211       // Numeric constants in the tree become variables for parameter optimization.
     264      bool updateParametersInTree = true, Action<double[], double, object> iterationCallback = null, EvaluationsCounter counter = null) {
     265
     266      // Numeric parameters in the tree become variables for parameter optimization.
    212267      // Variables in the tree become parameters (fixed values) for parameter optimization.
    213268      // For each parameter (variable in the original tree) we store the
    214269      // variable name, variable value (for factor vars) and lag as a DataForVariable object.
    215270      // A dictionary is used to find parameters
    216       double[] initialConstants;
     271      double[] initialParameters;
    217272      var parameters = new List<TreeToAutoDiffTermConverter.DataForVariable>();
    218273
    219274      TreeToAutoDiffTermConverter.ParametricFunction func;
    220275      TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad;
    221       if (!TreeToAutoDiffTermConverter.TryConvertToAutoDiff(tree, updateVariableWeights, applyLinearScaling, out parameters, out initialConstants, out func, out func_grad))
    222         throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
     276      if (!TreeToAutoDiffTermConverter.TryConvertToAutoDiff(tree, updateVariableWeights, applyLinearScaling, out parameters, out initialParameters, out func, out func_grad))
     277        throw new NotSupportedException("Could not optimize parameters of symbolic expression tree due to not supported symbols used in the tree.");
    223278      if (parameters.Count == 0) return 0.0; // constant expressions always have a R² of 0.0
    224279      var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
    225280
    226       // extract inital constants
     281      // extract inital parameters
    227282      double[] c;
    228283      if (applyLinearScaling) {
    229         c = new double[initialConstants.Length + 2];
     284        c = new double[initialParameters.Length + 2];
    230285        c[0] = 0.0;
    231286        c[1] = 1.0;
    232         Array.Copy(initialConstants, 0, c, 2, initialConstants.Length);
     287        Array.Copy(initialParameters, 0, c, 2, initialParameters.Length);
    233288      } else {
    234         c = (double[])initialConstants.Clone();
     289        c = (double[])initialParameters.Clone();
    235290      }
    236291
     
    283338      counter.GradientEvaluations += rowEvaluationsCounter.GradientEvaluations / n;
    284339
    285       //retVal == -7  => constant optimization failed due to wrong gradient
     340      //retVal == -7  => parameter optimization failed due to wrong gradient
    286341      //          -8  => optimizer detected  NAN / INF  in  the target
    287342      //                 function and/ or gradient
     
    290345          var tmp = new double[c.Length - 2];
    291346          Array.Copy(c, 2, tmp, 0, tmp.Length);
    292           UpdateConstants(tree, tmp, updateVariableWeights);
    293         } else UpdateConstants(tree, c, updateVariableWeights);
     347          UpdateParameters(tree, tmp, updateVariableWeights);
     348        } else UpdateParameters(tree, c, updateVariableWeights);
    294349      }
    295350      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
    296351
    297       if (!updateConstantsInTree) UpdateConstants(tree, initialConstants, updateVariableWeights);
     352      if (!updateParametersInTree) UpdateParameters(tree, initialParameters, updateVariableWeights);
    298353
    299354      if (originalQuality - quality > 0.001 || double.IsNaN(quality)) {
    300         UpdateConstants(tree, initialConstants, updateVariableWeights);
     355        UpdateParameters(tree, initialParameters, updateVariableWeights);
    301356        return originalQuality;
    302357      }
     
    304359    }
    305360
    306     private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants, bool updateVariableWeights) {
     361    private static void UpdateParameters(ISymbolicExpressionTree tree, double[] parameters, bool updateVariableWeights) {
    307362      int i = 0;
    308363      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
     
    313368          if (numberTreeNode.Parent.Symbol is Power
    314369              && numberTreeNode.Parent.GetSubtree(1) == numberTreeNode) continue; // exponents in powers are not optimizated (see TreeToAutoDiffTermConverter)
    315           numberTreeNode.Value = constants[i++];
     370          numberTreeNode.Value = parameters[i++];
    316371        } else if (updateVariableWeights && variableTreeNodeBase != null)
    317           variableTreeNodeBase.Weight = constants[i++];
     372          variableTreeNodeBase.Weight = parameters[i++];
    318373        else if (factorVarTreeNode != null) {
    319374          for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
    320             factorVarTreeNode.Weights[j] = constants[i++];
     375            factorVarTreeNode.Weights[j] = parameters[i++];
    321376        }
    322377      }
     
    340395      };
    341396    }
    342     public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
     397    public static bool CanOptimizeParameters(ISymbolicExpressionTree tree) {
    343398      return TreeToAutoDiffTermConverter.IsCompatible(tree);
    344399    }
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/ParameterOptimizationAnalyzer.cs

    r18112 r18113  
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3333  /// <summary>
    34   /// An operator that optimizes the constants for the best symbolic expression tress in the current generation.
     34  /// An operator that optimizes the parameters for the best symbolic expression tress in the current generation.
    3535  /// </summary>
    36   [Item("ConstantOptimizationAnalyzer", "An operator that performs a constant optimization on the best symbolic expression trees.")]
     36  [Item("ParameterOptimizationAnalyzer", "An operator that performs a parameter optimization on the best symbolic expression trees.")]
    3737  [StorableType("9FB87E7B-A9E2-49DD-A92A-78BD9FC17916")]
    38   public sealed class ConstantOptimizationAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer, IStatefulItem {
     38  public sealed class ParameterOptimizationAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer, IStatefulItem {
    3939    private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions";
    40     private const string ConstantOptimizationEvaluatorParameterName = "ConstantOptimizationOperator";
     40    private const string ParameterOptimizationEvaluatorParameterName = "ParameterOptimizationOperator";
    4141
    42     private const string DataTableNameConstantOptimizationImprovement = "Constant Optimization Improvement";
     42    private const string DataTableNameParameterOptimizationImprovement = "Parameter Optimization Improvement";
    4343    private const string DataRowNameMinimumImprovement = "Minimum improvement";
    4444    private const string DataRowNameMedianImprovement = "Median improvement";
     
    5151    }
    5252
    53     public IFixedValueParameter<SymbolicRegressionConstantOptimizationEvaluator> ConstantOptimizationEvaluatorParameter {
    54       get { return (IFixedValueParameter<SymbolicRegressionConstantOptimizationEvaluator>)Parameters[ConstantOptimizationEvaluatorParameterName]; }
     53    public IFixedValueParameter<SymbolicRegressionParameterOptimizationEvaluator> ParameterOptimizationEvaluatorParameter {
     54      get { return (IFixedValueParameter<SymbolicRegressionParameterOptimizationEvaluator>)Parameters[ParameterOptimizationEvaluatorParameterName]; }
    5555    }
    5656    #endregion
    5757
    5858    #region properties
    59     public SymbolicRegressionConstantOptimizationEvaluator ConstantOptimizationEvaluator {
    60       get { return ConstantOptimizationEvaluatorParameter.Value; }
     59    public SymbolicRegressionParameterOptimizationEvaluator ParameterOptimizationEvaluator {
     60      get { return ParameterOptimizationEvaluatorParameter.Value; }
    6161    }
    6262    public double PercentageOfBestSolutions {
     
    6464    }
    6565
    66     private DataTable ConstantOptimizationImprovementDataTable {
     66    private DataTable ParameterOptimizationImprovementDataTable {
    6767      get {
    6868        IResult result;
    69         ResultCollection.TryGetValue(DataTableNameConstantOptimizationImprovement, out result);
     69        ResultCollection.TryGetValue(DataTableNameParameterOptimizationImprovement, out result);
    7070        if (result == null) return null;
    7171        return (DataTable)result.Value;
     
    7373    }
    7474    private DataRow MinimumImprovement {
    75       get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameMinimumImprovement]; }
     75      get { return ParameterOptimizationImprovementDataTable.Rows[DataRowNameMinimumImprovement]; }
    7676    }
    7777    private DataRow MedianImprovement {
    78       get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameMedianImprovement]; }
     78      get { return ParameterOptimizationImprovementDataTable.Rows[DataRowNameMedianImprovement]; }
    7979    }
    8080    private DataRow AverageImprovement {
    81       get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameAverageImprovement]; }
     81      get { return ParameterOptimizationImprovementDataTable.Rows[DataRowNameAverageImprovement]; }
    8282    }
    8383    private DataRow MaximumImprovement {
    84       get { return ConstantOptimizationImprovementDataTable.Rows[DataRowNameMaximumImprovement]; }
     84      get { return ParameterOptimizationImprovementDataTable.Rows[DataRowNameMaximumImprovement]; }
    8585    }
    8686    #endregion
    8787
    8888    [StorableConstructor]
    89     private ConstantOptimizationAnalyzer(StorableConstructorFlag _) : base(_) { }
    90     private ConstantOptimizationAnalyzer(ConstantOptimizationAnalyzer original, Cloner cloner) : base(original, cloner) { }
    91     public override IDeepCloneable Clone(Cloner cloner) { return new ConstantOptimizationAnalyzer(this, cloner); }
    92     public ConstantOptimizationAnalyzer()
     89    private ParameterOptimizationAnalyzer(StorableConstructorFlag _) : base(_) { }
     90    private ParameterOptimizationAnalyzer(ParameterOptimizationAnalyzer original, Cloner cloner) : base(original, cloner) { }
     91    public override IDeepCloneable Clone(Cloner cloner) { return new ParameterOptimizationAnalyzer(this, cloner); }
     92    public ParameterOptimizationAnalyzer()
    9393      : base() {
    9494      Parameters.Add(new FixedValueParameter<PercentValue>(PercentageOfBestSolutionsParameterName, "The percentage of the top solutions which should be analyzed.", new PercentValue(0.1)));
    95       Parameters.Add(new FixedValueParameter<SymbolicRegressionConstantOptimizationEvaluator>(ConstantOptimizationEvaluatorParameterName, "The operator used to perform the constant optimization"));
     95      Parameters.Add(new FixedValueParameter<SymbolicRegressionParameterOptimizationEvaluator>(ParameterOptimizationEvaluatorParameterName, "The operator used to perform the parameter optimization"));
    9696
    9797      //Changed the ActualName of the EvaluationPartitionParameter so that it matches the parameter name of symbolic regression problems.
    98       ConstantOptimizationEvaluator.EvaluationPartitionParameter.ActualName = "FitnessCalculationPartition";
     98      ParameterOptimizationEvaluator.EvaluationPartitionParameter.ActualName = "FitnessCalculationPartition";
     99    }
     100
     101    [StorableHook(HookType.AfterDeserialization)]
     102    private void AfterDeserialization() {
     103      if (!Parameters.ContainsKey(ParameterOptimizationEvaluatorParameterName)) {
     104        if (Parameters.ContainsKey("ConstantOptimizationOperator")) {
     105          Parameters.Add(new FixedValueParameter<SymbolicRegressionParameterOptimizationEvaluator>(ParameterOptimizationEvaluatorParameterName, "The operator used to perform the parameter optimization"));
     106          Parameters.Remove("ConstantOptimizationOperator");
     107        } else {
     108          Parameters.Add(new FixedValueParameter<SymbolicRegressionParameterOptimizationEvaluator>(ParameterOptimizationEvaluatorParameterName, "The operator used to perform the parameter optimization"));
     109        }
     110      }
    99111    }
    100112
     
    134146        operationCollection.Parallel = true;
    135147        foreach (var scopeIndex in scopeIndexes) {
    136           var childOperation = ExecutionContext.CreateChildOperation(ConstantOptimizationEvaluator, ExecutionContext.Scope.SubScopes[scopeIndex]);
     148          var childOperation = ExecutionContext.CreateChildOperation(ParameterOptimizationEvaluator, ExecutionContext.Scope.SubScopes[scopeIndex]);
    137149          operationCollection.Add(childOperation);
    138150        }
     
    141153      }
    142154
    143       //code executed to analyze results of constant optimization
     155      //code executed to analyze results of parameter optimization
    144156      double[] qualitiesAfterCoOp = scopeIndexes.Select(x => Quality[x].Value).ToArray();
    145157      var qualityImprovement = qualitiesBeforeCoOp.Zip(qualitiesAfterCoOp, (b, a) => a - b).ToArray();
    146158
    147       if (!ResultCollection.ContainsKey(DataTableNameConstantOptimizationImprovement)) {
    148         var dataTable = new DataTable(DataTableNameConstantOptimizationImprovement);
    149         ResultCollection.Add(new Result(DataTableNameConstantOptimizationImprovement, dataTable));
     159      if (!ResultCollection.ContainsKey(DataTableNameParameterOptimizationImprovement)) {
     160        var dataTable = new DataTable(DataTableNameParameterOptimizationImprovement);
     161        ResultCollection.Add(new Result(DataTableNameParameterOptimizationImprovement, dataTable));
    150162        dataTable.VisualProperties.YAxisTitle = "R²";
    151163
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs

    r17902 r18113  
    4141    }
    4242
    43     private static readonly Constant constantSy = new Constant();
     43    private static readonly Number numberSy = new Number();
     44    private static readonly Constant constSy = new Constant();
    4445    private static readonly Addition addSy = new Addition();
    4546    private static readonly Subtraction subSy = new Subtraction();
     
    5253
    5354    public static ISymbolicExpressionTreeNode Derive(ISymbolicExpressionTreeNode branch, string variableName) {
    54       if (branch.Symbol is Constant) {
    55         return CreateConstant(0.0);
     55      if (branch.Symbol is INumericSymbol) {
     56        return CreateNumber(0.0);
    5657      }
    5758      if (branch.Symbol is Variable) {
    5859        var varNode = branch as VariableTreeNode;
    5960        if (varNode.VariableName == variableName) {
    60           return CreateConstant(varNode.Weight);
     61          return CreateNumber(varNode.Weight);
    6162        } else {
    62           return CreateConstant(0.0);
     63          return CreateNumber(0.0);
    6364        }
    6465      }
     
    102103        if (branch.SubtreeCount == 1) {
    103104          var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    104           var gPrime = Product(CreateConstant(-1.0), Derive(g, variableName));
     105          var gPrime = Product(CreateNumber(-1.0), Derive(g, variableName));
    105106          var sqrNode = new Square().CreateTreeNode();
    106107          sqrNode.AddSubtree(g);
     
    125126      if (branch.Symbol is Logarithm) {
    126127        var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    127         return Product(Div(CreateConstant(1.0), f), Derive(f, variableName));
     128        return Product(Div(CreateNumber(1.0), f), Derive(f, variableName));
    128129      }
    129130      if (branch.Symbol is Exponential) {
     
    133134      if (branch.Symbol is Square) {
    134135        var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    135         return Product(Product(CreateConstant(2.0), f), Derive(f, variableName));
     136        return Product(Product(CreateNumber(2.0), f), Derive(f, variableName));
    136137      }
    137138      if (branch.Symbol is SquareRoot) {
    138139        var f = (ISymbolicExpressionTreeNode)branch.Clone();
    139140        var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    140         return Product(Div(CreateConstant(1.0), Product(CreateConstant(2.0), f)), Derive(u, variableName));
     141        return Product(Div(CreateNumber(1.0), Product(CreateNumber(2.0), f)), Derive(u, variableName));
    141142      }
    142143      if (branch.Symbol is CubeRoot) {
    143144        var f = (ISymbolicExpressionTreeNode)branch.Clone();
    144145        var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    145         return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName));  // 1/3 1/cbrt(f(x))^2 d/dx f(x)
     146        return Product(Div(CreateNumber(1.0), Product(CreateNumber(3.0), Square(f))), Derive(u, variableName));  // 1/3 1/cbrt(f(x))^2 d/dx f(x)
    146147      }
    147148      if (branch.Symbol is Cube) {
    148149        var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    149         return Product(Product(CreateConstant(3.0), Square(f)), Derive(f, variableName));
     150        return Product(Product(CreateNumber(3.0), Square(f)), Derive(f, variableName));
    150151      }
    151152      if (branch.Symbol is Power) {
    152153        // HL evaluators handle power strangely (exponent is rounded to an integer)
    153154        // here we only support the case when the exponent is a constant integer
    154         var exponent = branch.GetSubtree(1) as ConstantTreeNode;
     155        var exponent = branch.GetSubtree(1) as INumericTreeNode;
    155156        if (exponent != null && Math.Truncate(exponent.Value) == exponent.Value) {
    156157          var newPower = (ISymbolicExpressionTreeNode)branch.Clone();
    157158          var f = (ISymbolicExpressionTreeNode)newPower.GetSubtree(0).Clone();
    158           var newExponent = (ConstantTreeNode)newPower.GetSubtree(1);
     159          var newExponent = (INumericTreeNode)newPower.GetSubtree(1);
    159160          newExponent.Value -= 1;
    160           return Product(Product(CreateConstant(exponent.Value), newPower), Derive(f, variableName));
     161          return Product(Product(CreateNumber(exponent.Value), newPower), Derive(f, variableName));
    161162        } else throw new NotSupportedException("Cannot derive non-integer powers");
    162163      }
     
    171172        var b = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();
    172173
    173         var definition = Div(a, SquareRoot(Sum(Square(b), CreateConstant(1.0))));
     174        var definition = Div(a, SquareRoot(Sum(Square(b), CreateNumber(1.0))));
    174175        return Derive(definition, variableName);
    175176      }
     
    184185        var sin = (new Sine()).CreateTreeNode();
    185186        sin.AddSubtree(u);
    186         return Product(CreateConstant(-1.0), Product(sin, Derive(u, variableName)));
     187        return Product(CreateNumber(-1.0), Product(sin, Derive(u, variableName)));
    187188      }
    188189      if (branch.Symbol is Tangent) {
     
    196197        var fxp = Derive(branch.GetSubtree(0), variableName);
    197198        var tanh = (ISymbolicExpressionTreeNode)branch.Clone();
    198         return Product(fxp, Subtract(CreateConstant(1.0), Square(tanh)));
     199        return Product(fxp, Subtract(CreateNumber(1.0), Square(tanh)));
    199200      }
    200201      throw new NotSupportedException(string.Format("Symbol {0} is not supported.", branch.Symbol));
     
    253254    }
    254255
    255     private static ISymbolicExpressionTreeNode CreateConstant(double v) {
    256       var constNode = (ConstantTreeNode)constantSy.CreateTreeNode();
    257       constNode.Value = v;
    258       return constNode;
     256    private static ISymbolicExpressionTreeNode CreateNumber(double v) {
     257      var numberNode = (NumberTreeNode)numberSy.CreateTreeNode();
     258      numberNode.Value = v;
     259      return numberNode;
    259260    }
    260261
     
    264265        where
    265266          !(n.Symbol is Variable) &&
     267          !(n.Symbol is Number) &&
    266268          !(n.Symbol is Constant) &&
    267269          !(n.Symbol is Addition) &&
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs

    r17963 r18113  
    3636    private static readonly Multiplication mulSymbol = new Multiplication();
    3737    private static readonly Division divSymbol = new Division();
     38    private static readonly Number numberSymbol = new Number();
    3839    private static readonly Constant constSymbol = new Constant();
    3940    private static readonly Absolute absSymbol = new Absolute();
     
    240241    }
    241242
     243    private static bool IsNumber(ISymbolicExpressionTreeNode node) {
     244      return node.Symbol is Number;
     245    }
     246
    242247    private static bool IsConstant(ISymbolicExpressionTreeNode node) {
    243248      return node.Symbol is Constant;
    244249    }
    245 
    246250    // dynamic
    247251    private static bool IsTimeLag(ISymbolicExpressionTreeNode node) {
     
    261265    /// <returns></returns>
    262266    public static ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
    263       if (IsConstant(original) || IsVariableBase(original)) {
     267      if (IsNumber(original) || IsConstant(original) || IsVariableBase(original)) {
    264268        return (ISymbolicExpressionTreeNode)original.Clone();
    265269      } else if (IsAbsolute(original)) {
     
    335339        clone.AddSubtree(simplifiedSubtree);
    336340      }
    337       if (simplifiedSubtrees.TrueForAll(t => IsConstant(t))) {
     341      if (simplifiedSubtrees.TrueForAll(IsNumber)) {
    338342        SimplifyConstantExpression(clone);
    339343      }
     
    501505      var laggedTreeNode = original as ILaggedTreeNode;
    502506      var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
    503       if (IsConstant(simplifiedSubtree)) {
     507      if (IsNumber(simplifiedSubtree)) {
    504508        return GetSimplifiedTree(MakeProduct(simplifiedSubtree, MakeConstant(-laggedTreeNode.Lag)));
    505509      } else {
     
    514518    private static ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {
    515519      if (lag == 0) return subtree;
    516       if (IsConstant(subtree)) return subtree;
     520      if (IsNumber(subtree)) return subtree;
    517521      var lagNode = (LaggedTreeNode)timeLagSymbol.CreateTreeNode();
    518522      lagNode.Lag = lag;
     
    534538
    535539    private static ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {
    536       if (IsConstant(t)) {
    537         var constNode = t as ConstantTreeNode;
     540      if (IsNumber(t)) {
     541        var constNode = t as NumberTreeNode;
    538542        if (constNode.Value > 0) return MakeConstant(-1.0);
    539543        else return MakeConstant(1.0);
     
    555559
    556560    private static ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    557       if (IsConstant(a) && IsConstant(b)) {
    558         var constA = a as ConstantTreeNode;
    559         var constB = b as ConstantTreeNode;
     561      if (IsNumber(a) && IsNumber(b)) {
     562        var constA = a as NumberTreeNode;
     563        var constB = b as NumberTreeNode;
    560564        if (constA.Value > 0.0 || constB.Value > 0.0) {
    561565          return MakeConstant(1.0);
     
    563567          return MakeConstant(-1.0);
    564568        }
    565       } else if (IsConstant(a)) {
     569      } else if (IsNumber(a)) {
    566570        return MakeOr(b, a);
    567       } else if (IsConstant(b)) {
    568         var constT = b as ConstantTreeNode;
     571      } else if (IsNumber(b)) {
     572        var constT = b as NumberTreeNode;
    569573        if (constT.Value > 0.0) {
    570574          // boolean expression is necessarily true
     
    585589
    586590    private static ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    587       if (IsConstant(a) && IsConstant(b)) {
    588         var constA = a as ConstantTreeNode;
    589         var constB = b as ConstantTreeNode;
     591      if (IsNumber(a) && IsNumber(b)) {
     592        var constA = a as NumberTreeNode;
     593        var constB = b as NumberTreeNode;
    590594        if (constA.Value > 0.0 && constB.Value > 0.0) {
    591595          return MakeConstant(1.0);
     
    593597          return MakeConstant(-1.0);
    594598        }
    595       } else if (IsConstant(a)) {
     599      } else if (IsNumber(a)) {
    596600        return MakeAnd(b, a);
    597       } else if (IsConstant(b)) {
    598         var constB = b as ConstantTreeNode;
     601      } else if (IsNumber(b)) {
     602        var constB = b as NumberTreeNode;
    599603        if (constB.Value > 0.0) {
    600604          // the constant value has no effect on the result of the boolean condition so we can drop the constant term
     
    616620    private static ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,
    617621      ISymbolicExpressionTreeNode rightSide) {
    618       if (IsConstant(leftSide) && IsConstant(rightSide)) {
    619         var lsConst = leftSide as ConstantTreeNode;
    620         var rsConst = rightSide as ConstantTreeNode;
     622      if (IsNumber(leftSide) && IsNumber(rightSide)) {
     623        var lsConst = leftSide as NumberTreeNode;
     624        var rsConst = rightSide as NumberTreeNode;
    621625        if (lsConst.Value < rsConst.Value) return MakeConstant(1.0);
    622626        else return MakeConstant(-1.0);
     
    631635    private static ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,
    632636      ISymbolicExpressionTreeNode rightSide) {
    633       if (IsConstant(leftSide) && IsConstant(rightSide)) {
    634         var lsConst = leftSide as ConstantTreeNode;
    635         var rsConst = rightSide as ConstantTreeNode;
     637      if (IsNumber(leftSide) && IsNumber(rightSide)) {
     638        var lsConst = leftSide as NumberTreeNode;
     639        var rsConst = rightSide as NumberTreeNode;
    636640        if (lsConst.Value > rsConst.Value) return MakeConstant(1.0);
    637641        else return MakeConstant(-1.0);
     
    646650    private static ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,
    647651      ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) {
    648       if (IsConstant(condition)) {
    649         var constT = condition as ConstantTreeNode;
     652      if (IsNumber(condition)) {
     653        var constT = condition as NumberTreeNode;
    650654        if (constT.Value > 0.0) return trueBranch;
    651655        else return falseBranch;
     
    667671
    668672    private static ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {
    669       if (IsConstant(node)) {
    670         var constT = node as ConstantTreeNode;
     673      if (IsNumber(node)) {
     674        var constT = node as NumberTreeNode;
    671675        return MakeConstant(Math.Sin(constT.Value));
    672676      } else if (IsFactor(node)) {
     
    684688
    685689    private static ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {
    686       if (IsConstant(node)) {
    687         var constT = node as ConstantTreeNode;
     690      if (IsNumber(node)) {
     691        var constT = node as NumberTreeNode;
    688692        return MakeConstant(Math.Tan(constT.Value));
    689693      } else if (IsFactor(node)) {
     
    701705
    702706    private static ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {
    703       if (IsConstant(node)) {
    704         var constT = node as ConstantTreeNode;
     707      if (IsNumber(node)) {
     708        var constT = node as NumberTreeNode;
    705709        return MakeConstant(Math.Cos(constT.Value));
    706710      } else if (IsFactor(node)) {
     
    720724
    721725    private static ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {
    722       if (IsConstant(node)) {
    723         var constT = node as ConstantTreeNode;
     726      if (IsNumber(node)) {
     727        var constT = node as NumberTreeNode;
    724728        return MakeConstant(Math.Exp(constT.Value));
    725729      } else if (IsFactor(node)) {
     
    744748    }
    745749    private static ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
    746       if (IsConstant(node)) {
    747         var constT = node as ConstantTreeNode;
     750      if (IsNumber(node)) {
     751        var constT = node as NumberTreeNode;
    748752        return MakeConstant(Math.Log(constT.Value));
    749753      } else if (IsFactor(node)) {
     
    762766
    763767    private static ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {
    764       if (IsConstant(node)) {
    765         var constT = node as ConstantTreeNode;
     768      if (IsNumber(node)) {
     769        var constT = node as NumberTreeNode;
    766770        return MakeConstant(constT.Value * constT.Value);
    767771      } else if (IsFactor(node)) {
     
    796800
    797801    private static ISymbolicExpressionTreeNode MakeCube(ISymbolicExpressionTreeNode node) {
    798       if (IsConstant(node)) {
    799         var constT = node as ConstantTreeNode;
     802      if (IsNumber(node)) {
     803        var constT = node as NumberTreeNode;
    800804        return MakeConstant(constT.Value * constT.Value * constT.Value);
    801805      } else if (IsFactor(node)) {
     
    821825
    822826    private static ISymbolicExpressionTreeNode MakeAbs(ISymbolicExpressionTreeNode node) {
    823       if (IsConstant(node)) {
    824         var constT = node as ConstantTreeNode;
     827      if (IsNumber(node)) {
     828        var constT = node as NumberTreeNode;
    825829        return MakeConstant(Math.Abs(constT.Value));
    826830      } else if (IsFactor(node)) {
     
    853857    // constant folding only
    854858    private static ISymbolicExpressionTreeNode MakeAnalyticalQuotient(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    855       if (IsConstant(b)) {
    856         var c = b as ConstantTreeNode;
     859      if (IsNumber(b)) {
     860        var c = b as NumberTreeNode;
    857861        return MakeFraction(a, MakeConstant(Math.Sqrt(1.0 + c.Value * c.Value)));
    858862      } else if (IsFactor(b)) {
     
    871875
    872876    private static ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {
    873       if (IsConstant(node)) {
    874         var constT = node as ConstantTreeNode;
     877      if (IsNumber(node)) {
     878        var constT = node as NumberTreeNode;
    875879        return MakeConstant(Math.Sqrt(constT.Value));
    876880      } else if (IsFactor(node)) {
     
    890894
    891895    private static ISymbolicExpressionTreeNode MakeCubeRoot(ISymbolicExpressionTreeNode node) {
    892       if (IsConstant(node)) {
    893         var constT = node as ConstantTreeNode;
     896      if (IsNumber(node)) {
     897        var constT = node as NumberTreeNode;
    894898        return MakeConstant(Math.Pow(constT.Value, 1.0 / 3.0));
    895899      } else if (IsFactor(node)) {
     
    909913
    910914    private static ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    911       if (IsConstant(a) && IsConstant(b)) {
    912         var constA = a as ConstantTreeNode;
    913         var constB = b as ConstantTreeNode;
     915      if (IsNumber(a) && IsNumber(b)) {
     916        var constA = a as NumberTreeNode;
     917        var constB = b as NumberTreeNode;
    914918        return MakeConstant(Math.Pow(constA.Value, 1.0 / Math.Round(constB.Value)));
    915       } else if (IsFactor(a) && IsConstant(b)) {
     919      } else if (IsFactor(a) && IsNumber(b)) {
    916920        var factNode = a as FactorVariableTreeNode;
    917         var constNode = b as ConstantTreeNode;
     921        var constNode = b as NumberTreeNode;
    918922        return MakeFactor(factNode.Symbol, factNode.VariableName,
    919923          factNode.Weights.Select(w => Math.Pow(w, 1.0 / Math.Round(constNode.Value))));
    920       } else if (IsBinFactor(a) && IsConstant(b)) {
     924      } else if (IsBinFactor(a) && IsNumber(b)) {
    921925        var binFactor = a as BinaryFactorVariableTreeNode;
    922         var constNode = b as ConstantTreeNode;
     926        var constNode = b as NumberTreeNode;
    923927        return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Pow(binFactor.Weight, 1.0 / Math.Round(constNode.Value)));
    924       } else if (IsConstant(a) && IsFactor(b)) {
    925         var constNode = a as ConstantTreeNode;
     928      } else if (IsNumber(a) && IsFactor(b)) {
     929        var constNode = a as NumberTreeNode;
    926930        var factNode = b as FactorVariableTreeNode;
    927931        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(constNode.Value, 1.0 / Math.Round(w))));
    928       } else if (IsConstant(a) && IsBinFactor(b)) {
    929         var constNode = a as ConstantTreeNode;
     932      } else if (IsNumber(a) && IsBinFactor(b)) {
     933        var constNode = a as NumberTreeNode;
    930934        var factNode = b as BinaryFactorVariableTreeNode;
    931935        return MakeBinFactor(factNode.Symbol, factNode.VariableName, factNode.VariableValue, Math.Pow(constNode.Value, 1.0 / Math.Round(factNode.Weight)));
     
    934938        var node1 = b as FactorVariableTreeNode;
    935939        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => Math.Pow(u, 1.0 / Math.Round(v))));
    936       } else if (IsConstant(b)) {
    937         var constB = b as ConstantTreeNode;
     940      } else if (IsNumber(b)) {
     941        var constB = b as NumberTreeNode;
    938942        var constBValue = Math.Round(constB.Value);
    939943        if (constBValue == 1.0) {
     
    969973
    970974    private static ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    971       if (IsConstant(a) && IsConstant(b)) {
    972         var constA = a as ConstantTreeNode;
    973         var constB = b as ConstantTreeNode;
     975      if (IsNumber(a) && IsNumber(b)) {
     976        var constA = a as NumberTreeNode;
     977        var constB = b as NumberTreeNode;
    974978        return MakeConstant(Math.Pow(constA.Value, Math.Round(constB.Value)));
    975       } else if (IsFactor(a) && IsConstant(b)) {
     979      } else if (IsFactor(a) && IsNumber(b)) {
    976980        var factNode = a as FactorVariableTreeNode;
    977         var constNode = b as ConstantTreeNode;
     981        var constNode = b as NumberTreeNode;
    978982        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(w, Math.Round(constNode.Value))));
    979       } else if (IsBinFactor(a) && IsConstant(b)) {
     983      } else if (IsBinFactor(a) && IsNumber(b)) {
    980984        var binFactor = a as BinaryFactorVariableTreeNode;
    981         var constNode = b as ConstantTreeNode;
     985        var constNode = b as NumberTreeNode;
    982986        return MakeBinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Pow(binFactor.Weight, Math.Round(constNode.Value)));
    983       } else if (IsConstant(a) && IsFactor(b)) {
    984         var constNode = a as ConstantTreeNode;
     987      } else if (IsNumber(a) && IsFactor(b)) {
     988        var constNode = a as NumberTreeNode;
    985989        var factNode = b as FactorVariableTreeNode;
    986990        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(constNode.Value, Math.Round(w))));
    987       } else if (IsConstant(a) && IsBinFactor(b)) {
    988         var constNode = a as ConstantTreeNode;
     991      } else if (IsNumber(a) && IsBinFactor(b)) {
     992        var constNode = a as NumberTreeNode;
    989993        var factNode = b as BinaryFactorVariableTreeNode;
    990994        return MakeBinFactor(factNode.Symbol, factNode.VariableName, factNode.VariableValue, Math.Pow(constNode.Value, Math.Round(factNode.Weight)));
     
    993997        var node1 = b as FactorVariableTreeNode;
    994998        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => Math.Pow(u, Math.Round(v))));
    995       } else if (IsConstant(b)) {
    996         var constB = b as ConstantTreeNode;
     999      } else if (IsNumber(b)) {
     1000        var constB = b as NumberTreeNode;
    9971001        double exponent = Math.Round(constB.Value);
    9981002        if (exponent == 0.0) {
     
    10281032    // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
    10291033    private static ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    1030       if (IsConstant(a) && IsConstant(b)) {
     1034      if (IsNumber(a) && IsNumber(b)) {
    10311035        // fold constants
    1032         return MakeConstant(((ConstantTreeNode)a).Value / ((ConstantTreeNode)b).Value);
    1033       } else if ((IsConstant(a) && ((ConstantTreeNode)a).Value != 1.0)) {
     1036        return MakeConstant(((NumberTreeNode)a).Value / ((NumberTreeNode)b).Value);
     1037      } else if ((IsNumber(a) && ((NumberTreeNode)a).Value != 1.0)) {
    10341038        // a / x => (a * 1/a) / (x * 1/a) => 1 / (x * 1/a)
    10351039        return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
    1036       } else if (IsVariableBase(a) && IsConstant(b)) {
     1040      } else if (IsVariableBase(a) && IsNumber(b)) {
    10371041        // merge constant values into variable weights
    1038         var constB = ((ConstantTreeNode)b).Value;
     1042        var constB = ((NumberTreeNode)b).Value;
    10391043        ((VariableTreeNodeBase)a).Weight /= constB;
    10401044        return a;
    1041       } else if (IsFactor(a) && IsConstant(b)) {
     1045      } else if (IsFactor(a) && IsNumber(b)) {
    10421046        var factNode = a as FactorVariableTreeNode;
    1043         var constNode = b as ConstantTreeNode;
     1047        var constNode = b as NumberTreeNode;
    10441048        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => w / constNode.Value));
    1045       } else if (IsBinFactor(a) && IsConstant(b)) {
     1049      } else if (IsBinFactor(a) && IsNumber(b)) {
    10461050        var factNode = a as BinaryFactorVariableTreeNode;
    1047         var constNode = b as ConstantTreeNode;
     1051        var constNode = b as NumberTreeNode;
    10481052        return MakeBinFactor(factNode.Symbol, factNode.VariableName, factNode.VariableValue, factNode.Weight / constNode.Value);
    10491053      } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
     
    10701074        var bVar = b as VariableTreeNode;
    10711075        return MakeConstant(aVar.Weight / bVar.Weight);
    1072       } else if (IsAddition(a) && IsConstant(b)) {
     1076      } else if (IsAddition(a) && IsNumber(b)) {
    10731077        return a.Subtrees
    10741078          .Select(x => GetSimplifiedTree(x))
    10751079          .Select(x => MakeFraction(x, GetSimplifiedTree(b)))
    10761080          .Aggregate((c, d) => MakeSum(c, d));
    1077       } else if (IsMultiplication(a) && IsConstant(b)) {
     1081      } else if (IsMultiplication(a) && IsNumber(b)) {
    10781082        return MakeProduct(a, Invert(b));
    1079       } else if (IsDivision(a) && IsConstant(b)) {
     1083      } else if (IsDivision(a) && IsNumber(b)) {
    10801084        // (a1 / a2) / c => (a1 / (a2 * c))
    10811085        return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
     
    11001104
    11011105    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    1102       if (IsConstant(a) && IsConstant(b)) {
     1106      if (IsNumber(a) && IsNumber(b)) {
    11031107        // fold constants
    1104         ((ConstantTreeNode)a).Value += ((ConstantTreeNode)b).Value;
     1108        ((NumberTreeNode)a).Value += ((NumberTreeNode)b).Value;
    11051109        return a;
    1106       } else if (IsConstant(a)) {
     1110      } else if (IsNumber(a)) {
    11071111        // c + x => x + c
    11081112        // b is not constant => make sure constant is on the right
    11091113        return MakeSum(b, a);
    1110       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) {
     1114      } else if (IsNumber(b) && ((NumberTreeNode)b).Value == 0.0) {
    11111115        // x + 0 => x
    11121116        return a;
    1113       } else if (IsFactor(a) && IsConstant(b)) {
     1117      } else if (IsFactor(a) && IsNumber(b)) {
    11141118        var factNode = a as FactorVariableTreeNode;
    1115         var constNode = b as ConstantTreeNode;
     1119        var constNode = b as NumberTreeNode;
    11161120        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select((w) => w + constNode.Value));
    11171121      } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
     
    11381142        for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i));
    11391143        for (int i = 0; i < b.Subtrees.Count() - 1; i++) add.AddSubtree(b.GetSubtree(i));
    1140         if (IsConstant(a.Subtrees.Last()) && IsConstant(b.Subtrees.Last())) {
     1144        if (IsNumber(a.Subtrees.Last()) && IsNumber(b.Subtrees.Last())) {
    11411145          add.AddSubtree(MakeSum(a.Subtrees.Last(), b.Subtrees.Last()));
    1142         } else if (IsConstant(a.Subtrees.Last())) {
     1146        } else if (IsNumber(a.Subtrees.Last())) {
    11431147          add.AddSubtree(b.Subtrees.Last());
    11441148          add.AddSubtree(a.Subtrees.Last());
     
    11551159      } else if (IsAddition(b)) {
    11561160        return MakeSum(b, a);
    1157       } else if (IsAddition(a) && IsConstant(b)) {
     1161      } else if (IsAddition(a) && IsNumber(b)) {
    11581162        // a is an addition and b is a constant => append b to a and make sure the constants are merged
    11591163        var add = addSymbol.CreateTreeNode();
    11601164        // add all sub trees except for the last
    11611165        for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i));
    1162         if (IsConstant(a.Subtrees.Last()))
     1166        if (IsNumber(a.Subtrees.Last()))
    11631167          add.AddSubtree(MakeSum(a.Subtrees.Last(), b));
    11641168        else {
     
    12011205                            group node by GroupId(node) into g
    12021206                            select g;
    1203       var constant = (from node in subtrees.OfType<ConstantTreeNode>()
     1207      var constant = (from node in subtrees.OfType<NumberTreeNode>()
    12041208                      select node.Value).DefaultIfEmpty(0.0).Sum();
    1205       var unchangedSubtrees = subtrees.Where(t => t.SubtreeCount > 0 || !(t is IVariableTreeNode) && !(t is ConstantTreeNode));
     1209      var unchangedSubtrees = subtrees.Where(t => t.SubtreeCount > 0 || !(t is IVariableTreeNode) && !(t is NumberTreeNode));
    12061210
    12071211      foreach (var variableNodeGroup in groupedVarNodes) {
     
    12501254
    12511255    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    1252       if (IsConstant(a) && IsConstant(b)) {
     1256      if (IsNumber(a) && IsNumber(b)) {
    12531257        // fold constants
    1254         return MakeConstant(((ConstantTreeNode)a).Value * ((ConstantTreeNode)b).Value);
    1255       } else if (IsConstant(a)) {
     1258        return MakeConstant(((NumberTreeNode)a).Value * ((NumberTreeNode)b).Value);
     1259      } else if (IsNumber(a)) {
    12561260        // a * $ => $ * a
    12571261        return MakeProduct(b, a);
     
    12641268        var node1 = b as BinaryFactorVariableTreeNode;
    12651269        return MakeBinFactor(node0.Symbol, node0.VariableName, node0.VariableValue, node0.Weight * node1.Weight);
    1266       } else if (IsFactor(a) && IsConstant(b)) {
     1270      } else if (IsFactor(a) && IsNumber(b)) {
    12671271        var node0 = a as FactorVariableTreeNode;
    1268         var node1 = b as ConstantTreeNode;
     1272        var node1 = b as NumberTreeNode;
    12691273        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Select(w => w * node1.Value));
    1270       } else if (IsBinFactor(a) && IsConstant(b)) {
     1274      } else if (IsBinFactor(a) && IsNumber(b)) {
    12711275        var node0 = a as BinaryFactorVariableTreeNode;
    1272         var node1 = b as ConstantTreeNode;
     1276        var node1 = b as NumberTreeNode;
    12731277        return MakeBinFactor(node0.Symbol, node0.VariableName, node0.VariableValue, node0.Weight * node1.Value);
    12741278      } else if (IsBinFactor(a) && IsFactor(b)) {
     
    12821286        if (wi < 0) throw new ArgumentException();
    12831287        return MakeBinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]);
    1284       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 1.0) {
     1288      } else if (IsNumber(b) && ((NumberTreeNode)b).Value == 1.0) {
    12851289        // $ * 1.0 => $
    12861290        return a;
    1287       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) {
     1291      } else if (IsNumber(b) && ((NumberTreeNode)b).Value == 0.0) {
    12881292        return MakeConstant(0);
    1289       } else if (IsConstant(b) && IsVariableBase(a)) {
     1293      } else if (IsNumber(b) && IsVariableBase(a)) {
    12901294        // multiply constants into variables weights
    1291         ((VariableTreeNodeBase)a).Weight *= ((ConstantTreeNode)b).Value;
     1295        ((VariableTreeNodeBase)a).Weight *= ((NumberTreeNode)b).Value;
    12921296        return a;
    1293       } else if (IsConstant(b) && IsAddition(a) ||
     1297      } else if (IsNumber(b) && IsAddition(a) ||
    12941298          IsFactor(b) && IsAddition(a) ||
    12951299          IsBinFactor(b) && IsAddition(a)) {
     
    13211325      } else if (IsAbsolute(a) && IsAbsolute(b)) {
    13221326        return MakeAbs(MakeProduct(a.GetSubtree(0), b.GetSubtree(0)));
    1323       } else if (IsAbsolute(a) && IsConstant(b)) {
    1324         var constNode = b as ConstantTreeNode;
     1327      } else if (IsAbsolute(a) && IsNumber(b)) {
     1328        var constNode = b as NumberTreeNode;
    13251329        var posF = Math.Abs(constNode.Value);
    13261330        if (constNode.Value > 0) {
     
    13911395      var constantProduct = (from node in subtrees.OfType<VariableTreeNodeBase>()
    13921396                             select node.Weight)
    1393         .Concat(from node in subtrees.OfType<ConstantTreeNode>()
     1397        .Concat(from node in subtrees.OfType<NumberTreeNode>()
    13941398                select node.Value)
    13951399        .DefaultIfEmpty(1.0)
     
    13971401
    13981402      var unchangedSubtrees = from tree in subtrees
    1399                               where tree.SubtreeCount > 0 || !(tree is IVariableTreeNode) && !(tree is ConstantTreeNode)
     1403                              where tree.SubtreeCount > 0 || !(tree is IVariableTreeNode) && !(tree is NumberTreeNode)
    14001404                              select tree;
    14011405
     
    14461450    /// <returns>-x</returns>
    14471451    private static ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
    1448       if (IsConstant(x)) {
    1449         ((ConstantTreeNode)x).Value *= -1;
     1452      if (IsNumber(x)) {
     1453        ((NumberTreeNode)x).Value *= -1;
    14501454      } else if (IsVariableBase(x)) {
    14511455        var variableTree = (VariableTreeNodeBase)x;
     
    14831487    /// <returns></returns>
    14841488    private static ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
    1485       if (IsConstant(x)) {
    1486         return MakeConstant(1.0 / ((ConstantTreeNode)x).Value);
     1489      if (IsNumber(x)) {
     1490        return MakeConstant(1.0 / ((NumberTreeNode)x).Value);
    14871491      } else if (IsFactor(x)) {
    14881492        var factorNode = (FactorVariableTreeNode)x;
     
    14971501
    14981502    private static ISymbolicExpressionTreeNode MakeConstant(double value) {
    1499       ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
    1500       constantTreeNode.Value = value;
    1501       return constantTreeNode;
     1503      NumberTreeNode numberTreeNode = (NumberTreeNode)(numberSymbol.CreateTreeNode());
     1504      numberTreeNode.Value = value;
     1505      return numberTreeNode;
    15021506    }
    15031507
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs

    r18093 r18113  
    3636    private static readonly Sine sin = new Sine();
    3737    private static readonly Cosine cos = new Cosine();
    38     private static readonly Num Num = new Num();
     38    private static readonly Number number = new Number();
    3939
    4040    private static ISymbolicExpressionTreeNode ActualRoot(this ISymbolicExpressionTree tree) => tree.Root.GetSubtree(0).GetSubtree(0);
     
    6666      var symbol = node.Symbol;
    6767      var name = symbol.Name;
    68       if (node is NumTreeNode constantNode) {
     68      if (node is NumberTreeNode constantNode) {
    6969        name = strict ? constantNode.Value.ToString() : symbol.Name;
    7070      } else if (node is VariableTreeNode variableNode) {
     
    222222            variableTreeNode.VariableName = variable.VariableName;
    223223            variableTreeNode.Weight = variable.Weight;
    224           } else if (node.Data is NumTreeNode @const) {
    225             var constantTreeNode = (NumTreeNode)treeNodes[i];
     224          } else if (node.Data is NumberTreeNode @const) {
     225            var constantTreeNode = (NumberTreeNode)treeNodes[i];
    226226            constantTreeNode.Value = @const.Value;
    227227          }
     
    285285
    286286        var symbol = child.Data.Symbol;
    287         if (child.Data is NumTreeNode firstConst) {
     287        if (child.Data is NumberTreeNode firstConst) {
    288288          // fold sibling constant nodes into the first constant
    289289          for (int k = j + 1; k < children.Length; ++k) {
    290290            var sibling = nodes[children[k]];
    291             if (sibling.Data is NumTreeNode otherConst) {
     291            if (sibling.Data is NumberTreeNode otherConst) {
    292292              sibling.Enabled = false;
    293293              node.Arity--;
     
    301301          for (int k = j + 1; k < children.Length; ++k) {
    302302            var sibling = nodes[children[k]];
    303             if (sibling.Data is NumTreeNode constantNode) {
     303            if (sibling.Data is NumberTreeNode constantNode) {
    304304              sibling.Enabled = false;
    305305              node.Arity--;
     
    329329
    330330        if (node.Arity == 0) { // if everything is simplified this node becomes constant
    331           var constantTreeNode = Num.CreateTreeNode<NumTreeNode>();
     331          var constantTreeNode = number.CreateTreeNode<NumberTreeNode>();
    332332          constantTreeNode.Value = 1;
    333333          nodes[i] = constantTreeNode.ToHashNode();
     
    344344      var tmp = nodes;
    345345
    346       if (children.All(x => tmp[x].Data.Symbol is Num)) {
    347         var v = ((NumTreeNode)nodes[children.First()].Data).Value;
     346      if (children.All(x => tmp[x].Data.Symbol is Number)) {
     347        var v = ((NumberTreeNode)nodes[children.First()].Data).Value;
    348348        if (node.Arity == 1) {
    349349          v = 1 / v;
    350350        } else if (node.Arity > 1) {
    351351          foreach (var j in children.Skip(1)) {
    352             v /= ((NumTreeNode)nodes[j].Data).Value;
     352            v /= ((NumberTreeNode)nodes[j].Data).Value;
    353353          }
    354354        }
    355         var constantTreeNode = Num.CreateTreeNode<NumTreeNode>();
     355        var constantTreeNode = number.CreateTreeNode<NumberTreeNode>();
    356356        constantTreeNode.Value = v;
    357357        nodes[i] = constantTreeNode.ToHashNode();
     
    368368        }
    369369        if (node.Arity == 0) {
    370           var constantTreeNode = Num.CreateTreeNode<NumTreeNode>();
     370          var constantTreeNode = number.CreateTreeNode<NumberTreeNode>();
    371371          constantTreeNode.Value = 1; // x / x = 1
    372372          nodes[i] = constantTreeNode.ToHashNode();
     
    383383      var childSymbol = child.Data.Symbol;
    384384
    385       if (childSymbol is Num) {
     385      if (childSymbol is Number) {
    386386        nodes[i].Enabled = false;
    387387      } else if ((parentSymbol is Exponential && childSymbol is Logarithm) || (parentSymbol is Logarithm && childSymbol is Exponential)) {
     
    393393      var children = nodes.IterateChildren(i);
    394394      var tmp = nodes;
    395       if (children.All(x => tmp[x].Data.Symbol is Num)) {
     395      if (children.All(x => tmp[x].Data.Symbol is Number)) {
    396396        foreach (var j in children) {
    397397          nodes[j].Enabled = false;
    398398        }
    399         nodes[i] = Num.CreateTreeNode().ToHashNode();
     399        nodes[i] = number.CreateTreeNode().ToHashNode();
    400400      }
    401401    }
  • branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs

    r18093 r18113  
    115115      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) return;
    116116
    117       NumTreeNode alphaTreeNode = null;
    118       NumTreeNode betaTreeNode = null;
     117      NumberTreeNode alphaTreeNode = null;
     118      NumberTreeNode betaTreeNode = null;
    119119      // check if model has a structure that can be re-used for scaling
    120120      var startNode = SymbolicExpressionTree.Root.GetSubtree(0);
    121121      var addNode = startNode.GetSubtree(0);
    122122      if (addNode.Symbol is Addition && addNode.SubtreeCount == 2) {
    123         alphaTreeNode = (NumTreeNode)addNode.Subtrees.LastOrDefault(n => n is NumTreeNode);
     123        alphaTreeNode = (NumberTreeNode)addNode.Subtrees.LastOrDefault(n => n is NumberTreeNode);
    124124        var mulNode = addNode.Subtrees.FirstOrDefault(n => n.Symbol is Multiplication);
    125125        if (mulNode != null) {
    126           betaTreeNode = (NumTreeNode)mulNode.Subtrees.LastOrDefault(n => n is NumTreeNode);
     126          betaTreeNode = (NumberTreeNode)mulNode.Subtrees.LastOrDefault(n => n is NumberTreeNode);
    127127        }
    128128      }
     
    146146        var addition = new Addition();
    147147        var node = addition.CreateTreeNode();
    148         var alphaConst = MakeConstant(alpha);
     148        var alphaConst = MakeNumber(alpha);
    149149        node.AddSubtree(treeNode);
    150150        node.AddSubtree(alphaConst);
     
    157157        return treeNode;
    158158      } else {
    159         var multipliciation = new Multiplication();
    160         var node = multipliciation.CreateTreeNode();
    161         var betaConst = MakeConstant(beta);
     159        var multiplication = new Multiplication();
     160        var node = multiplication.CreateTreeNode();
     161        var betaConst = MakeNumber(beta);
    162162        node.AddSubtree(treeNode);
    163163        node.AddSubtree(betaConst);
     
    166166    }
    167167
    168     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    169       var node = (NumTreeNode)(new Num()).CreateTreeNode();
     168    private static ISymbolicExpressionTreeNode MakeNumber(double c) {
     169      var node = (NumberTreeNode)(new Number()).CreateTreeNode();
    170170      node.Value = c;
    171171      return node;
Note: See TracChangeset for help on using the changeset viewer.