Presentation is loading. Please wait.

Presentation is loading. Please wait.

State Representation of State Space Searching Alan Tam Siu Lung 99967891 96397999.

Similar presentations


Presentation on theme: "State Representation of State Space Searching Alan Tam Siu Lung 99967891 96397999."— Presentation transcript:

1 State Representation of State Space Searching Alan Tam Siu Lung Tam@SiuLung.com 99967891 96397999

2 Prerequisite State Space Search Pascal/C/C++ –Familiar with the data types Mathematics

3 Generic Graph Searching Algorithm c: container of states insert start to c while c not empty –pop an element from c (with minimum total cost) –if found goal, return –add/update some elements in c

4 Generic Graph Searching Best first search: choose the node with minimum estimated total path cost = current path cost + estimated cost to goal –Uniform cost search: estimated cost to goal = 0 Breadth first search: current path cost = depth of the node in an un-weighted graph Depth first search: estimated total path cost = inverse of current path cost

5 Missionaries and Cannibals

6 8-puzzle 45 618 732  123 456 78

7 States Storage Problem Need to store many states Memory is limited Runtime is limited Compiler is stupid we need intelligence to represent states wisely

8 Operation on States 3 operations: –Is goal? –Are these 2 states equal? –Find all neighbors. Questions –Can some operation be slow? –Can we pre-compute? (i.e. store redundant info) –Can states be not uniquely represented?

9 8-puzzle struct state { –int num[9]; –int space_pos; }; record state –num : array[1..9] of 0..8 –space_pos: 1..9; end; 45 618 732  123 456 78

10 Entropy Possible Different states: 181440 Space we used? 320 bits Using bytes? 80 bits

11 Missionaries and Cannibals struct state { –int m[2], c[2]; –bool b; }; record state –m, c : array[1..2] of 1..3; –b : boolean end;

12 Entropy Possible Different states: 16 Space we used? 160 bits Using bytes? 40 bits One side only? 24 bits

13 Be realistic |{3, 2, 1, 0}| 2  |{L, R}| = 32 –Store it as M + C * 4 + B * 16 |{3M3C, 3M2C, 3M1C, 3M, 2M2C, 1M1C, 3C, 2C, 1C, }|  |{L, R}| = 20

14 Computer Organization x86 stores whole numbers in: –Binary Form x86 stores negative integers in: –2’s complement x86 stores real numbers in: –IEEE 754 x86 stores alphabets in: –ASCII

15 Integer Representation 46709394 (10) = 00000010 11001000 10111010 10010010 (2) = 0C C8 BA 92 (16) (0x0cc8ba92 in C/C++) Little Endian: 10010010101110101100100000000010 92BAC802

16 State Representation M + C * 4 + B * 16 M=3, C=3, B=0 –00001111 (=15) M=0, C=0, B=1 –00010000 (=16) M=2, C=2, B=1 –00011010 (=26)

17 If you discovered… M=3, C=3, B=0 –00001111 (=15) M=0, C=0, B=1 –00010000 (=16) M=2, C=2, B=1 –00011010 (=26) This is Bitwise Representation

18 Why bitwise? It is the native language of the computer It works extremely fast –Integer Multiplication: 4 cycles latency –Moving Bits: 1 cycle latency Packing and unpacking is easier to code and thus less error prone

19 Bitwise Operations Binary << shl >> shr &and |or ^xor Unary ~not shift # bits left shift # bits right intersection union mutual exclusion complement

20 Examples 1 << 24 7 >> 2 13 & 7 12 | 4 12 ^ 24 ~55 (8-bit unsigned) -7 >> 2 (8-bit)

21 Examples 1 << 24 7 >> 21 13 & 75 12 | 412 12 ^ 2420 ~55 (8-bit unsigned)200 -7 >> 2 (8-bit)

22 Bitmap Shift operators and bitwise operators can be used to manage a sequence of bits of  64 elements Operations –Get(p : 0..Size-1) : Boolean –Set(p : 0..Size-1) –Unset(p : 0..Size-1) –Toggle(p : 0..Size-1)

23 Using a 32-bit Integer 00000010 11001000 10111010 10010010 1 << p = a number with only bit p on value & (1 << p) != 0  Get(p) value = value | (1 << p)  Set(p) value = value & ~(1 << p)  Unset(p) value = value ^ (1 << p)  Toggle(p) Bit 31 Bit 0

24 Using Bitmap Space-efficient Runtime may be faster –Cache hit –Less copying Or maybe slower –More calculations

25 Massive Calculations Example: Count number of bits on a 16-bit integer v v = (v & 0x5555) + ((v >> 1) & 0x5555) v = (v & 0x3333) + ((v >> 2) & 0x3333) v = (v & 0x0f0f) + ((v >> 4) & 0x0f0f) v = (v & 0x00ff) + ((v >> 8) & 0x00ff)

26 Counting #bits 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 0 01 01 10 10 00 01 00 01 0010 0100 0001 0001 00000110 00000010 0000000000001000 Exercise: Do it for 32-bit

27 Massive Calculations Given a bit pattern, find a bit pattern which: –only the leftmost bit of a bit group is on E.g. –1110101101111001 becomes 1000101001000001

28 Answer –1110101101111001 becomes 1000101001000001 The leftmost of a bit group means: –It is 1 –Its left is 0 So: –value = value & ~(value >> 1)

29 Bit Movements Question – 1 1 0 1 0 1 0 0 becomes 0101000100010000 Solution –value = ((value & 0x00f0) << 4)) | (value & 0x000f) –value = ((value & 0x0c0c) << 2)) | (value & 0x0303) –value = ((value & 0x2222) << 1)) | (value & 0x1111) Exercise – 1 1 0 1 0 1 0 0 becomes 1111001100110000 – 0 1 0 1 becomes 0000111100001111

30 Direct Addressing 0000000000000000000010001111000000000000 0001000000000000111110011111000000001111 0010000000001111000010101111000011110000 0011000000001111111110111111000011111111 0100000011110000000011001111111100000000 0101000011110000111111011111111100001111 0110000011111111000011101111111111110000 0111000011111111111111111111111111111111

31 Complex Bit Patterns Othello –11101011 (where occupied) –10001001 (color) –You are 1, where you can play? Solution –11101011 & ~10001001 = 01100010 –11101011 + 01100010 = 01001101 –01001101 & ~11101011 = 00000100 How about the reverse direction?

32 Other Uses Store multiple values, each one assigned a bit-range (value % 32) == (value & 31) Storing sets of  64 elements –Union –Difference –Is Subset

33 Multiple Comparisons switch (i) { case 1: case 3: case 4: case 7: case 8: case 10: f(); break; default: g(); } if ((1<<i) & 0x039a) f(); else g();

34 Lexicographical Ordering Consider the 8 puzzle Store it verbatim: 8 9 = 387420489 Hash table? How large? Calculate a mapping from the 362880 possible permutations to 0..362879

35 Example What are smaller than 530841672? –5308416[0-6]?:1  1 –530841[0-5]??:1  2 –53084[0-0]???:0  6 –5308[0-3]????:2  24 –530[0-7]?????:5  120 –53[^]??????: 0  720 –5[0-2]???????:3  5040 –[0-4]????????:5  40320


Download ppt "State Representation of State Space Searching Alan Tam Siu Lung 99967891 96397999."

Similar presentations


Ads by Google