Pattern Databases Storing all permutations

user3667111 :

I am looking for some advice on storing all possible permutations for the fringe pattern database.

So the fifteen tile problem has 16! possible permutations, however storing the values for fringe so the 0 (blank tile),3,7,11,12,13,14,15 is 16!/(16-8)! = 518,918,400 permutations.

I am looking to store all of these permutations in a datastructure along with the value of the heuristic function (which is just incremented each time a iteration of the breadth first search), so far I am doing so but very slowly and took me 5 minutes to store 60,000 which is time I don't have!

Fringe Tiles

At the moment I have a structure which looks like this.

Value Pos0 Pos3 Pos7 Pos11 Pos12 Pos13 Pos14 Pos15

Where I store the position of the given numbers. I have to use these positions as the ID for when I am calculating the heuristic value I can quickly trawl through to the given composition and retrieve the value.

I am pretty unsure about this. The state of the puzzle is represented by an array example:

int[] goalState = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

My question is what would be the best data structure to store these values? and the best way to retrieve them.

(This question was originally based on storing in a database, but now I want to store them in some form of local data structure - as retrieving from a database slow )

Bohemian :

Each number 0-15 is a 4-bit number. You must represent 7 such numbers, making a minimum requirement of 28 bits, which is well within the 31 signed bit space of an int. Thus all permutations may be assigned, and derived from, an int.

To calculate this number, given variables a through g:

int key = a | (b << 4) | (c << 8) | (d << 12) | (e << 16) | (f << 20) | (g << 24);

To decode (if you need to):

int a = key & 0xF;
int b = key & 0xF0;
int c = key & 0xF00; // etc

Storing ints in a database is very efficient and will use minimal disk space:

create table heuristics (
    key_value int not null,
    heuristic varchar(32) not null -- as small as you can, char(n) if all the same length
);

After inserting all the rows, create a covering index for super fast lookup:

create unique index heuristics_covering heuristics(key_value, heuristic);

If you create this index before insertion, insertions will be very, very slow.

Creating the data and inserting it is relatively straightforward coding.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=446909&siteId=1