【问题标题】:Declare a variable to maximize it in Minizinc在 Minizinc 中声明一个变量以使其最大化
【发布时间】:2022-09-24 04:31:02
【问题描述】:

我有一个问题是确定一组骑士可以在棋盘内进行的最大移动长度,条件是:

  • 有 4 个骑士,他们的移动顺序是:A -> B -> C -> D。他们的第一个位置是角。
  • 有些单元格不能访问,其余的只能访问k次。第一个位置不算。
  • 结果应该是骑士在棋盘上可以做的一组动作。

这是我的代码,但我不知道如何修改程序以最大化路径(t)的值:

include \"globals.mzn\";

int: n=4; %nxnxt board
int: k=1; %k times visited cell
var 0..100: t; %Lenth of the path

%Initial board
array[1..t, 1..n, 1..n] of var 0..k:b;

% Decision variables (*CHANGED*)
array[1..t,1..4] of var 1..n: r;% The sequence of moves in the path
array[1..t,1..4] of var 1..n: c;% (row and column of each move).
%%% Always the same order A -> B -> C -> D knights

%Constraints

   % Forcing the first moves.

constraint r[1,1] = 1;%A
constraint c[1,1] = 1;
constraint r[1,2] = 1;%B
constraint c[1,2] = n;
constraint r[1,3] = n;%C
constraint c[1,3] = 1;
constraint r[1,4] = n;%D
constraint c[1,4] = n;

constraint b[1,1,2] = k;
constraint b[1,1,3] = k;
constraint b[1,2,1] = k;
constraint b[1,3,1] = k;
constraint b[1,2,4] = k;
constraint b[1,3,4] = k;
constraint b[1,4,2] = k;
constraint b[1,4,3] = k;

% LIMIT ON VISITS (*ADDED*)
constraint
     forall (i in 1..t, j in 1..n, l in 1..n) (
           b[i,j,l] <= k
     );

% SUCCESSOR (STEP OF THE KNIGHT)

constraint
     forall (i in 1..t-1, j in 1..4) (
           c[i,j] != c[i+1,j] /\\%Each movement has to be diferent than the previous one
           r[i,j] != r[i+1,j] /\\
           abs(c[i,j] - c[i+1,j]) + abs(r[i,j] - r[i+1,j]) = 3
     );
     
% NEVER TWO QUEENS ON THE SAME CELL

constraint forall(i in 1..t, j in 1..3, p in 2..4 where p > j )(
        r[i,j] != r[i,p] \\/     
        c[i,j] != c[i,p]);
 
constraint forall(i in 2..t, j in 1..n, l in 1..n)(
      if b[i-1,j,l] = k then  
          b[i, j, l] = k
      endif
);

% APPLY THE MOVE IN THE MATRIX
constraint
     forall (i in 2..t, j in 1..4) ( 
         exists(w in {-2, 2}, q in {-1, 1}) ( % Set up the possible moviments.
         if  1 <= r[i-1,j]+w /\\ r[i-1,j]+w <= n /\\ 
             1 <= c[i-1,j]+q /\\ c[i-1,j]+q <= n /\\ 
             b[i-1, r[i-1, j]+w, c[i-1, j]+q] < k then
              (r[i,j] = r[i-1, j] + w /\\
               c[i,j] = c[i-1, j] + q)
         endif
              \\/
         if  1 <= r[i-1,j]+q /\\ r[i-1,j]+q <= n /\\ 
             1 <= c[i-1,j]+w /\\ c[i-1,j]+w <= n /\\ 
             b[i-1, r[i-1,j]+q, c[i-1,j]+w] < k then
              (r[i,j] = r[i-1, j] + q /\\
               c[i,j] = c[i-1, j] + w) 
        endif) /\\
        b[i, r[i,j], c[i,j]] = b[i-1, r[i,j], c[i,j]] + 1
);     
solve maximize t;

output[\"r\"]++[
  if j = 1 then \"\\n\" else \"\" endif ++
    show(r[i,j]) ++ \" \"
  | i in 1..t, j in 1..n   
]++[\"\\n\\nc\"]++
[
  if j = 1 then \"\\n\" else \"\" endif ++
    show(c[i,j]) ++ \" \"
  | i in 1..t, j in 1..n  
]++[\"\\n\"] ++
[ if l = 1 then \"\\n\" else \"\" endif ++  
show(b[i,j,l]) ++ \" \" 
|i in 1..t, j in 1..n, l in 1..n];

include \"globals.mzn\";

int: n=4; %nxnxt board
int: k=1; %k times visited cell
var 0..100: t; %Lenth of the path
l in 1..n];

    标签: constraints constraint-programming maximize minizinc


    【解决方案1】:

    规划风格问题的一般方法是首先设置可能步骤数的上限。然后修改模型,以便可以应用一些哨兵无移动移动,但只能在移动结束时应用。最后,您可以通过计算不移动的数量并从固定上限中减去它来计算所使用的移动数量。

    【讨论】:

      【解决方案2】:

      您好,请问,是否可以将正确的最终代码发送给我的邮件?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        相关资源
        最近更新 更多