JaCoP.constraints.knapsack
Class Knapsack

java.lang.Object
  extended by JaCoP.constraints.DecomposedConstraint
      extended by JaCoP.constraints.Constraint
          extended by JaCoP.constraints.knapsack.Knapsack

public class Knapsack
extends Constraint

It specifies a knapsack constraint. This implementation was inspired by the paper by Irit Katriel, Meinolf Sellmann, Eli Upfal, Pascal Van Hentenryck: "Propagating Knapsack Constraints in Sublinear Time", AAAI 2007: pp. 231-236. Tha major extensions of that paper are the following. The quantity variables do not have to be binary. The profit and capacity of the knapsacks do not have to be integers. In both cases, the constraint accepts any finite domain variable. This implementation is based on the implementation obtained by Wadeck Follonier during his work on a student semester project. We would like to thank Meinolf Sellmann for his appreciation of our work and useful comments.

Author:
Radoslaw Szymanek and Wadeck Follonier

Field Summary
 int CONSISTENCY_INFO_FROM
          It specifies how many consistency functions must be executed before the information about the constraint is being printed out.
 int countConsistency
          It counts the number of time the consistency function has been executed.
 int countQueueVariable
          It counts the number of times the queueVariable function has been executed.
 int countRemoveLevel
          It counts the number of time the removeLevel function has been executed.
 int currentLevel
          It specifies the current level of the constraint store at which the consistency function of this constraint is being executed.
static boolean debugAll
          It specifies if any debugging information should be printed.
 boolean impositionFailure
          It specifies if the constraint has already discovered to be unsatisfied during the imposition stage.
 boolean inConsistency
          It specifies if the constraint is executing the consistency function.
 KnapsackItem[] items
          The array of items present in the knapsack constraint.
 IntVar knapsackCapacity
          This is a finite domain variable to specify the knapsack capacity.
 IntVar knapsackProfit
          This is a finite domain variable to specify the knapsack profit.
 TreeLeaf[] leaves
          It stores all the leaves of the knapsack tree in one array.
 boolean needConsistency
          It specifies if the consistency function should execute.
 boolean needCriticalUpdate
          It specifies if the recomputation of the critical item should take place.
 boolean needForbidden
          It specifies if the forbidden part of the consistency algortihm should be executed.
 boolean needMandatory
          It specifies if the mandatory part of the consistency algorithm should be executed.
 boolean needUpdate
          It specifies if the knapsack tree requires an update.
 int positionOfAlreadyUpdated
          It specifies the position of the last changed item which has been already been recomputed.
 int QUEUE_INFO_FROM
          It specifies how many queueVariable functions must be executed before the information about the constraint is being printed out.
 int REMOVE_INFO_FROM
          It specifies how many removeLevel functions must be executed before the information about the constraint is being printed out.
 Tree tree
          The tree for the storing information about the maximalWeight, sum of weights and sum of profits.
 int updateLimit
          It specifies the limit after which the changed leaves are not store and the remove level will simply recompute attributes of all nodes in the knapsack tree.
 java.util.HashMap<IntVar,TreeLeaf> variableLeafMapping
          It specifies mapping from variables into the leaf of the knapsack tree.
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.
 
Fields inherited from class JaCoP.constraints.Constraint
atomicExecution, consistencyPruningEvents, earlyTerminationOK, id, increaseWeight, numberArgs, numberId
 
Fields inherited from class JaCoP.constraints.DecomposedConstraint
queueIndex
 
Constructor Summary
Knapsack(int[] profits, int[] weights, IntVar[] quantity, IntVar knapsackCapacity, IntVar knapsackProfit)
          It constructs the knapsack constraint.
Knapsack(KnapsackItem[] items, IntVar knapsackCapacity, IntVar knapsackProfit)
          It constructs an knapsack constraint.
 
Method Summary
 java.util.ArrayList<Var> arguments()
          It returns the variables in a scope of the constraint.
 void cleanAfterFailure()
          It is executed after the constraint has failed.
 void computeForbidden()
          It searches through a subset of right items to find the ones which can not be fully included without violating the profit requirement in the knapsack constraint.
 void computeMandatory()
          It computes the mandatory part of the knapsack pruning.
 void consistency(Store store)
          It is a (most probably incomplete) consistency function which removes the values from variables domains.
 int getConsistencyPruningEvent(Var var)
          It retrieves the pruning event which causes reevaluation of the constraint.
 java.lang.String id()
          It gives the id string of a constraint.
 void impose(Store store)
          It imposes the constraint in a given store.
 void increaseWeight()
          It increases the weight of the variables in the constraint scope.
 int numberArgs()
          It returns the number of variables within a constraint scope.
 void queueVariable(int level, Var v)
          This is a function called to indicate which variable in a scope of constraint has changed.
 void removeConstraint()
          It removes the constraint by removing this constraint from all variables.
 void removeLevel(int level)
          This function is called in case of the backtrack, so a constraint can clear the queue of changed variables which is no longer valid.
 void removeLevelLate(int level)
          This function is called in case of the backtrack.
 boolean satisfied()
          It checks if the constraint is satisfied.
 java.lang.String toString()
          It produces a string representation of a constraint state.
 
Methods inherited from class JaCoP.constraints.Constraint
decompose, getGuideConstraint, getGuideValue, getGuideVariable, impose, imposeDecomposition, requiresMonotonicity, setConsistencyPruningEvent, supplyGuideFeedback
 
Methods inherited from class JaCoP.constraints.DecomposedConstraint
auxiliaryVariables, imposeDecomposition
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

debugAll

public static final boolean debugAll
It specifies if any debugging information should be printed.

See Also:
Constant Field Values

variableLeafMapping

public java.util.HashMap<IntVar,TreeLeaf> variableLeafMapping
It specifies mapping from variables into the leaf of the knapsack tree.


leaves

public TreeLeaf[] leaves
It stores all the leaves of the knapsack tree in one array. The leaves are sorted from the most efficient to the least efficient.


updateLimit

public final int updateLimit
It specifies the limit after which the changed leaves are not store and the remove level will simply recompute attributes of all nodes in the knapsack tree. By default it is equal to n/log(n), where n is the number of the items.


impositionFailure

public boolean impositionFailure
It specifies if the constraint has already discovered to be unsatisfied during the imposition stage.


knapsackCapacity

public final IntVar knapsackCapacity
This is a finite domain variable to specify the knapsack capacity.


knapsackProfit

public final IntVar knapsackProfit
This is a finite domain variable to specify the knapsack profit.


currentLevel

public int currentLevel
It specifies the current level of the constraint store at which the consistency function of this constraint is being executed.


positionOfAlreadyUpdated

public int positionOfAlreadyUpdated
It specifies the position of the last changed item which has been already been recomputed. It helps to avoid recomputation of the same parts of the tree if consistency function of the knapsack constraint is called multiple times within the same store level.


needUpdate

public boolean needUpdate
It specifies if the knapsack tree requires an update.


needConsistency

public boolean needConsistency
It specifies if the consistency function should execute.


needMandatory

public boolean needMandatory
It specifies if the mandatory part of the consistency algorithm should be executed.


needForbidden

public boolean needForbidden
It specifies if the forbidden part of the consistency algortihm should be executed.


needCriticalUpdate

public boolean needCriticalUpdate
It specifies if the recomputation of the critical item should take place.


inConsistency

public boolean inConsistency
It specifies if the constraint is executing the consistency function.


tree

public Tree tree
The tree for the storing information about the maximalWeight, sum of weights and sum of profits.


items

public KnapsackItem[] items
The array of items present in the knapsack constraint.


countConsistency

public int countConsistency
It counts the number of time the consistency function has been executed.


countQueueVariable

public int countQueueVariable
It counts the number of times the queueVariable function has been executed.


countRemoveLevel

public int countRemoveLevel
It counts the number of time the removeLevel function has been executed.


REMOVE_INFO_FROM

public int REMOVE_INFO_FROM
It specifies how many removeLevel functions must be executed before the information about the constraint is being printed out.


QUEUE_INFO_FROM

public int QUEUE_INFO_FROM
It specifies how many queueVariable functions must be executed before the information about the constraint is being printed out.


CONSISTENCY_INFO_FROM

public int CONSISTENCY_INFO_FROM
It specifies how many consistency functions must be executed before the information about the constraint is being printed out.


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

Knapsack

public Knapsack(KnapsackItem[] items,
                IntVar knapsackCapacity,
                IntVar knapsackProfit)
It constructs an knapsack constraint.

Parameters:
items - list of items in knapsack with its weight, profit and quantity variable.
knapsackCapacity - overall knapsack capacity.
knapsackProfit - overall profit obtained by the items in the knapsack.

Knapsack

public Knapsack(int[] profits,
                int[] weights,
                IntVar[] quantity,
                IntVar knapsackCapacity,
                IntVar knapsackProfit)
It constructs the knapsack constraint.

Parameters:
profits - the list of profits, each for the corresponding item no.
weights - the list of weights, each for the corresponding item no.
quantity - finite domain variable specifying allowed values for the vars.
knapsackCapacity - finite domain variable specifying the capacity limit of the knapsack.
knapsackProfit -
Method Detail

removeLevel

public void removeLevel(int level)
Description copied from class: Constraint
This function is called in case of the backtrack, so a constraint can clear the queue of changed variables which is no longer valid. This function is called *before* all timestamps, variables, mutablevariables have reverted to their previous value.

Overrides:
removeLevel in class Constraint
Parameters:
level - the level which is being removed.

cleanAfterFailure

public void cleanAfterFailure()
Description copied from class: Constraint
It is executed after the constraint has failed. It allows to clean some data structures.

Overrides:
cleanAfterFailure in class Constraint

removeLevelLate

public void removeLevelLate(int level)
Description copied from class: Constraint
This function is called in case of the backtrack. It is called after all timestamps, variables, mutablevariables have reverted to their values *after* removing the level.

Overrides:
removeLevelLate in class Constraint
Parameters:
level - the level which is being removed.

consistency

public void consistency(Store store)
Description copied from class: Constraint
It is a (most probably incomplete) consistency function which removes the values from variables domains. Only values which do not have any support in a solution space are removed.

Specified by:
consistency in class Constraint
Parameters:
store - constraint store within which the constraint consistency is being checked.

computeForbidden

public void computeForbidden()
It searches through a subset of right items to find the ones which can not be fully included without violating the profit requirement in the knapsack constraint.


computeMandatory

public void computeMandatory()
It computes the mandatory part of the knapsack pruning.


impose

public void impose(Store store)
Description copied from class: Constraint
It imposes the constraint in a given store.

Specified by:
impose in class Constraint
Parameters:
store - the constraint store to which the constraint is imposed to.

queueVariable

public void queueVariable(int level,
                          Var v)
Description copied from class: Constraint
This is a function called to indicate which variable in a scope of constraint has changed. It also indicates a store level at which the change has occurred.

Overrides:
queueVariable in class Constraint
Parameters:
level - the level of the store at which the change has occurred.
v - variable which has changed.

numberArgs

public int numberArgs()
Description copied from class: Constraint
It returns the number of variables within a constraint scope.

Overrides:
numberArgs in class Constraint
Returns:
number of variables in the constraint scope.

getConsistencyPruningEvent

public int getConsistencyPruningEvent(Var var)
Description copied from class: Constraint
It retrieves the pruning event which causes reevaluation of the constraint.

Specified by:
getConsistencyPruningEvent in class Constraint
Parameters:
var - variable for which pruning event is retrieved
Returns:
it returns the int code of the pruning event (GROUND, BOUND, ANY, NONE)

id

public java.lang.String id()
Description copied from class: Constraint
It gives the id string of a constraint.

Specified by:
id in class Constraint
Returns:
string id of the constraint.

removeConstraint

public void removeConstraint()
Description copied from class: Constraint
It removes the constraint by removing this constraint from all variables.

Specified by:
removeConstraint in class Constraint

increaseWeight

public void increaseWeight()
Description copied from class: Constraint
It increases the weight of the variables in the constraint scope.

Specified by:
increaseWeight in class Constraint

arguments

public java.util.ArrayList<Var> arguments()
Description copied from class: Constraint
It returns the variables in a scope of the constraint.

Specified by:
arguments in class Constraint
Returns:
variables in a scope of the constraint.

satisfied

public boolean satisfied()
Description copied from class: Constraint
It checks if the constraint is satisfied. If this function is incorrectly implemented a constraint may not be satisfied in a solution.

Specified by:
satisfied in class Constraint
Returns:
true if the constraint is for certain satisfied, false otherwise.

toString

public java.lang.String toString()
Description copied from class: Constraint
It produces a string representation of a constraint state.

Specified by:
toString in class Constraint