Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2.  
  3. solve(N) :-
  4. latin_square(N,Square),
  5. maplist(writeln,Square).
  6.  
  7. latin_square(N,Square) :-
  8. length(Square,N), % Square = [[X11,...,X1N],...,[XN1,...,XNN]]
  9. maplist(same_length(Square),Square), %
  10. append(Square,AllVars),
  11. AllVars ins 1..N,
  12. maplist(all_different,Square), % All different in a row.
  13. transpose(Square,TSquare), % All different in a column.
  14. maplist(all_different,TSquare), %
  15. labeling([ff],AllVars).
  16.  
  17.  
  18. diagonal_to_lst([[H]],[H]).
  19. diagonal_to_lst([[H|_]|T],[H|L]) :-
  20. maplist(tail,T,T1),
  21. diagonal_to_lst(T1,L).
  22.  
  23. tail([_|T],T).
  24.  
  25.  
  26. :- use_module(library(clpfd)).
  27. solve(N) :-
  28. latin_square(N,Square),
  29. maplist(writeln,Square).
  30.  
  31. latin_square(N,Square) :-
  32. length(Square,N), % Square = [[X11,...,X1N],...,[XN1,...,XNN]]
  33. maplist(same_length(Square),Square), %
  34. append(Square,AllVars),
  35. AllVars ins 1..N,
  36. maplist(all_different,Square), % All different in a row.
  37. transpose(Square,TSquare), % All different in a column.
  38. maplist(all_different,TSquare), %
  39. labeling([ff],AllVars).
  40.  
  41. :- use_module(library(clpfd)).
  42.  
  43. mrok(Sol) :-
  44. Vars = [M,R,O,K,C,I,E,N,S1,C1],
  45. Sol = sol(m:M,r:R,o:O,k:K,c:C,i:I,e:E,n:N,s1:S1,c1:C1),
  46. Vars ins 0..9,
  47. all_different(Vars),
  48. M #\= 0, C #\= 0,
  49. (M*1000 + R*100 + O*10 + K) *
  50. (M*1000 + R*100 + O*10 + K) #=
  51. C*10000000 + I*1000000 + E*100000 + M*10000 + N*1000 + O*100 + S1*10 + C1,
  52. labeling([ff],Vars).
  53.  
  54. :- use_module(library(clpfd)).
  55.  
  56. %zadanie z książki ze stronki meissnera 152 strona
  57. visit(sol(fs:FS, hs:HS, j:J, m:M, t:T)) :-
  58. Vars = [FS, HS, J, M, T],
  59. Vars ins 0..1,
  60. HS #<== FS,
  61. M #\/ J,
  62. FS #\ T,
  63. T #<==> J,
  64. M #==> (J #/\ HS),
  65. labeling([ff],Vars).
  66.  
  67.  
  68. :- use_module(library(clpfd)).
  69. equations2(sol(a:A, b:B, c:C, d:D, e:E, f:F, g:G)) :-
  70. Vars = [A, B, C, D, E, F, G],
  71. Vars ins 1..10,
  72.  
  73. all_different(Vars),
  74. B + G #= D,
  75. B + C #= A,
  76. C + E + G #= F,
  77. D #< A #==> C #= 2,
  78. D #>= A #==> E #= 2,
  79. sum(Vars,#=,Sum),
  80. labeling([min(Sum),ff],Vars).
  81.  
  82. %dwuwymiarowa dystrybucja
  83. equations21(sol(a:A, b:B, c:C, d:D, e:E, f:F, g:G)) :-
  84. Vars = [A, B, C, D, E, F, G],
  85. Vars ins 1..10,
  86. all_different(Vars),
  87. B + G #= D,
  88. B + C #= A,
  89. C + E + G #= F,
  90. D #< A #==> C #= 2,
  91. D #>= A #==> E #= 2,
  92. Sum in 7..70,
  93. labeling([ff],[Sum]),
  94. sum(Vars,#=,Sum),
  95. labeling([ff],Vars).
  96. %dwuwymiarowa dystrybucja
  97.  
  98. %
  99. % Let V(G) be a set of vertices of the graph G, let N(v)
  100. % be a set of all neighbours of the vertex v in V, and
  101. % let a coloring be any assignment of the form V -> Colors,
  102. % where Colors = {1,2,3,...}. Find a coloring, which uses
  103. % a minimal number of colors, such that all sums of colors
  104. % of N(v) for every vertex v are pairvise distinct.
  105. % coloring([[2,3,4,5],[1,3],[1,2,4],[1,3,5],[1,4]],L).
  106. %
  107.  
  108. :- use_module(library(clpfd)).
  109.  
  110. coloring2(Graph,Coloring) :-
  111. length(Graph,N),
  112. length(Coloring,N),
  113. length(Tints,N),
  114. Cn in 1..N,
  115. labeling([ff], [Cn]),
  116. Coloring ins 1..Cn,
  117. constr(Graph,Coloring,Tints),
  118. all_different(Tints),
  119. labeling([ff], Tints).
  120.  
  121. constr(Graph,Coloring,Tints) :-
  122. maplist(vert_sum(Coloring),Graph,Tints).
  123.  
  124. vert_sum(VertexColoring,VertNeighInds, VertSum) :-
  125. maplist(ind_to_vert(VertexColoring),VertNeighInds,VertNeighs),
  126. sum(VertNeighs, #=, VertSum).
  127.  
  128. ind_to_vert(VertexColoring,VertI,Vert) :-
  129. nth1(VertI,VertexColoring,Vert).
  130.  
  131. :- use_module(library(clpfd)).
  132. fraction(sol(a:A, b:B, c:C, d:D, e:E, f:F, g:G, h:H, i:I), Test1, Test2) :-
  133. statistics(inferences, Test1),
  134. Vars = [A, B, C, D, E, F, G, H, I],
  135. all_different(Vars),
  136. Vars ins 1..9,
  137. DD = [BC, EF, HI],
  138. DD ins 1..81,
  139. BC #= 10*B + C,
  140. EF #= 10*E + F,
  141. HI #= 10*H + I,
  142. A*EF*HI + D*BC*HI + G*BC*EF #= BC*EF*HI,
  143. A*EF #>= D*BC,
  144. D*HI #>= G*EF,
  145.  
  146. 3*A #>= BC,
  147. 3*G #=< HI,
  148. labeling([ff, bisect], Vars),
  149. statistics(inferences, Test2).
  150. %oz version 3.2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement