[6656] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[6784] | 29 | using HeuristicLab.MainForm;
|
---|
[6656] | 30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
| 33 | [View("Response Function View")]
|
---|
| 34 | [Content(typeof(RegressionSolutionBase), false)]
|
---|
| 35 | public partial class SymbolicDataAnalysisSolutionResponseFunctionView : AsynchronousContentView {
|
---|
| 36 | private Dictionary<string, List<ISymbolicExpressionTreeNode>> variableNodes;
|
---|
| 37 | private ISymbolicExpressionTree clonedTree;
|
---|
| 38 | private Dictionary<string, double> medianValues;
|
---|
| 39 | public SymbolicDataAnalysisSolutionResponseFunctionView() {
|
---|
| 40 | InitializeComponent();
|
---|
| 41 | this.variableNodes = new Dictionary<string, List<ISymbolicExpressionTreeNode>>();
|
---|
| 42 | medianValues = new Dictionary<string, double>();
|
---|
| 43 | this.Caption = "Response Function View";
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public new ISymbolicDataAnalysisSolution Content {
|
---|
| 47 | get { return (ISymbolicDataAnalysisSolution)base.Content; }
|
---|
| 48 | set { base.Content = value; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | protected override void RegisterContentEvents() {
|
---|
| 52 | base.RegisterContentEvents();
|
---|
| 53 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
| 54 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 55 | }
|
---|
| 56 | protected override void DeregisterContentEvents() {
|
---|
| 57 | base.DeregisterContentEvents();
|
---|
| 58 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
| 59 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
| 63 | OnModelChanged();
|
---|
| 64 | }
|
---|
| 65 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
| 66 | OnProblemDataChanged();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | protected virtual void OnModelChanged() {
|
---|
| 70 | this.UpdateView();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | protected virtual void OnProblemDataChanged() {
|
---|
| 74 | this.UpdateView();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected override void OnContentChanged() {
|
---|
| 78 | base.OnContentChanged();
|
---|
| 79 | this.UpdateView();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private void UpdateView() {
|
---|
| 83 | if (Content != null && Content.Model != null && Content.ProblemData != null) {
|
---|
| 84 | var referencedVariables =
|
---|
| 85 | (from varNode in Content.Model.SymbolicExpressionTree.IterateNodesPrefix().OfType<VariableTreeNode>()
|
---|
| 86 | select varNode.VariableName)
|
---|
| 87 | .Distinct()
|
---|
[6784] | 88 | .OrderBy(x => x)
|
---|
[6656] | 89 | .ToList();
|
---|
| 90 |
|
---|
| 91 | medianValues.Clear();
|
---|
| 92 | foreach (var variableName in referencedVariables) {
|
---|
[6784] | 93 | medianValues.Add(variableName, Content.ProblemData.Dataset.GetDoubleValues(variableName).Median());
|
---|
[6656] | 94 | }
|
---|
| 95 |
|
---|
| 96 | comboBox.Items.Clear();
|
---|
| 97 | comboBox.Items.AddRange(referencedVariables.ToArray());
|
---|
| 98 | comboBox.SelectedIndex = 0;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | private void CreateSliders(IEnumerable<string> variableNames) {
|
---|
| 103 | flowLayoutPanel.Controls.Clear();
|
---|
| 104 |
|
---|
| 105 | foreach (var variableName in variableNames) {
|
---|
| 106 | var variableTrackbar = new VariableTrackbar(variableName,
|
---|
[6784] | 107 | Content.ProblemData.Dataset.GetDoubleValues(variableName));
|
---|
[6656] | 108 | variableTrackbar.Size = new Size(variableTrackbar.Size.Width, flowLayoutPanel.Size.Height - 23);
|
---|
| 109 | variableTrackbar.ValueChanged += TrackBarValueChanged;
|
---|
| 110 | flowLayoutPanel.Controls.Add(variableTrackbar);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private void TrackBarValueChanged(object sender, EventArgs e) {
|
---|
| 115 | var trackBar = (VariableTrackbar)sender;
|
---|
| 116 | string variableName = trackBar.VariableName;
|
---|
| 117 | ChangeVariableValue(variableName, trackBar.Value);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private void ChangeVariableValue(string variableName, double value) {
|
---|
| 121 | foreach (var constNode in variableNodes[variableName].Cast<ConstantTreeNode>())
|
---|
| 122 | constNode.Value = value;
|
---|
| 123 |
|
---|
| 124 | UpdateChart();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void UpdateChart() {
|
---|
| 128 | string freeVariable = (string)comboBox.SelectedItem;
|
---|
| 129 | IEnumerable<string> fixedVariables = comboBox.Items.OfType<string>()
|
---|
| 130 | .Except(new string[] { freeVariable });
|
---|
| 131 |
|
---|
[6784] | 132 | var freeVariableValues = Content.ProblemData.Dataset.GetDoubleValues(freeVariable, Content.ProblemData.TrainingIndizes).ToArray();
|
---|
[6656] | 133 | var responseValues = Content.Model.Interpreter.GetSymbolicExpressionTreeValues(clonedTree,
|
---|
| 134 | Content.ProblemData.Dataset,
|
---|
| 135 | Content.ProblemData.TrainingIndizes).ToArray();
|
---|
| 136 | Array.Sort(freeVariableValues, responseValues);
|
---|
| 137 | responseChart.Series["Model Response"].Points.DataBindXY(freeVariableValues, responseValues);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | private ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
| 141 | Constant constant = new Constant();
|
---|
| 142 | constant.MinValue = value - 1;
|
---|
| 143 | constant.MaxValue = value + 1;
|
---|
| 144 | ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
| 145 | constantTreeNode.Value = value;
|
---|
| 146 | return constantTreeNode;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | private void ComboBoxSelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 150 | string freeVariable = (string)comboBox.SelectedItem;
|
---|
| 151 | IEnumerable<string> fixedVariables = comboBox.Items.OfType<string>()
|
---|
| 152 | .Except(new string[] { freeVariable });
|
---|
| 153 |
|
---|
| 154 | variableNodes.Clear();
|
---|
| 155 | clonedTree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone();
|
---|
| 156 |
|
---|
| 157 | foreach (var varNode in clonedTree.IterateNodesPrefix().OfType<VariableTreeNode>()) {
|
---|
| 158 | if (fixedVariables.Contains(varNode.VariableName)) {
|
---|
| 159 | if (!variableNodes.ContainsKey(varNode.VariableName))
|
---|
| 160 | variableNodes.Add(varNode.VariableName, new List<ISymbolicExpressionTreeNode>());
|
---|
| 161 |
|
---|
| 162 | int childIndex = varNode.Parent.IndexOfSubtree(varNode);
|
---|
| 163 | var replacementNode = MakeConstantTreeNode(medianValues[varNode.VariableName]);
|
---|
| 164 | var parent = varNode.Parent;
|
---|
| 165 | parent.RemoveSubtree(childIndex);
|
---|
| 166 | parent.InsertSubtree(childIndex, replacementNode);
|
---|
| 167 | variableNodes[varNode.VariableName].Add(replacementNode);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | CreateSliders(fixedVariables);
|
---|
| 172 | UpdateChart();
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|