Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
- Timestamp:
- 03/18/19 17:24:30 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r12012 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 24 24 using System.Drawing; 25 25 using System.Linq; 26 using System.Threading.Tasks; 26 27 using System.Windows.Forms; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views; 31 using HeuristicLab.MainForm; 30 32 using HeuristicLab.MainForm.WindowsForms; 31 33 … … 36 38 private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts; 37 39 40 private readonly ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator; 41 42 private readonly IProgress progress = new Progress(); 43 38 44 private enum TreeState { Valid, Invalid } 39 45 private TreeState treeState; 40 46 41 protected InteractiveSymbolicDataAnalysisSolutionSimplifierView( ) {47 protected InteractiveSymbolicDataAnalysisSolutionSimplifierView(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator) { 42 48 InitializeComponent(); 43 49 foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); … … 45 51 nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>(); 46 52 this.Caption = "Interactive Solution Simplifier"; 53 this.impactCalculator = impactCalculator; 47 54 48 55 // initialize the tree modifier that will be used to perform edit operations over the tree … … 112 119 treeChart.Tree = tree; 113 120 treeChart.Repaint(); 114 bool valid = !tree.IterateNodesPostfix().Any(node => node.SubtreeCount < GetMinArity(node.Symbol) || node.SubtreeCount > node.Symbol.MaximumArity); 121 // check if all nodes have a legal arity 122 var nodes = tree.IterateNodesPostfix().ToList(); 123 bool valid = !nodes.Any(node => node.SubtreeCount < GetMinArity(node.Symbol) || node.SubtreeCount > node.Symbol.MaximumArity); 124 125 if (valid) { 126 // check if all variables are contained in the dataset 127 var variables = new HashSet<string>(Content.ProblemData.Dataset.DoubleVariables); 128 valid = nodes.OfType<VariableTreeNode>().All(x => variables.Contains(x.VariableName)); 129 } 130 115 131 if (valid) { 116 132 btnOptimizeConstants.Enabled = true; … … 136 152 Content.ProblemDataChanged += Content_Changed; 137 153 treeChart.Repainted += treeChart_Repainted; 154 MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(grpSimplify, progress); 138 155 } 139 156 protected override void DeregisterContentEvents() { … … 142 159 Content.ProblemDataChanged -= Content_Changed; 143 160 treeChart.Repainted -= treeChart_Repainted; 161 MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(grpSimplify, false); 144 162 } 145 163 … … 160 178 } 161 179 162 private void UpdateView() {180 private async void UpdateView() { 163 181 if (Content == null || Content.Model == null || Content.ProblemData == null) return; 164 182 var tree = Content.Model.SymbolicExpressionTree; 165 183 treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); 166 184 167 var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree); 185 progress.Start("Calculate Impact and Replacement Values ..."); 186 var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree)); 187 await Task.Delay(500); // wait for progressbar to finish animation 168 188 var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2); 169 189 foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) { … … 171 191 } 172 192 nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1); 193 progress.Finish(); 173 194 PaintNodeImpacts(); 174 195 } 175 196 176 protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree); 177 protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree); 178 protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree); 197 protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) { 198 var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>(); 199 foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 200 double impactValue, replacementValue, newQualityForImpactsCalculation; 201 impactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation); 202 double newProgressValue = progress.ProgressValue + 1.0 / (tree.Length - 2); 203 progress.ProgressValue = Math.Min(newProgressValue, 1); 204 impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue)); 205 } 206 return impactAndReplacementValues; 207 } 208 179 209 protected abstract void UpdateModel(ISymbolicExpressionTree tree); 210 211 protected virtual ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, IProgress progress) { 212 return tree; 213 } 180 214 181 215 private static ConstantTreeNode MakeConstantTreeNode(double value) { … … 259 293 260 294 private void btnSimplify_Click(object sender, EventArgs e) { 261 var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); 262 var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree); 295 var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree); 263 296 UpdateModel(simplifiedExpressionTree); 264 297 } 265 298 266 protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e); 299 private async void btnOptimizeConstants_Click(object sender, EventArgs e) { 300 progress.Start("Optimizing Constants ..."); 301 var tree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone(); 302 var newTree = await Task.Run(() => OptimizeConstants(tree, progress)); 303 await Task.Delay(500); // wait for progressbar to finish animation 304 UpdateModel(newTree); // UpdateModel calls Progress.Finish (via Content_Changed) 305 } 267 306 } 268 307 }
Note: See TracChangeset
for help on using the changeset viewer.