final working i think
This commit is contained in:
parent
e4fca60692
commit
ed2bfe4f88
4 changed files with 11 additions and 5 deletions
Binary file not shown.
13
nim.py
13
nim.py
|
|
@ -62,7 +62,7 @@ class NimAI():
|
||||||
float: The Q-value associated with the (state, action) pair.
|
float: The Q-value associated with the (state, action) pair.
|
||||||
Returns 0 if the pair is not yet in the Q-table.
|
Returns 0 if the pair is not yet in the Q-table.
|
||||||
"""
|
"""
|
||||||
print(self.q, state, action)
|
# print(self.q, state, action)
|
||||||
try:
|
try:
|
||||||
return self.q[(tuple(state), action)]
|
return self.q[(tuple(state), action)]
|
||||||
except:
|
except:
|
||||||
|
|
@ -123,10 +123,13 @@ class NimAI():
|
||||||
# keys = [key[1] for key in self.q.key if key[0] == state]
|
# keys = [key[1] for key in self.q.key if key[0] == state]
|
||||||
# for key in keys:
|
# for key in keys:
|
||||||
else:
|
else:
|
||||||
try:
|
# state = tuple(state)
|
||||||
return max([key[1] for key in self.q.keys() if key[0] == state])
|
# max(self.q[state, action]) for action in [key[1] for key in self.q if key[0] == state]
|
||||||
except:
|
# for q in self.q:
|
||||||
return (0,0)
|
# if q[0] == state:
|
||||||
|
|
||||||
|
return list(self.q.keys())[list(self.q.values()).index(self.best_future_reward(state))][1]
|
||||||
|
|
||||||
|
|
||||||
def train(n):
|
def train(n):
|
||||||
player = NimAI()
|
player = NimAI()
|
||||||
|
|
|
||||||
1
play.py
1
play.py
|
|
@ -5,6 +5,7 @@ if __name__ == "__main__":
|
||||||
# Train the AI with 1000 games
|
# Train the AI with 1000 games
|
||||||
print("START TRAINING \n")
|
print("START TRAINING \n")
|
||||||
ai = train(1000)
|
ai = train(1000)
|
||||||
|
print(ai.q)
|
||||||
|
|
||||||
# Start the game and play against the trained AI
|
# Start the game and play against the trained AI
|
||||||
print("STARTING THE GAME \n")
|
print("STARTING THE GAME \n")
|
||||||
|
|
|
||||||
2
test.py
2
test.py
|
|
@ -25,6 +25,8 @@ def test_best_future_reward(ai):
|
||||||
|
|
||||||
def test_choose_action(ai):
|
def test_choose_action(ai):
|
||||||
print("\n--- Testing choose_action ---")
|
print("\n--- Testing choose_action ---")
|
||||||
|
print(ai.choose_action([1,1,1,0]))
|
||||||
|
print(ai.choose_action([1,1,1,0], epsilon=False))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue