model 'LEAPER8O'
uses 'mmxprs', 'mmsystem'
! fiveleaper open tour on 8-board
parameters
s = 8 ! number of squares per side
end-parameters
declarations
S = 1..s ! range of squares per side
n = s*s ! number of squares
N = 1..n ! range of squares per board
A: array(N,N) of real ! 1 for legal move, otherwise 0
end-declarations
forall(i,j,k,l in S) do
if( (i-k)^2+(j-l)^2 = 25) then ! if legal move
A( j+s*(i-1), l+s*(k-1) ):=1 ! set value to 1
end-if
end-do
declarations
x: array(N,N) of mpvar
z: array(N) of mpvar
end-declarations
! any objective
any:= x(1,1)
! each cell is preceded by no more than one other
forall(j in N)
csl(j):= sum(i in N) x(i,j) <= 1
! each square precedes no more than one other
forall(i in N)
csr(i):= sum(j in N) x(i,j) <= 1
! legal moves only
forall(i in N,j in N)
lm(i,j):= x(i,j) <= A(i,j)
! total moves required
tm:= sum(i in N,j in N) x(i,j) = n-1
! exclude tourlets
forall(i in N,j in N | i <> j)
et(i,j):= z(i)-z(j)+n*x(i,j) <= n-1
forall(i in N,j in N)
x(i,j) is_binary
! solve the problem
starttime:=gettime
minimise( any )
writeln("Total time: ", gettime-starttime, " sec")
! produce results table
forall(i,j in S)
R(i,j):=getsol(z(j+(i-1)*s))+1
forall(i in S) do
forall(j in S) do
write(R(i,j),' ')
end-do
writeln
end-do
end-model
|