-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_run.py
43 lines (33 loc) · 1.41 KB
/
check_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import torch as tc
import numpy as np
import scipy
# import gym # allowed but not included in env, yet...
from kaggle_environments import evaluate, make, utils
# Make and view the env
env = make("connectx", debug=True)
env.render()
def my_agent(observation, configuration):
from random import choice
return choice([c for c in range(configuration.columns) if observation.board[c] == 0])
env.reset()
# Play as the first agent against default "random" agent.
env.run([my_agent, "random"])
env.render(mode="ansi", width=500, height=450)
# Play as first position against random agent.
trainer = env.train([None, "random"])
observation = trainer.reset()
while not env.done:
my_action = my_agent(observation, env.configuration)
print("Action taken:", my_action)
observation, reward, done, info = trainer.step(my_action)
# env.render(mode="ansi", width=100, height=90, header=False, controls=False)
env.render()
# EVALUATE - read this section
def mean_reward(rewards):
return sum(r[0] for r in rewards) / float(len(rewards))
# Run multiple episodes to estimate its performance.
print("My Agent vs Random Agent:", mean_reward(evaluate("connectx", [my_agent, "random"], num_episodes=10)))
# Takes longer:
print("My Agent vs Negamax Agent:", mean_reward(evaluate("connectx", [my_agent, "negamax"], num_episodes=3)))
# If in ipython, can do an interactive game with:
# env.play([None, "negamax"], width=500, height=450)