Skip to content

Commit

Permalink
Merge pull request #27 from bsamseth/search-refactor
Browse files Browse the repository at this point in the history
Search Refactor & Verification

Fixes #25 and #26.

```
    Goldfish v1.9.0 2353.1 :    430 (+201,=159,-70),  65.2 %

    vs.                    :  games (   +,   =,  -),   (%) :    Diff
    Goldfish v1.7.0        :    215 ( 101,  84, 30),  66.5 :  +101.6
    Goldfish v1.8.2        :    215 ( 100,  75, 40),  64.0 :  +119.1

   # PLAYER             :  RATING  POINTS  PLAYED   (%)
   1 Goldfish v1.9.0    :  2353.1   280.5     430    65
   2 Goldfish v1.7.0    :  2251.5   877.5    1699    52
   3 Goldfish v1.7.1    :  2243.8   244.0     477    51
   4 Goldfish v1.8.0    :  2237.0   324.0     650    50
   5 Goldfish v1.8.1    :  2234.1   485.5    1000    49
   6 Goldfish v1.8.2    :  2234.0   174.0     429    41
   7 Goldfish v1.6.0    :  2218.7   434.5     797    55
   8 Goldfish v1.7.2    :  2218.0    69.5     150    46
   9 Goldfish v1.5.1    :  2168.8   460.5     970    47
  10 Goldfish v1.5      :  2158.6   554.5    1145    48
  11 Goldfish v1.4      :  2153.5   487.0     970    50
  12 Goldfish v1.3      :  2130.7   162.5     325    50
  13 Goldfish v1.2      :  2112.2   107.5     230    47
  14 Goldfish v1.1      :  2055.2    93.0     232    40
  15 Goldfish v1.0      :  2000.0    13.5      32    42
```
  • Loading branch information
bsamseth committed Feb 15, 2019
2 parents 6ed378c + a56b2f8 commit 112802e
Show file tree
Hide file tree
Showing 17 changed files with 7,109 additions and 437 deletions.
31 changes: 25 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
language: cpp
compiler: gcc
dist: trusty
dist: xenial

notifications:
email: false

addons:
apt:
sources:
- deadsnakes
packages:
- python3.7-dev

before_install:
# C++17
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq

install:
- pyenv global $(pyenv whence 2to3) # activate all python versions
- pip install pipenv

# C++17
- sudo apt-get install -qq g++-7 gcc-7
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 90
Expand All @@ -21,6 +32,15 @@ install:
- tar xf lcov_1.11.orig.tar.gz
- sudo make -C lcov-1.11/ install
- which lcov

# Install stockfish (used in search verification). Script from https://github.com/niklasf/python-chess/blob/master/.travis.yml
- wget https://stockfish.s3.amazonaws.com/stockfish-10-linux.zip
- unzip stockfish-10-linux.zip
- mkdir -p bin
- cp stockfish-10-linux/Linux/stockfish_10_x64_modern bin/stockfish
- chmod +x bin/stockfish
- export PATH="`pwd`/bin:${PATH}"
- which stockfish || (echo $PATH && false)

# Install coverals gem for uploading coverage to coveralls.
- gem install coveralls-lcov
Expand All @@ -30,18 +50,17 @@ before_script:
- gcov --version
- g++ --version
- PARENTDIR=$(pwd)
- mkdir $PARENTDIR/build && cd $PARENTDIR/build

script:
- pipenv install
- CMAKE_BUILD_TYPE=Coverage pipenv run python tests/test_search.py
- cd $PARENTDIR/build
- cmake -DCMAKE_BUILD_TYPE=Coverage $PARENTDIR
- make
- make gtest
- make bench
- make coverage
- printf "bench\nquit\n" | ./goldfish.x

after_success:
- cd $PARENTDIR/build
- lcov --list coverage_out.info.cleaned # Show test report in travis log.
- coveralls-lcov coverage_out.info.cleaned # uploads to coveralls
- bash <(curl -s https://codecov.io/bash) -f coverage_out.info.cleaned || echo "Codecov did not collect coverage reports"

7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
cmake_minimum_required(VERSION 3.0)

# Set project name here.
project(Goldfish VERSION 1.8.2 LANGUAGES CXX)
project(Goldfish VERSION 1.9.0 LANGUAGES CXX)

# Include stuff. No change needed.
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
Expand All @@ -31,7 +31,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wfatal-errors")

# Build-type specific flags. Change as needed.
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_DEBUG "-g -Og")
SET(CMAKE_CXX_FLAGS_DEBUG "-g -O0")

message(STATUS "Building with the following extra flags: ${CMAKE_CXX_FLAGS}")

Expand Down Expand Up @@ -88,6 +88,9 @@ set(SOURCES
src/perft.cpp
src/position.cpp
src/search.cpp
src/searchmanagement.cpp
src/semaphore.cpp
src/timer.cpp
src/tt.cpp
)
set(TESTFILES
Expand Down
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
chester = "*"
python-chess = "*"

[requires]
python_version = "3.7"
37 changes: 37 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)
[![Build Status](https://travis-ci.org/bsamseth/Goldfish.svg?branch=master)](https://travis-ci.org/bsamseth/Goldfish)
[![Coverage Status](https://coveralls.io/repos/github/bsamseth/Goldfish/badge.svg?branch=master)](https://coveralls.io/github/bsamseth/Goldfish?branch=master)
[![codecov](https://codecov.io/gh/bsamseth/Goldfish/branch/master/graph/badge.svg)](https://codecov.io/gh/bsamseth/Goldfish)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/7f53976fd4bb42b4bfb2f53bd67fce65)](https://www.codacy.com/app/bsamseth/Goldfish?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=bsamseth/Goldfish&amp;utm_campaign=Badge_Grade)
[![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/bsamseth/Goldfish.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/bsamseth/Goldfish/context:cpp)
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/bsamseth/Goldfish.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/bsamseth/Goldfish/context:python)
Expand Down Expand Up @@ -59,7 +60,7 @@ been added so far. The list is inspired in large part by [this writeup](http://w
- [X] Killer move heuristic
- [X] Principal variation search
- [X] Internal iterative deepening
- [X] Aspiration window search
- [X] Aspiration window search (repealed by PR #27, needs tuning before reapplying)
- [ ] Futility pruning
- [ ] Delta pruning in quiescence search.
+ [X] Prune when _no_ move can improve enough
Expand Down
2 changes: 1 addition & 1 deletion cmake/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
ADD_CUSTOM_TARGET(${_targetname}

# Cleanup lcov
${LCOV_PATH} --directory . --zerocounters
# ${LCOV_PATH} --directory . --zerocounters

# Run tests
COMMAND ${_testrunner} ${ARGV3}
Expand Down
2 changes: 1 addition & 1 deletion include/bound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace goldfish {

enum class Bound : uint8_t {
enum Bound : uint8_t {
NONE, LOWER, UPPER, EXACT = LOWER | UPPER
};

Expand Down
5 changes: 5 additions & 0 deletions include/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Position {

bool is_attacked(Square target_square, Color attacker_color);

bool last_move_was_null_move();

private:
class Zobrist {
public:
Expand Down Expand Up @@ -109,6 +111,9 @@ class Position {
std::array<State, MAX_MOVES> states;
int states_size = 0;

std::array<Move, MAX_MOVES> moves;
int move_count = 0;

Zobrist &zobrist;

void clear_castling(Square square);
Expand Down
18 changes: 16 additions & 2 deletions include/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,27 @@ class Search {

void update_search(int ply);

void save_pv(const Move move, const MoveVariation &src, MoveVariation &dest);

Value pv_search(Depth depth, Value alpha, Value beta, int ply, int move_number);

Value search_root(Depth depth, Value alpha, Value beta);

Value search(Depth depth, Value alpha, Value beta, int ply);

Value quiescent(Value alpha, Value beta, int ply);

void save_pv(Move move, MoveVariation &src, MoveVariation &dest);
};

inline uint64_t Search::get_total_nodes() {
return total_nodes;
}

inline void Search::save_pv(const Move move, const MoveVariation &src, MoveVariation &dest) {
dest.moves[0] = move;
for (int i = 0; i < src.size; i++) {
dest.moves[i + 1] = src.moves[i];
}
dest.size = src.size + 1;
}

}
18 changes: 18 additions & 0 deletions include/tt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,22 @@ void TranspositionTable<Mb_Size>::store(const uint64_t key, Value value, Bound b
tte->save(key, value, bound, depth, move);
}

// value_to_tt() adjusts a mate score from "plies to mate from the root" to
// "plies to mate from the current position". Non-mate scores are unchanged.
// The function is called before storing a value in the transposition table.
constexpr Value value_to_tt(Value v, int ply) {
assert(v != Value::NO_VALUE);
return v >= Value::CHECKMATE_THRESHOLD ? v + ply
: v <= Value::CHECKMATE_THRESHOLD ? v - ply : v;
}

// value_from_tt() is the inverse of value_to_tt(): It adjusts a mate score
// from the transposition table (which refers to the plies to mate/be mated
// from current position) to "plies to mate/be mated from the root".
constexpr Value value_from_tt(Value v, int ply) {
return v == Value::NO_VALUE ? Value::NO_VALUE
: v >= Value::CHECKMATE_THRESHOLD ? v - ply
: v <= Value::CHECKMATE_THRESHOLD ? v + ply : v;
}

}
Loading

0 comments on commit 112802e

Please sign in to comment.