state representation of state space searching
DESCRIPTION
State Representation of State Space Searching. Alan Tam Siu Lung [email protected] 99967891 96397999. Prerequisite. State Space Search Pascal/C/C++ Familiar with the data types Mathematics. Generic Graph Searching Algorithm. c: container of states insert start to c while c not empty - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/2.jpg)
Prerequisite
• State Space Search• Pascal/C/C++
– Familiar with the data types
• Mathematics
![Page 3: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/3.jpg)
Generic GraphSearching 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
![Page 4: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/4.jpg)
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
![Page 5: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/5.jpg)
Missionaries and Cannibals
![Page 6: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/6.jpg)
8-puzzle
4 5
6 1 8
7 3 2
1 2 3
4 5 6
7 8
![Page 7: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/7.jpg)
States Storage Problem
• Need to store many states• Memory is limited• Runtime is limited• Compiler is stupid
• we need intelligence to represent states wisely
![Page 8: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/8.jpg)
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?
![Page 9: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/9.jpg)
8-puzzle
• struct state {– int num[9];– int space_pos;
• };
• record state– num : array[1..9] of
0..8– space_pos: 1..9;
• end;
4 5
6 1 8
7 3 2
1 2 3
4 5 6
7 8
![Page 10: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/10.jpg)
Entropy
• Possible Different states: 181440
•Space we used? 320 bits•Using bytes? 80 bits
![Page 11: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/11.jpg)
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;
![Page 12: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/12.jpg)
Entropy
• Possible Different states: 16•Space we used? 160 bits•Using bytes? 40 bits•One side only? 24 bits
![Page 13: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/13.jpg)
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
![Page 14: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/14.jpg)
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
![Page 15: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/15.jpg)
Integer Representation
46709394 (10)
= 00000010 11001000 10111010 10010010 (2)
= 0C C8 BA 92 (16) (0x0cc8ba92 in C/C++)
Little Endian:10010010 10111010 11001000 00000010
92 BA C8 02
![Page 16: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/16.jpg)
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)
![Page 17: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/17.jpg)
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
![Page 18: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/18.jpg)
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
![Page 19: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/19.jpg)
Bitwise Operations
Binary• << shl• >> shr• & and• | or• ^ xor
Unary• ~ not
• shift # bits left• shift # bits right• intersection• union• mutual exclusion
• complement
![Page 20: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/20.jpg)
Examples
1 << 2 4
7 >> 2
13 & 7
12 | 4
12 ^ 24
~55 (8-bit unsigned)
-7 >> 2 (8-bit)
![Page 21: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/21.jpg)
Examples
1 << 2 4
7 >> 2 1
13 & 7 5
12 | 4 12
12 ^ 24 20
~55 (8-bit unsigned) 200
-7 >> 2 (8-bit) -1
![Page 22: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/22.jpg)
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)
![Page 23: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/23.jpg)
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
![Page 24: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/24.jpg)
Using Bitmap
• Space-efficient• Runtime may be faster
– Cache hit– Less copying
• Or maybe slower– More calculations
![Page 25: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/25.jpg)
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)
![Page 26: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/26.jpg)
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
![Page 27: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/27.jpg)
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
![Page 28: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/28.jpg)
Answer
– 1110101101111001 becomes1000101001000001
• The leftmost of a bit group means:– It is 1– Its left is 0
• So:– value = value & ~(value >> 1)
![Page 29: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/29.jpg)
Bit Movements
• Question– 1 1 0 1 0 1 0 0 becomes0101000100010000
• 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 becomes1111001100110000
– 0 1 0 1 becomes0000111100001111
•
![Page 30: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/30.jpg)
Direct Addressing
0000 0000000000000000 1000 1111000000000000
0001 0000000000001111 1001 1111000000001111
0010 0000000011110000 1010 1111000011110000
0011 0000000011111111 1011 1111000011111111
0100 0000111100000000 1100 1111111100000000
0101 0000111100001111 1101 1111111100001111
0110 0000111111110000 1110 1111111111110000
0111 0000111111111111 1111 1111111111111111
![Page 31: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/31.jpg)
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?
![Page 32: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/32.jpg)
Other Uses
• Store multiple values, each one assigned a bit-range
• (value % 32) == (value & 31)• Storing sets of 64 elements
– Union– Difference– Is Subset
![Page 33: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/33.jpg)
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();
elseg();
![Page 34: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/34.jpg)
Lexicographical Ordering
• Consider the 8 puzzle• Store it verbatim: 89 = 387420489
• Hash table? How large?• Calculate a mapping from the
362880 possible permutations to 0..362879
![Page 35: State Representation of State Space Searching](https://reader036.vdocument.in/reader036/viewer/2022081420/5681384e550346895d9ffadf/html5/thumbnails/35.jpg)
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