Changeset 8915 for branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 11/15/12 16:47:25 (12 years ago)
- 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 93 93 </PropertyGroup> 94 94 <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> 97 103 </Reference> 98 104 <Reference Include="HeuristicLab.Analysis-3.3"> 99 105 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath> 106 <Private>False</Private> 100 107 </Reference> 101 108 <Reference Include="HeuristicLab.Collections-3.3"> 102 109 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> 110 <Private>False</Private> 103 111 </Reference> 104 112 <Reference Include="HeuristicLab.Common-3.3"> 105 113 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 114 <Private>False</Private> 106 115 </Reference> 107 116 <Reference Include="HeuristicLab.Core-3.3"> 108 117 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 118 <Private>False</Private> 109 119 </Reference> 110 120 <Reference Include="HeuristicLab.Data-3.3"> 111 121 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath> 122 <Private>False</Private> 112 123 </Reference> 113 124 <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4"> 114 125 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath> 126 <Private>False</Private> 115 127 </Reference> 116 128 <Reference Include="HeuristicLab.Operators-3.3"> 117 129 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> 130 <Private>False</Private> 118 131 </Reference> 119 132 <Reference Include="HeuristicLab.Optimization-3.3"> 120 133 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> 134 <Private>False</Private> 121 135 </Reference> 122 136 <Reference Include="HeuristicLab.Parameters-3.3"> 123 137 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> 138 <Private>False</Private> 124 139 </Reference> 125 140 <Reference Include="HeuristicLab.Persistence-3.3"> 126 141 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath> 142 <Private>False</Private> 127 143 </Reference> 128 144 <Reference Include="HeuristicLab.PluginInfrastructure-3.3"> 129 145 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 146 <Private>False</Private> 130 147 </Reference> 131 148 <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4"> 132 149 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath> 150 <Private>False</Private> 133 151 </Reference> 134 152 <Reference Include="HeuristicLab.Problems.Instances-3.3"> 135 153 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath> 154 <Private>False</Private> 136 155 </Reference> 137 156 <Reference Include="System" /> … … 152 171 <Compile Include="MultiObjective\SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs" /> 153 172 <Compile Include="Plugin.cs" /> 173 <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" /> 154 174 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" /> 155 175 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs" /> … … 176 196 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" /> 177 197 <Compile Include="SymbolicRegressionSolution.cs" /> 178 <Compile Include="SymbolicRegressionSolutionValuesCalculator.cs" />179 198 <None Include="HeuristicLab.snk" /> 180 199 <None Include="Plugin.cs.frame" /> … … 215 234 --> 216 235 <PropertyGroup> 217 <PreBuildEvent >set Path=%25Path%25;$(ProjectDir);$(SolutionDir)236 <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir) 218 237 set ProjectDir=$(ProjectDir) 219 238 set SolutionDir=$(SolutionDir) … … 222 241 call PreBuildEvent.cmd 223 242 </PreBuildEvent> 243 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' "> 244 export ProjectDir=$(ProjectDir) 245 export SolutionDir=$(SolutionDir) 246 247 $SolutionDir/PreBuildEvent.sh 248 </PreBuildEvent> 224 249 </PropertyGroup> 225 250 </Project> -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Interfaces/ISymbolicRegressionModel.cs
r7259 r8915 20 20 #endregion 21 21 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;23 22 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 24 23 public interface ISymbolicRegressionModel : IRegressionModel, ISymbolicDataAnalysisModel { 24 double LowerEstimationLimit { get; } 25 double UpperEstimationLimit { get; } 25 26 } 26 27 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs
r7259 r8915 47 47 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 48 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); 50 50 QualitiesParameter.ActualValue = new DoubleArray(qualities); 51 51 return base.Apply(); 52 52 } 53 53 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) { 55 55 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); 58 57 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 } 60 69 if (errorState != OnlineCalculatorError.None) mse = double.NaN; 61 70 return new double[2] { mse, solution.Length }; … … 65 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 66 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 67 77 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); 69 79 70 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 71 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 72 83 73 84 return quality; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs
r7259 r8915 47 47 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 48 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); 50 50 QualitiesParameter.ActualValue = new DoubleArray(qualities); 51 51 return base.Apply(); 52 52 } 53 53 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) { 55 55 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); 57 57 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 }; 61 72 } 62 73 … … 64 75 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 65 76 EstimationLimitsParameter.ExecutionContext = context; 77 ApplyLinearScalingParameter.ExecutionContext = context; 66 78 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); 68 80 69 81 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 70 82 EstimationLimitsParameter.ExecutionContext = null; 83 ApplyLinearScalingParameter.ExecutionContext = null; 71 84 72 85 return quality; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveProblem.cs
r8175 r8915 65 65 EstimationLimitsParameter.Hidden = true; 66 66 67 ApplyLinearScalingParameter.Value.Value = true; 67 68 Maximization = new BoolArray(new bool[] { true, false }); 68 69 MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs
r7259 r8915 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 38 37 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 38 private const string EstimationLimitsParameterName = "EstimationLimits"; 40 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";41 39 #region parameter properties 42 40 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { … … 48 46 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { 49 47 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 50 }51 public IValueParameter<BoolValue> ApplyLinearScalingParameter {52 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }53 }54 #endregion55 56 #region properties57 public BoolValue ApplyLinearScaling {58 get { return ApplyLinearScalingParameter.Value; }59 48 } 60 49 #endregion … … 68 57 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); 69 58 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)));71 59 } 72 60 … … 77 65 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 78 66 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); 81 68 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 82 69 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs
r7259 r8915 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 36 35 ISymbolicDataAnalysisBoundedOperator { 37 36 private const string EstimationLimitsParameterName = "EstimationLimits"; 38 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";39 37 40 38 #region parameter properties … … 42 40 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 43 41 } 44 public IValueParameter<BoolValue> ApplyLinearScalingParameter {45 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }46 }47 42 #endregion 48 43 49 #region properties50 public BoolValue ApplyLinearScaling {51 get { return ApplyLinearScalingParameter.Value; }52 }53 #endregion54 44 [StorableConstructor] 55 45 private SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } … … 58 48 : base() { 59 49 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)));61 50 } 62 51 public override IDeepCloneable Clone(Cloner cloner) { … … 66 55 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 67 56 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); 70 58 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 71 59 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Plugin.cs.frame
r8246 r8915 28 28 [Plugin("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression","Provides classes to perform symbolic regression (single- or multiobjective).", "3.4.3.$WCREV$")] 29 29 [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")] 31 32 [PluginDependency("HeuristicLab.Analysis", "3.3")] 32 33 [PluginDependency("HeuristicLab.Common", "3.3")] -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs
r8053 r8915 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using AutoDiff; 24 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; … … 37 39 private const string ConstantOptimizationProbabilityParameterName = "ConstantOptimizationProbability"; 38 40 private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage"; 41 private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree"; 39 42 40 43 private const string EvaluatedTreesResultName = "EvaluatedTrees"; … … 60 63 get { return (IFixedValueParameter<PercentValue>)Parameters[ConstantOptimizationRowsPercentageParameterName]; } 61 64 } 65 public IFixedValueParameter<BoolValue> UpdateConstantsInTreeParameter { 66 get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateConstantsInTreeParameterName]; } 67 } 62 68 63 69 public IntValue ConstantOptimizationIterations { … … 72 78 public PercentValue ConstantOptimizationRowsPercentage { 73 79 get { return ConstantOptimizationRowsPercentageParameter.Value; } 80 } 81 public bool UpdateConstantsInTree { 82 get { return UpdateConstantsInTreeParameter.Value.Value; } 83 set { UpdateConstantsInTreeParameter.Value.Value = value; } 74 84 } 75 85 … … 89 99 Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1), true)); 90 100 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))); 91 102 92 103 Parameters.Add(new LookupParameter<IntValue>(EvaluatedTreesResultName)); … … 98 109 } 99 110 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 100 117 public override IOperation Apply() { 101 118 AddResults(); 102 int seed = RandomParameter.ActualValue.Next();103 119 var solution = SymbolicExpressionTreeParameter.ActualValue; 104 120 double quality; … … 106 122 IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value); 107 123 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, 110 126 EvaluatedTreesParameter.ActualValue, EvaluatedTreeNodesParameter.ActualValue); 111 127 if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) { 112 128 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); 114 130 } 115 131 } else { 116 132 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); 118 134 } 119 135 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 } 122 140 123 141 if (Successor != null) … … 127 145 } 128 146 147 private object locker = new object(); 129 148 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 } 141 162 } 142 163 } … … 145 166 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 146 167 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); 149 171 150 172 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 151 173 EstimationLimitsParameter.ExecutionContext = null; 174 ApplyLinearScalingParameter.ExecutionContext = context; 152 175 153 176 return r2; 154 177 } 155 178 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 156 205 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 158 219 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); 210 293 }; 211 294 } 212 295 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 } 213 505 } 214 506 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs
r8127 r8915 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 31 31 [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 { 42 33 [StorableConstructor] 43 34 protected SymbolicRegressionSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { } 44 35 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() {} 95 37 } 96 38 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs
r8113 r8915 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 47 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 47 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); 50 49 QualityParameter.ActualValue = new DoubleValue(quality); 51 50 … … 68 67 mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 68 } 70 if (errorState != OnlineCalculatorError.None) return Double.NaN;71 elsereturn mse;69 if (errorState != OnlineCalculatorError.None) return double.NaN; 70 return mse; 72 71 } 73 72 … … 75 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 76 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 77 77 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); 79 79 80 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 81 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 82 83 83 84 return mse; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs
r8113 r8915 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 47 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 47 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); 50 49 QualityParameter.ActualValue = new DoubleValue(quality); 51 50 … … 58 57 OnlineCalculatorError errorState; 59 58 60 double m se;59 double mae; 61 60 if (applyLinearScaling) { 62 61 var maeCalculator = new OnlineMeanAbsoluteErrorCalculator(); 63 62 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows); 64 63 errorState = maeCalculator.ErrorState; 65 m se = maeCalculator.MeanAbsoluteError;64 mae = maeCalculator.MeanAbsoluteError; 66 65 } 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); 70 68 } 71 if (errorState != OnlineCalculatorError.None) return Double.NaN;72 else return mse;69 if (errorState != OnlineCalculatorError.None) return double.NaN; 70 return mae; 73 71 } 74 72 … … 76 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 77 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 78 77 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); 80 79 81 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 82 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 83 83 84 84 return mse; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs
r8113 r8915 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 47 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 47 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); 50 49 QualityParameter.ActualValue = new DoubleValue(quality); 51 50 … … 68 67 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 68 } 70 if (errorState != OnlineCalculatorError.None) return Double.NaN;71 elsereturn mse;69 if (errorState != OnlineCalculatorError.None) return double.NaN; 70 return mse; 72 71 } 73 72 … … 75 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 76 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 77 77 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); 79 79 80 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 81 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 82 83 83 84 return mse; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs
r7672 r8915 48 48 IEnumerable<int> rows = GenerateRowsToEvaluate(); 49 49 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); 51 51 QualityParameter.ActualValue = new DoubleValue(quality); 52 52 … … 54 54 } 55 55 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) { 57 57 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); 59 59 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; 63 73 } 64 74 … … 66 76 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 67 77 EstimationLimitsParameter.ExecutionContext = context; 78 ApplyLinearScalingParameter.ExecutionContext = context; 68 79 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); 70 81 71 82 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 72 83 EstimationLimitsParameter.ExecutionContext = null; 84 ApplyLinearScalingParameter.ExecutionContext = null; 73 85 74 86 return r2; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs
r8175 r8915 61 61 EstimationLimitsParameter.Hidden = true; 62 62 63 64 ApplyLinearScalingParameter.Value.Value = true; 63 65 Maximization.Value = true; 64 66 MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth; -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer.cs
r7259 r8915 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 38 37 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 38 private const string EstimationLimitsParameterName = "EstimationLimits"; 40 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";41 39 #region parameter properties 42 40 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { … … 48 46 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { 49 47 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 50 }51 public IValueParameter<BoolValue> ApplyLinearScalingParameter {52 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }53 }54 #endregion55 56 #region properties57 public BoolValue ApplyLinearScaling {58 get { return ApplyLinearScalingParameter.Value; }59 48 } 60 49 #endregion … … 68 57 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); 69 58 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)));71 59 } 72 60 public override IDeepCloneable Clone(Cloner cloner) { … … 76 64 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 77 65 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); 80 67 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 81 68 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs
r8169 r8915 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Parameters;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 … … 34 32 [StorableClass] 35 33 public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IRegressionProblemData, ISymbolicRegressionSolution> { 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";37 #region parameter properties38 public IValueParameter<BoolValue> ApplyLinearScalingParameter {39 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }40 }41 #endregion42 43 #region properties44 public BoolValue ApplyLinearScaling {45 get { return ApplyLinearScalingParameter.Value; }46 }47 #endregion48 34 49 35 [StorableConstructor] 50 36 private SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } 51 37 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() { } 56 39 public override IDeepCloneable Clone(Cloner cloner) { 57 40 return new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(this, cloner); … … 60 43 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree) { 61 44 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); 64 46 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 65 47 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer.cs
r7259 r8915 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 36 35 ISymbolicDataAnalysisBoundedOperator { 37 36 private const string EstimationLimitsParameterName = "EstimationLimits"; 38 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";39 37 40 38 #region parameter properties 41 39 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { 42 40 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 43 }44 public IValueParameter<BoolValue> ApplyLinearScalingParameter {45 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }46 }47 #endregion48 49 #region properties50 public BoolValue ApplyLinearScaling {51 get { return ApplyLinearScalingParameter.Value; }52 41 } 53 42 #endregion … … 59 48 : base() { 60 49 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)));62 50 } 63 51 … … 68 56 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 69 57 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); 72 59 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 73 60 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs
r8169 r8915 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Parameters;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 … … 34 32 [StorableClass] 35 33 public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData> { 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";37 #region parameter properties38 public IValueParameter<BoolValue> ApplyLinearScalingParameter {39 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }40 }41 #endregion42 43 #region properties44 public BoolValue ApplyLinearScaling {45 get { return ApplyLinearScalingParameter.Value; }46 }47 #endregion48 49 34 [StorableConstructor] 50 35 private SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } 51 36 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 56 39 public override IDeepCloneable Clone(Cloner cloner) { 57 40 return new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(this, cloner); … … 60 43 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree) { 61 44 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); 64 46 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 65 47 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs
r8139 r8915 64 64 65 65 public ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { 66 return new SymbolicRegressionSolution(this, problemData);66 return new SymbolicRegressionSolution(this, new RegressionProblemData(problemData)); 67 67 } 68 68 IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) { 69 69 return CreateRegressionSolution(problemData); 70 70 } 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 tree87 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 nodes99 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 }142 71 } 143 72 } -
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs
r7259 r8915 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 35 36 private const string ModelLengthResultName = "Model Length"; 36 37 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"; 37 47 38 48 public new ISymbolicRegressionModel Model { … … 53 63 } 54 64 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 55 97 [StorableConstructor] 56 98 private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { } … … 62 104 Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue())); 63 105 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 64 117 RecalculateResults(); 65 118 } … … 69 122 } 70 123 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 71 140 protected override void RecalculateResults() { 72 141 base.RecalculateResults(); 142 CalculateResults(); 143 } 144 145 private void CalculateResults() { 73 146 ModelLength = Model.SymbolicExpressionTree.Length; 74 147 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); 75 158 } 76 159 }
Note: See TracChangeset
for help on using the changeset viewer.