1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
36 | using System.Collections;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.VariableInteractionNetworks.Views {
|
---|
39 | [View("Variable Interaction Network")]
|
---|
40 | [Content(typeof(RunCollection), IsDefaultView = false)]
|
---|
41 |
|
---|
42 | public sealed partial class VariableInteractionNetworkView : AsynchronousContentView {
|
---|
43 | private const string variableImpactResultName = "Variable impacts";
|
---|
44 | public new RunCollection Content {
|
---|
45 | get { return (RunCollection)base.Content; }
|
---|
46 | set { base.Content = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public VariableInteractionNetworkView() {
|
---|
50 | InitializeComponent();
|
---|
51 | }
|
---|
52 |
|
---|
53 | #region events
|
---|
54 |
|
---|
55 | // #region Event Handlers (Content)
|
---|
56 | protected override void OnContentChanged() {
|
---|
57 | base.OnContentChanged();
|
---|
58 | if (Content == null) {
|
---|
59 | // TODO: Add code when content has been changed and is null
|
---|
60 | } else {
|
---|
61 | // TODO: Add code when content has been changed and is not null
|
---|
62 | CalculateAdjacencyMatrix();
|
---|
63 | }
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | protected override void SetEnabledStateOfControls() {
|
---|
68 | base.SetEnabledStateOfControls();
|
---|
69 | // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
|
---|
70 | }
|
---|
71 |
|
---|
72 | #region Event Handlers (child controls)
|
---|
73 | // TODO: Put event handlers of child controls here.
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | private void CalculateAdjacencyMatrix()
|
---|
77 | {
|
---|
78 | var runCollection = Content;
|
---|
79 | var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
|
---|
80 |
|
---|
81 | var allVariableImpacts = runCollection.Select(run => (DoubleMatrix)run.Results[variableImpactResultName]);
|
---|
82 | var variableNames = (from variableImpact in allVariableImpacts
|
---|
83 | from variableName in variableImpact.RowNames
|
---|
84 | select variableName).Distinct().ToArray();
|
---|
85 |
|
---|
86 | var adjMatrix = new DoubleMatrix(variableNames.Length, variableNames.Length);
|
---|
87 | var adjRow = groupRunCollection.Select(x => CalculateAdjacencyRows(x)).ToList();
|
---|
88 | var targets = groupRunCollection.Select(x => x.Key);
|
---|
89 |
|
---|
90 | adjMatrix.RowNames = targets;
|
---|
91 | adjMatrix.ColumnNames = adjMatrix.RowNames;
|
---|
92 |
|
---|
93 | for (int j = 0; j < groupRunCollection.Count; ++j)
|
---|
94 | {
|
---|
95 | var g = groupRunCollection[j];
|
---|
96 | var matrix = CalculateAdjacencyRows(g);
|
---|
97 | var variables = new List<Tuple<string, double>>();
|
---|
98 | var columnNames = matrix.ColumnNames.ToList();
|
---|
99 |
|
---|
100 | for (int i = 0; i < matrix.Columns; ++i)
|
---|
101 | {
|
---|
102 | variables.Add(new Tuple<string, double>(columnNames[i], matrix[0, i]));
|
---|
103 | }
|
---|
104 | variables.Add(new Tuple<string, double>(g.Key, 0));
|
---|
105 | variables.Sort((a, b) => a.Item1.CompareTo(b.Item1));
|
---|
106 | for (int i = 0; i < variables.Count; ++i)
|
---|
107 | {
|
---|
108 | adjMatrix[j, i] = variables[i].Item2;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | viewHost1.Content = adjMatrix;
|
---|
112 | }
|
---|
113 |
|
---|
114 | private DoubleMatrix CalculateAdjacencyRows(IEnumerable<IRun> runs)
|
---|
115 | {
|
---|
116 | IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs
|
---|
117 | select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
|
---|
118 | var variableNames = (from variableImpact in allVariableImpacts
|
---|
119 | from variableName in variableImpact.RowNames
|
---|
120 | select variableName)
|
---|
121 | .Distinct().ToArray();
|
---|
122 |
|
---|
123 | List<string> variableNamesList = (from variableName in variableNames
|
---|
124 | where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
|
---|
125 | select variableName)
|
---|
126 | .ToList();
|
---|
127 |
|
---|
128 | var inputVariables = runs.Select(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).InputVariables).ToArray();
|
---|
129 | var runNames = runs.Select(x => x.Name).ToArray();
|
---|
130 | var runsArray = runs.ToArray();
|
---|
131 | DoubleMatrix varImpactMatrix = CalculateVariableImpactMatrix(runsArray, runNames);
|
---|
132 | var targetMatrix = new DoubleMatrix(1, variableNames.Length);
|
---|
133 |
|
---|
134 | for (int i = 0; i < varImpactMatrix.Rows; ++i)
|
---|
135 | {
|
---|
136 | targetMatrix[0, i] = varImpactMatrix[i, runNames.Length];
|
---|
137 | }
|
---|
138 |
|
---|
139 | targetMatrix.RowNames = new[] { "Target" };
|
---|
140 | targetMatrix.ColumnNames = variableNames;
|
---|
141 |
|
---|
142 | return targetMatrix;
|
---|
143 | }
|
---|
144 |
|
---|
145 | //taken from RunCollectionVariableImpactView
|
---|
146 | private IEnumerable<double> GetVariableImpacts(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts)
|
---|
147 | {
|
---|
148 | foreach (DoubleMatrix runVariableImpacts in allVariableImpacts)
|
---|
149 | {
|
---|
150 | int row = 0;
|
---|
151 | foreach (string rowName in runVariableImpacts.RowNames)
|
---|
152 | {
|
---|
153 | if (rowName == variableName)
|
---|
154 | yield return runVariableImpacts[row, 0];
|
---|
155 | row++;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | //adapted from RunCollectionVariableImpactView
|
---|
161 | private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames)
|
---|
162 | {
|
---|
163 | IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs
|
---|
164 | select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
|
---|
165 | IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
|
---|
166 | from variableName in variableImpact.RowNames
|
---|
167 | select variableName).Distinct();
|
---|
168 |
|
---|
169 | // filter variableNames: only include names that have at least one non-zero value in a run
|
---|
170 | List<string> variableNamesList = (from variableName in variableNames
|
---|
171 | where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
|
---|
172 | select variableName).ToList();
|
---|
173 |
|
---|
174 | List<string> columnNames = new List<string>(runNames);
|
---|
175 | columnNames.Add("Mean");
|
---|
176 |
|
---|
177 | int numberOfRuns = runs.Length;
|
---|
178 |
|
---|
179 | DoubleMatrix matrix = new DoubleMatrix(variableNamesList.Count, numberOfRuns + 1);
|
---|
180 | matrix.SortableView = true;
|
---|
181 | matrix.ColumnNames = columnNames;
|
---|
182 |
|
---|
183 | List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
|
---|
184 | select GetVariableImpacts(variableName, allVariableImpacts).ToList()).ToList();
|
---|
185 |
|
---|
186 | for (int row = 0; row < variableImpactsOverRuns.Count; row++)
|
---|
187 | {
|
---|
188 | matrix[row, numberOfRuns] = Math.Round(variableImpactsOverRuns[row].Average(), 3);
|
---|
189 | }
|
---|
190 |
|
---|
191 | // fill matrix with impacts from runs
|
---|
192 | for (int i = 0; i < runs.Length; i++)
|
---|
193 | {
|
---|
194 | IRun run = runs[i];
|
---|
195 | DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
|
---|
196 | for (int j = 0; j < runVariableImpacts.Rows; j++)
|
---|
197 | {
|
---|
198 | int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
|
---|
199 | if (rowIndex > -1)
|
---|
200 | {
|
---|
201 | matrix[rowIndex, i] = Math.Round(runVariableImpacts[j, 0], 3);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 | return matrix;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | } |
---|