- added idea project

- improved formatting
- improved state print
This commit is contained in:
2024-06-08 18:55:19 +02:00
parent da12213f95
commit 3a0246d0c9
7 changed files with 81 additions and 21 deletions
+3
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+6
View File
@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (tic_tac_toe)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (tic_tac_toe)" project-jdk-type="Python SDK" />
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/tic_tac_toe.iml" filepath="$PROJECT_DIR$/.idea/tic_tac_toe.iml" />
</modules>
</component>
</project>
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+41 -21
View File
@@ -1,6 +1,9 @@
import numpy as np
from numpy.random import uniform
float_formatter = "{:.3f}".format
np.set_printoptions(formatter={'float_kind':float_formatter})
def softmax(values: np.array) -> np.array:
result = values / sum(values)
@@ -13,17 +16,18 @@ def sample(values: np.array):
probs = softmax(values + 1.0E-6)
z = uniform()
print(f"Probs={probs}")
print(f"Z={z}")
result = None
print(f"Z={z:.3f}")
index = None
p_sum = 0
for idx, p in enumerate(probs):
p_sum += p
if z <= p_sum:
result = idx
index = idx
break
is_exploration = result != (len(probs) - 1)
return result, is_exploration
probs_max = np.max(probs)
is_exploration = probs[index] < probs_max
return index, is_exploration
def test_sample():
@@ -37,15 +41,31 @@ def test_sample():
print(c)
def to_state_string(state):
state_str = ''
for i in range(0, 3):
for j in range(0, 3):
char = state[3*i+j]
def to_state_string(state, state_nex=None):
sp = ' '
sp_arrow = ' => '
sp_ = [sp, sp_arrow, sp]
def col(str_in, state):
str_out = str_in
for c in range(0, 3):
char = state[3*r+c]
if char == '-':
char = ' '
state_str += "|"+char
state_str += '|\x0A'
str_out += "|" + char
str_out += '|'
return str_out
state_str = ''
for r in range(0, 3):
state_str = col(state_str, state)
if state_nex is not None:
state_str += sp_[r]
state_str = col(state_str, state_nex)
if r != 2:
state_str += '\x0A'
return state_str
@@ -93,7 +113,7 @@ class Player(object):
index, is_exp = sample(values)
field = moves[index]
print(f"{player.mark}: Chose {index}")
print(f"{player.mark}: Chose {index}, is_exp={is_exp}")
state_next = self.state_from_move(state, field)
# Learn
@@ -103,7 +123,7 @@ class Player(object):
d = max(0, v1-v0)
if d > 0:
self.set_value(0.1*d)
print(f"{player.mark}: Learned {d}")
print(f"{player.mark}: Learned {d:0.3f}")
self.state_last = state_next
return state_next, can_move
@@ -153,7 +173,7 @@ p_x = Player(mark='X')
p_o = Player(mark='O')
for k in range(0, 10000):
for k in range(0, 1000):
# Wer fängt an?
if uniform() < 0.5:
players = [p_x, p_o]
@@ -162,12 +182,14 @@ for k in range(0, 10000):
state = "---------"
move = 1
run = True
last_state = None
while run:
last_state = state
for player in players:
print(to_state_string(state))
print(f"---------------------------------------------------")
print(f"- Game {k:06d}, Move {move} -----------------------------")
print(f"---------------------------------------------------")
last_state = state
state, has_moved = player.move(state)
print(to_state_string(last_state, state))
if not has_moved:
print(f"{player.mark}: No more moves")
@@ -175,12 +197,10 @@ for k in range(0, 10000):
if player.has_won(state):
print(f"{player.mark}: Has won the game")
player.set_value(1.0)
print(to_state_string(state))
run = False
if not run:
break
move += 1
for player in players:
print(f"{player.mark}: Values: {player.values}")