Beispiel #1
0
    def update_state(state, move, outcome):
        """ Update (or create) outcome probabilities for a game state
    and move combo
    
    Arguments:
      state and move: standard notation game strings
      outcome: array of player scores [ player, bot ]
    """

        from bot import Bot
        game = BotMoves.find_state(state, move)
        outcome = BotMoves.outcome(outcome)
        if game:
            if game[0].count() > 1:  # Dup!
                # Use the most played move
                dups = []
                for game in game[0]:
                    dups.append(game)
                dups.sort(key=lambda g: g.played, reverse=True)
                for i in range(1, len(dups)):
                    dups[i].delete()
                game = dups[0]
            else:
                game = game[0].one()
            game.played += 1
            if outcome == 1: game.wins += 1
            elif outcome == 0: game.draws += 1
            else: game.losses += 1
            game.winp = float(game.wins) / float(game.played)
            game.lossp = float(game.losses) / float(game.played)
            game.drawp = float(game.draws) / float(game.played)
            if game.played_in_sequence >= Bot.noise_factor():
                game.played_in_sequence = 0
            else:
                game.played_in_sequence += 1
            elixir.session.commit()
            # Add mapped flag if all valid moves have been played
            from botmovesmapped import BotMovesMapped
            if not BotMovesMapped.has(game.state):
                from state import State
                state = State(game.state)
                if not Bot.get_missing_move(state):
                    BotMovesMapped(state=game.state)
                    elixir.session.commit()
        else:
            # Create new record
            w = d = l = 0
            if outcome == 1: w = 1
            elif outcome == 0: d = 1
            else: l = 1
            BotMoves(state=state,
                     move=move,
                     wins=w,
                     draws=d,
                     losses=l,
                     winp=float(w),
                     drawp=float(d),
                     lossp=float(l))
            elixir.session.commit()
Beispiel #2
0
    def get_missing_move(state):
        """ Get first valid move for this state that isn't in the DB
    or None if all are present """

        from botmoves import BotMoves
        from botmovesmapped import BotMovesMapped
        from move import Move, COLLAPSE
        valid = state.get_valid_moves()
        s = state.dumps()
        last_move = state.moves[-1]
        weight = last_move.weight + (1 if last_move.type != COLLAPSE else 0)
        if not BotMovesMapped.has(s):
            # Check for collapse
            if state.cycle_squares:
                # Append another valid move to the move string
                all = []
                for move in valid:
                    state.step(Move(2, weight, move[0], move[1]))
                    if state.outcome:
                        all.append(move)
                    else:
                        for a in state.get_valid_moves():
                            all.append('%s/%s' % (move, a))
                    state.unstep()
                valid = all
            # Look for all found moves or any variations in the DB
            for move in valid:
                if not BotMoves.find_state(s, move):
                    return move
        return None
Beispiel #3
0
 def get_missing_move(state):
   """ Get first valid move for this state that isn't in the DB
   or None if all are present """
   
   from botmoves import BotMoves
   from botmovesmapped import BotMovesMapped
   from move import Move, COLLAPSE
   valid = state.get_valid_moves()
   s = state.dumps()
   last_move = state.moves[-1]
   weight = last_move.weight + (1 if last_move.type != COLLAPSE else 0)
   if not BotMovesMapped.has(s):
     # Check for collapse
     if state.cycle_squares:
       # Append another valid move to the move string
       all = []
       for move in valid:
         state.step(Move(2, weight, move[0], move[1]))
         if state.outcome:
           all.append(move)
         else:
           for a in state.get_valid_moves():
             all.append('%s/%s'%(move,a))
         state.unstep()
       valid = all
     # Look for all found moves or any variations in the DB
     for move in valid:
       if not BotMoves.find_state(s, move):
         return move
   return None
Beispiel #4
0
 def update_state(state, move, outcome):
   """ Update (or create) outcome probabilities for a game state
   and move combo
   
   Arguments:
     state and move: standard notation game strings
     outcome: array of player scores [ player, bot ]
   """
   
   from bot import Bot
   game = BotMoves.find_state(state, move)
   outcome = BotMoves.outcome(outcome)
   if game:
     if game[0].count() > 1: # Dup!
       # Use the most played move
       dups = []
       for game in game[0]: dups.append(game)
       dups.sort(key=lambda g: g.played, reverse=True)
       for i in range(1,len(dups)): dups[i].delete()
       game = dups[0]
     else:
       game = game[0].one()
     game.played += 1
     if outcome == 1: game.wins += 1
     elif outcome == 0: game.draws += 1
     else: game.losses += 1
     game.winp = float(game.wins) / float(game.played);
     game.lossp = float(game.losses) / float(game.played);
     game.drawp = float(game.draws) / float(game.played);
     if game.played_in_sequence >= Bot.noise_factor():
       game.played_in_sequence = 0
     else:
       game.played_in_sequence += 1
     elixir.session.commit()
     # Add mapped flag if all valid moves have been played
     from botmovesmapped import BotMovesMapped
     if not BotMovesMapped.has(game.state):
       from state import State
       state = State(game.state)
       if not Bot.get_missing_move(state):
         BotMovesMapped(state=game.state)
         elixir.session.commit()
   else:
     # Create new record
     w = d = l = 0
     if outcome == 1: w = 1
     elif outcome == 0: d = 1
     else: l = 1
     BotMoves(state=state,
              move=move,
              wins=w,
              draws=d,
              losses=l,
              winp=float(w),
              drawp=float(d),
              lossp=float(l))
     elixir.session.commit()