Skip to content

Instantly share code, notes, and snippets.

@neizod
Last active November 26, 2018 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/49f4362ac2c2c1fc19bcc32fe030a267 to your computer and use it in GitHub Desktop.
Save neizod/49f4362ac2c2c1fc19bcc32fe030a267 to your computer and use it in GitHub Desktop.
MiniZinc examples for Blognone.
array[1..3] of int: divisors = [2, 3, 7];
var 1..100: answer;
constraint forall(d in divisors)( answer mod d == 0 );
solve satisfy;
output [ "The ultimate answer is \(answer).\n" ];
var int: answer;
constraint 1 <= answer /\ answer <= 100;
constraint answer mod 2 == 0;
constraint answer mod 3 == 0;
constraint answer mod 7 == 0;
solve satisfy;
output [ show(answer) ];
var 0..9: S;
var 0..9: E;
var 0..9: N;
var 0..9: D;
var 0..9: M;
var 0..9: O;
var 0..9: R;
var 0..9: Y;
predicate alldiff(array[int] of var int: bag) =
forall(i,j in 1..length(bag)) ( i == j <-> bag[i] == bag[j] );
constraint alldiff([S,E,N,D,M,O,R,Y]);
constraint 1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
== 10000*M + 1000*O + 100*N + 10*E + Y ;
constraint forall(alp in [S,M])( alp > 0 );
solve satisfy;
int: salmon = 5000;
int: rice = 2000;
var int: sashimi;
var int: sushi;
var int: revenue;
constraint sashimi >= 0;
constraint sushi >= 0;
constraint 120*sashimi + 15*sushi <= salmon;
constraint 10*sushi <= rice;
constraint 129*sashimi + 19*sushi == revenue;
solve maximize revenue;
n = 42;
capacity = 5555;
values = [ 900, 195, 626, 120, 470, 107, 593,
615, 317, 411, 371, 346, 205, 752,
790, 630, 466, 455, 434, 965, 952,
934, 579, 999, 567, 153, 791, 692,
981, 373, 717, 657, 993, 988, 381,
494, 339, 968, 658, 235, 966, 790 ];
weights = [ 253, 786, 467, 222, 532, 297, 687,
150, 136, 245, 934, 227, 452, 926,
487, 178, 601, 459, 363, 136, 380,
337, 657, 563, 280, 431, 244, 734,
646, 445, 682, 209, 323, 398, 800,
453, 529, 353, 844, 775, 792, 950 ];
int: n;
set of int: items = 1..n;
int: capacity;
array[items] of int: values;
array[items] of int: weights;
var set of items: knapsack;
constraint sum(i in items)(bool2int(i in knapsack)*weights[i]) <= capacity;
solve maximize sum(i in items)(bool2int(i in knapsack)*values[i]);
s = [|0,0,0,0,0,0,0,0,1|
0,0,0,0,0,0,0,2,3|
0,0,4,0,0,5,0,0,0|
0,0,0,1,0,0,0,0,0|
0,0,0,0,3,0,6,0,0|
0,0,7,0,0,0,5,8,0|
0,0,0,0,6,7,0,0,0|
0,1,0,0,0,4,0,0,0|
5,2,0,0,0,0,0,0,0|];
set of int: idxs = 1..9;
array[1..3] of set of int: subsq = [ 1..3, 4..6, 7..9 ];
array[idxs,idxs] of var 1..9: t;
array[idxs,idxs] of int: s;
constraint forall(i,j in idxs) ( s[i,j] != 0 -> t[i,j] == s[i,j] );
predicate alldiff(array[int] of var int: bag) =
forall(i,j in 1..length(bag)) ( i == j <-> bag[i] == bag[j] );
constraint forall(r in idxs) ( alldiff([t[r,c] | c in idxs]) );
constraint forall(c in idxs) ( alldiff([t[r,c] | r in idxs]) );
constraint forall(R,C in subsq) ( alldiff([t[r,c] | r in R, c in C]) );
solve satisfy;
output [
show_int(1, t[r,c]) ++ (if c == 9 then "\n" else " " endif)
| r in idxs, c in idxs
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment