-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (132 loc) · 3.2 KB
/
main.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"log"
"tictactoe/models"
"tictactoe/models/board"
"tictactoe/models/player"
)
func getValueDefault(board *board.Board, mp map[string]float64, c *int) {
done := board.IsDone()
decisive := board.IsDecisive()
mp[board.String()] = 0.5
if done && !decisive {
*c = *c + 1
mp[board.String()] = 0.5
}
if decisive {
*c += 1
res := board.GetResult()
if res.Winner == models.X {
mp[board.String()] = 1.0
} else {
mp[board.String()] = 0.0
}
}
moves := board.GetAllMoves()
for _, mv := range moves {
board.Apply(mv)
getValueDefault(board, mp, c)
board.RevertLastMove()
}
}
func main() {
board := &board.Board{}
board.Reset()
valFn := map[string]float64{}
c := 0
getValueDefault(board, valFn, &c)
logger := log.Default()
agentLogger := log.Default()
agentLogger.SetPrefix("Agent")
agentX := player.NewAgent(valFn, &player.AgentParameters{
Mode: player.LearnMode,
ExploratoryProbability: 0.1,
LearningRate: 0.01,
}, agentLogger, models.X)
agentO := player.NewAgent(valFn, &player.AgentParameters{
Mode: player.LearnMode,
ExploratoryProbability: 0.1,
LearningRate: 0.1,
}, agentLogger, models.O)
trainEpisodes := 10000
for i := 0; i < trainEpisodes; i++ {
board.Reset()
for !board.IsDone() && !board.IsDecisive() {
turn := board.GetTurn()
if turn == models.X {
mv := agentX.Move(*board)
ok := board.Check(mv)
if !ok {
logger.Println("agentX gave an illegal move")
break
}
board.Apply(mv)
} else {
mv := agentO.Move(*board)
ok := board.Check(mv)
if !ok {
logger.Println("agentO gave an illegal move")
break
}
board.Apply(mv)
}
}
// board.PrintHistory()
if i%100 == 0 || i == trainEpisodes-1 {
board.PrintHistory()
}
}
trainEpisodes = 10000
fmt.Println(c)
agentX = player.NewAgent(valFn, &player.AgentParameters{
Mode: player.PlayMode,
ExploratoryProbability: 0.0,
LearningRate: 0.01,
}, agentLogger, models.X)
agentO = player.NewAgent(valFn, &player.AgentParameters{
Mode: player.PlayMode,
ExploratoryProbability: 0.0,
LearningRate: 0.01,
}, agentLogger, models.O)
for i := 0; i < trainEpisodes; i++ {
board.Reset()
for !board.IsDone() && !board.IsDecisive() {
turn := board.GetTurn()
if turn == models.X {
mv := agentX.Move(*board)
ok := board.Check(mv)
if !ok {
logger.Println("agentX gave an illegal move")
break
}
board.Apply(mv)
board.Print()
fmt.Printf("value of board: %f\n", valFn[board.String()])
} else {
fmt.Print("Enter move(r c): ")
mv := getUserMove(board)
board.Apply(mv)
board.Print()
fmt.Printf("value of board: %f\n", valFn[board.String()])
}
}
res := board.GetResult()
if res.Draw == "draw" {
fmt.Printf("Game drawn\n")
} else {
fmt.Printf("Winner is %s\n", res.Winner.String())
}
}
}
func getUserMove(b *board.Board) models.Move {
var x int
var y int
fmt.Scanf("%d", &x)
fmt.Scanf("%d", &y)
for !b.Check(models.Move{X: x, Y: y, XO: models.O}) {
fmt.Scanf("%d", &x)
fmt.Scanf("%d", &y)
}
return models.Move{X: x, Y: y, XO: models.O}
}