Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Problems.TestFunctions.Views/3.3/ParetoFrontScatterPlotView.cs @ 16605

Last change on this file since 16605 was 16453, checked in by jkarder, 6 years ago

#2520: updated year of copyrights

File size: 6.6 KB
RevLine 
[13672]1#region License Information
2/* HeuristicLab
[16453]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[13672]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
[15203]21
[13672]22using System;
[13620]23using System.Linq;
[15203]24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
[13672]26using HeuristicLab.Core.Views;
[13620]27using HeuristicLab.MainForm;
[14132]28using HeuristicLab.Problems.TestFunctions.MultiObjective;
[13620]29
[14132]30namespace HeuristicLab.Problems.TestFunctions.Views {
[13988]31  [View("Scatter Plot")]
[15203]32  [Content(typeof(ParetoFrontScatterPlot))]
33  public partial class ParetoFrontScatterPlotView : ItemView {
[13620]34
[15203]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; }
[13988]46      set { base.Content = value; }
47    }
[13620]48
[15203]49    public ParetoFrontScatterPlotView() {
[13988]50      InitializeComponent();
[13620]51
[15203]52      scatterPlot = new ScatterPlot();
[13620]53
[15203]54      qualitiesRow = new ScatterPlotDataRow("Qualities", string.Empty, Enumerable.Empty<Point2D<double>>()) {
55        VisualProperties = {
56          PointSize = 8 ,
57          PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle
[13894]58        }
[15203]59      };
60      scatterPlot.Rows.Add(qualitiesRow);
[13672]61
[15203]62      paretoFrontRow = new ScatterPlotDataRow("Best Known Pareto Front", string.Empty, Enumerable.Empty<Point2D<double>>()) {
63        VisualProperties = {
64            PointSize = 4,
65            PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Square
[13988]66          }
[15203]67      };
68      scatterPlot.Rows.Add(paretoFrontRow);
[13988]69    }
[13894]70
[13988]71    protected override void OnContentChanged() {
72      base.OnContentChanged();
[13894]73
[15203]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;
[13988]81      }
[13894]82
[15203]83      scatterPlotView.Content = scatterPlot;
[13620]84
[15203]85      if (oldObjectives != Content.Objectives || oldProblemSize != Content.ProblemSize)
86        UpdateAxisComboBoxes();
[13620]87
[15203]88      UpdateChartData();
[13620]89
[15203]90      oldObjectives = Content.Objectives;
91      oldProblemSize = Content.ProblemSize;
92    }
[13620]93
[15203]94    private void UpdateChartData() {
95      if (InvokeRequired) {
96        Invoke((Action)UpdateChartData);
97        return;
[13988]98      }
[13620]99
[15203]100      int xDimGlobal = xAxisComboBox.SelectedIndex;
101      int yDimGlobal = yAxisComboBox.SelectedIndex;
[13725]102
[15203]103      qualitiesRow.Points.Replace(CreatePoints(Content.Qualities, Content.Solutions, xDimGlobal, yDimGlobal));
[13620]104
[15203]105      paretoFrontRow.Points.Replace(CreatePoints(Content.ParetoFront, null, xDimGlobal, yDimGlobal));
106      paretoFrontRow.VisualProperties.IsVisibleInLegend = paretoFrontRow.Points.Count > 0; // hide if empty
107    }
[13620]108
[15203]109    private void UpdateAxisComboBoxes() {
110      try {
111        suppressEvents = true;
[13620]112
[15203]113        string prevSelectedX = (string)xAxisComboBox.SelectedItem;
114        string prevSelectedY = (string)yAxisComboBox.SelectedItem;
[13620]115
[15203]116        xAxisComboBox.Items.Clear();
117        yAxisComboBox.Items.Clear();
[13620]118
[15203]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        }
[13620]124
[15203]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);
[13988]129        }
[13620]130
[15203]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;
[13620]137
[15203]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;
[13894]144        }
[13672]145
[15203]146        UpdateAxisDescription();
147      } finally {
148        suppressEvents = false;
[13988]149      }
150    }
[13672]151
[15203]152    private void UpdateAxisDescription() {
153      scatterPlot.VisualProperties.XAxisTitle = (string)xAxisComboBox.SelectedItem;
154      scatterPlot.VisualProperties.YAxisTitle = (string)yAxisComboBox.SelectedItem;
[13988]155    }
[13672]156
[15203]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];
[13620]159
[15203]160      int objectives = qualities[0].Length;
[13620]161
[15203]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;
[13620]168
[15203]169      if (xDimArray == null || yDimArray == null)
170        return new Point2D<double>[0];
[13620]171
[15203]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]);
[13988]175      }
[15203]176      return points;
[13988]177    }
[13620]178
[15203]179    #region Event Handler
180    private void axisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
181      if (suppressEvents) return;
182      UpdateAxisDescription();
183      UpdateChartData();
[13620]184    }
[15203]185    #endregion
[13988]186  }
[15203]187}
Note: See TracBrowser for help on using the repository browser.