[12198] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13727] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12198] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
[13821] | 26 | using System.Globalization;
|
---|
[12198] | 27 | using System.Linq;
|
---|
[13727] | 28 | using System.Text;
|
---|
[12198] | 29 | using System.Windows.Forms;
|
---|
| 30 | using HeuristicLab.Common;
|
---|
[13727] | 31 | using HeuristicLab.Core;
|
---|
| 32 | using HeuristicLab.Core.Views;
|
---|
[12198] | 33 | using HeuristicLab.Data;
|
---|
| 34 | using HeuristicLab.MainForm;
|
---|
| 35 | using HeuristicLab.Optimization;
|
---|
| 36 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[13727] | 37 | using HeuristicLab.Visualization;
|
---|
[13773] | 38 | using Ellipse = HeuristicLab.Visualization.Ellipse;
|
---|
[13727] | 39 | using Rectangle = HeuristicLab.Visualization.Rectangle;
|
---|
[12198] | 40 |
|
---|
[13727] | 41 | namespace HeuristicLab.VariableInteractionNetworks.Views {
|
---|
| 42 | [View("Variable Interaction Network")]
|
---|
| 43 | [Content(typeof(RunCollection), IsDefaultView = false)]
|
---|
[12198] | 44 |
|
---|
[13727] | 45 | public sealed partial class RunCollectionVariableInteractionNetworkView : ItemView {
|
---|
| 46 | public RunCollectionVariableInteractionNetworkView() {
|
---|
| 47 | InitializeComponent();
|
---|
| 48 | ConfigureNodeShapes();
|
---|
| 49 | }
|
---|
[12229] | 50 |
|
---|
[13727] | 51 | public new RunCollection Content {
|
---|
| 52 | get { return (RunCollection)base.Content; }
|
---|
| 53 | set {
|
---|
| 54 | if (value != null && value != Content) {
|
---|
| 55 | base.Content = value;
|
---|
[12320] | 56 | }
|
---|
[13727] | 57 | }
|
---|
| 58 | }
|
---|
[12229] | 59 |
|
---|
[13773] | 60 | private VariableInteractionNetwork variableInteractionNetwork;
|
---|
[12229] | 61 |
|
---|
[13773] | 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;
|
---|
[12320] | 74 | }
|
---|
[13773] | 75 | if (dataset == null) {
|
---|
| 76 | dataset = ds;
|
---|
| 77 | continue;
|
---|
[13727] | 78 | }
|
---|
[12198] | 79 |
|
---|
[13773] | 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.");
|
---|
[12198] | 82 |
|
---|
[13773] | 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.");
|
---|
[12320] | 95 | }
|
---|
[13773] | 96 | }
|
---|
| 97 | }
|
---|
[12198] | 98 |
|
---|
[14275] | 99 | public static RegressionEnsembleSolution CreateEnsembleSolution(IEnumerable<IRun> runs) {
|
---|
[13773] | 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"];
|
---|
[12568] | 136 | }
|
---|
[13773] | 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 | }
|
---|
[12568] | 145 | }
|
---|
[13727] | 146 | }
|
---|
[13773] | 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);
|
---|
[12320] | 154 | }
|
---|
[13727] | 155 | }
|
---|
[13773] | 156 | return targetImpacts;
|
---|
| 157 | }
|
---|
[12320] | 158 |
|
---|
[13773] | 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;
|
---|
[13727] | 186 | }
|
---|
[12320] | 187 |
|
---|
[14275] | 188 | public static Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> CalculateVariableImpactsFromRunResults(RunCollection runs,
|
---|
[13773] | 189 | string qualityResultName, bool maximization, string impactsResultName, bool useBestRunsPerTarget = false) {
|
---|
[13727] | 190 | var targets = runs.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
|
---|
[13773] | 191 | var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>();
|
---|
[13727] | 192 | if (useBestRunsPerTarget) {
|
---|
[13773] | 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());
|
---|
[12229] | 196 |
|
---|
[13727] | 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];
|
---|
[13773] | 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]));
|
---|
[12320] | 202 | }
|
---|
[13727] | 203 | } else {
|
---|
| 204 | foreach (var target in targets) {
|
---|
| 205 | var averageImpacts = CalculateAverageImpacts(new RunCollection(target), impactsResultName);
|
---|
[13773] | 206 | targetImpacts[target.Key] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(target, averageImpacts);
|
---|
[12198] | 207 | }
|
---|
[13727] | 208 | }
|
---|
[13773] | 209 | return targetImpacts;
|
---|
| 210 | }
|
---|
[12198] | 211 |
|
---|
[14275] | 212 | public static VariableInteractionNetwork CreateNetwork(Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> targetImpacts) {
|
---|
[13773] | 213 | var nodes = new Dictionary<string, IVertex>();
|
---|
| 214 | var vn = new VariableInteractionNetwork();
|
---|
[13727] | 215 | foreach (var ti in targetImpacts) {
|
---|
| 216 | var target = ti.Key;
|
---|
[13773] | 217 | var variableImpacts = ti.Value.Item2;
|
---|
| 218 | var targetRuns = ti.Value.Item1;
|
---|
[13727] | 219 | IVertex targetNode;
|
---|
[12198] | 220 |
|
---|
[13773] | 221 | var variables = variableImpacts.Keys.ToList();
|
---|
[13727] | 222 | if (variables.Count == 0) continue;
|
---|
[12198] | 223 |
|
---|
[13727] | 224 | if (!nodes.TryGetValue(target, out targetNode)) {
|
---|
| 225 | targetNode = new VariableNetworkNode { Label = target };
|
---|
| 226 | vn.AddVertex(targetNode);
|
---|
| 227 | nodes[target] = targetNode;
|
---|
[12320] | 228 | }
|
---|
[12229] | 229 |
|
---|
[13727] | 230 | IVertex variableNode;
|
---|
| 231 | if (variables.Count > 1) {
|
---|
| 232 | var variableList = new List<string>(variables) { target };
|
---|
| 233 | var junctionLabel = Concatenate(variableList);
|
---|
| 234 | IVertex junctionNode;
|
---|
[13874] | 235 | var sb = new StringBuilder();
|
---|
[13727] | 236 | if (!nodes.TryGetValue(junctionLabel, out junctionNode)) {
|
---|
[13789] | 237 | var solutionsEnsemble = CreateEnsembleSolution(targetRuns);
|
---|
[13874] | 238 | junctionNode = new JunctionNetworkNode { Label = solutionsEnsemble.TrainingRSquared.ToString("N3", CultureInfo.CurrentCulture), Data = solutionsEnsemble };
|
---|
[13727] | 239 | vn.AddVertex(junctionNode);
|
---|
| 240 | nodes[junctionLabel] = junctionNode;
|
---|
[13874] | 241 | sb.AppendLine(junctionNode.Label);
|
---|
[13727] | 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;
|
---|
[12320] | 250 | }
|
---|
[13821] | 251 | arc = new Arc(variableNode, junctionNode) { Weight = impact, Label = impact.ToString("N3", CultureInfo.CurrentCulture) };
|
---|
[13874] | 252 | sb.AppendLine(v + ": " + arc.Label);
|
---|
[13727] | 253 | vn.AddArc(arc);
|
---|
| 254 | }
|
---|
[13874] | 255 | var jcnNode = (JunctionNetworkNode)junctionNode;
|
---|
| 256 | var trainingR2 = ((IRegressionSolution)jcnNode.Data).TrainingRSquared;
|
---|
[13821] | 257 | arc = new Arc(junctionNode, targetNode) { Weight = junctionNode.InArcs.Sum(x => x.Weight), Label = trainingR2.ToString("N3", CultureInfo.CurrentCulture) };
|
---|
[13727] | 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;
|
---|
[12320] | 266 | }
|
---|
[13821] | 267 | var arc = new Arc(variableNode, targetNode) {
|
---|
| 268 | Weight = impact, Label = impact.ToString("N3", CultureInfo.CurrentCulture)
|
---|
| 269 | };
|
---|
[13727] | 270 | vn.AddArc(arc);
|
---|
| 271 | }
|
---|
[12320] | 272 | }
|
---|
[13727] | 273 | }
|
---|
| 274 | return vn;
|
---|
| 275 | }
|
---|
[12320] | 276 |
|
---|
[14275] | 277 | public static VariableInteractionNetwork ApplyThreshold(VariableInteractionNetwork originalNetwork, double threshold) {
|
---|
[13806] | 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 |
|
---|
[13727] | 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;
|
---|
[12198] | 297 |
|
---|
[13727] | 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 | }
|
---|
[12320] | 306 |
|
---|
[14275] | 307 | public static Dictionary<string, double> CalculateAverageImpacts(RunCollection runs, string resultName) {
|
---|
[13727] | 308 | var pd = (IRegressionProblemData)runs.First().Parameters["ProblemData"];
|
---|
| 309 | var target = pd.TargetVariable;
|
---|
| 310 | var inputs = pd.AllowedInputVariables.ToList();
|
---|
[12320] | 311 |
|
---|
[13727] | 312 | var impacts = inputs.ToDictionary(x => x, x => 0d);
|
---|
[12320] | 313 |
|
---|
[13727] | 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 | }
|
---|
[12320] | 321 |
|
---|
[13727] | 322 | foreach (var run in runs) {
|
---|
| 323 | var impactsMatrix = (DoubleMatrix)run.Results[resultName];
|
---|
| 324 | int i = 0;
|
---|
| 325 | foreach (var v in impactsMatrix.RowNames) {
|
---|
| 326 | impacts[v] += impactsMatrix[i, 0];
|
---|
| 327 | ++i;
|
---|
[12320] | 328 | }
|
---|
[13727] | 329 | }
|
---|
[12320] | 330 |
|
---|
[13727] | 331 | foreach (var v in inputs) {
|
---|
| 332 | impacts[v] /= runs.Count;
|
---|
| 333 | }
|
---|
[12320] | 334 |
|
---|
[13727] | 335 | return impacts;
|
---|
| 336 | }
|
---|
[12263] | 337 |
|
---|
[13727] | 338 | private static string Concatenate(IEnumerable<string> strings) {
|
---|
| 339 | var sb = new StringBuilder();
|
---|
| 340 | foreach (var s in strings) {
|
---|
| 341 | sb.Append(s);
|
---|
| 342 | }
|
---|
| 343 | return sb.ToString();
|
---|
| 344 | }
|
---|
[12320] | 345 |
|
---|
[13727] | 346 | private void ConfigureNodeShapes() {
|
---|
| 347 | graphChart.ClearShapes();
|
---|
| 348 | var font = new Font(FontFamily.GenericSansSerif, 12);
|
---|
| 349 | graphChart.AddShape(typeof(VariableNetworkNode), new LabeledPrimitive(new Ellipse(graphChart.Chart, new PointD(0, 0), new PointD(30, 30), Pens.Black, Brushes.White), "", font));
|
---|
| 350 | graphChart.AddShape(typeof(JunctionNetworkNode), new LabeledPrimitive(new Rectangle(graphChart.Chart, new PointD(0, 0), new PointD(15, 15), Pens.Black, Brushes.DarkGray), "", font));
|
---|
| 351 | }
|
---|
[12320] | 352 |
|
---|
[13727] | 353 | #region events
|
---|
| 354 | protected override void OnContentChanged() {
|
---|
| 355 | base.OnContentChanged();
|
---|
| 356 | var run = Content.First();
|
---|
| 357 | var pd = (IRegressionProblemData)run.Parameters["ProblemData"];
|
---|
| 358 | var variables = new HashSet<string>(new List<string>(pd.Dataset.DoubleVariables));
|
---|
| 359 | impactResultNameComboBox.Items.Clear();
|
---|
| 360 | foreach (var result in run.Results.Where(x => x.Value is DoubleMatrix)) {
|
---|
| 361 | var m = (DoubleMatrix)result.Value;
|
---|
| 362 | if (m.RowNames.All(x => variables.Contains(x)))
|
---|
| 363 | impactResultNameComboBox.Items.Add(result.Key);
|
---|
| 364 | }
|
---|
| 365 | qualityResultNameComboBox.Items.Clear();
|
---|
| 366 | foreach (var result in run.Results.Where(x => x.Value is DoubleValue)) {
|
---|
| 367 | qualityResultNameComboBox.Items.Add(result.Key);
|
---|
| 368 | }
|
---|
| 369 | if (impactResultNameComboBox.Items.Count > 0) {
|
---|
| 370 | impactResultNameComboBox.Text = (string)impactResultNameComboBox.Items[0];
|
---|
| 371 | }
|
---|
| 372 | if (qualityResultNameComboBox.Items.Count > 0) {
|
---|
| 373 | qualityResultNameComboBox.Text = (string)qualityResultNameComboBox.Items[0];
|
---|
| 374 | }
|
---|
| 375 | if (impactResultNameComboBox.Items.Count > 0 && qualityResultNameComboBox.Items.Count > 0)
|
---|
| 376 | NetworkConfigurationChanged(this, EventArgs.Empty);
|
---|
| 377 | }
|
---|
[12320] | 378 |
|
---|
[13727] | 379 | private void TextBoxValidating(object sender, CancelEventArgs e) {
|
---|
| 380 | double v;
|
---|
| 381 | string errorMsg = "Could not parse the entered value. Please input a real number.";
|
---|
| 382 | var tb = (TextBox)sender;
|
---|
| 383 | if (!double.TryParse(tb.Text, out v)) {
|
---|
| 384 | e.Cancel = true;
|
---|
| 385 | tb.Select(0, tb.Text.Length);
|
---|
[12320] | 386 |
|
---|
[13727] | 387 | // Set the ErrorProvider error with the text to display.
|
---|
| 388 | this.errorProvider.SetError(tb, errorMsg);
|
---|
| 389 | errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
|
---|
| 390 | errorProvider.SetIconPadding(tb, -20);
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
[12320] | 393 |
|
---|
[13773] | 394 | private void ImpactThresholdTextBoxValidated(object sender, EventArgs e) {
|
---|
[13727] | 395 | var tb = (TextBox)sender;
|
---|
| 396 | errorProvider.SetError(tb, string.Empty);
|
---|
[13806] | 397 | double impact;
|
---|
[13874] | 398 | if (!double.TryParse(tb.Text, out impact)) {
|
---|
[13821] | 399 | impact = 0.2;
|
---|
[13874] | 400 | }
|
---|
[13806] | 401 | var network = ApplyThreshold(variableInteractionNetwork, impact);
|
---|
[13773] | 402 | graphChart.Graph = network;
|
---|
[13727] | 403 | }
|
---|
[12320] | 404 |
|
---|
[13727] | 405 | private void LayoutConfigurationBoxValidated(object sender, EventArgs e) {
|
---|
| 406 | var tb = (TextBox)sender;
|
---|
| 407 | errorProvider.SetError(tb, string.Empty);
|
---|
| 408 | LayoutConfigurationChanged(sender, e);
|
---|
| 409 | }
|
---|
[12320] | 410 |
|
---|
[13727] | 411 | private void NetworkConfigurationChanged(object sender, EventArgs e) {
|
---|
| 412 | var useBest = impactAggregationComboBox.SelectedIndex <= 0;
|
---|
[13893] | 413 | var threshold = impactThresholdTrackBar.Value / 100.0;
|
---|
[13727] | 414 | var qualityResultName = qualityResultNameComboBox.Text;
|
---|
| 415 | var impactsResultName = impactResultNameComboBox.Text;
|
---|
| 416 | if (string.IsNullOrEmpty(qualityResultName) || string.IsNullOrEmpty(impactsResultName))
|
---|
| 417 | return;
|
---|
| 418 | var maximization = maximizationCheckBox.Checked;
|
---|
[13773] | 419 | var impacts = CalculateVariableImpactsFromRunResults(Content, qualityResultName, maximization, impactsResultName, useBest);
|
---|
| 420 | variableInteractionNetwork = CreateNetwork(impacts);
|
---|
| 421 | var network = ApplyThreshold(variableInteractionNetwork, threshold);
|
---|
| 422 | graphChart.Graph = network;
|
---|
[12263] | 423 | }
|
---|
[13727] | 424 |
|
---|
| 425 | private void LayoutConfigurationChanged(object sender, EventArgs e) {
|
---|
| 426 | ConstrainedForceDirectedLayout.EdgeRouting routingMode;
|
---|
| 427 | switch (edgeRoutingComboBox.SelectedIndex) {
|
---|
| 428 | case 0:
|
---|
| 429 | routingMode = ConstrainedForceDirectedLayout.EdgeRouting.None;
|
---|
| 430 | break;
|
---|
| 431 | case 1:
|
---|
| 432 | routingMode = ConstrainedForceDirectedLayout.EdgeRouting.Polyline;
|
---|
| 433 | break;
|
---|
| 434 | case 2:
|
---|
| 435 | routingMode = ConstrainedForceDirectedLayout.EdgeRouting.Orthogonal;
|
---|
| 436 | break;
|
---|
| 437 | default:
|
---|
| 438 | throw new ArgumentException("Invalid edge routing mode.");
|
---|
| 439 | }
|
---|
| 440 | var idealEdgeLength = double.Parse(idealEdgeLengthTextBox.Text);
|
---|
[14275] | 441 | if (routingMode == graphChart.RoutingMode && idealEdgeLength.IsAlmost(graphChart.DefaultEdgeLength)) return;
|
---|
[13727] | 442 | graphChart.RoutingMode = routingMode;
|
---|
| 443 | graphChart.PerformEdgeRouting = routingMode != ConstrainedForceDirectedLayout.EdgeRouting.None;
|
---|
[14275] | 444 | graphChart.DefaultEdgeLength = idealEdgeLength;
|
---|
[13727] | 445 | graphChart.Draw();
|
---|
| 446 | }
|
---|
[13773] | 447 |
|
---|
[13874] | 448 | private void ControlsEnable(bool enabled) {
|
---|
| 449 | qualityResultNameComboBox.Enabled
|
---|
| 450 | = impactResultNameComboBox.Enabled
|
---|
| 451 | = impactAggregationComboBox.Enabled
|
---|
[13893] | 452 | = impactThresholdTrackBar.Enabled
|
---|
[13874] | 453 | = onlineImpactCalculationButton.Enabled
|
---|
| 454 | = edgeRoutingComboBox.Enabled
|
---|
[13893] | 455 | = idealEdgeLengthTextBox.Enabled
|
---|
| 456 | = maximizationCheckBox.Enabled = enabled;
|
---|
[13874] | 457 | }
|
---|
| 458 |
|
---|
[13773] | 459 | private void onlineImpactCalculationButton_Click(object sender, EventArgs args) {
|
---|
| 460 | var worker = new BackgroundWorker();
|
---|
| 461 | worker.DoWork += (o, e) => {
|
---|
[13874] | 462 | ControlsEnable(false);
|
---|
[13773] | 463 | var impacts = CalculateVariableImpactsOnline(Content, false);
|
---|
[13789] | 464 | variableInteractionNetwork = CreateNetwork(impacts);
|
---|
[13893] | 465 | var threshold = impactThresholdTrackBar.Minimum + (double)impactThresholdTrackBar.Value / impactThresholdTrackBar.Maximum;
|
---|
[13789] | 466 | graphChart.Graph = ApplyThreshold(variableInteractionNetwork, threshold);
|
---|
[13773] | 467 | };
|
---|
[13874] | 468 | worker.RunWorkerCompleted += (o, e) => ControlsEnable(true);
|
---|
[13773] | 469 | worker.RunWorkerAsync();
|
---|
| 470 | }
|
---|
[13893] | 471 |
|
---|
| 472 | private void relayoutGraphButton_Click(object sender, EventArgs e) {
|
---|
| 473 | graphChart.Draw();
|
---|
| 474 | }
|
---|
[13727] | 475 | #endregion
|
---|
[13893] | 476 |
|
---|
| 477 | private void exportImpactsMatrixButton_Click(object sender, EventArgs e) {
|
---|
| 478 | var graph = graphChart.Graph;
|
---|
| 479 | var labels = graph.Vertices.OfType<VariableNetworkNode>().Select(x => x.Label).ToList();
|
---|
| 480 | labels.Sort(); // sort variables alphabetically
|
---|
| 481 | var matrix = new DoubleMatrix(labels.Count, labels.Count) { RowNames = labels, ColumnNames = labels };
|
---|
| 482 | var indexes = labels.Select((x, i) => new { Label = x, Index = i }).ToDictionary(x => x.Label, x => x.Index);
|
---|
| 483 | var junctions = graph.Vertices.OfType<JunctionNetworkNode>().ToList();
|
---|
| 484 | foreach (var jn in junctions) {
|
---|
| 485 | var target = jn.OutArcs.First().Target.Label;
|
---|
| 486 | var targetIndex = indexes[target];
|
---|
| 487 | foreach (var input in jn.InArcs) {
|
---|
| 488 | var inputIndex = indexes[input.Source.Label];
|
---|
| 489 | var inputImpact = input.Weight;
|
---|
| 490 | matrix[targetIndex, inputIndex] = inputImpact;
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | for (int i = 0; i < labels.Count; ++i) matrix[i, i] = 1;
|
---|
| 494 | MainFormManager.MainForm.ShowContent(matrix);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | private void impactThresholdTrackBar_ValueChanged(object sender, EventArgs e) {
|
---|
| 498 | var impact = impactThresholdTrackBar.Minimum + (double)impactThresholdTrackBar.Value / impactThresholdTrackBar.Maximum;
|
---|
| 499 | impactThresholdLabel.Text = impact.ToString("N3", CultureInfo.CurrentCulture);
|
---|
| 500 | var network = ApplyThreshold(variableInteractionNetwork, impact);
|
---|
| 501 | graphChart.Graph = network;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 |
|
---|
[13727] | 505 | }
|
---|
| 506 | }
|
---|