[7526] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
[7541] | 23 | using System.Collections.Generic;
|
---|
[7526] | 24 | using System.Drawing;
|
---|
| 25 | using System.Globalization;
|
---|
[7541] | 26 | using System.Linq;
|
---|
[7526] | 27 | using System.Text;
|
---|
| 28 | using System.Text.RegularExpressions;
|
---|
[7541] | 29 | using System.Windows.Forms;
|
---|
[7526] | 30 | using HeuristicLab.Analysis;
|
---|
[7541] | 31 | using HeuristicLab.Common;
|
---|
| 32 | using HeuristicLab.Core.Views;
|
---|
[7526] | 33 | using HeuristicLab.Data;
|
---|
| 34 | using HeuristicLab.MainForm;
|
---|
| 35 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 36 |
|
---|
| 37 | namespace HeuristicLab.Optimizer.Tools {
|
---|
| 38 | [View("MDSView")]
|
---|
[7541] | 39 | [Content(typeof(DoubleMatrix), IsDefaultView = false)]
|
---|
| 40 | public partial class MDSView : ItemView {
|
---|
[7526] | 41 |
|
---|
[7541] | 42 | private static Color[] KellysDistinguishableColors = new Color[] {
|
---|
| 43 | Color.FromArgb(0xFF, Color.FromArgb(0xFFB300)),
|
---|
| 44 | Color.FromArgb(0xFF, Color.FromArgb(0x803E75)),
|
---|
| 45 | Color.FromArgb(0xFF, Color.FromArgb(0xFF6800)),
|
---|
| 46 | Color.FromArgb(0xFF, Color.FromArgb(0xA6BDD7)),
|
---|
| 47 | Color.FromArgb(0xFF, Color.FromArgb(0xC10020)),
|
---|
| 48 | Color.FromArgb(0xFF, Color.FromArgb(0xCEA262)),
|
---|
| 49 | Color.FromArgb(0xFF, Color.FromArgb(0x817066)),
|
---|
| 50 | Color.FromArgb(0xFF, Color.FromArgb(0x007D34)),
|
---|
| 51 | Color.FromArgb(0xFF, Color.FromArgb(0xF6768E)),
|
---|
| 52 | Color.FromArgb(0xFF, Color.FromArgb(0x00538A)),
|
---|
| 53 | Color.FromArgb(0xFF, Color.FromArgb(0x53377A)),
|
---|
| 54 | Color.FromArgb(0xFF, Color.FromArgb(0xB32851)),
|
---|
| 55 | Color.FromArgb(0xFF, Color.FromArgb(0x7F180D)),
|
---|
| 56 | Color.FromArgb(0xFF, Color.FromArgb(0x93AA00)),
|
---|
| 57 | Color.FromArgb(0xFF, Color.FromArgb(0x593315)),
|
---|
| 58 | Color.FromArgb(0xFF, Color.FromArgb(0xF13A13)),
|
---|
| 59 | Color.FromArgb(0xFF, Color.FromArgb(0x232C16)),
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | public new DoubleMatrix Content {
|
---|
| 63 | get { return (DoubleMatrix)base.Content; }
|
---|
| 64 | set { base.Content = value; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | private DoubleArray Characteristic { get; set; }
|
---|
| 68 |
|
---|
[7526] | 69 | public MDSView() {
|
---|
| 70 | InitializeComponent();
|
---|
[7541] | 71 | Characteristic = new DoubleArray();
|
---|
| 72 | characteristicView.Content = Characteristic;
|
---|
[7526] | 73 | }
|
---|
| 74 |
|
---|
| 75 | protected override void RegisterContentEvents() {
|
---|
| 76 | base.RegisterContentEvents();
|
---|
| 77 | Content.RowsChanged += new EventHandler(Content_Changed);
|
---|
| 78 | Content.ColumnsChanged += new EventHandler(Content_Changed);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | protected override void DeregisterContentEvents() {
|
---|
| 82 | Content.RowsChanged -= new EventHandler(Content_Changed);
|
---|
| 83 | Content.ColumnsChanged -= new EventHandler(Content_Changed);
|
---|
| 84 | base.DeregisterContentEvents();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | private void Content_Changed(object sender, EventArgs e) {
|
---|
| 88 | SetEnabledStateOfControls();
|
---|
| 89 | OnRedraw();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | protected override void OnContentChanged() {
|
---|
| 93 | base.OnContentChanged();
|
---|
[7541] | 94 | if (Content != null && Content.Rows != Characteristic.Length)
|
---|
| 95 | ((IStringConvertibleArray)Characteristic).Length = Content.Rows;
|
---|
[7526] | 96 | OnRedraw();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | protected override void SetEnabledStateOfControls() {
|
---|
| 100 | base.SetEnabledStateOfControls();
|
---|
[7541] | 101 | visualizationPanel.Enabled = Content != null && Content.Rows == Content.Columns && Content.Rows > 0;
|
---|
[7526] | 102 | }
|
---|
| 103 |
|
---|
[7541] | 104 | private void drawLinesCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
[7526] | 105 | OnRedraw();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[7541] | 108 | private void setNamesButton_Click(object sender, EventArgs e) {
|
---|
| 109 | SetNamesDialog dialog = null;
|
---|
| 110 | if (Content.RowNames != null && Content.RowNames.Any())
|
---|
| 111 | dialog = new SetNamesDialog(Content.RowNames);
|
---|
| 112 | else dialog = new SetNamesDialog(Content.Rows);
|
---|
| 113 |
|
---|
| 114 | try {
|
---|
| 115 | if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
|
---|
| 116 | Content.RowNames = dialog.Names;
|
---|
| 117 | Content.ColumnNames = dialog.Names;
|
---|
| 118 | OnRedraw();
|
---|
| 119 | }
|
---|
| 120 | } finally {
|
---|
| 121 | dialog.Dispose();
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void copyCoordinatesButton_Click(object sender, EventArgs e) {
|
---|
| 126 | try {
|
---|
| 127 | var coordinates = MultidimensionalScaling.KruskalShepard(Content);
|
---|
| 128 | StringBuilder s = new StringBuilder();
|
---|
| 129 |
|
---|
| 130 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
| 131 | s.Append(coordinates[i, 0].ToString());
|
---|
| 132 | s.Append('\t');
|
---|
| 133 | s.Append(coordinates[i, 1].ToString());
|
---|
| 134 | s.Append(Environment.NewLine);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | Clipboard.SetText(s.ToString());
|
---|
| 138 | } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Error: " + ex.ToString()); }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[7526] | 141 | private void OnRedraw() {
|
---|
| 142 | if (InvokeRequired) {
|
---|
| 143 | Invoke((Action)OnRedraw, null);
|
---|
| 144 | } else {
|
---|
[7541] | 145 | if (!visualizationPanel.Visible) return;
|
---|
[7526] | 146 | GenerateImage();
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | private void GenerateImage() {
|
---|
| 151 | if (Content.Rows == Content.Columns && Content.Rows > 0
|
---|
| 152 | && mdsPictureBox.Width > 0 && mdsPictureBox.Height > 0) {
|
---|
| 153 | Bitmap newBitmap = GenerateDistanceImage();
|
---|
| 154 | if (newBitmap != null) {
|
---|
| 155 | var oldImage = mdsPictureBox.Image;
|
---|
| 156 | mdsPictureBox.Image = newBitmap;
|
---|
| 157 | if (oldImage != null) oldImage.Dispose();
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | private Bitmap GenerateDistanceImage() {
|
---|
| 163 | Bitmap newBitmap = new Bitmap(mdsPictureBox.Width, mdsPictureBox.Height);
|
---|
| 164 |
|
---|
| 165 | DoubleMatrix coordinates;
|
---|
| 166 | double stress = double.NaN;
|
---|
| 167 | try {
|
---|
[7541] | 168 | coordinates = MultidimensionalScaling.KruskalShepard(Content);
|
---|
| 169 | stress = MultidimensionalScaling.CalculateNormalizedStress(Content, coordinates);
|
---|
| 170 | infoLabel.Text = "Stress: " + stress.ToString("0.00", CultureInfo.CurrentCulture.NumberFormat);
|
---|
[7526] | 171 | } catch {
|
---|
| 172 | WriteCenteredTextToBitmap(ref newBitmap, "Matrix is not symmetric");
|
---|
[7541] | 173 | infoLabel.Text = "-";
|
---|
[7526] | 174 | return newBitmap;
|
---|
| 175 | }
|
---|
| 176 | double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
|
---|
| 177 | double maxDistance = double.MinValue;
|
---|
| 178 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
| 179 | if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
|
---|
| 180 | if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
|
---|
| 181 | if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
|
---|
| 182 | if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
|
---|
| 183 |
|
---|
| 184 | for (int j = i + 1; j < coordinates.Rows; j++) {
|
---|
[7541] | 185 | if (Content[i, j] > maxDistance) maxDistance = Content[i, j];
|
---|
| 186 | if (Content[j, i] > maxDistance) maxDistance = Content[j, i];
|
---|
[7526] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | int border = 20;
|
---|
| 191 | double xStep = xMax != xMin ? (mdsPictureBox.Width - 2 * border) / (xMax - xMin) : 1;
|
---|
| 192 | double yStep = yMax != yMin ? (mdsPictureBox.Height - 2 * border) / (yMax - yMin) : 1;
|
---|
| 193 |
|
---|
| 194 | Point[] points = new Point[coordinates.Rows];
|
---|
| 195 | for (int i = 0; i < coordinates.Rows; i++)
|
---|
| 196 | points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
|
---|
| 197 | newBitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
|
---|
| 198 |
|
---|
| 199 | Random rand = new Random();
|
---|
| 200 | using (Graphics graphics = Graphics.FromImage(newBitmap)) {
|
---|
| 201 | graphics.Clear(Color.White);
|
---|
| 202 |
|
---|
[7541] | 203 | if (drawLinesCheckBox.Checked) {
|
---|
| 204 | for (int i = 0; i < coordinates.Rows - 1; i++) {
|
---|
| 205 | for (int j = i + 1; j < coordinates.Rows; j++) {
|
---|
| 206 | Point start = points[i], end = points[j];
|
---|
| 207 | string caption = String.Empty;
|
---|
| 208 | double d = Math.Max(Content[i, j], Content[j, i]);
|
---|
| 209 | float width = (float)Math.Ceiling(5.0 * d / maxDistance);
|
---|
| 210 | if (d > 0) {
|
---|
| 211 | graphics.DrawLine(new Pen(Color.IndianRed, width), start, end);
|
---|
| 212 | if (Content[i, j] != Content[j, i])
|
---|
| 213 | caption = Content[i, j].ToString(CultureInfo.InvariantCulture.NumberFormat)
|
---|
| 214 | + " / " + Content[j, i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 215 | else
|
---|
| 216 | caption = Content[i, j].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
| 217 | }
|
---|
| 218 | if (!String.IsNullOrEmpty(caption)) {
|
---|
| 219 | double r = rand.NextDouble();
|
---|
| 220 | while (r < 0.2 || r > 0.8) r = rand.NextDouble();
|
---|
| 221 | float x = (float)(start.X + (end.X - start.X) * r + 5);
|
---|
| 222 | float y = (float)(start.Y + (end.Y - start.Y) * r + 5);
|
---|
| 223 | graphics.DrawString(caption, Font, Brushes.Black, x, y);
|
---|
| 224 | }
|
---|
[7526] | 225 | }
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[7541] | 229 | string[] rowNames = null;
|
---|
| 230 | if (Content.RowNames != null && Content.RowNames.Any())
|
---|
| 231 | rowNames = Content.RowNames.ToArray();
|
---|
| 232 | double min = Characteristic.Min(), max = Characteristic.Max();
|
---|
| 233 | if (min == max) {
|
---|
| 234 | var colorCodes = new Dictionary<string, int>();
|
---|
| 235 | for (int i = 0; i < points.Length; i++) {
|
---|
| 236 | Point p = new Point(points[i].X - 3, points[i].Y - 3);
|
---|
| 237 | if (rowNames == null || (rowNames != null && i >= rowNames.Length)) {
|
---|
| 238 | graphics.FillRectangle(Brushes.Black, p.X, p.Y, 8, 8);
|
---|
| 239 | graphics.DrawString(i.ToString(), Font, Brushes.Black, p.X, p.Y + 10);
|
---|
| 240 | } else {
|
---|
| 241 | string pre = rowNames[i].Substring(0, 3);
|
---|
| 242 | string post = rowNames[i].Substring(3);
|
---|
| 243 | if (!colorCodes.ContainsKey(pre)) colorCodes[pre] = !colorCodes.Any() ? 0 : colorCodes.Values.Max() + 1;
|
---|
| 244 | var brush = new SolidBrush(KellysDistinguishableColors[colorCodes[pre]]);
|
---|
| 245 | var size = TextRenderer.MeasureText(post, Font);
|
---|
| 246 | graphics.FillRectangle(brush, p.X, p.Y, 8, 8);
|
---|
| 247 | graphics.DrawString(post, Font, brush, p.X - (size.Width / 2.0f), p.Y + 10);
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | int offset = 1;
|
---|
| 252 | foreach (var code in colorCodes) {
|
---|
| 253 | using (var brush = new SolidBrush(KellysDistinguishableColors[code.Value])) {
|
---|
| 254 | graphics.DrawString(code.Key, Font, brush, 5, 40 + offset * 20);
|
---|
| 255 | }
|
---|
| 256 | offset++;
|
---|
| 257 | }
|
---|
| 258 | } else {
|
---|
| 259 | var colors = ColorGradient.Colors.ToArray();
|
---|
| 260 | for (int i = 0; i < points.Length; i++) {
|
---|
| 261 | Point p = new Point(points[i].X - 3, points[i].Y - 3);
|
---|
| 262 | double interpolation = Math.Sqrt((Characteristic[i] - min) / (max - min));
|
---|
| 263 | Color c = colors[(int)Math.Floor((colors.Length - 1) * interpolation)];
|
---|
| 264 | var brush = new SolidBrush(c);
|
---|
| 265 | graphics.FillRectangle(brush, p.X, p.Y, 8, 8);
|
---|
| 266 | if (rowNames == null || (rowNames != null && i >= rowNames.Length)) {
|
---|
| 267 | graphics.DrawString(i.ToString(), Font, brush, p.X, p.Y + 10);
|
---|
| 268 | } else {
|
---|
| 269 | var size = TextRenderer.MeasureText(rowNames[i], Font);
|
---|
| 270 | graphics.DrawString(rowNames[i], Font, brush, p.X - (size.Width / 2.0f), p.Y + 10);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
[7526] | 273 | }
|
---|
| 274 | }
|
---|
| 275 | return newBitmap;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | private void WriteCenteredTextToBitmap(ref Bitmap bitmap, string text) {
|
---|
| 279 | if (bitmap == null) return;
|
---|
| 280 | using (Graphics g = Graphics.FromImage(bitmap)) {
|
---|
| 281 | g.Clear(Color.White);
|
---|
| 282 |
|
---|
| 283 | Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
|
---|
| 284 | SizeF strSize = g.MeasureString(text, font);
|
---|
| 285 | if (strSize.Width + 50 > mdsPictureBox.Width) {
|
---|
| 286 | var m = Regex.Match(text, @"\b\w+[.,]*\b*");
|
---|
| 287 | var builder = new StringBuilder();
|
---|
| 288 | while (m.Success) {
|
---|
| 289 | builder.Append(m.Value + " ");
|
---|
| 290 | Match next = m.NextMatch();
|
---|
| 291 | if (g.MeasureString(builder.ToString() + " " + next.Value, font).Width + 50 > mdsPictureBox.Width)
|
---|
| 292 | builder.AppendLine();
|
---|
| 293 | m = next;
|
---|
| 294 | }
|
---|
| 295 | builder.Remove(builder.Length - 1, 1);
|
---|
| 296 | text = builder.ToString();
|
---|
| 297 | strSize = g.MeasureString(text, font);
|
---|
| 298 | }
|
---|
| 299 | g.DrawString(text, font, Brushes.Black, (float)(mdsPictureBox.Width - strSize.Width) / 2.0f, (float)(mdsPictureBox.Height - strSize.Height) / 2.0f);
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | }
|
---|