簡體   English   中英

Python遞歸我缺少什么

[英]Python recursion what am I missing

我有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]和其他可能的結果,直到遞歸停止而不是我的目標狀態。 我想念什么?

廣度優先搜索類:

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()]])

您的第一個錯誤是將類標記為2jugs python中的變量名稱(包括類和函數的名稱)以及許多其他編程語言不能以數字開頭。 因此,將2jugs重命名為two_jugs

python中標識符的精確規則是標識符必須以大寫( AZ ),小寫( az )或下划線( _ )開頭。 標識符的以下字符也可以包含數字( 0-9 )。 (該規則以Backus-Naur形式指定)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM