Changeset 16141 for branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
- Timestamp:
- 09/14/18 11:47:37 (6 years ago)
- Location:
- branches/2817-BinPackingSpeedup
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2817-BinPackingSpeedup
- Property svn:mergeinfo changed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views
- Property svn:mergeinfo changed
-
branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs
r16140 r16141 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Threading; 25 26 using System.Threading.Tasks; 26 27 using HeuristicLab.Common; … … 32 33 [Content(typeof(IRegressionSolution))] 33 34 public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView { 35 private CancellationTokenSource cancellationToken = new CancellationTokenSource(); 34 36 private enum SortingCriteria { 35 37 ImpactValue, … … 37 39 VariableName 38 40 } 39 40 private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>(); 41 private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>(); 41 42 42 43 public new IRegressionSolution Content { … … 51 52 InitializeComponent(); 52 53 53 //Little workaround. If you fill the ComboBox-Items in the other partial class, the UI-Designer will moan. 54 this.sortByComboBox.Items.AddRange(Enum.GetValues(typeof(SortingCriteria)).Cast<object>().ToArray()); 54 //Set the default values 55 this.dataPartitionComboBox.SelectedIndex = 0; 56 this.replacementComboBox.SelectedIndex = 3; 57 this.factorVarReplComboBox.SelectedIndex = 0; 55 58 this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue; 56 57 this.dataPartitionComboBox.SelectedIndex = 0; 58 this.replacementComboBox.SelectedIndex = 0; 59 this.factorVarReplComboBox.SelectedIndex = 0; 60 } 61 62 #region events 59 } 60 63 61 protected override void RegisterContentEvents() { 64 62 base.RegisterContentEvents(); … … 86 84 variableImactsArrayView.Content = null; 87 85 } else { 88 UpdateVariableImpacts(); 89 } 86 UpdateVariableImpact(); 87 } 88 } 89 90 private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) { 91 cancellationToken.Cancel(); 90 92 } 91 93 92 94 93 95 private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) { 94 UpdateVariableImpact s();96 UpdateVariableImpact(); 95 97 } 96 98 97 99 private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) { 98 UpdateVariableImpact s();100 UpdateVariableImpact(); 99 101 } 100 102 … … 102 104 //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice) 103 105 ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged; 104 switch ((SortingCriteria)sortByComboBox.SelectedItem) { 105 case SortingCriteria.ImpactValue: 106 ascendingCheckBox.Checked = false; 107 break; 108 case SortingCriteria.Occurrence: 109 ascendingCheckBox.Checked = true; 110 break; 111 case SortingCriteria.VariableName: 112 ascendingCheckBox.Checked = true; 113 break; 114 default: 115 throw new NotImplementedException("Ordering for selected SortingCriteria not implemented"); 116 } 106 ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue; 117 107 ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged; 118 108 119 Update DataOrdering();109 UpdateOrdering(); 120 110 } 121 111 122 112 private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) { 123 UpdateDataOrdering(); 124 } 125 #endregion 126 127 #region Helper Methods 128 private void UpdateVariableImpacts() { 113 UpdateOrdering(); 114 } 115 116 117 private async void UpdateVariableImpact() { 118 IProgress progress; 119 120 //Check if the selection is valid 129 121 if (Content == null) { return; } 130 122 if (replacementComboBox.SelectedIndex < 0) { return; } … … 132 124 if (factorVarReplComboBox.SelectedIndex < 0) { return; } 133 125 134 variableImactsArrayView.Caption = Content.Name + " Variable Impacts"; 135 126 //Prepare arguments 136 127 var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm; 137 128 var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex]; … … 139 130 var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem; 140 131 141 Task.Factory.StartNew(() => { 142 try { 143 mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name); 144 145 //Remember the original ordering of the variables 146 var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod); 147 var problemData = Content.ProblemData; 148 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction)); 149 var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList(); 150 rawVariableImpacts.Clear(); 151 originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2)); 152 UpdateDataOrdering(); 153 } finally { 154 mainForm.RemoveOperationProgressFromView(this); 155 } 156 }); 132 variableImactsArrayView.Caption = Content.Name + " Variable Impacts"; 133 progress = mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name); 134 progress.ProgressValue = 0; 135 136 cancellationToken = new CancellationTokenSource(); 137 //Remember the original ordering of the variables 138 try { 139 var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod, 140 (i, s) => { 141 progress.ProgressValue = i; 142 progress.Status = s; 143 return cancellationToken.Token.IsCancellationRequested; 144 }), cancellationToken.Token); 145 146 if (cancellationToken.Token.IsCancellationRequested) { return; } 147 var problemData = Content.ProblemData; 148 var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction)); 149 var originalVariableOrdering = problemData.Dataset.VariableNames 150 .Where(v => inputvariables.Contains(v)) 151 .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v)) 152 .ToList(); 153 154 rawVariableImpacts.Clear(); 155 originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2))); 156 UpdateOrdering(); 157 } finally { 158 ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this); 159 } 157 160 } 158 161 … … 161 164 /// The default is "Descending" by "VariableImpact" (as in previous versions) 162 165 /// </summary> 163 private void Update DataOrdering() {166 private void UpdateOrdering() { 164 167 //Check if valid sortingCriteria is selected and data exists 165 168 if (sortByComboBox.SelectedIndex == -1) { return; } … … 170 173 bool ascending = ascendingCheckBox.Checked; 171 174 172 IEnumerable< KeyValuePair<string, double>> orderedEntries = null;175 IEnumerable<Tuple<string, double>> orderedEntries = null; 173 176 174 177 //Sort accordingly 175 178 switch (selectedItem) { 176 179 case SortingCriteria.ImpactValue: 177 orderedEntries = rawVariableImpacts.OrderBy(v => v. Value);180 orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2); 178 181 break; 179 182 case SortingCriteria.Occurrence: … … 181 184 break; 182 185 case SortingCriteria.VariableName: 183 orderedEntries = rawVariableImpacts.OrderBy(v => v. Key, new NaturalStringComparer());186 orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer()); 184 187 break; 185 188 default: … … 190 193 191 194 //Write the data back 192 var impactArray = new DoubleArray(orderedEntries.Select(i => i. Value).ToArray()) {193 ElementNames = orderedEntries.Select(i => i. Key)195 var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) { 196 ElementNames = orderedEntries.Select(i => i.Item1) 194 197 }; 195 variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 196 } 197 #endregion 198 199 //Could be, if the View was closed 200 if (!variableImactsArrayView.IsDisposed) { 201 variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly(); 202 } 203 } 198 204 } 199 205 }
Note: See TracChangeset
for help on using the changeset viewer.