Changeset 18190 for branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 01/13/22 11:35:38 (3 years ago)
- Location:
- branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NMSESingleObjectiveConstraintsEvaluator.cs
r18183 r18190 287 287 var parameterEntries = parameters.ToArray(); // order of entries must be the same for x 288 288 289 // extract init al parameters289 // extract initial parameters 290 290 double[] c = (double[])initialParameters.Clone(); 291 291 … … 380 380 if (numberTreeNode != null) { 381 381 if (numberTreeNode.Parent.Symbol is Power 382 && numberTreeNode.Parent.GetSubtree(1) == numberTreeNode) continue; // exponents in powers are not optimiz ated (see TreeToAutoDiffTermConverter)382 && numberTreeNode.Parent.GetSubtree(1) == numberTreeNode) continue; // exponents in powers are not optimized (see TreeToAutoDiffTermConverter) 383 383 numberTreeNode.Value = parameters[i++]; 384 384 } else if (updateVariableWeights && variableTreeNodeBase != null) -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18184 r18190 1 using System; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 2 23 using System.Linq; 3 24 using HEAL.Attic; 4 25 using HeuristicLab.Common; 5 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 6 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 7 29 using HeuristicLab.Optimization; … … 24 46 private const string EstimationLimitsParameterName = "EstimationLimits"; 25 47 private const string BestTrainingSolutionParameterName = "Best Training Solution"; 48 private const string ApplyLinearScalingParameterName = "Apply Linear Scaling"; 49 private const string OptimizeParametersParameterName = "Optimize Parameters"; 26 50 27 51 private const string SymbolicExpressionTreeName = "SymbolicExpressionTree"; … … 42 66 public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter => (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; 43 67 public IResultParameter<ISymbolicRegressionSolution> BestTrainingSolutionParameter => (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestTrainingSolutionParameterName]; 68 69 public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter => (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; 70 public IFixedValueParameter<BoolValue> OptimizeParametersParameter => (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName]; 44 71 #endregion 45 72 … … 64 91 65 92 public DoubleLimit EstimationLimits => EstimationLimitsParameter.Value; 93 94 public bool ApplyLinearScaling { 95 get => ApplyLinearScalingParameter.Value.Value; 96 set => ApplyLinearScalingParameter.Value.Value = value; 97 } 98 99 public bool OptimizeParameters { 100 get => OptimizeParametersParameter.Value.Value; 101 set => OptimizeParametersParameter.Value.Value = value; 102 } 66 103 67 104 public override bool Maximization => false; … … 99 136 structureTemplate)); 100 137 138 Parameters.Add(new FixedValueParameter<BoolValue>( 139 ApplyLinearScalingParameterName, new BoolValue(true) 140 )); 141 142 Parameters.Add(new FixedValueParameter<BoolValue>( 143 OptimizeParametersParameterName, new BoolValue(true) 144 )); 145 101 146 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>( 102 147 InterpreterParameterName, 103 148 new SymbolicDataAnalysisExpressionTreeBatchInterpreter()) { Hidden = true }); 104 105 149 Parameters.Add(new FixedValueParameter<DoubleLimit>( 106 150 EstimationLimitsParameterName, 107 151 new DoubleLimit(double.NegativeInfinity, double.PositiveInfinity)) { Hidden = true }); 108 109 152 Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "") { Hidden = true }); 110 153 … … 116 159 117 160 RegisterEventHandlers(); 161 162 StructureTemplate.ApplyLinearScaling = ApplyLinearScaling; 118 163 StructureTemplate.Template = 119 164 "(" + … … 137 182 [StorableHook(HookType.AfterDeserialization)] 138 183 private void AfterDeserialization() { 184 if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) { 185 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, new BoolValue(StructureTemplate.ApplyLinearScaling))); 186 } 187 188 if (!Parameters.ContainsKey(OptimizeParametersParameterName)) { 189 Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeParametersParameterName, new BoolValue(false))); 190 } 191 139 192 RegisterEventHandlers(); 140 193 } … … 148 201 149 202 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 203 ApplyLinearScalingParameter.Value.ValueChanged += (o, e) => StructureTemplate.ApplyLinearScaling = ApplyLinearScaling; 204 OptimizeParametersParameter.Value.ValueChanged += (o, e) => { 205 if (OptimizeParameters) ApplyLinearScaling = true; 206 }; 207 150 208 } 151 209 … … 156 214 157 215 private void OnTemplateChanged(object sender, EventArgs args) { 216 ApplyLinearScaling = StructureTemplate.ApplyLinearScaling; 158 217 SetupEncoding(); 159 218 } … … 165 224 foreach (var subFunction in StructureTemplate.SubFunctions) { 166 225 subFunction.SetupVariables(ProblemData.AllowedInputVariables); 167 // toprevent the same encoding twice168 if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue; // duplicate subfunction226 // prevent the same encoding twice 227 if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue; 169 228 170 229 var encoding = new SymbolicExpressionTreeEncoding( … … 196 255 197 256 var tree = (ISymbolicExpressionTree)best[SymbolicExpressionTreeName]; 198 199 257 var model = new SymbolicRegressionModel(ProblemData.TargetVariable, tree, Interpreter); 200 258 var solution = model.CreateRegressionSolution(ProblemData); … … 227 285 } 228 286 229 private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {230 var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);231 var scalingNode = offsetNode.Subtrees.Where(x => !(x is NumberTreeNode)).First();232 233 var offsetNumberNode = (NumberTreeNode)offsetNode.Subtrees.Where(x => x is NumberTreeNode).First();234 var scalingNumberNode = (NumberTreeNode)scalingNode.Subtrees.Where(x => x is NumberTreeNode).First();235 236 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);237 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);238 239 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);240 if (error == OnlineCalculatorError.None) {241 offsetNumberNode.Value = a;242 scalingNumberNode.Value = b;243 }244 }245 246 287 private static ISymbolicExpressionTree BuildTree(ISymbolicExpressionTree template, Individual individual) { 247 288 var resolvedTree = (ISymbolicExpressionTree)template.Clone(); 248 249 289 // build main tree 250 290 foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) { … … 260 300 } 261 301 302 private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) { 303 var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0); 304 var scalingNode = offsetNode.Subtrees.Where(x => !(x is NumberTreeNode)).First(); 305 306 var offsetNumberNode = (NumberTreeNode)offsetNode.Subtrees.Where(x => x is NumberTreeNode).First(); 307 var scalingNumberNode = (NumberTreeNode)scalingNode.Subtrees.Where(x => x is NumberTreeNode).First(); 308 309 var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices); 310 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices); 311 312 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error); 313 if (error == OnlineCalculatorError.None) { 314 offsetNumberNode.Value = a; 315 scalingNumberNode.Value = b; 316 } 317 } 318 319 320 321 322 262 323 263 324 public void Load(IRegressionProblemData data) {
Note: See TracChangeset
for help on using the changeset viewer.