【问题标题】:Python recursion what am I missingPython递归我错过了什么
【发布时间】:2015-02-21 01:29:15
【问题描述】:

我对 2 个水壶有疑问。 初始状态为 [0,0],每个水壶的容量为 [4,9],目标状态为 [0,6] 合法动作:将壶 1 或壶 2 装满,清空壶 1 或壶 2,将一壶放入另一壶。

import search #get the interface
import sys
sys.setrecursionlimit(5000)
class two_jugs(search.Nodes):

    #return the starting state vessel 1: 0, vessel 2: 0
    def start(self):
            return [0,0]

    #returns true if node is equal to goal node
    def goal(self,node):
            return node ==(0,6)
    #yields the successors of the configuration indicated by node
    def succ(self,node):             
            # set capacities for vessel 1: 4, vessel 2 : 9
            c = [4,9];
            #stop at second last
            for i in range(len(node)-1):
                    #create a safety copy
                    new_node = node[:]

                    #fill vessel 1
                    if new_node[i]<=0:
                            new_node[i]=c[i]
                            print new_node

                    #fill vessel 2
                    if new_node[i+1]<=0:
                            new_node[i+1]=c[i+1]
                            print new_node

                    #dump vessel i+1
                    if (new_node[i+1]>0):
                            new_node[i+1]=0
                            print new_node

                    #poor vessel i to vessel i+1                
                    if (new_node[i+1]<c[i+1] and new_node[i]>0):
                            #calculate the difference
                            d = min(new_node[i],c[i+1]-new_node[i+1])
                            new_node[i]= new_node[i]-d
                            new_node[i+1]= new_node[i+1]+d
                            print new_node

                    #dump vessel i
                    if (new_node[i]>0):
                            new_node[i]=0
                            print new_node


               #poor vessel i+1 to vessel 1
                    if (new_node[i]<c[i] and new_node[i+1]>0):
                            #calculate the difference
                            d = min(new_node[i+1],c[i]-new_node[i])
                            #set new node
                            new_node[i+1]= new_node[i+1]-d
                            new_node[i]= new_node[i]+d
                            yield new_node
                            print new_node

问题是,既然我已经声明了所有的合法移动,为什么我的程序只返回一个合法移动的结果?例如,当我运行程序时,从起始状态 [0,0] 返回 [4,0]、[0,4]、[0,9] 和其他可能的结果,直到递归停止但不是我的目标状态。 我错过了什么?

breadth_first_search 类:

def breadth_first_search(problem, candidates):
    if not candidates: return
    # make sure there is something in the candidate list
    # I am modifying ’candidates’ list here.
    # Why don’t I need to copy?
    c = candidates.pop(0) # pop from front
    node = c[-1] # must exist
    if problem.goal(node): return c
    # base case
    succ = [s for s in problem.succ(node)]
    for s in problem.succ(node):
        candidates.append(c + [s])
        # 1-step extension
    return breadth_first_search(problem, candidates)

搜索类:

class Nodes:
    def succ(self,n):
        raise Exception, "Successor undefined"
    def start (self):
        raise Exception, "Start undefined"
    def goal (self,n):
        raise Exception, "Goal undefined"

运行程序的类:

import decant
from breadth_first_search import *

dec = decant.Decant()
print breadth_first_search(dec,[[dec.start()]])

【问题讨论】:

  • 递归在哪里?
  • 请提供更多关于所有这些部分应该如何组合在一起的信息
  • 首先我通过运行我发布的最后一段代码来运行程序。然后,程序导入类 breadth_first_search,然后打印该类的结果。主要问题出在 two_jugs 类中。其他一切我很确定是正确的

标签: python search recursion


【解决方案1】:

您的第一个错误是将类标记为 2jugs。 python 中的变量名(包括类名和函数名)以及许多其他编程语言不能以数字开头。所以将2jugs 重命名为two_jugs。

precise rule for identifiers in python 是标识符必须以大写字母 (A-Z)、小写字母 (a-z) 或下划线 (_) 开头。标识符的以下字符也可以包含数字 (0-9)。 (规则在Backus-Naur form中指定)。

【讨论】:

  • 对此我深表歉意,并感谢您清除它。顺便说一句,感谢您编辑我的问题。现在它看起来像一个专业问题。任何帮助表示赞赏。圣诞快乐
猜你喜欢
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 2020-11-11
  • 2021-11-23
相关资源
最近更新 更多