% Example with matrices,based on: % % Three jugs problem in Minzinc modelled as a shortest path problem. % % Problem from Taha "Introduction to Operations Research", page 245 % % Model created by Hakan Kjellerstrand, hakank@bonetmail.com % See also my MiniZinc page: http://www.hakank.org/minizinc % % VSC: had to transpose the matrix, and change the constraints.... % :- style_check( all ). :- use_module(library(gecode/clpfd)). :- use_module(library(maplist)). :- use_module(library(lists)). main :- problem(Z, X, InFlow, OutFlow, N), out(Z, X, InFlow, OutFlow, N), fail. main. problem(Z, X, InFlow, OutFlow, N) :- N = 15, Start = 1, End = 15, M = 999, d( M, DD ), D <== array[1..N,1..N] of DD, % distance RHS <== array[1..N] of _, % requirements (right hand statement) X <== array[1..N, 1..N] of 0..1, % the resulting matrix, 1 if connected, 0 else OutFlow <== array[1..N] of 0..1, InFlow <== array[1..N] of 0..1, % objective to minimize Z in 0..M, Z #= sum( [I in 1..N, J in 1..N] where D[I,J] RHS[I] <== 1 ; I == End -> RHS[I] <== -1 ; RHS[I] <== 0 ) ), % must be larger than 0?? foreach( [I in 1..N, J in 1..N], ( D[J,I] = M -> X[J,I] #= 0 ; true ) ), % outflow constraint foreach(I in 1..N, OutFlow[I] #= sum(J in 1..N where D[J,I]