Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit b1a39dd

Browse files
committed
refactor: move logger to internal package
1 parent c3577fa commit b1a39dd

File tree

8 files changed

+40
-116
lines changed

8 files changed

+40
-116
lines changed

counter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (c *counter) rng(f func(key interface{}, count float64)) {
5151
func interface2key(i interface{}) interface{} {
5252
rt := reflect.TypeOf(i)
5353
if rt.Kind() == reflect.Ptr {
54-
return fmt.Sprintf("%v", i)
54+
return fmt.Sprintf("%#v", i)
5555
}
5656
return i
5757
}

examples/nim/nim_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestNim(t *testing.T) {
1818
playerToMove: 1,
1919
chips: chips,
2020
}
21-
move := mcts.ComputeMove(state, mcts.MaxIterations(100000), mcts.Verbose(true))
21+
move := mcts.ComputeMove(state, mcts.MaxIterations(100000))
2222
assert.Equal(t, chips%4, move)
2323
}
2424
}

examples/tictactoe/tictactoe_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestTicTacToe(t *testing.T) {
2020
{0, 0, 0},
2121
},
2222
}
23-
mctsMove := mcts.ComputeMove(rootState, mcts.MaxIterations(20000), mcts.Verbose(true))
23+
mctsMove := mcts.ComputeMove(rootState, mcts.MaxIterations(20000))
2424
m := mctsMove.(move)
2525
assert.Equal(t, 1, m.x)
2626
assert.Equal(t, 1, m.y)
@@ -34,7 +34,7 @@ func TestTicTacToe(t *testing.T) {
3434
{0, -1, 0},
3535
},
3636
}
37-
mctsMove = mcts.ComputeMove(rootState, mcts.Verbose(true))
37+
mctsMove = mcts.ComputeMove(rootState, mcts.MaxIterations(20000))
3838
m = mctsMove.(move)
3939
assert.Equal(t, 1, m.v)
4040

logger.go renamed to internal/log/logger.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package mcts
5+
package log
66

77
import (
8+
"os"
9+
810
"go.uber.org/zap"
911
"go.uber.org/zap/zapcore"
1012
)
@@ -13,11 +15,14 @@ var (
1315
defaultLogger Logger
1416
)
1517

18+
// Init logger with level
1619
func init() {
17-
// TODO: parse env config and add file log
1820
cfg := zap.NewDevelopmentConfig()
1921
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
2022
cfg.EncoderConfig.EncodeCaller = nil
23+
if _, ok := os.LookupEnv("GO_MCTS_DEBUG"); !ok {
24+
cfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
25+
}
2126
zapLogger, _ := cfg.Build()
2227
defaultLogger = zapLogger.Sugar()
2328
}

mcts_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ type testGameMove int
122122

123123
func TestMCTS(t *testing.T) {
124124
state := newTestGameState(1)
125-
move := ComputeMove(state, Verbose(true))
125+
move := ComputeMove(state)
126126
assert.Equal(t, 2, move)
127127

128128
state = newTestGameState(2)
129-
move = ComputeMove(state, Verbose(true))
129+
move = ComputeMove(state)
130130
assert.Equal(t, 1, move)
131131
}

options.go

-9
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ type Options struct {
1414
Goroutines int
1515
MaxIterations int
1616
MaxTime time.Duration
17-
Verbose bool
1817
}
1918

2019
var defaultOptions = Options{
2120
Goroutines: runtime.NumCPU(),
2221
MaxIterations: 10000,
2322
MaxTime: -1,
24-
Verbose: false,
2523
}
2624

2725
type Option func(*Options)
@@ -49,13 +47,6 @@ func MaxTime(d time.Duration) Option {
4947
}
5048
}
5149

52-
// Verbose print details log, default is false
53-
func Verbose(v bool) Option {
54-
return func(o *Options) {
55-
o.Verbose = v
56-
}
57-
}
58-
5950
func newOptions(opts ...Option) Options {
6051
options := defaultOptions
6152

options_test.go

-68
This file was deleted.

uct.go

+27-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package mcts
77
import (
88
"math/rand"
99
"time"
10+
11+
"github.com/go-mcts/mcts/internal/log"
1012
)
1113

1214
func computeTree(rootState State, rd *rand.Rand, opts ...Option) *node {
@@ -48,16 +50,14 @@ func computeTree(rootState State, rd *rand.Rand, opts ...Option) *node {
4850
node = node.parent
4951
}
5052

51-
if options.Verbose || options.MaxTime >= 0 {
52-
now := time.Now()
53-
if options.Verbose && (now.Sub(printTime) >= time.Second || i == options.MaxIterations) {
54-
Debugf("%d games played (%.2f / second).", i, float64(i)/now.Sub(startTime).Seconds())
55-
printTime = now
56-
}
53+
now := time.Now()
54+
if now.Sub(printTime) >= time.Second || i == options.MaxIterations {
55+
log.Debugf("%d games played (%.2f / second).", i, float64(i)/now.Sub(startTime).Seconds())
56+
printTime = now
57+
}
5758

58-
if options.MaxTime >= 0 && now.Sub(startTime) >= options.MaxTime {
59-
break
60-
}
59+
if options.MaxTime >= 0 && now.Sub(startTime) >= options.MaxTime {
60+
break
6161
}
6262
}
6363

@@ -114,29 +114,25 @@ func ComputeMove(rootState State, opts ...Option) Move {
114114
bestScore = expectedSuccessRate
115115
}
116116

117-
if options.Verbose {
118-
Debugf("Move: %v (%2d%% visits) (%2d%% wins)",
119-
move, int(100.0*v/float64(gamePlayed)+0.5), int(100.0*w/v+0.5))
120-
}
117+
log.Debugf("Move: %v (%2d%% visits) (%2d%% wins)",
118+
move, int(100.0*v/float64(gamePlayed)+0.5), int(100.0*w/v+0.5))
121119
})
122120

123-
if options.Verbose {
124-
bestWins := wins.get(bestMove)
125-
bestVisits := visits.get(bestMove)
126-
Debugf("Best: %v (%2d%% visits) (%2d%% wins)",
127-
bestMove,
128-
int(100.0*bestVisits/float64(gamePlayed)+0.5),
129-
int(100.0*bestWins/bestVisits+0.5),
130-
)
131-
132-
now := time.Now()
133-
Debugf(
134-
"%d games played in %.2f s. (%.2f / second, %d parallel jobs).",
135-
gamePlayed,
136-
now.Sub(startTime).Seconds(),
137-
float64(gamePlayed)/now.Sub(startTime).Seconds(),
138-
options.Goroutines,
139-
)
140-
}
121+
bestWins := wins.get(bestMove)
122+
bestVisits := visits.get(bestMove)
123+
log.Infof("Best: %v (%2d%% visits) (%2d%% wins)",
124+
bestMove,
125+
int(100.0*bestVisits/float64(gamePlayed)+0.5),
126+
int(100.0*bestWins/bestVisits+0.5),
127+
)
128+
129+
now := time.Now()
130+
log.Infof(
131+
"%d games played in %.2f s. (%.2f / second, %d parallel jobs).",
132+
gamePlayed,
133+
now.Sub(startTime).Seconds(),
134+
float64(gamePlayed)/now.Sub(startTime).Seconds(),
135+
options.Goroutines,
136+
)
141137
return bestMove
142138
}

0 commit comments

Comments
 (0)