开发者

Figuring out evaluator function for best-first search

While running a best-first search from M Tim Jones' Ar开发者_如何学JAVAtificial Intelligence: A Systems Approach, we're asked to determine how the next moves are generated and why the solution is picked.

In order to do that, two code sections are stumping us:

#define checkPiece( board, y )((board & (1 << (15-y))) ? 1 : 0)

#define MAX_TESTS   14
#define MAX_VECTOR  4

typedef struct {
    unsigned char len;
    unsigned char vector[MAX_VECTOR];
} test_t;

and

const test_t tests[MAX_TESTS]={
                            { 4, { 0,  4, 8,  12 } },
                            { 4, { 1,  5, 9,  13 } },
                            { 4, { 2,  6, 10, 14 } },
                            { 4, { 3,  7, 11, 15 } },
                            { 2, { 8, 13 } },
                            { 3, { 4,  9, 14 } },
                            { 4, { 0,  5, 10, 15 } },
                            { 3, { 1,  6, 11 } },
                            { 2, { 2,  7 } },
                            { 2, { 1,  4 } },
                            { 3, { 2,  5,  8 } },
                            { 4, { 3,  6,  9, 12 } },
                            { 3, { 7, 10, 13 } },
                            { 2, { 11, 14 } } 
                          };

Both these are used in the EvaluateBoard function

void evaluateBoard( node_t *node_p ) {
    int test, i, check;
    int cost = 0;

    for (test = 0 ; test < MAX_TESTS ; test++) {

        check = 0;

        for (i = 0 ; i < tests[test].len ; i++) {
            check += checkPiece( node_p->board, tests[test].vector[i] );
        }

        if (check > 1) cost+= (check-1);
    }

 node_p->g = cost;

  printf(" evaluateBoard %04x = (h %d, g %d)\n", 
       node_p->board, node_p->h, node_p->g);

  return;
}

The next move is determined by the Cost int, and is a result of the checkPiece result. In this case, board== 1288, but how is the y value chosen from test_t tests?

Also, just what is the structure test_t tests? We've never seen anything similar in C code. Is it a form of multi-dimensional array?


test_t is a structure. The author of code is initializing the tests array with an array of initialized structures. In C, you can initialize a structure like this;

struct t {
     int num1;
     double num2;
};

struct t my_struct = { 123, 3.141 };

If I wanted to initialize an array of struct t's, then I could do something like this:

struct t my_array[] = {
    {123, 3.141},
    {3245, 6.2156},
    {912, 5.3},
    {0, 1.0}
};

For the BFS stuff, Best first search is a purely heuristical combinatorial search algorithm. This means that at any given node in your graph, you will choose the successor that is the most optimal. Optimal could be least cost or most cost. node_t looks like a generic C structure to support A*, BFS, and Dijkstra search algorithms. This is why the h member variable of the node_t structure is ignored--it's not used in BFS.

evaluateBoard() is evaluating the cost of the node_t position by going through the entire list of tests. The y value isn't chosen, you are merely evaluating the cost of the node so that later you can choose the most optimal successor move in the algorithm.


Also, just what is the structure test_t tests?

tests is defined as an array of struct test_t. Each of the elements contained in the array is itself containing an array of chars. So, yes you could say it is a two dimensional array of some sort.

C (and C++11) allows to initialize structs and arrays of structs with nested curly brackets, just as in the example.

but how is the y value chosen from test_t tests?

The y value needed in the checkPiece makro is the n'th value of the vector array which is itself contained in the current struct test_t.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜