Loading ...
Sorry, an error occurred while loading the content.

Improving on the AIMA-Python code a little bit

Expand Messages
  • Antti Ylikoski
    I m currently delving into the AIMA-Python modules. In the module games.py there was there something to correct: in order to be able to run at all the
    Message 1 of 3 , Apr 6 7:18 PM
    View Source
    • 0 Attachment
      I'm currently delving into the AIMA-Python modules. In the module
      games.py there was there something to correct: in order to be able to
      run at all the play_game() function in the way that it is indicated in
      the documentation, it is necessary to precede the call of the
      play_game() function with a ">>> from games import *" statement.

      Moreover, I made the play_game() function and the display() method of
      the TicTacToe() class a little neater.

      Try the games.py module with these changes.

      ------------------------------------------------------
      # The play_game function of the games.py module. AJY 04-07-2012.

      def play_game(game, *players):
      """Play an n-person, move-alternating game.
      >>> from games import * # Added this. A. J. Y. 04-07-2012.
      >>> play_game(TicTacToe(), alphabeta_player, alphabeta_player) # Changed.
      0 # With correct play, it is always a draw. AJY 04-07-2012.
      """
      state = game.initial
      while True:
      for player in players:
      move = player(game, state)
      state = game.result(state, move)
      game.display(state) # Added this. A. J. Y. 04-07-2012.
      if game.terminal_test(state):
      return game.utility(state, game.to_move(game.initial))

      ------------------------------------------------------
      # The display method of the TicTacToe() class. AJY 04-07-2012.

      def display(self, state):
      board = state.board
      print "\n" # Added this. A. J. Y. 04-07-2012.
      for x in range(1, self.h+1):
      for y in range(1, self.v+1):
      print board.get((x, y), '.'),
      print
      print "\n" # Added this. A. J. Y. 04-07-2012.

      ------------------------------------------------------


      kind regards, Antti J Ylikoski
      Helsinki, Finland, the E.U.
    • darius_bacon
      ... You can run the examples with python doctests.py . It s standard in Python to write examples assuming the import implicitly, because you always need to
      Message 2 of 3 , Apr 17 12:30 AM
      View Source
      • 0 Attachment
        --- In aima-talk@yahoogroups.com, Antti Ylikoski <antti.ylikoski@...> wrote:
        > I'm currently delving into the AIMA-Python modules. In the module
        > games.py there was there something to correct: in order to be able to
        > run at all the play_game() function in the way that it is indicated in
        > the documentation, it is necessary to precede the call of the
        > play_game() function with a ">>> from games import *" statement.

        You can run the examples with "python doctests.py". It's standard in Python to write examples assuming the import implicitly, because you always need to import code to run it. Maybe the README should say something.

        > Moreover, I made the play_game() function and the display() method of
        > the TicTacToe() class a little neater.

        I'd rather leave display() out of play_game() so it can be used to evaluate AI players silently, e.g. for tournaments and training. Display in the players instead, like query_player(). There is a need for a standard hook for these displays to show up in a user interface, and it'd be reasonable for play_game to emit events to such a hook. We've been leaning towards a web interface with Javascript, though so far nobody's stepped up to do it.

        I'm not sure about the extra newline for tictactoe's display -- generally it's easier to add a newline in the caller than to remove a newline the caller wished wasn't there, so the current scheme seems right to me. I've only made a quick look right now, though. Thanks for the suggestion.

        Darius

        > Try the games.py module with these changes.
        >
        > ------------------------------------------------------
        > # The play_game function of the games.py module. AJY 04-07-2012.
        >
        > def play_game(game, *players):
        > """Play an n-person, move-alternating game.
        > >>> from games import * # Added this. A. J. Y. 04-07-2012.
        > >>> play_game(TicTacToe(), alphabeta_player, alphabeta_player) # Changed.
        > 0 # With correct play, it is always a draw. AJY 04-07-2012.
        > """
        > state = game.initial
        > while True:
        > for player in players:
        > move = player(game, state)
        > state = game.result(state, move)
        > game.display(state) # Added this. A. J. Y. 04-07-2012.
        > if game.terminal_test(state):
        > return game.utility(state, game.to_move(game.initial))
        >
        > ------------------------------------------------------
        > # The display method of the TicTacToe() class. AJY 04-07-2012.
        >
        > def display(self, state):
        > board = state.board
        > print "\n" # Added this. A. J. Y. 04-07-2012.
        > for x in range(1, self.h+1):
        > for y in range(1, self.v+1):
        > print board.get((x, y), '.'),
        > print
        > print "\n" # Added this. A. J. Y. 04-07-2012.
        >
        > ------------------------------------------------------
        >
        >
        > kind regards, Antti J Ylikoski
        > Helsinki, Finland, the E.U.
        >
      • Antti Ylikoski
        Thank you for the clarification. I only know the language, I m not proficient in programming in Python. When I unzipped the Python files from the .zip file
        Message 3 of 3 , Apr 17 11:17 AM
        View Source
        • 0 Attachment
          Thank you for the clarification.  I only know the language, I'm not proficient in programming in Python.

          When I unzipped the Python files from the .zip file obtained from the AIMA site, the README file did not come with them.  I did not notice that it does exist at all.... My omission, but one could make the README somewhat more visible for casual visitors.  Professional visitors certainly will find it.

          kind regards, Antti J Ylikoski
          Helsinki, Finland, the EU


          2012/4/17 darius_bacon <withal@...>
           

          --- In aima-talk@yahoogroups.com, Antti Ylikoski <antti.ylikoski@...> wrote:
          > I'm currently delving into the AIMA-Python modules. In the module
          > games.py there was there something to correct: in order to be able to
          > run at all the play_game() function in the way that it is indicated in
          > the documentation, it is necessary to precede the call of the
          > play_game() function with a ">>> from games import *" statement.

          You can run the examples with "python doctests.py". It's standard in Python to write examples assuming the import implicitly, because you always need to import code to run it. Maybe the README should say something.


          > Moreover, I made the play_game() function and the display() method of
          > the TicTacToe() class a little neater.

          I'd rather leave display() out of play_game() so it can be used to evaluate AI players silently, e.g. for tournaments and training. Display in the players instead, like query_player(). There is a need for a standard hook for these displays to show up in a user interface, and it'd be reasonable for play_game to emit events to such a hook. We've been leaning towards a web interface with Javascript, though so far nobody's stepped up to do it.

          I'm not sure about the extra newline for tictactoe's display -- generally it's easier to add a newline in the caller than to remove a newline the caller wished wasn't there, so the current scheme seems right to me. I've only made a quick look right now, though. Thanks for the suggestion.

          Darius


          > Try the games.py module with these changes.
          >
          > ------------------------------------------------------
          > # The play_game function of the games.py module. AJY 04-07-2012.
          >
          > def play_game(game, *players):
          > """Play an n-person, move-alternating game.
          > >>> from games import * # Added this. A. J. Y. 04-07-2012.
          > >>> play_game(TicTacToe(), alphabeta_player, alphabeta_player) # Changed.
          > 0 # With correct play, it is always a draw. AJY 04-07-2012.
          > """
          > state = game.initial
          > while True:
          > for player in players:
          > move = player(game, state)
          > state = game.result(state, move)
          > game.display(state) # Added this. A. J. Y. 04-07-2012.
          > if game.terminal_test(state):
          > return game.utility(state, game.to_move(game.initial))
          >
          > ------------------------------------------------------
          > # The display method of the TicTacToe() class. AJY 04-07-2012.
          >
          > def display(self, state):
          > board = state.board
          > print "\n" # Added this. A. J. Y. 04-07-2012.
          > for x in range(1, self.h+1):
          > for y in range(1, self.v+1):
          > print board.get((x, y), '.'),
          > print
          > print "\n" # Added this. A. J. Y. 04-07-2012.
          >
          > ------------------------------------------------------
          >
          >
          > kind regards, Antti J Ylikoski
          > Helsinki, Finland, the E.U.
          >


        Your message has been successfully submitted and would be delivered to recipients shortly.