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