Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.cs @ 13821

Last change on this file since 13821 was 13821, checked in by bburlacu, 7 years ago

#2288: Used localization when formatting strings. Add canceling in CreateTargetVariationExperimentDialog. Remove use of C# 4.6 language features.

File size: 20.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Globalization;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Common;
31using HeuristicLab.Core;
32using HeuristicLab.Core.Views;
33using HeuristicLab.Data;
34using HeuristicLab.MainForm;
35using HeuristicLab.Optimization;
36using HeuristicLab.Problems.DataAnalysis;
37using HeuristicLab.Visualization;
38using Ellipse = HeuristicLab.Visualization.Ellipse;
39using Rectangle = HeuristicLab.Visualization.Rectangle;
40
41namespace HeuristicLab.VariableInteractionNetworks.Views {
42  [View("Variable Interaction Network")]
43  [Content(typeof(RunCollection), IsDefaultView = false)]
44
45  public sealed partial class RunCollectionVariableInteractionNetworkView : ItemView {
46    public RunCollectionVariableInteractionNetworkView() {
47      InitializeComponent();
48      ConfigureNodeShapes();
49    }
50
51    public new RunCollection Content {
52      get { return (RunCollection)base.Content; }
53      set {
54        if (value != null && value != Content) {
55          base.Content = value;
56        }
57      }
58    }
59
60    private VariableInteractionNetwork variableInteractionNetwork;
61
62    private static void AssertSameProblemData(RunCollection runs) {
63      IDataset dataset = null;
64      IRegressionProblemData problemData = null;
65      foreach (var run in runs) {
66        var solution = (IRegressionSolution)run.Results.Values.Single(x => x is IRegressionSolution);
67        var ds = solution.ProblemData.Dataset;
68
69        if (solution.ProblemData == problemData) continue;
70        if (ds == dataset) continue;
71        if (problemData == null) {
72          problemData = solution.ProblemData;
73          continue;
74        }
75        if (dataset == null) {
76          dataset = ds;
77          continue;
78        }
79
80        if (problemData.TrainingPartition.Start != solution.ProblemData.TrainingPartition.Start || problemData.TrainingPartition.End != solution.ProblemData.TrainingPartition.End)
81          throw new InvalidOperationException("The runs must share the same data.");
82
83        if (!ds.DoubleVariables.SequenceEqual(dataset.DoubleVariables))
84          throw new InvalidOperationException("The runs must share the same data.");
85
86        foreach (var v in ds.DoubleVariables) {
87          var values1 = (IList<double>)ds.GetReadOnlyDoubleValues(v);
88          var values2 = (IList<double>)dataset.GetReadOnlyDoubleValues(v);
89
90          if (values1.Count != values2.Count)
91            throw new InvalidOperationException("The runs must share the same data.");
92
93          if (!values1.SequenceEqual(values2))
94            throw new InvalidOperationException("The runs must share the same data.");
95        }
96      }
97    }
98
99    private static RegressionEnsembleSolution CreateEnsembleSolution(IEnumerable<IRun> runs) {
100      var solutions = runs.Select(x => x.Results.Values.Single(v => v is IRegressionSolution)).Cast<IRegressionSolution>();
101      return new RegressionEnsembleSolution(new RegressionEnsembleModel(solutions.Select(x => x.Model)), solutions.First().ProblemData);
102    }
103
104    public static Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> CalculateVariableImpactsOnline(RunCollection runs, bool useBest) {
105      AssertSameProblemData(runs);
106      var solution = (IRegressionSolution)runs.First().Results.Values.Single(x => x is IRegressionSolution);
107      var dataset = (Dataset)solution.ProblemData.Dataset;
108      var originalValues = dataset.DoubleVariables.ToDictionary(x => x, x => dataset.GetReadOnlyDoubleValues(x).ToList());
109      var md = dataset.ToModifiable();
110      var medians = new Dictionary<string, List<double>>();
111      foreach (var v in dataset.DoubleVariables) {
112        var median = dataset.GetDoubleValues(v, solution.ProblemData.TrainingIndices).Median();
113        medians[v] = Enumerable.Repeat(median, originalValues[v].Count).ToList();
114      }
115
116      var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>();
117
118      if (useBest) {
119        // build network using only the best run for each target
120      } else {
121        var groups = runs.GroupBy(run => {
122          var sol = (IRegressionSolution)run.Results.Values.Single(x => x is IRegressionSolution);
123          return Concatenate(sol.ProblemData.AllowedInputVariables) + sol.ProblemData.TargetVariable;
124        });
125
126        foreach (var group in groups) {
127          // calculate average impacts
128          var averageImpacts = new Dictionary<string, double>();
129          solution = (IRegressionSolution)group.First().Results.Values.Single(x => x is IRegressionSolution);
130          foreach (var run in group) {
131            var sol = (IRegressionSolution)run.Results.Values.Single(v => v is IRegressionSolution);
132
133            DoubleLimit estimationLimits = null;
134            if (run.Parameters.ContainsKey("EstimationLimits")) {
135              estimationLimits = (DoubleLimit)run.Parameters["EstimationLimits"];
136            }
137            var impacts = CalculateImpacts(sol, md, originalValues, medians, estimationLimits);
138            //            var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(sol).ToDictionary(x => x.Item1, x => x.Item2);
139            foreach (var pair in impacts) {
140              if (averageImpacts.ContainsKey(pair.Key))
141                averageImpacts[pair.Key] += pair.Value;
142              else {
143                averageImpacts[pair.Key] = pair.Value;
144              }
145            }
146          }
147          var count = group.Count();
148          var keys = averageImpacts.Keys.ToList();
149          foreach (var v in keys) {
150            averageImpacts[v] /= count;
151          }
152
153          targetImpacts[solution.ProblemData.TargetVariable] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(group, averageImpacts);
154        }
155      }
156      return targetImpacts;
157    }
158
159    private static Dictionary<string, double> CalculateImpacts(IRegressionSolution solution, ModifiableDataset dataset,
160      Dictionary<string, List<double>> originalValues, Dictionary<string, List<double>> medianValues, DoubleLimit estimationLimits = null) {
161      var impacts = new Dictionary<string, double>();
162
163      var model = solution.Model;
164      var pd = solution.ProblemData;
165
166      var rows = pd.TrainingIndices.ToList();
167      var targetValues = pd.Dataset.GetDoubleValues(pd.TargetVariable, rows).ToList();
168
169
170      foreach (var v in pd.AllowedInputVariables) {
171        dataset.ReplaceVariable(v, medianValues[v]);
172
173        var estimatedValues = model.GetEstimatedValues(dataset, rows);
174        if (estimationLimits != null)
175          estimatedValues = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
176
177        OnlineCalculatorError error;
178        var r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out error);
179        var newQuality = error == OnlineCalculatorError.None ? r * r : double.NaN;
180        var originalQuality = solution.TrainingRSquared;
181        impacts[v] = originalQuality - newQuality;
182
183        dataset.ReplaceVariable(v, originalValues[v]);
184      }
185      return impacts;
186    }
187
188    private static Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> CalculateVariableImpactsFromRunResults(RunCollection runs,
189      string qualityResultName, bool maximization, string impactsResultName, bool useBestRunsPerTarget = false) {
190      var targets = runs.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
191      var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>();
192      if (useBestRunsPerTarget) {
193        var bestRunsPerTarget = maximization
194          ? targets.Select(x => x.OrderBy(y => ((DoubleValue)y.Results[qualityResultName]).Value).Last())
195          : targets.Select(x => x.OrderBy(y => ((DoubleValue)y.Results[qualityResultName]).Value).First());
196
197        foreach (var run in bestRunsPerTarget) {
198          var pd = (IRegressionProblemData)run.Parameters["ProblemData"];
199          var target = pd.TargetVariable;
200          var impacts = (DoubleMatrix)run.Results[impactsResultName];
201          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]));
202        }
203      } else {
204        foreach (var target in targets) {
205          var averageImpacts = CalculateAverageImpacts(new RunCollection(target), impactsResultName);
206          targetImpacts[target.Key] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(target, averageImpacts);
207        }
208      }
209      return targetImpacts;
210    }
211
212    private static VariableInteractionNetwork CreateNetwork(Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> targetImpacts) {
213      var nodes = new Dictionary<string, IVertex>();
214      var vn = new VariableInteractionNetwork();
215      foreach (var ti in targetImpacts) {
216        var target = ti.Key;
217        var variableImpacts = ti.Value.Item2;
218        var targetRuns = ti.Value.Item1;
219        IVertex targetNode;
220
221        var variables = variableImpacts.Keys.ToList();
222        if (variables.Count == 0) continue;
223
224        if (!nodes.TryGetValue(target, out targetNode)) {
225          targetNode = new VariableNetworkNode { Label = target };
226          vn.AddVertex(targetNode);
227          nodes[target] = targetNode;
228        }
229
230        IVertex variableNode;
231        if (variables.Count > 1) {
232          var variableList = new List<string>(variables) { target };
233          var junctionLabel = Concatenate(variableList);
234          IVertex junctionNode;
235          if (!nodes.TryGetValue(junctionLabel, out junctionNode)) {
236            var solutionsEnsemble = CreateEnsembleSolution(targetRuns);
237            junctionNode = new JunctionNetworkNode { Label = string.Empty, Data = solutionsEnsemble };
238            vn.AddVertex(junctionNode);
239            nodes[junctionLabel] = junctionNode;
240            junctionNode.Label = solutionsEnsemble.TrainingRSquared.ToString("N3", CultureInfo.CurrentCulture);
241          }
242          IArc arc;
243          foreach (var v in variables) {
244            var impact = variableImpacts[v];
245            if (!nodes.TryGetValue(v, out variableNode)) {
246              variableNode = new VariableNetworkNode { Label = v };
247              vn.AddVertex(variableNode);
248              nodes[v] = variableNode;
249            }
250            arc = new Arc(variableNode, junctionNode) { Weight = impact, Label = impact.ToString("N3", CultureInfo.CurrentCulture) };
251            vn.AddArc(arc);
252          }
253          var trainingR2 = ((IRegressionSolution)((JunctionNetworkNode)junctionNode).Data).TrainingRSquared;
254          arc = new Arc(junctionNode, targetNode) { Weight = junctionNode.InArcs.Sum(x => x.Weight), Label = trainingR2.ToString("N3", CultureInfo.CurrentCulture) };
255          vn.AddArc(arc);
256        } else {
257          foreach (var v in variables) {
258            var impact = variableImpacts[v];
259            if (!nodes.TryGetValue(v, out variableNode)) {
260              variableNode = new VariableNetworkNode { Label = v };
261              vn.AddVertex(variableNode);
262              nodes[v] = variableNode;
263            }
264            var arc = new Arc(variableNode, targetNode) {
265              Weight = impact, Label = impact.ToString("N3", CultureInfo.CurrentCulture)
266            };
267            vn.AddArc(arc);
268          }
269        }
270      }
271      return vn;
272    }
273
274    private static VariableInteractionNetwork ApplyThreshold(VariableInteractionNetwork originalNetwork, double threshold) {
275      var arcs = originalNetwork.Arcs.Where(x => x.Weight >= threshold).ToList();
276      if (!arcs.Any()) return originalNetwork;
277      var filteredNetwork = new VariableInteractionNetwork();
278      var cloner = new Cloner();
279      var vertices = arcs.SelectMany(x => new[] { x.Source, x.Target }).Select(cloner.Clone).Distinct(); // arcs are not cloned
280      filteredNetwork.AddVertices(vertices);
281      filteredNetwork.AddArcs(arcs.Select(x => (IArc)x.Clone(cloner)));
282
283      var unusedJunctions = filteredNetwork.Vertices.Where(x => x.InDegree == 0 && x is JunctionNetworkNode).ToList();
284      filteredNetwork.RemoveVertices(unusedJunctions);
285      var orphanedNodes = filteredNetwork.Vertices.Where(x => x.Degree == 0).ToList();
286      filteredNetwork.RemoveVertices(orphanedNodes);
287      return filteredNetwork.Vertices.Any() ? filteredNetwork : originalNetwork;
288    }
289
290    private static double CalculateAverageQuality(RunCollection runs) {
291      var pd = (IRegressionProblemData)runs.First().Parameters["ProblemData"];
292      var target = pd.TargetVariable;
293      var inputs = pd.AllowedInputVariables;
294
295      if (!runs.All(x => {
296        var problemData = (IRegressionProblemData)x.Parameters["ProblemData"];
297        return target == problemData.TargetVariable && inputs.SequenceEqual(problemData.AllowedInputVariables);
298      })) {
299        throw new ArgumentException("All runs must have the same target and inputs.");
300      }
301      return runs.Average(x => ((DoubleValue)x.Results["Best training solution quality"]).Value);
302    }
303
304    private static Dictionary<string, double> CalculateAverageImpacts(RunCollection runs, string resultName) {
305      var pd = (IRegressionProblemData)runs.First().Parameters["ProblemData"];
306      var target = pd.TargetVariable;
307      var inputs = pd.AllowedInputVariables.ToList();
308
309      var impacts = inputs.ToDictionary(x => x, x => 0d);
310
311      // check if all the runs have the same target and same inputs
312      if (!runs.All(x => {
313        var problemData = (IRegressionProblemData)x.Parameters["ProblemData"];
314        return target == problemData.TargetVariable && inputs.SequenceEqual(problemData.AllowedInputVariables);
315      })) {
316        throw new ArgumentException("All runs must have the same target and inputs.");
317      }
318
319      foreach (var run in runs) {
320        var impactsMatrix = (DoubleMatrix)run.Results[resultName];
321
322        int i = 0;
323        foreach (var v in impactsMatrix.RowNames) {
324          impacts[v] += impactsMatrix[i, 0];
325          ++i;
326        }
327      }
328
329      foreach (var v in inputs) {
330        impacts[v] /= runs.Count;
331      }
332
333      return impacts;
334    }
335
336    private static string Concatenate(IEnumerable<string> strings) {
337      var sb = new StringBuilder();
338      foreach (var s in strings) {
339        sb.Append(s);
340      }
341      return sb.ToString();
342    }
343
344    private void ConfigureNodeShapes() {
345      graphChart.ClearShapes();
346      var font = new Font(FontFamily.GenericSansSerif, 12);
347      graphChart.AddShape(typeof(VariableNetworkNode), new LabeledPrimitive(new Ellipse(graphChart.Chart, new PointD(0, 0), new PointD(30, 30), Pens.Black, Brushes.White), "", font));
348      graphChart.AddShape(typeof(JunctionNetworkNode), new LabeledPrimitive(new Rectangle(graphChart.Chart, new PointD(0, 0), new PointD(15, 15), Pens.Black, Brushes.DarkGray), "", font));
349    }
350
351    #region events
352    protected override void OnContentChanged() {
353      base.OnContentChanged();
354      var run = Content.First();
355      var pd = (IRegressionProblemData)run.Parameters["ProblemData"];
356      var variables = new HashSet<string>(new List<string>(pd.Dataset.DoubleVariables));
357      impactResultNameComboBox.Items.Clear();
358      foreach (var result in run.Results.Where(x => x.Value is DoubleMatrix)) {
359        var m = (DoubleMatrix)result.Value;
360        if (m.RowNames.All(x => variables.Contains(x)))
361          impactResultNameComboBox.Items.Add(result.Key);
362      }
363      qualityResultNameComboBox.Items.Clear();
364      foreach (var result in run.Results.Where(x => x.Value is DoubleValue)) {
365        qualityResultNameComboBox.Items.Add(result.Key);
366      }
367      if (impactResultNameComboBox.Items.Count > 0) {
368        impactResultNameComboBox.Text = (string)impactResultNameComboBox.Items[0];
369      }
370      if (qualityResultNameComboBox.Items.Count > 0) {
371        qualityResultNameComboBox.Text = (string)qualityResultNameComboBox.Items[0];
372      }
373      if (impactResultNameComboBox.Items.Count > 0 && qualityResultNameComboBox.Items.Count > 0)
374        NetworkConfigurationChanged(this, EventArgs.Empty);
375    }
376
377    private void TextBoxValidating(object sender, CancelEventArgs e) {
378      double v;
379      string errorMsg = "Could not parse the entered value. Please input a real number.";
380      var tb = (TextBox)sender;
381      if (!double.TryParse(tb.Text, out v)) {
382        e.Cancel = true;
383        tb.Select(0, tb.Text.Length);
384
385        // Set the ErrorProvider error with the text to display.
386        this.errorProvider.SetError(tb, errorMsg);
387        errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
388        errorProvider.SetIconPadding(tb, -20);
389      }
390    }
391
392    private void ImpactThresholdTextBoxValidated(object sender, EventArgs e) {
393      var tb = (TextBox)sender;
394      errorProvider.SetError(tb, string.Empty);
395      double impact;
396      if (!double.TryParse(tb.Text, out impact))
397        impact = 0.2;
398      var network = ApplyThreshold(variableInteractionNetwork, impact);
399      graphChart.Graph = network;
400    }
401
402    private void LayoutConfigurationBoxValidated(object sender, EventArgs e) {
403      var tb = (TextBox)sender;
404      errorProvider.SetError(tb, string.Empty);
405      LayoutConfigurationChanged(sender, e);
406    }
407
408    private void NetworkConfigurationChanged(object sender, EventArgs e) {
409      var useBest = impactAggregationComboBox.SelectedIndex <= 0;
410      var threshold = double.Parse(impactThresholdTextBox.Text);
411      var qualityResultName = qualityResultNameComboBox.Text;
412      var impactsResultName = impactResultNameComboBox.Text;
413      if (string.IsNullOrEmpty(qualityResultName) || string.IsNullOrEmpty(impactsResultName))
414        return;
415      var maximization = maximizationCheckBox.Checked;
416      var impacts = CalculateVariableImpactsFromRunResults(Content, qualityResultName, maximization, impactsResultName, useBest);
417      variableInteractionNetwork = CreateNetwork(impacts);
418      var network = ApplyThreshold(variableInteractionNetwork, threshold);
419      graphChart.Graph = network;
420    }
421
422    private void LayoutConfigurationChanged(object sender, EventArgs e) {
423      ConstrainedForceDirectedLayout.EdgeRouting routingMode;
424      switch (edgeRoutingComboBox.SelectedIndex) {
425        case 0:
426          routingMode = ConstrainedForceDirectedLayout.EdgeRouting.None;
427          break;
428        case 1:
429          routingMode = ConstrainedForceDirectedLayout.EdgeRouting.Polyline;
430          break;
431        case 2:
432          routingMode = ConstrainedForceDirectedLayout.EdgeRouting.Orthogonal;
433          break;
434        default:
435          throw new ArgumentException("Invalid edge routing mode.");
436      }
437      var idealEdgeLength = double.Parse(idealEdgeLengthTextBox.Text);
438      if (routingMode == graphChart.RoutingMode && idealEdgeLength.IsAlmost(graphChart.IdealEdgeLength)) return;
439      graphChart.RoutingMode = routingMode;
440      graphChart.PerformEdgeRouting = routingMode != ConstrainedForceDirectedLayout.EdgeRouting.None;
441      graphChart.IdealEdgeLength = idealEdgeLength;
442      graphChart.Draw();
443    }
444
445    private void onlineImpactCalculationButton_Click(object sender, EventArgs args) {
446      var button = (Button)sender;
447      var worker = new BackgroundWorker();
448      worker.DoWork += (o, e) => {
449        button.Enabled = false;
450        var impacts = CalculateVariableImpactsOnline(Content, false);
451        variableInteractionNetwork = CreateNetwork(impacts);
452        var threshold = double.Parse(impactThresholdTextBox.Text);
453        graphChart.Graph = ApplyThreshold(variableInteractionNetwork, threshold);
454      };
455      worker.RunWorkerCompleted += (o, e) => button.Enabled = true;
456      worker.RunWorkerAsync();
457    }
458    #endregion
459  }
460}
Note: See TracBrowser for help on using the repository browser.