user2829319
user2829319

Reputation: 319

Minizinc simpler approach for an array Model?

Is it possible to represent an array and for all statement Model into a simpler approach using only int and conjuctive/ disjunctive statements? I am trying to skip arrays and for all statements but it seems it does not work only with integers.. Below the array .mzn approach that gives results:

int: x; 
int: y;
int: z;
int: k;

array[1..50] of {0,1,5,15,30}: Ingredient_1=[30 ,   30 ,    30 ,    15, 15, 15, 5 , 5 , 5 , 1,  1,  1,  30 ,    30 ,    30 ,    15, 15, 15, 5 , 5 , 5 , 1,  1,  1,  30 ,    30 ,    30 ,    15, 15, 15, 5 , 5 , 5 , 1,  1,  1,  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0   ];

array[1..50] of {0,3,7,12}: Ingredient_2=[3 ,   7 , 12, 3 , 7 , 12, 3 , 7 , 12, 3 , 7 , 12, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , 3 , 3 , 7 , 7 , 7 , 12, 12, 12, 3 , 3 , 3 , 7 , 7 ];

array[1..50] of {0,3,6,1000}: Ingredient_3= [0   ,  0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   0   ,   0   ,   0   ,   0   ,   0];

array[1..50] of {0,3,6,1000}: Ingredient_4=[0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   3   ,   6   ,   1000,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   0   ,   3   ,   6   ,   1000,   3   ,   6];



%decision variable%
var set of 1..50 : occur; 


%c1
constraint x=0-> forall (i in occur) (Ingredient_4 [i]= 0);
%c2
constraint y=7 \/ y=6 -> forall (i in occur)(Ingredient_1 [i]=30 );
%c3
constraint y=1 -> forall (i in occur)(Ingredient_1 [i]=0 ) ;
%c4 z in 5..7 
constraint z =5 \/ z=6\/ z=7  ->  forall (i in occur)(Ingredient_4[i] !=0) ;
%c5
constraint k=7 \/ k=6  -> forall (i in occur)(Ingredient_2 [i] =12);
%c6
constraint k=5 -> forall (i in occur)(Ingredient_2 [i] =7);
%c7
constraint k=4 \/ k=3  -> forall (i in occur)(Ingredient_2 [i] !=0);

solve satisfy;



output ["product ids:" ++show(occur) ++"\n" 

++"Ingredient_1:" ++show(i in occur) (Ingredient_1[i])++"\n"
++"Ingredient_2:" ++show(i in occur) (Ingredient_2[i])++"\n"
++"Ingredient_3:" ++show(i in occur) (Ingredient_3[i])++"\n"
++"Ingredient_4:" ++show(i in occur) (Ingredient_4[i])++"\n"
++"Total number of products:"++ show(card(occur))++"\n"
];

%Data Input

y =7;
x=0;
z=0;
k=4;

and the integer simpler approach that does not return after some time:

int: x; 

int: y ;

int: z ;

int: k;


%Decision Variables

var int : Ingredient_1;

var int : Ingredient_2;

var int : Ingredient_3;

var int : Ingredient_4;

var int :product ;


constraint x=0->  Ingredient_4= 0;
constraint y=7 \/ y=6 -> Ingredient_1=30 ;
constraint y=1 -> Ingredient_1=0  ;
constraint z in 5..7   ->   Ingredient_4!=0 ;
constraint k=7 \/ k=6  ->Ingredient_2  =12;
constraint k=5 -> Ingredient_2 =7;
constraint k=4 \/ k=3  ->Ingredient_2 !=0;



constraint
(product = 1 /\ Ingredient_1 = 30 /\ Ingredient_2=3 /\ Ingredient_3 =0 /\ Ingredient_4 = 0) 
\/
(   product=    2   /\ Ingredient_1=    30  /\ Ingredient_2 =   7   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    3   /\ Ingredient_1=    30  /\ Ingredient_2 =   12  /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    4 /\ Ingredient_1=  15  /\ Ingredient_2 =   3   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    5       /\ Ingredient_1=    15  /\ Ingredient_2 =   7   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    6       /\ Ingredient_1=    15  /\ Ingredient_2 =   12  /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    7       /\ Ingredient_1=    5   /\ Ingredient_2 =   3   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    8       /\ Ingredient_1=    5   /\ Ingredient_2 =   7   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    9       /\ Ingredient_1=    5   /\ Ingredient_2 =   12  /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    10                      /\ Ingredient_1=    1   /\ Ingredient_2 =   3   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    11                      /\ Ingredient_1=    1   /\ Ingredient_2 =   7   /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    12                      /\ Ingredient_3=    1   /\ Ingredient_2 =   12  /\ Ingredient_3=    0   /\ Ingredient_4=    0   )   \/
(   product=    13                      /\ Ingredient_1=    30  /\ Ingredient_2 =   0   /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    14                      /\ Ingredient_1=    30  /\ Ingredient_2 =   0   /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    15                      /\ Ingredient_1=    30  /\ Ingredient_2 =   0   /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    16                      /\ Ingredient_1=    15  /\ Ingredient_2 =   0   /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    17                      /\ Ingredient_1=    15  /\ Ingredient_2 =   0   /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    18                      /\ Ingredient_1=    15  /\ Ingredient_2 =   0   /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    19                      /\ Ingredient_1=    5   /\ Ingredient_2 =   0   /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    20                      /\ Ingredient_1=    5   /\ Ingredient_2 =   0   /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    21                      /\ Ingredient_1=    5   /\ Ingredient_2 =   0   /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    22                      /\ Ingredient_1=    1   /\ Ingredient_2 =   0   /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    23                      /\ Ingredient_1=    1   /\ Ingredient_2 =   0   /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    24                      /\ Ingredient_1=    1   /\ Ingredient_2 =   0   /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    25                      /\ Ingredient_1=    30  /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    3   )   \/
(   product=    26                      /\ Ingredient_1=    30  /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    6   )   \/
(   product=    27                      /\ Ingredient_1=    30  /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    1000    )   \/
(   product=    28                      /\ Ingredient_1=    15  /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    3   )   \/
(   product=    29                      /\ Ingredient_1=    15  /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    6   )   \/
(   product=    30                      /\ Ingredient_1=    15  /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    1000    )   \/
(   product=    31                      /\ Ingredient_1=    5   /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    3   )   \/
(   product=    32                      /\ Ingredient_1=    5   /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    6   )   \/
(   product=    33                      /\ Ingredient_1=    5   /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    1000    )   \/
(   product=    34                      /\ Ingredient_1=    1   /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    3   )   \/
(   product=    35                      /\ Ingredient_1=    1   /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    6   )   \/
(   product=    36                      /\ Ingredient_1=    1   /\ Ingredient_2 =   0   /\ Ingredient_3=    0   /\ Ingredient_4=    1000    )   \/
(   product=    37                      /\ Ingredient_1=    0   /\ Ingredient_2 =   3   /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    38                      /\ Ingredient_1=    0   /\ Ingredient_2 =   3   /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    39                      /\ Ingredient_1=    0   /\ Ingredient_2 =   3   /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    40                      /\ Ingredient_1=    0   /\ Ingredient_2 =   7   /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    41                      /\ Ingredient_1=    0   /\ Ingredient_2 =   7   /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    42                      /\ Ingredient_1=    0   /\ Ingredient_2 =   7   /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    43                      /\ Ingredient_1=    0   /\ Ingredient_2 =   12  /\ Ingredient_3=    3   /\ Ingredient_4=    0   )   \/
(   product=    44                      /\ Ingredient_1=    0   /\ Ingredient_2 =   12  /\ Ingredient_3=    6   /\ Ingredient_4=    0   )   \/
(   product=    45                      /\ Ingredient_1=    0   /\ Ingredient_2 =   12  /\ Ingredient_3=    1000    /\ Ingredient_4=    0   )   \/
(   product=    46                      /\ Ingredient_1=    0   /\ Ingredient_2 =   3   /\ Ingredient_3=    0   /\ Ingredient_4=    3   )   \/
(   product=    47                      /\ Ingredient_1=    0   /\ Ingredient_2 =   3   /\ Ingredient_3=    0   /\ Ingredient_4=    6   )   \/
(   product=    48                      /\ Ingredient_1=    0   /\ Ingredient_2 =   3   /\ Ingredient_3=    0   /\ Ingredient_4=    1000    )   \/
(   product=    49                      /\ Ingredient_1=    0   /\ Ingredient_2 =   7   /\ Ingredient_3=    0   /\ Ingredient_4=    3   )   \/
(   product=    50                      /\ Ingredient_1=    0   /\ Ingredient_2 =   7   /\ Ingredient_3=    0   /\ Ingredient_4=    6   );

solve satisfy;


%data
y = 7;
x=0;
z=0;
k=4;

Upvotes: 0

Views: 129

Answers (1)

Dekker1
Dekker1

Reputation: 5786

Your problem is a very good example of how different formulations of a problems perform very inconsistently on different solvers.

Your initial formulation is work relatively well on general CP solvers (like Gecode). Although it contains disjunctions, more decision variables, and reifications, (all of which can be bad) it will find solutions relatively quick. This seems to indicate that the solver can propagate quite a lot, even with reified constraints.

Your second model, however, contains a huge disjunctive constraint. A CP solver will generally just have to guess which parts of these constraints will hold, and which won't. That means there will be zero propagation from this constraint until more information about the constraint is known. This means search is very important!

This format of constraint is not unlike SAT formulations, and when this model is used with a LCG solver (which have a SAT solver inside), like chuffed, the model is solver within a second. Chuffed learns from its mistakes and can therefore eliminate many parts of its search tree.

Upvotes: 1

Related Questions