Changeset 18187
- Timestamp:
- 01/12/22 15:07:18 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
r18164 r18187 18 18 get => template; 19 19 set { 20 if (value == template) return; 21 20 22 template = value; 21 Tree = Parser.Parse(template); 23 var parsedTree = Parser.Parse(template); 24 if (applyLinearScaling) 25 parsedTree = AddLinearScalingTerms(parsedTree); 26 Tree = parsedTree; 22 27 OnChanged(); 23 28 } … … 25 30 26 31 [Storable] 27 private ISymbolicExpressionTree treeWithoutLinearScaling; 28 [Storable] 29 private ISymbolicExpressionTree treeWithLinearScaling; 32 private ISymbolicExpressionTree tree; 30 33 public ISymbolicExpressionTree Tree { 31 get => ApplyLinearScaling ? treeWithLinearScaling : treeWithoutLinearScaling;34 get => tree; 32 35 private set { 33 treeWithLinearScaling = AddLinearScalingTerms(value); 34 treeWithoutLinearScaling = value; 36 tree = value; 35 37 36 38 var newFunctions = GetSubFunctions(); 37 var oldFunctions = subFunctions?.Intersect(newFunctions) 39 var oldFunctions = subFunctions?.Intersect(newFunctions) 38 40 ?? Enumerable.Empty<SubFunction>(); 39 41 // adds new functions and keeps the old ones (if they match) … … 51 53 get => applyLinearScaling; 52 54 set { 55 if (value == applyLinearScaling) return; 56 53 57 applyLinearScaling = value; 58 if (applyLinearScaling) Tree = AddLinearScalingTerms(Tree); 59 else Tree = RemoveLinearScalingTerms(Tree); 60 54 61 OnChanged(); 55 62 } … … 75 82 76 83 protected StructureTemplate(StructureTemplate original, Cloner cloner) : base(original, cloner) { 77 this. Tree = cloner.Clone(original.Tree);78 this. Template = original.Template;79 this. ApplyLinearScaling = original.ApplyLinearScaling;84 this.tree = cloner.Clone(original.tree); 85 this.template = original.Template; 86 this.applyLinearScaling = original.ApplyLinearScaling; 80 87 this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList(); 81 88 RegisterEventHandlers(); … … 96 103 public void Reset() { 97 104 subFunctions = new List<SubFunction>(); 98 treeWithoutLinearScaling = null; 99 treeWithLinearScaling = null; 100 applyLinearScaling = false; 105 tree = null; 101 106 Template = "f(_)"; 102 OnChanged();103 107 } 104 108 … … 112 116 var existingSubFunction = subFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault(); 113 117 if (existingSubFunction != null) { 114 // an existing subFunction needsthe same signature115 if (!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))118 // an existing subFunction must have the same signature 119 if (!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments)) 116 120 throw new ArgumentException( 117 121 $"The sub-function '{existingSubFunction.Name}' has (at least two) different signatures " + … … 131 135 132 136 private void RegisterEventHandlers() { 133 foreach (var sf in SubFunctions) {137 foreach (var sf in SubFunctions) { 134 138 sf.Changed += OnSubFunctionChanged; 135 139 } 136 140 } 137 141 138 private ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) {142 private static ISymbolicExpressionTree AddLinearScalingTerms(ISymbolicExpressionTree tree) { 139 143 var clonedTree = (ISymbolicExpressionTree)tree.Clone(); 140 144 var startNode = clonedTree.Root.Subtrees.First(); 141 145 var template = startNode.Subtrees.First(); 142 146 143 var add = new Addition(); 144 var addNode = add.CreateTreeNode(); 145 146 var mul = new Multiplication(); 147 var mulNode = mul.CreateTreeNode(); 148 149 var offset = new Number(); 150 var offsetNode = (NumberTreeNode)offset.CreateTreeNode(); 151 offsetNode.Value = 0.0; 152 var scale = new Number(); 153 var scaleNode = (NumberTreeNode)scale.CreateTreeNode(); 154 scaleNode.Value = 1.0; 147 var addNode = new Addition().CreateTreeNode(); 148 var mulNode = new Multiplication().CreateTreeNode(); 149 var offsetNode = new NumberTreeNode(0.0); 150 var scaleNode = new NumberTreeNode(1.0); 155 151 156 152 addNode.AddSubtree(offsetNode); … … 164 160 } 165 161 162 private static ISymbolicExpressionTree RemoveLinearScalingTerms(ISymbolicExpressionTree tree) { 163 var clonedTree = (ISymbolicExpressionTree)tree.Clone(); 164 var startNode = clonedTree.Root.Subtrees.First(); 165 166 //check for scaling terms 167 var addNode = startNode.GetSubtree(0); 168 var offsetNode = addNode.GetSubtree(0); 169 var mulNode = addNode.GetSubtree(1); 170 var scaleNode = mulNode.GetSubtree(0); 171 var templateNode = mulNode.GetSubtree(1); 172 173 var error = false; 174 if (addNode.Symbol is not Addition) error = true; 175 if (mulNode.Symbol is not Multiplication) error = true; 176 if (offsetNode is not NumberTreeNode offset || offset.Value != 0.0) error = true; 177 if (scaleNode is not NumberTreeNode scale || scale.Value != 1.0) error = true; 178 if (error) throw new ArgumentException("Scaling terms cannot be found."); 179 180 startNode.RemoveSubtree(0); 181 startNode.AddSubtree(templateNode); 182 183 return clonedTree; 184 } 185 166 186 private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged(); 167 187 }
Note: See TracChangeset
for help on using the changeset viewer.