Changeset 7422
- Timestamp:
- 01/27/12 16:14:06 (13 years ago)
- Location:
- branches/HeuristicLab.TreeSimplifierView
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TreeSimplifierView/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4.csproj
r7411 r7422 217 217 </BootstrapperPackage> 218 218 </ItemGroup> 219 <ItemGroup>220 <EmbeddedResource Include="InteractiveSymbolicDataAnalysisSolutionSimplifierView.resx">221 <DependentUpon>InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs</DependentUpon>222 </EmbeddedResource>223 <EmbeddedResource Include="SymbolicExpressionTreeNodeChangeValueDialog.resx">224 <DependentUpon>SymbolicExpressionTreeNodeChangeValueDialog.cs</DependentUpon>225 </EmbeddedResource>226 </ItemGroup>227 219 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 228 220 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/HeuristicLab.TreeSimplifierView/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r7411 r7422 37 37 private Dictionary<ISymbolicExpressionTreeNode, double> originalValues; 38 38 private bool updateInProgress = false; 39 private VisualSymbolicExpressionTreeNode visualTreeNode; // need some kind of state for correctly handling events 39 private VisualSymbolicExpressionTreeNode visualTreeNode; // for correctly handling events when changing node values 40 private ISymbolicExpressionTree model; 40 41 41 42 public InteractiveSymbolicDataAnalysisSolutionSimplifierView() { … … 71 72 72 73 protected virtual void OnModelChanged() { 73 this.CalculateReplacementNodesAndNodeImpacts(); 74 model = (model == null) 75 ? (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone() 76 : Content.Model.SymbolicExpressionTree; 77 this.CalculateReplacementNodesAndNodeImpacts(model); 78 this.PaintModel(); 74 79 } 75 80 76 81 protected virtual void OnProblemDataChanged() { 77 82 this.CalculateReplacementNodesAndNodeImpacts(); 83 this.PaintModel(); 78 84 } 79 85 80 86 protected override void OnContentChanged() { 87 model = (model == null) 88 ? (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone() 89 : Content.Model.SymbolicExpressionTree; 81 90 base.OnContentChanged(); 82 this.CalculateReplacementNodesAndNodeImpacts(); 91 this.CalculateReplacementNodesAndNodeImpacts(model); 92 this.PaintModel(); 83 93 this.viewHost.Content = this.Content; 84 94 } 85 95 86 96 private void CalculateReplacementNodesAndNodeImpacts() { 87 if (Content != null && Content.Model != null && Content.ProblemData != null) { 88 var tree = Content.Model.SymbolicExpressionTree; 89 var replacementValues = CalculateReplacementValues(tree); 90 foreach (var pair in replacementValues) { 91 if (!(pair.Key is ConstantTreeNode)) { 92 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 93 } 94 } 95 nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree); 96 97 if (!updateInProgress) { 98 // automatically fold all branches with impact = 0 99 List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 100 foreach (var parent in nodeList) { 101 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) { 102 var child = parent.GetSubtree(subTreeIndex); 103 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) { 104 SwitchNodeWithReplacementNode(parent, subTreeIndex); 105 } 97 if (Content == null || Content.Model == null || Content.ProblemData == null) return; 98 //var tree = Content.Model.SymbolicExpressionTree; 99 var tree = model; 100 var replacementValues = CalculateReplacementValues(tree); 101 foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) { 102 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 103 } 104 nodeImpacts = CalculateImpactValues(tree); 105 106 if (!updateInProgress) { 107 // automatically fold all branches with impact = 0 108 List<ISymbolicExpressionTreeNode> nodeList = tree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 109 foreach (var parent in nodeList) { 110 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) { 111 var child = parent.GetSubtree(subTreeIndex); 112 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) { 113 SwitchNodeWithReplacementNode(parent, subTreeIndex); 106 114 } 107 115 } 108 116 } 109 110 // show only interesting part of solution 111 if (tree.Root.SubtreeCount > 1) 112 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs 113 else 114 this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB 115 this.PaintNodeImpacts(); 116 } 117 } 118 PaintModel(); 119 } 120 121 private void CalculateReplacementNodesAndNodeImpacts(ISymbolicExpressionTree tree) { 122 var replacementValues = CalculateReplacementValues(tree); 123 foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) { 124 replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value); 125 } 126 nodeImpacts = CalculateImpactValues(tree); 127 128 if (!updateInProgress) { 129 // automatically fold all branches with impact = 0 130 List<ISymbolicExpressionTreeNode> nodeList = tree.Root.GetSubtree(0).IterateNodesPrefix().ToList(); 131 foreach (var parent in nodeList) { 132 for (int subTreeIndex = 0; subTreeIndex < parent.SubtreeCount; subTreeIndex++) { 133 var child = parent.GetSubtree(subTreeIndex); 134 if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) { 135 SwitchNodeWithReplacementNode(parent, subTreeIndex); 136 } 137 } 138 } 139 } 140 PaintModel(); 141 } 142 143 private void PaintModel() { 144 // show only interesting part of solution 145 this.treeChart.Tree = model.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(model.Root) : new SymbolicExpressionTree(model.Root.GetSubtree(0).GetSubtree(0)); 146 this.PaintNodeImpacts(); 117 147 } 118 148 … … 132 162 private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) { 133 163 visualTreeNode = (VisualSymbolicExpressionTreeNode)sender; 134 var tree = Content.Model.SymbolicExpressionTree;164 var tree = model; 135 165 // check if the node value/weight has been altered 136 166 // if so, the first double click will return the node to its original value/weight … … 219 249 var value = double.Parse(textBox.Text); 220 250 var subTree = visualTreeNode.SymbolicExpressionTreeNode; 221 var tree = Content.Model.SymbolicExpressionTree;222 251 223 252 if (subTree.Symbol is Constant) … … 226 255 (subTree as VariableTreeNode).Weight = value; 227 256 228 // show only interesting part of solution 229 treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); 230 updateInProgress = true; 231 UpdateModel(tree); 232 updateInProgress = false; 257 PaintModel(); 233 258 } 234 259 … … 249 274 double max = impacts.Max(); 250 275 double min = impacts.Min(); 251 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {276 foreach (ISymbolicExpressionTreeNode treeNode in model.IterateNodesPostfix()) { 252 277 VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode); 253 278 bool flag1 = replacementNodes.ContainsKey(treeNode); … … 287 312 288 313 private void PaintCollapsedNodes() { 289 foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {314 foreach (ISymbolicExpressionTreeNode treeNode in model.IterateNodesPostfix()) { 290 315 bool flag1 = replacementNodes.ContainsKey(treeNode); 291 316 bool flag2 = originalValues.ContainsKey(treeNode); … … 300 325 private void btnSimplify_Click(object sender, EventArgs e) { 301 326 SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); 302 var simplifiedExpressionTree = simplifier.Simplify( Content.Model.SymbolicExpressionTree);327 var simplifiedExpressionTree = simplifier.Simplify(model); 303 328 UpdateModel(simplifiedExpressionTree); 304 329 } 305 330 331 protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e); 332 306 333 private void btnPrune_Click(object sender, EventArgs e) { 334 btnPrune.Enabled = false; 307 335 backgroundWorker1.RunWorkerAsync(); 308 336 } 309 337 338 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { 339 var worker = sender as BackgroundWorker; 340 PruneTree(worker); 341 } 342 343 private void PruneTree(BackgroundWorker worker) { 344 var tree = model; 345 // get all tree nodes 346 foreach (var node in GetNodesAtDepth(tree.Root, new Data.IntRange(2, tree.Depth))) { 347 if (worker.CancellationPending) 348 break; // pruning cancelled 349 if (!(node.Symbol is Constant) && nodeImpacts.ContainsKey(node) && nodeImpacts[node] < 0.001) { 350 SwitchNodeWithReplacementNode(node.Parent, node.Parent.IndexOfSubtree(node)); 351 nodeImpacts = CalculateImpactValues(tree); 352 // we do not refresh the replacementValues because they won't change when folding nodes with such low impacts (<0.001) 353 } 354 } 355 } 356 357 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 358 if (e.Cancelled) { 359 // The user canceled the operation. 360 } else if (e.Error != null) { 361 // There was an error during the operation. 362 // Error-handling 363 } else { 364 // The operation completed normally. We can update the model 365 UpdateModel(model); 366 } 367 btnPrune.Enabled = true; 368 } 369 370 #region helpers 310 371 private static IEnumerable<ISymbolicExpressionTreeNode> GetNodesAtDepth(ISymbolicExpressionTreeNode root, Data.IntRange range) { 311 372 var list = new List<Tuple<ISymbolicExpressionTreeNode, int>> { new Tuple<ISymbolicExpressionTreeNode, int>(root, 0) }; … … 328 389 } 329 390 } 330 331 protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e); 332 333 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { 334 BackgroundWorker worker = sender as BackgroundWorker; 335 PruneTree(worker); 336 337 } 338 339 private void PruneTree(BackgroundWorker worker) { 340 var tree = Content.Model.SymbolicExpressionTree; 341 // get all tree nodes 342 foreach (var node in GetNodesAtDepth(tree.Root, new Data.IntRange(2, tree.Depth))) { 343 if (worker.CancellationPending) 344 break; // pruning cancelled 345 if (!(node.Symbol is Constant) && nodeImpacts.ContainsKey(node) && nodeImpacts[node] < 0.001) { 346 SwitchNodeWithReplacementNode(node.Parent, node.Parent.IndexOfSubtree(node)); 347 CalculateReplacementNodesAndNodeImpacts(); 348 } 349 } 350 } 351 352 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 353 if (e.Cancelled) { 354 // The user canceled the operation. 355 } else if (e.Error != null) { 356 // There was an error during the operation. 357 // Error-handling 358 } else { 359 // The operation completed normally. We can update the model 360 UpdateModel(Content.Model.SymbolicExpressionTree); 361 } 362 } 391 #endregion 363 392 } 364 393 }
Note: See TracChangeset
for help on using the changeset viewer.