Changeset 18151
- Timestamp:
- 12/16/21 16:47:07 (3 years ago)
- Location:
- branches/3136_Structural_GP
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18146 r18151 80 80 var shapeConstraintProblemData = new ShapeConstrainedRegressionProblemData(problemData); 81 81 82 83 82 var targetInterval = shapeConstraintProblemData.VariableRanges.GetInterval(shapeConstraintProblemData.TargetVariable); 84 83 var estimationWidth = targetInterval.Width * 10; 85 84 86 87 85 var structureTemplate = new StructureTemplate(); 88 structureTemplate.Changed += OnTemplateChanged;89 86 90 87 var evaluators = new ItemSet<SymbolicRegressionSingleObjectiveEvaluator>( … … 100 97 ProblemDataParameterName, 101 98 shapeConstraintProblemData)); 102 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;103 99 104 100 Parameters.Add(new FixedValueParameter<StructureTemplate>( … … 123 119 Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer()); 124 120 121 RegisterEventHandlers(); 125 122 StructureTemplate.Template = 126 123 "(" + … … 132 129 133 130 public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, 134 Cloner cloner) : base(original, cloner) { } 131 Cloner cloner) : base(original, cloner) { 132 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 133 RegisterEventHandlers(); 134 } 135 135 136 136 [StorableConstructor] 137 137 protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { } 138 139 140 [StorableHook(HookType.AfterDeserialization)] 141 private void AfterDeserialization() { 142 RegisterEventHandlers(); 143 } 144 138 145 #endregion 139 146 … … 148 155 } 149 156 157 private void RegisterEventHandlers() { 158 if (StructureTemplate != null) { 159 StructureTemplate.Changed += OnTemplateChanged; 160 } 161 162 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 163 } 164 150 165 private void OnTemplateChanged(object sender, EventArgs args) { 151 166 SetupStructureTemplate(); … … 156 171 Encoding.Remove(e); 157 172 158 foreach (var f in StructureTemplate.SubFunctions .Values) {173 foreach (var f in StructureTemplate.SubFunctions) { 159 174 SetupVariables(f); 160 175 if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs
r18134 r18151 44 44 if(visualTreeNode != null) { 45 45 var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode; 46 if(subFunctionTreeNode != null && Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction)) 47 viewHost.Content = subFunction; 46 var selectedSubFunction = Content.SubFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault(); 47 if(subFunctionTreeNode != null && selectedSubFunction != null) 48 viewHost.Content = selectedSubFunction; 48 49 } 49 50 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
r18146 r18151 33 33 treeWithLinearScaling = AddLinearScalingTerms(value); 34 34 treeWithoutLinearScaling = value; 35 SubFunctions = GetSubFunctions();35 subFunctions = GetSubFunctions(); 36 36 } 37 37 } 38 38 39 39 [Storable] 40 public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } 40 private IList<SubFunction> subFunctions = new List<SubFunction>(); 41 public IEnumerable<SubFunction> SubFunctions => subFunctions; 41 42 42 43 [Storable] … … 72 73 this.Template = original.Template; 73 74 this.ApplyLinearScaling = original.ApplyLinearScaling; 74 this.SubFunctions = original.SubFunctions; 75 this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList(); 76 RegisterEventHandlers(); 77 } 78 79 80 [StorableHook(HookType.AfterDeserialization)] 81 private void AfterDeserialization() { 82 RegisterEventHandlers(); 75 83 } 76 84 #endregion … … 82 90 83 91 public void Reset() { 84 SubFunctions = new Dictionary<string,SubFunction>();92 subFunctions = new List<SubFunction>(); 85 93 treeWithoutLinearScaling = null; 86 94 treeWithLinearScaling = null; … … 90 98 } 91 99 92 private Dictionary<string,SubFunction> GetSubFunctions() {93 var subFunctions = new Dictionary<string,SubFunction>();100 private IList<SubFunction> GetSubFunctions() { 101 var subFunctions = new List<SubFunction>(); 94 102 foreach (var node in Tree.IterateNodesPrefix()) 95 103 if (node is SubFunctionTreeNode subFunctionTreeNode) { … … 97 105 throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2))."); 98 106 99 if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) { 100 if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments)) 107 var existingSubFunction = subFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault(); 108 if (existingSubFunction != null) { 109 // an existing subFunction needs the same signature 110 if(!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments)) 101 111 throw new ArgumentException( 102 $"The sub-function '{v.Name}' has (at least two) different signatures " + 103 $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)}))."); 112 $"The sub-function '{existingSubFunction.Name}' has (at least two) different signatures " + 113 $"({existingSubFunction.Name}({string.Join(",", existingSubFunction.Arguments)}) <> " + 114 $"{subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)}))."); 104 115 } else { 105 116 var subFunction = new SubFunction() { … … 108 119 }; 109 120 subFunction.Changed += OnSubFunctionChanged; 110 subFunctions.Add(subFunction .Name, subFunction);121 subFunctions.Add(subFunction); 111 122 } 112 123 } 113 124 return subFunctions; 125 } 126 127 private void RegisterEventHandlers() { 128 foreach(var sf in SubFunctions) { 129 sf.Changed += OnSubFunctionChanged; 130 } 114 131 } 115 132 -
branches/3136_Structural_GP/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r17958 r18151 471 471 <Compile Include="HeuristicLab-3.3\Samples\GAGroupingProblemSampleTest.cs" /> 472 472 <Compile Include="HeuristicLab-3.3\Samples\ShapeConstrainedRegressionSampleTest.cs" /> 473 <Compile Include="HeuristicLab-3.3\Samples\StructureTemplateRegressionSampleTest.cs" /> 473 474 <Compile Include="HeuristicLab-3.3\Samples\VnsOpSampleTest.cs" /> 474 475 <Compile Include="HeuristicLab-3.3\Samples\EsGriewankSampleTest.cs" />
Note: See TracChangeset
for help on using the changeset viewer.