Skip to content

Commit

Permalink
tune
Browse files Browse the repository at this point in the history
  • Loading branch information
ChizhovVadim committed Oct 22, 2018
1 parent ca12dd7 commit c3549eb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 104 deletions.
145 changes: 82 additions & 63 deletions engine/evaluation.go
Expand Up @@ -48,30 +48,28 @@ func (s score) Neg() score {
}

type EvaluationService struct {
TraceEnabled bool
experimentSettings bool
materialPawn score
materialKnight score
materialBishop score
materialRook score
materialQueen score
bishopPair score
knightMobility [1 + 8]score
bishopMobility [1 + 13]score
rookMobility [1 + 14]score
queenMobility [1 + 27]score
rook7Th score
rookOpen score
rookSemiopen score
pawnIsolated score
pawnDoubled score
pawnDoubledIsolated score
pawnConnected score
kingShelter int
kingAttack int
threat score
sideToMove score
pst pst
TraceEnabled bool
experimentSettings bool
materialPawn score
materialMinor score
materialMajor score
materialMajorOverMinor score
material2MinorOverMajor score
materialBishopPair score
knightMobility [1 + 8]score
bishopMobility [1 + 13]score
rookMobility [1 + 14]score
queenMobility [1 + 27]score
rook7Th score
rookOpen score
rookSemiopen score
pawnIsolated score
pawnDoubled score
pawnDoubledIsolated score
pawnConnected score
threat score
sideToMove score
pst pst
}

type pst struct {
Expand All @@ -85,8 +83,8 @@ func NewEvaluationService() *EvaluationService {
}

func (srv *EvaluationService) DefaultParams() []int {
// Error: 0.042270
return []int{108, 356, 330, 384, 349, 519, 633, 924, 1297, 27, 35, 75, 0, 23, 15, 0, 19, 39, 0, 15, 7, -9, 0, 0, 0, -6, -2, 8, 14, 9, 11, 9, 10, 7, 4, 0, 1, 9, 8, 6, 0, 7, 6, 3, 7, 2, 6}
// Error: 0.060010 (generated by tune.go)
return []int{64, 54, 99, 0, 30, 15, 0, 29, 50, 0, 18, 15, -12, -3, 0, 0, -12, -11, 10, 6, 10, 7, 10, 5, 0, 6, 10, 9, 10, 0, 9, 5, 3, 11, 2, 0}
}

type evalValues struct {
Expand All @@ -111,16 +109,6 @@ func (ev *evalValues) NextScore() score {
return score{ev.Next(), ev.Next()}
}

func limitValue(v, lower, upper int) int {
if v < lower {
return lower
}
if upper < v {
return upper
}
return v
}

func absInt(x int) int {
if x < 0 {
return -x
Expand All @@ -146,7 +134,7 @@ func regularizationSlice(source []score) int {

// should be coordinated with method Init
func (e *EvaluationService) Regularization() int {
return regularizationScore(e.bishopPair) +
return regularizationScore(e.materialBishopPair) +
regularizationScore(e.sideToMove) +
regularizationScore(e.threat) +
regularizationScore(e.rook7Th) +
Expand All @@ -156,20 +144,14 @@ func (e *EvaluationService) Regularization() int {
regularizationScore(e.pawnConnected) +
regularizationScore(e.pawnDoubled) +
regularizationScore(e.pawnDoubledIsolated) +
//regularizationScore(e.kingShelter) +
regularizationSlice(e.knightMobility[:]) +
regularizationSlice(e.bishopMobility[:]) +
regularizationSlice(e.rookMobility[:]) +
regularizationSlice(e.queenMobility[:]) +
regularizationSlice(e.pst.wn[:]) +
regularizationSlice(e.pst.wb[:]) +
regularizationSlice(e.pst.wq[:]) +
regularizationSlice(e.pst.wk[:]) +
absInt(e.materialPawn.opening) +
regularizationScore(e.materialKnight) +
regularizationScore(e.materialBishop) +
regularizationScore(e.materialRook) +
regularizationScore(e.materialQueen)
regularizationSlice(e.pst.wk[:])
}

var (
Expand All @@ -184,13 +166,14 @@ var (
func (e *EvaluationService) Init(params []int) []int {
var ev = evalValues{params, 0}

e.materialPawn = score{ev.NextWithDefault(100), 100}
e.materialKnight = score{ev.NextWithDefault(325), ev.NextWithDefault(325)}
e.materialBishop = score{ev.NextWithDefault(325), ev.NextWithDefault(325)}
e.materialRook = score{ev.NextWithDefault(500), ev.NextWithDefault(500)}
e.materialQueen = score{ev.NextWithDefault(1000), ev.NextWithDefault(1000)}
e.materialPawn = score{100, 100}
e.materialMinor = score{400, 350}
e.materialMajor = score{500, 500}
e.materialMajorOverMinor = score{200, 200}
e.material2MinorOverMajor = score{200, 100}

e.materialBishopPair = ev.NextScore()

e.bishopPair = ev.NextScore()
e.threat = ev.NextScore()
e.sideToMove = ev.NextScore()
e.rook7Th = ev.NextScore()
Expand All @@ -200,8 +183,6 @@ func (e *EvaluationService) Init(params []int) []int {
e.pawnDoubled = ev.NextScore()
e.pawnDoubledIsolated = ev.NextScore()
e.pawnConnected = ev.NextScore()
e.kingShelter = ev.Next()
e.kingAttack = 25 * ev.Next()

var knightCenter = ev.NextScore()
var bishopCenter = ev.NextScore()
Expand All @@ -219,6 +200,9 @@ func (e *EvaluationService) Init(params []int) []int {
kingCenter.endgame * (kingLine[f] + kingLine[r]),
}
}
centerScores(e.pst.wn[:], e.pst.wn[SquareG3])
centerScores(e.pst.wb[:], e.pst.wb[SquareE2])
centerScores(e.pst.wq[:], e.pst.wq[SquareE2])
e.pst.initBlack()

initMobility(e.knightMobility[:], ev.NextScore())
Expand All @@ -233,7 +217,13 @@ func initMobility(source []score, s score) {
var total = s.multK(float64(len(source) - 1))
for i := range source {
var x = math.Sqrt(float64(i) / float64(len(source)-1))
source[i] = total.multK(x)
source[i] = total.multK(x - 0.5)
}
}

func centerScores(source []score, mean score) {
for i := range source {
source[i].Sub(mean)
}
}

Expand Down Expand Up @@ -480,27 +470,56 @@ func (e *EvaluationService) Evaluate(p *Position) int {

if (bq > 0 && br+bn+bb > 0) ||
(br > 1 && bn+bb > 1) {
kingScore.opening -= e.kingShelter*shelterWKingSquare(p, wkingSq) +
e.kingAttack*battackTot*Max(1, battackTot-5)/150
kingScore.opening -= 15*shelterWKingSquare(p, wkingSq) +
2*battackTot*Max(1, battackTot-5)
}

if (wq > 0 && wr+wn+wb > 0) ||
(wr > 1 && wn+wb > 1) {
kingScore.opening += e.kingShelter*shelterBKingSquare(p, bkingSq) +
e.kingAttack*wattackTot*Max(1, wattackTot-5)/150
kingScore.opening += 15*shelterBKingSquare(p, bkingSq) +
2*wattackTot*Max(1, wattackTot-5)
}

var materialScore score
materialScore.AddN(e.materialPawn, wp-bp)
materialScore.AddN(e.materialKnight, wn-bn)
materialScore.AddN(e.materialBishop, wb-bb)
materialScore.AddN(e.materialRook, wr-br)
materialScore.AddN(e.materialQueen, wq-bq)

var minors = wn - bn + wb - bb
var majors = wr - br + 2*(wq-bq)
for majors != 0 || minors != 0 {
if majors >= 2 && minors <= -3 {
majors -= 2
minors += 3
} else if majors <= -2 && minors >= 3 {
majors += 2
minors -= 3
} else if minors >= 2 && majors <= -1 {
materialScore.Add(e.material2MinorOverMajor)
minors -= 2
majors++
} else if minors <= -2 && majors >= 1 {
materialScore.Sub(e.material2MinorOverMajor)
minors += 2
majors--
} else if majors >= 1 && minors <= -1 {
materialScore.Add(e.materialMajorOverMinor)
majors--
minors++
} else if majors <= -1 && minors >= 1 {
materialScore.Sub(e.materialMajorOverMinor)
majors++
minors--
} else {
materialScore.AddN(e.materialMajor, majors)
materialScore.AddN(e.materialMinor, minors)
break
}
}

if wb >= 2 {
materialScore.Add(e.bishopPair)
materialScore.Add(e.materialBishopPair)
}
if bb >= 2 {
materialScore.Sub(e.bishopPair)
materialScore.Sub(e.materialBishopPair)
}

var total = pawnScore
Expand Down
43 changes: 2 additions & 41 deletions learn/tune.go
Expand Up @@ -32,18 +32,6 @@ func RunTuning() {
log.Println("Tune finished.")
}

func ShowError() {
var entries, err = readTuningFile("/home/vadim/chess/tuner/quiet-labeled.epd")
if err != nil {
log.Println(err)
return
}
var _, errorFunc = newErrorFunc(entries)
var es = engine.NewEvaluationService()
var e = errorFunc(es.DefaultParams())
log.Println("Error:", e)
}

func readTuningFile(filePath string) ([]tuningEntry, error) {
file, err := os.Open(filePath)
if err != nil {
Expand Down Expand Up @@ -132,7 +120,7 @@ func tuneEvalService(params []int, errorFunc func([]int) float64) {
break
}
}
fmt.Printf("// Error: %.6f\n", bestE)
fmt.Printf("// Error: %.6f (generated by tune.go)\n", bestE)
fmt.Printf("return %#v\n", params)
}

Expand Down Expand Up @@ -165,12 +153,7 @@ func newErrorFunc(entries []tuningEntry) (params []int, errorFunc func([]int) fl
if !entry.p.WhiteMove {
score = -score
}
var expected = mixProbability(
float64(entry.score),
sigmoid(evalMaterial(&entry.p)),
0.25,
0.5)
var diff = sigmoid(score) - expected
var diff = sigmoid(score) - float64(entry.score)
localSum += diff * diff
}
sums[thread] = localSum
Expand All @@ -189,25 +172,3 @@ func newErrorFunc(entries []tuningEntry) (params []int, errorFunc func([]int) fl
func sigmoid(s int) float64 {
return 1.0 / (1.0 + math.Exp(-float64(s)/150))
}

func evalMaterial(p *common.Position) int {
return 100*(common.PopCount(p.Pawns&p.White)-common.PopCount(p.Pawns&p.Black)) +
325*(common.PopCount(p.Knights&p.White)-common.PopCount(p.Knights&p.Black)) +
325*(common.PopCount(p.Bishops&p.White)-common.PopCount(p.Bishops&p.Black)) +
500*(common.PopCount(p.Rooks&p.White)-common.PopCount(p.Rooks&p.Black)) +
1000*(common.PopCount(p.Queens&p.White)-common.PopCount(p.Queens&p.Black))
}

func limitValue(v, lower, upper float64) float64 {
if v < lower {
return lower
}
if v > upper {
return upper
}
return v
}

func mixProbability(mainProb, materialProb, materialFree, materialWeight float64) float64 {
return mainProb + materialWeight*(limitValue(mainProb, materialProb-materialFree, materialProb+materialFree)-mainProb)
}

0 comments on commit c3549eb

Please sign in to comment.