Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/15/12 16:47:25 (12 years ago)
Author:
mkommend
Message:

#1763: merged changes from trunk into the tree simplifier branch.

Location:
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
21 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r8388 r8915  
    9393  </PropertyGroup>
    9494  <ItemGroup>
    95     <Reference Include="ALGLIB-3.5.0">
    96       <HintPath>..\..\..\..\Trunk\sources\bin\ALGLIB-3.5.0.dll</HintPath>
     95    <Reference Include="ALGLIB-3.6.0, Version=3.6.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     96      <SpecificVersion>False</SpecificVersion>
     97      <HintPath>..\..\..\..\trunk\sources\bin\ALGLIB-3.6.0.dll</HintPath>
     98      <Private>False</Private>
     99    </Reference>
     100    <Reference Include="AutoDiff-1.0, Version=1.0.0.14388, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     101      <HintPath>..\..\bin\AutoDiff-1.0.dll</HintPath>
     102      <Private>False</Private>
    97103    </Reference>
    98104    <Reference Include="HeuristicLab.Analysis-3.3">
    99105      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath>
     106      <Private>False</Private>
    100107    </Reference>
    101108    <Reference Include="HeuristicLab.Collections-3.3">
    102109      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath>
     110      <Private>False</Private>
    103111    </Reference>
    104112    <Reference Include="HeuristicLab.Common-3.3">
    105113      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath>
     114      <Private>False</Private>
    106115    </Reference>
    107116    <Reference Include="HeuristicLab.Core-3.3">
    108117      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
     118      <Private>False</Private>
    109119    </Reference>
    110120    <Reference Include="HeuristicLab.Data-3.3">
    111121      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath>
     122      <Private>False</Private>
    112123    </Reference>
    113124    <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4">
    114125      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath>
     126      <Private>False</Private>
    115127    </Reference>
    116128    <Reference Include="HeuristicLab.Operators-3.3">
    117129      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath>
     130      <Private>False</Private>
    118131    </Reference>
    119132    <Reference Include="HeuristicLab.Optimization-3.3">
    120133      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
     134      <Private>False</Private>
    121135    </Reference>
    122136    <Reference Include="HeuristicLab.Parameters-3.3">
    123137      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath>
     138      <Private>False</Private>
    124139    </Reference>
    125140    <Reference Include="HeuristicLab.Persistence-3.3">
    126141      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath>
     142      <Private>False</Private>
    127143    </Reference>
    128144    <Reference Include="HeuristicLab.PluginInfrastructure-3.3">
    129145      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
     146      <Private>False</Private>
    130147    </Reference>
    131148    <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4">
    132149      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath>
     150      <Private>False</Private>
    133151    </Reference>
    134152    <Reference Include="HeuristicLab.Problems.Instances-3.3">
    135153      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath>
     154      <Private>False</Private>
    136155    </Reference>
    137156    <Reference Include="System" />
     
    152171    <Compile Include="MultiObjective\SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs" />
    153172    <Compile Include="Plugin.cs" />
     173    <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" />
    154174    <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
    155175    <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs" />
     
    176196    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" />
    177197    <Compile Include="SymbolicRegressionSolution.cs" />
    178     <Compile Include="SymbolicRegressionSolutionValuesCalculator.cs" />
    179198    <None Include="HeuristicLab.snk" />
    180199    <None Include="Plugin.cs.frame" />
     
    215234  -->
    216235  <PropertyGroup>
    217     <PreBuildEvent>set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     236    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    218237set ProjectDir=$(ProjectDir)
    219238set SolutionDir=$(SolutionDir)
     
    222241call PreBuildEvent.cmd
    223242</PreBuildEvent>
     243    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     244export ProjectDir=$(ProjectDir)
     245export SolutionDir=$(SolutionDir)
     246
     247$SolutionDir/PreBuildEvent.sh
     248</PreBuildEvent>
    224249  </PropertyGroup>
    225250</Project>
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Interfaces/ISymbolicRegressionModel.cs

    r7259 r8915  
    2020#endregion
    2121
    22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2322namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2423  public interface ISymbolicRegressionModel : IRegressionModel, ISymbolicDataAnalysisModel {
     24    double LowerEstimationLimit { get; }
     25    double UpperEstimationLimit { get; }
    2526  }
    2627}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs

    r7259 r8915  
    4747      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4848      var solution = SymbolicExpressionTreeParameter.ActualValue;
    49       double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     49      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5050      QualitiesParameter.ActualValue = new DoubleArray(qualities);
    5151      return base.Apply();
    5252    }
    5353
    54     public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows) {
     54    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5555      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    56       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    57       IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     56      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5857      OnlineCalculatorError errorState;
    59       double mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimationValues, out errorState);
     58
     59      double mse;
     60      if (applyLinearScaling) {
     61        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
     62        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
     63        errorState = mseCalculator.ErrorState;
     64        mse = mseCalculator.MeanSquaredError;
     65      } else {
     66        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     67        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     68      }
    6069      if (errorState != OnlineCalculatorError.None) mse = double.NaN;
    6170      return new double[2] { mse, solution.Length };
     
    6574      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6675      EstimationLimitsParameter.ExecutionContext = context;
     76      ApplyLinearScalingParameter.ExecutionContext = context;
    6777
    68       double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     78      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    6979
    7080      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7181      EstimationLimitsParameter.ExecutionContext = null;
     82      ApplyLinearScalingParameter.ExecutionContext = null;
    7283
    7384      return quality;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs

    r7259 r8915  
    4747      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4848      var solution = SymbolicExpressionTreeParameter.ActualValue;
    49       double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     49      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5050      QualitiesParameter.ActualValue = new DoubleArray(qualities);
    5151      return base.Apply();
    5252    }
    5353
    54     public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows) {
     54    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5555      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    56       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     56      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5757      OnlineCalculatorError errorState;
    58       double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
    59       if (errorState != OnlineCalculatorError.None) r2 = 0.0;
    60       return new double[] { r2, solution.Length };
     58
     59      double r2;
     60      if (applyLinearScaling) {
     61        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
     62        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows);
     63        errorState = r2Calculator.ErrorState;
     64        r2 = r2Calculator.RSquared;
     65      } else {
     66        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     67        r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     68      }
     69
     70      if (errorState != OnlineCalculatorError.None) r2 = double.NaN;
     71      return new double[2] { r2, solution.Length };
    6172    }
    6273
     
    6475      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6576      EstimationLimitsParameter.ExecutionContext = context;
     77      ApplyLinearScalingParameter.ExecutionContext = context;
    6678
    67       double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     79      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    6880
    6981      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7082      EstimationLimitsParameter.ExecutionContext = null;
     83      ApplyLinearScalingParameter.ExecutionContext = null;
    7184
    7285      return quality;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveProblem.cs

    r8175 r8915  
    6565      EstimationLimitsParameter.Hidden = true;
    6666
     67      ApplyLinearScalingParameter.Value.Value = true;
    6768      Maximization = new BoolArray(new bool[] { true, false });
    6869      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3837    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    3938    private const string EstimationLimitsParameterName = "EstimationLimits";
    40     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4139    #region parameter properties
    4240    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
     
    4846    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    4947      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    50     }
    51     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    52       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    53     }
    54     #endregion
    55 
    56     #region properties
    57     public BoolValue ApplyLinearScaling {
    58       get { return ApplyLinearScalingParameter.Value; }
    5948    }
    6049    #endregion
     
    6857      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    6958      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
    70       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    7159    }
    7260
     
    7765    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    7866      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    79       if (ApplyLinearScaling.Value)
    80         SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue);
     67      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    8168      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    8269    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3635    ISymbolicDataAnalysisBoundedOperator {
    3736    private const string EstimationLimitsParameterName = "EstimationLimits";
    38     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3937
    4038    #region parameter properties
     
    4240      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    4341    }
    44     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    45       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    46     }
    4742    #endregion
    4843
    49     #region properties
    50     public BoolValue ApplyLinearScaling {
    51       get { return ApplyLinearScalingParameter.Value; }
    52     }
    53     #endregion
    5444    [StorableConstructor]
    5545    private SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    5848      : base() {
    5949      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
    60       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    6150    }
    6251    public override IDeepCloneable Clone(Cloner cloner) {
     
    6655    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    6756      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    68       if (ApplyLinearScaling.Value)
    69         SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue);
     57      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    7058      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    7159    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Plugin.cs.frame

    r8246 r8915  
    2828  [Plugin("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression","Provides classes to perform symbolic regression (single- or multiobjective).", "3.4.3.$WCREV$")]
    2929  [PluginFile("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.dll", PluginFileType.Assembly)]
    30   [PluginDependency("HeuristicLab.ALGLIB", "3.5")]
     30  [PluginDependency("HeuristicLab.ALGLIB", "3.6.0")]
     31  [PluginDependency("HeuristicLab.AutoDiff", "1.0")]
    3132  [PluginDependency("HeuristicLab.Analysis", "3.3")]
    3233  [PluginDependency("HeuristicLab.Common", "3.3")]
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r8053 r8915  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     25using AutoDiff;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
     
    3739    private const string ConstantOptimizationProbabilityParameterName = "ConstantOptimizationProbability";
    3840    private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage";
     41    private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree";
    3942
    4043    private const string EvaluatedTreesResultName = "EvaluatedTrees";
     
    6063      get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationRowsPercentageParameterName]; }
    6164    }
     65    public IFixedValueParameter<BoolValue> UpdateConstantsInTreeParameter {
     66      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateConstantsInTreeParameterName]; }
     67    }
    6268
    6369    public IntValue ConstantOptimizationIterations {
     
    7278    public PercentValue ConstantOptimizationRowsPercentage {
    7379      get { return ConstantOptimizationRowsPercentageParameter.Value; }
     80    }
     81    public bool UpdateConstantsInTree {
     82      get { return UpdateConstantsInTreeParameter.Value.Value; }
     83      set { UpdateConstantsInTreeParameter.Value.Value = value; }
    7484    }
    7585
     
    8999      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1), true));
    90100      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1), true));
     101      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)));
    91102
    92103      Parameters.Add(new LookupParameter<IntValue>(EvaluatedTreesResultName));
     
    98109    }
    99110
     111    [StorableHook(HookType.AfterDeserialization)]
     112    private void AfterDeserialization() {
     113      if (!Parameters.ContainsKey(UpdateConstantsInTreeParameterName))
     114        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)));
     115    }
     116
    100117    public override IOperation Apply() {
    101118      AddResults();
    102       int seed = RandomParameter.ActualValue.Next();
    103119      var solution = SymbolicExpressionTreeParameter.ActualValue;
    104120      double quality;
     
    106122        IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
    107123        quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
    108            constantOptimizationRows, ConstantOptimizationImprovement.Value, ConstantOptimizationIterations.Value, 0.001,
    109            EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower,
     124           constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value,
     125           EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower, UpdateConstantsInTree,
    110126          EvaluatedTreesParameter.ActualValue, EvaluatedTreeNodesParameter.ActualValue);
    111127        if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
    112128          var evaluationRows = GenerateRowsToEvaluate();
    113           quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows);
     129          quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
    114130        }
    115131      } else {
    116132        var evaluationRows = GenerateRowsToEvaluate();
    117         quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows);
     133        quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
    118134      }
    119135      QualityParameter.ActualValue = new DoubleValue(quality);
    120       EvaluatedTreesParameter.ActualValue.Value += 1;
    121       EvaluatedTreeNodesParameter.ActualValue.Value += solution.Length;
     136      lock (locker) {
     137        EvaluatedTreesParameter.ActualValue.Value += 1;
     138        EvaluatedTreeNodesParameter.ActualValue.Value += solution.Length;
     139      }
    122140
    123141      if (Successor != null)
     
    127145    }
    128146
     147    private object locker = new object();
    129148    private void AddResults() {
    130       if (EvaluatedTreesParameter.ActualValue == null) {
    131         var scope = ExecutionContext.Scope;
    132         while (scope.Parent != null)
    133           scope = scope.Parent;
    134         scope.Variables.Add(new Core.Variable(EvaluatedTreesResultName, new IntValue()));
    135       }
    136       if (EvaluatedTreeNodesParameter.ActualValue == null) {
    137         var scope = ExecutionContext.Scope;
    138         while (scope.Parent != null)
    139           scope = scope.Parent;
    140         scope.Variables.Add(new Core.Variable(EvaluatedTreeNodesResultName, new IntValue()));
     149      lock (locker) {
     150        if (EvaluatedTreesParameter.ActualValue == null) {
     151          var scope = ExecutionContext.Scope;
     152          while (scope.Parent != null)
     153            scope = scope.Parent;
     154          scope.Variables.Add(new Core.Variable(EvaluatedTreesResultName, new IntValue()));
     155        }
     156        if (EvaluatedTreeNodesParameter.ActualValue == null) {
     157          var scope = ExecutionContext.Scope;
     158          while (scope.Parent != null)
     159            scope = scope.Parent;
     160          scope.Variables.Add(new Core.Variable(EvaluatedTreeNodesResultName, new IntValue()));
     161        }
    141162      }
    142163    }
     
    145166      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    146167      EstimationLimitsParameter.ExecutionContext = context;
    147 
    148       double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     168      ApplyLinearScalingParameter.ExecutionContext = context;
     169
     170      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    149171
    150172      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    151173      EstimationLimitsParameter.ExecutionContext = null;
     174      ApplyLinearScalingParameter.ExecutionContext = context;
    152175
    153176      return r2;
    154177    }
    155178
     179    #region derivations of functions
     180    // create function factory for arctangent
     181    private readonly Func<Term, UnaryFunc> arctan = UnaryFunc.Factory(
     182      eval: Math.Atan,
     183      diff: x => 1 / (1 + x * x));
     184    private static readonly Func<Term, UnaryFunc> sin = UnaryFunc.Factory(
     185      eval: Math.Sin,
     186      diff: Math.Cos);
     187    private static readonly Func<Term, UnaryFunc> cos = UnaryFunc.Factory(
     188       eval: Math.Cos,
     189       diff: x => -Math.Sin(x));
     190    private static readonly Func<Term, UnaryFunc> tan = UnaryFunc.Factory(
     191      eval: Math.Tan,
     192      diff: x => 1 + Math.Tan(x) * Math.Tan(x));
     193    private static readonly Func<Term, UnaryFunc> square = UnaryFunc.Factory(
     194       eval: x => x * x,
     195       diff: x => 2 * x);
     196    private static readonly Func<Term, UnaryFunc> erf = UnaryFunc.Factory(
     197      eval: alglib.errorfunction,
     198      diff: x => 2.0 * Math.Exp(-(x * x)) / Math.Sqrt(Math.PI));
     199    private static readonly Func<Term, UnaryFunc> norm = UnaryFunc.Factory(
     200      eval: alglib.normaldistribution,
     201      diff: x => -(Math.Exp(-(x * x)) * Math.Sqrt(Math.Exp(x * x)) * x) / Math.Sqrt(2 * Math.PI));
     202    #endregion
     203
     204
    156205    public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData,
    157       IEnumerable<int> rows, double improvement, int iterations, double differentialStep, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {
     206      IEnumerable<int> rows, bool applyLinearScaling, int maxIterations, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, bool updateConstantsInTree = true, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {
     207
     208      List<AutoDiff.Variable> variables = new List<AutoDiff.Variable>();
     209      List<AutoDiff.Variable> parameters = new List<AutoDiff.Variable>();
     210      List<string> variableNames = new List<string>();
     211
     212      AutoDiff.Term func;
     213      if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, out func))
     214        throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
     215      if (variableNames.Count == 0) return 0.0;
     216
     217      AutoDiff.IParametricCompiledTerm compiledFunc = AutoDiff.TermUtils.Compile(func, variables.ToArray(), parameters.ToArray());
     218
    158219      List<SymbolicExpressionTreeTerminalNode> terminalNodes = tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>().ToList();
    159       double[] c = new double[terminalNodes.Count];
    160       int treeLength = tree.Length;
    161 
    162       //extract inital constants
    163       for (int i = 0; i < terminalNodes.Count; i++) {
    164         ConstantTreeNode constantTreeNode = terminalNodes[i] as ConstantTreeNode;
    165         if (constantTreeNode != null) c[i] = constantTreeNode.Value;
    166         VariableTreeNode variableTreeNode = terminalNodes[i] as VariableTreeNode;
    167         if (variableTreeNode != null) c[i] = variableTreeNode.Weight;
    168       }
    169 
    170       double epsg = 0;
    171       double epsf = improvement;
    172       double epsx = 0;
    173       int maxits = iterations;
    174       double diffstep = differentialStep;
    175 
    176       alglib.minlmstate state;
    177       alglib.minlmreport report;
    178 
    179       alglib.minlmcreatev(1, c, diffstep, out state);
    180       alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);
    181       alglib.minlmoptimize(state, CreateCallBack(interpreter, tree, problemData, rows, upperEstimationLimit, lowerEstimationLimit, treeLength, evaluatedTrees, evaluatedTreeNodes), null, terminalNodes);
    182       alglib.minlmresults(state, out c, out report);
    183 
    184       for (int i = 0; i < c.Length; i++) {
    185         ConstantTreeNode constantTreeNode = terminalNodes[i] as ConstantTreeNode;
    186         if (constantTreeNode != null) constantTreeNode.Value = c[i];
    187         VariableTreeNode variableTreeNode = terminalNodes[i] as VariableTreeNode;
    188         if (variableTreeNode != null) variableTreeNode.Weight = c[i];
    189       }
    190 
    191       return (state.fi[0] - 1) * -1;
    192     }
    193 
    194     private static alglib.ndimensional_fvec CreateCallBack(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows, double upperEstimationLimit, double lowerEstimationLimit, int treeLength, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {
    195       return (double[] arg, double[] fi, object obj) => {
    196         // update constants of tree
    197         List<SymbolicExpressionTreeTerminalNode> terminalNodes = (List<SymbolicExpressionTreeTerminalNode>)obj;
    198         for (int i = 0; i < terminalNodes.Count; i++) {
    199           ConstantTreeNode constantTreeNode = terminalNodes[i] as ConstantTreeNode;
    200           if (constantTreeNode != null) constantTreeNode.Value = arg[i];
    201           VariableTreeNode variableTreeNode = terminalNodes[i] as VariableTreeNode;
    202           if (variableTreeNode != null) variableTreeNode.Weight = arg[i];
    203         }
    204 
    205         double quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows);
    206 
    207         fi[0] = 1 - quality;
    208         if (evaluatedTrees != null) evaluatedTrees.Value++;
    209         if (evaluatedTreeNodes != null) evaluatedTreeNodes.Value += treeLength;
     220      double[] c = new double[variables.Count];
     221
     222      {
     223        c[0] = 0.0;
     224        c[1] = 1.0;
     225        //extract inital constants
     226        int i = 2;
     227        foreach (var node in terminalNodes) {
     228          ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     229          VariableTreeNode variableTreeNode = node as VariableTreeNode;
     230          if (constantTreeNode != null)
     231            c[i++] = constantTreeNode.Value;
     232          else if (variableTreeNode != null)
     233            c[i++] = variableTreeNode.Weight;
     234        }
     235      }
     236
     237      alglib.lsfitstate state;
     238      alglib.lsfitreport rep;
     239      int info;
     240
     241      Dataset ds = problemData.Dataset;
     242      double[,] x = new double[rows.Count(), variableNames.Count];
     243      int row = 0;
     244      foreach (var r in rows) {
     245        for (int col = 0; col < variableNames.Count; col++) {
     246          x[row, col] = ds.GetDoubleValue(variableNames[col], r);
     247        }
     248        row++;
     249      }
     250      double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
     251      int n = x.GetLength(0);
     252      int m = x.GetLength(1);
     253      int k = c.Length;
     254
     255      alglib.ndimensional_pfunc function_cx_1_func = CreatePFunc(compiledFunc);
     256      alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(compiledFunc);
     257
     258      try {
     259        alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
     260        alglib.lsfitsetcond(state, 0, 0, maxIterations);
     261        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
     262        alglib.lsfitresults(state, out info, out c, out rep);
     263
     264      }
     265      catch (ArithmeticException) {
     266        return 0.0;
     267      }
     268      catch (alglib.alglibexception) {
     269        return 0.0;
     270      }
     271      var newTree = tree;
     272      if (!updateConstantsInTree) newTree = (ISymbolicExpressionTree)tree.Clone();
     273      {
     274        // only when no error occurred
     275        // set constants in tree
     276        int i = 2;
     277        foreach (var node in newTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
     278          ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     279          VariableTreeNode variableTreeNode = node as VariableTreeNode;
     280          if (constantTreeNode != null)
     281            constantTreeNode.Value = c[i++];
     282          else if (variableTreeNode != null)
     283            variableTreeNode.Weight = c[i++];
     284        }
     285
     286      }
     287      return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, newTree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
     288    }
     289
     290    private static alglib.ndimensional_pfunc CreatePFunc(AutoDiff.IParametricCompiledTerm compiledFunc) {
     291      return (double[] c, double[] x, ref double func, object o) => {
     292        func = compiledFunc.Evaluate(c, x);
    210293      };
    211294    }
    212295
     296    private static alglib.ndimensional_pgrad CreatePGrad(AutoDiff.IParametricCompiledTerm compiledFunc) {
     297      return (double[] c, double[] x, ref double func, double[] grad, object o) => {
     298        var tupel = compiledFunc.Differentiate(c, x);
     299        func = tupel.Item2;
     300        Array.Copy(tupel.Item1, grad, grad.Length);
     301      };
     302    }
     303
     304    private static bool TryTransformToAutoDiff(ISymbolicExpressionTreeNode node, List<AutoDiff.Variable> variables, List<AutoDiff.Variable> parameters, List<string> variableNames, out AutoDiff.Term term) {
     305      if (node.Symbol is Constant) {
     306        var var = new AutoDiff.Variable();
     307        variables.Add(var);
     308        term = var;
     309        return true;
     310      }
     311      if (node.Symbol is Variable) {
     312        var varNode = node as VariableTreeNode;
     313        var par = new AutoDiff.Variable();
     314        parameters.Add(par);
     315        variableNames.Add(varNode.VariableName);
     316        var w = new AutoDiff.Variable();
     317        variables.Add(w);
     318        term = AutoDiff.TermBuilder.Product(w, par);
     319        return true;
     320      }
     321      if (node.Symbol is Addition) {
     322        List<AutoDiff.Term> terms = new List<Term>();
     323        foreach (var subTree in node.Subtrees) {
     324          AutoDiff.Term t;
     325          if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, out t)) {
     326            term = null;
     327            return false;
     328          }
     329          terms.Add(t);
     330        }
     331        term = AutoDiff.TermBuilder.Sum(terms);
     332        return true;
     333      }
     334      if (node.Symbol is Subtraction) {
     335        List<AutoDiff.Term> terms = new List<Term>();
     336        for (int i = 0; i < node.SubtreeCount; i++) {
     337          AutoDiff.Term t;
     338          if (!TryTransformToAutoDiff(node.GetSubtree(i), variables, parameters, variableNames, out t)) {
     339            term = null;
     340            return false;
     341          }
     342          if (i > 0) t = -t;
     343          terms.Add(t);
     344        }
     345        term = AutoDiff.TermBuilder.Sum(terms);
     346        return true;
     347      }
     348      if (node.Symbol is Multiplication) {
     349        AutoDiff.Term a, b;
     350        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out a) ||
     351          !TryTransformToAutoDiff(node.GetSubtree(1), variables, parameters, variableNames, out b)) {
     352          term = null;
     353          return false;
     354        } else {
     355          List<AutoDiff.Term> factors = new List<Term>();
     356          foreach (var subTree in node.Subtrees.Skip(2)) {
     357            AutoDiff.Term f;
     358            if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, out f)) {
     359              term = null;
     360              return false;
     361            }
     362            factors.Add(f);
     363          }
     364          term = AutoDiff.TermBuilder.Product(a, b, factors.ToArray());
     365          return true;
     366        }
     367      }
     368      if (node.Symbol is Division) {
     369        // only works for at least two subtrees
     370        AutoDiff.Term a, b;
     371        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out a) ||
     372          !TryTransformToAutoDiff(node.GetSubtree(1), variables, parameters, variableNames, out b)) {
     373          term = null;
     374          return false;
     375        } else {
     376          List<AutoDiff.Term> factors = new List<Term>();
     377          foreach (var subTree in node.Subtrees.Skip(2)) {
     378            AutoDiff.Term f;
     379            if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, out f)) {
     380              term = null;
     381              return false;
     382            }
     383            factors.Add(1.0 / f);
     384          }
     385          term = AutoDiff.TermBuilder.Product(a, 1.0 / b, factors.ToArray());
     386          return true;
     387        }
     388      }
     389      if (node.Symbol is Logarithm) {
     390        AutoDiff.Term t;
     391        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     392          term = null;
     393          return false;
     394        } else {
     395          term = AutoDiff.TermBuilder.Log(t);
     396          return true;
     397        }
     398      }
     399      if (node.Symbol is Exponential) {
     400        AutoDiff.Term t;
     401        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     402          term = null;
     403          return false;
     404        } else {
     405          term = AutoDiff.TermBuilder.Exp(t);
     406          return true;
     407        }
     408      } if (node.Symbol is Sine) {
     409        AutoDiff.Term t;
     410        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     411          term = null;
     412          return false;
     413        } else {
     414          term = sin(t);
     415          return true;
     416        }
     417      } if (node.Symbol is Cosine) {
     418        AutoDiff.Term t;
     419        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     420          term = null;
     421          return false;
     422        } else {
     423          term = cos(t);
     424          return true;
     425        }
     426      } if (node.Symbol is Tangent) {
     427        AutoDiff.Term t;
     428        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     429          term = null;
     430          return false;
     431        } else {
     432          term = tan(t);
     433          return true;
     434        }
     435      }
     436      if (node.Symbol is Square) {
     437        AutoDiff.Term t;
     438        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     439          term = null;
     440          return false;
     441        } else {
     442          term = square(t);
     443          return true;
     444        }
     445      } if (node.Symbol is Erf) {
     446        AutoDiff.Term t;
     447        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     448          term = null;
     449          return false;
     450        } else {
     451          term = erf(t);
     452          return true;
     453        }
     454      } if (node.Symbol is Norm) {
     455        AutoDiff.Term t;
     456        if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out t)) {
     457          term = null;
     458          return false;
     459        } else {
     460          term = norm(t);
     461          return true;
     462        }
     463      }
     464      if (node.Symbol is StartSymbol) {
     465        var alpha = new AutoDiff.Variable();
     466        var beta = new AutoDiff.Variable();
     467        variables.Add(beta);
     468        variables.Add(alpha);
     469        AutoDiff.Term branchTerm;
     470        if (TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, out branchTerm)) {
     471          term = branchTerm * alpha + beta;
     472          return true;
     473        } else {
     474          term = null;
     475          return false;
     476        }
     477      }
     478      term = null;
     479      return false;
     480    }
     481
     482    public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
     483      var containsUnknownSymbol = (
     484        from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
     485        where
     486         !(n.Symbol is Variable) &&
     487         !(n.Symbol is Constant) &&
     488         !(n.Symbol is Addition) &&
     489         !(n.Symbol is Subtraction) &&
     490         !(n.Symbol is Multiplication) &&
     491         !(n.Symbol is Division) &&
     492         !(n.Symbol is Logarithm) &&
     493         !(n.Symbol is Exponential) &&
     494         !(n.Symbol is Sine) &&
     495         !(n.Symbol is Cosine) &&
     496         !(n.Symbol is Tangent) &&
     497         !(n.Symbol is Square) &&
     498         !(n.Symbol is Erf) &&
     499         !(n.Symbol is Norm) &&
     500         !(n.Symbol is StartSymbol)
     501        select n).
     502      Any();
     503      return !containsUnknownSymbol;
     504    }
    213505  }
    214506}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs

    r8127 r8915  
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3131  [StorableClass]
    32   public abstract class SymbolicRegressionSingleObjectiveEvaluator : SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionSingleObjectiveEvaluator {
    33     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    34     public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
    35       get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    36     }
    37     public bool ApplyLinearScaling {
    38       get { return ApplyLinearScalingParameter.Value.Value; }
    39       set { ApplyLinearScalingParameter.Value.Value = value; }
    40     }
    41 
     32  public abstract class SymbolicRegressionSingleObjectiveEvaluator : SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionSingleObjectiveEvaluator { 
    4233    [StorableConstructor]
    4334    protected SymbolicRegressionSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
    4435    protected SymbolicRegressionSingleObjectiveEvaluator(SymbolicRegressionSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
    45     protected SymbolicRegressionSingleObjectiveEvaluator()
    46       : base() {
    47       Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(true)));
    48       ApplyLinearScalingParameter.Hidden = true;
    49     }
    50 
    51     [StorableHook(HookType.AfterDeserialization)]
    52     private void AfterDeserialization() {
    53       if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
    54         Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(false)));
    55         ApplyLinearScalingParameter.Hidden = true;
    56       }
    57     }
    58 
    59     [ThreadStatic]
    60     private static double[] cache;
    61 
    62     protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
    63       double lowerEstimationLimit, double upperEstimationLimit,
    64       IOnlineCalculator calculator, int maxRows) {
    65       if (cache == null || cache.GetLength(0) < maxRows) {
    66         cache = new double[maxRows];
    67       }
    68 
    69       //calculate linear scaling
    70       //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements
    71       //this is not true if the cache is used
    72       int i = 0;
    73       var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
    74       var targetValuesEnumerator = targetValues.GetEnumerator();
    75       var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
    76       while (targetValuesEnumerator.MoveNext() && estimatedValuesEnumerator.MoveNext()) {
    77         double target = targetValuesEnumerator.Current;
    78         double estimated = estimatedValuesEnumerator.Current;
    79         cache[i] = estimated;
    80         linearScalingCalculator.Add(estimated, target);
    81         i++;
    82       }
    83       double alpha = linearScalingCalculator.Alpha;
    84       double beta = linearScalingCalculator.Beta;
    85 
    86       //calculate the quality by using the passed online calculator
    87       targetValuesEnumerator = targetValues.GetEnumerator();
    88       var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
    89         .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
    90 
    91       while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
    92         calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
    93       }
    94     }
     36    protected SymbolicRegressionSingleObjectiveEvaluator(): base() {}   
    9537  }
    9638}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs

    r8113 r8915  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
     
    4746      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4847
    49       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling);
     48      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5049      QualityParameter.ActualValue = new DoubleValue(quality);
    5150
     
    6867        mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    6968      }
    70       if (errorState != OnlineCalculatorError.None) return Double.NaN;
    71       else return mse;
     69      if (errorState != OnlineCalculatorError.None) return double.NaN;
     70      return mse;
    7271    }
    7372
     
    7574      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    7675      EstimationLimitsParameter.ExecutionContext = context;
     76      ApplyLinearScalingParameter.ExecutionContext = context;
    7777
    78       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling);
     78      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    7979
    8080      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    8181      EstimationLimitsParameter.ExecutionContext = null;
     82      ApplyLinearScalingParameter.ExecutionContext = null;
    8283
    8384      return mse;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs

    r8113 r8915  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
     
    4746      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4847
    49       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling);
     48      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5049      QualityParameter.ActualValue = new DoubleValue(quality);
    5150
     
    5857      OnlineCalculatorError errorState;
    5958
    60       double mse;
     59      double mae;
    6160      if (applyLinearScaling) {
    6261        var maeCalculator = new OnlineMeanAbsoluteErrorCalculator();
    6362        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows);
    6463        errorState = maeCalculator.ErrorState;
    65         mse = maeCalculator.MeanAbsoluteError;
     64        mae = maeCalculator.MeanAbsoluteError;
    6665      } else {
    67         IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit,
    68                                                                                   upperEstimationLimit);
    69         mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     66        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     67        mae = OnlineMeanAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    7068      }
    71       if (errorState != OnlineCalculatorError.None) return Double.NaN;
    72       else return mse;
     69      if (errorState != OnlineCalculatorError.None) return double.NaN;
     70      return mae;
    7371    }
    7472
     
    7674      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    7775      EstimationLimitsParameter.ExecutionContext = context;
     76      ApplyLinearScalingParameter.ExecutionContext = context;
    7877
    79       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling);
     78      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    8079
    8180      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    8281      EstimationLimitsParameter.ExecutionContext = null;
     82      ApplyLinearScalingParameter.ExecutionContext = null;
    8383
    8484      return mse;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs

    r8113 r8915  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
     
    4746      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4847
    49       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling);
     48      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5049      QualityParameter.ActualValue = new DoubleValue(quality);
    5150
     
    6867        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    6968      }
    70       if (errorState != OnlineCalculatorError.None) return Double.NaN;
    71       else return mse;
     69      if (errorState != OnlineCalculatorError.None) return double.NaN;
     70      return mse;
    7271    }
    7372
     
    7574      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    7675      EstimationLimitsParameter.ExecutionContext = context;
     76      ApplyLinearScalingParameter.ExecutionContext = context;
    7777
    78       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling);
     78      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    7979
    8080      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    8181      EstimationLimitsParameter.ExecutionContext = null;
     82      ApplyLinearScalingParameter.ExecutionContext = null;
    8283
    8384      return mse;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs

    r7672 r8915  
    4848      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4949
    50       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     50      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    5151      QualityParameter.ActualValue = new DoubleValue(quality);
    5252
     
    5454    }
    5555
    56     public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows) {
     56    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5757      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    58       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     58      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5959      OnlineCalculatorError errorState;
    60       double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
    61       if (errorState != OnlineCalculatorError.None) return 0.0;
    62       else return r2;
     60
     61      double r2;
     62      if (applyLinearScaling) {
     63        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
     64        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows);
     65        errorState = r2Calculator.ErrorState;
     66        r2 = r2Calculator.RSquared;
     67      } else {
     68        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     69        r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     70      }
     71      if (errorState != OnlineCalculatorError.None) return double.NaN;
     72      return r2;
    6373    }
    6474
     
    6676      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6777      EstimationLimitsParameter.ExecutionContext = context;
     78      ApplyLinearScalingParameter.ExecutionContext = context;
    6879
    69       double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     80      double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    7081
    7182      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7283      EstimationLimitsParameter.ExecutionContext = null;
     84      ApplyLinearScalingParameter.ExecutionContext = null;
    7385
    7486      return r2;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs

    r8175 r8915  
    6161      EstimationLimitsParameter.Hidden = true;
    6262
     63
     64      ApplyLinearScalingParameter.Value.Value = true;
    6365      Maximization.Value = true;
    6466      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3837    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    3938    private const string EstimationLimitsParameterName = "EstimationLimits";
    40     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4139    #region parameter properties
    4240    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
     
    4846    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    4947      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    50     }
    51     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    52       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    53     }
    54     #endregion
    55 
    56     #region properties
    57     public BoolValue ApplyLinearScaling {
    58       get { return ApplyLinearScalingParameter.Value; }
    5948    }
    6049    #endregion
     
    6857      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    6958      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
    70       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    7159    }
    7260    public override IDeepCloneable Clone(Cloner cloner) {
     
    7664    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    7765      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    78       if (ApplyLinearScaling.Value)
    79         SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue);
     66      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    8067      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    8168    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs

    r8169 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HeuristicLab.Parameters;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2826
     
    3432  [StorableClass]
    3533  public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IRegressionProblemData, ISymbolicRegressionSolution> {
    36     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    37     #region parameter properties
    38     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    39       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    40     }
    41     #endregion
    42 
    43     #region properties
    44     public BoolValue ApplyLinearScaling {
    45       get { return ApplyLinearScalingParameter.Value; }
    46     }
    47     #endregion
    4834
    4935    [StorableConstructor]
    5036    private SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
    5137    private SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
    52     public SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer()
    53       : base() {
    54       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    55     }
     38    public SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer() : base() { }
    5639    public override IDeepCloneable Clone(Cloner cloner) {
    5740      return new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(this, cloner);
     
    6043    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree) {
    6144      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    62       if (ApplyLinearScaling.Value)
    63         SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue);
     45      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    6446      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    6547    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer.cs

    r7259 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3635    ISymbolicDataAnalysisBoundedOperator {
    3736    private const string EstimationLimitsParameterName = "EstimationLimits";
    38     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3937
    4038    #region parameter properties
    4139    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    4240      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    43     }
    44     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    45       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    46     }
    47     #endregion
    48 
    49     #region properties
    50     public BoolValue ApplyLinearScaling {
    51       get { return ApplyLinearScalingParameter.Value; }
    5241    }
    5342    #endregion
     
    5948      : base() {
    6049      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
    61       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    6250    }
    6351
     
    6856    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    6957      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    70       if (ApplyLinearScaling.Value)
    71         SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue);
     58      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    7259      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    7360    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs

    r8169 r8915  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    26 using HeuristicLab.Parameters;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2826
     
    3432  [StorableClass]
    3533  public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData> {
    36     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    37     #region parameter properties
    38     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    39       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    40     }
    41     #endregion
    42 
    43     #region properties
    44     public BoolValue ApplyLinearScaling {
    45       get { return ApplyLinearScalingParameter.Value; }
    46     }
    47     #endregion
    48 
    4934    [StorableConstructor]
    5035    private SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
    5136    private SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
    52     public SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer()
    53       : base() {
    54       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    55     }
     37    public SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer() : base() { }
     38
    5639    public override IDeepCloneable Clone(Cloner cloner) {
    5740      return new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(this, cloner);
     
    6043    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree) {
    6144      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    62       if (ApplyLinearScaling.Value)
    63         SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue);
     45      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    6446      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    6547    }
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs

    r8139 r8915  
    6464
    6565    public ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    66       return new SymbolicRegressionSolution(this, problemData);
     66      return new SymbolicRegressionSolution(this, new RegressionProblemData(problemData));
    6767    }
    6868    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
    6969      return CreateRegressionSolution(problemData);
    7070    }
    71 
    72     public static void Scale(SymbolicRegressionModel model, IRegressionProblemData problemData) {
    73       var dataset = problemData.Dataset;
    74       var targetVariable = problemData.TargetVariable;
    75       var rows = problemData.TrainingIndices;
    76       var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    77       var targetValues = dataset.GetDoubleValues(targetVariable, rows);
    78       double alpha;
    79       double beta;
    80       OnlineCalculatorError errorState;
    81       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
    82       if (errorState != OnlineCalculatorError.None) return;
    83 
    84       ConstantTreeNode alphaTreeNode = null;
    85       ConstantTreeNode betaTreeNode = null;
    86       // check if model has been scaled previously by analyzing the structure of the tree
    87       var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
    88       if (startNode.GetSubtree(0).Symbol is Addition) {
    89         var addNode = startNode.GetSubtree(0);
    90         if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
    91           alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    92           var mulNode = addNode.GetSubtree(0);
    93           if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    94             betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    95           }
    96         }
    97       }
    98       // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    99       if (alphaTreeNode != null && betaTreeNode != null) {
    100         betaTreeNode.Value *= beta;
    101         alphaTreeNode.Value *= beta;
    102         alphaTreeNode.Value += alpha;
    103       } else {
    104         var mainBranch = startNode.GetSubtree(0);
    105         startNode.RemoveSubtree(0);
    106         var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
    107         startNode.AddSubtree(scaledMainBranch);
    108       }
    109     }
    110 
    111     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
    112       if (alpha.IsAlmost(0.0)) {
    113         return treeNode;
    114       } else {
    115         var addition = new Addition();
    116         var node = addition.CreateTreeNode();
    117         var alphaConst = MakeConstant(alpha);
    118         node.AddSubtree(treeNode);
    119         node.AddSubtree(alphaConst);
    120         return node;
    121       }
    122     }
    123 
    124     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
    125       if (beta.IsAlmost(1.0)) {
    126         return treeNode;
    127       } else {
    128         var multipliciation = new Multiplication();
    129         var node = multipliciation.CreateTreeNode();
    130         var betaConst = MakeConstant(beta);
    131         node.AddSubtree(treeNode);
    132         node.AddSubtree(betaConst);
    133         return node;
    134       }
    135     }
    136 
    137     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    138       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    139       node.Value = c;
    140       return node;
    141     }
    14271  }
    14372}
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs

    r7259 r8915  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3536    private const string ModelLengthResultName = "Model Length";
    3637    private const string ModelDepthResultName = "Model Depth";
     38
     39    private const string EstimationLimitsResultsResultName = "Estimation Limits Results";
     40    private const string EstimationLimitsResultName = "Estimation Limits";
     41    private const string TrainingUpperEstimationLimitHitsResultName = "Training Upper Estimation Limit Hits";
     42    private const string TestLowerEstimationLimitHitsResultName = "Test Lower Estimation Limit Hits";
     43    private const string TrainingLowerEstimationLimitHitsResultName = "Training Lower Estimation Limit Hits";
     44    private const string TestUpperEstimationLimitHitsResultName = "Test Upper Estimation Limit Hits";
     45    private const string TrainingNaNEvaluationsResultName = "Training NaN Evaluations";
     46    private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
    3747
    3848    public new ISymbolicRegressionModel Model {
     
    5363    }
    5464
     65    private ResultCollection EstimationLimitsResultCollection {
     66      get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
     67    }
     68    public DoubleLimit EstimationLimits {
     69      get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
     70    }
     71
     72    public int TrainingUpperEstimationLimitHits {
     73      get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
     74      private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
     75    }
     76    public int TestUpperEstimationLimitHits {
     77      get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
     78      private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
     79    }
     80    public int TrainingLowerEstimationLimitHits {
     81      get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
     82      private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
     83    }
     84    public int TestLowerEstimationLimitHits {
     85      get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
     86      private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
     87    }
     88    public int TrainingNaNEvaluations {
     89      get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
     90      private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
     91    }
     92    public int TestNaNEvaluations {
     93      get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
     94      private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
     95    }
     96
    5597    [StorableConstructor]
    5698    private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
     
    62104      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
    63105      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
     106
     107      ResultCollection estimationLimitResults = new ResultCollection();
     108      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
     109      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
     110      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
     111      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
     112      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
     113      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
     114      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
     115      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
     116
    64117      RecalculateResults();
    65118    }
     
    69122    }
    70123
     124    [StorableHook(HookType.AfterDeserialization)]
     125    private void AfterDeserialization() {
     126      if (!ContainsKey(EstimationLimitsResultsResultName)) {
     127        ResultCollection estimationLimitResults = new ResultCollection();
     128        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
     129        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
     130        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
     131        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
     132        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
     133        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
     134        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
     135        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
     136        CalculateResults();
     137      }
     138    }
     139
    71140    protected override void RecalculateResults() {
    72141      base.RecalculateResults();
     142      CalculateResults();
     143    }
     144
     145    private void CalculateResults() {
    73146      ModelLength = Model.SymbolicExpressionTree.Length;
    74147      ModelDepth = Model.SymbolicExpressionTree.Depth;
     148
     149      EstimationLimits.Lower = Model.LowerEstimationLimit;
     150      EstimationLimits.Upper = Model.UpperEstimationLimit;
     151
     152      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
     153      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
     154      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
     155      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
     156      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
     157      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
    75158    }
    76159  }
Note: See TracChangeset for help on using the changeset viewer.