Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Initialize state values
- player = ['X','O',' ']
- states_dict = {}
- all_possible_states = [[list(i[0:3]),list(i[3:6]),list(i[6:10])] for i in itertools.product(player, repeat = 9)]
- n_states = len(all_possible_states)
- n_actions = 9 # 9 spaces
- state_values_for_AI = np.full((n_states),0.0)
- print("n_states = %i \nn_actions = %i"%(n_states, n_actions))
- for i in range(n_states):
- states_dict[i] = all_possible_states[i]
- winner, _ = check_current_state(states_dict[i])
- if winner == 'O': # AI won
- state_values_for_AI[i] = 1
- elif winner == 'X': # AI lost
- state_values_for_AI[i] = -1
- def update_state_value(curr_state_idx, next_state_idx, learning_rate):
- new_value = state_values_for_AI[curr_state_idx] + learning_rate*(state_values_for_AI[next_state_idx] - state_values_for_AI[curr_state_idx])
- state_values_for_AI[curr_state_idx] = new_value
- def getBestMove(state, player, epsilon):
- '''
- Reinforcement Learning Algorithm
- '''
- moves = []
- curr_state_values = []
- empty_cells = []
- for i in range(3):
- for j in range(3):
- if state[i][j] is ' ':
- empty_cells.append(i*3 + (j+1))
- for empty_cell in empty_cells:
- moves.append(empty_cell)
- new_state = copy_game_state(state)
- play_move(new_state, player, empty_cell)
- next_state_idx = list(states_dict.keys())[list(states_dict.values()).index(new_state)]
- curr_state_values.append(state_values_for_AI[next_state_idx])
- print('Possible moves = ' + str(moves))
- print('Move values = ' + str(curr_state_values))
- best_move_idx = np.argmax(curr_state_values)
- if np.random.uniform(0,1) <= epsilon: # Exploration
- best_move = random.choice(empty_cells)
- print('Agent decides to explore! Takes action = ' + str(best_move))
- epsilon *= 0.99
- else: # Exploitation
- best_move = moves[best_move_idx]
- print('Agent decides to exploit! Takes action = ' + str(best_move))
- return best_move
Add Comment
Please, Sign In to add comment