1

I have a problem with 2 jugs. initial state is [0,0], capacity of each jug is [4,9], goal state is [0,6] legal moves: fill jug 1 or jug 2 till the end, empty jug 1 or jug 2 and poor one jug into the other one.

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

The question is since I have declared all the legal moves, why does my program return only the result of one legal move? Eg from starting state [0,0] when i run the program it returns [4,0],[0,4],[0,9] and other possible results untill the recursion stops but not my goal state. What am I missing?

breadth_first_search class:

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)

search class:

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"

the class that runs the program:

import decant
from breadth_first_search import *

dec = decant.Decant()
print breadth_first_search(dec,[[dec.start()]])
3
  • 4
    Where is the recursion?
    – Barmar
    Dec 22, 2014 at 22:25
  • Please provide a lot more information on how all these parts are supposed to fit together
    – Stuart
    Dec 22, 2014 at 22:36
  • First I run the program by running the last bit of code i posted. Then, the program imports class breadth_first_search and then it prints the results of that class. The main problem is in the two_jugs class. Everything else im pretty sure is correct
    – spyr
    Dec 22, 2014 at 22:37

1 Answer 1

0

Your first mistake is labeling the class 2jugs. Variable names in python (including names of classes and functions) as well as many other programming languages cannot start with numbers. So rename 2jugs to two_jugs.

The precise rule for identifiers in python is the identifier must start with a uppercase (A-Z), lowercase (a-z), or underscore (_). The following characters of the identifier can also contain digits (0-9). (The rule is specified in Backus-Naur form).

1
  • My apologizes on that and thank you for clearing that out. Btw thanks for editing my question. now it looks as a professional issue. Any help appreciated. Merry xmas
    – spyr
    Dec 22, 2014 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.