Class KnapsackExample


  • public class KnapsackExample
    extends ExampleFD
    It shows the capabilities and usage of Knapsack constraint.
    Version:
    4.8

    It models and solves a simple knapsack problem. There are two different models. The first one uses quantity from 0 to n, where the second model is allowed to use only binary variables.

    Each item is specified by its weight and profit. Find what objects should be put in the knapsack to maximize the profit without exceeding the knapsack capacity.

    • Field Detail

      • args

        public java.lang.String[] args
        It stores the parameters of the main function to be used by the model functions.
    • Constructor Detail

      • KnapsackExample

        public KnapsackExample()
    • Method Detail

      • model

        public void model()
        Description copied from class: ExampleFD
        It specifies a standard way of modeling the problem.
        Specified by:
        model in class ExampleFD
      • modelNoKnapsackConstraint

        public void modelNoKnapsackConstraint()
        It does not use Knapsack constraint only SumWeight constraints.
      • modelBoth

        public void modelBoth()
        It does not use Knapsack constraint only SumWeight constraints.
      • modelBasic

        public void modelBasic()
        It creates a model where quantity variable is allowed only to be between 0 and 1, so if the original description allows n items n copies of that items must be created.
      • main

        public static void main​(java.lang.String[] args)
        It executes the two different models to find a solution to a knapsack problem. It is possible to supply the knapsack problem through the parameters. The parameters are order as follows : string denoting the capacity of the knapsack 4 strings denoting the item (weight, profit, maximumQuantity, name) the number of strings total must be equal to 1+4*noOfItems.

        If no arguments is provided or improper number of them the program will use internal instance of the knapsack problem.

        Parameters:
        args - the capacity of the knapsack, 4 strings denoting the item (weight, profit, maximumQuantity, name), the number of strings total must be equal to 1+4*noOfItems.