Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Analysis.Views/3.3/ParetoFrontScatterPlotViewOfT.cs @ 17230

Last change on this file since 17230 was 17230, checked in by abeham, 5 years ago

#2521: add multi-objective analysis to all multi-objective encoding-base problems

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Analysis.Views {
32  [View("Scatter Plot")]
33  [Content(typeof(ParetoFrontScatterPlot<>))]
34  public partial class ParetoFrontScatterPlotView<T> : ItemView where T : class, IItem {
35
36    private readonly ScatterPlot scatterPlot;
37    private LinkedList<ScatterPlotDataRow> frontsRow;
38    private ScatterPlotDataRow bestKnownFrontRow;
39
40    private int oldObjectives = -1;
41
42    private bool suppressEvents;
43
44    public new ParetoFrontScatterPlot<T> Content {
45      get { return (ParetoFrontScatterPlot<T>)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public ParetoFrontScatterPlotView() {
50      InitializeComponent();
51
52      scatterPlot = new ScatterPlot();
53    }
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57
58      if (Content == null) {
59        scatterPlotView.Content = null;
60        xAxisComboBox.Items.Clear();
61        xAxisComboBox.SelectedIndex = -1;
62        yAxisComboBox.Items.Clear();
63        yAxisComboBox.SelectedIndex = -1;
64        return;
65      }
66
67      scatterPlotView.Content = scatterPlot;
68
69      if (oldObjectives != Content.Objectives)
70        UpdateAxisComboBoxes();
71
72      UpdateChartData();
73
74      oldObjectives = Content.Objectives;
75    }
76
77    private void UpdateChartData() {
78      if (InvokeRequired) {
79        Invoke((Action)UpdateChartData);
80        return;
81      }
82
83      int xDimIndex = xAxisComboBox.SelectedIndex;
84      int yDimIndex = yAxisComboBox.SelectedIndex;
85
86      if (Content.BestKnownFront != null && Content.BestKnownFront.Count > 0) {
87        if (bestKnownFrontRow == null) {
88          bestKnownFrontRow = new ScatterPlotDataRow("Best Known Pareto Front", string.Empty, Enumerable.Empty<Point2D<double>>()) {
89            VisualProperties = {
90            PointSize = 4,
91            PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Square,
92            Color = Color.Gray
93          }
94          };
95          scatterPlot.Rows.Add(bestKnownFrontRow);
96        }
97        bestKnownFrontRow.Points.Replace(CreatePoints(Content.BestKnownFront, null, xDimIndex, yDimIndex));
98      } else if (bestKnownFrontRow != null) {
99        scatterPlot.Rows.Remove(bestKnownFrontRow);
100        bestKnownFrontRow = null;
101      }
102
103      if (Content.Fronts == null || Content.Fronts.Count == 0) {
104        if (frontsRow != null) {
105          foreach (var row in frontsRow) scatterPlot.Rows.Remove(row);
106          frontsRow = null;
107        }
108      } else {
109        if (frontsRow == null) frontsRow = new LinkedList<ScatterPlotDataRow>();
110        var row = frontsRow.First;
111        var front = 0;
112        while (front < Content.Fronts.Count) {
113          if (Content.Fronts[front].Length == 0) break;
114          if(row == null) {
115            row = frontsRow.AddLast(new ScatterPlotDataRow("Front " + front, "Points on Front #" + front, Enumerable.Empty<Point2D<double>>()) {
116              VisualProperties = {
117                  PointSize = 8,
118                  PointStyle = front == 0 ? ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Diamond : ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle,
119                  Color = front == 0 ? Color.Goldenrod : Color.DodgerBlue
120                }
121            });
122            scatterPlot.Rows.Add(row.Value);
123          }
124          row.Value.Points.Replace(CreatePoints(Content.Fronts[front], Content.Items[front], xDimIndex, yDimIndex));
125          row = row.Next;
126          front++;
127        }
128        while (row != null) {
129          scatterPlot.Rows.Remove(row.Value);
130          var next = row.Next;
131          frontsRow.Remove(row);
132          row = next;
133        }
134      }
135    }
136
137    private void UpdateAxisComboBoxes() {
138      try {
139        suppressEvents = true;
140
141        string prevSelectedX = (string)xAxisComboBox.SelectedItem;
142        string prevSelectedY = (string)yAxisComboBox.SelectedItem;
143
144        xAxisComboBox.Items.Clear();
145        yAxisComboBox.Items.Clear();
146
147        // Add Objectives first
148        for (int i = 0; i < Content.Objectives; i++) {
149          xAxisComboBox.Items.Add("Objective " + i);
150          yAxisComboBox.Items.Add("Objective " + i);
151        }
152
153        // Selection
154        int count = xAxisComboBox.Items.Count;
155        if (count > 0) {
156          if (prevSelectedX != null && xAxisComboBox.Items.Contains(prevSelectedX))
157            xAxisComboBox.SelectedItem = prevSelectedX;
158          else xAxisComboBox.SelectedIndex = 0;
159
160          if (prevSelectedY != null && yAxisComboBox.Items.Contains(prevSelectedY))
161            yAxisComboBox.SelectedItem = prevSelectedY;
162          else yAxisComboBox.SelectedIndex = Math.Min(1, count - 1);
163        } else {
164          xAxisComboBox.SelectedIndex = -1;
165          yAxisComboBox.SelectedIndex = -1;
166        }
167
168        UpdateAxisDescription();
169      } finally {
170        suppressEvents = false;
171      }
172    }
173
174    private void UpdateAxisDescription() {
175      scatterPlot.VisualProperties.XAxisTitle = (string)xAxisComboBox.SelectedItem;
176      scatterPlot.VisualProperties.YAxisTitle = (string)yAxisComboBox.SelectedItem;
177    }
178
179    private static Point2D<double>[] CreatePoints(IReadOnlyList<double[]> front, T[] solutions, int xDimIndex, int yDimIndex) {
180      if (front == null || front.Count == 0) return new Point2D<double>[0];
181     
182      var points = new Point2D<double>[front.Count];
183      for (int i = 0; i < front.Count; i++) {
184        points[i] = new Point2D<double>(front[i][xDimIndex], front[i][yDimIndex], solutions != null ? solutions[i] : null);
185      }
186      return points;
187    }
188
189    #region Event Handler
190    private void axisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
191      if (suppressEvents) return;
192      UpdateAxisDescription();
193      UpdateChartData();
194    }
195    #endregion
196  }
197}
Note: See TracBrowser for help on using the repository browser.