Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Problems.TestFunctions.Views/3.3/ParetoFrontScatterPlotView.cs @ 16154

Last change on this file since 16154 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.TestFunctions.MultiObjective;
29
30namespace HeuristicLab.Problems.TestFunctions.Views {
31  [View("Scatter Plot")]
32  [Content(typeof(ParetoFrontScatterPlot))]
33  public partial class ParetoFrontScatterPlotView : ItemView {
34
35    private readonly ScatterPlot scatterPlot;
36    private readonly ScatterPlotDataRow qualitiesRow;
37    private readonly ScatterPlotDataRow paretoFrontRow;
38
39    private int oldObjectives = -1;
40    private int oldProblemSize = -1;
41
42    private bool suppressEvents;
43
44    public new ParetoFrontScatterPlot Content {
45      get { return (ParetoFrontScatterPlot)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public ParetoFrontScatterPlotView() {
50      InitializeComponent();
51
52      scatterPlot = new ScatterPlot();
53
54      qualitiesRow = new ScatterPlotDataRow("Qualities", string.Empty, Enumerable.Empty<Point2D<double>>()) {
55        VisualProperties = {
56          PointSize = 8 ,
57          PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle
58        }
59      };
60      scatterPlot.Rows.Add(qualitiesRow);
61
62      paretoFrontRow = new ScatterPlotDataRow("Best Known Pareto Front", string.Empty, Enumerable.Empty<Point2D<double>>()) {
63        VisualProperties = {
64            PointSize = 4,
65            PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Square
66          }
67      };
68      scatterPlot.Rows.Add(paretoFrontRow);
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73
74      if (Content == null) {
75        scatterPlotView.Content = null;
76        xAxisComboBox.Items.Clear();
77        xAxisComboBox.SelectedIndex = -1;
78        yAxisComboBox.Items.Clear();
79        yAxisComboBox.SelectedIndex = -1;
80        return;
81      }
82
83      scatterPlotView.Content = scatterPlot;
84
85      if (oldObjectives != Content.Objectives || oldProblemSize != Content.ProblemSize)
86        UpdateAxisComboBoxes();
87
88      UpdateChartData();
89
90      oldObjectives = Content.Objectives;
91      oldProblemSize = Content.ProblemSize;
92    }
93
94    private void UpdateChartData() {
95      if (InvokeRequired) {
96        Invoke((Action)UpdateChartData);
97        return;
98      }
99
100      int xDimGlobal = xAxisComboBox.SelectedIndex;
101      int yDimGlobal = yAxisComboBox.SelectedIndex;
102
103      qualitiesRow.Points.Replace(CreatePoints(Content.Qualities, Content.Solutions, xDimGlobal, yDimGlobal));
104
105      paretoFrontRow.Points.Replace(CreatePoints(Content.ParetoFront, null, xDimGlobal, yDimGlobal));
106      paretoFrontRow.VisualProperties.IsVisibleInLegend = paretoFrontRow.Points.Count > 0; // hide if empty
107    }
108
109    private void UpdateAxisComboBoxes() {
110      try {
111        suppressEvents = true;
112
113        string prevSelectedX = (string)xAxisComboBox.SelectedItem;
114        string prevSelectedY = (string)yAxisComboBox.SelectedItem;
115
116        xAxisComboBox.Items.Clear();
117        yAxisComboBox.Items.Clear();
118
119        // Add Objectives first
120        for (int i = 0; i < Content.Objectives; i++) {
121          xAxisComboBox.Items.Add("Objective " + i);
122          yAxisComboBox.Items.Add("Objective " + i);
123        }
124
125        // Add Problem Dimension
126        for (int i = 0; i < Content.ProblemSize; i++) {
127          xAxisComboBox.Items.Add("Problem Dimension " + i);
128          yAxisComboBox.Items.Add("Problem Dimension " + i);
129        }
130
131        // Selection
132        int count = xAxisComboBox.Items.Count;
133        if (count > 0) {
134          if (prevSelectedX != null && xAxisComboBox.Items.Contains(prevSelectedX))
135            xAxisComboBox.SelectedItem = prevSelectedX;
136          else xAxisComboBox.SelectedIndex = 0;
137
138          if (prevSelectedY != null && yAxisComboBox.Items.Contains(prevSelectedY))
139            yAxisComboBox.SelectedItem = prevSelectedY;
140          else yAxisComboBox.SelectedIndex = Math.Min(1, count - 1);
141        } else {
142          xAxisComboBox.SelectedIndex = -1;
143          yAxisComboBox.SelectedIndex = -1;
144        }
145
146        UpdateAxisDescription();
147      } finally {
148        suppressEvents = false;
149      }
150    }
151
152    private void UpdateAxisDescription() {
153      scatterPlot.VisualProperties.XAxisTitle = (string)xAxisComboBox.SelectedItem;
154      scatterPlot.VisualProperties.YAxisTitle = (string)yAxisComboBox.SelectedItem;
155    }
156
157    private static Point2D<double>[] CreatePoints(double[][] qualities, double[][] solutions, int xDimGlobal, int yDimGlobal) {
158      if (qualities == null || qualities.Length == 0) return new Point2D<double>[0];
159
160      int objectives = qualities[0].Length;
161
162      // "Global" dimension index describes the index as if the qualities and solutions would be in a single array
163      // If the global dimension index is too long for the qualities, use solutions
164      var xDimArray = xDimGlobal < objectives ? qualities : solutions;
165      var yDimArray = yDimGlobal < objectives ? qualities : solutions;
166      var xDimIndex = xDimGlobal < objectives ? xDimGlobal : xDimGlobal - objectives;
167      var yDimIndex = yDimGlobal < objectives ? yDimGlobal : yDimGlobal - objectives;
168
169      if (xDimArray == null || yDimArray == null)
170        return new Point2D<double>[0];
171
172      var points = new Point2D<double>[xDimArray.Length];
173      for (int i = 0; i < xDimArray.Length; i++) {
174        points[i] = new Point2D<double>(xDimArray[i][xDimIndex], yDimArray[i][yDimIndex]);
175      }
176      return points;
177    }
178
179    #region Event Handler
180    private void axisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
181      if (suppressEvents) return;
182      UpdateAxisDescription();
183      UpdateChartData();
184    }
185    #endregion
186  }
187}
Note: See TracBrowser for help on using the repository browser.