Changeset 13773 for branches/HeuristicLab.VariableInteractionNetworks
- Timestamp:
- 04/18/16 13:35:50 (9 years ago)
- Location:
- branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.Designer.cs
r13727 r13773 26 26 this.components = new System.ComponentModel.Container(); 27 27 this.settingsGroupBox = new System.Windows.Forms.GroupBox(); 28 this.onlineImpactCalculationButton = new System.Windows.Forms.Button(); 29 this.maximizationCheckBox = new System.Windows.Forms.CheckBox(); 30 this.qualityResultNameComboBox = new System.Windows.Forms.ComboBox(); 31 this.qualityResultNameLabel = new System.Windows.Forms.Label(); 28 32 this.impactThresholdTextBox = new System.Windows.Forms.TextBox(); 29 33 this.impactAggregationComboBox = new System.Windows.Forms.ComboBox(); … … 38 42 this.label3 = new System.Windows.Forms.Label(); 39 43 this.errorProvider = new System.Windows.Forms.ErrorProvider(this.components); 40 this.qualityResultNameLabel = new System.Windows.Forms.Label();41 this.qualityResultNameComboBox = new System.Windows.Forms.ComboBox();42 this.maximizationCheckBox = new System.Windows.Forms.CheckBox();43 44 this.graphChart = new HeuristicLab.VariableInteractionNetworks.Views.DirectedGraphChart(); 44 45 this.settingsGroupBox.SuspendLayout(); … … 51 52 this.settingsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 52 53 | System.Windows.Forms.AnchorStyles.Left))); 54 this.settingsGroupBox.Controls.Add(this.onlineImpactCalculationButton); 53 55 this.settingsGroupBox.Controls.Add(this.maximizationCheckBox); 54 56 this.settingsGroupBox.Controls.Add(this.qualityResultNameComboBox); … … 67 69 this.settingsGroupBox.Text = "Network Configuration"; 68 70 // 71 // onlineImpactCalculationButton 72 // 73 this.onlineImpactCalculationButton.Location = new System.Drawing.Point(10, 201); 74 this.onlineImpactCalculationButton.Name = "onlineImpactCalculationButton"; 75 this.onlineImpactCalculationButton.Size = new System.Drawing.Size(149, 23); 76 this.onlineImpactCalculationButton.TabIndex = 6; 77 this.onlineImpactCalculationButton.Text = "Online Impact Calculation"; 78 this.onlineImpactCalculationButton.UseVisualStyleBackColor = true; 79 this.onlineImpactCalculationButton.Click += new System.EventHandler(this.onlineImpactCalculationButton_Click); 80 // 81 // maximizationCheckBox 82 // 83 this.maximizationCheckBox.AutoSize = true; 84 this.maximizationCheckBox.Location = new System.Drawing.Point(236, 44); 85 this.maximizationCheckBox.Name = "maximizationCheckBox"; 86 this.maximizationCheckBox.Size = new System.Drawing.Size(86, 17); 87 this.maximizationCheckBox.TabIndex = 5; 88 this.maximizationCheckBox.Text = "Maximization"; 89 this.maximizationCheckBox.UseVisualStyleBackColor = true; 90 this.maximizationCheckBox.CheckedChanged += new System.EventHandler(this.NetworkConfigurationChanged); 91 // 92 // qualityResultNameComboBox 93 // 94 this.qualityResultNameComboBox.FormattingEnabled = true; 95 this.qualityResultNameComboBox.Location = new System.Drawing.Point(121, 17); 96 this.qualityResultNameComboBox.Name = "qualityResultNameComboBox"; 97 this.qualityResultNameComboBox.Size = new System.Drawing.Size(201, 21); 98 this.qualityResultNameComboBox.TabIndex = 4; 99 this.qualityResultNameComboBox.SelectedIndexChanged += new System.EventHandler(this.NetworkConfigurationChanged); 100 // 101 // qualityResultNameLabel 102 // 103 this.qualityResultNameLabel.AutoSize = true; 104 this.qualityResultNameLabel.Location = new System.Drawing.Point(7, 20); 105 this.qualityResultNameLabel.Name = "qualityResultNameLabel"; 106 this.qualityResultNameLabel.Size = new System.Drawing.Size(96, 13); 107 this.qualityResultNameLabel.TabIndex = 3; 108 this.qualityResultNameLabel.Text = "Quality result name"; 109 // 69 110 // impactThresholdTextBox 70 111 // … … 75 116 this.impactThresholdTextBox.Text = "0.2"; 76 117 this.impactThresholdTextBox.Validating += new System.ComponentModel.CancelEventHandler(this.TextBoxValidating); 77 this.impactThresholdTextBox.Validated += new System.EventHandler(this. NetworkConfigurationBoxValidated);118 this.impactThresholdTextBox.Validated += new System.EventHandler(this.ImpactThresholdTextBoxValidated); 78 119 // 79 120 // impactAggregationComboBox … … 186 227 // 187 228 this.errorProvider.ContainerControl = this; 188 //189 // qualityResultNameLabel190 //191 this.qualityResultNameLabel.AutoSize = true;192 this.qualityResultNameLabel.Location = new System.Drawing.Point(7, 20);193 this.qualityResultNameLabel.Name = "qualityResultNameLabel";194 this.qualityResultNameLabel.Size = new System.Drawing.Size(96, 13);195 this.qualityResultNameLabel.TabIndex = 3;196 this.qualityResultNameLabel.Text = "Quality result name";197 //198 // qualityResultNameComboBox199 //200 this.qualityResultNameComboBox.FormattingEnabled = true;201 this.qualityResultNameComboBox.Location = new System.Drawing.Point(121, 17);202 this.qualityResultNameComboBox.Name = "qualityResultNameComboBox";203 this.qualityResultNameComboBox.Size = new System.Drawing.Size(201, 21);204 this.qualityResultNameComboBox.TabIndex = 4;205 this.qualityResultNameComboBox.SelectedIndexChanged += new System.EventHandler(this.NetworkConfigurationChanged);206 //207 // maximizationCheckBox208 //209 this.maximizationCheckBox.AutoSize = true;210 this.maximizationCheckBox.Location = new System.Drawing.Point(236, 44);211 this.maximizationCheckBox.Name = "maximizationCheckBox";212 this.maximizationCheckBox.Size = new System.Drawing.Size(86, 17);213 this.maximizationCheckBox.TabIndex = 5;214 this.maximizationCheckBox.Text = "Maximization";215 this.maximizationCheckBox.UseVisualStyleBackColor = true;216 this.maximizationCheckBox.CheckedChanged += new System.EventHandler(this.NetworkConfigurationChanged);217 229 // 218 230 // graphChart … … 232 244 this.graphChart.ShowToolBar = true; 233 245 this.graphChart.Size = new System.Drawing.Size(411, 458); 246 this.graphChart.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 234 247 this.graphChart.TabIndex = 0; 235 248 // … … 269 282 private System.Windows.Forms.ComboBox qualityResultNameComboBox; 270 283 private System.Windows.Forms.CheckBox maximizationCheckBox; 284 private System.Windows.Forms.Button onlineImpactCalculationButton; 271 285 } 272 286 } -
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.cs
r13727 r13773 35 35 using HeuristicLab.Problems.DataAnalysis; 36 36 using HeuristicLab.Visualization; 37 using Ellipse = HeuristicLab.Visualization.Ellipse; 37 38 using Rectangle = HeuristicLab.Visualization.Rectangle; 38 39 … … 56 57 } 57 58 58 private static VariableInteractionNetwork BuildNetworkFromSolutionQualities(RunCollection runs, double threshold, bool useBestRunsPerTarget = false) { 59 var nodes = new Dictionary<string, IVertex>(); 60 var vn = new VariableInteractionNetwork(); 59 private VariableInteractionNetwork variableInteractionNetwork; 60 61 private static void AssertSameProblemData(RunCollection runs) { 62 IDataset dataset = null; 63 IRegressionProblemData problemData = null; 64 foreach (var run in runs) { 65 var solution = (IRegressionSolution)run.Results.Values.Single(x => x is IRegressionSolution); 66 var ds = solution.ProblemData.Dataset; 67 68 if (solution.ProblemData == problemData) continue; 69 if (ds == dataset) continue; 70 if (problemData == null) { 71 problemData = solution.ProblemData; 72 continue; 73 } 74 if (dataset == null) { 75 dataset = ds; 76 continue; 77 } 78 79 if (problemData.TrainingPartition.Start != solution.ProblemData.TrainingPartition.Start || problemData.TrainingPartition.End != solution.ProblemData.TrainingPartition.End) 80 throw new InvalidOperationException("The runs must share the same data."); 81 82 if (!ds.DoubleVariables.SequenceEqual(dataset.DoubleVariables)) 83 throw new InvalidOperationException("The runs must share the same data."); 84 85 foreach (var v in ds.DoubleVariables) { 86 var values1 = (IList<double>)ds.GetReadOnlyDoubleValues(v); 87 var values2 = (IList<double>)dataset.GetReadOnlyDoubleValues(v); 88 89 if (values1.Count != values2.Count) 90 throw new InvalidOperationException("The runs must share the same data."); 91 92 if (!values1.SequenceEqual(values2)) 93 throw new InvalidOperationException("The runs must share the same data."); 94 } 95 } 96 } 97 98 private static RegressionEnsembleSolution CreateEnsembleSolution(IEnumerable<IRun> runs) { 99 var solutions = runs.Select(x => x.Results.Values.Single(v => v is IRegressionSolution)).Cast<IRegressionSolution>(); 100 return new RegressionEnsembleSolution(new RegressionEnsembleModel(solutions.Select(x => x.Model)), solutions.First().ProblemData); 101 } 102 103 public static Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> CalculateVariableImpactsOnline(RunCollection runs, bool useBest) { 104 AssertSameProblemData(runs); 105 var solution = (IRegressionSolution)runs.First().Results.Values.Single(x => x is IRegressionSolution); 106 var dataset = (Dataset)solution.ProblemData.Dataset; 107 var originalValues = dataset.DoubleVariables.ToDictionary(x => x, x => dataset.GetReadOnlyDoubleValues(x).ToList()); 108 var md = dataset.ToModifiable(); 109 var medians = new Dictionary<string, List<double>>(); 110 foreach (var v in dataset.DoubleVariables) { 111 var median = dataset.GetDoubleValues(v, solution.ProblemData.TrainingIndices).Median(); 112 medians[v] = Enumerable.Repeat(median, originalValues[v].Count).ToList(); 113 } 114 115 var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>(); 116 117 if (useBest) { 118 // build network using only the best run for each target 119 } else { 120 var groups = runs.GroupBy(run => { 121 var sol = (IRegressionSolution)run.Results.Values.Single(x => x is IRegressionSolution); 122 return Concatenate(sol.ProblemData.AllowedInputVariables) + sol.ProblemData.TargetVariable; 123 }); 124 125 foreach (var group in groups) { 126 // calculate average impacts 127 var averageImpacts = new Dictionary<string, double>(); 128 solution = (IRegressionSolution)group.First().Results.Values.Single(x => x is IRegressionSolution); 129 foreach (var run in group) { 130 var sol = (IRegressionSolution)run.Results.Values.Single(v => v is IRegressionSolution); 131 132 DoubleLimit estimationLimits = null; 133 if (run.Parameters.ContainsKey("EstimationLimits")) { 134 estimationLimits = (DoubleLimit)run.Parameters["EstimationLimits"]; 135 } 136 var impacts = CalculateImpacts(sol, md, originalValues, medians, estimationLimits); 137 // var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(sol).ToDictionary(x => x.Item1, x => x.Item2); 138 foreach (var pair in impacts) { 139 if (averageImpacts.ContainsKey(pair.Key)) 140 averageImpacts[pair.Key] += pair.Value; 141 else { 142 averageImpacts[pair.Key] = pair.Value; 143 } 144 } 145 } 146 var count = group.Count(); 147 var keys = averageImpacts.Keys.ToList(); 148 foreach (var v in keys) { 149 averageImpacts[v] /= count; 150 } 151 152 targetImpacts[solution.ProblemData.TargetVariable] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(group, averageImpacts); 153 } 154 } 155 return targetImpacts; 156 } 157 158 private static Dictionary<string, double> CalculateImpacts(IRegressionSolution solution, ModifiableDataset dataset, 159 Dictionary<string, List<double>> originalValues, Dictionary<string, List<double>> medianValues, DoubleLimit estimationLimits = null) { 160 var impacts = new Dictionary<string, double>(); 161 162 var model = solution.Model; 163 var pd = solution.ProblemData; 164 165 var rows = pd.TrainingIndices.ToList(); 166 var targetValues = pd.Dataset.GetDoubleValues(pd.TargetVariable, rows).ToList(); 167 168 169 foreach (var v in pd.AllowedInputVariables) { 170 dataset.ReplaceVariable(v, medianValues[v]); 171 172 var estimatedValues = model.GetEstimatedValues(dataset, rows); 173 if (estimationLimits != null) 174 estimatedValues = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper); 175 176 OnlineCalculatorError error; 177 var r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out error); 178 var newQuality = error == OnlineCalculatorError.None ? r * r : double.NaN; 179 var originalQuality = solution.TrainingRSquared; 180 impacts[v] = originalQuality - newQuality; 181 182 dataset.ReplaceVariable(v, originalValues[v]); 183 } 184 return impacts; 185 } 186 187 private static Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> CalculateVariableImpactsFromRunResults(RunCollection runs, 188 string qualityResultName, bool maximization, string impactsResultName, bool useBestRunsPerTarget = false) { 61 189 var targets = runs.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 62 var targetQualities = new Dictionary<string, double>(); 63 var targetInputs = new Dictionary<string, List<string>>(); 64 190 var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>(); 65 191 if (useBestRunsPerTarget) { 66 foreach (var target in targets) { 67 var bestRun = target.OrderBy(x => ((DoubleValue)x.Results["Best training solution quality"]).Value).First(); 68 var bestQuality = ((DoubleValue)bestRun.Results["Best training solution quality"]).Value; 69 var pd = (IRegressionProblemData)bestRun.Parameters["ProblemData"]; 70 if (threshold > bestQuality) continue; // skip if quality is below the treshold 71 targetQualities[target.Key] = bestQuality; 72 targetInputs[target.Key] = pd.AllowedInputVariables.ToList(); 73 } 74 } else { 75 foreach (var target in targets) { 76 var avgQuality = CalculateAverageQuality(new RunCollection(target)); 77 if (threshold > avgQuality) continue; 78 targetQualities[target.Key] = avgQuality; 79 var pd = (IRegressionProblemData)target.First().Parameters["ProblemData"]; 80 targetInputs[target.Key] = pd.AllowedInputVariables.ToList(); 81 } 82 } 83 84 foreach (var ti in targetQualities) { 85 var target = ti.Key; 86 var variables = targetInputs[ti.Key]; 87 var quality = ti.Value; 88 IVertex targetNode; 89 90 if (!nodes.TryGetValue(target, out targetNode)) { 91 targetNode = new VariableNetworkNode { Label = target }; 92 vn.AddVertex(targetNode); 93 nodes[target] = targetNode; 94 } 95 96 IVertex variableNode; 97 if (variables.Count > 0) { 98 var variableList = new List<string>(variables); 99 variableList.Add(target); 100 var junctionLabel = Concatenate(variableList); 101 IVertex junctionNode; 102 if (!nodes.TryGetValue(junctionLabel, out junctionNode)) { 103 junctionNode = new JunctionNetworkNode { Label = string.Empty }; 104 vn.AddVertex(junctionNode); 105 nodes[junctionLabel] = junctionNode; 106 } 107 IArc arc; 108 foreach (var v in variables) { 109 var impact = quality; 110 if (!nodes.TryGetValue(v, out variableNode)) { 111 variableNode = new VariableNetworkNode { Label = v }; 112 vn.AddVertex(variableNode); 113 nodes[v] = variableNode; 114 } 115 arc = new Arc(variableNode, junctionNode) { Weight = impact }; 116 vn.AddArc(arc); 117 } 118 arc = new Arc(junctionNode, targetNode) { Weight = junctionNode.InArcs.Sum(x => x.Weight) }; 119 vn.AddArc(arc); 120 } else { 121 foreach (var v in variables) { 122 var impact = quality; 123 if (!nodes.TryGetValue(v, out variableNode)) { 124 variableNode = new VariableNetworkNode { Label = v }; 125 vn.AddVertex(variableNode); 126 nodes[v] = variableNode; 127 } 128 var arc = new Arc(variableNode, targetNode) { Weight = impact }; 129 vn.AddArc(arc); 130 } 131 } 132 } 133 134 return vn; 135 } 136 137 private static VariableInteractionNetwork BuildNetworkFromVariableImpacts(RunCollection runs, string qualityResultName, bool maximization, string impactsResultName, double threshold, bool useBestRunsPerTarget = false) { 138 var nodes = new Dictionary<string, IVertex>(); 139 var vn = new VariableInteractionNetwork(); 140 var targets = runs.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 141 142 var targetImpacts = new Dictionary<string, Dictionary<string, double>>(); 143 144 if (useBestRunsPerTarget) { 145 var bestRunsPerTarget = maximization ? 146 targets.Select(x => x.OrderBy(y => ((DoubleValue)y.Results[qualityResultName]).Value).Last()) : 147 targets.Select(x => x.OrderBy(y => ((DoubleValue)y.Results[qualityResultName]).Value).First()); 192 var bestRunsPerTarget = maximization 193 ? targets.Select(x => x.OrderBy(y => ((DoubleValue)y.Results[qualityResultName]).Value).Last()) 194 : targets.Select(x => x.OrderBy(y => ((DoubleValue)y.Results[qualityResultName]).Value).First()); 148 195 149 196 foreach (var run in bestRunsPerTarget) { … … 151 198 var target = pd.TargetVariable; 152 199 var impacts = (DoubleMatrix)run.Results[impactsResultName]; 153 targetImpacts[target] = impacts.RowNames.Select((x, i) => new { Name = x, Index = i }).ToDictionary(x => x.Name, x => impacts[x.Index, 0]);200 targetImpacts[target] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(new[] { run }, impacts.RowNames.Select((x, i) => new { Name = x, Index = i }).ToDictionary(x => x.Name, x => impacts[x.Index, 0])); 154 201 } 155 202 } else { 156 203 foreach (var target in targets) { 157 204 var averageImpacts = CalculateAverageImpacts(new RunCollection(target), impactsResultName); 158 targetImpacts[target.Key] = averageImpacts; 159 } 160 } 161 205 targetImpacts[target.Key] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(target, averageImpacts); 206 } 207 } 208 return targetImpacts; 209 } 210 211 private static VariableInteractionNetwork CreateNetwork(Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> targetImpacts) { 212 var nodes = new Dictionary<string, IVertex>(); 213 var vn = new VariableInteractionNetwork(); 162 214 foreach (var ti in targetImpacts) { 163 215 var target = ti.Key; 164 var variableImpacts = ti.Value; 216 var variableImpacts = ti.Value.Item2; 217 var targetRuns = ti.Value.Item1; 165 218 IVertex targetNode; 166 219 167 var variables = variableImpacts.Keys. Where(x => variableImpacts[x] >= threshold).ToList();220 var variables = variableImpacts.Keys.ToList(); 168 221 if (variables.Count == 0) continue; 169 222 … … 180 233 IVertex junctionNode; 181 234 if (!nodes.TryGetValue(junctionLabel, out junctionNode)) { 182 junctionNode = new JunctionNetworkNode { Label = string.Empty };235 junctionNode = new JunctionNetworkNode { Label = string.Empty, Data = CreateEnsembleSolution(targetRuns) }; 183 236 vn.AddVertex(junctionNode); 184 237 nodes[junctionLabel] = junctionNode; … … 315 368 } 316 369 317 private void NetworkConfigurationBoxValidated(object sender, EventArgs e) {370 private void ImpactThresholdTextBoxValidated(object sender, EventArgs e) { 318 371 var tb = (TextBox)sender; 319 372 errorProvider.SetError(tb, string.Empty); 320 NetworkConfigurationChanged(sender, e); 373 var network = ApplyThreshold(variableInteractionNetwork, double.Parse(tb.Text)); 374 graphChart.Graph = network; 375 } 376 377 private static VariableInteractionNetwork ApplyThreshold(VariableInteractionNetwork originalNetwork, double threshold) { 378 var arcs = originalNetwork.Arcs.Where(x => x.Weight >= threshold).ToList(); 379 if (!arcs.Any()) return originalNetwork; 380 var filteredNetwork = new VariableInteractionNetwork(); 381 var cloner = new Cloner(); 382 var vertices = arcs.SelectMany(x => new[] { x.Source, x.Target }).Select(cloner.Clone); // arcs are not cloned 383 filteredNetwork.AddVertices(vertices); 384 foreach (var arc in arcs) { 385 var source = cloner.Clone(arc.Source); 386 var target = cloner.Clone(arc.Target); 387 filteredNetwork.AddArc(source, target); 388 } 389 var unusedJunctions = filteredNetwork.Vertices.Where(x => x.InDegree == 0 && x is JunctionNetworkNode).ToList(); 390 filteredNetwork.RemoveVertices(unusedJunctions); 391 var orphanedNodes = filteredNetwork.Vertices.Where(x => x.Degree == 0).ToList(); 392 filteredNetwork.RemoveVertices(orphanedNodes); 393 return filteredNetwork; 321 394 } 322 395 … … 335 408 return; 336 409 var maximization = maximizationCheckBox.Checked; 337 var network = BuildNetworkFromVariableImpacts(Content, qualityResultName, maximization, impactsResultName, threshold, useBest); 338 if (network.Vertices.Any()) 339 graphChart.Graph = network; 410 var impacts = CalculateVariableImpactsFromRunResults(Content, qualityResultName, maximization, impactsResultName, useBest); 411 variableInteractionNetwork = CreateNetwork(impacts); 412 var network = ApplyThreshold(variableInteractionNetwork, threshold); 413 graphChart.Graph = network; 340 414 } 341 415 … … 362 436 graphChart.Draw(); 363 437 } 438 439 private void onlineImpactCalculationButton_Click(object sender, EventArgs args) { 440 var button = (Button)sender; 441 var worker = new BackgroundWorker(); 442 worker.DoWork += (o, e) => { 443 button.Enabled = false; 444 var impacts = CalculateVariableImpactsOnline(Content, false); 445 var network = CreateNetwork(impacts); 446 var threshold = double.Parse(impactThresholdTextBox.Text); 447 graphChart.Graph = ApplyThreshold(network, threshold); 448 }; 449 worker.RunWorkerCompleted += (o, e) => button.Enabled = true; 450 worker.RunWorkerAsync(); 451 } 364 452 #endregion 365 453 }
Note: See TracChangeset
for help on using the changeset viewer.