Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymbolicDataAnalysisSolutionResponseFunctionView.cs @ 6703

Last change on this file since 6703 was 6656, checked in by gkronber, 13 years ago

#1621: implemented a first version of the model response view

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