【问题标题】:Is Iterative Deepening Search supposed to be that slow?迭代深化搜索应该那么慢吗?
【发布时间】:2013-11-30 18:54:10
【问题描述】:

我的数据结构:

class Cell
{
public:
    struct CellLink 
    {
        Cell *cell;
        int weight;
    };
public:
    int row;
    int column;
    vector<CellLink> neighbors;
    State state;
    int totalCost = 0;
};

主要功能:

    void AI::IterativeDeepeningSearch(Cell* cell)
        {
            Cell* temp;
            int bound = 0;


        while (true)
        {
            naturalFailure = false;
            temp = IDShelper(cell, bound);

            if (IsExit(temp))
            {
                break;
            }
            bound++;
        }   
    }

助手:

Cell* AI::IDShelper(Cell* cell, int bound)
{
    Cell* temp = cell;

    SetEnvironment(cell, State::visited);
    PrintEnvironment();

    if (bound > 0)
    {
        for (int i = 0; i < cell->neighbors.size(); i++)
        {
            temp = IDShelper(cell->neighbors[i].cell, bound - 1);
            if (IsExit(temp))
            {
                naturalFailure = true;
                return temp;
            }
        }
    }
    else if (IsExit(cell))
    {
        return cell;
    }
    return temp;
}

我对迷宫进行了迭代深化搜索。问题在于,在 21x21 的迷宫中完成搜索实际上需要数小时,而其他算法则需要几秒钟。

我知道 IDS 应该很慢,但它应该那么慢吗?

【问题讨论】:

  • 其他算法之类的?最接近的比较是 BFS,但结果仍然很大程度上取决于目标节点的位置(但如果 BFS 需要几秒钟而 DFS ID 需要几小时,则可能有问题)。
  • @Dukeling 我已经实现了 BFS 和 UFC,它们只需要大约 3-4 秒
  • 似乎不太可能慢几个数量级。我会检查您的递归,以确保您不会不小心一遍又一遍地重新访问相同的东西。例如,self->neighbor->neighbor 回到自己..
  • @JoeZ 这似乎正是发生了什么。
  • Joe 可能是对的,您不应该在递归之前或在递归函数开始时检查是否访问了单元格吗?

标签: c++ algorithm search c++11 recursion


【解决方案1】:

我想我明白为什么这很慢了。

在你的助手中,你像这样访问邻居:

if (bound > 0)
{
    for (int i = 0; i < cell->neighbors.size(); i++)
    {
        temp = IDShelper(cell->neighbors[i].cell, bound - 1);
        if (IsExit(temp))
        {
            naturalFailure = true;
            return temp;
        }
    }
}

但您从不使用过去的结果。您将某项标记为已访问,但从不检查它是否已被访问。

【讨论】:

  • 我认为这就是问题所在。让我检查后回复你
  • 这实际上不起作用,它让我陷入了无限循环。但也许在每个级别的搜索之后,我需要清理每个单元格的状态。我试试看
  • @Powerbyte 是的,一定要清除每一关之后的状态。
  • 我正在想办法做到这一点,我会尽快回复你
  • 我马上就要离开了,所以如果在那之前我不能完全帮助你,我将不得不让像 Dukeling 和 JoeZ 这样的人从我离开的地方继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多