Changeset 8206 for branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Timestamp:
- 07/03/12 16:46:35 (12 years ago)
- Location:
- branches/GP-MoveOperators
- Files:
-
- 14 edited
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-MoveOperators
- Property svn:mergeinfo changed
/trunk/sources merged: 8084,8088-8090,8092-8100,8102-8113,8115,8117-8132,8134-8146,8148-8156,8158-8160,8163-8170,8173-8176,8178-8190,8192-8205
- Property svn:mergeinfo changed
-
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r8085 r8206 112 112 </ItemGroup> 113 113 <ItemGroup> 114 <Compile Include="ReplaceBranchMove.cs" /> 114 115 <Compile Include="ReplaceBranchMoveEvaluator.cs" /> 115 116 <Compile Include="Interfaces\ISymbolicRegressionMoveEvaluator.cs" /> 116 117 <Compile Include="MultiObjective\SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs" /> 117 118 <Compile Include="Plugin.cs" /> 119 <Compile Include="ReplaceBranchMoveMaker.cs" /> 120 <Compile Include="ReplaceBranchMoveSoftTabuCriterion.cs" /> 121 <Compile Include="ReplaceBranchMoveTabuMaker.cs" /> 118 122 <Compile Include="ReplaceBranchMultiMoveGenerator.cs" /> 119 123 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" /> … … 141 145 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" /> 142 146 <Compile Include="SymbolicRegressionSolution.cs" /> 143 <Compile Include="SymbolicRegressionMoveEvaluator.cs" />144 147 <None Include="HeuristicLab.snk" /> 145 148 <None Include="Plugin.cs.frame" /> … … 199 202 <Name>HeuristicLab.Operators-3.3</Name> 200 203 <Private>False</Private> 204 </ProjectReference> 205 <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj"> 206 <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project> 207 <Name>HeuristicLab.Optimization.Operators-3.3</Name> 201 208 </ProjectReference> 202 209 <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj"> -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveProblem.cs
r8085 r8206 53 53 [StorableConstructor] 54 54 protected SymbolicRegressionMultiObjectiveProblem(bool deserializing) : base(deserializing) { } 55 protected SymbolicRegressionMultiObjectiveProblem(SymbolicRegressionMultiObjectiveProblem original, Cloner cloner) : base(original, cloner) { } 55 protected SymbolicRegressionMultiObjectiveProblem(SymbolicRegressionMultiObjectiveProblem original, Cloner cloner) 56 : base(original, cloner) { 57 RegisterEventHandlers(); 58 } 56 59 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionMultiObjectiveProblem(this, cloner); } 57 60 … … 66 69 MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength; 67 70 68 SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols(); 69 71 RegisterEventHandlers(); 70 72 ConfigureGrammarSymbols(); 71 73 InitializeOperators(); 72 74 UpdateEstimationLimits(); 75 } 76 77 [StorableHook(HookType.AfterDeserialization)] 78 private void AfterDeserialization() { 79 RegisterEventHandlers(); 80 } 81 82 private void RegisterEventHandlers() { 83 SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols(); 73 84 } 74 85 … … 85 96 86 97 private void UpdateEstimationLimits() { 87 if (ProblemData.TrainingIndi zes.Any()) {88 var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndi zes).ToList();98 if (ProblemData.TrainingIndices.Any()) { 99 var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList(); 89 100 var mean = targetValues.Average(); 90 101 var range = targetValues.Max() - targetValues.Min(); -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMove.cs
r8083 r8206 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 26 26 namespace HeuristicLab. Encodings.SymbolicExpressionTreeEncoding{27 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 27 28 [Item("ReplaceBranchMove", "")] 28 29 [StorableClass] … … 41 42 public ISymbolicExpressionTreeNode NewBranch { get; set; } 42 43 44 [Storable] 45 public double[] OriginalOutput { get; private set; } 46 47 [Storable] 48 public double[] NewOutput { get; private set; } 49 50 [Storable] 51 public double Alpha { get; set; } 52 53 [Storable] 54 public double Beta { get; set; } 55 43 56 44 57 [StorableConstructor] … … 50 63 this.Parent = cloner.Clone(original.Parent); 51 64 this.NewBranch = cloner.Clone(original.NewBranch); 65 this.OriginalOutput = original.OriginalOutput; 66 this.NewOutput = original.NewOutput; 67 this.Alpha = original.Alpha; 68 this.Beta = original.Beta; 52 69 } 53 public ReplaceBranchMove(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, int subtreeIndex, ISymbolicExpressionTreeNode newChild )70 public ReplaceBranchMove(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, int subtreeIndex, ISymbolicExpressionTreeNode newChild, double[] originalOutput, double[] newOutput) 54 71 : base() { 55 72 this.Tree = tree; … … 57 74 this.Parent = parent; 58 75 this.NewBranch = newChild; 76 this.OriginalOutput = originalOutput; 77 this.NewOutput = newOutput; 59 78 } 60 79 -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveEvaluator.cs
r7832 r8206 80 80 var move = (ReplaceBranchMove)MoveParameter.ActualValue.Clone(); 81 81 82 // calculate linear scaling 83 OnlineCalculatorError errorState; 84 double alpha, beta; 85 OnlineLinearScalingParameterCalculator.Calculate(move.NewOutput, move.OriginalOutput, out alpha, out beta, out errorState); 86 87 move.Alpha = alpha; 88 move.Beta = beta; 89 82 90 move.Parent.RemoveSubtree(move.SubtreeIndex); 83 move.Parent.InsertSubtree(move.SubtreeIndex, move.NewBranch);91 move.Parent.InsertSubtree(move.SubtreeIndex, Scale(move.NewBranch, alpha, beta)); 84 92 85 93 var problemData = ProblemDataParameter.ActualValue; … … 94 102 } 95 103 104 private Addition add = new Addition(); 105 private Multiplication mul = new Multiplication(); 106 private Constant constant = new Constant(); 107 108 private ISymbolicExpressionTreeNode Scale(ISymbolicExpressionTreeNode node, double alpha, double beta) { 109 var constAlpha = (ConstantTreeNode)constant.CreateTreeNode(); 110 var constBeta = (ConstantTreeNode)constant.CreateTreeNode(); 111 constAlpha.Value = alpha; 112 constBeta.Value = beta; 113 var sum = add.CreateTreeNode(); 114 var prod = mul.CreateTreeNode(); 115 prod.AddSubtree(node); prod.AddSubtree(constBeta); 116 sum.AddSubtree(prod); sum.AddSubtree(constAlpha); 117 return sum; 118 } 119 96 120 public override IDeepCloneable Clone(Cloner cloner) { 97 121 return new ReplaceBranchMoveEvaluator(this, cloner); -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveMaker.cs
r8083 r8206 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 26 using HeuristicLab.Operators; 26 27 using HeuristicLab.Optimization; … … 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 30 30 namespace HeuristicLab. Encodings.SymbolicExpressionTreeEncoding{31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 31 32 [Item("ReplaceBranchMoveMaker", "")] 32 33 [StorableClass] … … 65 66 public override IOperation Apply() { 66 67 var move = ReplaceBranchMoveParameter.ActualValue; 67 var tree = SymbolicExpressionTreeParameter.ActualValue;68 68 DoubleValue moveQuality = MoveQualityParameter.ActualValue; 69 69 DoubleValue quality = QualityParameter.ActualValue; … … 71 71 var newNode = (ISymbolicExpressionTreeNode)move.NewBranch.Clone(); 72 72 move.Parent.RemoveSubtree(move.SubtreeIndex); 73 move.Parent.InsertSubtree(move.SubtreeIndex, newNode);73 move.Parent.InsertSubtree(move.SubtreeIndex, Scale(newNode, move.Alpha, move.Beta)); 74 74 75 75 quality.Value = moveQuality.Value; … … 77 77 return base.Apply(); 78 78 } 79 80 81 private Addition add = new Addition(); 82 private Multiplication mul = new Multiplication(); 83 private Constant constant = new Constant(); 84 85 private ISymbolicExpressionTreeNode Scale(ISymbolicExpressionTreeNode node, double alpha, double beta) { 86 var constAlpha = (ConstantTreeNode)constant.CreateTreeNode(); 87 var constBeta = (ConstantTreeNode)constant.CreateTreeNode(); 88 constAlpha.Value = alpha; 89 constBeta.Value = beta; 90 var sum = add.CreateTreeNode(); 91 var prod = mul.CreateTreeNode(); 92 prod.AddSubtree(node); prod.AddSubtree(constBeta); 93 sum.AddSubtree(prod); sum.AddSubtree(constAlpha); 94 return sum; 95 } 79 96 } 80 97 } -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveSoftTabuCriterion.cs
r8083 r8206 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Data; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 27 using HeuristicLab.Operators; 27 28 using HeuristicLab.Optimization; … … 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 31 31 namespace HeuristicLab. Encodings.SymbolicExpressionTreeEncoding{32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 32 33 [Item("ReplaceBranchMoveSoftTabuCriterion", @"")] 33 34 [StorableClass] … … 119 120 } 120 121 } 121 //int length = permutation.Length;122 //double moveQuality = MoveQualityParameter.ActualValue.Value;123 //bool maximization = MaximizationParameter.ActualValue.Value;124 //bool useAspiration = UseAspirationCriterion.Value;125 //bool isTabu = false;126 122 127 //if (permutation.PermutationType == PermutationTypes.Absolute) {128 // int count = move.Index2 - move.Index1 + 1;129 // int[] numbers = new int[count];130 // for (int i = move.Index1; i <= move.Index2; i++)131 // numbers[i - move.Index1] = permutation[i];132 133 // foreach (IItem tabuMove in tabuList) {134 // TranslocationMoveAbsoluteAttribute attribute = (tabuMove as TranslocationMoveAbsoluteAttribute);135 // if (attribute != null) {136 // if (!useAspiration137 // || maximization && moveQuality <= attribute.MoveQuality138 // || !maximization && moveQuality >= attribute.MoveQuality) { // if the move quality is improving beyond what was recorded when the move in the tabu list was recorded the move is regarded as okay139 140 // for (int i = 0; i < count; i++) {141 // for (int j = 0; j < attribute.Number.Length; j++) {142 // if (attribute.Number[j] == numbers[i] && attribute.OldPosition + j == move.Index3 + i) {143 // isTabu = true;144 // break;145 // }146 // }147 // if (isTabu) break;148 // }149 // }150 // }151 // if (isTabu) break;152 // }153 //} else {154 // int E1S = permutation.GetCircular(move.Index1 - 1);155 // int E1T = permutation[move.Index1];156 // int E2S = permutation[move.Index2];157 // int E2T = permutation.GetCircular(move.Index2 + 1);158 // int E3S, E3T;159 // if (move.Index3 > move.Index1) {160 // E3S = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);161 // E3T = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);162 // } else {163 // E3S = permutation.GetCircular(move.Index3 - 1);164 // E3T = permutation[move.Index3];165 // }166 // foreach (IItem tabuMove in tabuList) {167 // TranslocationMoveRelativeAttribute attribute = (tabuMove as TranslocationMoveRelativeAttribute);168 // if (attribute != null) {169 // if (!useAspiration170 // || maximization && moveQuality <= attribute.MoveQuality171 // || !maximization && moveQuality >= attribute.MoveQuality) {172 173 // // if previously deleted Edge1Source-Target is readded174 // if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {175 // if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T || attribute.Edge1Source == E1T && attribute.Edge1Target == E3S176 // || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T || attribute.Edge1Source == E3T && attribute.Edge1Target == E2S177 // // if previously deleted Edge2Source-Target is readded178 // || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T || attribute.Edge2Source == E1T && attribute.Edge2Target == E3S179 // || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T || attribute.Edge2Source == E3T && attribute.Edge2Target == E2S180 // // if previously deleted Edge3Source-Target is readded181 // || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T || attribute.Edge3Source == E1T && attribute.Edge3Target == E3S182 // || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T || attribute.Edge3Source == E3T && attribute.Edge3Target == E2S) {183 // isTabu = true;184 // break;185 // }186 // } else {187 // if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T188 // || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T189 // // if previously deleted Edge2Source-Target is readded190 // || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T191 // || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T192 // // if previously deleted Edge3Source-Target is readded193 // || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T194 // || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T) {195 // isTabu = true;196 // break;197 // }198 // }199 // }200 // }201 // }202 //}203 123 MoveTabuParameter.ActualValue = new BoolValue(isTabu); 204 124 return base.Apply(); -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveTabuMaker.cs
r8083 r8206 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 26 using HeuristicLab.Optimization.Operators; 26 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 29 namespace HeuristicLab. Encodings.SymbolicExpressionTreeEncoding{30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 30 31 [Item("ReplaceBranchMoveTabuMaker", "")] 31 32 [StorableClass] -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMultiMoveGenerator.cs
r7832 r8206 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 63 64 get { return (LookupParameter<ReplaceBranchMove>)Parameters["ReplaceBranchMove"]; } 64 65 } 66 67 public IValueParameter<IntValue> ReplacementBranchesPoolSize { 68 get { return (IValueParameter<IntValue>)Parameters["ReplacementBranchesPoolSize"]; } 69 } 70 71 public IValueParameter<IntValue> MaxReplacementBranchLength { 72 get { return (IValueParameter<IntValue>)Parameters["MaxReplacementBranchLength"]; } 73 } 74 75 public IValueParameter<IntValue> MaxReplacementBranchDepth { 76 get { return (IValueParameter<IntValue>)Parameters["MaxReplacementBranchDepth"]; } 77 } 78 65 79 protected ScopeParameter CurrentScopeParameter { 66 80 get { return (ScopeParameter)Parameters["CurrentScope"]; } … … 84 98 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>("Interpreter")); 85 99 Parameters.Add(new LookupParameter<IRegressionProblemData>("ProblemData")); 100 Parameters.Add(new ValueParameter<IntValue>("ReplacementBranchesPoolSize", new IntValue(10000))); 101 Parameters.Add(new ValueParameter<IntValue>("MaxReplacementBranchLength", new IntValue(8))); 102 Parameters.Add(new ValueParameter<IntValue>("MaxReplacementBranchDepth", new IntValue(4))); 86 103 } 87 104 … … 121 138 public IEnumerable<ReplaceBranchMove> GenerateMoves(ISymbolicExpressionTree tree, IRandom random, int n) { 122 139 int count = 0; 123 var g = tree.Root.Grammar;124 140 var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix() 125 141 from i in Enumerable.Range(0, parent.SubtreeCount) … … 136 152 137 153 while (count < n) { 154 // select a random replacement point 138 155 var selected = possibleChildren[random.Next(possibleChildren.Length)]; 139 156 // evaluate … … 142 159 start.RemoveSubtree(0); 143 160 144 var bestTrees = new SortedList<double, ISymbolicExpressionTree>(); 145 double bestSimilarity = double.MaxValue; 161 var bestTrees = new SortedList<double, Tuple<ISymbolicExpressionTree, double[]>>(); 162 double bestSimilarity = 0.0; 163 // iterate over the whole pool of branches for replacement 146 164 for (int i = 0; i < trees.Count; i++) { 165 // if the branch is allowed in the selected point 147 166 if (tree.Root.Grammar.IsAllowedChildSymbol(selected.parent.Symbol, trees[i].Root.GetSubtree(0).GetSubtree(0).Symbol, selected.i)) { 148 167 OnlineCalculatorError error; 149 double similarity = OnlineMeanSquaredErrorCalculator.Calculate(output, treeOutput[i], out error); 150 if (error != OnlineCalculatorError.None) similarity = double.MaxValue; 151 if (similarity < bestSimilarity && !similarity.IsAlmost(0.0)) { 168 // calculate the similarity 169 double similarity = OnlinePearsonsRSquaredCalculator.Calculate(output, treeOutput[i], out error); 170 if (error != OnlineCalculatorError.None) similarity = 0.0; 171 172 // if we found a new bestSimilarity then keep the replacement branch in a sorted list (keep maximally the 30 best for this replacement point) 173 if (similarity > bestSimilarity && !similarity.IsAlmost(1.0)) { 152 174 bestSimilarity = similarity; 153 bestTrees.Add(similarity, trees[i]);175 bestTrees.Add(similarity, Tuple.Create(trees[i], treeOutput[i])); 154 176 if (bestTrees.Count > 30) 155 177 bestTrees.RemoveAt(bestTrees.Count - 1); … … 157 179 } 158 180 } 159 foreach (var bestTree in bestTrees.Values) { 181 // did not find a move better than similarity 0.0 => create a move to replace it with a random branch 182 // this is necessary to make it possible to replace branches that evaluate to NaN or infinity 183 if (bestTrees.Count == 0) { 184 int r = random.Next(trees.Count); 185 bestTrees.Add(0.0, Tuple.Create(trees[r], treeOutput[r])); 186 } 187 // yield moves (we need to add linear scaling parameters for the inserted tree) 188 foreach (var pair in bestTrees.Values) { 160 189 yield return 161 new ReplaceBranchMove(tree, selected.parent, selected.i, bestTree.Root.GetSubtree(0).GetSubtree(0));190 new ReplaceBranchMove(tree, selected.parent, selected.i, pair.Item1.Root.GetSubtree(0).GetSubtree(0), output, pair.Item2); 162 191 count++; 163 192 } … … 166 195 167 196 private void InitializeOperator() { 168 trees = new List<ISymbolicExpressionTree>(); 169 treeOutput = new List<double[]>(); 197 // init locally and set only at the end in case of exceptions 198 var trees = new List<ISymbolicExpressionTree>(); 199 var treeOutput = new List<double[]>(); 170 200 var random = RandomParameter.ActualValue; 171 201 var g = SymbolicExpressionTreeGrammarParameter.ActualValue; 172 var constSym = g. AllowedSymbols.Single(s => s is Constant);202 var constSym = g.Symbols.Single(s => s is Constant); 173 203 // temporarily disable constants 174 204 double oldConstFreq = constSym.InitialFrequency; … … 177 207 var ds = ProblemDataParameter.ActualValue.Dataset; 178 208 var rows = ProblemDataParameter.ActualValue.TrainingIndizes; 179 for (int i = 0; i < 10000; i++) { 180 var t = ProbabilisticTreeCreator.Create(random, g, 8, 4); 209 // create pool of random branches for replacement (no constants) 210 // and evaluate the output 211 // only keep trees if the output does not contain invalid values 212 var n = ReplacementBranchesPoolSize.Value.Value; 213 while (trees.Count < n) { 214 var t = ProbabilisticTreeCreator.Create(random, g, MaxReplacementBranchLength.Value.Value, MaxReplacementBranchDepth.Value.Value); 181 215 var output = interpreter.GetSymbolicExpressionTreeValues(t, ds, rows); 182 trees.Add(t); 183 treeOutput.Add(output.ToArray()); 216 if (!output.Any(x => double.IsInfinity(x) || double.IsNaN(x))) { 217 trees.Add(t); 218 treeOutput.Add(output.ToArray()); 219 } 184 220 } 185 221 // enable constants again 186 222 constSym.InitialFrequency = oldConstFreq; 223 this.trees = trees; 224 this.treeOutput = treeOutput; 187 225 } 188 226 -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs
r7677 r8206 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 59 60 private static double[] cache; 60 61 61 protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, IOnlineCalculator calculator, int maxRows) { 62 protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, 63 double lowerEstimationLimit, double upperEstimationLimit, 64 IOnlineCalculator calculator, int maxRows) { 62 65 if (cache == null || cache.GetLength(0) < maxRows) { 63 66 cache = new double[maxRows]; … … 83 86 //calculate the quality by using the passed online calculator 84 87 targetValuesEnumerator = targetValues.GetEnumerator(); 85 i = 0; 86 while (targetValuesEnumerator.MoveNext()) { 87 double target = targetValuesEnumerator.Current; 88 double estimated = cache[i] * beta + alpha; 89 calculator.Add(target, estimated); 90 i++; 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); 91 93 } 92 94 } -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs
r7677 r8206 56 56 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 57 57 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 58 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);59 58 OnlineCalculatorError errorState; 60 59 … … 62 61 if (applyLinearScaling) { 63 62 var maeCalculator = new OnlineMaxAbsoluteErrorCalculator(); 64 CalculateWithScaling(targetValues, boundedEstimatedValues, maeCalculator, problemData.Dataset.Rows);63 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows); 65 64 errorState = maeCalculator.ErrorState; 66 65 mse = maeCalculator.MaxAbsoluteError; 67 } else 66 } else { 67 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 68 68 mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 69 } 70 70 if (errorState != OnlineCalculatorError.None) return Double.NaN; 71 71 else return mse; -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs
r7677 r8206 56 56 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 57 57 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 58 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);59 58 OnlineCalculatorError errorState; 60 59 … … 62 61 if (applyLinearScaling) { 63 62 var maeCalculator = new OnlineMeanAbsoluteErrorCalculator(); 64 CalculateWithScaling(targetValues, boundedEstimatedValues, maeCalculator, problemData.Dataset.Rows);63 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows); 65 64 errorState = maeCalculator.ErrorState; 66 65 mse = maeCalculator.MeanAbsoluteError; 67 } else 66 } else { 67 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, 68 upperEstimationLimit); 68 69 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 70 } 70 71 if (errorState != OnlineCalculatorError.None) return Double.NaN; 71 72 else return mse; -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs
r7677 r8206 56 56 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 57 57 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 58 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);59 58 OnlineCalculatorError errorState; 60 59 … … 62 61 if (applyLinearScaling) { 63 62 var mseCalculator = new OnlineMeanSquaredErrorCalculator(); 64 CalculateWithScaling(targetValues, boundedEstimatedValues, mseCalculator, problemData.Dataset.Rows);63 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows); 65 64 errorState = mseCalculator.ErrorState; 66 65 mse = mseCalculator.MeanSquaredError; 67 } else 66 } else { 67 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 68 68 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 69 } 70 70 if (errorState != OnlineCalculatorError.None) return Double.NaN; 71 71 else return mse; -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs
r8085 r8206 49 49 [StorableConstructor] 50 50 protected SymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { } 51 protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { } 51 protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner) 52 : base(original, cloner) { 53 RegisterEventHandlers(); 54 } 52 55 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); } 53 56 … … 62 65 MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength; 63 66 64 SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols(); 65 67 RegisterEventHandlers(); 66 68 ConfigureGrammarSymbols(); 67 69 InitializeOperators(); 68 70 UpdateEstimationLimits(); 71 } 72 73 [StorableHook(HookType.AfterDeserialization)] 74 private void AfterDeserialization() { 75 RegisterEventHandlers(); 76 // compatibility 77 bool changed = false; 78 if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) { 79 Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer()); 80 changed = true; 81 } 82 if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) { 83 Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer()); 84 changed = true; 85 } 86 if (changed) { 87 ParameterizeOperators(); 88 } 89 } 90 91 private void RegisterEventHandlers() { 92 SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols(); 69 93 } 70 94 … … 85 109 86 110 private void UpdateEstimationLimits() { 87 if (ProblemData.TrainingIndi zes.Any()) {88 var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndi zes).ToList();111 if (ProblemData.TrainingIndices.Any()) { 112 var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList(); 89 113 var mean = targetValues.Average(); 90 114 var range = targetValues.Max() - targetValues.Min(); -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs
r7726 r8206 33 33 [Item("SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer", "An operator that collects the training Pareto-best symbolic regression solutions for single objective symbolic regression problems.")] 34 34 [StorableClass] 35 public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<ISymbolicRegressionSolution>, 36 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator { 37 private const string ProblemDataParameterName = "ProblemData"; 38 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 private const string EstimationLimitsParameterName = "EstimationLimits"; 35 public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IRegressionProblemData, ISymbolicRegressionSolution> { 40 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 41 37 #region parameter properties 42 public ILookupParameter<IRegressionProblemData> ProblemDataParameter {43 get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }44 }45 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {46 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }47 }48 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {49 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }50 }51 38 public IValueParameter<BoolValue> ApplyLinearScalingParameter { 52 39 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } … … 65 52 public SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer() 66 53 : base() { 67 Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution."));68 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));69 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));70 54 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 71 55 } -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs
r7734 r8206 33 33 [Item("SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer", "An operator that collects the validation Pareto-best symbolic regression solutions for single objective symbolic regression problems.")] 34 34 [StorableClass] 35 public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData>, 36 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator { 37 private const string EstimationLimitsParameterName = "EstimationLimits"; 35 public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData> { 38 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 39 37 #region parameter properties 40 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {41 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }42 }43 38 public IValueParameter<BoolValue> ApplyLinearScalingParameter { 44 39 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } … … 57 52 public SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer() 58 53 : base() { 59 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));60 54 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 61 55 } -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs
r8085 r8206 73 73 var dataset = problemData.Dataset; 74 74 var targetVariable = problemData.TargetVariable; 75 var rows = problemData.TrainingIndi zes;75 var rows = problemData.TrainingIndices; 76 76 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows); 77 77 var targetValues = dataset.GetDoubleValues(targetVariable, rows); -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionMoveEvaluator.cs
r7832 r8206 81 81 // clone the move and all contained objects to make sure that the items in the scope are untouched 82 82 var move = (ChangeNodeTypeMove)MoveParameter.ActualValue.Clone(); 83 var t = SymbolicExpressionTreeParameter.ActualValue;84 83 var oldNode = move.Parent.GetSubtree(move.SubtreeIndex); 85 84 var children = new List<ISymbolicExpressionTreeNode>(oldNode.Subtrees);
Note: See TracChangeset
for help on using the changeset viewer.