Search code examples
minizinc

How can I get a combination of items


I'm just starting in Minizinc and I would some help on this.

How can I write the following constraints:

I want to buy at most 4 GROUP offers, spend at most 100$ and I can buy only one item from every group. Maximize quality.

int: items = 10;
set of int: GROUPS = 0..itms; 
set of int: PRODUCTS = 1..7;

set of int:BUYS = 1..4;
int : max_spent = 100;

array[GROUPS] of set of int : package = array1d(GROUPS,[{},{1,2,3},{4,7},{3,6},{1,4,5},{3,4,7},{1,2,5},{4,6},{3,7},{3,7,5},{2,3}]);
array[GROUPS] of int: package_price  =  array1d(GROUPS,[0,5,5,25,10,12,20,40,55,52,10]);
array[GROUPS] of int: package_quality = array1d(GROUPS,[0,7,2,7,2,3,5,4,9,6,5]);

The desired output should be something like:

{3,7} {4,6} {1,2,5} {}
quality = 10;
price = 97;

--- Update ---

So far I tried:

var int : will_buy;
will_buy = sum(i in BUYS)(package_price[i]);
constraint will_buy <= max_spent;

var int : quality;
quality = sum(i in GROUPS)(package_quality[i]);

array[GROUPS] of var BUYS: index;
include "element.mzn";
constraint forall(t in GROUPS)
            ( 
             element(index[t], package_price, package_price[t]) 
             /\  
             element(index[t], package_quality, package_quality[t])
             ); 

:/


Solution

  • Your problem is a variation on the well known knapsack problem. A solution to this problem is given in the MiniZinc tutorial, chapter 3.6. Understanding the MiniZinc model for the knapsack model should give you a guide to solving this problem.

    Alternatively, you might want to have a look at the global packing constraints, these might make the modeling easier.