Free cookie consent management tool by TermsFeed Policy Generator

Ticket #2231: knapsack.patch

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

The originally suggested patch that was added to the mailing list

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

     
    146146                knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height);
    147147              }
    148148
    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 
    155149              //draw items sorted by value
    156150              List<int> sortedIndices = new List<int>();
    157151              for (int i = 0; i < Content.BinaryVector.Length; i++) {
     
    160154                }
    161155              }
    162156
     157              // TODO: how to take into account the penalty incurred by including these values? i.e. how to sort by effective value?
    163158              sortedIndices.Sort(
    164159                delegate(int i, int j) {
    165160                  if (Content.Values[i] < Content.Values[j])
     
    171166                });
    172167
    173168              int currentPosition = pictureBox.Height - borderY;
     169              double totalWeight = 0;
    174170              foreach (int i in sortedIndices) {
    175171                if (Content.BinaryVector[i]) {
    176172
    177173                  double weight = Content.Weights[i];
     174                  totalWeight += weight;
    178175                  double factor = weight / capacity;
    179176                  int elementHeight = (int)Math.Floor(knapsackHeight * factor);
    180177
    181178                  double value = Content.Values[i];
    182179                  //color according to value
     180                  // TODO: calculate effective value, not just the non-penalized value?
    183181                  int colorValue = 0;
    184182                  if (value != 0)
    185183                    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);
    188186
    189187                  using (Brush brush = new SolidBrush(color)) {
    190188                    graphics.FillRectangle(brush,
     
    196194                  currentPosition -= elementHeight;
    197195                }
    198196              }
     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              }
    199203            }
    200204          }
    201205          pictureBox.Image = bitmap;
  • HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.Designer.cs

     
    5252      this.label2 = new System.Windows.Forms.Label();
    5353      this.label1 = new System.Windows.Forms.Label();
    5454      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();
    5558      this.splitContainer.Panel1.SuspendLayout();
    5659      this.splitContainer.Panel2.SuspendLayout();
    5760      this.splitContainer.SuspendLayout();
     
    6871      //
    6972      // splitContainer.Panel1
    7073      //
     74      this.splitContainer.Panel1.Controls.Add(this.label9);
     75      this.splitContainer.Panel1.Controls.Add(this.label8);
    7176      this.splitContainer.Panel1.Controls.Add(this.label6);
    7277      this.splitContainer.Panel1.Controls.Add(this.label5);
    7378      this.splitContainer.Panel1.Controls.Add(this.label4);
     
    8590      // label6
    8691      //
    8792      this.label6.AutoSize = true;
    88       this.label6.Location = new System.Drawing.Point(2, 37);
     93      this.label6.Location = new System.Drawing.Point(7, 37);
    8994      this.label6.Name = "label6";
    9095      this.label6.Size = new System.Drawing.Size(116, 13);
    9196      this.label6.TabIndex = 5;
     
    9499      // label5
    95100      //
    96101      this.label5.AutoSize = true;
    97       this.label5.Location = new System.Drawing.Point(2, 23);
     102      this.label5.Location = new System.Drawing.Point(7, 23);
    98103      this.label5.Name = "label5";
    99104      this.label5.Size = new System.Drawing.Size(125, 13);
    100105      this.label5.TabIndex = 4;
     
    103108      // label4
    104109      //
    105110      this.label4.AutoSize = true;
    106       this.label4.Location = new System.Drawing.Point(5, 80);
     111      this.label4.Location = new System.Drawing.Point(7, 89);
    107112      this.label4.Name = "label4";
    108113      this.label4.Size = new System.Drawing.Size(131, 13);
    109114      this.label4.TabIndex = 3;
     
    112117      // label3
    113118      //
    114119      this.label3.AutoSize = true;
    115       this.label3.Location = new System.Drawing.Point(4, 52);
     120      this.label3.Location = new System.Drawing.Point(7, 61);
    116121      this.label3.Name = "label3";
    117122      this.label3.Size = new System.Drawing.Size(134, 13);
    118123      this.label3.TabIndex = 2;
     
    131136      // label1
    132137      //
    133138      this.label1.AutoSize = true;
    134       this.label1.Location = new System.Drawing.Point(5, 66);
     139      this.label1.Location = new System.Drawing.Point(7, 75);
    135140      this.label1.Name = "label1";
    136141      this.label1.Size = new System.Drawing.Size(129, 13);
    137142      this.label1.TabIndex = 0;
     
    148153      this.pictureBox.TabStop = false;
    149154      this.pictureBox.SizeChanged += new System.EventHandler(this.pictureBox_SizeChanged);
    150155      //
     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      //
    151174      // KnapsackSolutionView
    152175      //
    153       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    154176      this.Controls.Add(this.splitContainer);
    155177      this.Name = "KnapsackSolutionView";
    156178      this.Size = new System.Drawing.Size(392, 310);
     
    157179      this.splitContainer.Panel1.ResumeLayout(false);
    158180      this.splitContainer.Panel1.PerformLayout();
    159181      this.splitContainer.Panel2.ResumeLayout(false);
     182      ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit();
    160183      this.splitContainer.ResumeLayout(false);
    161184      ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
    162185      this.ResumeLayout(false);
     
    173196    private System.Windows.Forms.Label label1;
    174197    private System.Windows.Forms.Label label6;
    175198    private System.Windows.Forms.Label label5;
     199    private System.Windows.Forms.Label label9;
     200    private System.Windows.Forms.Label label8;
    176201
    177202  }
    178203}
  • HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs

     
    6767    public LookupParameter<DoubleValue> BestKnownQualityParameter {
    6868      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    6969    }
    70     public LookupParameter<BinaryVector> BestKnownSolutionParameter {
    71       get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
     70    public LookupParameter<KnapsackSolution> BestKnownSolutionParameter {
     71        get { return (LookupParameter<KnapsackSolution>)Parameters["BestKnownSolution"]; }
    7272    }
     73    public LookupParameter<BinaryVector> BestKnownSolutionVectorParameter {
     74      get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolutionVector"]; }
     75    }
    7376
    7477    [StorableConstructor]
    7578    protected BestKnapsackSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    8689      Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution", "The best Knapsack solution."));
    8790      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the knapsack solution should be stored."));
    8891      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."));
    9094    }
    9195
    9296    public override IDeepCloneable Clone(Cloner cloner) {
     
    109113          max && qualities[i].Value > bestKnownQuality.Value ||
    110114          !max && qualities[i].Value < bestKnownQuality.Value) {
    111115        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);
    113119      }
    114120
    115121      KnapsackSolution solution = BestSolutionParameter.ActualValue;
  • HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

     
    5252    public ValueParameter<DoubleValue> PenaltyParameter {
    5353      get { return (ValueParameter<DoubleValue>)Parameters["Penalty"]; }
    5454    }
    55     public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter {
    56       get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; }
     55    public OptionalValueParameter<KnapsackSolution> BestKnownSolutionParameter {
     56      get { return (OptionalValueParameter<KnapsackSolution>)Parameters["BestKnownSolution"]; }
    5757    }
     58    public OptionalValueParameter<BinaryVector> BestKnownSolutionVectorParameter {
     59      get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolutionVector"]; }
     60    }
    5861    #endregion
    5962
    6063    #region Properties
     
    7477      get { return PenaltyParameter.Value; }
    7578      set { PenaltyParameter.Value = value; }
    7679    }
    77     public BinaryVector BestKnownSolution {
     80    public KnapsackSolution BestKnownSolution {
    7881      get { return BestKnownSolutionParameter.Value; }
    7982      set { BestKnownSolutionParameter.Value = value; }
    8083    }
     84    public BinaryVector BestKnownSolutionVector {
     85      get { return BestKnownSolutionVectorParameter.Value; }
     86      set { BestKnownSolutionVectorParameter.Value = value; }
     87    }
    8188    private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer {
    8289      get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); }
    8390    }
     
    114121      Parameters.Add(new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));
    115122      Parameters.Add(new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));
    116123      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."));
    118126
    119127      Maximization.Value = true;
    120128      MaximizationParameter.Hidden = true;
     
    236244        BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true;
    237245        BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
    238246        BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true;
     247        BestKnapsackSolutionAnalyzer.BestKnownSolutionVectorParameter.ActualName = BestKnownSolutionVectorParameter.Name;
     248        BestKnapsackSolutionAnalyzer.BestKnownSolutionVectorParameter.Hidden = true;
    239249        BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
    240250        BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true;
    241251        BestKnapsackSolutionAnalyzer.BinaryVectorParameter.ActualName = SolutionCreator.BinaryVectorParameter.ActualName;