Advertisement
JosepRivaille

LI - Tomography

Jun 23rd, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.45 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2. ejemplo(1, [0,0,8,2,6,4,5,3,7,0,0], [0,0,7,1,6,3,4,5,2,7,0,0] ).
  3. ejemplo(2, [10,4,8,5,6], [5,3,4,0,5,0,5,2,2,0,1,5,1] ).
  4. ejemplo(3, [11,5,4], [3,2,3,1,1,1,1,2,3,2,1] ).
  5.  
  6. doExample(E) :-
  7.     ejemplo(E, RowSums, ColSums),
  8.     length(RowSums, NumRows),
  9.     length(ColSums, NumCols),
  10.     NVars is NumRows * NumCols,
  11.     length(L, NVars), L ins 0..1,
  12.     matrixByRows(L, NumCols, MatrixByRows),
  13.     transpose(MatrixByRows, MatrixByColumns),
  14.     declareConstraints(RowSums, MatrixByRows),
  15.     declareConstraints(ColSums, MatrixByColumns),
  16.     label(L), pretty_print(RowSums, ColSums, MatrixByRows).
  17.  
  18. flatten([], []).
  19. flatten([X|L], F) :- flatten(X, F1), flatten(L, F2), append(F1, F2, F), !.
  20. flatten(X, [X]).
  21.  
  22. declareConstraints([], []) :- !.
  23. declareConstraints([RS|RowSums], [Row|MatrixByRows]) :-
  24.     sum(Row, #=, RS), declareConstraints(RowSums, MatrixByRows).
  25.  
  26. matrixByRows([], _, []) :- !.
  27. matrixByRows(L, NumCols, MatrixByRows) :-
  28.     length(Row, NumCols), append(Row, RestL, L),
  29.     matrixByRows(RestL, NumCols, MatrixByRowsP),
  30.     append([Row], MatrixByRowsP, MatrixByRows).
  31.  
  32. pretty_print(_,ColSums,_):- write('     '), member(S, ColSums), writef('%2r ',[S]), fail.
  33. pretty_print(RowSums,_,M):- nl,nth1(N,M,Row), nth1(N,RowSums,S), nl, writef('%3r   ',[S]), member(B,Row), wbit(B), fail.
  34. pretty_print(_,_,_) :- nl, nl.
  35. wbit(1):- write('*  '),!.
  36. wbit(0):- write('   '),!.
  37.  
  38. main :-
  39.     doExample(1),
  40.     doExample(2),
  41.     doExample(3),
  42.     halt.
  43.  
  44. % JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement