Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13874 was 13874, checked in by bburlacu, 8 years ago

#2288: Small refactoring.

File size: 21.1 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          var sb = new StringBuilder();
236          if (!nodes.TryGetValue(junctionLabel, out junctionNode)) {
237            var solutionsEnsemble = CreateEnsembleSolution(targetRuns);
238            junctionNode = new JunctionNetworkNode { Label = solutionsEnsemble.TrainingRSquared.ToString("N3", CultureInfo.CurrentCulture), Data = solutionsEnsemble };
239            vn.AddVertex(junctionNode);
240            nodes[junctionLabel] = junctionNode;
241            sb.AppendLine(junctionNode.Label);
242          }
243          IArc arc;
244          foreach (var v in variables) {
245            var impact = variableImpacts[v];
246            if (!nodes.TryGetValue(v, out variableNode)) {
247              variableNode = new VariableNetworkNode { Label = v };
248              vn.AddVertex(variableNode);
249              nodes[v] = variableNode;
250            }
251            arc = new Arc(variableNode, junctionNode) { Weight = impact, Label = impact.ToString("N3", CultureInfo.CurrentCulture) };
252            sb.AppendLine(v + ": " + arc.Label);
253            vn.AddArc(arc);
254          }
255          var jcnNode = (JunctionNetworkNode)junctionNode;
256          var trainingR2 = ((IRegressionSolution)jcnNode.Data).TrainingRSquared;
257          arc = new Arc(junctionNode, targetNode) { Weight = junctionNode.InArcs.Sum(x => x.Weight), Label = trainingR2.ToString("N3", CultureInfo.CurrentCulture) };
258          vn.AddArc(arc);
259        } else {
260          foreach (var v in variables) {
261            var impact = variableImpacts[v];
262            if (!nodes.TryGetValue(v, out variableNode)) {
263              variableNode = new VariableNetworkNode { Label = v };
264              vn.AddVertex(variableNode);
265              nodes[v] = variableNode;
266            }
267            var arc = new Arc(variableNode, targetNode) {
268              Weight = impact, Label = impact.ToString("N3", CultureInfo.CurrentCulture)
269            };
270            vn.AddArc(arc);
271          }
272        }
273      }
274      return vn;
275    }
276
277    private static VariableInteractionNetwork ApplyThreshold(VariableInteractionNetwork originalNetwork, double threshold) {
278      var arcs = originalNetwork.Arcs.Where(x => x.Weight >= threshold).ToList();
279      if (!arcs.Any()) return originalNetwork;
280      var filteredNetwork = new VariableInteractionNetwork();
281      var cloner = new Cloner();
282      var vertices = arcs.SelectMany(x => new[] { x.Source, x.Target }).Select(cloner.Clone).Distinct(); // arcs are not cloned
283      filteredNetwork.AddVertices(vertices);
284      filteredNetwork.AddArcs(arcs.Select(x => (IArc)x.Clone(cloner)));
285
286      var unusedJunctions = filteredNetwork.Vertices.Where(x => x.InDegree == 0 && x is JunctionNetworkNode).ToList();
287      filteredNetwork.RemoveVertices(unusedJunctions);
288      var orphanedNodes = filteredNetwork.Vertices.Where(x => x.Degree == 0).ToList();
289      filteredNetwork.RemoveVertices(orphanedNodes);
290      return filteredNetwork.Vertices.Any() ? filteredNetwork : originalNetwork;
291    }
292
293    private static double CalculateAverageQuality(RunCollection runs) {
294      var pd = (IRegressionProblemData)runs.First().Parameters["ProblemData"];
295      var target = pd.TargetVariable;
296      var inputs = pd.AllowedInputVariables;
297
298      if (!runs.All(x => {
299        var problemData = (IRegressionProblemData)x.Parameters["ProblemData"];
300        return target == problemData.TargetVariable && inputs.SequenceEqual(problemData.AllowedInputVariables);
301      })) {
302        throw new ArgumentException("All runs must have the same target and inputs.");
303      }
304      return runs.Average(x => ((DoubleValue)x.Results["Best training solution quality"]).Value);
305    }
306
307    private static Dictionary<string, double> CalculateAverageImpacts(RunCollection runs, string resultName) {
308      var pd = (IRegressionProblemData)runs.First().Parameters["ProblemData"];
309      var target = pd.TargetVariable;
310      var inputs = pd.AllowedInputVariables.ToList();
311
312      var impacts = inputs.ToDictionary(x => x, x => 0d);
313
314      // check if all the runs have the same target and same inputs
315      if (!runs.All(x => {
316        var problemData = (IRegressionProblemData)x.Parameters["ProblemData"];
317        return target == problemData.TargetVariable && inputs.SequenceEqual(problemData.AllowedInputVariables);
318      })) {
319        throw new ArgumentException("All runs must have the same target and inputs.");
320      }
321
322      foreach (var run in runs) {
323        var impactsMatrix = (DoubleMatrix)run.Results[resultName];
324
325        int i = 0;
326        foreach (var v in impactsMatrix.RowNames) {
327          impacts[v] += impactsMatrix[i, 0];
328          ++i;
329        }
330      }
331
332      foreach (var v in inputs) {
333        impacts[v] /= runs.Count;
334      }
335
336      return impacts;
337    }
338
339    private static string Concatenate(IEnumerable<string> strings) {
340      var sb = new StringBuilder();
341      foreach (var s in strings) {
342        sb.Append(s);
343      }
344      return sb.ToString();
345    }
346
347    private void ConfigureNodeShapes() {
348      graphChart.ClearShapes();
349      var font = new Font(FontFamily.GenericSansSerif, 12);
350      graphChart.AddShape(typeof(VariableNetworkNode), new LabeledPrimitive(new Ellipse(graphChart.Chart, new PointD(0, 0), new PointD(30, 30), Pens.Black, Brushes.White), "", font));
351      graphChart.AddShape(typeof(JunctionNetworkNode), new LabeledPrimitive(new Rectangle(graphChart.Chart, new PointD(0, 0), new PointD(15, 15), Pens.Black, Brushes.DarkGray), "", font));
352    }
353
354    #region events
355    protected override void OnContentChanged() {
356      base.OnContentChanged();
357      var run = Content.First();
358      var pd = (IRegressionProblemData)run.Parameters["ProblemData"];
359      var variables = new HashSet<string>(new List<string>(pd.Dataset.DoubleVariables));
360      impactResultNameComboBox.Items.Clear();
361      foreach (var result in run.Results.Where(x => x.Value is DoubleMatrix)) {
362        var m = (DoubleMatrix)result.Value;
363        if (m.RowNames.All(x => variables.Contains(x)))
364          impactResultNameComboBox.Items.Add(result.Key);
365      }
366      qualityResultNameComboBox.Items.Clear();
367      foreach (var result in run.Results.Where(x => x.Value is DoubleValue)) {
368        qualityResultNameComboBox.Items.Add(result.Key);
369      }
370      if (impactResultNameComboBox.Items.Count > 0) {
371        impactResultNameComboBox.Text = (string)impactResultNameComboBox.Items[0];
372      }
373      if (qualityResultNameComboBox.Items.Count > 0) {
374        qualityResultNameComboBox.Text = (string)qualityResultNameComboBox.Items[0];
375      }
376      if (impactResultNameComboBox.Items.Count > 0 && qualityResultNameComboBox.Items.Count > 0)
377        NetworkConfigurationChanged(this, EventArgs.Empty);
378    }
379
380    private void TextBoxValidating(object sender, CancelEventArgs e) {
381      double v;
382      string errorMsg = "Could not parse the entered value. Please input a real number.";
383      var tb = (TextBox)sender;
384      if (!double.TryParse(tb.Text, out v)) {
385        e.Cancel = true;
386        tb.Select(0, tb.Text.Length);
387
388        // Set the ErrorProvider error with the text to display.
389        this.errorProvider.SetError(tb, errorMsg);
390        errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
391        errorProvider.SetIconPadding(tb, -20);
392      }
393    }
394
395    private void ImpactThresholdTextBoxValidated(object sender, EventArgs e) {
396      var tb = (TextBox)sender;
397      errorProvider.SetError(tb, string.Empty);
398      double impact;
399      if (!double.TryParse(tb.Text, out impact)) {
400        impact = 0.2;
401      }
402      var network = ApplyThreshold(variableInteractionNetwork, impact);
403      graphChart.Graph = network;
404    }
405
406    private void LayoutConfigurationBoxValidated(object sender, EventArgs e) {
407      var tb = (TextBox)sender;
408      errorProvider.SetError(tb, string.Empty);
409      LayoutConfigurationChanged(sender, e);
410    }
411
412    private void NetworkConfigurationChanged(object sender, EventArgs e) {
413      var useBest = impactAggregationComboBox.SelectedIndex <= 0;
414      var threshold = double.Parse(impactThresholdTextBox.Text);
415      var qualityResultName = qualityResultNameComboBox.Text;
416      var impactsResultName = impactResultNameComboBox.Text;
417      if (string.IsNullOrEmpty(qualityResultName) || string.IsNullOrEmpty(impactsResultName))
418        return;
419      var maximization = maximizationCheckBox.Checked;
420      var impacts = CalculateVariableImpactsFromRunResults(Content, qualityResultName, maximization, impactsResultName, useBest);
421      variableInteractionNetwork = CreateNetwork(impacts);
422      var network = ApplyThreshold(variableInteractionNetwork, threshold);
423      graphChart.Graph = network;
424    }
425
426    private void LayoutConfigurationChanged(object sender, EventArgs e) {
427      ConstrainedForceDirectedLayout.EdgeRouting routingMode;
428      switch (edgeRoutingComboBox.SelectedIndex) {
429        case 0:
430          routingMode = ConstrainedForceDirectedLayout.EdgeRouting.None;
431          break;
432        case 1:
433          routingMode = ConstrainedForceDirectedLayout.EdgeRouting.Polyline;
434          break;
435        case 2:
436          routingMode = ConstrainedForceDirectedLayout.EdgeRouting.Orthogonal;
437          break;
438        default:
439          throw new ArgumentException("Invalid edge routing mode.");
440      }
441      var idealEdgeLength = double.Parse(idealEdgeLengthTextBox.Text);
442      if (routingMode == graphChart.RoutingMode && idealEdgeLength.IsAlmost(graphChart.IdealEdgeLength)) return;
443      graphChart.RoutingMode = routingMode;
444      graphChart.PerformEdgeRouting = routingMode != ConstrainedForceDirectedLayout.EdgeRouting.None;
445      graphChart.IdealEdgeLength = idealEdgeLength;
446      graphChart.Draw();
447    }
448
449    private void ControlsEnable(bool enabled) {
450      qualityResultNameComboBox.Enabled
451        = impactResultNameComboBox.Enabled
452        = impactAggregationComboBox.Enabled
453        = impactThresholdTextBox.Enabled
454        = onlineImpactCalculationButton.Enabled
455        = edgeRoutingComboBox.Enabled
456        = idealEdgeLengthTextBox.Enabled = enabled;
457    }
458
459    private void onlineImpactCalculationButton_Click(object sender, EventArgs args) {
460      var worker = new BackgroundWorker();
461      worker.DoWork += (o, e) => {
462        ControlsEnable(false);
463        var impacts = CalculateVariableImpactsOnline(Content, false);
464        variableInteractionNetwork = CreateNetwork(impacts);
465        var threshold = double.Parse(impactThresholdTextBox.Text);
466        graphChart.Graph = ApplyThreshold(variableInteractionNetwork, threshold);
467      };
468      worker.RunWorkerCompleted += (o, e) => ControlsEnable(true);
469      worker.RunWorkerAsync();
470    }
471    #endregion
472  }
473}
Note: See TracBrowser for help on using the repository browser.