Class KnapsackItem

java.lang.Object
org.jacop.constraints.knapsack.KnapsackItem
All Implemented Interfaces:
Comparable<KnapsackItem>

public final class KnapsackItem extends Object implements 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.10
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final double
    It stores information about the item efficiency - profit/weight.
    final int
    It specifies the profit of a single instance of this item.
    final IntVar
    It is a finite domain variable specifying the possible quantity of that item.
    final int
    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

    Modifier and Type
    Method
    Description
    int
    Method used in the sorting of the items, we use profit and weight to know the less efficient item without using division.
    final double
    It returns an efficiency of that item.
    final int
    It returns a profit of a single instance of that item.
    final IntVar
    t returns quantity variable associated with that item.
    final int
    It returns a weight of a single instance of that item.
     

    Methods inherited from class java.lang.Object

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

    • 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 Details

    • 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 Details

    • 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 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 String toString()
      Overrides:
      toString in class Object