Последний раз редактировалось dmd 03.11.2014, 22:26, всего редактировалось 1 раз.
Задача. Дан квадрат сторона которого равна 1. В каждую вершину квадрата необходимо поместить k чисел из интервала [1...4k]. Найти решение с максимальным числом Делакорта. (MiniZinc модель)
Код: include "globals.mzn";
int: k = 7; set of int: I = 1..4; set of int: J = 1..k; set of int: N = 1..4*k; array[I,J] of var N: X;
function var int: gcd(var int: x, var int: y) = let { int: p = min(ub(x),ub(y)); var 1..p: g; constraint exists(i in 1..p) (x mod i = 0 /\ y mod i = 0 /\ forall(j in i+1..p) (not(x mod j = 0 /\ y mod j = 0)) /\ g = i); } in g;
constraint alldifferent (i in I, j in J) (X[i,j]);
var int: DN = sum(ia,ib in I, ja,jb in J) (gcd(X[ia,ja],X[ib,jb])*(if (ia=1 /\ ib=4) \/ (ia=2 /\ ib=3) then 2 else 1 endif));
solve maximize DN;
output ["k = ", show(k), "\n"] ++ ["Delacorte Number = ", show(DN), "\n"] ++ ["X = \n"] ++ [if j = 1 then "(" else "" endif ++ show(X[i,j]) ++ if j = k then ")\n" else ", " endif | i in I, j in J] ++ [ "\n" ];
(решения)
Код: k = 1 Delacorte Number = 27 X = (3) (4) (2) (1)
k = 2 Delacorte Number = 126 X = (1, 3) (2, 8) (4, 7) (6, 5)
k = 3 Delacorte Number = 325 X = (5, 3, 2) (4, 12, 9) (8, 11, 6) (10, 1, 7)
k = 4 Delacorte Number = 614 X = (3, 6, 4, 8) (2, 13, 1, 15) (10, 5, 11, 9) (7, 14, 16, 12)
k = 5 Delacorte Number = 983 X = (16, 3, 20, 14, 2) (18, 17, 1, 11, 5) (4, 10, 9, 13, 6) (15, 19, 7, 8, 12)
k = 6 Delacorte Number = 1504 X = (19, 4, 16, 22, 17, 7) (13, 10, 15, 18, 3, 14) (20, 9, 5, 21, 2, 12) (8, 11, 23, 6, 24, 1)
k = 7 Delacorte Number = 2066 X = (17, 19, 5, 10, 23, 7, 20) (2, 18, 24, 3, 4, 6, 12) (13, 1, 28, 11, 16, 14, 27) (15, 8, 9, 26, 25, 22, 21) Не факт, конечно, что решения полностью оптимальны. Решалось в версии 2.0.b4 внутренним солвером G12_lazyfd -- Вт ноя 04, 2014 00:26:55 --Задача. Разбить числа из интервала [1...2k] на два подмножества по k чисел в каждом, так чтобы число Делакорта было максимальным. Если числа принадлежат разным подмножествам будем считать, что расстояние между ними 1. В противном случае растояние будет 0. (модель)
Код: include "globals.mzn";
int: k = 8; set of int: I = 1..2; set of int: J = 1..k; set of int: N = 1..2*k; array[I,J] of var N: X;
function var int: gcd(var int: x, var int: y) = let { int: p = min(ub(x),ub(y)); var 1..p: g; constraint exists(i in 1..p) (x mod i = 0 /\ y mod i = 0 /\ forall(j in i+1..p) (not(x mod j = 0 /\ y mod j = 0)) /\ g = i); } in g;
constraint alldifferent (i in I, j in J) (X[i,j]);
var int: DN = sum(ia,ib in I, ja,jb in J) (gcd(X[ia,ja],X[ib,jb])*(if (ia!=ib) then 1 else 0 endif));
solve maximize DN;
output ["k = ", show(k), "\n"] ++ ["Delacorte Number = ", show(DN), "\n"] ++ ["X = \n"] ++ [if j = 1 then "(" else "" endif ++ show(X[i,j]) ++ if j = k then ")\n" else ", " endif | i in I, j in J] ++ [ "\n" ];
(решения)
Код: k = 1 Delacorte Number = 2 X = (2) (1)
k = 2 Delacorte Number = 10 X = (4, 3) (2, 1)
k = 3 Delacorte Number = 26 X = (6, 4, 5) (3, 2, 1)
k = 4 Delacorte Number = 48 X = (5, 6, 7, 8) (4, 3, 2, 1)
k = 5 Delacorte Number = 82 X = (8, 5, 9, 3, 2) (4, 7, 10, 6, 1)
k = 6 Delacorte Number = 126 X = (10, 8, 3, 11, 7, 12) (4, 2, 6, 5, 9, 1)
k = 7 Delacorte Number = 158 X = (10, 14, 8, 6, 9, 4, 2) (1, 7, 11, 5, 3, 12, 13)
k = 8 Delacorte Number = 236 X = (3, 16, 5, 4, 2, 15, 1, 14) (12, 10, 13, 6, 9, 7, 8, 11)
|