/*********************************************************** magic_square.pi from Constraint Solving and Planning with Picat, Springer by Neng-Fa Zhou, Hakan Kjellerstrand, and Jonathan Fruhman ***********************************************************/ import cp. main => selection(VarSels), choice(ValSels), Timeout = 10000, % Timeout in milliseconds N = 7, % size of problem foreach (VarSel in VarSels, ValSel in ValSels) time2(time_out(magic(N,_Square,VarSel,ValSel), Timeout,Status)), println([VarSel,ValSel,Status]) end. magic(N,Square,VarSel,ValSel) => NN = N*N, Sum = N*(NN+1)//2,% magical sum Square = new_array(N,N), Square :: 1..NN, all_different(Square.vars()), foreach (I in 1..N) Sum #= sum([Square[I,J] : J in 1..N]), % rows Sum #= sum([Square[J,I] : J in 1..N]) % columns end, % diagonal sums Sum #= sum([Square[I,I] : I in 1..N]), Sum #= sum([Square[I,N-I+1] : I in 1..N]), solve([VarSel,ValSel], Square). % Variable selection selection(VarSels) => VarSels = [backward,constr,degree,ff,ffc,ffd, forward,inout,leftmost,max,min,up]. % Value selection choice(ValSels) => ValSels = [down,reverse_split,split,up,updown].