Package org.jacop.constraints.knapsack
Class KnapsackItem
java.lang.Object
org.jacop.constraints.knapsack.KnapsackItem
- All Implemented Interfaces:
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.9
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal 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
ConstructorsConstructorDescriptionKnapsackItem
(IntVar quantity, int weight, int profit) It constructs an item. -
Method Summary
Modifier and TypeMethodDescriptionint
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.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.toString()
-
Field Details
-
quantity
It is a finite domain variable specifying the possible quantity of that item. -
weight
public final int weightIt specifies the weight of a single instance of this item. -
profit
public final int profitIt specifies the profit of a single instance of this item. -
efficiency
public final double efficiencyIt 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
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 interfaceComparable<KnapsackItem>
-
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
-