Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 49 additions & 22 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ func Search(
stats *SearchStats,
moveInfo *SearchMoveInfo,
) SearchResult {
return SearchWithConfig(boardState, depth, stats, moveInfo, ExternalSearchConfig{}, nil)
eval := getLeafResult(boardState, stats)
return SearchWithConfig(boardState, depth, eval, stats, moveInfo, ExternalSearchConfig{}, nil)
}

func SearchWithConfig(
boardState *BoardState,
depth uint,
lastValue int,
stats *SearchStats,
moveInfo *SearchMoveInfo,
config ExternalSearchConfig,
thinkingChan chan ThinkingOutput,
) SearchResult {
startTime := time.Now()

alpha := -INFINITY
beta := INFINITY
searchConfig := SearchConfig{
isDebug: config.isDebug,
debugMoves: config.debugMoves,
Expand All @@ -103,19 +103,40 @@ func SearchWithConfig(
}
moves := make([]Move, 64*256)
scores := make([]int, len(moves))

var moveStart [64]int
score := searchAlphaBeta(boardState, stats, moveInfo,
thinkingChan,
int(depth),
0,
alpha,
beta,
searchConfig,
moves[:],
scores[:],
moveStart[:],
)
var score int

g := lastValue
upper := INFINITY
lower := -INFINITY
for lower < upper {
var beta int
if g == lower {
beta = g + 1
} else {
beta = g
}
g = searchAlphaBeta(
boardState,
stats,
moveInfo,
thinkingChan,
int(depth),
0,
beta-1,
beta,
searchConfig,
moves[:],
scores[:],
moveStart[:],
)
if g < beta {
upper = g
} else {
lower = g
}
}
score = g

result := SearchResult{}

Expand Down Expand Up @@ -144,6 +165,15 @@ func SearchWithConfig(

if shouldAbort {
close(thinkingChan)
} else if thinkingChan != nil {
sendToThinkingChannel(
boardState,
stats,
thinkingChan,
searchConfig,
result.value,
int(result.depth),
)
}

return result
Expand Down Expand Up @@ -384,11 +414,8 @@ func searchAlphaBeta(
if score > bestScore {
bestScore = score
bestMove = move
if bestScore > alpha {
if bestScore >= alpha {
currentAlpha = score
if currentDepth == 0 && thinkingChan != nil {
sendToThinkingChannel(boardState, searchStats, thinkingChan, searchConfig, bestScore, depthLeft)
}
}
}
}
Expand Down Expand Up @@ -453,7 +480,7 @@ func searchQuiescent(
// Evaluate the board to see what the position is without making any quiescent moves.
score := getLeafResult(boardState, searchStats)
if score >= beta {
return beta
return score
}
if score >= alpha {
bestScore = score
Expand Down Expand Up @@ -487,7 +514,7 @@ func searchQuiescent(
searchStats.cutoffs++
searchStats.qcutoffs++

return beta
return score
}

if score > bestScore {
Expand All @@ -498,7 +525,7 @@ func searchQuiescent(
}
}

return bestScore
return alpha
}

func getLeafResult(boardState *BoardState, searchStats *SearchStats) int {
Expand Down
6 changes: 5 additions & 1 deletion transpositiontable.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ func (entry *TranspositionEntry) String() string {
case TT_EXACT:
entryTypeAsString = "exact"
}
return fmt.Sprintf("{score=%d, depth=%d, type=%s}", entry.score, entry.depth, entryTypeAsString)
return fmt.Sprintf("%s {score=%d, depth=%d, type=%s}",
MoveToXboardString(entry.move),
entry.score,
entry.depth,
entryTypeAsString)
}

func StoreTranspositionTable(boardState *BoardState, move Move, score int, entryType int, depth int) {
Expand Down
11 changes: 10 additions & 1 deletion xboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func thinkAndChooseMove(

go func() {
var i uint = 1
var lastValue int = 0
// var res SearchResult

for {
Expand All @@ -212,7 +213,15 @@ func thinkAndChooseMove(
default:
// TODO: having to copy the board state indicates a bug somewhere
state := CopyBoardState(boardState)
result := SearchWithConfig(&state, uint(i), stats, &searchMoveInfo, config, thinkingChan)
result := SearchWithConfig(
&state,
uint(i),
lastValue,
stats,
&searchMoveInfo,
config,
thinkingChan)
lastValue = result.value
resultCh <- result
i = i + 1
}
Expand Down