1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.Knapsack.Views {
|
---|
29 | /// <summary>
|
---|
30 | /// A view for a Knapsack solution.
|
---|
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 | }
|
---|
39 |
|
---|
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);
|
---|
49 | Content.QualityChanged -= new EventHandler(Content_QualityChanged);
|
---|
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);
|
---|
59 | Content.QualityChanged += new EventHandler(Content_QualityChanged);
|
---|
60 | }
|
---|
61 |
|
---|
62 | private void Content_BinaryVectorChanged(object sender, EventArgs e) {
|
---|
63 | if (InvokeRequired)
|
---|
64 | Invoke(new EventHandler(Content_BinaryVectorChanged), sender, e);
|
---|
65 | else
|
---|
66 | GenerateImage();
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void Content_QualityChanged(object sender, EventArgs e) {
|
---|
70 | if (InvokeRequired)
|
---|
71 | Invoke(new EventHandler(Content_QualityChanged), sender, e);
|
---|
72 | else
|
---|
73 | GenerateImage();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void Content_CapacityChanged(object sender, EventArgs e) {
|
---|
77 | if (InvokeRequired)
|
---|
78 | Invoke(new EventHandler(Content_CapacityChanged), sender, e);
|
---|
79 | else
|
---|
80 | GenerateImage();
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void Content_WeightsChanged(object sender, EventArgs e) {
|
---|
84 | if (InvokeRequired)
|
---|
85 | Invoke(new EventHandler(Content_WeightsChanged), sender, e);
|
---|
86 | else
|
---|
87 | GenerateImage();
|
---|
88 | }
|
---|
89 |
|
---|
90 | private void Content_ValuesChanged(object sender, EventArgs e) {
|
---|
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();
|
---|
99 | if (Content == null)
|
---|
100 | pictureBox.Image = null;
|
---|
101 | else
|
---|
102 | GenerateImage();
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected override void SetEnabledStateOfControls() {
|
---|
106 | base.SetEnabledStateOfControls();
|
---|
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
|
---|
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 | }
|
---|
154 |
|
---|
155 | //draw items sorted by value
|
---|
156 | List<int> sortedIndices = new List<int>();
|
---|
157 | for (int i = 0; i < Content.BinaryVector.Length; i++) {
|
---|
158 | if (Content.BinaryVector[i]) {
|
---|
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]) {
|
---|
176 |
|
---|
177 | double weight = Content.Weights[i];
|
---|
178 | double factor = weight / capacity;
|
---|
179 | int elementHeight = (int)Math.Floor(knapsackHeight * factor);
|
---|
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 | }
|
---|