Changeset 18194
- Timestamp:
- 01/14/22 08:33:46 (3 years ago)
- Location:
- branches/3136_Structural_GP
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r18192 r18194 220 220 <Private>False</Private> 221 221 </ProjectReference> 222 <ProjectReference Include="..\..\HeuristicLab.Encodings.RealVectorEncoding\3.3\HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj"> 223 <Project>{BB6D334A-4BB6-4674-9883-31A6EBB32CAB}</Project> 224 <Name>HeuristicLab.Encodings.RealVectorEncoding-3.3</Name> 225 </ProjectReference> 222 226 <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj"> 223 227 <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project> -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Plugin.cs.frame
r18158 r18194 34 34 [PluginDependency("HeuristicLab.Core", "3.3")] 35 35 [PluginDependency("HeuristicLab.Data", "3.3")] 36 [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")] 36 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 37 [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")] 37 38 [PluginDependency("HeuristicLab.Operators", "3.3")] 38 39 [PluginDependency("HeuristicLab.Optimization", "3.3")] -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18192 r18194 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.RealVectorEncoding; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 30 using HeuristicLab.Optimization; … … 48 49 49 50 private const string SymbolicExpressionTreeName = "SymbolicExpressionTree"; 51 private const string NumericParametersEncoding = "Numeric Parameters"; 50 52 51 53 private const string StructureTemplateDescriptionText = … … 188 190 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 189 191 ApplyLinearScalingParameter.Value.ValueChanged += (o, e) => StructureTemplate.ApplyLinearScaling = ApplyLinearScaling; 190 //OptimizeParametersParameter.Value.ValueChanged += (o, e) => {191 // if (OptimizeParameters) ApplyLinearScaling = true;192 //};193 194 192 } 195 193 … … 207 205 foreach (var e in Encoding.Encodings.ToArray()) 208 206 Encoding.Remove(e); 207 208 209 var templateNumberTreeNodes = StructureTemplate.Tree.IterateNodesPrefix().OfType<NumberTreeNode>(); 210 if (templateNumberTreeNodes.Any()) { 211 var templateParameterValues = templateNumberTreeNodes.Select(n => n.Value).ToArray(); 212 var encoding = new RealVectorEncoding(NumericParametersEncoding, templateParameterValues.Length); 213 214 var creator = encoding.Operators.OfType<NormalDistributedRealVectorCreator>().First(); 215 creator.MeanParameter.Value = new RealVector(templateParameterValues); 216 creator.SigmaParameter.Value = new DoubleArray(templateParameterValues.Length); 217 encoding.SolutionCreator = creator; 218 219 Encoding.Add(encoding); 220 } 209 221 210 222 foreach (var subFunction in StructureTemplate.SubFunctions) { … … 221 233 } 222 234 223 //set multi manipulator as default manipulator for all encoding parts 224 var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().FirstOrDefault(); 225 if (manipulator != null) { 226 foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) { 227 var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault(); 228 param.Value = m == null ? param.ValidValues.First() : m; 235 //set single point crossover for numeric parameters 236 var multiCrossover = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingCrossover>().First(); 237 foreach (var param in multiCrossover.Parameters.OfType<ConstrainedValueParameter<ICrossover>>()) { 238 var singlePointCrossover = param.ValidValues.OfType<SinglePointCrossover>().FirstOrDefault(); 239 param.Value = singlePointCrossover ?? param.ValidValues.First(); 240 } 241 242 //adapt crossover probability for subtree crossover 243 foreach (var param in multiCrossover.Parameters.OfType<ConstrainedValueParameter<ICrossover>>()) { 244 var subtreeCrossover = param.ValidValues.OfType<SubtreeCrossover>().FirstOrDefault(); 245 if (subtreeCrossover != null) { 246 subtreeCrossover.CrossoverProbability = 1.0 / Encoding.Encodings.OfType<SymbolicExpressionTreeEncoding>().Count(); 247 param.Value = subtreeCrossover; 229 248 } 249 } 250 251 //set multi manipulator as default manipulator for all symbolic expression tree encoding parts 252 var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().First(); 253 foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) { 254 var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault(); 255 param.Value = m ?? param.ValidValues.First(); 230 256 } 231 257 } … … 253 279 throw new ArgumentException("No structure template defined!"); 254 280 255 //create tree where all functions have been resolved (integrated) 256 var tree = BuildTree(templateTree, individual); 281 var tree = BuildTreeFromIndividual(templateTree, individual, updateNumericParameters: StructureTemplate.ContainsNumericParameters); 257 282 individual[SymbolicExpressionTreeName] = tree; 258 283 … … 262 287 LinearScaling.AdjustLinearScalingParams(ProblemData, tree, Interpreter); 263 288 } 289 290 UpdateIndividualFromTree(tree, individual, updateNumericParameters: StructureTemplate.ContainsNumericParameters); 264 291 265 292 //calculate NMSE … … 290 317 } 291 318 292 private static ISymbolicExpressionTree BuildTree (ISymbolicExpressionTree template, Individual individual) {319 private static ISymbolicExpressionTree BuildTreeFromIndividual(ISymbolicExpressionTree template, Individual individual, bool updateNumericParameters) { 293 320 var resolvedTree = (ISymbolicExpressionTree)template.Clone(); 321 322 //set numeric parameter values 323 if (updateNumericParameters) { 324 var realVector = individual.RealVector(NumericParametersEncoding); 325 var numberTreeNodes = resolvedTree.IterateNodesPrefix().OfType<NumberTreeNode>().ToArray(); 326 327 if (realVector.Length != numberTreeNodes.Length) 328 throw new InvalidOperationException("The number of numeric parameters in the tree does not match the provided numerical values."); 329 330 for (int i = 0; i < numberTreeNodes.Length; i++) 331 numberTreeNodes[i].Value = realVector[i]; 332 } 333 294 334 // build main tree 295 335 foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) { … … 305 345 } 306 346 347 private static void UpdateIndividualFromTree(ISymbolicExpressionTree tree, Individual individual, bool updateNumericParameters) { 348 var clonedTree = (ISymbolicExpressionTree)tree.Clone(); 349 350 foreach (var subFunctionTreeNode in clonedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) { 351 var grammar = ((ISymbolicExpressionTree)individual[subFunctionTreeNode.Name]).Root.Grammar; 352 var functionTreeNode = subFunctionTreeNode.GetSubtree(0); 353 //remove function code to make numeric parameters extraction easier 354 subFunctionTreeNode.RemoveSubtree(0); 355 356 357 var rootNode = (SymbolicExpressionTreeTopLevelNode)new ProgramRootSymbol().CreateTreeNode(); 358 rootNode.SetGrammar(grammar); 359 360 var startNode = (SymbolicExpressionTreeTopLevelNode)new StartSymbol().CreateTreeNode(); 361 startNode.SetGrammar(grammar); 362 363 rootNode.AddSubtree(startNode); 364 startNode.AddSubtree(functionTreeNode); 365 var functionTree = new SymbolicExpressionTree(rootNode); 366 individual[subFunctionTreeNode.Name] = functionTree; 367 } 368 369 //set numeric parameter values 370 if (updateNumericParameters) { 371 var realVector = individual.RealVector(NumericParametersEncoding); 372 var numberTreeNodes = clonedTree.IterateNodesPrefix().OfType<NumberTreeNode>().ToArray(); 373 374 if (realVector.Length != numberTreeNodes.Length) 375 throw new InvalidOperationException("The number of numeric parameters in the tree does not match the provided numerical values."); 376 377 for (int i = 0; i < numberTreeNodes.Length; i++) 378 realVector[i] = numberTreeNodes[i].Value; 379 } 380 } 381 307 382 public void Load(IRegressionProblemData data) { 308 383 ProblemData = data; -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
r18191 r18194 61 61 get => tree; 62 62 private set { 63 containsNumericParameters = null; 63 64 tree = value; 64 65 … … 68 69 // adds new functions and keeps the old ones (if they match) 69 70 subFunctions = newFunctions.Except(oldFunctions).Concat(oldFunctions).ToList(); 71 } 72 } 73 74 private bool? containsNumericParameters; 75 public bool ContainsNumericParameters { 76 get { 77 if (!containsNumericParameters.HasValue) 78 containsNumericParameters = Tree.IterateNodesPrefix().OfType<NumberTreeNode>().Any(); 79 80 return containsNumericParameters.Value; 70 81 } 71 82 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs
r18184 r18194 75 75 #region Event Handling 76 76 private void RegisterEventHandlers() { 77 GrammarParameter.ValueChanged += OnParameterValueChanged; 77 GrammarParameter.ValueChanged += (o, e) => { 78 if (Grammar is TypeCoherentExpressionGrammar tceg) tceg.ConfigureAsDefaultRegressionGrammar(); 79 OnParameterValueChanged(o, e); 80 }; 78 81 MaximumSymbolicExpressionTreeDepthParameter.Value.ValueChanged += OnParameterValueChanged; 79 82 MaximumSymbolicExpressionTreeLengthParameter.Value.ValueChanged += OnParameterValueChanged;
Note: See TracChangeset
for help on using the changeset viewer.