Skip to content

Commit

Permalink
Tuning king psqt (#175)
Browse files Browse the repository at this point in the history
Bench | 7145768

ELO | 4.95 +- 5.55 (95%)
SPRT | 10.0+0.05s Threads=1 Hash=8MB
LLR | 2.21 (-2.20, 2.20) [-1.50, 4.50]
Games | N: 8000 W: 2184 L: 2070 D: 3746

ELO | 2.51 +- 3.25 (95%)
SPRT | 60.0+0.3s Threads=1 Hash=64MB
LLR | 2.20 (-2.20, 2.20) [-1.50, 4.50]
Games | N: 18810 W: 4119 L: 3983 D: 10708
  • Loading branch information
ratosh authored Jun 21, 2019
1 parent c3a4833 commit 38726d4
Show file tree
Hide file tree
Showing 19 changed files with 192 additions and 94 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group=ratosh
version=3.0.6
version=3.0.7
kotlin_version=1.3.0
detekt_version=1.0.0.RC6-3
48 changes: 28 additions & 20 deletions pirarucu-common/src/main/kotlin/pirarucu/move/Move.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,50 +121,58 @@ object Move {
.replace("0", "o").replace("O", "o")

// Fixing castle string
if (move == "oo") {
val castlingIndex = CastlingRights.getCastlingRightIndex(board.colorToMove, CastlingRights.KING_SIDE)
move = Square.toString(board.kingSquare[board.colorToMove]) +
Square.toString(CastlingRights.KING_FINAL_SQUARE[castlingIndex])
} else if (move == "ooo") {
val castlingIndex = CastlingRights.getCastlingRightIndex(board.colorToMove, CastlingRights.QUEEN_SIDE)
move = Square.toString(board.kingSquare[board.colorToMove]) +
Square.toString(CastlingRights.KING_FINAL_SQUARE[castlingIndex])
} else {
when (move.length) {
when (move) {
"oo" -> {
val castlingIndex = CastlingRights.getCastlingRightIndex(board.colorToMove, CastlingRights.KING_SIDE)
move = Square.toString(board.kingSquare[board.colorToMove]) +
Square.toString(CastlingRights.KING_FINAL_SQUARE[castlingIndex])
}
"ooo" -> {
val castlingIndex = CastlingRights.getCastlingRightIndex(board.colorToMove, CastlingRights.QUEEN_SIDE)
move = Square.toString(board.kingSquare[board.colorToMove]) +
Square.toString(CastlingRights.KING_FINAL_SQUARE[castlingIndex])
}
else -> when (move.length) {
2 -> {
move = Square.toString(Move.getFromSquare(ourMove)) + move
move = Square.toString(getFromSquare(ourMove)) + move
}
3 -> {
val piece = Piece.getPiece(move[0])
if (board.pieceTypeBoard[Move.getFromSquare(ourMove)] != piece) {
return false
if (board.pieceTypeBoard[getFromSquare(ourMove)] == Piece.PAWN) {
if (File.toString(File.getFile(getFromSquare(ourMove))) != move[0]) {
return false
}
} else {
val piece = Piece.getPiece(move[0])
if (board.pieceTypeBoard[getFromSquare(ourMove)] != piece) {
return false
}
}
move = Square.toString(Move.getFromSquare(ourMove)) + move.substring(1, move.length)
move = Square.toString(getFromSquare(ourMove)) + move.substring(1, move.length)
}
4 -> {
val piece = Piece.getPiece(move[0])
if (board.pieceTypeBoard[Move.getFromSquare(ourMove)] != piece) {
if (board.pieceTypeBoard[getFromSquare(ourMove)] != piece) {
return false
}
val file = File.getFile(move[1])
if (file != File.INVALID) {
if (File.getFile(Move.getFromSquare(ourMove)) != file) {
if (File.getFile(getFromSquare(ourMove)) != file) {
return false
}
} else {
val rank = Rank.getRank(move[1])
if (rank != Rank.INVALID && Rank.getRank(Move.getFromSquare(ourMove)) != rank) {
if (rank != Rank.INVALID && Rank.getRank(getFromSquare(ourMove)) != rank) {
return false
}
}
move = Square.toString(Move.getFromSquare(ourMove)) + move.substring(2, move.length)
move = Square.toString(getFromSquare(ourMove)) + move.substring(2, move.length)
}
5 -> {
move = move.substring(1, move.length)
}
}
}
return Move.getMove(board, move) == ourMove
return getMove(board, move) == ourMove
}

fun getFromTo(move: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ object TunableConstants {
27, 37, 45, 53
),
intArrayOf(
-82, 124, 38, 37, 43, 78, 37, 84, 39, 137, 144, 89, -30, 4, 31, 20, -84, -35, -58, -59, -51, -12, -57, -39,
23, 10, -51, -34, 32, 37, -24, 32
-23, 92, 39, 60, 24, 37, 25, 86, 35, 108, 126, 88, -34, -5, -6, -32, -89, -50, -64, -69, -55, -24, -68, -51,
18, 4, -50, -28, 34, 29, -5, 21
)
)

Expand Down Expand Up @@ -108,8 +108,8 @@ object TunableConstants {
-48, -40, -55, -70, -63, -44
),
intArrayOf(
-39, -41, -12, -32, -12, 13, 28, 8, -1, 19, 18, 6, -2, 29, 26, 22, -2, 14, 32, 33, -1, 17, 29, 29, -25, 3,
28, 24, -62, -36, -7, -27
-56, -27, -18, -16, -5, 35, 36, 18, 0, 24, 25, 32, 4, 27, 37, 41, 1, 19, 33, 39, -2, 12, 27, 25, -19, 10,
24, 17, -65, -33, -18, -27
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Benchmark {
EvalConstants.PAWN_EVAL_CACHE = false
UciOutput.silent = true

val iterator = epdFileLoader.getEpdInfoList()
val iterator = epdFileLoader.epdList
var nodeCount = 0L
val searchOptions = SearchOptions()
val transpositionTable = TranspositionTable(16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class EpdFileLoader(inputStream: InputStream) {

private val epdInfoList = mutableListOf<EpdInfo>()

val epdList : List<EpdInfo>
get() = epdInfoList

init {
try {
val scanner = Scanner(inputStream)
Expand All @@ -25,14 +28,11 @@ class EpdFileLoader(inputStream: InputStream) {
println(
String.format("Found %d good positions in %d possibilities.", epdInfoList.size, lines)
)
inputStream.close()
} catch (ex: Exception) {
ex.printStackTrace()
}
}

constructor(file: String) : this(FileInputStream(File(file)))

fun getEpdInfoList(): List<EpdInfo> {
return epdInfoList
}
}
1 change: 1 addition & 0 deletions pirarucu-jvm/src/main/kotlin/pirarucu/util/epd/EpdInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data class EpdInfo(val fenPosition: String,
val result: Double,
val comment: String?) {
var eval = 0
var error = 0.0
var valid = true

fun getMoveScore(board: Board, foundMove: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ object EpdInfoFactory {
} else {
result = 0.5
}
val c1Index = tempLine.indexOf("c1")
val c1Index = tempLine.indexOf(" c1 ")
if (c1Index >= 0) {
tempLine = tempLine.substring(0, c1Index)
}

val c0Index = tempLine.indexOf("c0")
val c0Index = tempLine.indexOf(" c0 ")
if (c0Index >= 0) {
var c0String = tempLine.substring(c0Index + 3, tempLine.length).replace("\"", "").replace(" ", "")
val c0End = c0String.indexOf(";")
Expand Down
12 changes: 12 additions & 0 deletions pirarucu-jvm/src/test/kotlin/pirarucu/move/MoveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class MoveTest {
assertTrue(Move.areMovesCompatibles(board, Move.createMove(Square.D5, Square.D6), "d6"))
}

@Test
fun testMoveCompatiblePawnCapture() {
val board = BoardFactory.getBoard("rqr3k1/4bpp1/pBbp1n1p/Pp2p3/3PPP2/2N1QNP1/1P4P1/R4R1K w - -")
assertTrue(Move.areMovesCompatibles(board, Move.createMove(Square.F4, Square.E5), "fxe5"))
}

@Test
fun testMoveCompatiblePawnCapture2() {
val board = BoardFactory.getBoard("r1qr2k1/4bp1p/2p1p1p1/2pb4/PP1P1P2/3NQ3/5BPP/R2R2K1 w - -")
assertTrue(Move.areMovesCompatibles(board, Move.createMove(Square.B4, Square.C5), "bxc5"))
}

@Test
fun testMoveCompatibleKnightMove() {
val board = BoardFactory.getBoard("5k2/8/8/3Np3/8/8/8/4K3 w - -")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pirarucu.benchmark

fun main(args: Array<String>) {
fun main() {
val speed = LongArray(1)
for (tries in 0 until speed.size) {
speed[tries] = Benchmark.runBenchmark(Benchmark.DEFAULT_BENCHMARK_DEPTH)
Expand Down
42 changes: 42 additions & 0 deletions pirarucu-util/src/main/kotlin/pirarucu/epd/EpdFileWriter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pirarucu.epd

import pirarucu.tuning.ErrorUtil
import pirarucu.util.epd.EpdInfo
import java.io.File

class EpdFileWriter(private val file: File) {

constructor(file: String) : this(File(file))

fun flush(epdInfoList: List<EpdInfo>) {
if (epdInfoList.isEmpty()) {
return
}
try {
val tempFile = createTempFile()
tempFile.printWriter().use { writer ->
file.forEachLine { line ->
val epdInfo = findPosition(line, epdInfoList)
writer.println(
when {
epdInfo != null -> "${epdInfo.fenPosition} c9 ${Math.round(ErrorUtil.calculateSigmoid(epdInfo.eval))}.0"
else -> line
}
)
}
}
check(file.delete() && tempFile.renameTo(file)) { "failed to replace file" }
} catch (ex: Exception) {
ex.printStackTrace()
}
}

private fun findPosition(line: String, epdInfoList: List<EpdInfo>): EpdInfo?? {
epdInfoList.forEach {
if (line.contains(it.fenPosition)) {
return it
}
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object TestingApplication {
searchOptions.searchTimeIncrement = 60000L
val board = BoardFactory.getBoard()
var partialScore = 0
for ((index, epdInfo) in testFile.getEpdInfoList().withIndex()) {
for ((index, epdInfo) in testFile.epdList.withIndex()) {
BoardFactory.setBoard(epdInfo.fenPosition, board)
transpositionTable.reset()
searchOptions.startControl()
Expand Down
46 changes: 9 additions & 37 deletions pirarucu-util/src/main/kotlin/pirarucu/tuning/ErrorUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pirarucu.tuning

import pirarucu.util.epd.EpdInfo


object ErrorUtil {

const val ORIGINAL_CONSTANT = 1.4
Expand All @@ -11,43 +12,14 @@ object ErrorUtil {
}

fun calculate(list: List<EpdInfo>, constant: Double = ORIGINAL_CONSTANT): Double {
var result = 0.0
for (entry in list) {
result += Math.pow(entry.result - ErrorUtil.calculateSigmoid(entry.eval, constant), 2.0)
}

return result / list.size

setError(list, constant)
return list.sumByDouble { it.error } / list.size
}

fun biggestError(list: List<EpdInfo>, size: Int = 10, constant: Double = ORIGINAL_CONSTANT): Map<EpdInfo, Double> {
val result = mutableMapOf<EpdInfo, Double>()
var smallestError = 1.0
var smallestEntry: EpdInfo? = null
for (entry in list) {
val entryError = Math.pow(entry.result - ErrorUtil.calculateSigmoid(entry.eval, constant), 2.0)
if (result.size < size) {
result[entry] = entryError
if (smallestError > entryError) {
smallestError = entryError
smallestEntry = entry
}
} else if (entryError > smallestError) {
result.remove(smallestEntry)

smallestError = entryError
smallestEntry = entry

result.forEach {
if (it.value < smallestError) {
smallestError = it.value
smallestEntry = it.key
}
}
result[entry] = entryError
}
}

return result
fun setError(list: List<EpdInfo>, constant: Double = ORIGINAL_CONSTANT) {
list.forEach { it.error = calculateError(it, constant) }
}
}

private fun calculateError(entry: EpdInfo, constant: Double) =
Math.pow(entry.result - calculateSigmoid(entry.eval, constant), 2.0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import kotlin.system.measureTimeMillis

object EvaluationErrorApplication {

private const val THREADS = 2
private const val THREADS = 6

@Throws(ExecutionException::class, InterruptedException::class)
@JvmStatic
fun main(args: Array<String>) {
val list = mutableListOf<EpdInfo>()
val zurichess = EpdFileLoader("g:\\chess\\epds\\quiet_labeled.epd")
list.addAll(zurichess.getEpdInfoList())
val fileLoader = EpdFileLoader("g:\\chess\\epds\\texel-sets\\zuri_quiet_labeled.epd")
list.addAll(fileLoader.epdList)
val epdList = InvalidPositionFilter(THREADS).filter(list)
println("Using ${epdList.size} positions")
val evaluator = EvaluationErrorEvaluator(THREADS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import kotlin.system.measureTimeMillis

object QuiescenceErrorApplication {

private const val THREADS = 2
private const val THREADS = 6

@Throws(ExecutionException::class, InterruptedException::class)
@JvmStatic
fun main(args: Array<String>) {
val list = mutableListOf<EpdInfo>()
val zurichess = EpdFileLoader("g:\\chess\\epds\\quiet_labeled.epd")
list.addAll(zurichess.getEpdInfoList())
val fileLoader = EpdFileLoader("g:\\chess\\epds\\texel-sets\\zuri_quiet_labeled.epd")
list.addAll(fileLoader.epdList)
val epdList = InvalidPositionFilter(THREADS).filter(list)
println("Using ${epdList.size} positions")
val evaluator = QuiescenceEvaluator(THREADS)
Expand Down
Loading

0 comments on commit 38726d4

Please sign in to comment.