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