source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Views/MOFrontScatterPlotView.cs @ 14108

Last change on this file since 14108 was 14108, checked in by mkommend, 4 years ago

#1087: Fixed bugs in Spacing and Hypervolume calculation, corrected plugin dependencies and minor changes to the scatter plot view.

File size: 14.6 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
21using System;
22using System.Drawing;
23using System.Linq;
24using System.Text;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
31  [View("Scatter Plot")]
32  [Content(typeof(ScatterPlotContent))]
33  public partial class MOQualitiesScatterPlotView : ItemView {
34    private const string QUALITIES = "Qualities";
35    private const string PARETO_FRONT = "Best Known Pareto Front";
36    private Series qualitySeries;
37    private Series paretoSeries;
38    private int xDim = 0;
39    private int yDim = 1;
40    int objectives = -1;
41
42    public new ScatterPlotContent Content {
43      get { return (ScatterPlotContent)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public MOQualitiesScatterPlotView()
48      : base() {
49      InitializeComponent();
50
51      BuildEmptySeries();
52
53      //start with qualities toggled ON
54      qualitySeries.Points.AddXY(0, 0);
55
56      this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
57      this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
58      this.chart.GetToolTipText += new System.EventHandler<ToolTipEventArgs>(this.Chart_GetToolTipText);
59
60      //configure axis
61      this.chart.CustomizeAllChartAreas();
62      this.chart.ChartAreas[0].AxisX.Title = "Objective " + xDim;
63      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
64      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
65      this.chart.ChartAreas[0].CursorX.Interval = 1;
66      this.chart.ChartAreas[0].CursorY.Interval = 1;
67
68      this.chart.ChartAreas[0].AxisY.Title = "Objective " + yDim;
69      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
70      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
71      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
72    }
73
74
75    private void Chart_GetToolTipText(object sender, ToolTipEventArgs e) {
76      if (e.HitTestResult.ChartElementType == ChartElementType.LegendItem) {
77        if (e.HitTestResult.Series == paretoSeries && (Content.ParetoFront == null || Content.ParetoFront.Length == 0)) {
78          e.Text = "No optimal pareto front is available for this problem with this number of objectives";
79        }
80        if (e.HitTestResult.Series == paretoSeries && (xDim >= Content.Objectives || yDim >= Content.Objectives)) {
81          e.Text = "The optimal pareto front can only be displayed in  Objective Space";
82        }
83      }
84
85      // Check selected chart element and set tooltip text
86      if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) {
87        int i = e.HitTestResult.PointIndex;
88        StringBuilder toolTippText = new StringBuilder();
89        DataPoint qp = e.HitTestResult.Series.Points[i];
90        toolTippText.Append("Objective " + xDim + " = " + qp.XValue + "\n");
91        toolTippText.Append("Objective " + yDim + " = " + qp.YValues[0]);
92
93        Series s = e.HitTestResult.Series;
94        if (s.Equals(this.chart.Series[QUALITIES])) {
95          double[] dp = Content.Solutions[i];
96          toolTippText.Append("\nSolution: {");
97          for (int j = 0; j < dp.Length; j++) {
98            toolTippText.Append(dp[j]);
99            toolTippText.Append(";");
100          }
101          toolTippText.Remove(toolTippText.Length - 1, 1);
102          toolTippText.Append("}");
103          e.Text = toolTippText.ToString();
104        }
105
106
107      }
108    }
109
110    protected override void OnContentChanged() {
111      base.OnContentChanged();
112      if (Content == null) return;
113      if (objectives != Content.Objectives) {
114        AddMenuItems();
115        objectives = Content.Objectives;
116      }
117      if (Content.ParetoFront == null && chart.Series.Contains(paretoSeries)) {
118        Series s = this.chart.Series[PARETO_FRONT];
119        paretoSeries = null;
120        this.chart.Series.Remove(s);
121
122      } else if (Content.ParetoFront != null && !chart.Series.Contains(paretoSeries)) {
123        this.chart.Series.Add(PARETO_FRONT);
124        paretoSeries = this.chart.Series[PARETO_FRONT];
125        this.chart.Series[PARETO_FRONT].LegendText = PARETO_FRONT;
126        this.chart.Series[PARETO_FRONT].ChartType = SeriesChartType.FastPoint;
127      }
128      UpdateChart();
129    }
130
131    private void UpdateChart() {
132      if (InvokeRequired) Invoke((Action)UpdateChart);
133      else {
134        if (Content != null) {
135          this.UpdateSeries();
136          if (!this.chart.Series.Any(s => s.Points.Count > 0))
137            this.ClearChart();
138        }
139      }
140    }
141
142    private void UpdateCursorInterval() {
143      var estimatedValues = this.chart.Series[QUALITIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
144      var targetValues = this.chart.Series[QUALITIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
145      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
146      double targetValuesRange = targetValues.Max() - targetValues.Min();
147      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
148      double digits = (int)Math.Log10(interestingValuesRange) - 3;
149      double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
150      this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
151      this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
152
153      this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = zoomInterval;
154      this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = zoomInterval;
155
156      this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number;
157      this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = zoomInterval;
158      this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number;
159      this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = zoomInterval;
160
161      if (digits < 0) {
162        this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F" + (int)Math.Abs(digits);
163        this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F" + (int)Math.Abs(digits);
164      } else {
165        this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F0";
166        this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F0";
167      }
168    }
169
170    private void UpdateSeries() {
171      if (InvokeRequired) Invoke((Action)UpdateSeries);
172      else {
173
174        if (this.chart.Series.Contains(qualitySeries) && qualitySeries.Points.Count() != 0) {
175          FillSeries(Content.Qualities, Content.Solutions, qualitySeries);
176        }
177        if (this.chart.Series.Contains(paretoSeries) && paretoSeries.Points.Count() != 0) {
178          FillSeries(Content.ParetoFront, null, paretoSeries);
179        }
180
181
182        double minX = Double.MaxValue;
183        double maxX = Double.MinValue;
184        double minY = Double.MaxValue;
185        double maxY = Double.MinValue;
186        foreach (Series s in this.chart.Series) {
187          if (s.Points.Count == 0) continue;
188          minX = Math.Min(minX, s.Points.Select(p => p.XValue).Min());
189          maxX = Math.Max(maxX, s.Points.Select(p => p.XValue).Max());
190          minY = Math.Min(minY, s.Points.Select(p => p.YValues.Min()).Min());
191          maxY = Math.Max(maxY, s.Points.Select(p => p.YValues.Max()).Max());
192        }
193
194        maxX = maxX + 0.2 * Math.Abs(maxX);
195        minX = minX - 0.2 * Math.Abs(minX);
196        maxY = maxY + 0.2 * Math.Abs(maxY);
197        minY = minY - 0.2 * Math.Abs(minY);
198
199        double interestingValuesRangeX = maxX - minX;
200        double interestingValuesRangeY = maxY - minY;
201
202        int digitsX = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRangeX));
203        int digitsY = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRangeY));
204
205
206        maxX = Math.Round(maxX, digitsX);
207        minX = Math.Round(minX, digitsX);
208        maxY = Math.Round(maxY, digitsY);
209        minY = Math.Round(minY, digitsY);
210        if (minX > maxX) {
211          minX = 0;
212          maxX = 1;
213        }
214        if (minY > maxY) {
215          minY = 0;
216          maxY = 1;
217        }
218
219
220        this.chart.ChartAreas[0].AxisX.Maximum = maxX;
221        this.chart.ChartAreas[0].AxisX.Minimum = minX;
222        this.chart.ChartAreas[0].AxisY.Maximum = maxY;
223        this.chart.ChartAreas[0].AxisY.Minimum = minY;
224        UpdateCursorInterval();
225      }
226    }
227
228    private void ClearChart() {
229      if (chart.Series.Contains(qualitySeries)) chart.Series.Remove(qualitySeries);
230      if (chart.Series.Contains(paretoSeries)) chart.Series.Remove(paretoSeries);
231      BuildEmptySeries();
232    }
233
234    private void ToggleSeriesData(Series series) {
235      if (series.Points.Count > 0) {  //checks if series is shown
236        series.Points.Clear();
237      } else if (Content != null) {
238        switch (series.Name) {
239          case PARETO_FRONT:
240            FillSeries(Content.ParetoFront, null, this.chart.Series[PARETO_FRONT]);
241            break;
242          case QUALITIES:
243            FillSeries(Content.Qualities, Content.Solutions, this.chart.Series[QUALITIES]);
244            break;
245        }
246      }
247    }
248
249    private void chart_MouseDown(object sender, MouseEventArgs e) {
250      HitTestResult result = chart.HitTest(e.X, e.Y);
251      if (result.ChartElementType == ChartElementType.LegendItem) {
252        this.ToggleSeriesData(result.Series);
253      }
254
255    }
256
257    private void chart_MouseMove(object sender, MouseEventArgs e) {
258      HitTestResult result = chart.HitTest(e.X, e.Y);
259      if (result.ChartElementType == ChartElementType.LegendItem)
260        this.Cursor = Cursors.Hand;
261      else
262        this.Cursor = Cursors.Default;
263    }
264
265    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
266      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
267      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
268    }
269
270    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
271      if (this.chart.Series.Contains(qualitySeries)) e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[QUALITIES].Points.Count == 0 ? Color.Gray : Color.Black;
272      if (this.chart.Series.Contains(paretoSeries)) e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[PARETO_FRONT].Points.Count == 0 ? Color.Gray : Color.Black;
273    }
274
275    private void AddMenuItems() {
276      chooseDimensionToolStripMenuItem.DropDownItems.Clear();
277      chooseYDimensionToolStripMenuItem.DropDownItems.Clear();
278      if (Content == null) { return; }
279      int i = 0;
280      for (; i < Content.Objectives; i++) {
281        //add Menu Points
282        ToolStripMenuItem xItem = MakeMenuItem("X", "Objective " + i, i);
283        ToolStripMenuItem yItem = MakeMenuItem("Y", "Objective " + i, i);
284        xItem.Click += new System.EventHandler(this.XMenu_Click);
285        yItem.Click += new System.EventHandler(this.YMenu_Click);
286        chooseDimensionToolStripMenuItem.DropDownItems.Add(xItem);
287        chooseYDimensionToolStripMenuItem.DropDownItems.Add(yItem);
288      }
289
290      for (; i < Content.Solutions[0].Length + Content.Objectives; i++) {
291        ToolStripMenuItem xItem = MakeMenuItem("X", "ProblemDimension " + (i - Content.Objectives), i);
292        ToolStripMenuItem yItem = MakeMenuItem("Y", "ProblemDimension " + (i - Content.Objectives), i); ;
293        xItem.Click += new System.EventHandler(this.XMenu_Click);
294        yItem.Click += new System.EventHandler(this.YMenu_Click);
295        chooseDimensionToolStripMenuItem.DropDownItems.Add(xItem);
296        chooseYDimensionToolStripMenuItem.DropDownItems.Add(yItem);
297      }
298    }
299
300    private ToolStripMenuItem MakeMenuItem(String axis, String label, int i) {
301      ToolStripMenuItem xItem = new ToolStripMenuItem();
302      xItem.Name = "obj" + i;
303      xItem.Size = new System.Drawing.Size(269, 38);
304      xItem.Text = label;
305      return xItem;
306    }
307
308    private void YMenu_Click(object sender, EventArgs e) {
309      ToolStripMenuItem item = (ToolStripMenuItem)sender;
310      yDim = Int32.Parse(item.Name.Remove(0, 3));
311      String label = item.Text;
312      this.chooseYDimensionToolStripMenuItem.Text = label;
313      this.chart.ChartAreas[0].AxisY.Title = label;
314      UpdateChart();
315    }
316
317    private void XMenu_Click(object sender, EventArgs e) {
318      ToolStripMenuItem item = (ToolStripMenuItem)sender;
319      xDim = Int32.Parse(item.Name.Remove(0, 3));
320      String label = item.Text;
321      this.chooseDimensionToolStripMenuItem.Text = label;
322      this.chart.ChartAreas[0].AxisX.Title = label;
323      UpdateChart();
324    }
325
326    private void FillSeries(double[][] qualities, double[][] solutions, Series series) {
327      series.Points.Clear();
328      if (qualities == null || qualities.Length == 0) return;
329      int jx = xDim - qualities[0].Length;
330      int jy = yDim - qualities[0].Length;
331      if ((jx >= 0 || jy >= 0) && solutions == null) {
332        return;
333      }
334      for (int i = 0; i < qualities.Length; i++) {   //Assumtion: Columnwise
335        double[] d = qualities[i];
336        double[] q = null;
337        if (jx >= 0 || jy >= 0) { q = solutions[i]; }
338        series.Points.AddXY(jx < 0 ? d[xDim] : q[jx], jy < 0 ? d[yDim] : q[jy]);
339      }
340    }
341
342    private void BuildEmptySeries() {
343
344      this.chart.Series.Add(QUALITIES);
345      qualitySeries = this.chart.Series[QUALITIES];
346
347      this.chart.Series[QUALITIES].LegendText = QUALITIES;
348      this.chart.Series[QUALITIES].ChartType = SeriesChartType.FastPoint;
349
350      this.chart.Series.Add(PARETO_FRONT);
351      paretoSeries = this.chart.Series[PARETO_FRONT];
352      paretoSeries.Color = Color.FromArgb(100, Color.Orange);
353      this.chart.Series[PARETO_FRONT].LegendText = PARETO_FRONT;
354      this.chart.Series[PARETO_FRONT].ChartType = SeriesChartType.FastPoint;
355    }
356  }
357}
358
Note: See TracBrowser for help on using the repository browser.