future
future

Reputation: 89

Select and sum from a set of set

I'm new to Minizinc and need some help with the following problem:

let say I have the following sets

int: budget= 8;
enum paths = {c1,c2,c3,c4};
array[paths] of int: size=[20,50,90,10];
array[paths] of set of int:colors=[{1,2},{3,2},{4,1,3},{4,3}];
array[paths] of set of int:cost=[{1,2},{10,8},{4,4,4},{50,12}];
array[paths] of var set of int: x; 

or I can formulate it in the following way

int: budget= 8;
enum paths = {c1,c2,c3,c4};
array[paths] of int:size =[20,50,90,10];
enum colors = {w1,b2,y3,r4, h5};
array[colors] of int:cost=[2,10,50,12];
array[paths] of set of colors: s=[{w1,h5},{b2,y3,r4,},{w1,b2},{r4, h5}];

array [paths] of var set of colors: a;

So, each path has a fixed size, but I can choose between colors and each color has a cost. So if I allowed choosing one color for each path and then the sum of the cost for all the selected colors should be equal or below the budget. I know how to select from or sum an array but not from a set of set. Can I get any help, please?

Upvotes: 1

Views: 769

Answers (1)

Matias Agelvis
Matias Agelvis

Reputation: 971

Using the second formulation you can sum over the variable array a, and since its elements are sets you can make a sum over those sets, something like this

To pick a color constraint forall(i in paths)(card(a[i]) == 1);

To sum over the chosen colors constraint sum(p in paths)( sum(c in a[p]) (cost[c]) ) < budget; Since every set contains only one color you are basically just summing the over the array.

Edit:
- Change the definition of a to array [paths] of var colors: a;
- Ensure that you pick a value from the set of options for that path
constraint forall(p in paths)(a[p] in s[p]);
- Sum over the costs of the variable a sum(p in paths)( cost[a[p]] ) <= budget;
The modified code looks like this.

int: budget = 20;
enum paths = {c1,c2,c3,c4};
array[paths] of int : size = [20,50,90,10];
enum colors = {w1,b2,y3,r4,h5};
array[colors] of int : cost = [2,10,50,12,4];
array[paths] of set of colors: s = [ {w1,h5}, {b2,y3,r4}, {w1,b2}, {r4,h5} ];

array [paths] of var colors: a;

constraint forall(p in paths)(a[p] in s[p]);

constraint sum(p in paths)( cost[a[p]] ) <= budget;

Upvotes: 2

Related Questions