# Ticket #2231: knapsack.patch

File knapsack.patch, 13.6 KB (added by abeham, 6 years ago)

The originally suggested patch that was added to the mailing list

• ## HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.cs

 knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height); } //draw knapsack using (Pen pen = new Pen(Color.Black, 2)) { graphics.DrawRectangle(pen, borderX - 2, pictureBox.Height - borderY - knapsackHeight - 2, width + 4, knapsackHeight + 4); } //draw items sorted by value List sortedIndices = new List(); for (int i = 0; i < Content.BinaryVector.Length; i++) { } } // TODO: how to take into account the penalty incurred by including these values? i.e. how to sort by effective value? sortedIndices.Sort( delegate(int i, int j) { if (Content.Values[i] < Content.Values[j]) }); int currentPosition = pictureBox.Height - borderY; double totalWeight = 0; foreach (int i in sortedIndices) { if (Content.BinaryVector[i]) { double weight = Content.Weights[i]; totalWeight += weight; double factor = weight / capacity; int elementHeight = (int)Math.Floor(knapsackHeight * factor); double value = Content.Values[i]; //color according to value // TODO: calculate effective value, not just the non-penalized value? int colorValue = 0; if (value != 0) colorValue = (int)Math.Round(255.0 * value / maxValue); Color color = Color.FromArgb( 0, 0, colorValue); Color color = totalWeight <= capacity ? Color.FromArgb( 0, 0, colorValue) : Color.FromArgb(colorValue, 0, 0); using (Brush brush = new SolidBrush(color)) { graphics.FillRectangle(brush, currentPosition -= elementHeight; } } //draw knapsack using (Pen pen = new Pen(Color.Black, 2)) { graphics.DrawRectangle(pen, borderX - 2, pictureBox.Height - borderY - knapsackHeight - 2, width + 4, knapsackHeight + 4); } } } pictureBox.Image = bitmap;
• ## HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.Designer.cs

 this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.label8 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit(); this.splitContainer.Panel1.SuspendLayout(); this.splitContainer.Panel2.SuspendLayout(); this.splitContainer.SuspendLayout(); // // splitContainer.Panel1 // this.splitContainer.Panel1.Controls.Add(this.label9); this.splitContainer.Panel1.Controls.Add(this.label8); this.splitContainer.Panel1.Controls.Add(this.label6); this.splitContainer.Panel1.Controls.Add(this.label5); this.splitContainer.Panel1.Controls.Add(this.label4); // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(2, 37); this.label6.Location = new System.Drawing.Point(7, 37); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(116, 13); this.label6.TabIndex = 5; // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(2, 23); this.label5.Location = new System.Drawing.Point(7, 23); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(125, 13); this.label5.TabIndex = 4; // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(5, 80); this.label4.Location = new System.Drawing.Point(7, 89); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(131, 13); this.label4.TabIndex = 3; // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(4, 52); this.label3.Location = new System.Drawing.Point(7, 61); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(134, 13); this.label3.TabIndex = 2; // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(5, 66); this.label1.Location = new System.Drawing.Point(7, 75); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(129, 13); this.label1.TabIndex = 0; this.pictureBox.TabStop = false; this.pictureBox.SizeChanged += new System.EventHandler(this.pictureBox_SizeChanged); // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(7, 132); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(70, 13); this.label8.TabIndex = 6; this.label8.Text = "Blue boxes fit"; // // label9 // this.label9.AutoSize = true; this.label9.Location = new System.Drawing.Point(7, 149); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(124, 13); this.label9.TabIndex = 7; this.label9.Text = "Red boxes are penalized"; // // KnapsackSolutionView // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.Controls.Add(this.splitContainer); this.Name = "KnapsackSolutionView"; this.Size = new System.Drawing.Size(392, 310); this.splitContainer.Panel1.ResumeLayout(false); this.splitContainer.Panel1.PerformLayout(); this.splitContainer.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit(); this.splitContainer.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label9; private System.Windows.Forms.Label label8; } }
• ## HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs

 public LookupParameter BestKnownQualityParameter { get { return (LookupParameter)Parameters["BestKnownQuality"]; } } public LookupParameter BestKnownSolutionParameter { get { return (LookupParameter)Parameters["BestKnownSolution"]; } public LookupParameter BestKnownSolutionParameter { get { return (LookupParameter)Parameters["BestKnownSolution"]; } } public LookupParameter BestKnownSolutionVectorParameter { get { return (LookupParameter)Parameters["BestKnownSolutionVector"]; } } [StorableConstructor] protected BestKnapsackSolutionAnalyzer(bool deserializing) : base(deserializing) { } Parameters.Add(new LookupParameter("BestSolution", "The best Knapsack solution.")); Parameters.Add(new ValueLookupParameter("Results", "The result collection where the knapsack solution should be stored.")); Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution.")); Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution.")); Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution.")); Parameters.Add(new LookupParameter("BestKnownSolutionVector", "The best known solution vector.")); } public override IDeepCloneable Clone(Cloner cloner) { max && qualities[i].Value > bestKnownQuality.Value || !max && qualities[i].Value < bestKnownQuality.Value) { BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); BestKnownSolutionParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone(); BestKnownSolutionVectorParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone(); BestKnownSolutionParameter.ActualValue = new KnapsackSolution(BestKnownSolutionVectorParameter.ActualValue, new DoubleValue(qualities[i].Value), KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue); } KnapsackSolution solution = BestSolutionParameter.ActualValue;
• ## HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

 public ValueParameter PenaltyParameter { get { return (ValueParameter)Parameters["Penalty"]; } } public OptionalValueParameter BestKnownSolutionParameter { get { return (OptionalValueParameter)Parameters["BestKnownSolution"]; } public OptionalValueParameter BestKnownSolutionParameter { get { return (OptionalValueParameter)Parameters["BestKnownSolution"]; } } public OptionalValueParameter BestKnownSolutionVectorParameter { get { return (OptionalValueParameter)Parameters["BestKnownSolutionVector"]; } } #endregion #region Properties get { return PenaltyParameter.Value; } set { PenaltyParameter.Value = value; } } public BinaryVector BestKnownSolution { public KnapsackSolution BestKnownSolution { get { return BestKnownSolutionParameter.Value; } set { BestKnownSolutionParameter.Value = value; } } public BinaryVector BestKnownSolutionVector { get { return BestKnownSolutionVectorParameter.Value; } set { BestKnownSolutionVectorParameter.Value = value; } } private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer { get { return Operators.OfType().FirstOrDefault(); } } Parameters.Add(new ValueParameter("Weights", "The weights of the items.", new IntArray(5))); Parameters.Add(new ValueParameter("Values", "The values of the items.", new IntArray(5))); Parameters.Add(new ValueParameter("Penalty", "The penalty value for each unit of overweight.", new DoubleValue(1))); Parameters.Add(new OptionalValueParameter("BestKnownSolution", "The best known solution of this Knapsack instance.")); Parameters.Add(new OptionalValueParameter("BestKnownSolution", "The best known solution of this Knapsack instance.")); Parameters.Add(new OptionalValueParameter("BestKnownSolutionVector", "The best known solution vector of this Knapsack instance.")); Maximization.Value = true; MaximizationParameter.Hidden = true; BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true; BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true; BestKnapsackSolutionAnalyzer.BestKnownSolutionVectorParameter.ActualName = BestKnownSolutionVectorParameter.Name; BestKnapsackSolutionAnalyzer.BestKnownSolutionVectorParameter.Hidden = true; BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true; BestKnapsackSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;