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

    • idNumber

      private static final AtomicInteger idNumber
    • debugAll

      public static final boolean debugAll
      It specifies if any debugging information should be printed.
      See Also:
    • variableLeafMapping

      private Map<IntVar,TreeLeaf> variableLeafMapping
      It specifies mapping from variables into the leaf of the knapsack tree.
    • positionOfCriticalItem

      private TimeStamp<Integer> positionOfCriticalItem
      The current position of the critical item in the tree
    • leaves

      private 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.
    • hashForUpdate

      private Map<Integer,List<TreeLeaf>> 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 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

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

      protected IntVar knapsackCapacity
      This is a finite domain variable to specify the knapsack capacity.
    • knapsackProfit

      protected 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

      private 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

      private boolean needUpdate
      It specifies if the knapsack tree requires an update.
    • needConsistency

      private boolean needConsistency
      It specifies if the consistency function should execute.
    • needMandatory

      private boolean needMandatory
      It specifies if the mandatory part of the consistency algorithm should be executed.
    • needForbidden

      private boolean needForbidden
      It specifies if the forbidden part of the consistency algortihm should be executed.
    • needCriticalUpdate

      private boolean needCriticalUpdate
      It specifies if the recomputation of the critical item should take place.
    • inConsistency

      private 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

      private int countConsistency
      It counts the number of time the consistency function has been executed.
    • countQueueVariable

      private int countQueueVariable
      It counts the number of times the queueVariable function has been executed.
    • countRemoveLevel

      private int countRemoveLevel
      It counts the number of time the removeLevel function has been executed.
    • REMOVE_INFO_FROM

      private 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

      private 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

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

    • 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 - finite domain variable defining the profit
  • Method Details

    • commonInitialization

      private void commonInitialization(int[] profits, int[] weights, IntVar[] quantity, IntVar knapsackCapacity, IntVar knapsackProfit)
    • 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 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 interface RemoveLevelLate
      Parameters:
      level - the level which is being removed.
    • restrictItemQuantity

      private void restrictItemQuantity(Store store, TreeNode parent, int availableCapacity)
      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

      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

      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

      public void impose(Store store)
      Description copied from class: Constraint
      It imposes the constraint in a given store.
      Overrides:
      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.
    • getDefaultConsistencyPruningEvent

      public int getDefaultConsistencyPruningEvent()
      Specified by:
      getDefaultConsistencyPruningEvent in class Constraint
    • 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 interface SatisfiedPresent
      Returns:
      true if constraint is possible to verify that it is satisfied.
    • toString

      public String toString()
      Description copied from class: Constraint
      It produces a string representation of a constraint state.
      Overrides:
      toString in class Constraint
    • 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

      private String displayQuantitiesInEfficiencyOrder()
    • checkInvariants

      private boolean checkInvariants()
      It verifies that leaves within tree have properly reflected slice variables within the items.