Mahesh Subramaniam
Mahesh Subramaniam

Reputation: 33

Maximize based on constraints

3 Arrays f1,f2 and f3 have child elements (f1a,f1b) ; (f2a,f2b,f2c) ; (f3a,f3b,f3c,f3d) respectively.They are all bool variables

I have a combination of these child elements in a list with 2 columns . The first column gives an ID and second col has the combination of child elements as shown below

I need to choose one element of each of the arrays and form a selected array if all elements of row are represented in selected array, I set 1 for that Row , else 0

So if selected array is [ f1a,f2a,f3c] , then ( using the above list example)

I need to choose the selected array so as to maximize the sum of Row count (0+1+0+0 ...) for all Unique ID for example if list is as shown below and selected array is [f1a,f2b,f3c] , though both rows are set 1 , I would only take 1 from both the rows as ID is same in both rows

Any help is appreciated as I am new to Minizinc and struggling to formulate the constraint with Minizinc

Upvotes: 0

Views: 1594

Answers (2)

Mahesh Subramaniam
Mahesh Subramaniam

Reputation: 33

I could finally get it working with this code. Here row1 and row2 have same ID and similarly row 3 and row4 have another common ID

array[1..2] of var bool: f1 ;
array[1..2] of var bool: f1 ;
array[1..3] of var bool: f2 ;
array[1..4] of var bool: f3 ;
var bool: row1;
var bool: row2 ;
var bool: row3 ;
var bool: row4 ;
constraint sum(f1) == 1;  
constraint sum(f2) == 1;
constraint sum(f3) == 1;

constraint  row1 = forall ( [(f1[1])  ]  ) ;
constraint  row2 = forall ( [(f1[2]) , (f2[2]) , (f3[3]) ] );

constraint  row3 = forall ( [(f1[1]) , (f2[2]) , (f3[3]) ] ) ;
constraint  row4 = forall ( [(f1[2]) , (f2[1]) , (f3[4]) ] ) ;

var int: rown1 = bool2int(row1) ;
var int: rown2 = bool2int(row2) ;
var int: rown2a = max(rown1,rown2) ;

var int: rown3 = bool2int(row3) ;
var int: rown4 = bool2int(row4) ;
var int: rown4a = max( rown3,rown4) ;
var int: rown =  rown2a + rown4a ;

solve maximize rown;

Upvotes: 0

Dekker1
Dekker1

Reputation: 5786

If your row variables are themselves also located in an array, then

solve maximize sum(row);

should set the activation of the rows to be the objective.

If you don't have them in an array you can still manually add them together to form an objective:

solve maximize row1 + row2 + row3 + row4;

I'm unsure if you were also asking about how the row activation itself works, but if a row only activates when all its elements are selected, then this would be enforced using a simple forall constraint:

constraint row1 = forall([...]);

Upvotes: 1

Related Questions