Ticket #2231: knapsack.patch
File knapsack.patch, 13.6 KB (added by abeham, 10 years ago) |
---|
-
HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.cs
146 146 knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height); 147 147 } 148 148 149 //draw knapsack150 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 149 //draw items sorted by value 156 150 List<int> sortedIndices = new List<int>(); 157 151 for (int i = 0; i < Content.BinaryVector.Length; i++) { … … 160 154 } 161 155 } 162 156 157 // TODO: how to take into account the penalty incurred by including these values? i.e. how to sort by effective value? 163 158 sortedIndices.Sort( 164 159 delegate(int i, int j) { 165 160 if (Content.Values[i] < Content.Values[j]) … … 171 166 }); 172 167 173 168 int currentPosition = pictureBox.Height - borderY; 169 double totalWeight = 0; 174 170 foreach (int i in sortedIndices) { 175 171 if (Content.BinaryVector[i]) { 176 172 177 173 double weight = Content.Weights[i]; 174 totalWeight += weight; 178 175 double factor = weight / capacity; 179 176 int elementHeight = (int)Math.Floor(knapsackHeight * factor); 180 177 181 178 double value = Content.Values[i]; 182 179 //color according to value 180 // TODO: calculate effective value, not just the non-penalized value? 183 181 int colorValue = 0; 184 182 if (value != 0) 185 183 colorValue = (int)Math.Round(255.0 * value / maxValue); 186 Color color = Color.FromArgb(187 0, 0, colorValue) ;184 Color color = totalWeight <= capacity ? Color.FromArgb( 185 0, 0, colorValue) : Color.FromArgb(colorValue, 0, 0); 188 186 189 187 using (Brush brush = new SolidBrush(color)) { 190 188 graphics.FillRectangle(brush, … … 196 194 currentPosition -= elementHeight; 197 195 } 198 196 } 197 198 //draw knapsack 199 using (Pen pen = new Pen(Color.Black, 2)) { 200 graphics.DrawRectangle(pen, 201 borderX - 2, pictureBox.Height - borderY - knapsackHeight - 2, width + 4, knapsackHeight + 4); 202 } 199 203 } 200 204 } 201 205 pictureBox.Image = bitmap; -
HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.Designer.cs
52 52 this.label2 = new System.Windows.Forms.Label(); 53 53 this.label1 = new System.Windows.Forms.Label(); 54 54 this.pictureBox = new System.Windows.Forms.PictureBox(); 55 this.label8 = new System.Windows.Forms.Label(); 56 this.label9 = new System.Windows.Forms.Label(); 57 ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit(); 55 58 this.splitContainer.Panel1.SuspendLayout(); 56 59 this.splitContainer.Panel2.SuspendLayout(); 57 60 this.splitContainer.SuspendLayout(); … … 68 71 // 69 72 // splitContainer.Panel1 70 73 // 74 this.splitContainer.Panel1.Controls.Add(this.label9); 75 this.splitContainer.Panel1.Controls.Add(this.label8); 71 76 this.splitContainer.Panel1.Controls.Add(this.label6); 72 77 this.splitContainer.Panel1.Controls.Add(this.label5); 73 78 this.splitContainer.Panel1.Controls.Add(this.label4); … … 85 90 // label6 86 91 // 87 92 this.label6.AutoSize = true; 88 this.label6.Location = new System.Drawing.Point( 2, 37);93 this.label6.Location = new System.Drawing.Point(7, 37); 89 94 this.label6.Name = "label6"; 90 95 this.label6.Size = new System.Drawing.Size(116, 13); 91 96 this.label6.TabIndex = 5; … … 94 99 // label5 95 100 // 96 101 this.label5.AutoSize = true; 97 this.label5.Location = new System.Drawing.Point( 2, 23);102 this.label5.Location = new System.Drawing.Point(7, 23); 98 103 this.label5.Name = "label5"; 99 104 this.label5.Size = new System.Drawing.Size(125, 13); 100 105 this.label5.TabIndex = 4; … … 103 108 // label4 104 109 // 105 110 this.label4.AutoSize = true; 106 this.label4.Location = new System.Drawing.Point( 5, 80);111 this.label4.Location = new System.Drawing.Point(7, 89); 107 112 this.label4.Name = "label4"; 108 113 this.label4.Size = new System.Drawing.Size(131, 13); 109 114 this.label4.TabIndex = 3; … … 112 117 // label3 113 118 // 114 119 this.label3.AutoSize = true; 115 this.label3.Location = new System.Drawing.Point( 4, 52);120 this.label3.Location = new System.Drawing.Point(7, 61); 116 121 this.label3.Name = "label3"; 117 122 this.label3.Size = new System.Drawing.Size(134, 13); 118 123 this.label3.TabIndex = 2; … … 131 136 // label1 132 137 // 133 138 this.label1.AutoSize = true; 134 this.label1.Location = new System.Drawing.Point( 5, 66);139 this.label1.Location = new System.Drawing.Point(7, 75); 135 140 this.label1.Name = "label1"; 136 141 this.label1.Size = new System.Drawing.Size(129, 13); 137 142 this.label1.TabIndex = 0; … … 148 153 this.pictureBox.TabStop = false; 149 154 this.pictureBox.SizeChanged += new System.EventHandler(this.pictureBox_SizeChanged); 150 155 // 156 // label8 157 // 158 this.label8.AutoSize = true; 159 this.label8.Location = new System.Drawing.Point(7, 132); 160 this.label8.Name = "label8"; 161 this.label8.Size = new System.Drawing.Size(70, 13); 162 this.label8.TabIndex = 6; 163 this.label8.Text = "Blue boxes fit"; 164 // 165 // label9 166 // 167 this.label9.AutoSize = true; 168 this.label9.Location = new System.Drawing.Point(7, 149); 169 this.label9.Name = "label9"; 170 this.label9.Size = new System.Drawing.Size(124, 13); 171 this.label9.TabIndex = 7; 172 this.label9.Text = "Red boxes are penalized"; 173 // 151 174 // KnapsackSolutionView 152 175 // 153 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);154 176 this.Controls.Add(this.splitContainer); 155 177 this.Name = "KnapsackSolutionView"; 156 178 this.Size = new System.Drawing.Size(392, 310); … … 157 179 this.splitContainer.Panel1.ResumeLayout(false); 158 180 this.splitContainer.Panel1.PerformLayout(); 159 181 this.splitContainer.Panel2.ResumeLayout(false); 182 ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit(); 160 183 this.splitContainer.ResumeLayout(false); 161 184 ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); 162 185 this.ResumeLayout(false); … … 173 196 private System.Windows.Forms.Label label1; 174 197 private System.Windows.Forms.Label label6; 175 198 private System.Windows.Forms.Label label5; 199 private System.Windows.Forms.Label label9; 200 private System.Windows.Forms.Label label8; 176 201 177 202 } 178 203 } -
HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs
67 67 public LookupParameter<DoubleValue> BestKnownQualityParameter { 68 68 get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 69 69 } 70 public LookupParameter< BinaryVector> BestKnownSolutionParameter {71 get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }70 public LookupParameter<KnapsackSolution> BestKnownSolutionParameter { 71 get { return (LookupParameter<KnapsackSolution>)Parameters["BestKnownSolution"]; } 72 72 } 73 public LookupParameter<BinaryVector> BestKnownSolutionVectorParameter { 74 get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolutionVector"]; } 75 } 73 76 74 77 [StorableConstructor] 75 78 protected BestKnapsackSolutionAnalyzer(bool deserializing) : base(deserializing) { } … … 86 89 Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution", "The best Knapsack solution.")); 87 90 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the knapsack solution should be stored.")); 88 91 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution.")); 89 Parameters.Add(new LookupParameter<BinaryVector>("BestKnownSolution", "The best known solution.")); 92 Parameters.Add(new LookupParameter<KnapsackSolution>("BestKnownSolution", "The best known solution.")); 93 Parameters.Add(new LookupParameter<BinaryVector>("BestKnownSolutionVector", "The best known solution vector.")); 90 94 } 91 95 92 96 public override IDeepCloneable Clone(Cloner cloner) { … … 109 113 max && qualities[i].Value > bestKnownQuality.Value || 110 114 !max && qualities[i].Value < bestKnownQuality.Value) { 111 115 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 112 BestKnownSolutionParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone(); 116 BestKnownSolutionVectorParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone(); 117 BestKnownSolutionParameter.ActualValue = new KnapsackSolution(BestKnownSolutionVectorParameter.ActualValue, new DoubleValue(qualities[i].Value), 118 KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue); 113 119 } 114 120 115 121 KnapsackSolution solution = BestSolutionParameter.ActualValue; -
HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
52 52 public ValueParameter<DoubleValue> PenaltyParameter { 53 53 get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; } 54 54 } 55 public OptionalValueParameter< BinaryVector> BestKnownSolutionParameter {56 get { return (OptionalValueParameter< BinaryVector>)Parameters["BestKnownSolution"]; }55 public OptionalValueParameter<KnapsackSolution> BestKnownSolutionParameter { 56 get { return (OptionalValueParameter<KnapsackSolution>)Parameters["BestKnownSolution"]; } 57 57 } 58 public OptionalValueParameter<BinaryVector> BestKnownSolutionVectorParameter { 59 get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolutionVector"]; } 60 } 58 61 #endregion 59 62 60 63 #region Properties … … 74 77 get { return PenaltyParameter.Value; } 75 78 set { PenaltyParameter.Value = value; } 76 79 } 77 public BinaryVectorBestKnownSolution {80 public KnapsackSolution BestKnownSolution { 78 81 get { return BestKnownSolutionParameter.Value; } 79 82 set { BestKnownSolutionParameter.Value = value; } 80 83 } 84 public BinaryVector BestKnownSolutionVector { 85 get { return BestKnownSolutionVectorParameter.Value; } 86 set { BestKnownSolutionVectorParameter.Value = value; } 87 } 81 88 private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer { 82 89 get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); } 83 90 } … … 114 121 Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5))); 115 122 Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5))); 116 123 Parameters.Add(new ValueParameter<DoubleValue>("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1))); 117 Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance.")); 124 Parameters.Add(new OptionalValueParameter<KnapsackSolution>("BestKnownSolution", "The best known solution of this Knapsack instance.")); 125 Parameters.Add(new OptionalValueParameter<BinaryVector>("BestKnownSolutionVector", "The best known solution vector of this Knapsack instance.")); 118 126 119 127 Maximization.Value = true; 120 128 MaximizationParameter.Hidden = true; … … 236 244 BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true; 237 245 BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 238 246 BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true; 247 BestKnapsackSolutionAnalyzer.BestKnownSolutionVectorParameter.ActualName = BestKnownSolutionVectorParameter.Name; 248 BestKnapsackSolutionAnalyzer.BestKnownSolutionVectorParameter.Hidden = true; 239 249 BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; 240 250 BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true; 241 251 BestKnapsackSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;