微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError: 'int' 对象在尝试为深度有限搜索编写递归算法时没有属性 'map'

如何解决AttributeError: 'int' 对象在尝试为深度有限搜索编写递归算法时没有属性 'map'

尝试为 6 拼图游戏的深度有限搜索编写递归算法,但由于某种原因,我不断收到上述错误,无法理解为什么。 这是我的递归深度有限搜索代码

function getGPUtempHome(){
    let stinker = si.graphics();
    stinker.then(function(tempGPU){
        GPUtempHome = tempGPU.controllers[0].temperatureGpu;
        console.log(GPUtempHome);
        document.getElementById("GPUtemps").innerHTML = GPUtempHome+'°';
        document.documentElement.style.setProperty('--GPUTEMP',GPUtempHome*1.8 + 'deg');
    });
    setInterval( function() {getGPUtempHome();},5000 );
}

function getRAMhome(){
    let stinker3 = si.mem();
    stinker3.then(function(RAM){

        RAMUSAGE = Math.round(((RAM.used/RAM.total)*100));
        console.log(RAMUSAGE);
        document.getElementById("RAmloads").innerHTML = RAMUSAGE+'%';
        document.documentElement.style.setProperty('--RAmload',RAMUSAGE*1.8 + 'deg');
    });
    setInterval( function() {getRAMhome();},5000 );
}

还有 State 类:

def rec_dls(node,limit):
    global cutoff_occurred
    cutoff = 0
    failure = -1
    if goal_state == node.state:
        return node
    elif limit == 0:
        return cutoff          # cutoff
    else:
        cutoff_occurred = False
        actions = moves(node.state) # all possibles children of this state
        for action in actions:
            child = State(action,node,node.depth+1,node.cost+1)
            result = rec_dls(child,limit - 1)
            if result == cutoff:
                cutoff_occurred = True
            elif result != failure:
                return result
        if cutoff_occurred:
            return cutoff
        else:
            return failure
        

def dls(limit):
    node = State(initial_state,None,0)
    return rec_dls(node,limit)

这是我更详细的错误

enter image description here

作为参考,我的工作逻辑基于此(来自“人工智能,现代方法”):

enter image description here

解决方法

问题不在于 rec_dls 返回 int。当它返回您的 State 对象之一时。

考虑以下代码行:

           result = rec_dls(child,limit - 1)
           if result == cutoff:
               # ...

假设这里的 rec_dls 返回您的 State 对象之一。然后将您的 State 对象与包含 cutoffint0 进行比较,并且因为您在 __eq__ 类中覆盖了 State,此比较会导致调用 State.__eq__,并将 other 设置为 0

作为 int0 没有 map 属性,因此您的错误。

也许您想在 __eq__ 方法中包含检查 other 是另一个 State 对象:

    def __eq__(self,other): 
        return isinstance(other,State) and self.map == other.map 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。