Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Python] Trying to debug my program

  1. Apr 17, 2015 #1

    fluidistic

    User Avatar
    Gold Member

    Hi people,
    I'm trying to figure out what's wrong with my code.
    My goal is to calculate the average number of stones it takes so that 2 stones become adjascent on a 19x19 goban (the board of the game of go), when we randomly pick an intersection and place a stone on it. The answer should be almost 13 if I remember well.
    Here's my code:
    Code (Python):

    # Program that estimates the average number of stones required so that 2 stones
    # become adjascent in an nxn goban. I want to plot the distribution of number
    # of stones vs numbers of trial, i.e. an histogram.
    import random
    import numpy as np

    # Size of the board
    n = 19

    # Number of trials
    trials = 1000
    # Initialize the board list of lists to 0. 0 means no stone, 1 means stone.
    # Note that board[0][0] is the first element and board[20][20] is the last one
    # , for when n=19. That's because I define the board
    # as n+2 x n+2 for the edges...
    board = [[0]*(n+2) for i in range(n+2)]
    counter = 0
    # Generate random coordinates inside of the 19x19 board
    x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)

    # Now define a boolean function that I'll use as a condition in a while loop


    def check_if_stones(x_coordinates, y_coordinates):
      if (board[x_coordinates-1][y_coordinates] == 1
      or board[x_coordinates][y_coordinates-1] == 1
      or board[x_coordinates+1][y_coordinates] == 1
      or board[x_coordinates][y_coordinates+1] == 1):
          return False
      else:
          return True

    trial_list = []
    for j in range(trials):
        while check_if_stones(x_coordinates, y_coordinates):
            if board[x_coordinates][y_coordinates] == 0:
                # Now replace a random element by 1
                 board[x_coordinates][y_coordinates] = 1
                 counter += 1
                 x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)
        x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)
        # Update the counter when the boolean function returns false. Do this outside of the while loop.
        counter += 1
        trial_list.append(counter)
        x_coordinates, y_coordinates = random.randint(1, n), random.randint(1, n)
    print(np.mean(trial_list))
     
    Now, when trials = 1000 like in the code, one would expect the result to be close to 13. But it returns a number around 622. When trial is worth 5 I get an output of around 17...
    So that np.mean(trial_list) depends heavily on "trials" while it shouldn't depend that heavily on it.
    I've no idea what's wrong with the code and I'm desperate to fix it. I'd appreciate if you could spot anything weird, thanks!

    P.D.:Sorry about the indentation, it seems to be broken when I paste it from Emacs. I've edited it manually but I may have goofed here on PF somewhere.
     
    Last edited: Apr 17, 2015
  2. jcsd
  3. Apr 17, 2015 #2

    mheslep

    User Avatar
    Gold Member

    The trial_list append is outside the j loop, so append(counter) is executed once. The mean then is meaningless.
     
  4. Apr 17, 2015 #3

    fluidistic

    User Avatar
    Gold Member

    My bad.... the indentation is really messed up. In my code it is well inside the for j loop.



    EDIT: Problem solved!!! I had to reset both the counter and the board in the for j loop!!! It now works fine!
     
    Last edited: Apr 17, 2015
  5. Apr 18, 2015 #4
    You should look at using enumerate instead of defining "counter=0" to see if you like it more; it's more elegant to me.
     
  6. Apr 18, 2015 #5

    fluidistic

    User Avatar
    Gold Member

    Thanks for the tip. That would look more pythonic indeed. I'll check it out... though my priority for now is to translate this code into common lisp. I'm just starting to learn CL, it looks quite hard.
     
  7. Apr 18, 2015 #6

    Mark44

    Staff: Mentor

    LISP = Lots of Infuriating Superfluous Parentheses o0)
     
  8. Apr 18, 2015 #7
    Chicken scheme is my preferred lisp-dialect frankly, since scheme is less nasty than lisp and chicken's c compiler allows you to write decently fast code.
     
  9. Apr 19, 2015 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    First things first, why? (And why not Haskell, or Erlang, or OCaml?)

    Secondly, why are you trying to transliterate? There's a name for the hilarious result that typically ensues when a Fortran program is translated, statement for statement, to C. It's called C-Tran (and that is a derogatory term). Don't transliterate. On trying to learn a new language, learn the language. Jump right in! Learn the language, learn the idioms. Start from scratch. Don't translate.

    Thirdly, your code doesn't look pythonic to me. What's with the if statement in def check_if_stones? There's no need for an if. All you need is the boolean expression. Learn to do that and you'll be one step closer to functional programming.
     
  10. Apr 19, 2015 #9

    fluidistic

    User Avatar
    Gold Member

    One of my dreams is to understand Maxima's code and eventually contribute to it. I'd like to fix bugs, make the program more efficient, etc.

    Because as of now I don't know almost any lisp and as first program I want to be able to replicate what the Python's code is doing. I never started with the hello world program with python either, I jumped right into problem solving.
    Ok thanks for the tip. I don't see it now, but I'll think about it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Python] Trying to debug my program
  1. Try out my new program (Replies: 19)

  2. Programming in Python (Replies: 22)

Loading...