了解minizinc

Kim*_*son 2 minizinc

练习是:

一群n人想拍一张合影.每个人都可以在他或她想要放在照片上的人旁边给出偏好.要解决的问题是找到满足最大数量的首选项的展示位置.

我到目前为止编写的代码:

include "globals.mzn";

% input variables
int: n;
int: n_prefs;
array[1..n_prefs, 1..2] of var 1..n: prefs;

% FDV:s
array [1..n] of var 1..n: photo_arrangement;
var 0..n_prefs: cost;

constraint
     all_different(photo_arrangement)
% MORE Constraints

solve maximize cost;

output [show( photo_arrangement )]
Run Code Online (Sandbox Code Playgroud)

n是照片中的人数

n_prefs是首选项的数量

prefs是包含所有首选项的矩阵

主要思想是有一个包含1到n人的数组,以及我们想要最大化的成本变量.如何根据偏好更改成本变量?

hak*_*ank 5

这是一种方法.(更新:实际上,现在有三个不同的模型具有相同的基本想法.)

include "globals.mzn";

% input variables
int: n;
int: n_prefs;
array[1..n_prefs, 1..2] of 1..n: prefs;

% FDV:s
array [1..n] of var 1..n: photo_arrangement;
% the positions of each person in photo_arrangement
array [1..n] of var 1..n: pa_inv = inverse(photo_arrangement); 
% to see what preferences that are satisfied
array[1..n_prefs] of var int: prefs_sat; 
var 0..n_prefs: cost;

constraint
  all_different(photo_arrangement)
  /\
  forall(p in 1..n_prefs) (
     % note: we use the inverse of photo_arrangement for indexing since we
     %       want to compare the positions of the two persons prefs[p,1] and prefs[p,2]
    prefs_sat[p] = if abs(pa_inv[prefs[p,1]]-pa_inv[prefs[p,2]]) = 1 then 1 else 0 endif
 )
 /\
 cost = sum(prefs_sat)
 ;

 solve :: int_search(photo_arrangement, first_fail, indomain_split, complete) maximize cost;
 output [
   "cost: \(cost)\nphoto_arrangement: \(photo_arrangement)\n(pa_inv:           \(pa_inv))\n"
 ] ++
 [
   show([prefs[p,i] | i in 1..2]) ++ ": " ++ show(prefs_sat[p]) ++ "\n"
   | p in 1..n_prefs
 ];

 % data
 n = 9;
 n_prefs = 17;
 prefs = [| 1,3 | 1,5 | 1,8 | 2,5 | 2,9 | 3,4 | 3,5 | 4,1 | 4,5 | 5,6 | 5,1 | 6,1 | 6,9 | 7,3 | 7,8 | 8,9 | 8,7 |];
Run Code Online (Sandbox Code Playgroud)

主要的一点是使用额外的阵列(的pa_inv),它是inversephoto_arrangement并显示每个人的位置.这意味着我们可以用pa_inv[1]得到的人1的位置,从而可以计算出的位置的差异pa_inv[prefs[p,1]pa_inv[prefs[p,2](这是1,如果两个人彼此之间).该prefs_sat数组显示是否满足偏好(1)或不满足(0).

有20个最佳解决方案,10个满意的偏好.一个最佳解决方案是:

cost: 10
photo_arrangement: [2, 5, 1, 4, 3, 7, 8, 9, 6]
(pa_inv:           [3, 1, 5, 4, 2, 9, 6, 7, 8])
[1, 3]: 0
[1, 5]: 1
[1, 8]: 0
[2, 5]: 1
[2, 9]: 0
[3, 4]: 1
[3, 5]: 0
[4, 1]: 1
[4, 5]: 0
[5, 6]: 0
[5, 1]: 1
[6, 1]: 0
[6, 9]: 1
[7, 3]: 1
[7, 8]: 1
[8, 9]: 1
[8, 7]: 1
Run Code Online (Sandbox Code Playgroud)

几分钟后更新:

这是使用element函数而不是使用函数的另一种方法inverse,这意味着我们不需要pa_inv数组.forall上面代码中的循环可以替换为:

  %  
  forall(p in 1..n_prefs) (
       prefs_sat[p] = if abs(element([prefs[p,1],photo_arrangement)-element(prefs[p,2],photo_arrangement)) = 1 then 1 else 0 endif
   )
  %  
Run Code Online (Sandbox Code Playgroud)

几天后更新: 还有另一个 - 可以说更简单 - 模型,类似于以前的方法,但它使用输出中的"反向"部分.

include "globals.mzn";
int: n;
int: n_prefs;
array[1..n_prefs, 1..2] of 1..n: prefs;
array [1..n] of var 1..n: photo_arrangement;
var 0..n_prefs: cost;

constraint
   all_different(photo_arrangement) /\
   cost = sum(p in 1..n_prefs) (
      if abs(photo_arrangement[prefs[p,1]]-photo_arrangement[prefs[p,2]]) = 1 then 1 else 0 endif
          )
;

solve :: int_search(photo_arrangement, first_fail, indomain_split, complete) maximize cost;

output [
   "cost: \(cost)\nphoto_arrangement: \(photo_arrangement)\n",
  "positions:\n"
] ++ [
   if fix(photo_arrangement[j]) = i then show(j) ++ " " else "" endif
  | i,j in 1..n
];

n = 9;
n_prefs = 17;
prefs = [| 1,3 | 1,5 | 1,8 | 2,5 | 2,9 | 3,4 | 3,5 | 4,1 | 4,5 | 5,6 | 5,1 | 6,1 | 6,9 | 7,3 | 7,8 | 8,9 | 8,7 |];
Run Code Online (Sandbox Code Playgroud)

解决方案是

cost: 10
photo_arrangement: [8, 1, 5, 6, 7, 9, 4, 3, 2]
positions:
2 9 8 7 3 4 5 1 6 
Run Code Online (Sandbox Code Playgroud)