[3467] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3467] | 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3467] | 24 | using System.Drawing;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.Knapsack.Views {
|
---|
| 29 | /// <summary>
|
---|
[4513] | 30 | /// A view for a Knapsack solution.
|
---|
[3467] | 31 | /// </summary>
|
---|
| 32 | [View("Knapsack View")]
|
---|
| 33 | [Content(typeof(KnapsackSolution), true)]
|
---|
| 34 | public partial class KnapsackSolutionView : HeuristicLab.Core.Views.ItemView {
|
---|
| 35 | public new KnapsackSolution Content {
|
---|
| 36 | get { return (KnapsackSolution)base.Content; }
|
---|
| 37 | set { base.Content = value; }
|
---|
| 38 | }
|
---|
[3904] | 39 |
|
---|
[3467] | 40 | public KnapsackSolutionView() {
|
---|
| 41 | InitializeComponent();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | protected override void DeregisterContentEvents() {
|
---|
| 45 | Content.BinaryVectorChanged -= new EventHandler(Content_BinaryVectorChanged);
|
---|
| 46 | Content.CapacityChanged -= new EventHandler(Content_CapacityChanged);
|
---|
| 47 | Content.WeightsChanged -= new EventHandler(Content_WeightsChanged);
|
---|
| 48 | Content.ValuesChanged -= new EventHandler(Content_ValuesChanged);
|
---|
[3649] | 49 | Content.QualityChanged -= new EventHandler(Content_QualityChanged);
|
---|
[3467] | 50 | base.DeregisterContentEvents();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | protected override void RegisterContentEvents() {
|
---|
| 54 | base.RegisterContentEvents();
|
---|
| 55 | Content.BinaryVectorChanged += new EventHandler(Content_BinaryVectorChanged);
|
---|
| 56 | Content.CapacityChanged += new EventHandler(Content_CapacityChanged);
|
---|
| 57 | Content.WeightsChanged += new EventHandler(Content_WeightsChanged);
|
---|
| 58 | Content.ValuesChanged += new EventHandler(Content_ValuesChanged);
|
---|
[3649] | 59 | Content.QualityChanged += new EventHandler(Content_QualityChanged);
|
---|
[3467] | 60 | }
|
---|
| 61 |
|
---|
[3904] | 62 | private void Content_BinaryVectorChanged(object sender, EventArgs e) {
|
---|
[3467] | 63 | if (InvokeRequired)
|
---|
| 64 | Invoke(new EventHandler(Content_BinaryVectorChanged), sender, e);
|
---|
| 65 | else
|
---|
| 66 | GenerateImage();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[3904] | 69 | private void Content_QualityChanged(object sender, EventArgs e) {
|
---|
[3649] | 70 | if (InvokeRequired)
|
---|
| 71 | Invoke(new EventHandler(Content_QualityChanged), sender, e);
|
---|
| 72 | else
|
---|
| 73 | GenerateImage();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[3904] | 76 | private void Content_CapacityChanged(object sender, EventArgs e) {
|
---|
[3467] | 77 | if (InvokeRequired)
|
---|
| 78 | Invoke(new EventHandler(Content_CapacityChanged), sender, e);
|
---|
| 79 | else
|
---|
| 80 | GenerateImage();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[3904] | 83 | private void Content_WeightsChanged(object sender, EventArgs e) {
|
---|
[3467] | 84 | if (InvokeRequired)
|
---|
| 85 | Invoke(new EventHandler(Content_WeightsChanged), sender, e);
|
---|
| 86 | else
|
---|
| 87 | GenerateImage();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[3904] | 90 | private void Content_ValuesChanged(object sender, EventArgs e) {
|
---|
[3467] | 91 | if (InvokeRequired)
|
---|
| 92 | Invoke(new EventHandler(Content_ValuesChanged), sender, e);
|
---|
| 93 | else
|
---|
| 94 | GenerateImage();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | protected override void OnContentChanged() {
|
---|
| 98 | base.OnContentChanged();
|
---|
[3904] | 99 | if (Content == null)
|
---|
[3467] | 100 | pictureBox.Image = null;
|
---|
[3904] | 101 | else
|
---|
[3467] | 102 | GenerateImage();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[3904] | 105 | protected override void SetEnabledStateOfControls() {
|
---|
| 106 | base.SetEnabledStateOfControls();
|
---|
[3467] | 107 | pictureBox.Enabled = Content != null;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private void GenerateImage() {
|
---|
| 111 | if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
|
---|
| 112 | if (Content == null) {
|
---|
| 113 | pictureBox.Image = null;
|
---|
| 114 | } else {
|
---|
| 115 | Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
| 116 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
| 117 | int border = 10;
|
---|
| 118 |
|
---|
| 119 | int borderX = border;
|
---|
| 120 | if (pictureBox.Width - border * 2 < 0)
|
---|
| 121 | borderX = 0;
|
---|
| 122 | int width = pictureBox.Width - borderX * 2;
|
---|
| 123 |
|
---|
| 124 | int borderY = border;
|
---|
| 125 | if (pictureBox.Height - border * 2 < 0)
|
---|
| 126 | borderY = 0;
|
---|
| 127 | int height = pictureBox.Height - borderY * 2;
|
---|
| 128 |
|
---|
| 129 | //preprocess
|
---|
| 130 | double capacity = Content.Capacity.Value;
|
---|
| 131 |
|
---|
| 132 | if (capacity != 0) {
|
---|
| 133 | double packedCapacity = 0;
|
---|
| 134 | double maxValue = 0;
|
---|
| 135 | for (int i = 0; i < Content.BinaryVector.Length; i++) {
|
---|
| 136 | if (Content.BinaryVector[i]) {
|
---|
| 137 | packedCapacity += Content.Weights[i];
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | if (Content.Values[i] > maxValue)
|
---|
| 141 | maxValue = Content.Values[i];
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | int knapsackHeight = height;
|
---|
| 145 | if (packedCapacity > capacity) {
|
---|
| 146 | knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | //draw knapsack
|
---|
[3540] | 150 | using (Pen pen = new Pen(Color.Black, 2)) {
|
---|
| 151 | graphics.DrawRectangle(pen,
|
---|
| 152 | borderX - 2, pictureBox.Height - borderY - knapsackHeight - 2, width + 4, knapsackHeight + 4);
|
---|
| 153 | }
|
---|
[3467] | 154 |
|
---|
| 155 | //draw items sorted by value
|
---|
| 156 | List<int> sortedIndices = new List<int>();
|
---|
| 157 | for (int i = 0; i < Content.BinaryVector.Length; i++) {
|
---|
[3904] | 158 | if (Content.BinaryVector[i]) {
|
---|
[3467] | 159 | sortedIndices.Add(i);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | sortedIndices.Sort(
|
---|
| 164 | delegate(int i, int j) {
|
---|
| 165 | if (Content.Values[i] < Content.Values[j])
|
---|
| 166 | return -1;
|
---|
| 167 | else if (Content.Values[i] > Content.Values[j])
|
---|
| 168 | return 1;
|
---|
| 169 | else
|
---|
| 170 | return 0;
|
---|
| 171 | });
|
---|
| 172 |
|
---|
| 173 | int currentPosition = pictureBox.Height - borderY;
|
---|
| 174 | foreach (int i in sortedIndices) {
|
---|
| 175 | if (Content.BinaryVector[i]) {
|
---|
[3904] | 176 |
|
---|
[3467] | 177 | double weight = Content.Weights[i];
|
---|
| 178 | double factor = weight / capacity;
|
---|
[3537] | 179 | int elementHeight = (int)Math.Floor(knapsackHeight * factor);
|
---|
[3467] | 180 |
|
---|
| 181 | double value = Content.Values[i];
|
---|
| 182 | //color according to value
|
---|
| 183 | int colorValue = 0;
|
---|
| 184 | if (value != 0)
|
---|
| 185 | colorValue = (int)Math.Round(255.0 * value / maxValue);
|
---|
| 186 | Color color = Color.FromArgb(
|
---|
| 187 | 0, 0, colorValue);
|
---|
| 188 |
|
---|
| 189 | using (Brush brush = new SolidBrush(color)) {
|
---|
| 190 | graphics.FillRectangle(brush,
|
---|
| 191 | borderX, currentPosition - elementHeight, width, elementHeight);
|
---|
| 192 | }
|
---|
| 193 | graphics.DrawRectangle(Pens.White,
|
---|
| 194 | borderX, currentPosition - elementHeight, width, elementHeight);
|
---|
| 195 |
|
---|
| 196 | currentPosition -= elementHeight;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | pictureBox.Image = bitmap;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
| 207 | GenerateImage();
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|