Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

cp works, other solvers fail

28 views
Skip to first unread message

jf

unread,
Oct 17, 2024, 4:42:30 PM10/17/24
to Picat
Hi,

I was trying to solve the
https://www.zeit.de/zeit-magazin/2022/08/logelei
problem. My not exactly original solution is below,
better look at https://github.com/bernschneider/zeitlogelei/blob/main/2022/zeitlogelei202208.pi

My solution works well with cp, but fails with sat
(and other solvers). The problematic line is marked
in my program below, producing strange error message:

*** error(invalid_constraint_exp,[_330*(1#>0)*(1#<_368),_3b0*(2#>0)*(2#<_368),_428*(3#>0)*(3#<_368),_4a0*(4#>0)*(4#<_368),_518*(5#>0)*(5#<_368),_590*(6#>0)*(6#<_368),_608*(7#>0)*(7#<_368),_680*(8#>0)*(8#<_368),_6f8*(9#>0)*(9#<_368),0*(10#>0)*(10#<_368)])

Thank you for the opportunity to work with Picat.
Josef

/*
logelei-2022-08.pi
https://www.zeit.de/zeit-magazin/2022/08/logelei

 2  1  0  5  4  0  0  3  0
 0  2  0  4  0  5  3  0  1
 4  5  2  0  0  3  0  1  0
 0  0  5  3  1  4  0  0  2
 0  0  3  1  0  2  0  4  5

*/

import cp, util.

main =>
   Rowsums = [ [3,9,3], [2,4,8,1], [11,3,1], [13,2], [4,2,9] ],
   Colsums = [ [2,4], [8], [10], [9,4], [4,1], [14], [3], [3,1,4], [1,7] ],
   Rownum = 5,
   Colnum = 9,
   Max = 5,

   Board = new_array(Rownum+1,Colnum+1),
   Bvars = Board.vars(),

   foreach ( Row in 1..Rownum+1 )
      Board[Row, Colnum+1] #= 0
   end,
   foreach ( Col in 1..Colnum+1 )
      Board[Rownum+1, Col] #= 0
   end,

   foreach ( Row in 1..Rownum )
      Rowcol = Board[Row].to_list(),
      Rowcol :: 0..Max,
      all_different_except_0(Rowcol),
      splitter(Rowcol, Rowsums[Row])
   end,

   Columns = Board.columns(),
   foreach ( Col in 1..Colnum )
      Rowcol = Columns[Col].to_list(),
      Rowcol :: 0..Max,
      all_different_except_0(Rowcol),
      splitter(Rowcol, Colsums[Col])
   end,

   println("Solving..."),
   solve(Bvars),
   println("Solved"),

   nl,vysl(Board, Rownum, Colnum).

splitter(Input, Sumslist) =>
   Groupsno = Sumslist.len(),
   % automaton(Input, Groupsno),   % not necessary
   Groups = new_list(Groupsno),
   N = Input.len(),
   Pivotsnum = Groupsno - 1,
   Pivotinds = new_list(Pivotsnum),
   Pivotinds :: 2..N-1,
   foreach ( I in 1..Pivotsnum-1 )
      Pivotinds[I+1] - Pivotinds[I] #> 1
   end,
   foreach ( I in 1..Pivotsnum )
      element(Pivotinds[I], Input, 0)
   end,
   Pivotindsext = [0] ++ Pivotinds ++ [N],
   foreach ( Ind in 1..Pivotindsext.len()-1 )
      Ileft = Pivotindsext[Ind],
      Iright = Pivotindsext[Ind+1],
      % The problem is here
      Groups[Ind] #= [ Input[I] * ( I #> Ileft ) * ( I #< Iright ) : I in 1..N ],
      sum(Groups[Ind]) #= Sumslist[Ind]
   end.

vysl(A, Rownum, Colnum) =>
   foreach ( Row in 1..Rownum )
      foreach ( Col in 1..Colnum )
         printf(" %d ", A[Row,Col])
      end,
      nl
   end,
   nl.


Neng-Fa Zhou

unread,
Oct 20, 2024, 11:52:51 AM10/20/24
to Picat
Hi,

It's surprising that cp works on this program. The issue is here:

Groups[Ind] #= [Input[I] * ( I #> Ileft ) * ( I #< Iright ) : I in 1..N ],

It should be changed to 

Groups[Ind] = $[Input[I] * ( I #> Ileft ) * ( I #< Iright ) : I in 1..N ],

because it is a unification of two terms, not a constraint. With this change, the program works with other solver modules.

Cheers,
NF 
Reply all
Reply to author
Forward
0 new messages