| 1 | #include <math.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
| 4 | #include "bdd.h" |
| 5 | |
| 6 | float dummyStateNum; // Use to remove the number of states defined by the |
| 7 | // next-state variables |
| 8 | |
| 9 | #define SIZE 33 // Size of the board |
| 10 | #define CENTER 16 // Location of the center place |
| 11 | |
| 12 | bdd boardC[SIZE]; // Current state variables |
| 13 | bdd boardN[SIZE]; // Next state variables |
| 14 | bdd I; // Initial state |
| 15 | bdd T; // Transition relation |
| 16 | |
| 17 | bdd currentvar; // All current state variables |
| 18 | bddPair *pair; // Renaming pair |
| 19 | |
| 20 | |
| 21 | // All the possible moves. Note that the numbering starts from '1' |
| 22 | int moves[][3] = |
| 23 | { {1,4,9}, {1,2,3}, |
| 24 | {2,5,10}, |
| 25 | {3,2,1}, {3,6,11}, |
| 26 | {4,5,6}, {4,9,16}, |
| 27 | {5,10,17}, |
| 28 | {6,5,4}, {6,11,18}, |
| 29 | {7,8,9}, {7,14,21}, |
| 30 | {8,9,10}, {8,15,22}, |
| 31 | {9,8,7}, {9,10,11}, {9,4,1}, {9,16,23}, |
| 32 | {10,9,8}, {10,11,12}, {10,5,2}, {10,17,24}, |
| 33 | {11,10,9}, {11,12,13}, {11,6,3}, {11,18,25}, |
| 34 | {12,11,10}, {12,19,26}, |
| 35 | {13,12,11}, {13,20,27}, |
| 36 | {14,15,16}, |
| 37 | {15,16,17}, |
| 38 | {16,15,14}, {16,17,18}, {16,9,4}, {16,23,28}, |
| 39 | {17,16,15}, {17,18,19}, {17,10,5}, {17,24,29}, |
| 40 | {18,17,16}, {18,19,20}, {18,11,6}, {18,25,30}, |
| 41 | {19,18,17}, |
| 42 | {20,19,18}, |
| 43 | {21,22,23}, {21,14,7}, |
| 44 | {22,23,24}, {22,15,8}, |
| 45 | {23,22,21}, {23,24,25}, {23,16,9}, {23,28,31}, |
| 46 | {24,23,22}, {24,25,26}, {24,17,10}, {24,29,32}, |
| 47 | {25,24,23}, {25,26,27}, {25,18,11}, {25,30,33}, |
| 48 | {26,25,24}, {26,19,12}, |
| 49 | {27,26,25}, {27,20,13}, |
| 50 | {28,29,30}, {28,23,16}, |
| 51 | {29,24,17}, |
| 52 | {30,29,28}, {30,25,18}, |
| 53 | {31,32,33}, {31,28,23}, |
| 54 | {32,29,24}, |
| 55 | {33,32,31}, {33,30,25}, |
| 56 | {0,0,0} |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | // Setup the variables needed for the board |
| 64 | void make_board(void) |
| 65 | { |
| 66 | bdd_setvarnum(SIZE*2); |
| 67 | |
| 68 | for (int n=0 ; n<SIZE ; n++) |
| 69 | { |
| 70 | boardC[n] = bdd_ithvar(n*2); |
| 71 | boardN[n] = bdd_ithvar(n*2+1); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // Make the initial state predicate |
| 77 | void make_initial_state(void) |
| 78 | { |
| 79 | I = bddtrue; |
| 80 | |
| 81 | for (int n=0 ; n<SIZE ; n++) |
| 82 | if (n == CENTER) |
| 83 | I &= !boardC[n]; |
| 84 | else |
| 85 | I &= boardC[n]; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // Make sure all other places does nothing when |
| 90 | // there's a move from 'src' to 'dst' over 'tmp' |
| 91 | bdd all_other_idle(int src, int tmp, int dst) |
| 92 | { |
| 93 | bdd idle = bddtrue; |
| 94 | |
| 95 | for (int n=0 ; n<SIZE ; n++) |
| 96 | { |
| 97 | if (n != src && n != tmp && n != dst) |
| 98 | idle &= bdd_biimp(boardC[n], boardN[n]); |
| 99 | } |
| 100 | |
| 101 | return idle; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // Encode one move from 'src' to 'dst' over 'tmp' |
| 106 | bdd make_move(int src, int tmp, int dst) |
| 107 | { |
| 108 | bdd move = boardC[src] & boardC[tmp] & !boardC[dst] & |
| 109 | !boardN[src] & !boardN[tmp] & boardN[dst]; |
| 110 | |
| 111 | move &= all_other_idle(src, tmp, dst); |
| 112 | |
| 113 | return move; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void make_transition_relation(void) |
| 118 | { |
| 119 | using namespace std ; |
| 120 | T = bddfalse; |
| 121 | |
| 122 | for (int n=0 ; moves[n][0]!=moves[n][1] ; n++) |
| 123 | T |= make_move(moves[n][0]-1, moves[n][1]-1, moves[n][2]-1); |
| 124 | |
| 125 | cout << "Transition relation: " << bdd_nodecount(T) << " nodes, " |
| 126 | << bdd_satcount(T)/dummyStateNum << " states\n"; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // Make renaming pair and current state variables |
| 131 | void make_itedata(void) |
| 132 | { |
| 133 | pair = bdd_newpair(); |
| 134 | for (int n=0 ; n<SIZE ; n++) |
| 135 | bdd_setpair(pair, n*2+1, n*2); |
| 136 | |
| 137 | currentvar = bddtrue; |
| 138 | for (int n=0 ; n<SIZE ; n++) |
| 139 | currentvar &= boardC[n]; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | // Do the forward iteration |
| 144 | void iterate(void) |
| 145 | { |
| 146 | bdd tmp; |
| 147 | bdd reachable = I; |
| 148 | int cou = 1; |
| 149 | |
| 150 | make_itedata(); |
| 151 | |
| 152 | do |
| 153 | { |
| 154 | tmp = reachable; |
| 155 | bdd next = bdd_appex(reachable, T, bddop_and, currentvar); |
| 156 | next = bdd_replace(next, pair); |
| 157 | reachable |= next; |
| 158 | |
| 159 | std::cout << cou << ": " << bdd_nodecount(reachable) |
| 160 | << " nodes, " << bdd_satcount(reachable)/dummyStateNum |
| 161 | << " states\n" << std::endl ; |
| 162 | cou++; |
| 163 | } |
| 164 | while (tmp != reachable); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void iterate_front(void) |
| 169 | { |
| 170 | bdd tmp; |
| 171 | bdd reachable = I; |
| 172 | bdd front = reachable; |
| 173 | int cou = 1; |
| 174 | |
| 175 | make_itedata(); |
| 176 | |
| 177 | do |
| 178 | { |
| 179 | tmp = reachable; |
| 180 | bdd next = bdd_appex(front, T, bddop_and, currentvar); |
| 181 | next = bdd_replace(next, pair); |
| 182 | front = next - reachable; |
| 183 | reachable |= front; |
| 184 | |
| 185 | std::cout << cou << ": " << bdd_nodecount(reachable) |
| 186 | << " , " << bdd_satcount(reachable)/dummyStateNum << std::endl; |
| 187 | std::cout << cou << ": " << bdd_nodecount(front) |
| 188 | << " , " << bdd_satcount(front)/dummyStateNum << std::endl; |
| 189 | cou++; |
| 190 | } |
| 191 | while (tmp != reachable); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void setup(void) |
| 196 | { |
| 197 | bdd_init(100000,1000); |
| 198 | bdd_setcacheratio(64); |
| 199 | bdd_setmaxincrease(500000); |
| 200 | |
| 201 | dummyStateNum = pow(2.0, SIZE); |
| 202 | |
| 203 | make_board(); |
| 204 | make_transition_relation(); |
| 205 | make_initial_state(); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | int main(void) |
| 210 | { |
| 211 | setup(); |
| 212 | iterate(); |
| 213 | |
| 214 | system("ps aux | grep \"./solitare\" | grep -v \"grep\""); |
| 215 | } |
| 216 |