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

Commit

Permalink
refactor: move logger to internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed Oct 31, 2021
1 parent c3577fa commit b1a39dd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 116 deletions.
2 changes: 1 addition & 1 deletion counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *counter) rng(f func(key interface{}, count float64)) {
func interface2key(i interface{}) interface{} {
rt := reflect.TypeOf(i)
if rt.Kind() == reflect.Ptr {
return fmt.Sprintf("%v", i)
return fmt.Sprintf("%#v", i)
}
return i
}
2 changes: 1 addition & 1 deletion examples/nim/nim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestNim(t *testing.T) {
playerToMove: 1,
chips: chips,
}
move := mcts.ComputeMove(state, mcts.MaxIterations(100000), mcts.Verbose(true))
move := mcts.ComputeMove(state, mcts.MaxIterations(100000))
assert.Equal(t, chips%4, move)
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/tictactoe/tictactoe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestTicTacToe(t *testing.T) {
{0, 0, 0},
},
}
mctsMove := mcts.ComputeMove(rootState, mcts.MaxIterations(20000), mcts.Verbose(true))
mctsMove := mcts.ComputeMove(rootState, mcts.MaxIterations(20000))
m := mctsMove.(move)
assert.Equal(t, 1, m.x)
assert.Equal(t, 1, m.y)
Expand All @@ -34,7 +34,7 @@ func TestTicTacToe(t *testing.T) {
{0, -1, 0},
},
}
mctsMove = mcts.ComputeMove(rootState, mcts.Verbose(true))
mctsMove = mcts.ComputeMove(rootState, mcts.MaxIterations(20000))
m = mctsMove.(move)
assert.Equal(t, 1, m.v)

Expand Down
9 changes: 7 additions & 2 deletions logger.go → internal/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package mcts
package log

import (
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -13,11 +15,14 @@ var (
defaultLogger Logger
)

// Init logger with level
func init() {
// TODO: parse env config and add file log
cfg := zap.NewDevelopmentConfig()
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
cfg.EncoderConfig.EncodeCaller = nil
if _, ok := os.LookupEnv("GO_MCTS_DEBUG"); !ok {
cfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
}
zapLogger, _ := cfg.Build()
defaultLogger = zapLogger.Sugar()
}
Expand Down
4 changes: 2 additions & 2 deletions mcts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ type testGameMove int

func TestMCTS(t *testing.T) {
state := newTestGameState(1)
move := ComputeMove(state, Verbose(true))
move := ComputeMove(state)
assert.Equal(t, 2, move)

state = newTestGameState(2)
move = ComputeMove(state, Verbose(true))
move = ComputeMove(state)
assert.Equal(t, 1, move)
}
9 changes: 0 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ type Options struct {
Goroutines int
MaxIterations int
MaxTime time.Duration
Verbose bool
}

var defaultOptions = Options{
Goroutines: runtime.NumCPU(),
MaxIterations: 10000,
MaxTime: -1,
Verbose: false,
}

type Option func(*Options)
Expand Down Expand Up @@ -49,13 +47,6 @@ func MaxTime(d time.Duration) Option {
}
}

// Verbose print details log, default is false
func Verbose(v bool) Option {
return func(o *Options) {
o.Verbose = v
}
}

func newOptions(opts ...Option) Options {
options := defaultOptions

Expand Down
68 changes: 0 additions & 68 deletions options_test.go

This file was deleted.

58 changes: 27 additions & 31 deletions uct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package mcts
import (
"math/rand"
"time"

"github.com/go-mcts/mcts/internal/log"
)

func computeTree(rootState State, rd *rand.Rand, opts ...Option) *node {
Expand Down Expand Up @@ -48,16 +50,14 @@ func computeTree(rootState State, rd *rand.Rand, opts ...Option) *node {
node = node.parent
}

if options.Verbose || options.MaxTime >= 0 {
now := time.Now()
if options.Verbose && (now.Sub(printTime) >= time.Second || i == options.MaxIterations) {
Debugf("%d games played (%.2f / second).", i, float64(i)/now.Sub(startTime).Seconds())
printTime = now
}
now := time.Now()
if now.Sub(printTime) >= time.Second || i == options.MaxIterations {
log.Debugf("%d games played (%.2f / second).", i, float64(i)/now.Sub(startTime).Seconds())
printTime = now
}

if options.MaxTime >= 0 && now.Sub(startTime) >= options.MaxTime {
break
}
if options.MaxTime >= 0 && now.Sub(startTime) >= options.MaxTime {
break
}
}

Expand Down Expand Up @@ -114,29 +114,25 @@ func ComputeMove(rootState State, opts ...Option) Move {
bestScore = expectedSuccessRate
}

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

if options.Verbose {
bestWins := wins.get(bestMove)
bestVisits := visits.get(bestMove)
Debugf("Best: %v (%2d%% visits) (%2d%% wins)",
bestMove,
int(100.0*bestVisits/float64(gamePlayed)+0.5),
int(100.0*bestWins/bestVisits+0.5),
)

now := time.Now()
Debugf(
"%d games played in %.2f s. (%.2f / second, %d parallel jobs).",
gamePlayed,
now.Sub(startTime).Seconds(),
float64(gamePlayed)/now.Sub(startTime).Seconds(),
options.Goroutines,
)
}
bestWins := wins.get(bestMove)
bestVisits := visits.get(bestMove)
log.Infof("Best: %v (%2d%% visits) (%2d%% wins)",
bestMove,
int(100.0*bestVisits/float64(gamePlayed)+0.5),
int(100.0*bestWins/bestVisits+0.5),
)

now := time.Now()
log.Infof(
"%d games played in %.2f s. (%.2f / second, %d parallel jobs).",
gamePlayed,
now.Sub(startTime).Seconds(),
float64(gamePlayed)/now.Sub(startTime).Seconds(),
options.Goroutines,
)
return bestMove
}

0 comments on commit b1a39dd

Please sign in to comment.