Title: State Representation of State Space Searching
1State RepresentationofState Space Searching
- Alan Tam Siu Lung
- Tam_at_SiuLung.com
- 99967891
- 96397999
2Prerequisite
- State Space Search
- Pascal/C/C
- Familiar with the data types
- Mathematics
3Generic 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
4Generic 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
5Missionaries and Cannibals
68-puzzle
4 5
6 1 8
7 3 2
? ? ?
1 2 3
4 5 6
7 8
7States Storage Problem
- Need to store many states
- Memory is limited
- Runtime is limited
- Compiler is stupid
- we need intelligence to represent states wisely
8Operation 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?
98-puzzle
- struct state
- int num9
- int space_pos
-
- record state
- num array1..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
10Entropy
- Possible Different states 181440
- Space we used? 320 bits
- Using bytes? 80 bits
11Missionaries and Cannibals
- struct state
- int m2, c2
- bool b
-
- record state
- m, c array1..2 of 1..3
- b boolean
- end
-
12Entropy
- Possible Different states 16
- Space we used? 160 bits
- Using bytes? 40 bits
- One side only? 24 bits
13Be realistic
- 3, 2, 1, 02 ? L, R 32
- Store it as M C 4 B 16
- 3M3C, 3M2C, 3M1C, 3M, 2M2C, 1M1C, 3C, 2C, 1C,
? ? L, R 20
14Computer Organization
- x86 stores whole numbers in
- Binary Form
- x86 stores negative integers in
- 2s complement
- x86 stores real numbers in
- IEEE 754
- x86 stores alphabets in
- ASCII
15Integer 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
16State Representation
- M C 4 B 16
- M3, C3, B0
- 00001111 (15)
- M0, C0, B1
- 00010000 (16)
- M2, C2, B1
- 00011010 (26)
17If you discovered
- M3, C3, B0
- 00001111 (15)
- M0, C0, B1
- 00010000 (16)
- M2, C2, B1
- 00011010 (26)
- This is Bitwise Representation
18Why 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
19Bitwise Operations
- Binary
- ltlt shl
- gtgt shr
- and
- or
- xor
- Unary
- not
- shift bits left
- shift bits right
- intersection
- union
- mutual exclusion
- complement
20Examples
1 ltlt 2 4
7 gtgt 2
13 7
12 4
12 24
55 (8-bit unsigned)
-7 gtgt 2 (8-bit)
21Examples
1 ltlt 2 4
7 gtgt 2 1
13 7 5
12 4 12
12 24 20
55 (8-bit unsigned) 200
-7 gtgt 2 (8-bit) -1
22Bitmap
- 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)
23Using a 32-bit Integer
- 00000010 11001000 10111010 10010010
- 1 ltlt p a number with only bit p on
- value (1 ltlt p) ! 0 ? Get(p)
- value value (1 ltlt p) ? Set(p)
- value value (1 ltlt p) ? Unset(p)
- value value (1 ltlt p) ? Toggle(p)
Bit 31
Bit 0
24Using Bitmap
- Space-efficient
- Runtime may be faster
- Cache hit
- Less copying
- Or maybe slower
- More calculations
25Massive Calculations
- Example Count number of bits on a 16-bit integer
v - v (v 0x5555) ((v gtgt 1) 0x5555)
- v (v 0x3333) ((v gtgt 2) 0x3333)
- v (v 0x0f0f) ((v gtgt 4) 0x0f0f)
- v (v 0x00ff) ((v gtgt 8) 0x00ff)
26Counting 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
27Massive Calculations
- Given a bit pattern, find a bit pattern which
- only the leftmost bit of a bit group is on
- E.g.
- 1110101101111001 becomes1000101001000001
28Answer
- 1110101101111001 becomes1000101001000001
- The leftmost of a bit group means
- It is 1
- Its left is 0
- So
- value value (value gtgt 1)
29Bit Movements
- Question
- 1 1 0 1 0 1 0 0 becomes0101000100010000
- Solution
- value ((value 0x00f0) ltlt 4)) (value
0x000f) - value ((value 0x0c0c) ltlt 2)) (value
0x0303) - value ((value 0x2222) ltlt 1)) (value
0x1111) - Exercise
- 1 1 0 1 0 1 0 0 becomes1111001100110000
- 0 1 0 1 becomes0000111100001111
-
30Direct 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
31Complex 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?
32Other Uses
- Store multiple values, each one assigned a
bit-range - (value 32) (value 31)
- Storing sets of ? 64 elements
- Union
- Difference
- Is Subset
33Multiple Comparisons
- switch (i)
- case 1
- case 3
- case 4
- case 7
- case 8
- case 10
- f()
- break
- default
- g()
-
-
- if ((1ltlti) 0x039a)
- f()
- else
- g()
34Lexicographical 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
35Example
- What are smaller than 530841672?
- 53084160-6? 1 ? 1
- 5308410-5?? 1 ? 2
- 530840-0??? 0 ? 6
- 53080-3???? 2 ? 24
- 5300-7????? 5 ? 120
- 53?????? 0 ? 720
- 50-2??????? 3 ? 5040
- 0-4???????? 5 ? 40320