Class KnapsackItem

  • All Implemented Interfaces:
    java.lang.Comparable<KnapsackItem>

    public final class KnapsackItem
    extends java.lang.Object
    implements java.lang.Comparable<KnapsackItem>
    This class stores information about items being considered by a Knapsack constraint. It is a holder for integer attributes like weight and profit, as well as finite domain variable denoting the quantity being taken. It also stores precomputed efficiency of the item.

    It implements comparable interface in such a away so that items can be sorted in decreasing efficiency. In case of equal efficiency then item which is heavier is preferred.

    Version:
    4.7
    • Field Summary

      Fields 
      Modifier and Type Field Description
      double efficiency
      It stores information about the item efficiency - profit/weight.
      int profit
      It specifies the profit of a single instance of this item.
      IntVar quantity
      It is a finite domain variable specifying the possible quantity of that item.
      int weight
      It specifies the weight of a single instance of this item.
    • Constructor Summary

      Constructors 
      Constructor Description
      KnapsackItem​(IntVar quantity, int weight, int profit)
      It constructs an item.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(KnapsackItem that)
      Method used in the sorting of the items, we use profit and weight to know the less efficient item without using division.
      double getEfficiency()
      It returns an efficiency of that item.
      int getProfit()
      It returns a profit of a single instance of that item.
      IntVar getVariable()
      t returns quantity variable associated with that item.
      int getWeight()
      It returns a weight of a single instance of that item.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • quantity

        public final IntVar quantity
        It is a finite domain variable specifying the possible quantity of that item.
      • weight

        public final int weight
        It specifies the weight of a single instance of this item.
      • profit

        public final int profit
        It specifies the profit of a single instance of this item.
      • efficiency

        public final double efficiency
        It stores information about the item efficiency - profit/weight.
    • Constructor Detail

      • KnapsackItem

        KnapsackItem​(IntVar quantity,
                     int weight,
                     int profit)
        It constructs an item. It requires information about weight and profit, as well as finite domain variable denoting the quantity. It will compute efficiency as well.
        Parameters:
        quantity - - number of items it is possible to take.
        weight - - weight of the single item.
        profit - - profit due to one single item.
    • Method Detail

      • compareTo

        public int compareTo​(KnapsackItem that)
        Method used in the sorting of the items, we use profit and weight to know the less efficient item without using division. This function returns 1 if this item is less efficient than that item. This function returns -1 if this item is more efficient than that item. If both items are equally efficient then this function returns 1 if this item has smaller weight than that item.

        In connection with Arrays.sort() it will produce items from most efficient to least efficient breaking ties in the favor of the larger weight.

        Specified by:
        compareTo in interface java.lang.Comparable<KnapsackItem>
      • getVariable

        public final IntVar getVariable()
        t returns quantity variable associated with that item.
        Returns:
        quantity finite domain variable.
      • getProfit

        public final int getProfit()
        It returns a profit of a single instance of that item.
        Returns:
        profit of a single instance of that item.
      • getWeight

        public final int getWeight()
        It returns a weight of a single instance of that item.
        Returns:
        weight of a single instance of that item.
      • getEfficiency

        public final double getEfficiency()
        It returns an efficiency of that item.
        Returns:
        the efficiency of that item.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object