JaCoP.constraints.knapsack
Class KnapsackItem

java.lang.Object
  extended by JaCoP.constraints.knapsack.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.

Author:
Radoslaw Szymanek and Wadeck Follonier

Field Summary
 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.
static java.lang.String[] xmlAttributes
          It specifies the arguments required to be saved by an XML format as well as the constructor being called to recreate an object from an XML format.
 
Constructor Summary
KnapsackItem(IntVar quantity, int weight, int profit)
          It constructs an item.
 
Method Summary
 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

quantity

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


weight

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


profit

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


efficiency

public double efficiency
It stores information about the item efficiency - profit/weight.


xmlAttributes

public static java.lang.String[] xmlAttributes
It specifies the arguments required to be saved by an XML format as well as the constructor being called to recreate an object from an XML format.

Constructor Detail

KnapsackItem

public 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