Skip to content

Commit

Permalink
Added benchmarks
Browse files Browse the repository at this point in the history
Ordered move generation from high value pieces to lower value ones, this increases the speed drammatically allowing the search to reach depth 6 in ~4 seconds on the initial position

Change-type: minor
Signed-off-by: Giovanni Garufi <[email protected]>
  • Loading branch information
nazrhom committed Jan 26, 2021
1 parent 8812315 commit 373cda0
Show file tree
Hide file tree
Showing 6 changed files with 857 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# hask-chess

## Notes

Changing order of generated moves starting from king, queen and most valueble pieces instead of the other way around has the following huge speedup

```
benchmarking initial position/depth: 5 White
time 3.446 s (3.296 s .. 3.554 s)
1.000 R² (1.000 R² .. 1.000 R²)
mean 3.436 s (3.397 s .. 3.467 s)
std dev 40.69 ms (17.53 ms .. 54.18 ms)
variance introduced by outliers: 19% (moderately inflated)
```
to

```
benchmarking initial position/depth: 5 White
time 470.2 ms (464.1 ms .. 475.9 ms)
1.000 R² (1.000 R² .. 1.000 R²)
mean 469.3 ms (466.8 ms .. 470.5 ms)
std dev 2.301 ms (89.80 μs .. 2.860 ms)
variance introduced by outliers: 19% (moderately inflated)
```
705 changes: 705 additions & 0 deletions bench.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions bench/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Criterion.Main

import Fen
import Board
import GameTree

main = defaultMain [
bgroup "initial position" [
bench "depth: 2 White" $ whnf (negamax initialGameState White) 2
, bench "depth: 5 White" $ whnf (negamax initialGameState White) 5
, bench "depth: 6 White" $ whnf (negamax initialGameState White) 6
]
]
98 changes: 98 additions & 0 deletions hen.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.33.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 428cef491c676a221e29e7cb0aa5e5191422d48e704d833f35dbe1a4712f85d3

name: hen
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/hask-chess#readme>
homepage: https://github.com/nazrhom/hen#readme
bug-reports: https://github.com/nazrhom/hen/issues
author: Giovanni Garufi
maintainer: [email protected]
copyright: 2021 Giovanni Garufi
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md

source-repository head
type: git
location: https://github.com/nazrhom/hen

library
exposed-modules:
Board
Evaluation
Fen
GameTree
MoveGen
Uci
other-modules:
Paths_hen
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
, containers
, mtl
, parsec
, vector
default-language: Haskell2010

executable hen-exe
main-is: Main.hs
other-modules:
Paths_hen
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, hen
, mtl
, parsec
, vector
default-language: Haskell2010

test-suite hen-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_hen
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
HUnit
, base >=4.7 && <5
, containers
, hen
, mtl
, parsec
, vector
default-language: Haskell2010

benchmark hen-bench
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_hen
hs-source-dirs:
bench
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, criterion
, hen
, mtl
, parsec
, vector
default-language: Haskell2010
14 changes: 13 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies:
- mtl
- parsec
- containers
- HUnit

library:
source-dirs: src
Expand All @@ -51,3 +50,16 @@ tests:
- -with-rtsopts=-N
dependencies:
- hen
- HUnit

benchmarks:
hen-bench:
main: Spec.hs
source-dirs: bench
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- hen
- criterion
10 changes: 5 additions & 5 deletions src/MoveGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ knightMap = foldl (\m k -> M.insert k (go k m) m) M.empty [0..63]

genMoves :: GameState -> Colour -> [Move]
genMoves gs col =
genAllPawnMoves gs col
++ genAllKnightMoves gs col
++ genAllBishopMoves gs col
++ genAllRookMoves gs col
genAllKingMoves gs col
++ genAllQueenMoves gs col
++ genAllKingMoves gs col
++ genAllRookMoves gs col
++ genAllBishopMoves gs col
++ genAllKnightMoves gs col
++ genAllPawnMoves gs col

applyAll :: GameState -> [Move] -> [GameState]
applyAll gs moves = map (apply gs) moves
Expand Down

0 comments on commit 373cda0

Please sign in to comment.