Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. //
  2. //Gecode-demo - list with distinct elements (i.e. no two elements are the same)
  3. //
  4. #include <iostream>
  5. #include <fstream>
  6. #include <gecode/int.hh>
  7. #include <gecode/minimodel.hh>
  8. #include <gecode/search.hh>
  9.  
  10. using namespace Gecode;
  11.  
  12. class DistinctList : public Space {
  13. protected:
  14. IntVarArray l, puzzle, newPuzzle;
  15.  
  16. public:
  17.  
  18. DistinctList(void) : l(*this, 81, 1, 9) {
  19.  
  20. // Three possible solutions to this problem.
  21.  
  22. // 1. using the core gecode language
  23.  
  24.  
  25. std::ifstream innfil("sudoku1.DTA");
  26. int n[81];
  27. if (innfil) {
  28. while (!innfil.eof()) {
  29.  
  30. for (int a = 0; a < 81; a++) {
  31. innfil >> n[a];
  32. if (n[a] != 0)
  33. rel(*this, l[a] == n[a]); //assign value to arrayspace l[]
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. // 2. mini-model language
  43.  
  44.  
  45. int boks = 0;
  46.  
  47. for (int b = 0; b < 9; b++) {
  48. if (b % 3 == 0 && b) boks += 18;
  49.  
  50. int hopp = 0;
  51.  
  52. for (int i = 0; i < 9; i++) {
  53.  
  54. if (i % 3 == 0 && i) hopp += 6;
  55. //cout << "Fra: " << i + hopp + b * 3 + boks << "\n";
  56.  
  57. int avst = 0;
  58.  
  59. for (int j = i + 1; j < 9; j++) {
  60.  
  61. if (j % 3 == 0 && j) avst += 6;
  62. //cout << "\tj: " << j + avst + hopp + b * 3 + boks << endl;
  63.  
  64. rel(*this, l[i + hopp + b * 3 + boks] != l[j + avst + hopp + b * 3 + boks]);
  65.  
  66. }
  67. }
  68.  
  69. }
  70. // for rows and columns
  71. for (int i = 0; i < 9; i++)
  72. {
  73. for (int j = i + 1; j < 9; j++)
  74. {
  75. // rows
  76. for (int k = 0; k < 81; k += 9)
  77. {
  78. rel(*this, l[i + k] != l[j + k]);
  79. }
  80. // columns
  81. for (int m = 0; m < 9; m++) {
  82. rel(*this, l[9 * i + m] != l[9 * j + m]);
  83. }
  84. }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. // 3. built in function
  92.  
  93. // distinct(*this, l);
  94.  
  95. branch(*this, l, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
  96. }
  97.  
  98.  
  99. DistinctList(DistinctList& s) : Space(s) {
  100. l.update(*this, s.l);
  101. }
  102.  
  103. Space* copy() {
  104. return new DistinctList(*this);
  105. }
  106. /* jalla som faen print til screen */
  107. void print(void) const {
  108. //std::cout << l << std::endl;
  109.  
  110. int r = 1;
  111. for (int i = 0; i < 81; i++)
  112. {
  113. std::cout << l[i];
  114. if (r % 3 == 0)
  115. {
  116. std::cout << " ";
  117. }
  118. if (r % 9 == 0)
  119. {
  120. std::cout << '\n';
  121. }
  122. if (r == 27 || r == 54)
  123. {
  124. std::cout << '\n';
  125. }
  126. r++;
  127. if (r == 82)
  128. {
  129. std::cout << '\n\n';
  130. std::cout << "-------------------------------------------------------------------\n";
  131.  
  132. }
  133.  
  134. }
  135.  
  136. }
  137. void oppgave2b(int count)
  138. {
  139. puzzle = l;
  140. bool finished = false;
  141.  
  142. while (!finished) {
  143. newPuzzle = puzzle;
  144. rel(*this, newPuzzle[rand()%81+1] == 0);
  145.  
  146. if (count > 1) finished = true;
  147. else
  148. puzzle = newPuzzle;
  149. }
  150. }
  151.  
  152. };
  153.  
  154.  
  155.  
  156. int nomain(int argc, char* argv[])
  157. {
  158. DistinctList* m = new DistinctList;
  159. std::cout << "The contents of the space before search begins" << std::endl;
  160. m->print();
  161.  
  162. // We initialise and get ready for search - search has not yet begun
  163. // DFS = depth first search
  164. DFS<DistinctList> e(m);
  165. delete m;
  166.  
  167. // We loop through all solutions to the constraint problem
  168. int count = 0;
  169. while (DistinctList* s = e.next()) {
  170. s->print(); delete s;
  171. count++;
  172. }
  173. std::cout << count;
  174.  
  175. m->oppgave2b(count);
  176.  
  177. std::cout << "Number of elements: " << count << std::endl;
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement