具有曼哈顿距离启发式的A *算法

时间:2014-04-21 00:41:13

标签: performance artificial-intelligence a-star heuristics memory-efficient

我一直在使用C语言中的15个拼图解算器。我的代码使用了大量内存,但是我遇到了一些问题。

我不会发布我的代码,因为它太长了......我已经实现了我使用的大多数库,这可能只会给你带来困惑。让我们从基础开始。

我现在使用的东西是:(所有这些都在C中实现)

- Fibonacci Heap:

/* Struct for the Fibonacci Heap */
typedef struct _fiboheap {
    int  size;           // Number of nodes in the heap
    node min;            // Pointer to the minimun element in the Heap
    funCompare compare;  // Function to compare within nodes in the heap
    freeFunction freeFun;// Function for freeing nodes in the heap
} *fiboheap;


/* Struct of a Fibonacci Heap Node */
typedef struct _node {
    node parent;    // Pointer to the node parent
    node child;     // Pointer to the child node
    node left;      // Pointer to the right sibling
    node right;     // Pointer to the left sibling
    int  degree;    // Degree of the node
    int  mark;      // Mark
    void *key;      // Value of the node (Here is the element)
} *node;

- 哈希表

我唯一没有自己实现的,我使用的是 uthash

- 15-Puzzle States representation

这是一个有趣的话题。在解释这里的情况之前,让我们先想一想15拼图的问题......据我们所知,15-Puzzle有16个瓷砖(我们将空白瓷砖计为数字为0的瓷砖)。那么,有多少可能的状态有15个难题?嗯,它的阶乘(16)状态。所以,那是很多州。这意味着我们希望我们的状态尽可能小......如果我们的初始状态离解决方案太远,我们可能会探索很多状态,以至于我们的程序存储器只会爆炸。

所以...我的15-puzzle表示包括使用位和逻辑运算符:

/* Struct for the 15-puzzle States Representation */
typedef struct _state {
    unsigned int quad_1; /* This int represent the first 8 numbers */
    unsigned int quad_2; /* This int represent the last 8 numbers */
    unsigned short zero; /* This is the position of the zero */
} *state;

所以我正在做的是使用逻辑运算符来移动和更改数字,使用最小的空间。

注意这个结构的大小是 12个字节(因为它有三个整数)

- 曼哈顿距离

这只是众所周知的曼哈顿距离启发式。您基本上计算每个数字当前位置与目标状态中数字位置的距离之和。

- A *与A *

一起使用的实施和节点结构

让我们从节点开始。

typedef struct nodo_struct {
    nodo parent;        // Pointer to the parent of the node
    state estado;       // State associated with the node
    unsigned int g : 8; // Cost of the node
    action a;           // Action by which we arrived to this node
                        // If null, it means that this is the initial node
} *nodo;

注意这些节点与fibonacci堆中的节点无关。

现在这个主题的主要原因是......目前使用的A * I的伪代码。

a_star(state initial_state) {
    q = new_fibo_heap; // Sorted by (Cost g) + (Manhattan Distance)
                       // It will have nodes which contain a pointer to the state
    q.push(make_root_node(initial_state));
    closed = new_hash_table();
    while (!q.empty()) {
        n = q.pop();
        if ((n->state ∉ closed) || (n->g < dist(n->state))) { 
        /* The dist used above is stored in the hash table as well */
            closed.insert(n->state);
            dist(n->state) = n->g;   // Update the hash table
            if (is_goal(n->state)) {
                return extract_solution(n); // Go through parents, return the path
            }
            for <a,s> ∈ successors(n->state) {
            // 'a' is the action, It can be 'l', 'r', 'u', 'd'
            // 's' is the state that is a successor of n
                if (manhattan(s) < Infinity) {
                    q.push(make_node(n,a,s));
                    // Assuming that manhattan is safe
                }
            }
        }
    }
    return NULL;
}

所以我还没能回答的问题是......

如何有效管理内存?你可以重用状态或节点吗?这会带来什么后果?

我的意思是,如果你看看伪代码。它没有考虑重新使用状态或节点。它只是不断地分配节点和状态,即使它们之前已经计算过。

我一直在思考这个问题......每次运行解算​​器时,它都可以快速扩展数百万个节点。而且正如我们所知,当您找到成本低于前一个路径的其他路径时,A *可能会重新探索节点。这意味着......如果我们探索100万个节点,它将是2400万字节(哇)...并且考虑到每个节点都有一个状态,即每个节点14个字节,那些是1400万个字节。

最后,我需要的是一种重用/释放空间的方法,这样当我执行此解算器时,我的电脑就不会崩溃。

(PD:很抱歉很长的帖子)

1 个答案:

答案 0 :(得分:0)

你是为了作业还是为了好玩而做这件事?

如果它很有趣,那么请不要使用A *,请使用IDA *。 IDA *会更快,几乎不使用任何内存。此外,您可以使用额外的内存来构建更好的启发式方法,并获得更好的性能。 (你应该找到足够的信息,如果你是google&#34;模式数据库&#34;。这些是由Jonathan Schaeffer和Joe Culberson发明的,但是由Rich Korf和Ariel Felner详细研究过。)

IDA *有一些缺点,并且在每个领域都不起作用,但它对于滑动拼图难题来说是完美的。

另一种可以提供帮助的算法是Breadth-First Heuristic Search。本文和其他文章讨论了如何避免完全存储封闭列表。

基本上,很多聪明人之前已经解决了这个问题,并且他们已经发布了他们的方法/结果,所以你可以从中学习。

以下是改善A *的一些提示:

  • 我发现Fibonacci堆没有大量的速度增益,因此您可能希望使用更简单的数据结构。 (尽管自从我最后一次尝试以来可用的实现可能已经有所改进。)

  • 节点的f-cost将以2为增量跳跃。因此,您可以节省f-cost并且只担心在同一f-cost层中对项目进行排序。 FIFO队列实际上运行良好。

  • 您可以使用this paper中的提示将15-puzzle表示转换为排列,对于完整表示,这将占用大约43位。但是,扩展状态变得更加昂贵,因为你必须转换成不同的表示来生成移动。

  • 避免使用禁止的运算符完全存储已关闭的列表。 (有关详细信息,请参阅先前的广度优先启发式搜索文章或本文Best-First Frontier search。)

希望这些要点能解决您的问题。如果您需要,我很乐意提供更多链接或澄清。

相关问题