Package org.jacop.constraints.knapsack
Class Knapsack
java.lang.Object
org.jacop.constraints.DecomposedConstraint<Constraint>
org.jacop.constraints.Constraint
org.jacop.constraints.knapsack.Knapsack
- All Implemented Interfaces:
RemoveLevelLate
,SatisfiedPresent
,UsesQueueVariable
public class Knapsack
extends Constraint
implements UsesQueueVariable, SatisfiedPresent, RemoveLevelLate
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.
- Version:
- 4.8
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
It specifies how many consistency functions must be executed before the information about the constraint is being printed out.private int
It counts the number of time the consistency function has been executed.private int
It counts the number of times the queueVariable function has been executed.private int
It counts the number of time the removeLevel function has been executed.int
It specifies the current level of the constraint store at which the consistency function of this constraint is being executed.static final boolean
It specifies if any debugging information should be printed.It stores for each level the leaves which have changed at this level.private static final AtomicInteger
private boolean
It specifies if the constraint has already discovered to be unsatisfied during the imposition stage.private boolean
It specifies if the constraint is executing the consistency function.The array of items present in the knapsack constraint.protected IntVar
This is a finite domain variable to specify the knapsack capacity.protected IntVar
This is a finite domain variable to specify the knapsack profit.private TreeLeaf[]
It stores all the leaves of the knapsack tree in one array.private boolean
It specifies if the consistency function should execute.private boolean
It specifies if the recomputation of the critical item should take place.private boolean
It specifies if the forbidden part of the consistency algortihm should be executed.private boolean
It specifies if the mandatory part of the consistency algorithm should be executed.private boolean
It specifies if the knapsack tree requires an update.private int
It specifies the position of the last changed item which has been already been recomputed.The current position of the critical item in the treeprivate int
It specifies how many queueVariable functions must be executed before the information about the constraint is being printed out.private int
It specifies how many removeLevel functions must be executed before the information about the constraint is being printed out.The tree for the storing information about the maximalWeight, sum of weights and sum of profits.private int
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.It specifies mapping from variables into the leaf of the knapsack tree.Fields inherited from class org.jacop.constraints.Constraint
atomicExecution, consistencyPruningEvents, constraintScope, earlyTerminationOK, increaseWeight, numberId, scope, trace
Fields inherited from class org.jacop.constraints.DecomposedConstraint
queueIndex
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
It updates the knapsack tree to reflect all the changes which has happen since the last execution of the consistency function.private boolean
It verifies that leaves within tree have properly reflected slice variables within the items.void
It is executed after the constraint has failed.private void
commonInitialization
(int[] profits, int[] weights, IntVar[] quantity, IntVar knapsackCapacity, IntVar knapsackProfit) private void
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.private void
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.private String
int
void
It imposes the constraint in a given store.int
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
removeLevelLate
(int level) This function is called in case of the backtrack.private void
restrictItemQuantity
(Store store, TreeNode parent, int availableCapacity) It makes sure that no item has a possibility to use more than available capacity.boolean
It checks if the constraint is satisfied.private boolean
It checks that the minimal values of items are contributing correctly towards tree already obtained profit, as well as already used capacity.toString()
It produces a string representation of a constraint state.Methods inherited from class org.jacop.constraints.Constraint
afc, arguments, decompose, getConsistencyPruningEvent, getGuideConstraint, getGuideValue, getGuideVariable, grounded, grounded, id, impose, imposeDecomposition, increaseWeight, intArrayToString, removeConstraint, requiresMonotonicity, setConsistencyPruningEvent, setConstraintScope, setScope, setScope, setScope, setScope, setScope, setWatchedVariableGrounded, supplyGuideFeedback, updateAFC, watchedVariableGrounded
Methods inherited from class org.jacop.constraints.DecomposedConstraint
auxiliaryVariables, checkInput, checkInput, checkInputForDuplication, checkInputForDuplicationSkipSingletons, checkInputForNullness, checkInputForNullness, checkInputForNullness, derivative, getDubletonsSkipSingletons, imposeDecomposition
-
Field Details
-
idNumber
-
debugAll
public static final boolean debugAllIt specifies if any debugging information should be printed.- See Also:
-
variableLeafMapping
It specifies mapping from variables into the leaf of the knapsack tree. -
positionOfCriticalItem
The current position of the critical item in the tree -
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. -
hashForUpdate
It stores for each level the leaves which have changed at this level. It is used by removeLevel function. There is a limit at which leaves will not be added and the whole tree will be updated. -
updateLimit
private int updateLimitIt 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
private boolean impositionFailureIt specifies if the constraint has already discovered to be unsatisfied during the imposition stage. -
knapsackCapacity
This is a finite domain variable to specify the knapsack capacity. -
knapsackProfit
This is a finite domain variable to specify the knapsack profit. -
currentLevel
public int currentLevelIt specifies the current level of the constraint store at which the consistency function of this constraint is being executed. -
positionOfAlreadyUpdated
private int positionOfAlreadyUpdatedIt 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
private boolean needUpdateIt specifies if the knapsack tree requires an update. -
needConsistency
private boolean needConsistencyIt specifies if the consistency function should execute. -
needMandatory
private boolean needMandatoryIt specifies if the mandatory part of the consistency algorithm should be executed. -
needForbidden
private boolean needForbiddenIt specifies if the forbidden part of the consistency algortihm should be executed. -
needCriticalUpdate
private boolean needCriticalUpdateIt specifies if the recomputation of the critical item should take place. -
inConsistency
private boolean inConsistencyIt specifies if the constraint is executing the consistency function. -
tree
The tree for the storing information about the maximalWeight, sum of weights and sum of profits. -
items
The array of items present in the knapsack constraint. -
countConsistency
private int countConsistencyIt counts the number of time the consistency function has been executed. -
countQueueVariable
private int countQueueVariableIt counts the number of times the queueVariable function has been executed. -
countRemoveLevel
private int countRemoveLevelIt counts the number of time the removeLevel function has been executed. -
REMOVE_INFO_FROM
private int REMOVE_INFO_FROMIt specifies how many removeLevel functions must be executed before the information about the constraint is being printed out. -
QUEUE_INFO_FROM
private int QUEUE_INFO_FROMIt specifies how many queueVariable functions must be executed before the information about the constraint is being printed out. -
CONSISTENCY_INFO_FROM
private int CONSISTENCY_INFO_FROMIt specifies how many consistency functions must be executed before the information about the constraint is being printed out.
-
-
Constructor Details
-
Knapsack
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
- finite domain variable defining the profit
-
-
Method Details
-
commonInitialization
-
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 classConstraint
-
removeLevelLate
public void removeLevelLate(int level) Description copied from interface:RemoveLevelLate
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.- Specified by:
removeLevelLate
in interfaceRemoveLevelLate
- Parameters:
level
- the level which is being removed.
-
restrictItemQuantity
It makes sure that no item has a possibility to use more than available capacity.quantity.max() * weight < remainingCapacity.
- Parameters:
store
- the constraint store responsible for stroing the problem.parent
- the node from which the restriction of items quantities takes place (usually the root).availableCapacity
- it specifies how much left there is a knapsack company.
-
blockUpdate
private void blockUpdate()It updates the knapsack tree to reflect all the changes which has happen since the last execution of the consistency function. It will in particular update information about already obtained profit as well as already used capacity. The domain of knapsack profit or capacity variable may be reduced. -
consistency
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 classConstraint
- Parameters:
store
- constraint store within which the constraint consistency is being checked.
-
computeForbidden
private 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
private void computeMandatory()It computes the mandatory part of the knapsack pruning. -
impose
Description copied from class:Constraint
It imposes the constraint in a given store.- Overrides:
impose
in classConstraint
- Parameters:
store
- the constraint store to which the constraint is imposed to.
-
queueVariable
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 classConstraint
- 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 classConstraint
- Returns:
- number of variables in the constraint scope.
-
getDefaultConsistencyPruningEvent
public int getDefaultConsistencyPruningEvent()- Specified by:
getDefaultConsistencyPruningEvent
in classConstraint
-
satisfied
public boolean satisfied()Description copied from interface:SatisfiedPresent
It checks if the constraint is satisfied. It can return false even if constraint is satisfied but not all variables in its scope are grounded. It needs to return true if all variables in its scope are grounded and constraint is satisfied.Implementations of this interface for constraints that are not PrimitiveConstraint may require constraint imposition and consistency check as a requirement to work correctly.
- Specified by:
satisfied
in interfaceSatisfiedPresent
- Returns:
- true if constraint is possible to verify that it is satisfied.
-
toString
Description copied from class:Constraint
It produces a string representation of a constraint state.- Overrides:
toString
in classConstraint
-
sliceInvariant
private boolean sliceInvariant()It checks that the minimal values of items are contributing correctly towards tree already obtained profit, as well as already used capacity.- Returns:
- true to specify that invariants are maintained correctly.
-
displayQuantitiesInEfficiencyOrder
-
checkInvariants
private boolean checkInvariants()It verifies that leaves within tree have properly reflected slice variables within the items.
-