Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions.Views/3.3/MultiObjectiveTestFunctionParetoFrontScatterPlotView.cs @ 14777

Last change on this file since 14777 was 14132, checked in by pfleck, 8 years ago

#1087 Corrected namespace of the MultiObjectiveTestFunctionParetoFrontScatterPlotView.

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