[3647] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3647] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Drawing;
|
---|
| 24 | using System.Windows.Forms;
|
---|
| 25 | using HeuristicLab.Core.Views;
|
---|
[3665] | 26 | using HeuristicLab.Data;
|
---|
[4068] | 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[3647] | 28 | using HeuristicLab.MainForm;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.TestFunctions.Views {
|
---|
| 31 | /// <summary>
|
---|
[3661] | 32 | /// A view for a SingleObjectiveTestFunctions solution.
|
---|
[3647] | 33 | /// </summary>
|
---|
[3661] | 34 | [View("Single Objective Test Functions View")]
|
---|
[3647] | 35 | [Content(typeof(SingleObjectiveTestFunctionSolution), true)]
|
---|
[3665] | 36 | public partial class SingleObjectiveTestFunctionSolutionView : ItemView {
|
---|
| 37 | private Bitmap backgroundImage;
|
---|
| 38 |
|
---|
[3647] | 39 | public new SingleObjectiveTestFunctionSolution Content {
|
---|
| 40 | get { return (SingleObjectiveTestFunctionSolution)base.Content; }
|
---|
| 41 | set { base.Content = value; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public SingleObjectiveTestFunctionSolutionView() {
|
---|
| 45 | InitializeComponent();
|
---|
[3665] | 46 | pictureBox.SizeChanged += new EventHandler(pictureBox_SizeChanged);
|
---|
[3647] | 47 | qualityView.ReadOnly = true;
|
---|
| 48 | realVectorView.ReadOnly = true;
|
---|
[3665] | 49 | backgroundImage = null;
|
---|
[3647] | 50 | }
|
---|
| 51 |
|
---|
[3649] | 52 | protected override void DeregisterContentEvents() {
|
---|
[3665] | 53 | Content.BestKnownRealVectorChanged -= new EventHandler(Content_BestKnownRealVectorChanged);
|
---|
| 54 | Content.BestRealVectorChanged -= new EventHandler(Content_BestRealVectorChanged);
|
---|
[3649] | 55 | Content.QualityChanged -= new EventHandler(Content_QualityChanged);
|
---|
[3665] | 56 | Content.PopulationChanged -= new EventHandler(Content_PopulationChanged);
|
---|
[3894] | 57 | Content.BoundsChanged -= new EventHandler(Content_BoundsChanged);
|
---|
[3649] | 58 | base.DeregisterContentEvents();
|
---|
| 59 | }
|
---|
| 60 | protected override void RegisterContentEvents() {
|
---|
| 61 | base.RegisterContentEvents();
|
---|
[3665] | 62 | Content.BestKnownRealVectorChanged += new EventHandler(Content_BestKnownRealVectorChanged);
|
---|
| 63 | Content.BestRealVectorChanged += new EventHandler(Content_BestRealVectorChanged);
|
---|
[3649] | 64 | Content.QualityChanged += new EventHandler(Content_QualityChanged);
|
---|
[3665] | 65 | Content.PopulationChanged += new EventHandler(Content_PopulationChanged);
|
---|
[3894] | 66 | Content.BoundsChanged += new EventHandler(Content_BoundsChanged);
|
---|
[3649] | 67 | }
|
---|
| 68 |
|
---|
[3665] | 69 | private void Content_BestKnownRealVectorChanged(object sender, EventArgs e) {
|
---|
[3649] | 70 | if (InvokeRequired)
|
---|
[3665] | 71 | Invoke(new EventHandler(Content_BestKnownRealVectorChanged), sender, e);
|
---|
[3649] | 72 | else {
|
---|
[3665] | 73 | GenerateImage();
|
---|
[3649] | 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[3665] | 77 | private void Content_BestRealVectorChanged(object sender, EventArgs e) {
|
---|
[3649] | 78 | if (InvokeRequired)
|
---|
[3667] | 79 | Invoke(new EventHandler(Content_BestRealVectorChanged), sender, e);
|
---|
[3649] | 80 | else {
|
---|
[3661] | 81 | realVectorView.Content = Content.BestRealVector;
|
---|
| 82 | pictureBox.Visible = Content.BestRealVector.Length == 2;
|
---|
[3665] | 83 | GenerateImage();
|
---|
[3649] | 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[3665] | 87 | private void Content_QualityChanged(object sender, EventArgs e) {
|
---|
| 88 | if (InvokeRequired)
|
---|
| 89 | Invoke(new EventHandler(Content_QualityChanged), sender, e);
|
---|
| 90 | else {
|
---|
| 91 | qualityView.Content = Content.BestQuality;
|
---|
| 92 | pictureBox.Visible = Content.BestRealVector.Length == 2;
|
---|
| 93 | GenerateImage();
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | private void Content_PopulationChanged(object sender, EventArgs e) {
|
---|
| 98 | if (InvokeRequired)
|
---|
| 99 | Invoke(new EventHandler(Content_PopulationChanged), sender, e);
|
---|
| 100 | else {
|
---|
| 101 | GenerateImage();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[3894] | 105 | private void Content_BoundsChanged(object sender, EventArgs e) {
|
---|
| 106 | if (InvokeRequired)
|
---|
| 107 | Invoke(new EventHandler(Content_BoundsChanged), sender, e);
|
---|
| 108 | else
|
---|
| 109 | GenerateImage();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[3647] | 112 | protected override void OnContentChanged() {
|
---|
| 113 | base.OnContentChanged();
|
---|
| 114 | if (Content == null) {
|
---|
| 115 | qualityView.Content = null;
|
---|
| 116 | realVectorView.Content = null;
|
---|
| 117 | } else {
|
---|
[3661] | 118 | qualityView.Content = Content.BestQuality;
|
---|
| 119 | realVectorView.Content = Content.BestRealVector;
|
---|
| 120 |
|
---|
| 121 | pictureBox.Visible = Content.BestRealVector.Length == 2;
|
---|
[3665] | 122 | GenerateImage();
|
---|
[3647] | 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[3668] | 126 | protected override void OnPaint(PaintEventArgs e) {
|
---|
| 127 | GenerateImage();
|
---|
| 128 | base.OnPaint(e);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[3904] | 131 | protected override void SetEnabledStateOfControls() {
|
---|
| 132 | base.SetEnabledStateOfControls();
|
---|
[3647] | 133 | qualityView.Enabled = Content != null;
|
---|
| 134 | realVectorView.Enabled = Content != null;
|
---|
| 135 | }
|
---|
[3665] | 136 |
|
---|
| 137 | private void GenerateImage() {
|
---|
| 138 | if (pictureBox.Enabled && pictureBox.Width > 0 && pictureBox.Height > 0) {
|
---|
| 139 | if (Content == null) {
|
---|
| 140 | pictureBox.Image = null;
|
---|
| 141 | } else {
|
---|
| 142 | if (backgroundImage == null) {
|
---|
| 143 | GenerateBackgroundImage();
|
---|
| 144 | pictureBox.Image = backgroundImage;
|
---|
| 145 | }
|
---|
| 146 | pictureBox.Refresh();
|
---|
[3894] | 147 | DoubleMatrix bounds = Content.Bounds;
|
---|
| 148 | if (bounds == null) bounds = Content.Evaluator.Bounds;
|
---|
[3665] | 149 | double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
|
---|
| 150 | double xStep = backgroundImage.Width / (xMax - xMin), yStep = backgroundImage.Height / (yMax - yMin);
|
---|
| 151 | using (Graphics graphics = pictureBox.CreateGraphics()) {
|
---|
| 152 | if (Content.BestKnownRealVector != null) {
|
---|
| 153 | Pen cross = new Pen(Brushes.Red, 2.0f);
|
---|
| 154 | float a = (float)((Content.BestKnownRealVector[0] - xMin) * xStep);
|
---|
| 155 | float b = (float)((Content.BestKnownRealVector[1] - yMin) * yStep);
|
---|
| 156 | graphics.DrawLine(cross, a - 4, b - 4, a + 4, b + 4);
|
---|
| 157 | graphics.DrawLine(cross, a - 4, b + 4, a + 4, b - 4);
|
---|
| 158 | }
|
---|
[5204] | 159 | if (Content.Population != null) {
|
---|
| 160 | foreach (RealVector vector in Content.Population)
|
---|
| 161 | graphics.FillEllipse(Brushes.Blue, (float)((vector[0] - xMin) * xStep - 4), (float)((vector[1] - yMin) * yStep - 4), 8.0f, 8.0f);
|
---|
| 162 | }
|
---|
[3665] | 163 | if (Content.BestRealVector != null) {
|
---|
| 164 | graphics.FillEllipse(Brushes.Green, (float)((Content.BestRealVector[0] - xMin) * xStep - 5), (float)((Content.BestRealVector[1] - yMin) * yStep - 5), 10.0f, 10.0f);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | private void GenerateBackgroundImage() {
|
---|
| 172 | if (backgroundImage != null)
|
---|
| 173 | backgroundImage.Dispose();
|
---|
| 174 | backgroundImage = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
[3894] | 175 | DoubleMatrix bounds = Content.Bounds;
|
---|
| 176 | if (bounds == null) bounds = Content.Evaluator.Bounds;
|
---|
[3665] | 177 | double xMin = bounds[0, 0], xMax = bounds[0, 1], yMin = bounds[1 % bounds.Rows, 0], yMax = bounds[1 % bounds.Rows, 1];
|
---|
| 178 | double xStep = (xMax - xMin) / backgroundImage.Width, yStep = (yMax - yMin) / backgroundImage.Height;
|
---|
| 179 | double minPoint = Double.MaxValue, maxPoint = Double.MinValue;
|
---|
| 180 | DoubleMatrix points = new DoubleMatrix(backgroundImage.Height, backgroundImage.Width);
|
---|
| 181 | for (int i = 0; i < backgroundImage.Width; i++)
|
---|
| 182 | for (int j = 0; j < backgroundImage.Height; j++) {
|
---|
| 183 | points[j, i] = Content.Evaluator.Evaluate2D(xMin + i * xStep, yMin + j * yStep);
|
---|
| 184 | if (points[j, i] < minPoint) minPoint = points[j, i];
|
---|
| 185 | if (points[j, i] > maxPoint) maxPoint = points[j, i];
|
---|
| 186 | }
|
---|
| 187 | double grayStep;
|
---|
| 188 | if (maxPoint == minPoint) grayStep = -1;
|
---|
| 189 | else grayStep = 100 / (maxPoint - minPoint);
|
---|
| 190 |
|
---|
| 191 | for (int i = 0; i < backgroundImage.Width; i++)
|
---|
| 192 | for (int j = 0; j < backgroundImage.Height; j++) {
|
---|
| 193 | int luminosity = (grayStep > 0) ? (int)Math.Round((points[j, i] - minPoint) * grayStep) : (128);
|
---|
| 194 | backgroundImage.SetPixel(i, j, Color.FromArgb(255 - luminosity, 255 - luminosity, 255 - luminosity));
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
| 199 | if (backgroundImage != null) backgroundImage.Dispose();
|
---|
| 200 | backgroundImage = null;
|
---|
| 201 | pictureBox.Image = null;
|
---|
[4068] | 202 | GenerateImage();
|
---|
[3665] | 203 | }
|
---|
| 204 |
|
---|
| 205 | protected override void OnClosing(FormClosingEventArgs e) {
|
---|
| 206 | pictureBox.Enabled = false;
|
---|
| 207 | if (backgroundImage != null) backgroundImage.Dispose();
|
---|
| 208 | backgroundImage = null;
|
---|
| 209 | pictureBox.Image = null;
|
---|
| 210 | base.OnClosing(e);
|
---|
| 211 | }
|
---|
[3647] | 212 | }
|
---|
| 213 | }
|
---|