Minizinc更简单的数组模型方法?

时间:2018-10-16 08:04:26

标签: arrays minizinc

是否有可能仅使用int和取整/取反的语句将数组和所有语句Model表示为一种更简单的方法? 我正在尝试跳过数组和所有语句,但似乎仅对整数不起作用。 在提供结果的数组.mzn方法下面:

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;

以及一段时间后不返回的整数更简单方法:

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;

1 个答案:

答案 0 :(得分:1)

您的问题是一个很好的例子,说明问题的不同表达方式在不同的求解器上的表现非常不一致。

您的初始公式在一般的CP求解器(如Gecode)上工作得相对较好。尽管它包含析取,更多决策变量和修正(所有这些可能都是不好的),但它会相对较快地找到解决方案。这似乎表明,即使有约束条件,求解器也可以传播很多。

但是,您的第二个模型包含巨大的析取约束。 CP解算器通常只需要猜测这些约束中的哪些部分将成立,而哪些将不成立。这意味着从该约束开始将出现零传播,直到知道有关该约束的更多信息为止。这意味着搜索非常重要!

这种约束格式与SAT公式不同,当该模型与LCG解算器(内部具有SAT解算器)一起使用时(如恰如其分),模型在一秒钟之内就是解算器。 Chuffed从错误中吸取教训,因此可以消除其搜索树的许多部分。