This still isn't working...

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

This still isn't working...

 
0
  #1
Apr 10th, 2009
How come this just hangs, after debugging I found it never returns True for precondition even though it should on the very first go around....ugh!!!

  1. #!/usr/local/bin/python
  2. #Scott Landau
  3. #CS 380
  4. #Assignment 1 Programming Portion in Python
  5. #Created - 4/6/09
  6. #Last Edited - 4/8/09
  7.  
  8. import pdb
  9.  
  10. #n is going to be equal to 4 for this queens problem.
  11. n = 4
  12.  
  13. #Assigning the empty list to initialState.
  14. initialState = []
  15.  
  16. #Making an allDirections list.
  17. allDirections = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]
  18.  
  19. #declare the list 'state' which represents the number of non threatening
  20. #queens that have been placed on the board.
  21. state = []
  22.  
  23. def goal(state):
  24. if ((len(state)) == n):
  25. return True
  26. else:
  27. return False
  28.  
  29. #defining allRules list which the contents of which will be created in next function.
  30. allRules = []
  31. #defining all the rules for an nxn board. assigning these rules to allRules
  32. def makeRules(n):
  33. for i in range(0, (n), 1):
  34. for j in range(0, (n), 1):
  35. allRules.append([i,j])
  36. return
  37.  
  38. #this function will apply the rule that is in the rule list to the given state, which just appends it to the state list.
  39. def applyRule(rule,state):
  40. state.append(rule)
  41. return
  42.  
  43. #returns true if the row or column of 'pos' is out of bounds.
  44. def outOfBounds(pos):
  45. if ((pos[0] < 0) or (pos[0] > (n-1)) or (pos[1] < 0) or (pos[1] > (n-1))):
  46. return True
  47. else:
  48. return False
  49.  
  50. #function that will determine which positions queens cannot be placed into because they might eventually run into another queen already on the board.
  51. def blocked(pos,state,direction):
  52. newpos = [(pos[0]+direction[0]),(pos[1]+direction[1])]
  53. if newpos in state:
  54. return True
  55. elif outOfBounds(newpos):
  56. return False
  57. else:
  58. return True
  59. blocked(newpos,state,direction)
  60.  
  61.  
  62. #precondition to the actions checker
  63. def precondition(rule,state):
  64. if not blocked(rule,state,(allDirections[0])):
  65. if not blocked(rule,state,(allDirections[1])):
  66. if not blocked(rule,state,(allDirections[2])):
  67. if not blocked(rule,state,(allDirections[3])):
  68. if not blocked(rule,state,(allDirections[4])):
  69. if not blocked(rule,state,(allDirections[5])):
  70. if not blocked(rule,state,(allDirections[6])):
  71. if not blocked(rule,state,(allDirections[7])):
  72. return True
  73. else:
  74. return False
  75.  
  76. #running a simple test using all the functions
  77. pdb.set_trace()
  78. makeRules(n)
  79. while not goal(state):
  80. for l in range(0, (len(allRules)), 1):
  81. if precondition((allRules[l]),state):
  82. applyRule((allRules[l]),state)
  83. print state
  84.  
  85. print state
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: This still isn't working...

 
0
  #2
Apr 10th, 2009
With huge indentations like you have in your code my editor will go up in smoke! I will let someone else try it first!
Last edited by sneekula; Apr 10th, 2009 at 11:56 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: This still isn't working...

 
0
  #3
Apr 10th, 2009
Originally Posted by sneekula View Post
With huge indentations like you have in your code my editor will go up in smoke! I will let someone else try it first!
Haha.


I just tried to run through the execution of the code in my head without trying to follow the logic.

It seems to me this if not blocked(rule,state,(allDirections[0])): code will evaluate to false the first time it is executed which seems to be what you are describing.

Take out a piece of paper and try to work through your program. This will probably help you figure out where the problem is.

You should also call each of your functions with all possible test cases to see if they are behaving as you expect so that you know you can rely on them in other parts of your program.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 406
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Pro in Training

Re: This still isn't working...

 
0
  #4
Apr 10th, 2009
i cant even understand this code, let alone test it
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: This still isn't working...

 
0
  #5
Apr 10th, 2009
You can replace this:
  1. #precondition to the actions checker
  2. def precondition(rule,state):
  3. if not blocked(rule,state,(allDirections[0])):
  4. if not blocked(rule,state,(allDirections[1])):
  5. if not blocked(rule,state,(allDirections[2])):
  6. if not blocked(rule,state,(allDirections[3])):
  7. if not blocked(rule,state,(allDirections[4])):
  8. if not blocked(rule,state,(allDirections[5])):
  9. if not blocked(rule,state,(allDirections[6])):
  10. if not blocked(rule,state,(allDirections[7])):
  11. return True
  12. else:
  13. return False
  14. ##---------------------------------------------------------
  15. ## with (not tested)
  16. ## in other words, if any one is blocked, return false
  17. ## This is the way the code appears to work.
  18. ##---------------------------------------------------------
  19. def precondition(rule,state):
  20. for j in range(0, 8):
  21. if blocked(rule,state,(allDirections[j])):
  22. return False
  23. return True
Whe you have the same line of code multiple times, consider a for() loop or a function instead.

A print statement here shows that len(state) remains at zero.
  1. def goal(state):
  2. print len(state), n
  3. if ((len(state)) == n):
  4. return True
  5. else:
  6. return False
Also, put in a counter while testing to eliminate endless loops.
  1. ctr = 0
  2. ctr_max = 100
  3. while (not goal(state)) and (ctr < ctr_max):
  4. ctr += 1
  5. if ctr == ctr_max:
  6. print "Bye, Bye. ctr_max limit reached."
  7. for l in range(0, (len(allRules)), 1):
  8. if precondition((allRules[l]),state):
  9. applyRule((allRules[l]),state)
  10. print state
  11.  
  12. print state
Last edited by woooee; Apr 10th, 2009 at 12:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 406
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Pro in Training

Re: This still isn't working...

 
0
  #6
Apr 10th, 2009
also, you could replace this:
  1. #precondition to the actions checker
  2. def precondition(rule,state):
  3. if not blocked(rule,state,(allDirections[0])):
  4. if not blocked(rule,state,(allDirections[1])):
  5. if not blocked(rule,state,(allDirections[2])):
  6. if not blocked(rule,state,(allDirections[3])):
  7. if not blocked(rule,state,(allDirections[4])):
  8. if not blocked(rule,state,(allDirections[5])):
  9. if not blocked(rule,state,(allDirections[6])):
  10. if not blocked(rule,state,(allDirections[7])):
  11. return True
with this:
  1. #precondition to the actions checker
  2. def precondition(rule,state):
  3. if not blocked(rule,state,(allDirections[0]))and not blocked(rule,state,(allDirections[1])) and not blocked(rule,state,(allDirections[2]))
  4. and not blocked(rule,state,(allDirections[3])) and not blocked(rule,state,(allDirections[4]))
  5. and not blocked(rule,state,(allDirections[5])) and not blocked(rule,state,(allDirections[6]))
  6. and not blocked(rule,state,(allDirections[7])):
  7. return True
i know it isnt as readable but it does the same job with less indents.
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: This still isn't working...

 
0
  #7
Apr 10th, 2009
Part of the problem may be all of the global variables, and the functions possibly using locals instead of globals. This program should be using a class anyway. Here it is, converted to a class. The final line prints
Final print [[0, 0], [0, 1], [0, 2], [0, 3]]
Don't know if that is what you were expecting or not. And if you don't already know this, you can pipe any printed output to a file if there is too much to fit on the screen, with the ">" operator. From the command line, this will pipe any output to a file named "test1.txt".
./program_name > test1.txt
  1. #!/usr/bin/python
  2.  
  3. #Scott Landau
  4. #CS 380
  5. #Assignment 1 Programming Portion in Python
  6. #Created - 4/6/09
  7. #Last Edited - 4/8/09
  8.  
  9. import pdb
  10.  
  11. class Queens:
  12. def __init__(self):
  13. self.testing = True ## print diagnostics
  14.  
  15. #n is going to be equal to 4 for this queens problem.
  16. self.n = 4
  17.  
  18. #Assigning the empty list to initialState.
  19. self.initialState = []
  20.  
  21. #Making an allDirections list.
  22. self.allDirections = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]
  23.  
  24. #declare the list 'state' which represents the number of non threatening
  25. #queens that have been placed on the board.
  26. self.state = []
  27.  
  28. #defining allRules list which the contents of which will be created
  29. #in makeRules
  30. ##self.allRules = [] ## declared in makeRules()
  31.  
  32.  
  33. def goal(self):
  34. if self.testing:
  35. print "goal()", len(self.state), self.n
  36.  
  37. if ((len(self.state)) == self.n):
  38. return True
  39. else:
  40. return False
  41.  
  42. #defining all the rules for an nxn board. assigning these rules to allRules
  43. def makeRules(self):
  44. self.allRules = []
  45. for i in range(0, (self.n), 1):
  46. for j in range(0, (self.n), 1):
  47. self.allRules.append([i,j])
  48. return
  49.  
  50. #this function will apply the rule that is in the rule list to the
  51. #given state, which just appends it to the state list.
  52. def applyRule(self, rule):
  53. self.state.append(rule)
  54. return
  55.  
  56. #returns true if the row or column of 'pos' is out of bounds.
  57. def outOfBounds(self, pos):
  58. if ((pos[0] < 0) or (pos[0] > (self.n-1)) \
  59. or (pos[1] < 0) or (pos[1] > (self.n-1))):
  60. return True
  61. else:
  62. return False
  63.  
  64. #function that will determine which positions queens cannot be placed into
  65. #because they might eventually run into another queen already on the board.
  66. def blocked(self, pos, direction):
  67. newpos = [(pos[0]+direction[0]),(pos[1]+direction[1])]
  68. if newpos in self.state:
  69. return True
  70. elif self.outOfBounds(newpos):
  71. return False
  72. else:
  73. return True
  74. ##***** The Following Line Will Never Be Reached *****
  75. ##***** It Is After The Return Statement *****
  76. self.blocked(newpos, direction)
  77.  
  78.  
  79. #precondition to the actions checker
  80. def precondition(self, rule):
  81. for j in range(0, 8):
  82. if self.blocked(rule,(self.allDirections[j])):
  83. return False
  84. else:
  85. return True
  86.  
  87. ##====================================================================
  88. #running a simple test using all the functions
  89. ##pdb.set_trace()
  90.  
  91. ##instantiate class
  92. Q = Queens()
  93.  
  94. Q.makeRules()
  95. ctr = 0
  96. ctr_max = 100
  97. while (not Q.goal()) and (ctr < ctr_max):
  98. ctr += 1
  99. for l in range(0, (len(Q.allRules)), 1):
  100. if Q.precondition((Q.allRules[l])):
  101. Q.applyRule((Q.allRules[l]))
  102. print Q.state
  103. if ctr >= ctr_max:
  104. print "Bye, Bye. ctr_max exceeded"
  105. print "Final print", Q.state
Last edited by woooee; Apr 10th, 2009 at 4:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: This still isn't working...

 
0
  #8
Apr 14th, 2009
Is this working now, or have you given up?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: gnujohn is an unknown quantity at this point 
Solved Threads: 0
gnujohn gnujohn is offline Offline
Newbie Poster

Re: This still isn't working...

 
0
  #9
Apr 15th, 2009
Look: I'm new here, and maybe way, way offbase. You elders will correct me if I'm wrong, but I contend:

# we should not be a service to solve interesting lab projects. This one clearly is just that, a request for help in CS 380.

# one approach to a problem is to ask a person to think less mechanically about the problem. His approach would not be pythonic if it 'worked', and he's lucky that it failed.

--gnujohn@gmail.com
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: This still isn't working...

 
0
  #10
Apr 15th, 2009
I'm certainly not an 'elder' but you're right.

However, I do think the OP came with a legitimate question.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 635 | Replies: 13
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC