Compare commits

...

289 Commits

Author SHA1 Message Date
Marco Costalba e87931bbfc Stockfish 1.3
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-05-02 11:53:08 +01:00
Marco Costalba aa5c375ca9 Fix a very old UCI option parsing bug
We currently fail on an option with a sapece in the name,
as example

setoption name Clear Hash

returns error message "Option Clear not found". This
patch fixes this off-by-one type bug.

Thanks to Joona for spotting this.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-05-02 11:52:49 +01:00
Marco Costalba cd1cc39b04 Nicely simplify MovePicker::pick_move_from_list
It is a positive fall back from previous patch.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-05-02 10:08:10 +01:00
Marco Costalba 4e151f7e0d Sort moves just after scoring
Instead of a delayed selection sort so that the highest
score move is picked up from the list when needed, sort all
the moves up front just after score them.

Selection sort is O(n*n) while std::sort is O(n*log n), it
is true that delayed selection allows us to just pick the move
until a cut off occurs or up to a given limit (12), but with
an average of 30 non capture-moves delayed pick become slower
just after 5-6 moves and we now pick up to 12.

Profiling seem to prove this idea and movepick.cpp is now 10%
faster.

Also tests seem to confirm this:

After 700 games at 1+0: Mod vs Orig +178 -160 =362 +9 ELO

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-05-02 10:07:46 +01:00
Marco Costalba d13e4c16c2 Update polyglot.ini
Upadte to new parameters and parameters values.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-30 15:16:35 +01:00
Marco Costalba 1906df4494 Stockfish 1.3 rc1
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-30 09:18:20 +02:00
Marco Costalba 39f2eda285 Do not razor after a null move
We don't want to return unproven null move fails high, so
that if a position is so good that null move fails high we
want to check this with real do_move() / undo_move() test,
not just razoring the position because, from the opponent
point of view, is very bad.

These are tests results at 1+0

Mod vs Orig +252 -264 =483  49.40%
Mod vs Toga II 1.4.1SE  +365 -325 =309  52.00%

So it seems a very slightly regression regarding orig version (but
withing error bar) and a nice increase against Toga that is what we
are interested most. Orig version scores 49.75% against Toga, so
we welcome this change ;-)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-30 08:55:38 +02:00
Marco Costalba 00a3380885 Fix assignment of pv[0] when creating root move list
It is bogusly assigned from moves[i].move instead of mlist[i].move
or equivalently to moves[count].move that it seem more clear to me.

Bug is hidden while all the moves are included, in this default case
moves[i].move and mlist[i].move are the same variable.

Also a bit of cleanup while there.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-29 16:01:57 +02:00
Marco Costalba f1d982e2c0 Merge Joona's razoring tweaks
After proof testing on 3 different engines these
are the results:

Stockfish - Toga II 1.4.1SE +130 -132 =132 49.75%
Stockfish - Deep Sieng 3.0  +145 -110 =150 54.45%
Stockfish - HIARCS 12 MP    +94  -149 =150 43.00%

So it seems no regressions occurs, although also no
improvment. But anyhow this patch increases Stockfish
strenght against itself, so merge it.

Note that this patch not only adds back razoring at depth
one, but also increases razor depth limit from 3 to 4
because hard coded depth 4 limit is no more overwritten
by UCI parameter that otherwise defaults to 3.
2009-04-28 09:00:09 +02:00
Marco Costalba fbca16da57 Hardcode depth limit for selective search
Because futility margins array has a fixed size we cannot
arbitrarly choose or change the SelectiveDepth parameter,
otherwise we have a crash for values bigger then array size.

On the other hand tweaking of this parameter requires some
modification to the hardcoded margins, so makes sense to hard
code also this very bounded one.

Who wants to experiment is of course free to change the sources.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-28 08:47:26 +02:00
Marco Costalba 1e97cdd9f3 Fix a warning under MSVC
Somehow silly warning C4800:
'int' :forcing value to bool 'true' or 'false'(performance warning)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 18:00:45 +01:00
Marco Costalba ab69f50c64 Micro optimize Position::move_is_check()
More then optimization it is worth a bit of better
code self documenting.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 17:39:59 +02:00
Marco Costalba b35e593551 Inline Position::move_is_capture()
This is a very hot path function, profiling on Intel compiler
shows that inlining cuts in half the overhead.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 16:36:33 +02:00
Marco Costalba 20d88dbf98 Retire timeoday.cpp
Move the only function gettimeofday in misc.cpp
where is used.

This avoids polluting the global namespace.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 13:48:14 +01:00
Marco Costalba ef60043725 Small cleanup in misc.cpp
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 13:38:47 +01:00
Marco Costalba 2f2e48fad2 Code style cleanup in transposition table code
Assorted fixes but no functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 13:21:49 +01:00
Marco Costalba 2550c6383b Fix a bogus assert in tt.cpp
Max hash size is 4096 MB, not 1024 MB, see the corresponding
"Hash" UCI parameter in ucioption.cpp

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 10:29:33 +01:00
Marco Costalba fb560fa5d7 Convert piece.cpp to C++
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 10:12:34 +01:00
Marco Costalba 11491e71ee Remove an useless comparison in futility pruning
Currently futility is allowed when depth < SelectiveDepth
and SelectiveDepth is 7*OnePly, so the comprison is
always true.

Patch could introduce a functional change only if
we choose to increase SelectiveDepth.

Currently no functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-27 09:45:15 +01:00
Marco Costalba bd26374f21 Small code tidy up and test results
When testing at 1'+0" time control results are still
reasonably good. We have made two sessions on two
different PC.

After 840 games Mod - Orig: +221 -194 =425 +10 ELO (two CPU)

After 935 games Mod - Orig: +246 -222 =467  +9 ELO (single CPU)

So it seems that with fast CPU and/or longer time controls
benefits of the patch are a bit reduced. This could be due
to the fact that only 3% of nodes are pruned by razoring at
depth one and these nodes are very swallow ones, mostly get
pruned anyway with only a slightly additional cost, even
without performing any do_move() call.

Another reason is that sometime (0,3%% of cases) a possible
good move is missed typically in positions when moving side
gives check, as example in the following one

3r2k1/pbpp1nbp/1p6/3P3q/6RP/1P4P1/P4Pb1/3Q2K1 w - -

The winning move Rxg7+ is missed.

Bottom line is that patch seems good for blitz times, perhaps
also for longer times. We should test against a third engine
(Toga ?) to have a final answer regarding this new setup.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-26 13:51:54 +01:00
Joona Kiiski d20e0cf048 Razor again at depth one
Some time ago it was found by Marco Costalba that it's better
to disable razoring at depth one, because given the very low
evaluation of the node, futility pruning would already do
the job at very low cost and avoiding missing important moves.

Now enable razoring there again, but only when our quickly evaluated
material advantage is more than a rook. The idea is to try razoring
only when it's extremely likely that it will succeed.

Extreme lightning speed test show promising result:
Orig - Mod: +1285 =1495 -1348

This needs to be tested with longer time controls though.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-26 13:51:50 +01:00
Joona Kiiski 342ceb1c91 Make futility and razor margins more tunable
Restructure RazorMargins and FutilityMargins arrays so that their
values can be more easily tuned.

Add RazorApprMargins array which replaces razorAtDepthOne concept,
because setting RazorApprMargin very high value at ply one is
same as not razoring there at all.

Comment out setting razoring and futility margins through uci to
avoid errors while tuning.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-26 13:51:47 +01:00
Marco Costalba f010b6db71 Do not hardcode default values of UCI variables
If a variable will be populated reading an UCI option
then do not hard code its default values.

This avoids misleadings when reading the sources.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-26 13:41:17 +01:00
Marco Costalba 7c267587fc Greatly speedup has_mate_threat()
Instead of loop across all legal moves to find a mate
loop across possible check moves only.

This reduces more then 10 times the number of moves to
check for a possible mate.

Also rename generate_checks() in generate_non_capture_checks()
so to better clarify what the function does.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-26 13:40:26 +01:00
Marco Costalba 24485c96ec Micro optimize generate_piece_checks() take 2
Add some missing bits of this patch.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-19 21:09:53 +02:00
Joona Kiiski f98385e129 Add missing header file to make stockfish compile with latest Inter C++ Compiler under Linux (memcpy needs cstring)
Correct some references glaurung -> stockfish in Makefile

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-19 20:57:23 +01:00
Marco Costalba 2acc89c6e8 Simplify Position::is_mate()
Should be a bit faster too.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-19 17:13:04 +01:00
Marco Costalba a52ab2afbf Micro optimize generate_piece_checks()
Avoid calculating piece attacks if there aren't
available check sqaures for the given piece.

About 15% of cases. Not a biggie but still something
especially in the middle game where king is well covered
inside his castle.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 16:28:12 +01:00
Marco Costalba de050db2b0 MovePicker: retire per square MVV/LVA ordering
Is not used anyway and in case we need it again we
can resurrect from git archives.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 14:24:21 +01:00
Marco Costalba e68ebe618c In qsearch store the cut move in TT
And upon reentering the same position try it as first.

Normally qsearch moves order is already very good, first move
is the cut off in almost 90% of cases. With this patch, we get
a cut off on TT move of 98%.

Another good side effect is that we don't generate captures
and/or checks when we already have a TT move.

Unfortunatly we found a TT move only in 1% of cases. So real
impact of this patch is relatively low.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 14:04:00 +01:00
Marco Costalba 4634be8ba6 Merge Joona's new aspiration window search
It seems very positive.

After 999 games at 1'+0" result is: +249 -216 =534 +11 ELO

And after another 456 games we still have: +122 -113 =221
2009-04-18 09:15:42 +01:00
Marco Costalba e7f03913ea Introduce move_pawns() helper in movegen.cpp
This let us to have more readable code keeping the
same speed.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:13:31 +01:00
Marco Costalba e30720b0bf Remove failHigh/Low bits from IterationInfoType
We don't use that info anyway.

Also document a little more new aspiration window code.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:12:41 +01:00
Joona Kiiski 7af1b40b4e Restore calling insert_pv after search is aborted + small clean-up
Restore old behaviour that after search is aborted we call insert_pv,
before breaking out from the iterative deepening loop.

Remove one useless FIXME and document other better.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:12:18 +01:00
Marco Costalba ecec7dbf89 Little code style tweaks
Let the code be more conformant to current style.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:12:07 +01:00
Marco Costalba 8da2153ee8 Revert previous patch as per Joona request
Joona says patch gives bad results after testing,
so revert for now.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:11:56 +01:00
Joona Kiiski 22f9f0cabe Improve handling of fail-highs in assumed PV
Check all fail highs in assumed PV with greater care (fruit/Toga already does this).
Add a flag when aspiration search fails high at ply 1 to prevent search to
be terminated prematurely.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:11:41 +01:00
Joona Kiiski 3e7e1a7c53 Revert "Implement a fallback system when aspiration search fails low and we are out of time."
This reverts commit 55dd98f7c717b94a659931cd20e088334b1cf7a6.

Revert fallback-system for root_search

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:11:30 +01:00
Joona Kiiski 0ea716463b Implement a fallback system when aspiration search fails low and we are out of time.
However also this patch is immediately reverted. For three reasons:
1) the case it affects is very rare (and then we are likely to lose anyway),
   so we can well live without this.

2) Because the case is so rare it's hard to test this change properly.

3) To perform fallback search, we must reset so many global variables that this
   patch is very likely both buggy and extremely bad style.

Consider including this again if we clean-up global variables one day...

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:11:15 +01:00
Joona Kiiski 6c4e36aab6 Revert "Implement bestValue in root_search."
This reverts commit 9a39f93f35254787b7b57980019dde276a89c48c.

Revert bestValue in root_search

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:11:05 +01:00
Joona Kiiski 6f28bcd483 Implement bestValue in root_search.
However just after finished writing this patch I realized that this
is not the way to go. So this will be immediately reverted.

(Just save this here in git in case I change my mind later :) )

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:10:54 +01:00
Joona Kiiski acef5d6a59 Dynamic aspiration search without research.
Implement system where aspiration search window is calculated using
values from previous iterations.

And then some crazy experimental stuff: If search fails low at the root,
don't widen window, but continue and hope we will find a better move
with given window. If search fails high at the root, cut immediately,
add some more time and start new iteration.

Note: this patch is not complete implementation, but a first test
for this idea. There are many FIXMEs left around. Most importantly
how to deal with the situation when we don't have any move!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-18 09:10:41 +01:00
Marco Costalba 44b497a972 Re-fix square.h warning to avoid a compile error under MSVC
This fix adds ugliness to an already ugly previous fix...hopefully
it is the last one :-(

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-13 12:02:22 +01:00
Marco Costalba b893583bb6 Fix a gcc warning due to order of initialization in Option
Move idx declaration before minValue and maxValue and silence
this last warning.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-13 12:02:14 +01:00
Marco Costalba e81d0d08c3 Fix a compile error with Intel icc
To make std::sort() work operator<() should be
declared const.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-13 12:02:06 +01:00
Marco Costalba 72c7595f8a Fix a warning under Intel compiler in square.h
We need to cast to char the whole expression...

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-13 12:01:55 +01:00
Marco Costalba e38ad4d42b Fix a very nasty conversion bug in Option c'tor
Sometimes C++ can be really bad!

In this case an hard coded c string selects Option c'tor
with int argument instead of the std::string one becuase
it is considered a better matching by the compiler.

Fix the bug changing the argument type from std::string to
const char* so to be a better match then the int one.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-12 01:10:50 +01:00
Marco Costalba fad772f387 Store UCI options of type CHECK according to C++ convention
Store boolean values as "1" and "0" instead of "true" and "false"
and convert back to UCI protocol convention when needed.

This is simpler then the other way around.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-11 16:01:33 +01:00
Marco Costalba ebb0f31928 Restore original UCI option printing order
For each option store its index so to be printed
according to insertion order.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-11 16:01:21 +01:00
Marco Costalba f0701e2b0f Use a map instead of a vector to store UCI options
Apart from the teoretical speed increase, the main reason
of this patch is a good amount of code cleanup.

Note that now UCI options are printed in alphabetical
order and not in insertion order as before. Next patch
will take care of restoring old behaviour.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-11 16:01:08 +01:00
Marco Costalba 95aadb5e53 Remove unused currentMoveCaptureValue from search stack
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-11 11:09:37 +01:00
Marco Costalba 17ef886fc3 Less aggressive null move dynamic reduction
In null move search do not jump directly in
qsearch() from depth(4*OnePly), but only
from depth(3*OnePly).

After 999 games at 1+0: +248 -224 =527 +8ELO

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-11 10:58:10 +01:00
Marco Costalba 8590a6f3b7 Revert dynamic LMR
It doesn't seem to work against Toga. After more then 400 games
we are at -13 ELO while, without it we are at + 5 ELO after 1000
games.

So revert for now to keep code simple.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-04-03 20:34:22 +01:00
Marco Costalba 7b05b83bf2 Fix compile in the debug mode
Due to previous patches we end up with a compile
error in debug mode.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-31 20:50:24 +01:00
Marco Costalba d7ef09727a Silence idiotic warning on two's complement of an unsigned
MSVC gives:

warning C4146: unary minus operator applied to unsigned type,
               result still unsigned

When finds -b where b is an unsigned integer. So rewrite the two's
complement in a way to avoid the warning. Theoretically the new
version is slower, but in practice changes nothing.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-31 20:50:10 +01:00
Marco Costalba 683595fee1 Silence a bunch of warnings under MSVC /W4
Still some remain, but are really the silly ones.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-31 20:49:56 +01:00
Marco Costalba 659c54582d Revert setting a flag when TT value equals static evaluation
Strangely enough it seems that optimization doesn't work.

After 760 games at 1+0: +155 -184 =421 -13 ELO

Probably the overhead, although small, for setting the flag
is not compensated by the saved evaluation call.

This could be due to the fact that after a TT value is stored,
if and when we hit the position again the stored TT value is
actually used as a cut-off so that we don't need to go on
with another search and evaluation is avoided in any case.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-31 20:41:06 +01:00
Marco Costalba 2c0cd95ecf An VALUE_TYPE_EVAL score cannot overwrite an entry
If we want to store a value of type VALUE_TYPE_EVAL for
a given position and we found an already exsisting entry
for the same position then we skip.

We don't want to overwrite a more valuable score with a
lesser one. Note that also in case the exsisting entry is
of VALUE_TYPE_EVAL type the overwrite is unuseful because
we would store the same score again.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-30 09:09:27 +01:00
Marco Costalba 6a8e46d53e Remember when TT value equals static evaluation value
When the stored TT value equals the static value set a
proper flag so to not call evaluation() if we hit the
same position again but use the stored TT value instead.

This is another trick to avoid calling costly evaluation()
in qsearch.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-30 08:54:09 +01:00
Marco Costalba c6c4713ab2 Document TTEntry and move layouts
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-30 08:29:02 +01:00
Marco Costalba 9e44a6dba9 A move needs 17 bits not 19
Fix a bug in the way a move is stored and read in a TT entry.
We use a mask of 19 bits insteaad of 17 so that the last
two bits in the TT entry end up to be random data.

This bug will bite us when we will use these two until now
unused bits.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-29 17:23:41 +01:00
Marco Costalba 941f4e1643 Remove some obsolete code in movepick.cpp
This fixes some warning under Intel compiler.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-27 19:35:08 +01:00
Marco Costalba dcdac83187 Revert storing of TT when returning from "stand pat"
After testing it seems patch is bad:

After 999 games 1+0: +242 -271 =486 -10 ELO

So restore saving of TT at the end but using new Joona
idea of storing as VALUE_TYPE_UPPER/VALUE_TYPE_LOWER instead
of VALUE_TYPE_EXACT.

Some optimization is still possible but better test new ideas
one by one.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-27 15:30:45 +01:00
Marco Costalba 5a0581498c Cache evaluation score in qsearch
Instead of just drop evaluation score after stand pat
logic save it in TT so to be reused if the same position
occurs again.

Note that we NEVER use the cached value apart to avoid an
evaluation call, in particulary we never return to caller
after a succesful tt hit.

To accomodate this a new value type VALUE_TYPE_EVAL has been
introduced so that ok_to_use_TT() always returns false.

With this patch we cut about 15% of total evaluation calls.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 18:28:42 +01:00
Marco Costalba 1c087dd806 Let to toggle dynamic LMR
It is now disabled by default due to bad results
against a pool of engines...more testing is needed tough.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 18:28:25 +01:00
Marco Costalba 095a96b461 In qsearch update TT only if returning from stand pat
This is the only "correct" exact value we can store.

Otherwise there could be spurious failed high/low nodes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 11:26:43 +01:00
Marco Costalba 72af519e7f When asked for position key print it as an hex value
Merged from Glaurung current development snapshot.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 10:50:13 +01:00
Marco Costalba db46602b1f Wait at least until iteration 3 before to stop the search
It was 2 before.

Merged from Glaurung current development snapshot.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 10:49:02 +01:00
Marco Costalba d3f99aea6b Let zobrist keys to be compatible with Glaurung 1
Some changes to the zobrist keys, to make them identical
to those used by Glaurung 1.

The only purpose is to make it possible for both programs
to use the same opening book.

Merged from Glaurung current development snapshot.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 10:40:22 +01:00
Marco Costalba 0e835bd334 Fixed a sliding attack bitboard bug in 32-bit mode
This is what prevented USE_32BIT_ATTACKS from working
on some architectures (like PowerPC).

Merged from Glaurung current development snapshot.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-24 10:30:27 +01:00
Marco Costalba 43276cbec5 Fix a bug in insert_pv() where minimum depth is zero
We implicitly considered the minimum depth stored in TT
to be Depth(0), but because we store values in TT also in
qsearch() where depth is < 0, we need to use a negative
number as minimum depth.

Bug spotted by Joona Kiiski.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-23 15:30:20 +01:00
Marco Costalba a9e55d4326 Revert odd depths razoring
I have just made a new rule that no modification
that increases pruning is allowed if after 1000 games
ELO is not increased by at least 10 point (was +5 in this case)

Yes, I like this kind of nosense rules :-)
2009-03-23 12:02:15 +01:00
Marco Costalba 4d70e3aeac More aggressive dynamic LMR
Previous setup didn't change anything

After 996 games 1+0: +267 -261 =468 +2 ELO

Now with this new setup we have

After 999 games 1+0: +277 -245 =477 +11 ELO

Seems reasonable...

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-23 11:58:28 +01:00
Marco Costalba 24b7ad54c7 LMR dynamic reduction
Reduce of two plies near the leafs and when we still
have enough depth to go so to limit horizon effects.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 23:53:22 +01:00
Marco Costalba 0ff3bf34cd Always print a best move when requested
Little fix merged from iPhone Glaurung.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 23:53:10 +01:00
Marco Costalba 66d165921d Better castle move detector in move_to_san()
Merged from iPhone Glaurung.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 23:52:59 +01:00
Marco Costalba b82c3021fa Fix a smal bug in Position::from_fen
We could fail to parse an en-passant position
in same cases.

Merged from iPhone Glaurung.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 23:52:23 +01:00
Marco Costalba 320630ca1a Merge new pawn storm evaluation
More accuracy in pawn storm evaluation
directly from iPhone Glaurung.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 23:52:06 +01:00
Marco Costalba ef8acdc73b Fix a small bug in king safety
Merged from iPhone Glaurung.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 13:11:24 +01:00
Marco Costalba cc8e915ed5 Merge KBPP vs KB endgame from iPhone Glaurung
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-22 13:06:29 +01:00
Marco Costalba 16abc165d8 Fix: In qsearch do not use TT value when in a PV node
We already do like this in search_pv(), so extend
also in qsearch().

Bug spotted by Joona Kiiski.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-21 14:51:31 +01:00
Marco Costalba 74160ac602 Big headers cleanup
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-19 12:55:32 +01:00
Marco Costalba feb5342b39 Safe guard some wild and ugly casts
These casts are needed but plain ugly, at least be
sure they don't hide any subtle conversion bug.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-16 13:59:41 +01:00
Marco Costalba 6cddf9183c Partially revert pawns storm bug fix
Try to save space and use the minimum size
possible.

In particular restore int16_t for values and int8_t
for halfOpenFiles.

Use int16_t for storm values insted of int and also
instead of original buggy and too small int8_t.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-16 08:02:33 +01:00
Marco Costalba b870f5a091 Silence a good bunch of Intel warnings
Note that some pawns and material info has been switched
to int from int8_t.

This is a waste of space but it is not clear if we have a
faster or slower code (or nothing changed), some test should be
needed.

Few warnings still are alive.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-15 18:19:08 +01:00
Marco Costalba fcecc5212e Fix an overflow bug in pawns stormValue
These fields are defined as int8_t but values bigger
then 127 are stored there so that we silently overflow.

Fix bringing up all the fields to a sane int type. This
will increase memory usage, but apart from being safe, it is
not clear if code is slower or faster. Test is needed.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-15 18:18:56 +01:00
Marco Costalba 8de91be61e Fix a silly warning on Intel compiler
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-15 18:18:42 +01:00
Marco Costalba ca4e78db8d Revert NULL move beta corrections
After testing result is bad -25 ELO
2009-03-15 16:44:12 +01:00
Marco Costalba bfcfaf7101 Retire Null Driven IID
It does not seem to clearly improve things and
in any case is disabled by default, so retire for now.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-15 16:43:28 +01:00
Marco Costalba c8773c720a Merge Joona Kiiski NULL search beta correction
Prune more moves after a null search because of
a lower beta limit then in main search.

In test positions reduces the searched nodes of 30% !!!!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-14 13:00:22 +01:00
Marco Costalba 3ed603cd64 Merge Joona Kiiski evaluation tweaks
Merge tewaks to many evaluation parameters
by Joona Kiiski.

After test they seem good!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-14 12:55:14 +01:00
Marco Costalba f637ddc1e8 Micro optimize move_is_check()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-07 21:05:49 +01:00
Marco Costalba 964bd86272 Micro optimize pl_move_is_legal()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-07 21:05:31 +01:00
Marco Costalba 72b88e09e1 Micro optimize previous patch
Also remove some Intel warnings, not all :-(

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-06 20:47:19 +01:00
Marco Costalba 3e663d8c50 Introduce evaluate_pieces<>() to remove redundancy
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-06 19:19:45 +01:00
Marco Costalba 8c9c51c721 Fix compile error with inlines under gcc and Intel
It seems that these compilers do not like inline functions
that call a template when template definition is not in scope.

So move functions from header to in *.cpp file

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-05 00:38:45 +01:00
Marco Costalba 7fe1632a49 Fix some comments in position.cpp
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-04 22:51:20 +01:00
Marco Costalba 7c84b39a42 Avoid to call useless sliders attacks in update_checkers()
Quickly filter out some calls to sliders attacks
when we already know that will fail for sure.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-04 22:51:04 +01:00
Marco Costalba 68e711aac6 Super fast hidden_checkers()
Rewritten hidden_checkers() to avoid calling
sliders attacks functions but just a much
faster squares_between()

Also a good code semplification.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-04 22:50:51 +01:00
Marco Costalba 02cd96e4c2 Cleanup SearchStack initialization
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-03 21:01:00 +01:00
Marco Costalba 772a37cd54 Micro optimize copy of new state in do_move()
Instead of copying all, copy only the fields that
are updated incrementally, not the ones that are
recalcuated form scratch anyway.

This reduces copy overhead of 30%.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-02 18:00:42 +01:00
Marco Costalba c02613860a Revert hidden checkers rework
It is slower the previous uglier but faster code.

So completely restore old one for now :-(

Just leave in the rework of status backup/restore in do_move().

We will cherry pick bits of previous work once we are sure
we have fixed the performance regression.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-03-02 16:20:00 +01:00
Marco Costalba 9b6b9e67fe Use checker info to remove a bunch of hidden checks updates
Another powerful condition let us remove a big chunk of
useless updates.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-28 18:43:36 +01:00
Marco Costalba 6a8cfe79da Stricter condition to check for dc candidates
Another optimization that let us remove another half
of find_hidden_checks(them, DcCandidates) calls.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-28 18:43:20 +01:00
Marco Costalba 1f97b48a31 Split calculation of pinners from dc candidates
This let us to calculate only pinners when we now that
dc candidates are not possible.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-28 18:43:02 +01:00
Marco Costalba a96cba0ec8 Slightly better condition in update_hidden_checks()
Use a more strict condition to check if we have captured
an opponent pinner or hidden checker.

With this patch the occurrence of checkerCaptured == true are
reduced of 50%.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-28 18:42:51 +01:00
Marco Costalba f9f30412e7 Compute pinned and friends incrementally
In do_move() use previous pinned bitboards values to compute
the new one after the move. In particulary we end up with the
same bitboards in most cases. So detect these cases and just
keep the old values.

This should speedup a lot this slow computation in a very hot
path so that we can use this important info everywhere in the
code at very cheap cost.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-28 18:42:30 +01:00
Marco Costalba 55f9afee2a Fix a subtle bug due to the StateInfo pointer became stale
There was one occurence when the StateInfo variable went
out of scope before the corresponding Position object.

This yelds to a crash. Bug was not hit before because occurs
only when using an UCI interface and not the usual benchmark.

The fix consists in copying internally the content of the
about to stale StateInfo.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-23 21:45:12 +01:00
Marco Costalba 962216440c Teach SEE about pinned pieces
Remove pinned pieces from attacks when calculating
SEE value.

Algorithm is not perfect, there should be no false
positives, but can happen that we miss to remove a
pinned piece. Currently we don't cach 100% of cases,
but is a tradeoff between speed and accuracy. In any
case we stay on the safe side, so we remove an attacker
when we are sure it is pinned.

About only 0,5% of cases are affected by this patch, not
a lot given the hard work: this is a difficult patch!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-23 21:45:01 +01:00
Marco Costalba 243fa483d7 Small Position::clear() cleanup
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-23 21:44:46 +01:00
Marco Costalba da7a62852a Do not copy the whole old state in do_move()
Instead of copy the old state in the new one, copy only
fields that will be updated incrementally, not the ones
that will be recalculcated anyway.

This let us copy 13 bytes instead of 28 for each do_move()
call.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-23 21:44:29 +01:00
Marco Costalba 0bf45823da Update pinned bitboards and friends in do_move()
Probably is slightly slow, but code is surely better
in this way. We will optimize later for speed.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-23 21:44:17 +01:00
Marco Costalba 4324276419 Fix some asserts unhidden by a debug compile
Fallback form previous patches.
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-23 21:44:04 +01:00
Marco Costalba 43bc5479c2 Avoid resetting pinners[c]
Small optimization. No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-22 21:19:04 +01:00
Marco Costalba 8f59de48f5 Introduce StateInfo instead of UndoInfo
We don't backup anymore but use the renamed StateInfo
argument passed in do_move() to store the new position
state when doing a move.

Backup is now just revert to previous StateInfo that we know
because we store a pointer to it.
Note that now backing store is up to the caller, Position is
stateless in that regard, state is accessed through a pointer.

This patch will let us remove all the backup/restore copying,
just a pointer switch is now necessary.

Note that do_null_move() still uses StateInfo as backup.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-22 21:18:50 +01:00
Marco Costalba 2f6c5f00e6 Wrap state variables in a named struct
This will allow us to more easily move the state
out of Position class.

No functioanl change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-22 21:18:35 +01:00
Marco Costalba 2f21ec39ad Convert also undo_null_move() to avoid passing UndoInfo object
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-22 21:18:14 +01:00
Marco Costalba 9b257ba29d Passing UndoInfo is not needed anymore when undoing the move
We store it now in the same UndoInfo struct as 'previous'
field, so when doing a move we also know where to get
the previous info when undoing the back the move.

This is needed for future patches and is a nice cleanup anyway.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-22 21:18:02 +01:00
Marco Costalba 1b0fee9b17 Remove two useless calls to pinned_pieces()
Are obsoleted by new pinned caching code.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-22 21:17:44 +01:00
Marco Costalba b7cb6180cf Position: Unify and templetize mg_pst() and eg_pst()
Also templetize compute_value() can be simpler now that
the above is templetized too.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-20 22:50:35 +01:00
Marco Costalba f30aa83f8a Unify compute_mg_value() and compute_eg_value()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-20 22:50:20 +01:00
Marco Costalba 1dc27f3232 Use a union to fast and simply backup info in do_move()
This nice union trick let us optimize the speed and
remove the now unuseful backup() and restore() functions.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-20 22:50:08 +01:00
Marco Costalba 95cfc0e306 In Position backup and restore contiguous data
Give the compiler one more chance to optimize the copy.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-20 22:49:54 +01:00
Marco Costalba 9e1d142fdd Finally remove any occurence of dcCandidates from search.cpp
This ends our cleanup series.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-19 17:35:23 +01:00
Marco Costalba 683e6dc656 Do not pass discovery check candidates in Position::do_move()
Also remove any bit of 'pinned' and co. from MovePicker class.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-19 17:29:36 +01:00
Marco Costalba c6630abe0d Do not pass pinned argument in Position::move_is_check()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-19 17:01:08 +01:00
Marco Costalba 734941672e Do not pass pinned argument in Position::pl_move_is_legal()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-19 16:48:57 +01:00
Marco Costalba 0a0ea36e25 Cleanup pinned and friends in movegen.cpp
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-19 16:37:03 +01:00
Marco Costalba 5f142ec209 Cache pinned and discovery check bitboards
After have been calculated cache their values
so to avoid another expensive call to hidden_checks()
if pinned_pieces() or discovered_check_candidates() are
called with the same position.

Add also interface to get pinners bitboard, we already have
this value so save it instead of discard.

Now that, after the first call to pinned_pieces() the following
are very cheap we can rewrite and cleanup all the checking handling.

So this patch is a prerequisite for future work.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-19 16:28:35 +01:00
Marco Costalba 7f4f18f959 Revert mobility of pinned pieces for now
We will redo after rewriting the handling of
pinned bitboard and its friends.
2009-02-19 15:49:54 +01:00
Marco Costalba c45818e9f8 Remove xxx_of_color() for real
Remove also from assert expressions. Was hidden
in release mode.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-17 17:26:15 +01:00
Marco Costalba 7013efce4e Change piece_attacks_square() API
An extra argument let us simplify some code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-17 12:00:05 +01:00
Marco Costalba 9f5b709db7 Mobility is zero for a pinned piece
A little bit more overhead, but better mobility evaluation.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-17 11:29:14 +01:00
Marco Costalba 2c955f25de Remove xxx_of_color() helpers
They hide the underlying uniform function call with
no benefit.

A little bit more verbose but now is clear what happens.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-17 10:54:47 +01:00
Marco Costalba f32992f88c Avoid a call to move_is_capture() in extension()
Pass value as an argument instead or recalculating it.
Altough call is cheap this is a very hot path so with
this patch total time spent for move_is_capture() is almost
halved.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:57:03 +01:00
Marco Costalba e71d520758 Add scan for X-ray attacks in piece_attacks_square()
Used to avoid pruning interesting moves.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:56:46 +01:00
Marco Costalba 52ed0e9563 Fix a subtle bug in Position::move_is_capture()
Currently fails if we test with a move that is not of
from the side to move but from the opponent.

This is the typical case of the threat from null move
search. The result is an erroneus prune of the defending
moves, see PruneDefendingMoves in ok_to_prune()

Fix the test to work also with threat moves.

Bug was always in but was unhidden by a patch of 17/12/2008
"Trigger of PawnEndgameExtension if capture is not a pawn"

Until then it was hidden by a tricky check in the prune
conditions instead of the natural move_is_capture()

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:56:26 +01:00
Marco Costalba 30e8f0c9ad Do not manually build endgame functions hash keys
Use Position::compute_material_key() to do the job,
so we are sure there is not key mismatch during
endgame function lookups.

This fixes two endgames hash errors that caused two
endgames to be disabled.

This patch is also a code cleanup because removes a lot
of messy key assignments.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:55:59 +01:00
Marco Costalba 67375f4693 Use template for endgame scaling functions
Also integrate scaling and evaluation in a
single base class.

Nice use of templates here :-)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:55:29 +01:00
Marco Costalba 039badfda8 Use templates for end game evaluation functions
Huge simplification and no speed cost penalty.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:55:14 +01:00
Marco Costalba 088ecc242f Small code formatting in position.cpp
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:54:56 +01:00
Marco Costalba a188a047ab Use update_checkers<>() also for PAWN
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:54:37 +01:00
Marco Costalba 1d2247aea3 Introduce update_checkers() to simplify do_move()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:54:17 +01:00
Marco Costalba 8365f8ac1e Remove square_is_attacked()
Use attacks_to() instead. No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:53:58 +01:00
Marco Costalba 68d36b6f59 Rename generate_piece_blocking_evasions()
In generate_piece_moves() to be more uniform with other
functions. Unfortunatly the different number of calling arguments
do not allow us to easily integrate in generate_piece_moves()
template family.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:53:39 +01:00
Marco Costalba 33c608e140 Final touches to generate_evasions()
Small code tidy up and a little optimization
to avoid calling generate_piece_blocking_evasions()
when blockSquares is empty (30% of cases).

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:53:13 +01:00
Marco Costalba ff60dafe8d Simplify legality check in generate_evasions()
Now that we have removed sliders checkers attacks
from evasion's set we can simplyfy legality check.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-13 20:52:52 +01:00
Marco Costalba 214f9dcc27 generate_evasions() avoid an usless check for enpassant case
Remove ugly and useless code.
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-11 19:01:26 +01:00
Marco Costalba 1dc1cecf01 Optimize generate_piece_blocking_evasions()
Rewrite as in generate_piece_moves() using a for
loop instead of the slower serializing of the
bitboard with pop_1st_bit()

This will allow also to merge with generate_piece_moves()
when we will drop legality constrain on generate_evasions()

Generated moves are not changed, but are generated in a
different order, this changes the number of nodes at fixed
depth test.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-11 19:01:06 +01:00
Marco Costalba 67535711e8 generate_evasions() avoid to calculate pinned pieces
Pass as function argument.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-11 19:00:52 +01:00
Marco Costalba 56f607fe0f Drop a double semicolon
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-11 19:00:35 +01:00
Marco Costalba 9d044cf4ee Last touches to movegen.cpp
Of course no functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:51 +01:00
Marco Costalba 769f2fdecb Remove special case of pawn checks generation
Also additional renaming.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:49 +01:00
Marco Costalba 9bffe811c4 Remove special case of pawn move generatation
Code cleanup. No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:47 +01:00
Marco Costalba 3e20c6c07d Simplify generate_evasions()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:45 +01:00
Marco Costalba 4f5f97107e Simplify generate_checks()
Also rearrange signatures to be uniform.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:43 +01:00
Marco Costalba 151d47dc85 Micro-optimize do_generate_pawn_checks()
Discovery check candidates are normally empty, so
avoid discovery checks generation in that common case.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:41 +01:00
Marco Costalba 33ddeec5e0 Templetize generate_piece_checks_king()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-10 19:01:39 +01:00
Marco Costalba 4573d618e4 Small optimization in generate_evasions()
Find squares attacked by slider checkers, we will
remove them from king evasions set so to avoid a couple
of cycles in the slow king evasions legality check loop.

Not a biggie, but now generate_evasions() is faster then
generate_non_captures(), before was slower.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-07 13:26:57 +01:00
Marco Costalba 1156eb865b Simplify newly introduced castling_is_check()
Use bit_is_set() instead of open coding.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-07 13:26:50 +01:00
Marco Costalba 76381cbd69 Small code style tidy up
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-07 13:26:42 +01:00
Marco Costalba fa322b3768 Fix casting warnings under Intel Compiler
Int to Char warning fixed changing the function
signature to int.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-07 13:26:29 +01:00
Marco Costalba 2ea7449f2a Fix Makefile
Was broken after we removed color.cpp and square.cpp

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-07 13:26:17 +01:00
Marco Costalba ee6e8851be Templetize generate_castle_moves()
Cleanup the code and remove lines.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-06 07:14:38 +01:00
Marco Costalba 03211296f1 Add generation of castling checks
When we generate checks one case is missing: generation
of castling moves that give check to the opponent king.

This is a very rare case but anyway it is a case
and we can do this without slowing down the common
case of no castling checks.

So this is the patch.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-06 07:14:16 +01:00
Marco Costalba cc76951483 Remove square.cpp
Move the few stuff in square.h

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-04 19:41:56 +01:00
Marco Costalba f0858cd229 Move constants from piece.cpp to piece.h
Leave in piece.cpp only a couple of functions that
need #include <cstring>

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-04 19:41:38 +01:00
Marco Costalba 225d89c51b PawnInfo::clear() retire memset() and fix Ubuntu compile
Go back to original direct assignment, this allows to
add an include in pawns.h to teach about memset()

This fix a compile error under Ubuntu.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-04 19:41:17 +01:00
Marco Costalba 8bef9e59a0 Remove the useless color.cpp
Integrate the only (inline) function in color.h

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-04 19:40:43 +01:00
Marco Costalba 82bf08e4f2 Final endgame.cpp space inflate
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-04 19:40:31 +01:00
Marco Costalba c32904f0a0 Revert previous commit.
Optimization is correct but slightly slower
so it is a pessimization :-)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-02-04 19:40:09 +01:00
Marco Costalba 6cfb661ca5 Yet another count_1s() optimization
No functional change

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-25 19:29:15 +01:00
Marco Costalba be43219136 Rever count_1s() optimizations
They are wrong for all ones case.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-25 18:00:57 +01:00
Marco Costalba d5b77ad45e MovePicker, remove a variable
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-25 13:52:35 +01:00
Marco Costalba c6d62b7da5 MovePicker::find_best_index() never returns -1
So avoid checking for it.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-25 13:36:59 +01:00
Marco Costalba 3e6e57231e Rewrite count_1s() to be similar to 64bit counterpart
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-11 17:15:18 +01:00
Marco Costalba 14c1fd4d27 Micro optimize count_1s_max_15() for 32 bit system
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-11 17:05:29 +01:00
Marco Costalba 4b53bb02f6 Fix a very old bug in queen mobility
For queen mobility could be bigger then 15, so
we need count_1s() not count_1s_max_15().

This bug was introduced by patch:
"Group common evaluate code" of 24/9/2008

So it's almost 4 months and two release old!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-10 16:11:33 +01:00
Marco Costalba ec2927286a Start to space inflate endgame.cpp
Still a lot to do, it's a big file!

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-08 15:46:57 +01:00
Marco Costalba bdbbc4e06b Rewrite evaluate_common() as a template
Seems to speed up this very hot path and code is
cleaner too.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-07 15:48:22 +01:00
Marco Costalba 539051b1e0 Big trailing whitespace cleanup part 2
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-07 15:48:11 +01:00
Marco Costalba c97104e854 Big trailing whitespace cleanup part 1
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-07 15:47:59 +01:00
Marco Costalba 5cacefe7c6 Another micro-optmization in valuate_passed_pawns()
very small gain, but still a gain at the cost of
an extra indentation level.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-07 15:47:31 +01:00
Marco Costalba 9b87d151bc Fix a small bug in rook pawn evaluation
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-06 16:10:22 +01:00
Marco Costalba cbfbf25d1b evaluate_passed_pawns() micro-optmization
No functional change, only a bit faster.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-06 16:06:08 +01:00
Marco Costalba 90585a8a36 Final space inflate of evaluatio.cpp
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-06 15:59:08 +01:00
Marco Costalba e236a0c652 Space inflate evaluate_passed_pawns()
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-06 15:49:33 +01:00
Marco Costalba d0804341c5 Piece mobility: filter out squares protected by pawns
Do not consider squares protected by enemy pawns
in mobility evaluation.

This reduces the mobility value by about 15%

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-05 15:14:16 +01:00
Marco Costalba dc4e2d8184 Take in account odd depths in razoring formula
This is somewhat taken from Stockfish 1.2 Default,
only the razoring thresold are updated, not the
razoring depth.

At the end razoring is a bit more aggressive. Results
seems slightly positive.

After 999 games +239 =536 -224 Elo +5

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2009-01-05 12:41:27 +01:00
Marco Costalba e828753a3a Fix dbg_mean_of() to enable statistics when called
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-30 12:54:21 +01:00
Marco Costalba 33bb8305a9 Restore development versioning and LSN filtering
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-30 12:42:23 +01:00
Marco Costalba cc3c1dc25a Stockfish 1.2 optimistic
Optimistic razoring settings. It is stronger with
most engines but weaker with someones.

The default is instead more solid and uniform with all
the opponents.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-29 12:24:34 +01:00
Marco Costalba 11763d2b7f Stockfish 1.2
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-29 12:18:49 +01:00
Marco Costalba d99a95df29 Micro optimization in extension()
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-29 12:18:36 +01:00
Marco Costalba 0da1d6a846 Remove a gcc warning on an unused variable
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-29 12:18:23 +01:00
Marco Costalba 5d94305af3 Properly handle odd depths in razor formula
A little bit more aggressive, but should be more
in line with the depths logic.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-28 19:38:44 +01:00
Marco Costalba e4fd9a2df7 Safer razoring formula
Add also the possibility to razor at ply one.
It is disable dby default but it seems stronger
against Stockfish itself. It is still not clear if
is stronger against other engines. By now leave
disabled.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-28 12:55:33 +01:00
Marco Costalba aedc6c6f1f Don't silently accept an option name mismatch
With this we could have found earlier the futility
name option bug!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-28 12:37:13 +01:00
Marco Costalba dae4e7df07 Tweak again futility margings
Lower margins near the leafs, higher at high depth.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-25 20:08:45 +01:00
Marco Costalba 96a32eec69 Expose new futility margin interface to UCI
Now futility margin it's actually a scale factor
to apply to the base ones.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-25 19:41:24 +01:00
Marco Costalba e46d3670fd Tweak futility margins
Less prune at the bottom and at the middle, a bit more
at the top.

After 747 games: +215 =345 -187 +13 elo

Also introduced a vector of margins, now that start to be a lot
it is a more flexible solution.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-25 12:05:45 +01:00
Marco Costalba 8cd5cb930d Try razoring only for depth > OnePly
Because razoring verification after qsearch() cuts more
then 40% of candidates, do not waste a costly qsearch for
nodes at depth one that will be probably discarded anyway
by futility.

Also tight razoring conditions to keep dangerous false
negatives below 0,05%. Still not clear if it is enough.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-24 09:35:57 +01:00
Marco Costalba 2feb9d5100 Futility pruning till ply 6 included
Seems good:

After 796 games: +211 = 393 -192 +8 elo

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-24 09:34:09 +01:00
Marco Costalba d11426c777 Fix a comment
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-23 12:02:10 +01:00
Marco Costalba e96f56adfa Merge futility pruning from Glaurung 2.2
It seems much more powerful then previous one.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-23 12:01:48 +01:00
Marco Costalba e3b03f13b3 Passed pawns: consider enemy rooks or queens from behind
Merged from Glaurung 2.2

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-22 12:10:15 +01:00
Marco Costalba 54b7da120f King safety: retire rook contact check
Merged from Glaurung 2.2

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-22 12:07:00 +01:00
Marco Costalba 3fafc9768a Set the 'Problem' variable only at ply == 1
Bug fix merged from Glaurung 2.2 for search_pv()

Added the same fix also to sp_search_pv() where
was missing.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-22 11:44:00 +01:00
Marco Costalba 23490bd825 Retire EvaluatePawnStorms and UseEasyMove constants
Merged from Glaurung 2.2

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 20:10:20 +01:00
Marco Costalba 61c6a3d5a0 Merge cosmetics from Glaurung 2.2
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 17:02:34 +01:00
Marco Costalba 31d4f0b734 Merge space weigth evaluation fromGlaurung 2.2
Is a new evaluation rule that gives bonus in midgame
to the side that has more space behind pawns for its
minor pieces.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 16:26:36 +01:00
Marco Costalba f178f0a291 Merged two new endgames from Glaurung 2.2
It is two bishop against a knight and two minor
pieces against one minor piece.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 15:38:10 +01:00
Marco Costalba 72ca727b38 SEE: add support for enpassant moves
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 10:44:41 +01:00
Marco Costalba 2d0146fe1d Call poll() before to check for stopped search
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 10:44:12 +01:00
Marco Costalba 96d0501735 Less aggressive razoring
Use a margin to compare with beta so that positions
that after the verifying qsearch have gained a lot of points
are not discarded just becasue not above beta.

Also remove the second condition on depth <= OnePly, it
was too risky and added only a 2% more of pruned nodes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-21 10:42:39 +01:00
Marco Costalba b58ad355ca Revert: "Do not razor when in check"
It is slightly weaker after 500 games. Keep the
check on mate values.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-18 22:01:56 +01:00
Marco Costalba 17000d1ea0 Trigger of PawnEndgameExtension if capture is not a pawn
Instead of a rook.

This gives an unexpected graeat increase!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-17 19:36:51 +01:00
Marco Costalba b09cbaebb9 search_pv: an enpassant move is a capture
Fix the logic in search_pv and sp_search_pv

An additional issue to consider is that a castle move
is not a capture but destination square is not empty.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-16 22:20:53 +01:00
Marco Costalba 725c504a5f qsearch: take in account enpassant in futility formula
Should not change anything at ELO level but it is
the correct thing to do.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-16 22:20:42 +01:00
Marco Costalba bfbfc24d07 qsearch: do not call evaluate when in check
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-16 22:20:18 +01:00
Marco Costalba a55b06d3c9 Restore development versioning and LSN filtering
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-15 22:23:03 +01:00
Marco Costalba ecc19381b4 Do not razor while in check or value is mate
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-15 22:20:03 +01:00
Marco Costalba dae2f600d6 Do not null search when beta is a mate value
Also do not return unproven mates in null search.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-15 22:19:25 +01:00
Marco Costalba 4c294932e7 Better document null move dynamic reduction
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-15 22:18:56 +01:00
Marco Costalba c831b00544 Introduce beta counters to order moves at ply one
Instead of number of searched nodes use the number of
opponent beta-cutoff occurred under the move subtree.

After 570 games 1+0 we have: +150 =288 -132 (+11 ELO)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-15 22:13:42 +01:00
Marco Costalba 5b853c9be6 Debugging: move debug function definitions in misc.cpp
Also activate writing on log file.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-14 17:26:05 +01:00
Marco Costalba 5f8f83bc05 Debugging: print to file
Print debug info on log file, not only on std::cout

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-14 14:57:17 +01:00
Marco Costalba 8ee3124487 Fix two bugs in ok_to_prune() (2)
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-12 22:27:43 +01:00
Marco Costalba 07b45151d2 Disable "Null driven IID" by default
Testing is not clear. Probably we need to test
at deeper depths to have some clear results.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-11 20:39:08 +01:00
Marco Costalba 9e3ab9099f Null move only for depth > OnePly
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-11 20:38:52 +01:00
Marco Costalba f09884d72f Null driven IID: remove IIDMargin from see() condition
This seems to cut searched nodes also more.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-10 21:43:09 +01:00
Marco Costalba ab29d8df67 Fix inflate pawns.cpp fallout
Catched counting the nodes searched at
fixed depth. A quick and reliable cross check,
expecially in inflate only patches.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-10 21:35:07 +01:00
Marco Costalba 389dc0e83b Add behind_bb() helper to simplify code logic
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-10 00:14:24 +01:00
Marco Costalba 67aac4889e Space inflate pawns.cpp
Hopefully no functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-10 00:14:15 +01:00
Marco Costalba aaad48464b Add a see() function that take only destination square
In this case firstlocates the least valuable attacker, if any,
then proceed as usual.

This will be used by next patch.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-10 00:13:59 +01:00
Marco Costalba 235df6a887 Stockfish 1.1a
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-08 12:03:46 +01:00
Marco Costalba 8d86c87e1e Add "Null driven IID" UCI option (default true)
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-08 10:57:40 +01:00
Marco Costalba c172af1b61 Null move driven internal iterative deepening
When a null move fails low due to a capture, try
to detect if without the capture we are above beta,
in this case there is a good possibility this is
a cut-node and the capture is just a null move
artifact due to side to move change. So if we still
don't have a TT move it's a good time to start an IID.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-08 10:46:52 +01:00
Marco Costalba b7c36d078b Stockfish 1.1
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 17:18:59 +01:00
Marco Costalba db1b0bfa1d Revert see() shortcut for LxH and equal captures
It happens that more then 70% of cases are HxL, where
we call see() anyway. The mesured saving of calling
see is about 0,5% of total time, but considering the
added burden in score_captures() the saving is only
0,35% locally and due to more difficult inlining of
the function it ends up that we have no advantage at all,
possibly a small slow down!

So revert.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 12:32:23 +01:00
Marco Costalba 6f946f823c Fix two gcc warnings in san.cpp
One good, the other silly.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 12:32:11 +01:00
Marco Costalba 2de78ebc7b Fix an Intel warning in san.cpp
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 12:31:48 +01:00
Marco Costalba 32934c0c8d MovePicker: avoid calling see() for LxH and equal captures
No functional change but should speed-up the captures scoring.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 11:37:19 +01:00
Marco Costalba b4fcfed55b Null capture pruning
Null move can fail low because of a capture artifact due
to the side to move change. Try to detect this condition
and fail high instead.

This pruning is very powerful, around 7% of nodes, but is
still experimental so is disabled by default.

Set UseNullCapturePruning to true to enable.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 11:31:14 +01:00
Marco Costalba dc2302b701 Position::move_is_capture() does not handle MOVE_NONE
Actually square 0 can be dirty, so that move_is_capture(0)
can return any random values.

Add an assert to be sure it is caught.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 11:18:31 +01:00
Marco Costalba 268c12bf31 Allow to call Position::print() from MovePicker
Fix a recursion issue that gives a stack overflow.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-06 11:18:08 +01:00
Marco Costalba 9f943a132a san.cpp: rewrite broken move_from_san
Use a state machine to parse the input. Fixed
the many broken cases.

Tested on more then 15 milions nodes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-05 09:06:51 +01:00
Marco Costalba 20390fcb3c san.cpp cleanup
Hopefully no functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-12-05 09:06:30 +01:00
Marco Costalba d95b9189d8 Revert opponent time advantage logic
Strength increase was due to an hidden bug introduced
by the patch, namely the time per move to /30 instead
of /40 (see previous patch).

After testing this feature do not add any substantial
increase so is removed.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-30 19:23:33 +01:00
Marco Costalba 9d5d69e435 Revert sigmoid interpolator
After deep test (1000 games) it seems do not improve anything,
actually seems slightly weaker.

So remove it for now.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-30 21:16:47 +01:00
Marco Costalba 08265aef81 san.cpp pass position as constant reference
Make a copy of the position when needed instead
of passing as a reference. It is cleaner and
let us to simplify also Position::print()

A small space inflate while there.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-30 01:25:16 +01:00
Marco Costalba 9d1e4d041d piece_type_to_char() default argument in declaration
Default argument should be in declaration where it
is visible through header include, not in definition.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-30 00:38:33 +01:00
Marco Costalba cff3a6d33e Revert threat move ordering
Does not seem to improve anything.

Anyhow idea is nice, maybe we still have to find
correct recipe.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-26 11:22:30 +01:00
Marco Costalba 20a2ca366f Tweak allocated time per move
It seems better to give more time in middle game then
at the end.

Also Toga uses the same limit.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-25 20:41:06 +01:00
Marco Costalba c3ba5fb9d3 Rewrok the extendeable patch
Cleanup and document.

The real functional change is that not mate threat
moves are never pruned, as could happen before.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-25 20:40:49 +01:00
Marco Costalba eba8925d81 MovePicker: take advantage of threat move for ordering
If the null move was refuted by a capture then give a
bonus if we move away the captured piece.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-25 20:40:34 +01:00
Marco Costalba ee9f650242 Use extendable instead of depth extension
We can have depth(0) also in problematic cases
according to how extensions are tweaked by the user.

In any case we don't want to prune these moves.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-25 20:40:12 +01:00
Marco Costalba 8a0dd93c56 Generate moves for powerful pieces first
This seems to reduce searched nodes by a
surprising 2.5%

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-24 20:43:29 +01:00
Marco Costalba 5a72ff128e Benchmark: print nodes searched at the end of testing
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-24 20:43:11 +01:00
Marco Costalba 3f63dd1023 Easy debug macro enabling
Now you don't need to toggle show_debug_xxxx anymore

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-22 15:59:26 +01:00
Marco Costalba 62ab7e4612 Introduce node limited benchmarking
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-22 11:56:14 +01:00
Marco Costalba 1867785231 Introduce depth limited benchmarking
Also print some more info.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-22 11:02:05 +01:00
Marco Costalba bac4da70c9 Remove an include in movepick.h
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Signed-off-by: unknown <Marco@.(none)>
2008-11-19 22:15:41 +01:00
Marco Costalba 8189ae9e1c Fix a silly bug that disabled second killer
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Signed-off-by: unknown <Marco@.(none)>
2008-11-19 22:15:26 +01:00
Marco Costalba 1d525bb45f qsearch: restore pruning of pv nodes with negative SEE
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Signed-off-by: unknown <Marco@.(none)>
2008-11-19 22:15:14 +01:00
Marco Costalba da7d872eda Fix Intel warnings and init_search_stack argument
Should be a reference not a copy!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:46:12 +01:00
Marco Costalba 49d52b8266 Set killer slots number to 2
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:48 +01:00
Marco Costalba 75d001addd MovePicker: fix a nasty bug in EvalInfo optimization
EvalInfo has missing attack info when a specialized
endgame function is used.

We missed this case and were using an empty attack bitboard
instead so that no captures were generated for endgames.

After testing the EvalInfo optimization gave worst results,
so after a (long) debug session this nasty bug was found.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:48 +01:00
Marco Costalba 7daaf03b39 Add and use update_killers()
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:48 +01:00
Marco Costalba 2e778445d5 Add and use move_is_killer() helper
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:48 +01:00
Marco Costalba 93bc05cf69 Convert killers to a vector
Add infrastructure to threat killer moves as a vector,
this will allow us to easily parametrize the number of
killer moves, instead of hardcode this value to two as is now.

This patch just add the infrastructure, no functional
change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:47 +01:00
Marco Costalba a7227ac26f qsearch: do not prune pv nodes with negative SEE
Also small micro-optimization, take a line out of
the moves loop.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:47 +01:00
Marco Costalba bb0da595a7 Disable per-square MVV/LVA for now
Needs more testing.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:47 +01:00
Marco Costalba 20d7197a9b MovePicker: use EvalInfo to skip generating captures
When we know already no captures are possible in a given
position.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:46 +01:00
Marco Costalba f4758ced90 Position::to_fen(): fix a bug in side to move representation
Was introduced almost two months ago in patch:
"Space inflate Position::to_fen()"

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:46 +01:00
Marco Costalba 3c05bd70eb Print the move in addition to position
Teach Position::print() to optionally print a
given move in san notation in addition to
the ASCII representation of the board.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:46 +01:00
Marco Costalba 7000e100bd Enable per-square MVV/LVA
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:46 +01:00
Marco Costalba 2ed22e4fc8 MovePicker:find bad captures during scoring
Instead of pospone until picking. No functional
change and probably no performance change but it is
needed for following patch.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:46 +01:00
Marco Costalba 940c53c366 MovePicker: introduce per square MVV/LVA ordering
Just added the infrastructure, no functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:45 +01:00
Marco Costalba 4df8651c82 Fix hashfull info
Do not count has a replacement when a TT entry is
written in an empty slot.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-16 12:37:45 +01:00
Marco Costalba 2d4e2bc62a Fix in ok_to_history(): castle move is not a capture
It is erroneusly considered a capture because king
moves on the same square of the rook.

Use the correct function Position::move_is_capture()
instead of the open coded (and buggy) one.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:40 +01:00
Marco Costalba d89a03cc35 Small tidyup of TranspositionTable::store()
Hopefully without bugs this time!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:40 +01:00
Marco Costalba 34b1d0538b Fix a logic bug in TranspositionTable::store()
Make the logic work as advertised in the function
description.

Still a fallback from TT cleanup.

This should be less serious then the one in retrieve(),
but it's still a bug.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:40 +01:00
Marco Costalba cdf1f23bc5 Micro optimization of update_history()
Remove an useless comparison.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:40 +01:00
Marco Costalba e86468cc63 Use cut-off checks in qsearch as killer moves
Killers should not be captures, but checks are not
and are produced also in qsearch.

Use this information, will be useful for move ordering.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:40 +01:00
Marco Costalba e7bad2687a Smaller null move reduction when depth is high
Lower probability to miss something important.

It seems to increase strenght. Idea form Cyclone.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:39 +01:00
Marco Costalba 3f610e2b13 Introduce LastIterations variable
Is set during the last iteration.

Sometime also during the second last.

During the last iteration is set in the 95% of cases.

During the second last is set in the 40% of cases.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:39 +01:00
Marco Costalba 6cc11272e2 Restore development versioning and LSN filtering
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-10 19:19:39 +01:00
Marco Costalba a28c17ecb6 Fix a missed initialization in get_option_value()
Spotted and reported by Dann Corbit.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
2008-11-04 20:59:11 +01:00
60 changed files with 3878 additions and 3824 deletions
+3 -10
View File
@@ -2,13 +2,13 @@
[PolyGlot] [PolyGlot]
EngineDir = . EngineDir = .
EngineCommand = ./glaurung EngineCommand = ./stockfish
Book = false Book = false
BookFile = book.bin BookFile = book.bin
Log = true Log = true
LogFile = glaurung.log LogFile = stockfish.log
Resign = true Resign = true
ResignScore = 600 ResignScore = 600
@@ -34,8 +34,7 @@ King Safety Coefficient = 40
King Safety X Intercept = 0 King Safety X Intercept = 0
King Safety Max Slope = 30 King Safety Max Slope = 30
King Safety Max Value = 500 King Safety Max Value = 500
Queen Contact Check Bonus = 4 Queen Contact Check Bonus = 3
Rook Contact Check Bonus = 2
Queen Check Bonus = 2 Queen Check Bonus = 2
Rook Check Bonus = 1 Rook Check Bonus = 1
Bishop Check Bonus = 1 Bishop Check Bonus = 1
@@ -57,14 +56,8 @@ Pawn Endgame Extension (non-PV nodes) = 2
Full Depth Moves (PV nodes) = 14 Full Depth Moves (PV nodes) = 14
Full Depth Moves (non-PV nodes) = 3 Full Depth Moves (non-PV nodes) = 3
Threat Depth = 5 Threat Depth = 5
Selective Plies = 7
Futility Pruning (Main Search) = true Futility Pruning (Main Search) = true
Futility Pruning (Quiescence Search) = true Futility Pruning (Quiescence Search) = true
Futility Margin 0 = 50
Futility Margin 1 = 100
Futility Margin 2 = 300
Maximum Razoring Depth = 3
Razoring Margin = 300
Randomness = 0 Randomness = 0
Minimum Split Depth = 4 Minimum Split Depth = 4
Maximum Number of Threads per Split Point = 5 Maximum Number of Threads per Split Point = 5
+4 -4
View File
@@ -24,9 +24,9 @@
EXE = stockfish EXE = stockfish
OBJS = bitboard.o color.o pawns.o material.o endgame.o evaluate.o main.o \ OBJS = bitboard.o pawns.o material.o endgame.o evaluate.o main.o \
misc.o move.o movegen.o history.o movepick.o search.o piece.o \ misc.o move.o movegen.o history.o movepick.o search.o piece.o \
position.o square.o direction.o tt.o value.o uci.o ucioption.o \ position.o direction.o tt.o value.o uci.o ucioption.o \
mersenne.o book.o bitbase.o san.o benchmark.o mersenne.o book.o bitbase.o san.o benchmark.o
@@ -37,7 +37,7 @@ OBJS = bitboard.o color.o pawns.o material.o endgame.o evaluate.o main.o \
all: $(EXE) .depend all: $(EXE) .depend
clean: clean:
$(RM) *.o .depend glaurung $(RM) *.o .depend stockfish
### ###
@@ -94,7 +94,7 @@ CXXFLAGS += -wd383,869,981
# Profiler guided optimization with the Intel C++ compiler. To use it, first # Profiler guided optimization with the Intel C++ compiler. To use it, first
# create the directory ./profdata if it does not already exist, and delete its # create the directory ./profdata if it does not already exist, and delete its
# contents if it does exist. Then compile with -prof_gen, and run the # contents if it does exist. Then compile with -prof_gen, and run the
# resulting binary for a while (for instance, do ./glaurung bench 128 1, and # resulting binary for a while (for instance, do ./stockfish bench 128 1, and
# wait 15 minutes for the benchmark to complete). Then do a 'make clean', and # wait 15 minutes for the benchmark to complete). Then do a 'make clean', and
# recompile with -prof_use. # recompile with -prof_use.
+24 -5
View File
@@ -71,8 +71,8 @@ void benchmark(const std::string& commandLine) {
std::istringstream csVal(commandLine); std::istringstream csVal(commandLine);
std::istringstream csStr(commandLine); std::istringstream csStr(commandLine);
std::string ttSize, threads, fileName; std::string ttSize, threads, fileName, limitType;
int val, secsPerPos; int val, secsPerPos, maxDepth, maxNodes;
csStr >> ttSize; csStr >> ttSize;
csVal >> val; csVal >> val;
@@ -95,8 +95,18 @@ void benchmark(const std::string& commandLine) {
set_option_value("Use Search Log", "true"); set_option_value("Use Search Log", "true");
set_option_value("Search Log Filename", "bench.txt"); set_option_value("Search Log Filename", "bench.txt");
csVal >> secsPerPos; csVal >> val;
csVal >> fileName; csVal >> fileName;
csVal >> limitType;
secsPerPos = maxDepth = maxNodes = 0;
if (limitType == "time")
secsPerPos = val * 1000;
else if (limitType == "depth")
maxDepth = val;
else
maxNodes = val;
std::vector<std::string> positions; std::vector<std::string> positions;
@@ -121,12 +131,21 @@ void benchmark(const std::string& commandLine) {
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
positions.push_back(std::string(BenchmarkPositions[i])); positions.push_back(std::string(BenchmarkPositions[i]));
int startTime = get_system_time();
std::vector<std::string>::iterator it; std::vector<std::string>::iterator it;
for (it = positions.begin(); it != positions.end(); ++it) int cnt = 1;
int64_t totalNodes = 0;
for (it = positions.begin(); it != positions.end(); ++it, ++cnt)
{ {
Move moves[1] = {MOVE_NONE}; Move moves[1] = {MOVE_NONE};
int dummy[2] = {0, 0}; int dummy[2] = {0, 0};
Position pos(*it); Position pos(*it);
think(pos, true, false, 0, dummy, dummy, 0, 0, 0, secsPerPos * 1000, moves); std::cout << "\nProcessing position " << cnt << '/' << positions.size() << std::endl << std::endl;
think(pos, true, false, 0, dummy, dummy, 0, maxDepth, maxNodes, secsPerPos, moves);
totalNodes += nodes_searched();
} }
std::cout << "\nProcessing time (ms) " << get_system_time() - startTime << std::endl
<< "Nodes searched " << totalNodes << std::endl
<< "Press any key to exit" << std::endl;
std::cin >> fileName;
} }
+2 -1
View File
@@ -89,7 +89,8 @@ void generate_kpk_bitbase(uint8_t bitbase[]) {
int i, j, b; int i, j, b;
for(i = 0; i < 24576; i++) { for(i = 0; i < 24576; i++) {
for(b = 0, j = 0; j < 8; b |= (compress_result(Bitbase[8*i+j]) << j), j++); for(b = 0, j = 0; j < 8; b |= (compress_result(Bitbase[8*i+j]) << j), j++);
bitbase[i] = b; assert(b == int(uint8_t(b)));
bitbase[i] = (uint8_t)b;
} }
// Release allocated memory: // Release allocated memory:
+13 -61
View File
@@ -38,64 +38,6 @@
#include "direction.h" #include "direction.h"
////
//// Constants and variables
////
const Bitboard SquaresByColorBB[2] = {BlackSquaresBB, WhiteSquaresBB};
const Bitboard FileBB[8] = {
FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
};
const Bitboard NeighboringFilesBB[8] = {
FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
};
const Bitboard ThisAndNeighboringFilesBB[8] = {
FileABB|FileBBB, FileABB|FileBBB|FileCBB,
FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
};
const Bitboard RankBB[8] = {
Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
};
const Bitboard RelativeRankBB[2][8] = {
{
Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
},
{
Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB
}
};
const Bitboard InFrontBB[2][8] = {
{
Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank6BB | Rank7BB | Rank8BB,
Rank7BB | Rank8BB,
Rank8BB,
EmptyBoardBB
},
{
EmptyBoardBB,
Rank1BB,
Rank2BB | Rank1BB,
Rank3BB | Rank2BB | Rank1BB,
Rank4BB | Rank3BB | Rank2BB | Rank1BB,
Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
}
};
#if defined(USE_COMPACT_ROOK_ATTACKS) #if defined(USE_COMPACT_ROOK_ATTACKS)
Bitboard RankAttacks[8][64], FileAttacks[8][64]; Bitboard RankAttacks[8][64], FileAttacks[8][64];
@@ -250,8 +192,8 @@ Bitboard BMask[64];
int BAttackIndex[64]; int BAttackIndex[64];
Bitboard BAttacks[0x1480]; Bitboard BAttacks[0x1480];
Bitboard SetMaskBB[64]; Bitboard SetMaskBB[65];
Bitboard ClearMaskBB[64]; Bitboard ClearMaskBB[65];
Bitboard StepAttackBB[16][64]; Bitboard StepAttackBB[16][64];
Bitboard RayBB[64][8]; Bitboard RayBB[64][8];
@@ -270,6 +212,7 @@ Bitboard QueenPseudoAttacks[64];
//// ////
namespace { namespace {
void init_masks(); void init_masks();
void init_ray_bitboards(); void init_ray_bitboards();
void init_attacks(); void init_attacks();
@@ -433,6 +376,8 @@ namespace {
// be necessary to touch any of them. // be necessary to touch any of them.
void init_masks() { void init_masks() {
SetMaskBB[SQ_NONE] = 0ULL;
ClearMaskBB[SQ_NONE] = ~SetMaskBB[SQ_NONE];
for(Square s = SQ_A1; s <= SQ_H8; s++) { for(Square s = SQ_A1; s <= SQ_H8; s++) {
SetMaskBB[s] = (1ULL << s); SetMaskBB[s] = (1ULL << s);
ClearMaskBB[s] = ~SetMaskBB[s]; ClearMaskBB[s] = ~SetMaskBB[s];
@@ -448,7 +393,7 @@ namespace {
void init_ray_bitboards() { void init_ray_bitboards() {
int d[8] = {1, -1, 16, -16, 17, -17, 15, -15}; int d[8] = {1, -1, 16, -16, 17, -17, 15, -15};
for(int i = 0; i < 128; i = i + 9 & ~8) { for(int i = 0; i < 128; i = (i + 9) & ~8) {
for(int j = 0; j < 8; j++) { for(int j = 0; j < 8; j++) {
RayBB[(i&7)|((i>>4)<<3)][j] = EmptyBoardBB; RayBB[(i&7)|((i>>4)<<3)][j] = EmptyBoardBB;
for(int k = i + d[j]; (k & 0x88) == 0; k += d[j]) for(int k = i + d[j]; (k & 0x88) == 0; k += d[j])
@@ -534,8 +479,15 @@ namespace {
for(i = 0; i < 64; i++) { for(i = 0; i < 64; i++) {
attackIndex[i] = index; attackIndex[i] = index;
mask[i] = sliding_attacks(i, 0ULL, 4, deltas, 1, 6, 1, 6); mask[i] = sliding_attacks(i, 0ULL, 4, deltas, 1, 6, 1, 6);
#if defined(USE_32BIT_ATTACKS)
j = (1 << (32 - shift[i]));
#else
j = (1 << (64 - shift[i])); j = (1 << (64 - shift[i]));
#endif
for(k = 0; k < j; k++) { for(k = 0; k < j; k++) {
#if defined(USE_32BIT_ATTACKS) #if defined(USE_32BIT_ATTACKS)
b = index_to_bitboard(k, mask[i]); b = index_to_bitboard(k, mask[i]);
attacks[index + attacks[index +
+80 -25
View File
@@ -84,8 +84,7 @@ const Bitboard EmptyBoardBB = 0ULL;
const Bitboard WhiteSquaresBB = 0x55AA55AA55AA55AAULL; const Bitboard WhiteSquaresBB = 0x55AA55AA55AA55AAULL;
const Bitboard BlackSquaresBB = 0xAA55AA55AA55AA55ULL; const Bitboard BlackSquaresBB = 0xAA55AA55AA55AA55ULL;
const Bitboard SquaresByColorBB[2] = { BlackSquaresBB, WhiteSquaresBB };
extern const Bitboard SquaresByColorBB[2];
const Bitboard FileABB = 0x0101010101010101ULL; const Bitboard FileABB = 0x0101010101010101ULL;
const Bitboard FileBBB = 0x0202020202020202ULL; const Bitboard FileBBB = 0x0202020202020202ULL;
@@ -96,9 +95,21 @@ const Bitboard FileFBB = 0x2020202020202020ULL;
const Bitboard FileGBB = 0x4040404040404040ULL; const Bitboard FileGBB = 0x4040404040404040ULL;
const Bitboard FileHBB = 0x8080808080808080ULL; const Bitboard FileHBB = 0x8080808080808080ULL;
extern const Bitboard FileBB[8]; const Bitboard FileBB[8] = {
extern const Bitboard NeighboringFilesBB[8]; FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
extern const Bitboard ThisAndNeighboringFilesBB[8]; };
const Bitboard NeighboringFilesBB[8] = {
FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
};
const Bitboard ThisAndNeighboringFilesBB[8] = {
FileABB|FileBBB, FileABB|FileBBB|FileCBB,
FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
};
const Bitboard Rank1BB = 0xFFULL; const Bitboard Rank1BB = 0xFFULL;
const Bitboard Rank2BB = 0xFF00ULL; const Bitboard Rank2BB = 0xFF00ULL;
@@ -109,12 +120,38 @@ const Bitboard Rank6BB = 0xFF0000000000ULL;
const Bitboard Rank7BB = 0xFF000000000000ULL; const Bitboard Rank7BB = 0xFF000000000000ULL;
const Bitboard Rank8BB = 0xFF00000000000000ULL; const Bitboard Rank8BB = 0xFF00000000000000ULL;
extern const Bitboard RankBB[8]; const Bitboard RankBB[8] = {
extern const Bitboard RelativeRankBB[2][8]; Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
extern const Bitboard InFrontBB[2][8]; };
extern Bitboard SetMaskBB[64]; const Bitboard RelativeRankBB[2][8] = {
extern Bitboard ClearMaskBB[64]; { Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB },
{ Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB }
};
const Bitboard InFrontBB[2][8] = {
{ Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank5BB | Rank6BB | Rank7BB | Rank8BB,
Rank6BB | Rank7BB | Rank8BB,
Rank7BB | Rank8BB,
Rank8BB,
EmptyBoardBB
},
{ EmptyBoardBB,
Rank1BB,
Rank2BB | Rank1BB,
Rank3BB | Rank2BB | Rank1BB,
Rank4BB | Rank3BB | Rank2BB | Rank1BB,
Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
}
};
extern Bitboard SetMaskBB[65];
extern Bitboard ClearMaskBB[65];
extern Bitboard StepAttackBB[16][64]; extern Bitboard StepAttackBB[16][64];
extern Bitboard RayBB[64][8]; extern Bitboard RayBB[64][8];
@@ -124,13 +161,17 @@ extern Bitboard PassedPawnMask[2][64];
extern Bitboard OutpostMask[2][64]; extern Bitboard OutpostMask[2][64];
#if defined(USE_COMPACT_ROOK_ATTACKS) #if defined(USE_COMPACT_ROOK_ATTACKS)
extern Bitboard RankAttacks[8][64], FileAttacks[8][64]; extern Bitboard RankAttacks[8][64], FileAttacks[8][64];
#else #else
extern const uint64_t RMult[64]; extern const uint64_t RMult[64];
extern const int RShift[64]; extern const int RShift[64];
extern Bitboard RMask[64]; extern Bitboard RMask[64];
extern int RAttackIndex[64]; extern int RAttackIndex[64];
extern Bitboard RAttacks[0x19000]; extern Bitboard RAttacks[0x19000];
#endif // defined(USE_COMPACT_ROOK_ATTACKS) #endif // defined(USE_COMPACT_ROOK_ATTACKS)
extern const uint64_t BMult[64]; extern const uint64_t BMult[64];
@@ -212,7 +253,7 @@ inline Bitboard this_and_neighboring_files_bb(Square s) {
/// relative_rank_bb() takes a color and a rank as input, and returns a bitboard /// relative_rank_bb() takes a color and a rank as input, and returns a bitboard
/// representing all squares on the given rank from the given color's point of /// representing all squares on the given rank from the given color's point of
/// view. For instance, relative_rank_bb(WHITE, 7) gives all squares on the /// view. For instance, relative_rank_bb(WHITE, 7) gives all squares on the
/// 7th rank, while relative_rank_bb(BLACK, 7) gives all squares on the 2nd /// 7th rank, while relative_rank_bb(BLACK, 7) gives all squares on the 2nd
/// rank. /// rank.
@@ -236,6 +277,19 @@ inline Bitboard in_front_bb(Color c, Square s) {
} }
/// behind_bb() takes a color and a rank or square as input, and returns a
/// bitboard representing all the squares on all ranks behind of the rank
/// (or square), from the given color's point of view.
inline Bitboard behind_bb(Color c, Rank r) {
return InFrontBB[opposite_color(c)][r];
}
inline Bitboard behind_bb(Color c, Square s) {
return in_front_bb(opposite_color(c), square_rank(s));
}
/// ray_bb() gives a bitboard representing all squares along the ray in a /// ray_bb() gives a bitboard representing all squares along the ray in a
/// given direction from a given square. /// given direction from a given square.
@@ -382,24 +436,25 @@ inline int count_1s_max_15(Bitboard b) {
inline int count_1s(Bitboard b) { inline int count_1s(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b); unsigned w = unsigned(b >> 32), v = unsigned(b);
v = v - ((v >> 1) & 0x55555555); v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w = w - ((w >> 1) & 0x55555555); w -= (w >> 1) & 0x55555555;
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
w = (w & 0x33333333) + ((w >> 2) & 0x33333333); w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
v = (v + (v >> 4)) & 0x0F0F0F0F; v = ((v >> 4) + v) & 0x0F0F0F0F; // 0-8 in 8 bits
w = (w + (w >> 4)) & 0x0F0F0F0F; v += (((w >> 4) + w) & 0x0F0F0F0F); // 0-16 in 8 bits
v = ((v+w) * 0x01010101) >> 24; // mul is fast on amd procs v *= 0x01010101; // mul is fast on amd procs
return int(v); return int(v >> 24);
} }
inline int count_1s_max_15(Bitboard b) { inline int count_1s_max_15(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b); unsigned w = unsigned(b >> 32), v = unsigned(b);
v = v - ((v >> 1) & 0x55555555); v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w = w - ((w >> 1) & 0x55555555); w -= (w >> 1) & 0x55555555;
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
w = (w & 0x33333333) + ((w >> 2) & 0x33333333); w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
v = ((v+w) * 0x11111111) >> 28; v += w; // 0-8 in 4 bits
return int(v); v *= 0x11111111;
return int(v >> 28);
} }
#elif defined(BITCOUNT_SWAR_64) #elif defined(BITCOUNT_SWAR_64)
-36
View File
@@ -1,36 +0,0 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008 Marco Costalba
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////
//// Includes
////
#include "color.h"
////
//// Functions
////
/// color_is_ok(), for debugging:
bool color_is_ok(Color c) {
return c == WHITE || c == BLACK;
}
+4 -14
View File
@@ -21,12 +21,6 @@
#if !defined(COLOR_H_INCLUDED) #if !defined(COLOR_H_INCLUDED)
#define COLOR_H_INCLUDED #define COLOR_H_INCLUDED
////
//// Includes
////
#include "misc.h"
//// ////
//// Types //// Types
@@ -44,18 +38,14 @@ enum Color {
//// ////
inline Color operator+ (Color c, int i) { return Color(int(c) + i); } inline Color operator+ (Color c, int i) { return Color(int(c) + i); }
inline void operator++ (Color &c, int i) { c = Color(int(c) + 1); } inline void operator++ (Color &c, int) { c = Color(int(c) + 1); }
inline Color opposite_color(Color c) { inline Color opposite_color(Color c) {
return Color(int(c) ^ 1); return Color(int(c) ^ 1);
} }
inline bool color_is_ok(Color c) {
//// return c == WHITE || c == BLACK;
//// Prototypes }
////
extern bool color_is_ok(Color c);
#endif // !defined(COLOR_H_INCLUDED) #endif // !defined(COLOR_H_INCLUDED)
+13 -4
View File
@@ -38,8 +38,8 @@ enum Direction {
}; };
enum SignedDirection { enum SignedDirection {
SIGNED_DIR_E = 0, SIGNED_DIR_W = 1, SIGNED_DIR_E = 0, SIGNED_DIR_W = 1,
SIGNED_DIR_N = 2, SIGNED_DIR_S = 3, SIGNED_DIR_N = 2, SIGNED_DIR_S = 3,
SIGNED_DIR_NE = 4, SIGNED_DIR_SW = 5, SIGNED_DIR_NE = 4, SIGNED_DIR_SW = 5,
SIGNED_DIR_NW = 6, SIGNED_DIR_SE = 7, SIGNED_DIR_NW = 6, SIGNED_DIR_SE = 7,
SIGNED_DIR_NONE = 8 SIGNED_DIR_NONE = 8
@@ -58,9 +58,11 @@ extern uint8_t SignedDirectionTable[64][64];
//// Inline functions //// Inline functions
//// ////
inline void operator++ (Direction &d, int) { d = Direction(int(d) + 1); } inline void operator++ (Direction& d, int) {
d = Direction(int(d) + 1);
}
inline void operator++ (SignedDirection &d, int) { inline void operator++ (SignedDirection& d, int) {
d = SignedDirection(int(d) + 1); d = SignedDirection(int(d) + 1);
} }
@@ -72,6 +74,13 @@ inline SignedDirection signed_direction_between_squares(Square s1, Square s2) {
return SignedDirection(SignedDirectionTable[s1][s2]); return SignedDirection(SignedDirectionTable[s1][s2]);
} }
inline int direction_is_diagonal(Square s1, Square s2) {
return DirectionTable[s1][s2] & 2;
}
inline bool direction_is_straight(Square s1, Square s2) {
return DirectionTable[s1][s2] < 2;
}
//// ////
//// Prototypes //// Prototypes
+475 -403
View File
File diff suppressed because it is too large Load Diff
+63 -179
View File
@@ -34,149 +34,61 @@
//// Types //// Types
//// ////
/// Abstract base class for all special endgame evaluation functions: enum EndgameType {
class EndgameEvaluationFunction { // Evaluation functions
KXK, // Generic "mate lone king" eval
KBNK, // KBN vs K
KPK, // KP vs K
KRKP, // KR vs KP
KRKB, // KR vs KB
KRKN, // KR vs KN
KQKR, // KQ vs KR
KBBKN, // KBB vs KN
KmmKm, // K and two minors vs K and one or two minors
// Scaling functions
KBPK, // KBP vs K
KQKRP, // KQ vs KRP
KRPKR, // KRP vs KR
KRPPKRP, // KRPP vs KRP
KPsK, // King and pawns vs king
KBPKB, // KBP vs KB
KBPPKB, // KBPP vs KB
KBPKN, // KBP vs KN
KNPK, // KNP vs K
KPKP // KP vs KP
};
/// Template abstract base class for all special endgame functions
template<typename T>
class EndgameFunctionBase {
public: public:
EndgameEvaluationFunction(Color c); EndgameFunctionBase(Color c) : strongerSide(c) { weakerSide = opposite_color(strongerSide); }
virtual ~EndgameEvaluationFunction() { } virtual ~EndgameFunctionBase() {}
virtual T apply(const Position&) = 0;
virtual Value apply(const Position &pos) =0;
protected: protected:
Color strongerSide, weakerSide; Color strongerSide, weakerSide;
}; };
typedef EndgameFunctionBase<Value> EndgameEvaluationFunctionBase;
typedef EndgameFunctionBase<ScaleFactor> EndgameScalingFunctionBase;
/// Subclasses for various concrete endgames:
// Generic "mate lone king" eval: /// Templates subclass for various concrete endgames
class KXKEvaluationFunction : public EndgameEvaluationFunction {
public: template<EndgameType>
KXKEvaluationFunction(Color c); struct EvaluationFunction : public EndgameEvaluationFunctionBase {
Value apply(const Position &pos); explicit EvaluationFunction(Color c): EndgameEvaluationFunctionBase(c) {}
Value apply(const Position&);
}; };
// KBN vs K: template<EndgameType>
class KBNKEvaluationFunction : public EndgameEvaluationFunction { struct ScalingFunction : public EndgameScalingFunctionBase {
public: explicit ScalingFunction(Color c) : EndgameScalingFunctionBase(c) {}
KBNKEvaluationFunction(Color c); ScaleFactor apply(const Position&);
Value apply(const Position &pos);
};
// KP vs K:
class KPKEvaluationFunction : public EndgameEvaluationFunction {
public:
KPKEvaluationFunction(Color c);
Value apply(const Position &pos);
};
// KR vs KP:
class KRKPEvaluationFunction : public EndgameEvaluationFunction {
public:
KRKPEvaluationFunction(Color c);
Value apply(const Position &pos);
};
// KR vs KB:
class KRKBEvaluationFunction : public EndgameEvaluationFunction {
public:
KRKBEvaluationFunction(Color c);
Value apply(const Position &pos);
};
// KR vs KN:
class KRKNEvaluationFunction : public EndgameEvaluationFunction {
public:
KRKNEvaluationFunction(Color c);
Value apply(const Position &pos);
};
// KQ vs KR:
class KQKREvaluationFunction : public EndgameEvaluationFunction {
public:
KQKREvaluationFunction(Color c);
Value apply(const Position &pos);
};
/// Abstract base class for all evaluation scaling functions:
class ScalingFunction {
public:
ScalingFunction(Color c);
virtual ~ScalingFunction() { }
virtual ScaleFactor apply(const Position &pos) =0;
protected:
Color strongerSide, weakerSide;
};
/// Subclasses for various concrete endgames:
// KBP vs K:
class KBPKScalingFunction : public ScalingFunction {
public:
KBPKScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KQ vs KRP:
class KQKRPScalingFunction: public ScalingFunction {
public:
KQKRPScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KRP vs KR:
class KRPKRScalingFunction : public ScalingFunction {
public:
KRPKRScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KRPP vs KRP:
class KRPPKRPScalingFunction : public ScalingFunction {
public:
KRPPKRPScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// King and pawns vs king:
class KPsKScalingFunction : public ScalingFunction {
public:
KPsKScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KBP vs KB:
class KBPKBScalingFunction : public ScalingFunction {
public:
KBPKBScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KBP vs KN:
class KBPKNScalingFunction : public ScalingFunction {
public:
KBPKNScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KNP vs K:
class KNPKScalingFunction : public ScalingFunction {
public:
KNPKScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
};
// KP vs KP:
class KPKPScalingFunction : public ScalingFunction {
public:
KPKPScalingFunction(Color c);
ScaleFactor apply(const Position &pos);
}; };
@@ -184,54 +96,26 @@ public:
//// Constants and variables //// Constants and variables
//// ////
// Generic "mate lone king" eval: extern EvaluationFunction<KXK> EvaluateKXK, EvaluateKKX; // Generic "mate lone king" eval
extern KXKEvaluationFunction EvaluateKXK, EvaluateKKX; extern EvaluationFunction<KBNK> EvaluateKBNK, EvaluateKKBN; // KBN vs K
extern EvaluationFunction<KPK> EvaluateKPK, EvaluateKKP; // KP vs K
// KBN vs K: extern EvaluationFunction<KRKP> EvaluateKRKP, EvaluateKPKR; // KR vs KP
extern KBNKEvaluationFunction EvaluateKBNK, EvaluateKKBN; extern EvaluationFunction<KRKB> EvaluateKRKB, EvaluateKBKR; // KR vs KB
extern EvaluationFunction<KRKN> EvaluateKRKN, EvaluateKNKR; // KR vs KN
// KP vs K: extern EvaluationFunction<KQKR> EvaluateKQKR, EvaluateKRKQ; // KQ vs KR
extern KPKEvaluationFunction EvaluateKPK, EvaluateKKP; extern EvaluationFunction<KBBKN> EvaluateKBBKN, EvaluateKNKBB; // KBB vs KN
extern EvaluationFunction<KmmKm> EvaluateKmmKm; // K and two minors vs K and one or two minors:
// KR vs KP:
extern KRKPEvaluationFunction EvaluateKRKP, EvaluateKPKR;
// KR vs KB:
extern KRKBEvaluationFunction EvaluateKRKB, EvaluateKBKR;
// KR vs KN:
extern KRKNEvaluationFunction EvaluateKRKN, EvaluateKNKR;
// KQ vs KR:
extern KQKREvaluationFunction EvaluateKQKR, EvaluateKRKQ;
// KBP vs K:
extern KBPKScalingFunction ScaleKBPK, ScaleKKBP;
// KQ vs KRP:
extern KQKRPScalingFunction ScaleKQKRP, ScaleKRPKQ;
// KRP vs KR:
extern KRPKRScalingFunction ScaleKRPKR, ScaleKRKRP;
// KRPP vs KRP:
extern KRPPKRPScalingFunction ScaleKRPPKRP, ScaleKRPKRPP;
// King and pawns vs king:
extern KPsKScalingFunction ScaleKPsK, ScaleKKPs;
// KBP vs KB:
extern KBPKBScalingFunction ScaleKBPKB, ScaleKBKBP;
// KBP vs KN:
extern KBPKNScalingFunction ScaleKBPKN, ScaleKNKBP;
// KNP vs K:
extern KNPKScalingFunction ScaleKNPK, ScaleKKNP;
// KP vs KP:
extern KPKPScalingFunction ScaleKPKPw, ScaleKPKPb;
extern ScalingFunction<KBPK> ScaleKBPK, ScaleKKBP; // KBP vs K
extern ScalingFunction<KQKRP> ScaleKQKRP, ScaleKRPKQ; // KQ vs KRP
extern ScalingFunction<KRPKR> ScaleKRPKR, ScaleKRKRP; // KRP vs KR
extern ScalingFunction<KRPPKRP> ScaleKRPPKRP, ScaleKRPKRPP; // KRPP vs KRP
extern ScalingFunction<KPsK> ScaleKPsK, ScaleKKPs; // King and pawns vs king
extern ScalingFunction<KBPKB> ScaleKBPKB, ScaleKBKBP; // KBP vs KB
extern ScalingFunction<KBPPKB> ScaleKBPPKB, ScaleKBKBPP; // KBPP vs KB
extern ScalingFunction<KBPKN> ScaleKBPKN, ScaleKNKBP; // KBP vs KN
extern ScalingFunction<KNPK> ScaleKNPK, ScaleKKNP; // KNP vs K
extern ScalingFunction<KPKP> ScaleKPKPw, ScaleKPKPb; // KP vs KP
//// ////
//// Prototypes //// Prototypes
+388 -356
View File
@@ -52,19 +52,23 @@ namespace {
int WeightPassedPawnsMidgame = 0x100; int WeightPassedPawnsMidgame = 0x100;
int WeightPassedPawnsEndgame = 0x100; int WeightPassedPawnsEndgame = 0x100;
int WeightKingSafety[2] = { 0x100, 0x100 }; int WeightKingSafety[2] = { 0x100, 0x100 };
int WeightSpace;
// Internal evaluation weights. These are applied on top of the evaluation // Internal evaluation weights. These are applied on top of the evaluation
// weights read from UCI parameters. The purpose is to be able to change // weights read from UCI parameters. The purpose is to be able to change
// the evaluation weights while keeping the default values of the UCI // the evaluation weights while keeping the default values of the UCI
// parameters at 100, which looks prettier. // parameters at 100, which looks prettier.
const int WeightMobilityMidgameInternal = 0x100; //
const int WeightMobilityEndgameInternal = 0x100; // Values modified by Joona Kiiski
const int WeightPawnStructureMidgameInternal = 0x100; const int WeightMobilityMidgameInternal = 0x0FA;
const int WeightPawnStructureEndgameInternal = 0x100; const int WeightMobilityEndgameInternal = 0x10A;
const int WeightPassedPawnsMidgameInternal = 0x100; const int WeightPawnStructureMidgameInternal = 0x0EC;
const int WeightPassedPawnsEndgameInternal = 0x100; const int WeightPawnStructureEndgameInternal = 0x0CD;
const int WeightKingSafetyInternal = 0x100; const int WeightPassedPawnsMidgameInternal = 0x108;
const int WeightKingOppSafetyInternal = 0x100; const int WeightPassedPawnsEndgameInternal = 0x109;
const int WeightKingSafetyInternal = 0x0F7;
const int WeightKingOppSafetyInternal = 0x101;
const int WeightSpaceInternal = 0x02F;
// Visually better to define tables constants // Visually better to define tables constants
typedef Value V; typedef Value V;
@@ -165,18 +169,19 @@ namespace {
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) // 8 V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) // 8
}; };
// Bonus for unstoppable passed pawns: // Bonus for unstoppable passed pawns
const Value UnstoppablePawnValue = Value(0x500); const Value UnstoppablePawnValue = Value(0x500);
// Rooks and queens on the 7th rank: // Rooks and queens on the 7th rank
const Value MidgameRookOn7thBonus = Value(50); const Value MidgameRookOn7thBonus = Value(47);
const Value EndgameRookOn7thBonus = Value(100); const Value EndgameRookOn7thBonus = Value(98);
const Value MidgameQueenOn7thBonus = Value(25); const Value MidgameQueenOn7thBonus = Value(27);
const Value EndgameQueenOn7thBonus = Value(50); const Value EndgameQueenOn7thBonus = Value(54);
// Rooks on open files:
const Value RookOpenFileBonus = Value(40); // Rooks on open files
const Value RookHalfOpenFileBonus = Value(20); const Value RookOpenFileBonus = Value(43);
const Value RookHalfOpenFileBonus = Value(19);
// Penalty for rooks trapped inside a friendly king which has lost the // Penalty for rooks trapped inside a friendly king which has lost the
// right to castle: // right to castle:
@@ -203,6 +208,19 @@ namespace {
((1ULL << SQ_A8) | (1ULL << SQ_H8)) ((1ULL << SQ_A8) | (1ULL << SQ_H8))
}; };
// The SpaceMask[color] contains area of the board which is consdered by
// the space evaluation. In the middle game, each side is given a bonus
// based on how many squares inside this area are safe and available for
// friendly minor pieces.
const Bitboard SpaceMask[2] = {
(1ULL<<SQ_C2) | (1ULL<<SQ_D2) | (1ULL<<SQ_E2) | (1ULL<<SQ_F2) |
(1ULL<<SQ_C3) | (1ULL<<SQ_D3) | (1ULL<<SQ_E3) | (1ULL<<SQ_F3) |
(1ULL<<SQ_C4) | (1ULL<<SQ_D4) | (1ULL<<SQ_E4) | (1ULL<<SQ_F4),
(1ULL<<SQ_C7) | (1ULL<<SQ_D7) | (1ULL<<SQ_E7) | (1ULL<<SQ_F7) |
(1ULL<<SQ_C6) | (1ULL<<SQ_D6) | (1ULL<<SQ_E6) | (1ULL<<SQ_F6) |
(1ULL<<SQ_C5) | (1ULL<<SQ_D5) | (1ULL<<SQ_E5) | (1ULL<<SQ_F5)
};
/// King safety constants and variables. The king safety scores are taken /// King safety constants and variables. The king safety scores are taken
/// from the array SafetyTable[]. Various little "meta-bonuses" measuring /// from the array SafetyTable[]. Various little "meta-bonuses" measuring
/// the strength of the attack are added up into an integer, which is used /// the strength of the attack are added up into an integer, which is used
@@ -215,8 +233,7 @@ namespace {
const int KnightAttackWeight = 2; const int KnightAttackWeight = 2;
// Bonuses for safe checks for each piece type. // Bonuses for safe checks for each piece type.
int QueenContactCheckBonus = 4; int QueenContactCheckBonus = 3;
int RookContactCheckBonus = 2;
int QueenCheckBonus = 2; int QueenCheckBonus = 2;
int RookCheckBonus = 1; int RookCheckBonus = 1;
int BishopCheckBonus = 1; int BishopCheckBonus = 1;
@@ -246,23 +263,23 @@ namespace {
// in init_safety(). // in init_safety().
Value SafetyTable[100]; Value SafetyTable[100];
// Pawn and material hash tables, indexed by the current thread id: // Pawn and material hash tables, indexed by the current thread id
PawnInfoTable *PawnTable[8] = {0, 0, 0, 0, 0, 0, 0, 0}; PawnInfoTable *PawnTable[8] = {0, 0, 0, 0, 0, 0, 0, 0};
MaterialInfoTable *MaterialTable[8] = {0, 0, 0, 0, 0, 0, 0, 0}; MaterialInfoTable *MaterialTable[8] = {0, 0, 0, 0, 0, 0, 0, 0};
// Sizes of pawn and material hash tables: // Sizes of pawn and material hash tables
const int PawnTableSize = 16384; const int PawnTableSize = 16384;
const int MaterialTableSize = 1024; const int MaterialTableSize = 1024;
// Array which gives the number of nonzero bits in an 8-bit integer: // Array which gives the number of nonzero bits in an 8-bit integer:
uint8_t BitCount8Bit[256]; uint8_t BitCount8Bit[256];
// Function prototypes: // Function prototypes
void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei); template<PieceType Piece>
void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei); void evaluate_pieces(const Position& p, Color us, EvalInfo& ei);
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei);
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei); template<>
void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei); void evaluate_pieces<KING>(const Position& p, Color us, EvalInfo &ei);
void evaluate_passed_pawns(const Position &pos, EvalInfo &ei); void evaluate_passed_pawns(const Position &pos, EvalInfo &ei);
void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us, void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us,
@@ -270,6 +287,7 @@ namespace {
void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us, void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us,
EvalInfo &ei); EvalInfo &ei);
void evaluate_space(const Position &p, Color us, EvalInfo &ei);
inline Value apply_weight(Value v, int w); inline Value apply_weight(Value v, int w);
Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]); Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]);
@@ -337,52 +355,22 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
// Evaluate pieces // Evaluate pieces
for (Color c = WHITE; c <= BLACK; c++) for (Color c = WHITE; c <= BLACK; c++)
{ {
// Knights evaluate_pieces<KNIGHT>(pos, c, ei);
for (int i = 0; i < pos.piece_count(c, KNIGHT); i++) evaluate_pieces<BISHOP>(pos, c, ei);
evaluate_knight(pos, pos.piece_list(c, KNIGHT, i), c, ei); evaluate_pieces<ROOK>(pos, c, ei);
evaluate_pieces<QUEEN>(pos, c, ei);
// Bishops // Sum up all attacked squares
for (int i = 0; i < pos.piece_count(c, BISHOP); i++) ei.attackedBy[c][0] = ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT]
evaluate_bishop(pos, pos.piece_list(c, BISHOP, i), c, ei); | ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
| ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING];
// Rooks
for (int i = 0; i < pos.piece_count(c, ROOK); i++)
evaluate_rook(pos, pos.piece_list(c, ROOK, i), c, ei);
// Queens
for(int i = 0; i < pos.piece_count(c, QUEEN); i++)
evaluate_queen(pos, pos.piece_list(c, QUEEN, i), c, ei);
// Special pattern: trapped bishops on a7/h7/a2/h2
Bitboard b = pos.bishops(c) & MaskA7H7[c];
while (b)
{
Square s = pop_1st_bit(&b);
evaluate_trapped_bishop_a7h7(pos, s, c, ei);
}
// Special pattern: trapped bishops on a1/h1/a8/h8 in Chess960:
if (Chess960)
{
b = pos.bishops(c) & MaskA1H1[c];
while (b)
{
Square s = pop_1st_bit(&b);
evaluate_trapped_bishop_a1h1(pos, s, c, ei);
}
}
// Sum up all attacked squares
ei.attackedBy[c][0] = ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT]
| ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
| ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING];
} }
// Kings. Kings are evaluated after all other pieces for both sides, // Kings. Kings are evaluated after all other pieces for both sides,
// because we need complete attack information for all pieces when computing // because we need complete attack information for all pieces when computing
// the king safety evaluation. // the king safety evaluation.
for (Color c = WHITE; c <= BLACK; c++) for (Color c = WHITE; c <= BLACK; c++)
evaluate_king(pos, pos.king_square(c), c, ei); evaluate_pieces<KING>(pos, c, ei);
// Evaluate passed pawns. We evaluate passed pawns for both sides at once, // Evaluate passed pawns. We evaluate passed pawns for both sides at once,
// because we need to know which side promotes first in positions where // because we need to know which side promotes first in positions where
@@ -407,6 +395,13 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
ei.mgValue += ei.pi->kingside_storm_value(WHITE) ei.mgValue += ei.pi->kingside_storm_value(WHITE)
- ei.pi->queenside_storm_value(BLACK); - ei.pi->queenside_storm_value(BLACK);
// Evaluate space for both sides
if (ei.mi->space_weight() > 0)
{
evaluate_space(pos, WHITE, ei);
evaluate_space(pos, BLACK, ei);
}
} }
// Mobility // Mobility
@@ -414,7 +409,7 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
ei.egValue += apply_weight(ei.egMobility, WeightMobilityEndgame); ei.egValue += apply_weight(ei.egMobility, WeightMobilityEndgame);
// If we don't already have an unusual scale factor, check for opposite // If we don't already have an unusual scale factor, check for opposite
// colored bishop endgames, and use a lower scale for those: // colored bishop endgames, and use a lower scale for those
if ( phase < PHASE_MIDGAME if ( phase < PHASE_MIDGAME
&& pos.opposite_colored_bishops() && pos.opposite_colored_bishops()
&& ( (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0)) && ( (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0))
@@ -495,7 +490,10 @@ void init_eval(int threads) {
} }
for (Bitboard b = 0ULL; b < 256ULL; b++) for (Bitboard b = 0ULL; b < 256ULL; b++)
BitCount8Bit[b] = count_1s(b); {
assert(count_1s(b) == int(uint8_t(count_1s(b))));
BitCount8Bit[b] = (uint8_t)count_1s(b);
}
} }
@@ -527,6 +525,7 @@ void read_weights(Color us) {
WeightKingSafety[us] = weight_option("Cowardice", WeightKingSafetyInternal); WeightKingSafety[us] = weight_option("Cowardice", WeightKingSafetyInternal);
WeightKingSafety[them] = weight_option("Aggressiveness", WeightKingOppSafetyInternal); WeightKingSafety[them] = weight_option("Aggressiveness", WeightKingOppSafetyInternal);
WeightSpace = weight_option("Space", WeightSpaceInternal);
init_safety(); init_safety();
} }
@@ -536,34 +535,47 @@ namespace {
// evaluate_common() computes terms common to all pieces attack // evaluate_common() computes terms common to all pieces attack
int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei, template<PieceType Piece>
int AttackWeight, const Value* mgBonus, const Value* egBonus, int evaluate_common(const Position& p, const Bitboard& b, Color us, EvalInfo& ei, Square s = SQ_NONE) {
Square s = SQ_NONE, const Value* OutpostBonus = NULL) {
static const int AttackWeight[] = { 0, 0, KnightAttackWeight, BishopAttackWeight, RookAttackWeight, QueenAttackWeight };
static const Value* MgBonus[] = { 0, 0, MidgameKnightMobilityBonus, MidgameBishopMobilityBonus, MidgameRookMobilityBonus, MidgameQueenMobilityBonus };
static const Value* EgBonus[] = { 0, 0, EndgameKnightMobilityBonus, EndgameBishopMobilityBonus, EndgameRookMobilityBonus, EndgameQueenMobilityBonus };
static const Value* OutpostBonus[] = { 0, 0, KnightOutpostBonus, BishopOutpostBonus, 0, 0 };
Color them = opposite_color(us); Color them = opposite_color(us);
// Update attack info
ei.attackedBy[us][Piece] |= b;
// King attack // King attack
if (b & ei.kingZone[us]) if (b & ei.kingZone[us])
{ {
ei.kingAttackersCount[us]++; ei.kingAttackersCount[us]++;
ei.kingAttackersWeight[us] += AttackWeight; ei.kingAttackersWeight[us] += AttackWeight[Piece];
Bitboard bb = (b & ei.attackedBy[them][KING]); Bitboard bb = (b & ei.attackedBy[them][KING]);
if (bb) if (bb)
ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15(bb); ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15(bb);
} }
// Remove squares protected by enemy pawns
Bitboard bb = (b & ~ei.attackedBy[them][PAWN]);
// Mobility // Mobility
int mob = count_1s_max_15(b & ~p.pieces_of_color(us)); int mob = (Piece != QUEEN ? count_1s_max_15(bb & ~p.pieces_of_color(us))
ei.mgMobility += Sign[us] * mgBonus[mob]; : count_1s(bb & ~p.pieces_of_color(us)));
ei.egMobility += Sign[us] * egBonus[mob];
ei.mgMobility += Sign[us] * MgBonus[Piece][mob];
ei.egMobility += Sign[us] * EgBonus[Piece][mob];
// Bishop and Knight outposts // Bishop and Knight outposts
if (!OutpostBonus || !p.square_is_weak(s, them)) if ( (Piece != BISHOP && Piece != KNIGHT) // compile time condition
|| !p.square_is_weak(s, them))
return mob; return mob;
// Initial bonus based on square // Initial bonus based on square
Value v, bonus; Value v, bonus;
v = bonus = OutpostBonus[relative_square(us, s)]; v = bonus = OutpostBonus[Piece][relative_square(us, s)];
// Increase bonus if supported by pawn, especially if the opponent has // Increase bonus if supported by pawn, especially if the opponent has
// no minor piece which can exchange the outpost piece // no minor piece which can exchange the outpost piece
@@ -580,121 +592,101 @@ namespace {
} }
// evaluate_knight() assigns bonuses and penalties to a knight of a given // evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given
// color on a given square. // color.
void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) { template<PieceType Piece>
void evaluate_pieces(const Position& pos, Color us, EvalInfo& ei) {
Bitboard b = p.piece_attacks<KNIGHT>(s); Bitboard b;
ei.attackedBy[us][KNIGHT] |= b; Square s, ksq;
Color them;
int mob;
File f;
// King attack, mobility and outposts for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
evaluate_common(p, b, us, ei, KnightAttackWeight, MidgameKnightMobilityBonus,
EndgameKnightMobilityBonus, s, KnightOutpostBonus);
}
// evaluate_bishop() assigns bonuses and penalties to a bishop of a given
// color on a given square.
void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei) {
Bitboard b = bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us));
ei.attackedBy[us][BISHOP] |= b;
// King attack, mobility and outposts
evaluate_common(p, b, us, ei, BishopAttackWeight, MidgameBishopMobilityBonus,
EndgameBishopMobilityBonus, s, BishopOutpostBonus);
}
// evaluate_rook() assigns bonuses and penalties to a rook of a given
// color on a given square.
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) {
//Bitboard b = p.rook_attacks(s);
Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
ei.attackedBy[us][ROOK] |= b;
// King attack and mobility
int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus,
EndgameRookMobilityBonus);
// Rook on 7th rank
Color them = opposite_color(us);
if ( relative_rank(us, s) == RANK_7
&& relative_rank(us, p.king_square(them)) == RANK_8)
{ {
ei.mgValue += Sign[us] * MidgameRookOn7thBonus; s = pos.piece_list(us, Piece, i);
ei.egValue += Sign[us] * EndgameRookOn7thBonus;
}
// Open and half-open files if (Piece == KNIGHT || Piece == QUEEN)
File f = square_file(s); b = pos.piece_attacks<Piece>(s);
if (ei.pi->file_is_half_open(us, f)) else if (Piece == BISHOP)
{ b = bishop_attacks_bb(s, pos.occupied_squares() & ~pos.queens(us));
if (ei.pi->file_is_half_open(them, f)) else if (Piece == ROOK)
b = rook_attacks_bb(s, pos.occupied_squares() & ~pos.rooks_and_queens(us));
// Attacks, mobility and outposts
mob = evaluate_common<Piece>(pos, b, us, ei, s);
// Special patterns: trapped bishops on a7/h7/a2/h2
// and trapped bishops on a1/h1/a8/h8 in Chess960.
if (Piece == BISHOP)
{ {
ei.mgValue += Sign[us] * RookOpenFileBonus; if (bit_is_set(MaskA7H7[us], s))
ei.egValue += Sign[us] * RookOpenFileBonus; evaluate_trapped_bishop_a7h7(pos, s, us, ei);
if (Chess960 && bit_is_set(MaskA1H1[us], s))
evaluate_trapped_bishop_a1h1(pos, s, us, ei);
} }
else
if (Piece == ROOK || Piece == QUEEN)
{ {
ei.mgValue += Sign[us] * RookHalfOpenFileBonus; // Queen or rook on 7th rank
ei.egValue += Sign[us] * RookHalfOpenFileBonus; them = opposite_color(us);
if ( relative_rank(us, s) == RANK_7
&& relative_rank(us, pos.king_square(them)) == RANK_8)
{
ei.mgValue += Sign[us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
ei.egValue += Sign[us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
}
} }
}
// Penalize rooks which are trapped inside a king. Penalize more if // Special extra evaluation for rooks
// king has lost right to castle if (Piece == ROOK)
if (mob > 6 || ei.pi->file_is_half_open(us, f)) {
return; // Open and half-open files
f = square_file(s);
if (ei.pi->file_is_half_open(us, f))
{
if (ei.pi->file_is_half_open(them, f))
{
ei.mgValue += Sign[us] * RookOpenFileBonus;
ei.egValue += Sign[us] * RookOpenFileBonus;
}
else
{
ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
ei.egValue += Sign[us] * RookHalfOpenFileBonus;
}
}
Square ksq = p.king_square(us); // Penalize rooks which are trapped inside a king. Penalize more if
// king has lost right to castle.
if (mob > 6 || ei.pi->file_is_half_open(us, f))
continue;
if ( square_file(ksq) >= FILE_E ksq = pos.king_square(us);
&& square_file(s) > square_file(ksq)
&& (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
{
// Is there a half-open file between the king and the edge of the board?
if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
: Sign[us] * (TrappedRookPenalty - mob * 16);
}
else if ( square_file(ksq) <= FILE_D
&& square_file(s) < square_file(ksq)
&& (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
{
// Is there a half-open file between the king and the edge of the board?
if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
: Sign[us] * (TrappedRookPenalty - mob * 16);
}
}
if ( square_file(ksq) >= FILE_E
// evaluate_queen() assigns bonuses and penalties to a queen of a given && square_file(s) > square_file(ksq)
// color on a given square. && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
{
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) { // Is there a half-open file between the king and the edge of the board?
if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
Bitboard b = p.piece_attacks<QUEEN>(s); ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
ei.attackedBy[us][QUEEN] |= b; : Sign[us] * (TrappedRookPenalty - mob * 16);
}
// King attack and mobility else if ( square_file(ksq) <= FILE_D
evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus, && square_file(s) < square_file(ksq)
EndgameQueenMobilityBonus); && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
{
// Queen on 7th rank // Is there a half-open file between the king and the edge of the board?
Color them = opposite_color(us); if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
if ( relative_rank(us, s) == RANK_7 : Sign[us] * (TrappedRookPenalty - mob * 16);
&& relative_rank(us, p.king_square(them)) == RANK_8) }
{ }
ei.mgValue += Sign[us] * MidgameQueenOn7thBonus;
ei.egValue += Sign[us] * EndgameQueenOn7thBonus;
} }
} }
@@ -703,12 +695,14 @@ namespace {
return b >> (num << 3); return b >> (num << 3);
} }
// evaluate_king() assigns bonuses and penalties to a king of a given // evaluate_pieces<KING>() assigns bonuses and penalties to a king of a given
// color on a given square. // color.
void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei) { template<>
void evaluate_pieces<KING>(const Position& p, Color us, EvalInfo& ei) {
int shelter = 0, sign = Sign[us]; int shelter = 0, sign = Sign[us];
Square s = p.king_square(us);
// King shelter // King shelter
if (relative_rank(us, s) <= RANK_4) if (relative_rank(us, s) <= RANK_4)
@@ -744,14 +738,14 @@ namespace {
Bitboard occ = p.occupied_squares(), b, b2; Bitboard occ = p.occupied_squares(), b, b2;
// Initialize the 'attackUnits' variable, which is used later on as an // Initialize the 'attackUnits' variable, which is used later on as an
// index to the SafetyTable[] array. The initial is based on the number // index to the SafetyTable[] array. The initial value is based on the
// and types of the attacking pieces, the number of attacked and // number and types of the attacking pieces, the number of attacked and
// undefended squares around the king, the square of the king, and the // undefended squares around the king, the square of the king, and the
// quality of the pawn shelter. // quality of the pawn shelter.
int attackUnits = int attackUnits =
Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25) Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25)
+ (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3 + (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3
+ InitKingDanger[relative_square(us, s)] - shelter / 32; + InitKingDanger[relative_square(us, s)] - (shelter >> 5);
// Analyse safe queen contact checks // Analyse safe queen contact checks
b = undefended & ei.attacked_by(them, QUEEN) & ~p.pieces_of_color(them); b = undefended & ei.attacked_by(them, QUEEN) & ~p.pieces_of_color(them);
@@ -788,7 +782,7 @@ namespace {
if ( bit_is_set(p.piece_attacks<QUEEN>(from), to) if ( bit_is_set(p.piece_attacks<QUEEN>(from), to)
&& !bit_is_set(p.pinned_pieces(them), from) && !bit_is_set(p.pinned_pieces(them), from)
&& !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us)) && !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us))
&& !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us))) && !(bishop_attacks_bb(to, occ & ClearMaskBB[from]) & p.bishops_and_queens(us)))
ei.mateThreat[them] = make_move(from, to); ei.mateThreat[them] = make_move(from, to);
} }
@@ -797,25 +791,8 @@ namespace {
} }
} }
} }
// Analyse safe rook contact checks:
if (RookContactCheckBonus)
{
b = undefended & ei.attacked_by(them, ROOK) & ~p.pieces_of_color(them);
if (b)
{
Bitboard attackedByOthers =
ei.attacked_by(them, PAWN) | ei.attacked_by(them, KNIGHT)
| ei.attacked_by(them, BISHOP) | ei.attacked_by(them, QUEEN);
b &= attackedByOthers; // Analyse safe distance checks
if (b)
{
int count = count_1s_max_15(b);
attackUnits += (RookContactCheckBonus * count * (sente? 2 : 1));
}
}
}
// Analyse safe distance checks:
if (QueenCheckBonus > 0 || RookCheckBonus > 0) if (QueenCheckBonus > 0 || RookCheckBonus > 0)
{ {
b = p.piece_attacks<ROOK>(s) & ~p.pieces_of_color(them) & ~ei.attacked_by(us); b = p.piece_attacks<ROOK>(s) & ~p.pieces_of_color(them) & ~ei.attacked_by(us);
@@ -897,141 +874,161 @@ namespace {
// evaluate_passed_pawns() evaluates the passed pawns for both sides. // evaluate_passed_pawns() evaluates the passed pawns for both sides.
void evaluate_passed_pawns(const Position &pos, EvalInfo &ei) { void evaluate_passed_pawns(const Position &pos, EvalInfo &ei) {
bool hasUnstoppable[2] = {false, false}; bool hasUnstoppable[2] = {false, false};
int movesToGo[2] = {100, 100}; int movesToGo[2] = {100, 100};
for(Color us = WHITE; us <= BLACK; us++) { for (Color us = WHITE; us <= BLACK; us++)
Color them = opposite_color(us); {
Square ourKingSq = pos.king_square(us); Color them = opposite_color(us);
Square theirKingSq = pos.king_square(them); Square ourKingSq = pos.king_square(us);
Bitboard b = ei.pi->passed_pawns() & pos.pawns(us), b2, b3, b4; Square theirKingSq = pos.king_square(them);
Bitboard b = ei.pi->passed_pawns() & pos.pawns(us), b2, b3, b4;
while(b) { while (b)
Square s = pop_1st_bit(&b); {
assert(pos.piece_on(s) == pawn_of_color(us)); Square s = pop_1st_bit(&b);
assert(pos.pawn_is_passed(us, s));
int r = int(relative_rank(us, s) - RANK_2); assert(pos.piece_on(s) == piece_of_color_and_type(us, PAWN));
int tr = Max(0, r * (r-1)); assert(pos.pawn_is_passed(us, s));
Square blockSq = s + pawn_push(us);
// Base bonus based on rank: int r = int(relative_rank(us, s) - RANK_2);
Value mbonus = Value(20 * tr); int tr = Max(0, r * (r - 1));
Value ebonus = Value(10 + r * r * 10); Square blockSq = s + pawn_push(us);
// Adjust bonus based on king proximity: // Base bonus based on rank
ebonus -= Value(square_distance(ourKingSq, blockSq) * 3 * tr); Value mbonus = Value(20 * tr);
ebonus -= Value ebonus = Value(10 + r * r * 10);
Value(square_distance(ourKingSq, blockSq + pawn_push(us)) * 1 * tr);
ebonus += Value(square_distance(theirKingSq, blockSq) * 6 * tr);
// If the pawn is free to advance, increase bonus: // Adjust bonus based on king proximity
if(pos.square_is_empty(blockSq)) { if (tr != 0)
{
ebonus -= Value(square_distance(ourKingSq, blockSq) * 3 * tr);
ebonus -= Value(square_distance(ourKingSq, blockSq + pawn_push(us)) * 1 * tr);
ebonus += Value(square_distance(theirKingSq, blockSq) * 6 * tr);
b2 = squares_in_front_of(us, s); // If the pawn is free to advance, increase bonus
b3 = b2 & ei.attacked_by(them); if (pos.square_is_empty(blockSq))
b4 = b2 & ei.attacked_by(us); {
if((b2 & pos.pieces_of_color(them)) == EmptyBoardBB) { b2 = squares_in_front_of(us, s);
// There are no enemy pieces in the pawn's path! Are any of the b3 = b2 & ei.attacked_by(them);
// squares in the pawn's path attacked by the enemy? b4 = b2 & ei.attacked_by(us);
if(b3 == EmptyBoardBB)
// No enemy attacks, huge bonus!
ebonus += Value(tr * ((b2 == b4)? 17 : 15));
else
// OK, there are enemy attacks. Are those squares which are
// attacked by the enemy also attacked by us? If yes, big bonus
// (but smaller than when there are no enemy attacks), if no,
// somewhat smaller bonus.
ebonus += Value(tr * (((b3 & b4) == b3)? 13 : 8));
}
else {
// There are some enemy pieces in the pawn's path. While this is
// sad, we still assign a moderate bonus if all squares in the path
// which are either occupied by or attacked by enemy pieces are
// also attacked by us.
if(((b3 | (b2 & pos.pieces_of_color(them))) & ~b4) == EmptyBoardBB)
ebonus += Value(tr * 6);
}
// At last, add a small bonus when there are no *friendly* pieces
// in the pawn's path:
if((b2 & pos.pieces_of_color(us)) == EmptyBoardBB)
ebonus += Value(tr);
}
// If the pawn is supported by a friendly pawn, increase bonus. // If there is an enemy rook or queen attacking the pawn from behind,
b2 = pos.pawns(us) & neighboring_files_bb(s); // add all X-ray attacks by the rook or queen.
if(b2 & rank_bb(s)) if ( bit_is_set(ei.attacked_by(them,ROOK) | ei.attacked_by(them,QUEEN),s)
ebonus += Value(r * 20); && (squares_behind(us, s) & pos.rooks_and_queens(them)))
else if(pos.pawn_attacks(them, s) & b2) b3 = b2;
ebonus += Value(r * 12);
// If the other side has only a king, check whether the pawn is if ((b2 & pos.pieces_of_color(them)) == EmptyBoardBB)
// unstoppable: {
if(pos.non_pawn_material(them) == Value(0)) { // There are no enemy pieces in the pawn's path! Are any of the
Square qsq; // squares in the pawn's path attacked by the enemy?
int d; if (b3 == EmptyBoardBB)
// No enemy attacks, huge bonus!
qsq = relative_square(us, make_square(square_file(s), RANK_8)); ebonus += Value(tr * (b2 == b4 ? 17 : 15));
d = square_distance(s, qsq) - square_distance(theirKingSq, qsq) else
+ ((us == pos.side_to_move())? 0 : 1); // OK, there are enemy attacks. Are those squares which are
// attacked by the enemy also attacked by us? If yes, big bonus
if(d < 0) { // (but smaller than when there are no enemy attacks), if no,
int mtg = RANK_8 - relative_rank(us, s); // somewhat smaller bonus.
int blockerCount = ebonus += Value(tr * ((b3 & b4) == b3 ? 13 : 8));
count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares()); }
mtg += blockerCount; else
d += blockerCount; {
if(d < 0) { // There are some enemy pieces in the pawn's path. While this is
hasUnstoppable[us] = true; // sad, we still assign a moderate bonus if all squares in the path
movesToGo[us] = Min(movesToGo[us], mtg); // which are either occupied by or attacked by enemy pieces are
// also attacked by us.
if (((b3 | (b2 & pos.pieces_of_color(them))) & ~b4) == EmptyBoardBB)
ebonus += Value(tr * 6);
}
// At last, add a small bonus when there are no *friendly* pieces
// in the pawn's path.
if ((b2 & pos.pieces_of_color(us)) == EmptyBoardBB)
ebonus += Value(tr);
}
} }
}
}
// Rook pawns are a special case: They are sometimes worse, and
// sometimes better than other passed pawns. It is difficult to find
// good rules for determining whether they are good or bad. For now,
// we try the following: Increase the value for rook pawns if the
// other side has no pieces apart from a knight, and decrease the
// value if the other side has a rook or queen.
if(square_file(s) == FILE_A || square_file(s) == FILE_H) {
if(pos.non_pawn_material(them) == KnightValueMidgame
&& pos.piece_count(them, KNIGHT) == 1)
ebonus += ebonus / 4;
else if(pos.rooks_and_queens(them))
ebonus -= ebonus / 4;
}
// Add the scores for this pawn to the middle game and endgame eval. // If the pawn is supported by a friendly pawn, increase bonus
ei.mgValue += apply_weight(Sign[us] * mbonus, WeightPassedPawnsMidgame); b2 = pos.pawns(us) & neighboring_files_bb(s);
ei.egValue += apply_weight(Sign[us] * ebonus, WeightPassedPawnsEndgame); if (b2 & rank_bb(s))
} ebonus += Value(r * 20);
else if (pos.pawn_attacks(them, s) & b2)
ebonus += Value(r * 12);
// If the other side has only a king, check whether the pawn is
// unstoppable
if (pos.non_pawn_material(them) == Value(0))
{
Square qsq;
int d;
qsq = relative_square(us, make_square(square_file(s), RANK_8));
d = square_distance(s, qsq)
- square_distance(theirKingSq, qsq)
+ (us != pos.side_to_move());
if (d < 0)
{
int mtg = RANK_8 - relative_rank(us, s);
int blockerCount = count_1s_max_15(squares_in_front_of(us,s) & pos.occupied_squares());
mtg += blockerCount;
d += blockerCount;
if (d < 0)
{
hasUnstoppable[us] = true;
movesToGo[us] = Min(movesToGo[us], mtg);
}
}
}
// Rook pawns are a special case: They are sometimes worse, and
// sometimes better than other passed pawns. It is difficult to find
// good rules for determining whether they are good or bad. For now,
// we try the following: Increase the value for rook pawns if the
// other side has no pieces apart from a knight, and decrease the
// value if the other side has a rook or queen.
if (square_file(s) == FILE_A || square_file(s) == FILE_H)
{
if( pos.non_pawn_material(them) <= KnightValueMidgame
&& pos.piece_count(them, KNIGHT) <= 1)
ebonus += ebonus / 4;
else if(pos.rooks_and_queens(them))
ebonus -= ebonus / 4;
}
// Add the scores for this pawn to the middle game and endgame eval.
ei.mgValue += apply_weight(Sign[us] * mbonus, WeightPassedPawnsMidgame);
ei.egValue += apply_weight(Sign[us] * ebonus, WeightPassedPawnsEndgame);
}
} }
// Does either side have an unstoppable passed pawn? // Does either side have an unstoppable passed pawn?
if(hasUnstoppable[WHITE] && !hasUnstoppable[BLACK]) if (hasUnstoppable[WHITE] && !hasUnstoppable[BLACK])
ei.egValue += UnstoppablePawnValue - Value(0x40 * movesToGo[WHITE]); ei.egValue += UnstoppablePawnValue - Value(0x40 * movesToGo[WHITE]);
else if(hasUnstoppable[BLACK] && !hasUnstoppable[WHITE]) else if (hasUnstoppable[BLACK] && !hasUnstoppable[WHITE])
ei.egValue -= UnstoppablePawnValue - Value(0x40 * movesToGo[BLACK]); ei.egValue -= UnstoppablePawnValue - Value(0x40 * movesToGo[BLACK]);
else if(hasUnstoppable[BLACK] && hasUnstoppable[WHITE]) { else if (hasUnstoppable[BLACK] && hasUnstoppable[WHITE])
// Both sides have unstoppable pawns! Try to find out who queens {
// first. We begin by transforming 'movesToGo' to the number of // Both sides have unstoppable pawns! Try to find out who queens
// plies until the pawn queens for both sides: // first. We begin by transforming 'movesToGo' to the number of
movesToGo[WHITE] *= 2; // plies until the pawn queens for both sides.
movesToGo[BLACK] *= 2; movesToGo[WHITE] *= 2;
movesToGo[pos.side_to_move()]--; movesToGo[BLACK] *= 2;
movesToGo[pos.side_to_move()]--;
// If one side queens at least three plies before the other, that // If one side queens at least three plies before the other, that
// side wins: // side wins.
if(movesToGo[WHITE] <= movesToGo[BLACK] - 3) if (movesToGo[WHITE] <= movesToGo[BLACK] - 3)
ei.egValue += UnstoppablePawnValue - Value(0x40 * (movesToGo[WHITE]/2)); ei.egValue += UnstoppablePawnValue - Value(0x40 * (movesToGo[WHITE]/2));
else if(movesToGo[BLACK] <= movesToGo[WHITE] - 3) else if(movesToGo[BLACK] <= movesToGo[WHITE] - 3)
ei.egValue -= UnstoppablePawnValue - Value(0x40 * (movesToGo[BLACK]/2)); ei.egValue -= UnstoppablePawnValue - Value(0x40 * (movesToGo[BLACK]/2));
// We could also add some rules about the situation when one side // We could also add some rules about the situation when one side
// queens exactly one ply before the other: Does the first queen // queens exactly one ply before the other: Does the first queen
// check the opponent's king, or attack the opponent's queening square? // check the opponent's king, or attack the opponent's queening square?
// This is slightly tricky to get right, because it is possible that // This is slightly tricky to get right, because it is possible that
// the opponent's king has moved somewhere before the first pawn queens. // the opponent's king has moved somewhere before the first pawn queens.
} }
} }
@@ -1042,14 +1039,13 @@ namespace {
void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us, void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us,
EvalInfo &ei) { EvalInfo &ei) {
assert(square_is_ok(s)); assert(square_is_ok(s));
assert(pos.piece_on(s) == bishop_of_color(us)); assert(pos.piece_on(s) == piece_of_color_and_type(us, BISHOP));
Square b6 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B6 : SQ_G6); Square b6 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B6 : SQ_G6);
Square b8 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B8 : SQ_G8); Square b8 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B8 : SQ_G8);
if ( pos.piece_on(b6) == pawn_of_color(opposite_color(us)) if ( pos.piece_on(b6) == piece_of_color_and_type(opposite_color(us), PAWN)
&& pos.see(s, b6) < 0 && pos.see(s, b6) < 0
&& pos.see(s, b8) < 0) && pos.see(s, b8) < 0)
{ {
@@ -1066,49 +1062,92 @@ namespace {
void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us, void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us,
EvalInfo &ei) { EvalInfo &ei) {
Piece pawn = pawn_of_color(us); Piece pawn = piece_of_color_and_type(us, PAWN);
Square b2, b3, c3; Square b2, b3, c3;
assert(Chess960); assert(Chess960);
assert(square_is_ok(s)); assert(square_is_ok(s));
assert(pos.piece_on(s) == bishop_of_color(us)); assert(pos.piece_on(s) == piece_of_color_and_type(us, BISHOP));
if(square_file(s) == FILE_A) { if (square_file(s) == FILE_A)
b2 = relative_square(us, SQ_B2); {
b3 = relative_square(us, SQ_B3); b2 = relative_square(us, SQ_B2);
c3 = relative_square(us, SQ_C3); b3 = relative_square(us, SQ_B3);
c3 = relative_square(us, SQ_C3);
} }
else { else
b2 = relative_square(us, SQ_G2); {
b3 = relative_square(us, SQ_G3); b2 = relative_square(us, SQ_G2);
c3 = relative_square(us, SQ_F3); b3 = relative_square(us, SQ_G3);
c3 = relative_square(us, SQ_F3);
} }
if(pos.piece_on(b2) == pawn) { if (pos.piece_on(b2) == pawn)
Value penalty; {
Value penalty;
if(!pos.square_is_empty(b3)) if (!pos.square_is_empty(b3))
penalty = 2*TrappedBishopA1H1Penalty; penalty = 2*TrappedBishopA1H1Penalty;
else if(pos.piece_on(c3) == pawn) else if (pos.piece_on(c3) == pawn)
penalty = TrappedBishopA1H1Penalty; penalty = TrappedBishopA1H1Penalty;
else else
penalty = TrappedBishopA1H1Penalty / 2; penalty = TrappedBishopA1H1Penalty / 2;
ei.mgValue -= Sign[us] * penalty; ei.mgValue -= Sign[us] * penalty;
ei.egValue -= Sign[us] * penalty; ei.egValue -= Sign[us] * penalty;
} }
} }
// apply_weight applies an evaluation weight to a value. // evaluate_space() computes the space evaluation for a given side. The
// space evaluation is a simple bonus based on the number of safe squares
// available for minor pieces on the central four files on ranks 2--4. Safe
// squares one, two or three squares behind a friendly pawn are counted
// twice. Finally, the space bonus is scaled by a weight taken from the
// material hash table.
void evaluate_space(const Position &pos, Color us, EvalInfo &ei) {
Color them = opposite_color(us);
// Find the safe squares for our pieces inside the area defined by
// SpaceMask[us]. A square is unsafe it is attacked by an enemy
// pawn, or if it is undefended and attacked by an enemy piece.
Bitboard safeSquares = SpaceMask[us]
& ~pos.pawns(us)
& ~ei.attacked_by(them, PAWN)
& ~(~ei.attacked_by(us) & ei.attacked_by(them));
// Find all squares which are at most three squares behind some friendly
// pawn.
Bitboard behindFriendlyPawns = pos.pawns(us);
if (us == WHITE)
{
behindFriendlyPawns |= (behindFriendlyPawns >> 8);
behindFriendlyPawns |= (behindFriendlyPawns >> 16);
}
else
{
behindFriendlyPawns |= (behindFriendlyPawns << 8);
behindFriendlyPawns |= (behindFriendlyPawns << 16);
}
int space = count_1s_max_15(safeSquares)
+ count_1s_max_15(behindFriendlyPawns & safeSquares);
ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
}
// apply_weight() applies an evaluation weight to a value
inline Value apply_weight(Value v, int w) { inline Value apply_weight(Value v, int w) {
return (v*w) / 0x100; return (v*w) / 0x100;
} }
// scale_by_game_phase interpolates between a middle game and an endgame // scale_by_game_phase() interpolates between a middle game and an endgame
// score, based on game phase. It also scales the return value by a // score, based on game phase. It also scales the return value by a
// ScaleFactor array. // ScaleFactor array.
@@ -1120,13 +1159,7 @@ namespace {
ev = apply_scale_factor(ev, sf[(ev > Value(0) ? WHITE : BLACK)]); ev = apply_scale_factor(ev, sf[(ev > Value(0) ? WHITE : BLACK)]);
// Linearized sigmoid interpolator Value result = Value(int((mv * ph + ev * (128 - ph)) / 128));
int sph = int(ph);
sph -= (64 - sph) / 4;
sph = Min(PHASE_MIDGAME, Max(PHASE_ENDGAME, sph));
Value result = Value(int((mv * sph + ev * (128 - sph)) / 128));
return Value(int(result) & ~(GrainSize - 1)); return Value(int(result) & ~(GrainSize - 1));
} }
@@ -1144,6 +1177,7 @@ namespace {
// an UCI-configurable weight with an internal weight. // an UCI-configurable weight with an internal weight.
int compute_weight(int uciWeight, int internalWeight) { int compute_weight(int uciWeight, int internalWeight) {
uciWeight = (uciWeight * 0x100) / 100; uciWeight = (uciWeight * 0x100) / 100;
return (uciWeight * internalWeight) / 0x100; return (uciWeight * internalWeight) / 0x100;
} }
@@ -1162,7 +1196,6 @@ namespace {
void init_safety() { void init_safety() {
QueenContactCheckBonus = get_option_value_int("Queen Contact Check Bonus"); QueenContactCheckBonus = get_option_value_int("Queen Contact Check Bonus");
RookContactCheckBonus = get_option_value_int("Rook Contact Check Bonus");
QueenCheckBonus = get_option_value_int("Queen Check Bonus"); QueenCheckBonus = get_option_value_int("Queen Check Bonus");
RookCheckBonus = get_option_value_int("Rook Check Bonus"); RookCheckBonus = get_option_value_int("Rook Check Bonus");
BishopCheckBonus = get_option_value_int("Bishop Check Bonus"); BishopCheckBonus = get_option_value_int("Bishop Check Bonus");
@@ -1197,5 +1230,4 @@ namespace {
SafetyTable[i] = Value(peak); SafetyTable[i] = Value(peak);
} }
} }
} }
+9 -8
View File
@@ -27,29 +27,30 @@
#include "material.h" #include "material.h"
#include "pawns.h" #include "pawns.h"
#include "position.h"
//// ////
//// Types //// Types
//// ////
/// The EvalInfo struct contains various information computed and collected /// The EvalInfo struct contains various information computed and collected
/// by the evaluation function. An EvalInfo object is passed as one of the /// by the evaluation function. An EvalInfo object is passed as one of the
/// arguments to the evaluation function, and the search can make use of its /// arguments to the evaluation function, and the search can make use of its
/// contents to make intelligent search decisions. /// contents to make intelligent search decisions.
/// ///
/// At the moment, this is not utilized very much: The only part of the /// At the moment, this is not utilized very much: The only part of the
/// EvalInfo object which is used by the search is futilityMargin. /// EvalInfo object which is used by the search is futilityMargin.
class Position;
struct EvalInfo { struct EvalInfo {
// Middle game and endgame evaluations: // Middle game and endgame evaluations
Value mgValue, egValue; Value mgValue, egValue;
// Pointers to material and pawn hash table entries: // Pointers to material and pawn hash table entries
MaterialInfo *mi; MaterialInfo* mi;
PawnInfo *pi; PawnInfo* pi;
// attackedBy[color][piece type] is a bitboard representing all squares // attackedBy[color][piece type] is a bitboard representing all squares
// attacked by a given color and piece type. attackedBy[color][0] contains // attacked by a given color and piece type. attackedBy[color][0] contains
@@ -100,8 +101,8 @@ struct EvalInfo {
//// Prototypes //// Prototypes
//// ////
extern Value evaluate(const Position &pos, EvalInfo &ei, int threadID); extern Value evaluate(const Position& pos, EvalInfo& ei, int threadID);
extern Value quick_evaluate(const Position &pos); extern Value quick_evaluate(const Position& pos);
extern void init_eval(int threads); extern void init_eval(int threads);
extern void quit_eval(); extern void quit_eval();
extern void read_weights(Color sideToMove); extern void read_weights(Color sideToMove);
+1 -1
View File
@@ -87,7 +87,7 @@ typedef pthread_mutex_t Lock;
#else #else
# include <windows.h> #include <windows.h>
typedef CRITICAL_SECTION Lock; typedef CRITICAL_SECTION Lock;
# define lock_init(x, y) InitializeCriticalSection(x) # define lock_init(x, y) InitializeCriticalSection(x)
+6 -5
View File
@@ -52,7 +52,6 @@ int main(int argc, char *argv[]) {
std::cin.rdbuf()->pubsetbuf(NULL, 0); std::cin.rdbuf()->pubsetbuf(NULL, 0);
// Initialization // Initialization
init_mersenne(); init_mersenne();
init_direction_table(); init_direction_table();
init_bitboards(); init_bitboards();
@@ -71,16 +70,18 @@ int main(int argc, char *argv[]) {
// Process command line arguments // Process command line arguments
if (argc >= 2 && string(argv[1]) == "bench") if (argc >= 2 && string(argv[1]) == "bench")
{ {
if (argc < 4 || argc > 6) if (argc < 4 || argc > 7)
{ {
std::cout << "Usage: glaurung bench <hash size> <threads> " std::cout << "Usage: stockfish bench <hash size> <threads> "
<< "[time = 60s] [fen positions file = default]" << "[time = 60s] [fen positions file = default] "
<< "[time, depth or node limited = time]"
<< std::endl; << std::endl;
exit(0); exit(0);
} }
string time = argc > 4 ? argv[4] : "60"; string time = argc > 4 ? argv[4] : "60";
string fen = argc > 5 ? argv[5] : "default"; string fen = argc > 5 ? argv[5] : "default";
benchmark(string(argv[2]) + " " + string(argv[3]) + " " + time + " " + fen); string lim = argc > 6 ? argv[6] : "time";
benchmark(string(argv[2]) + " " + string(argv[3]) + " " + time + " " + fen + " " + lim);
return 0; return 0;
} }
+99 -50
View File
@@ -24,6 +24,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <sstream>
#include <map> #include <map>
#include "material.h" #include "material.h"
@@ -35,8 +36,8 @@
namespace { namespace {
const Value BishopPairMidgameBonus = Value(100); const Value BishopPairMidgameBonus = Value(109);
const Value BishopPairEndgameBonus = Value(100); const Value BishopPairEndgameBonus = Value(97);
Key KNNKMaterialKey, KKNNMaterialKey; Key KNNKMaterialKey, KKNNMaterialKey;
@@ -54,20 +55,21 @@ class EndgameFunctions {
public: public:
EndgameFunctions(); EndgameFunctions();
EndgameEvaluationFunction* getEEF(Key key) const; EndgameEvaluationFunctionBase* getEEF(Key key) const;
ScalingFunction* getESF(Key key, Color* c) const; EndgameScalingFunctionBase* getESF(Key key, Color* c) const;
private: private:
void add(Key k, EndgameEvaluationFunction* f); void add(const std::string& keyCode, EndgameEvaluationFunctionBase* f);
void add(Key k, Color c, ScalingFunction* f); void add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f);
Key buildKey(const std::string& keyCode);
struct ScalingInfo struct ScalingInfo
{ {
Color col; Color col;
ScalingFunction* fun; EndgameScalingFunctionBase* fun;
}; };
std::map<Key, EndgameEvaluationFunction*> EEFmap; std::map<Key, EndgameEvaluationFunctionBase*> EEFmap;
std::map<Key, ScalingInfo> ESFmap; std::map<Key, ScalingInfo> ESFmap;
}; };
@@ -143,7 +145,8 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
} }
// Let's look if we have a specialized evaluation function for this // Let's look if we have a specialized evaluation function for this
// particular material configuration. // particular material configuration. First we look for a fixed
// configuration one, then a generic one if previous search failed.
if ((mi->evaluationFunction = funcs->getEEF(key)) != NULL) if ((mi->evaluationFunction = funcs->getEEF(key)) != NULL)
return mi; return mi;
@@ -161,6 +164,22 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
mi->evaluationFunction = &EvaluateKKX; mi->evaluationFunction = &EvaluateKKX;
return mi; return mi;
} }
else if ( pos.pawns() == EmptyBoardBB
&& pos.rooks() == EmptyBoardBB
&& pos.queens() == EmptyBoardBB)
{
// Minor piece endgame with at least one minor piece per side,
// and no pawns.
assert(pos.knights(WHITE) | pos.bishops(WHITE));
assert(pos.knights(BLACK) | pos.bishops(BLACK));
if ( pos.piece_count(WHITE, BISHOP) + pos.piece_count(WHITE, KNIGHT) <= 2
&& pos.piece_count(BLACK, BISHOP) + pos.piece_count(BLACK, KNIGHT) <= 2)
{
mi->evaluationFunction = &EvaluateKmmKm;
return mi;
}
}
// OK, we didn't find any special evaluation function for the current // OK, we didn't find any special evaluation function for the current
// material configuration. Is there a suitable scaling function? // material configuration. Is there a suitable scaling function?
@@ -170,7 +189,7 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
// are several conflicting applicable scaling functions and we need to // are several conflicting applicable scaling functions and we need to
// decide which one to use. // decide which one to use.
Color c; Color c;
ScalingFunction* sf; EndgameScalingFunctionBase* sf;
if ((sf = funcs->getESF(key, &c)) != NULL) if ((sf = funcs->getESF(key, &c)) != NULL)
{ {
@@ -221,6 +240,18 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
} }
} }
// Compute the space weight
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >=
2*QueenValueMidgame + 4*RookValueMidgame + 2*KnightValueMidgame)
{
int minorPieceCount = pos.piece_count(WHITE, KNIGHT)
+ pos.piece_count(BLACK, KNIGHT)
+ pos.piece_count(WHITE, BISHOP)
+ pos.piece_count(BLACK, BISHOP);
mi->spaceWeight = minorPieceCount * minorPieceCount;
}
// Evaluate the material balance // Evaluate the material balance
int sign; int sign;
@@ -287,61 +318,79 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
EndgameFunctions::EndgameFunctions() { EndgameFunctions::EndgameFunctions() {
typedef Key ZM[2][8][16]; KNNKMaterialKey = buildKey("KNNK");
const ZM& z = Position::zobMaterial; KKNNMaterialKey = buildKey("KKNN");
static const Color W = WHITE; add("KPK", &EvaluateKPK);
static const Color B = BLACK; add("KKP", &EvaluateKKP);
add("KBNK", &EvaluateKBNK);
add("KKBN", &EvaluateKKBN);
add("KRKP", &EvaluateKRKP);
add("KPKR", &EvaluateKPKR);
add("KRKB", &EvaluateKRKB);
add("KBKR", &EvaluateKBKR);
add("KRKN", &EvaluateKRKN);
add("KNKR", &EvaluateKNKR);
add("KQKR", &EvaluateKQKR);
add("KRKQ", &EvaluateKRKQ);
add("KBBKN", &EvaluateKBBKN);
add("KNKBB", &EvaluateKNKBB);
KNNKMaterialKey = z[W][KNIGHT][1] ^ z[W][KNIGHT][2]; add("KNPK", WHITE, &ScaleKNPK);
KKNNMaterialKey = z[B][KNIGHT][1] ^ z[B][KNIGHT][2]; add("KKNP", BLACK, &ScaleKKNP);
add("KRPKR", WHITE, &ScaleKRPKR);
add(z[W][PAWN][1], &EvaluateKPK); add("KRKRP", BLACK, &ScaleKRKRP);
add(z[B][PAWN][1], &EvaluateKKP); add("KBPKB", WHITE, &ScaleKBPKB);
add("KBKBP", BLACK, &ScaleKBKBP);
add(z[W][BISHOP][1] ^ z[W][KNIGHT][1], &EvaluateKBNK); add("KBPPKB", WHITE, &ScaleKBPPKB);
add(z[B][BISHOP][1] ^ z[B][KNIGHT][1], &EvaluateKKBN); add("KBKBPP", BLACK, &ScaleKBKBPP);
add(z[W][ROOK][1] ^ z[B][PAWN][1], &EvaluateKRKP); add("KBPKN", WHITE, &ScaleKBPKN);
add(z[W][PAWN][1] ^ z[B][ROOK][1], &EvaluateKPKR); add("KNKBP", BLACK, &ScaleKNKBP);
add(z[W][ROOK][1] ^ z[B][BISHOP][1], &EvaluateKRKB); add("KRPPKRP", WHITE, &ScaleKRPPKRP);
add(z[W][BISHOP][1] ^ z[B][ROOK][1], &EvaluateKBKR); add("KRPKRPP", BLACK, &ScaleKRPKRPP);
add(z[W][ROOK][1] ^ z[B][KNIGHT][1], &EvaluateKRKN); add("KRPPKRP", WHITE, &ScaleKRPPKRP);
add(z[W][KNIGHT][1] ^ z[B][ROOK][1], &EvaluateKNKR); add("KRPKRPP", BLACK, &ScaleKRPKRPP);
add(z[W][QUEEN][1] ^ z[B][ROOK][1], &EvaluateKQKR);
add(z[W][ROOK][1] ^ z[B][QUEEN][1], &EvaluateKRKQ);
add(z[W][KNIGHT][1] ^ z[W][PAWN][1], W, &ScaleKNPK);
add(z[B][KNIGHT][1] ^ z[B][PAWN][1], B, &ScaleKKNP);
add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[B][ROOK][1] , W, &ScaleKRPKR);
add(z[W][ROOK][1] ^ z[B][ROOK][1] ^ z[B][PAWN][1] , B, &ScaleKRKRP);
add(z[W][BISHOP][1] ^ z[W][PAWN][1] ^ z[B][BISHOP][1], W, &ScaleKBPKB);
add(z[W][BISHOP][1] ^ z[B][BISHOP][1] ^ z[B][PAWN][1] , B, &ScaleKBKBP);
add(z[W][BISHOP][1] ^ z[W][PAWN][1] ^ z[B][KNIGHT][1], W, &ScaleKBPKN);
add(z[W][KNIGHT][1] ^ z[B][BISHOP][1] ^ z[B][PAWN][1] , B, &ScaleKNKBP);
add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[W][PAWN][2] ^ z[B][ROOK][1] ^ z[B][PAWN][1], W, &ScaleKRPPKRP);
add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[B][ROOK][1] ^ z[B][PAWN][1] ^ z[B][PAWN][2], B, &ScaleKRPKRPP);
} }
void EndgameFunctions::add(Key k, EndgameEvaluationFunction* f) { Key EndgameFunctions::buildKey(const std::string& keyCode) {
EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f)); assert(keyCode.length() > 0 && keyCode[0] == 'K');
assert(keyCode.length() < 8);
std::stringstream s;
bool upcase = false;
// Build up a fen substring with the given pieces, note
// that the fen string could be of an illegal position.
for (size_t i = 0; i < keyCode.length(); i++)
{
if (keyCode[i] == 'K')
upcase = !upcase;
s << char(upcase? toupper(keyCode[i]) : tolower(keyCode[i]));
}
s << 8 - keyCode.length() << "/8/8/8/8/8/8/8 w -";
return Position(s.str()).get_material_key();
} }
void EndgameFunctions::add(Key k, Color c, ScalingFunction* f) { void EndgameFunctions::add(const std::string& keyCode, EndgameEvaluationFunctionBase* f) {
EEFmap.insert(std::pair<Key, EndgameEvaluationFunctionBase*>(buildKey(keyCode), f));
}
void EndgameFunctions::add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f) {
ScalingInfo s = {c, f}; ScalingInfo s = {c, f};
ESFmap.insert(std::pair<Key, ScalingInfo>(k, s)); ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(keyCode), s));
} }
EndgameEvaluationFunction* EndgameFunctions::getEEF(Key key) const { EndgameEvaluationFunctionBase* EndgameFunctions::getEEF(Key key) const {
std::map<Key, EndgameEvaluationFunction*>::const_iterator it(EEFmap.find(key)); std::map<Key, EndgameEvaluationFunctionBase*>::const_iterator it(EEFmap.find(key));
return (it != EEFmap.end() ? it->second : NULL); return (it != EEFmap.end() ? it->second : NULL);
} }
ScalingFunction* EndgameFunctions::getESF(Key key, Color* c) const { EndgameScalingFunctionBase* EndgameFunctions::getESF(Key key, Color* c) const {
std::map<Key, ScalingInfo>::const_iterator it(ESFmap.find(key)); std::map<Key, ScalingInfo>::const_iterator it(ESFmap.find(key));
if (it == ESFmap.end()) if (it == ESFmap.end())
+14 -2
View File
@@ -52,6 +52,7 @@ public:
Value mg_value() const; Value mg_value() const;
Value eg_value() const; Value eg_value() const;
ScaleFactor scale_factor(const Position& pos, Color c) const; ScaleFactor scale_factor(const Position& pos, Color c) const;
int space_weight() const;
bool specialized_eval_exists() const; bool specialized_eval_exists() const;
Value evaluate(const Position& pos) const; Value evaluate(const Position& pos) const;
@@ -62,8 +63,9 @@ private:
int16_t mgValue; int16_t mgValue;
int16_t egValue; int16_t egValue;
uint8_t factor[2]; uint8_t factor[2];
EndgameEvaluationFunction* evaluationFunction; EndgameEvaluationFunctionBase* evaluationFunction;
ScalingFunction* scalingFunction[2]; EndgameScalingFunctionBase* scalingFunction[2];
int spaceWeight;
}; };
@@ -120,6 +122,7 @@ inline void MaterialInfo::clear() {
mgValue = egValue = 0; mgValue = egValue = 0;
factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL); factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
spaceWeight = 0;
evaluationFunction = NULL; evaluationFunction = NULL;
scalingFunction[WHITE] = scalingFunction[BLACK] = NULL; scalingFunction[WHITE] = scalingFunction[BLACK] = NULL;
} }
@@ -144,6 +147,15 @@ inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) cons
} }
/// MaterialInfo::space_weight() simply returns the weight for the space
/// evaluation for this material configuration.
inline int MaterialInfo::space_weight() const {
return spaceWeight;
}
/// MaterialInfo::specialized_eval_exists decides whether there is a /// MaterialInfo::specialized_eval_exists decides whether there is a
/// specialized evaluation function for the current material configuration, /// specialized evaluation function for the current material configuration,
/// or if the normal evaluation function should be used. /// or if the normal evaluation function should be used.
+102 -22
View File
@@ -29,14 +29,36 @@
# include <unistd.h> # include <unistd.h>
#else #else
/*
(c) Copyright 1992 Eric Backus
This software may be used freely so long as this copyright notice is
left intact. There is no warrantee on this software.
*/
# include <windows.h> # include <windows.h>
# include <time.h> # include <time.h>
# include "dos.h" # include "dos.h"
int gettimeofday(struct timeval * tp, struct timezone * tzp); static int gettimeofday(struct timeval* tp, struct timezone*)
{
SYSTEMTIME systime;
if (tp)
{
struct tm tmrec;
time_t theTime = time(NULL);
tmrec = *localtime(&theTime);
tp->tv_sec = mktime(&tmrec);
GetLocalTime(&systime); /* system time */
tp->tv_usec = systime.wMilliseconds * 1000;
}
return 0;
}
#endif #endif
#include <cassert>
#include <cstdio> #include <cstdio>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@@ -44,6 +66,15 @@ int gettimeofday(struct timeval * tp, struct timezone * tzp);
#include "misc.h" #include "misc.h"
using namespace std;
/// Version number. If this is left empty, the current date (in the format
/// YYMMDD) is used as a version number.
static const string EngineVersion = "1.3";
static const string AppName = "Stockfish";
static const string AppTag = "";
//// ////
//// Variables //// Variables
@@ -60,17 +91,65 @@ bool dbg_show_hit_rate = false;
//// Functions //// Functions
//// ////
void dbg_hit_on(bool b) {
assert(!dbg_show_mean);
dbg_show_hit_rate = true;
dbg_cnt0++;
if (b)
dbg_cnt1++;
}
void dbg_hit_on_c(bool c, bool b) {
if (c)
dbg_hit_on(b);
}
void dbg_before() {
assert(!dbg_show_mean);
dbg_show_hit_rate = true;
dbg_cnt0++;
}
void dbg_after() {
assert(!dbg_show_mean);
dbg_show_hit_rate = true;
dbg_cnt1++;
}
void dbg_mean_of(int v) {
assert(!dbg_show_hit_rate);
dbg_show_mean = true;
dbg_cnt0++;
dbg_cnt1 += v;
}
void dbg_print_hit_rate() { void dbg_print_hit_rate() {
std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1 cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
<< std::endl;
} }
void dbg_print_mean() { void dbg_print_mean() {
std::cout << "Total " << dbg_cnt0 << " Mean " cout << "Total " << dbg_cnt0 << " Mean "
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl; << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
}
void dbg_print_hit_rate(ofstream& logFile) {
logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
}
void dbg_print_mean(ofstream& logFile) {
logFile << "Total " << dbg_cnt0 << " Mean "
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
} }
/// engine_name() returns the full name of the current Stockfish version. /// engine_name() returns the full name of the current Stockfish version.
@@ -78,24 +157,25 @@ void dbg_print_mean() {
/// program was compiled) or "Stockfish <version number>", depending on whether /// program was compiled) or "Stockfish <version number>", depending on whether
/// the constant EngineVersion (defined in misc.h) is empty. /// the constant EngineVersion (defined in misc.h) is empty.
const std::string engine_name() { const string engine_name() {
if (EngineVersion.empty()) if (!EngineVersion.empty())
{
std::string date(__DATE__); // From compiler, format is "Sep 21 2008"
std::string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
std::stringstream s;
std::string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
s << "Stockfish " << date.substr(date.length() - 2) << std::setfill('0')
<< std::setw(2) << mon << std::setw(2) << day;
return s.str();
} else
return "Stockfish " + EngineVersion; return "Stockfish " + EngineVersion;
string date(__DATE__); // From compiler, format is "Sep 21 2008"
string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
stringstream s;
string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
string name = AppName + " " + AppTag + " ";
s << name << date.substr(date.length() - 2) << setfill('0')
<< setw(2) << mon << setw(2) << day;
return s.str();
} }
+14 -24
View File
@@ -26,20 +26,10 @@
//// Includes //// Includes
//// ////
#include <fstream>
#include <string> #include <string>
////
//// Constants
////
/// Version number. If this is left empty, the current date (in the format
/// YYMMDD) is used as a version number.
const std::string EngineVersion = "1.01";
//// ////
//// Macros //// Macros
//// ////
@@ -60,21 +50,21 @@ extern int Bioskey();
//// ////
//// Debug //// Debug
//// ////
extern long dbg_cnt0;
extern long dbg_cnt1;
inline void dbg_hit_on(bool b) { dbg_cnt0++; if (b) dbg_cnt1++; }
inline void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
inline void dbg_before() { dbg_cnt0++; }
inline void dbg_after() { dbg_cnt1++; }
inline void dbg_mean_of(int v) { dbg_cnt0++; dbg_cnt1 += v; }
extern void dbg_print_hit_rate();
extern void dbg_print_mean();
extern bool dbg_show_mean; extern bool dbg_show_mean;
extern bool dbg_show_hit_rate; extern bool dbg_show_hit_rate;
extern long dbg_cnt0;
extern long dbg_cnt1;
extern void dbg_hit_on(bool b);
extern void dbg_hit_on_c(bool c, bool b);
extern void dbg_before();
extern void dbg_after();
extern void dbg_mean_of(int v);
extern void dbg_print_hit_rate();
extern void dbg_print_mean();
extern void dbg_print_hit_rate(std::ofstream& logFile);
extern void dbg_print_mean(std::ofstream& logFile);
#endif // !defined(MISC_H_INCLUDED) #endif // !defined(MISC_H_INCLUDED)
+5 -5
View File
@@ -72,13 +72,13 @@ Move move_from_string(const Position& pos, const std::string& str) {
} }
} }
if (piece == king_of_color(us)) if (piece == piece_of_color_and_type(us, KING))
{ {
// Is this a castling move? A king move is assumed to be a castling // Is this a castling move? A king move is assumed to be a castling
// move if the destination square is occupied by a friendly rook, or // move if the destination square is occupied by a friendly rook, or
// if the distance between the source and destination squares is more // if the distance between the source and destination squares is more
// than 1. // than 1.
if (pos.piece_on(to) == rook_of_color(us)) if (pos.piece_on(to) == piece_of_color_and_type(us, ROOK))
return make_castle_move(from, to); return make_castle_move(from, to);
else if (square_distance(from, to) > 1) else if (square_distance(from, to) > 1)
@@ -87,13 +87,13 @@ Move move_from_string(const Position& pos, const std::string& str) {
// internal "king captures rook" representation. // internal "king captures rook" representation.
SquareDelta delta = (to > from ? DELTA_E : DELTA_W); SquareDelta delta = (to > from ? DELTA_E : DELTA_W);
Square s = from + delta; Square s = from + delta;
while (relative_rank(us, s) == RANK_1 && pos.piece_on(s) != rook_of_color(us)) while (relative_rank(us, s) == RANK_1 && pos.piece_on(s) != piece_of_color_and_type(us, ROOK))
s += delta; s += delta;
return (relative_rank(us, s) == RANK_1 ? make_castle_move(from, s) : MOVE_NONE); return (relative_rank(us, s) == RANK_1 ? make_castle_move(from, s) : MOVE_NONE);
} }
} }
else if (piece == pawn_of_color(us)) else if (piece == piece_of_color_and_type(us, PAWN))
{ {
// En passant move? We assume that a pawn move is an en passant move // En passant move? We assume that a pawn move is an en passant move
// without further testing if the destination square is epSquare. // without further testing if the destination square is epSquare.
@@ -139,7 +139,7 @@ const std::string move_to_string(Move move) {
/// Overload the << operator, to make it easier to print moves. /// Overload the << operator, to make it easier to print moves.
std::ostream &operator << (std::ostream &os, Move m) { std::ostream &operator << (std::ostream& os, Move m) {
return os << move_to_string(m); return os << move_to_string(m);
} }
+19 -4
View File
@@ -38,10 +38,22 @@
class Position; class Position;
/// A move needs 17 bits to be stored
///
/// bit 0- 5: destination square (from 0 to 63)
/// bit 6-11: origin square (from 0 to 63)
/// bit 12-14: promotion piece type
/// bit 15: en passant flag
/// bit 16: castle flag
///
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in
/// because in any normal move destination square is always different
/// from origin square while MOVE_NONE and MOVE_NULL have the same
/// origin and destination square, 0 and 1 respectively.
enum Move { enum Move {
MOVE_NONE = 0, MOVE_NONE = 0,
MOVE_NULL = 65, MOVE_NULL = 65
MOVE_MAX = 0xFFFFFF
}; };
@@ -50,17 +62,20 @@ struct MoveStack {
int score; int score;
}; };
// Note that operator< is set up such that std::sort() will sort in descending order
inline bool operator<(const MoveStack& f, const MoveStack& s) { return s.score < f.score; }
//// ////
//// Inline functions //// Inline functions
//// ////
inline Square move_from(Move m) { inline Square move_from(Move m) {
return Square((int(m) >> 6) & 077); return Square((int(m) >> 6) & 0x3F);
} }
inline Square move_to(Move m) { inline Square move_to(Move m) {
return Square(m & 077); return Square(m & 0x3F);
} }
inline PieceType move_promotion(Move m) { inline PieceType move_promotion(Move m) {
+298 -313
View File
@@ -35,73 +35,93 @@
namespace { namespace {
// Function enum CastlingSide {
MoveStack* generate_castle_moves(const Position&, MoveStack*); KING_SIDE,
QUEEN_SIDE
};
// Template generate_pawn_captures() with specializations enum MoveType {
template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta> CAPTURE,
MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist); NON_CAPTURE
};
template<Color> // Functions
inline MoveStack* generate_pawn_captures(const Position& p, MoveStack* m) { bool castling_is_check(const Position&, CastlingSide);
return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
}
template<>
inline MoveStack* generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
}
// Template generate_pawn_noncaptures() with specializations // Helper templates
template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta> template<CastlingSide Side>
MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist); MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
template<Color>
inline MoveStack* generate_pawn_noncaptures(const Position& p, MoveStack* m) {
return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
}
template<>
inline MoveStack* generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
}
// Template generate_pawn_blocking_evasions() with specializations
template<Color Us, Rank, Bitboard, SquareDelta> template<Color Us, Rank, Bitboard, SquareDelta>
MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned, MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
Bitboard blockSquares, MoveStack* mlist);
template<Color> template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
inline MoveStack* generate_pawn_blocking_evasions(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) { MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
return do_generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, np, bs, m);
} template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
template<> MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
inline MoveStack* generate_pawn_blocking_evasions<BLACK>(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
return do_generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, np, bs, m);
}
// Template generate_pawn_checks() with specializations
template<Color, Color, Bitboard, Bitboard, SquareDelta> template<Color, Color, Bitboard, Bitboard, SquareDelta>
MoveStack* do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*); MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
template<Color> template<Color Us, SquareDelta Direction>
inline MoveStack* generate_pawn_checks(const Position& p, Bitboard dc, Square ksq, MoveStack* m) { inline Bitboard move_pawns(Bitboard p) {
return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
if (Direction == DELTA_N)
return Us == WHITE ? p << 8 : p >> 8;
else if (Direction == DELTA_NE)
return Us == WHITE ? p << 9 : p >> 7;
else if (Direction == DELTA_NW)
return Us == WHITE ? p << 7 : p >> 9;
else
return p;
} }
// Template generate_piece_checks() with specializations
template<PieceType>
MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
template<> template<>
inline MoveStack* generate_pawn_checks<BLACK>(const Position& p, Bitboard dc, Square ksq, MoveStack* m) { inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
if (us == WHITE)
return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
else
return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
} }
// non-pawn templates // Template generate_piece_moves() with specializations and overloads
template<PieceType> template<PieceType>
MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard); MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
template<> template<>
MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target); MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
template<PieceType Piece, MoveType Type>
inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
assert(Piece == PAWN);
if (Type == CAPTURE)
return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
: generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
else
return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
: generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
}
template<PieceType> template<PieceType>
MoveStack* generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*); MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
MoveStack* generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
template<PieceType> template<>
MoveStack* generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*); inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
Color us, Bitboard t, Bitboard pnd) {
if (us == WHITE)
return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, t, m);
else
return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, t, m);
}
} }
@@ -122,22 +142,18 @@ int generate_captures(const Position& pos, MoveStack* mlist) {
Bitboard target = pos.pieces_of_color(opposite_color(us)); Bitboard target = pos.pieces_of_color(opposite_color(us));
MoveStack* mlist_start = mlist; MoveStack* mlist_start = mlist;
if (us == WHITE)
mlist = generate_pawn_captures<WHITE>(pos, mlist);
else
mlist = generate_pawn_captures<BLACK>(pos, mlist);
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
mlist = generate_piece_moves<KING>(pos, mlist, us, target); mlist = generate_piece_moves<KING>(pos, mlist, us, target);
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
/// generate_noncaptures() generates all pseudo-legal non-captures and /// generate_noncaptures() generates all pseudo-legal non-captures and
/// underpromotions. The return value is the number of moves generated. /// underpromotions. The return value is the number of moves generated.
int generate_noncaptures(const Position& pos, MoveStack* mlist) { int generate_noncaptures(const Position& pos, MoveStack* mlist) {
@@ -148,26 +164,22 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) {
Bitboard target = pos.empty_squares(); Bitboard target = pos.empty_squares();
MoveStack* mlist_start = mlist; MoveStack* mlist_start = mlist;
if (us == WHITE) mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
mlist = generate_pawn_noncaptures<WHITE>(pos, mlist);
else
mlist = generate_pawn_noncaptures<BLACK>(pos, mlist);
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target); mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target); mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_piece_moves<KING>(pos, mlist, us, target); mlist = generate_piece_moves<KING>(pos, mlist, us, target);
mlist = generate_castle_moves(pos, mlist); mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
/// generate_checks() generates all pseudo-legal non-capturing, non-promoting /// generate_non_capture_checks() generates all pseudo-legal non-capturing,
/// checks, except castling moves (will add this later). It returns the /// non-promoting checks. It returns the number of generated moves.
/// number of generated moves.
int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
@@ -176,48 +188,36 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
Square ksq = pos.king_square(opposite_color(us)); Square ksq = pos.king_square(opposite_color(us));
MoveStack* mlist_start = mlist; MoveStack* mlist_start = mlist;
assert(pos.piece_on(ksq) == king_of_color(opposite_color(us))); assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
dc = pos.discovered_check_candidates(us);
// Pawn moves
if (us == WHITE)
mlist = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
else
mlist = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
// Pieces moves // Pieces moves
Bitboard b = pos.knights(us); mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
if (b) mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist); mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
b = pos.bishops(us); // Castling moves that give check. Very rare but nice to have!
if (b) if ( pos.can_castle_queenside(us)
mlist = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist); && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
&& castling_is_check(pos, QUEEN_SIDE))
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
b = pos.rooks(us); if ( pos.can_castle_kingside(us)
if (b) && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
mlist = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist); && castling_is_check(pos, KING_SIDE))
mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
b = pos.queens(us);
if (b)
mlist = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist);
// Hopefully we always have a king ;-)
mlist = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist);
// TODO: Castling moves!
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
/// generate_evasions() generates all check evasions when the side to move is /// generate_evasions() generates all check evasions when the side to move is
/// in check. Unlike the other move generation functions, this one generates /// in check. Unlike the other move generation functions, this one generates
/// only legal moves. It returns the number of generated moves. This /// only legal moves. It returns the number of generated moves.
/// function is very ugly, and needs cleaning up some time later. FIXME
int generate_evasions(const Position& pos, MoveStack* mlist) { int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(pos.is_check()); assert(pos.is_check());
@@ -228,49 +228,56 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
Square ksq = pos.king_square(us); Square ksq = pos.king_square(us);
MoveStack* mlist_start = mlist; MoveStack* mlist_start = mlist;
assert(pos.piece_on(ksq) == king_of_color(us)); assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
// The bitboard of occupied pieces without our king
Bitboard b_noKing = pos.occupied_squares();
clear_bit(&b_noKing, ksq);
// Find squares attacked by slider checkers, we will
// remove them from king evasions set so to avoid a couple
// of cycles in the slow king evasions legality check loop
// and to be able to use square_is_attacked().
Bitboard checkers = pos.checkers();
Bitboard checkersAttacks = EmptyBoardBB;
Bitboard b = checkers & (pos.queens() | pos.bishops());
while (b)
{
from = pop_1st_bit(&b);
checkersAttacks |= bishop_attacks_bb(from, b_noKing);
}
b = checkers & (pos.queens() | pos.rooks());
while (b)
{
from = pop_1st_bit(&b);
checkersAttacks |= rook_attacks_bb(from, b_noKing);
}
// Generate evasions for king // Generate evasions for king
Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us); Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
Bitboard b2 = pos.occupied_squares();
clear_bit(&b2, ksq);
while (b1) while (b1)
{ {
to = pop_1st_bit(&b1); to = pop_1st_bit(&b1);
// Note that we can use square_is_attacked() only because we
// Make sure 'to' is not attacked by the other side. This is a bit ugly, // have already removed slider checkers.
// because we can't use Position::square_is_attacked. Instead we use if (!pos.square_is_attacked(to, them))
// the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard (*mlist++).move = make_move(ksq, to);
// b2 (the occupied squares with the king removed) in order to test whether
// the king will remain in check on the destination square.
if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
|| (pos.pawn_attacks(us, to) & pos.pawns(them))
|| (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
|| (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
|| (pos.piece_attacks<KING>(to) & pos.kings(them))))
(*mlist++).move = make_move(ksq, to);
} }
// Generate evasions for other pieces only if not double check. We use a // Generate evasions for other pieces only if not double check. We use a
// simple bit twiddling hack here rather than calling count_1s in order to // simple bit twiddling hack here rather than calling count_1s in order to
// save some time (we know that pos.checkers() has at most two nonzero bits). // save some time (we know that pos.checkers() has at most two nonzero bits).
Bitboard checkers = pos.checkers();
if (!(checkers & (checkers - 1))) // Only one bit set? if (!(checkers & (checkers - 1))) // Only one bit set?
{ {
Square checksq = first_1(checkers); Square checksq = first_1(checkers);
assert(pos.color_of_piece_on(checksq) == them); assert(pos.color_of_piece_on(checksq) == them);
// Find pinned pieces
Bitboard not_pinned = ~pos.pinned_pieces(us);
// Generate captures of the checking piece // Generate captures of the checking piece
// Pawn captures // Pawn captures
b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned; b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
while (b1) while (b1)
{ {
from = pop_1st_bit(&b1); from = pop_1st_bit(&b1);
@@ -287,7 +294,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
// Pieces captures // Pieces captures
b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us)) b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
| (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us)) | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
| (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned; | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & ~pinned;
while (b1) while (b1)
{ {
@@ -296,77 +303,56 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
} }
// Blocking check evasions are possible only if the checking piece is // Blocking check evasions are possible only if the checking piece is
// a slider // a slider.
if (checkers & pos.sliders()) if (checkers & pos.sliders())
{ {
Bitboard blockSquares = squares_between(checksq, ksq); Bitboard blockSquares = squares_between(checksq, ksq);
assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB); assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
// Pawn moves. Because a blocking evasion can never be a capture, we if (blockSquares != EmptyBoardBB)
// only generate pawn pushes. {
if (us == WHITE) mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
mlist = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
else mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
mlist = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist); mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
}
}
// Pieces moves // Finally, the special case of en passant captures. An en passant
b1 = pos.knights(us) & not_pinned; // capture can only be a check evasion if the check is not a discovered
if (b1) // check. If pos.ep_square() is set, the last move made must have been
mlist = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist); // a double pawn push. If, furthermore, the checking piece is a pawn,
// an en passant check evasion may be possible.
if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
{
to = pos.ep_square();
b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
b1 = pos.bishops(us) & not_pinned; // The checking pawn cannot be a discovered (bishop) check candidate
if (b1) // otherwise we were in check also before last double push move.
mlist = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist); assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
assert(count_1s(b1) == 1 || count_1s(b1) == 2);
b1 = pos.rooks(us) & not_pinned; b1 &= ~pinned;
if (b1) while (b1)
mlist = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist); {
from = pop_1st_bit(&b1);
b1 = pos.queens(us) & not_pinned; // Move is always legal because checking pawn is not a discovered
if (b1) // check candidate and our capturing pawn has been already tested
mlist = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist); // against pinned pieces.
} (*mlist++).move = make_ep_move(from, to);
}
// Finally, the ugly special case of en passant captures. An en passant }
// capture can only be a check evasion if the check is not a discovered
// check. If pos.ep_square() is set, the last move made must have been
// a double pawn push. If, furthermore, the checking piece is a pawn,
// an en passant check evasion may be possible.
if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
{
to = pos.ep_square();
b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
assert(b1 != EmptyBoardBB);
b1 &= not_pinned;
while (b1)
{
from = pop_1st_bit(&b1);
// Before generating the move, we have to make sure it is legal.
// This is somewhat tricky, because the two disappearing pawns may
// cause new "discovered checks". We test this by removing the
// two relevant bits from the occupied squares bitboard, and using
// the low-level bitboard functions for bishop and rook attacks.
b2 = pos.occupied_squares();
clear_bit(&b2, from);
clear_bit(&b2, checksq);
if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
(*mlist++).move = make_ep_move(from, to);
}
}
} }
return int(mlist - mlist_start); return int(mlist - mlist_start);
} }
/// generate_legal_moves() computes a complete list of legal moves in the /// generate_legal_moves() computes a complete list of legal moves in the
/// current position. This function is not very fast, and should be used /// current position. This function is not very fast, and should be used
/// only in situations where performance is unimportant. It wouldn't be /// only in situations where performance is unimportant. It wouldn't be
/// very hard to write an efficient legal move generator, but for the moment /// very hard to write an efficient legal move generator, but for the moment
/// we don't need it. /// we don't need it.
@@ -374,15 +360,15 @@ int generate_legal_moves(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
if (pos.is_check()) if (pos.is_check())
return generate_evasions(pos, mlist); return generate_evasions(pos, mlist, pinned);
// Generate pseudo-legal moves // Generate pseudo-legal moves
int n = generate_captures(pos, mlist); int n = generate_captures(pos, mlist);
n += generate_noncaptures(pos, mlist + n); n += generate_noncaptures(pos, mlist + n);
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
// Remove illegal moves from the list // Remove illegal moves from the list
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
if (!pos.pl_move_is_legal(mlist[i].move, pinned)) if (!pos.pl_move_is_legal(mlist[i].move, pinned))
@@ -427,7 +413,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
return false; return false;
assert(pos.square_is_empty(to)); assert(pos.square_is_empty(to));
assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them)); assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
// The move is pseudo-legal, check if it is also legal // The move is pseudo-legal, check if it is also legal
return pos.pl_move_is_legal(m, pinned); return pos.pl_move_is_legal(m, pinned);
@@ -444,7 +430,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
assert(from == pos.king_square(us)); assert(from == pos.king_square(us));
assert(to == pos.initial_kr_square(us)); assert(to == pos.initial_kr_square(us));
assert(pos.piece_on(to) == rook_of_color(us)); assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
Square g1 = relative_square(us, SQ_G1); Square g1 = relative_square(us, SQ_G1);
Square f1 = relative_square(us, SQ_F1); Square f1 = relative_square(us, SQ_F1);
@@ -477,7 +463,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
assert(from == pos.king_square(us)); assert(from == pos.king_square(us));
assert(to == pos.initial_qr_square(us)); assert(to == pos.initial_qr_square(us));
assert(pos.piece_on(to) == rook_of_color(us)); assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
Square c1 = relative_square(us, SQ_C1); Square c1 = relative_square(us, SQ_C1);
Square d1 = relative_square(us, SQ_D1); Square d1 = relative_square(us, SQ_D1);
@@ -494,8 +480,8 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
illegal = true; illegal = true;
if ( square_file(to) == FILE_B if ( square_file(to) == FILE_B
&& ( pos.piece_on(to + DELTA_W) == rook_of_color(them) && ( pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, ROOK)
|| pos.piece_on(to + DELTA_W) == queen_of_color(them))) || pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, QUEEN)))
illegal = true; illegal = true;
return !illegal; return !illegal;
@@ -566,7 +552,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
} }
// Luckly we can handle all the other pieces in one go // Luckly we can handle all the other pieces in one go
return ( pos.piece_attacks_square(from, to) return ( pos.piece_attacks_square(pos.piece_on(from), from, to)
&& pos.pl_move_is_legal(m, pinned) && pos.pl_move_is_legal(m, pinned)
&& !move_promotion(m)); && !move_promotion(m));
} }
@@ -589,6 +575,24 @@ namespace {
return mlist; return mlist;
} }
template<PieceType Piece>
MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
Color us, Bitboard target, Bitboard pinned) {
Square from;
Bitboard b;
for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
{
from = pos.piece_list(us, Piece, i);
if (pinned && bit_is_set(pinned, from))
continue;
b = pos.piece_attacks<Piece>(from) & target;
SERIALIZE_MOVES(b);
}
return mlist;
}
template<> template<>
MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
@@ -600,30 +604,17 @@ namespace {
return mlist; return mlist;
} }
template<PieceType Piece>
MoveStack* generate_piece_blocking_evasions(const Position& pos, Bitboard b,
Bitboard blockSquares, MoveStack* mlist) {
while (b)
{
Square from = pop_1st_bit(&b);
Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
SERIALIZE_MOVES(bb);
}
return mlist;
}
template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE, template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
SquareDelta TDELTA_NW, SquareDelta TDELTA_N SquareDelta TDELTA_NW, SquareDelta TDELTA_N
> >
MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) { MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
Square to; Square to;
Bitboard pawns = pos.pawns(Us); Bitboard pawns = pos.pawns(Us);
Bitboard enemyPieces = pos.pieces_of_color(Them); Bitboard enemyPieces = pos.pieces_of_color(Them);
// Captures in the a1-h8 (a8-h1 for black) direction // Captures in the a1-h8 (a8-h1 for black) direction
Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces; Bitboard b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces;
// Capturing promotions // Capturing promotions
Bitboard b2 = b1 & TRank8BB; Bitboard b2 = b1 & TRank8BB;
@@ -642,7 +633,7 @@ namespace {
} }
// Captures in the h1-a8 (h8-a1 for black) direction // Captures in the h1-a8 (h8-a1 for black) direction
b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces; b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces;
// Capturing promotions // Capturing promotions
b2 = b1 & TRank8BB; b2 = b1 & TRank8BB;
@@ -661,7 +652,7 @@ namespace {
} }
// Non-capturing promotions // Non-capturing promotions
b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB; b1 = move_pawns<Us, DELTA_N>(pawns) & pos.empty_squares() & TRank8BB;
while (b1) while (b1)
{ {
to = pop_1st_bit(&b1); to = pop_1st_bit(&b1);
@@ -689,7 +680,7 @@ namespace {
template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
> >
MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) { MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
Bitboard pawns = pos.pawns(Us); Bitboard pawns = pos.pawns(Us);
Bitboard enemyPieces = pos.pieces_of_color(Them); Bitboard enemyPieces = pos.pieces_of_color(Them);
@@ -698,7 +689,7 @@ namespace {
Square to; Square to;
// Underpromotion captures in the a1-h8 (a8-h1 for black) direction // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB; b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces & TRank8BB;
while (b1) while (b1)
{ {
to = pop_1st_bit(&b1); to = pop_1st_bit(&b1);
@@ -708,7 +699,7 @@ namespace {
} }
// Underpromotion captures in the h1-a8 (h8-a1 for black) direction // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB; b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces & TRank8BB;
while (b1) while (b1)
{ {
to = pop_1st_bit(&b1); to = pop_1st_bit(&b1);
@@ -718,14 +709,14 @@ namespace {
} }
// Single pawn pushes // Single pawn pushes
b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares; b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares;
b2 = b1 & TRank8BB; b2 = b1 & TRank8BB;
while (b2) while (b2)
{ {
to = pop_1st_bit(&b2); to = pop_1st_bit(&b2);
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK); (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP); (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT); (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
} }
b2 = b1 & ~TRank8BB; b2 = b1 & ~TRank8BB;
while (b2) while (b2)
@@ -735,50 +726,53 @@ namespace {
} }
// Double pawn pushes // Double pawn pushes
b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares; b2 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
while (b2) while (b2)
{ {
to = pop_1st_bit(&b2); to = pop_1st_bit(&b2);
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to); (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
} }
return mlist; return mlist;
} }
template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N> template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist) MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
{ {
// Pawn moves which give discovered check. This is possible only if the // Find all friendly pawns not on the enemy king's file
// pawn is not on the same file as the enemy king, because we don't Bitboard b1, b2, b3;
// generate captures.
Bitboard empty = pos.empty_squares(); Bitboard empty = pos.empty_squares();
// Find all friendly pawns not on the enemy king's file if (dc != EmptyBoardBB)
Bitboard b1 = pos.pawns(Us) & ~file_bb(ksq), b2, b3;
// Discovered checks, single pawn pushes
b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~TRank8BB & empty;
while (b3)
{ {
Square to = pop_1st_bit(&b3); // Pawn moves which gives discovered check. This is possible only if the
(*mlist++).move = make_move(to - TDELTA_N, to); // pawn is not on the same file as the enemy king, because we don't
} // generate captures.
b1 = pos.pawns(Us) & ~file_bb(ksq);
// Discovered checks, double pawn pushes // Discovered checks, single pawn pushes, no promotions
b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty; b2 = b3 = move_pawns<Us, DELTA_N>(b1 & dc) & empty & ~TRank8BB;
while (b3) while (b3)
{ {
Square to = pop_1st_bit(&b3); Square to = pop_1st_bit(&b3);
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to); (*mlist++).move = make_move(to - TDELTA_N, to);
}
// Discovered checks, double pawn pushes
b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty;
while (b3)
{
Square to = pop_1st_bit(&b3);
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
}
} }
// Direct checks. These are possible only for pawns on neighboring files // Direct checks. These are possible only for pawns on neighboring files
// of the enemy king // of the enemy king.
b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
// Direct checks, single pawn pushes // Direct checks, single pawn pushes
b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty; b2 = move_pawns<Us, DELTA_N>(b1) & empty;
b3 = b2 & pos.pawn_attacks(Them, ksq); b3 = b2 & pos.pawn_attacks(Them, ksq);
while (b3) while (b3)
{ {
@@ -787,10 +781,7 @@ namespace {
} }
// Direct checks, double pawn pushes // Direct checks, double pawn pushes
b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty & pos.pawn_attacks(Them, ksq);
& empty
& pos.pawn_attacks(Them, ksq);
while (b3) while (b3)
{ {
Square to = pop_1st_bit(&b3); Square to = pop_1st_bit(&b3);
@@ -800,52 +791,57 @@ namespace {
} }
template<PieceType Piece> template<PieceType Piece>
MoveStack* generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc, MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
Square ksq, MoveStack* mlist) { Bitboard dc, Square ksq) {
Bitboard target = pos.pieces_of_color_and_type(us, Piece);
// Discovered checks // Discovered checks
Bitboard b = target & dc; Bitboard b = target & dc;
while (b) while (b)
{ {
Square from = pop_1st_bit(&b); Square from = pop_1st_bit(&b);
Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares(); Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
if (Piece == KING)
bb &= ~QueenPseudoAttacks[ksq];
SERIALIZE_MOVES(bb); SERIALIZE_MOVES(bb);
} }
// Direct checks // Direct checks
b = target & ~dc; b = target & ~dc;
if (Piece == KING || !b)
return mlist;
Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares(); Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
if (!checkSqs)
return mlist;
while (b) while (b)
{ {
Square from = pop_1st_bit(&b); Square from = pop_1st_bit(&b);
if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
|| (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
|| (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
continue;
Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs; Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
SERIALIZE_MOVES(bb); SERIALIZE_MOVES(bb);
} }
return mlist; return mlist;
} }
MoveStack* generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
Square ksq, MoveStack* mlist) {
if (bit_is_set(dc, from))
{
Bitboard b = pos.piece_attacks<KING>(from)
& pos.empty_squares()
& ~QueenPseudoAttacks[ksq];
SERIALIZE_MOVES(b);
}
return mlist;
}
template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N> template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned, MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
Bitboard blockSquares, MoveStack* mlist) { Bitboard blockSquares, MoveStack* mlist) {
Square to; Square to;
// Find non-pinned pawns // Find non-pinned pawns and push them one square
Bitboard b1 = pos.pawns(Us) & not_pinned; Bitboard b1 = move_pawns<Us, DELTA_N>(pos.pawns(Us) & ~pinned);
// Single pawn pushes. We don't have to AND with empty squares here, // We don't have to AND with empty squares here,
// because the blocking squares will always be empty. // because the blocking squares will always be empty.
Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares; Bitboard b2 = b1 & blockSquares;
while (b2) while (b2)
{ {
to = pop_1st_bit(&b2); to = pop_1st_bit(&b2);
@@ -863,8 +859,8 @@ namespace {
} }
// Double pawn pushes // Double pawn pushes
b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB; b2 = b1 & pos.empty_squares() & TRank3BB;
b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;; b2 = move_pawns<Us, DELTA_N>(b2) & blockSquares;
while (b2) while (b2)
{ {
to = pop_1st_bit(&b2); to = pop_1st_bit(&b2);
@@ -878,70 +874,59 @@ namespace {
return mlist; return mlist;
} }
template<CastlingSide Side>
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) { MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
Color us = pos.side_to_move(); Color us = pos.side_to_move();
if (pos.can_castle(us)) if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
{ {
Color them = opposite_color(us); Color them = opposite_color(us);
Square ksq = pos.king_square(us); Square ksq = pos.king_square(us);
assert(pos.piece_on(ksq) == king_of_color(us)); assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
if (pos.can_castle_kingside(us)) Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
{ Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
Square rsq = pos.initial_kr_square(us); Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
Square g1 = relative_square(us, SQ_G1); Square s;
Square f1 = relative_square(us, SQ_F1); bool illegal = false;
Square s;
bool illegal = false;
assert(pos.piece_on(rsq) == rook_of_color(us)); assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
for (s = Min(ksq, g1); s <= Max(ksq, g1); s++) // It is a bit complicated to correctly handle Chess960
if ( (s != ksq && s != rsq && pos.square_is_occupied(s)) for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
|| pos.square_is_attacked(s, them)) if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
illegal = true; || pos.square_is_attacked(s, them))
illegal = true;
for (s = Min(rsq, f1); s <= Max(rsq, f1); s++) for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
if (s != ksq && s != rsq && pos.square_is_occupied(s)) if (s != ksq && s != rsq && pos.square_is_occupied(s))
illegal = true; illegal = true;
if (!illegal) if ( Side == QUEEN_SIDE
(*mlist++).move = make_castle_move(ksq, rsq); && square_file(rsq) == FILE_B
} && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
|| pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
if (pos.can_castle_queenside(us))
{
Square rsq = pos.initial_qr_square(us);
Square c1 = relative_square(us, SQ_C1);
Square d1 = relative_square(us, SQ_D1);
Square s;
bool illegal = false;
assert(pos.piece_on(rsq) == rook_of_color(us));
for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
|| pos.square_is_attacked(s, them))
illegal = true;
for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
if (s != ksq && s != rsq && pos.square_is_occupied(s))
illegal = true;
if ( square_file(rsq) == FILE_B
&& ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
|| pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
illegal = true; illegal = true;
if (!illegal) if (!illegal)
(*mlist++).move = make_castle_move(ksq, rsq); (*mlist++).move = make_castle_move(ksq, rsq);
}
} }
return mlist; return mlist;
} }
bool castling_is_check(const Position& pos, CastlingSide side) {
// After castling opponent king is attacked by the castled rook?
File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
Color us = pos.side_to_move();
Square ksq = pos.king_square(us);
Bitboard occ = pos.occupied_squares();
clear_bit(&occ, ksq); // Remove our king from the board
Square rsq = make_square(rookFile, square_rank(ksq));
return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));
}
} }
+6 -6
View File
@@ -32,12 +32,12 @@
//// Prototypes //// Prototypes
//// ////
extern int generate_captures(const Position &pos, MoveStack *mlist); extern int generate_captures(const Position& pos, MoveStack* mlist);
extern int generate_noncaptures(const Position &pos, MoveStack *mlist); extern int generate_noncaptures(const Position& pos, MoveStack* mlist);
extern int generate_checks(const Position &pos, MoveStack *mlist, Bitboard dc); extern int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc);
extern int generate_evasions(const Position &pos, MoveStack *mlist); extern int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned);
extern int generate_legal_moves(const Position &pos, MoveStack *mlist); extern int generate_legal_moves(const Position& pos, MoveStack* mlist);
extern bool move_is_legal(const Position &pos, const Move m, Bitboard pinned); extern bool move_is_legal(const Position& pos, const Move m, Bitboard pinned);
#endif // !defined(MOVEGEN_H_INCLUDED) #endif // !defined(MOVEGEN_H_INCLUDED)
+61 -151
View File
@@ -23,9 +23,11 @@
//// Includes //// Includes
//// ////
#include <algorithm>
#include <cassert> #include <cassert>
#include "history.h" #include "history.h"
#include "evaluate.h"
#include "movegen.h" #include "movegen.h"
#include "movepick.h" #include "movepick.h"
#include "search.h" #include "search.h"
@@ -49,7 +51,6 @@ namespace {
} }
//// ////
//// Functions //// Functions
//// ////
@@ -62,29 +63,31 @@ namespace {
/// search captures, promotions and some checks) and about how important good /// search captures, promotions and some checks) and about how important good
/// move ordering is at the current node. /// move ordering is at the current node.
MovePicker::MovePicker(const Position& p, bool pvnode, Move ttm, Move mk, MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
Move k1, Move k2, Depth d) : pos(p) { const SearchStack& ss, Depth d) : pos(p) {
pvNode = pvnode; pvNode = pv;
ttMove = ttm; ttMove = ttm;
mateKiller = (mk == ttm)? MOVE_NONE : mk; mateKiller = (ss.mateKiller == ttm)? MOVE_NONE : ss.mateKiller;
killer1 = k1; killer1 = ss.killers[0];
killer2 = k2; killer2 = ss.killers[1];
depth = d; depth = d;
movesPicked = 0; movesPicked = 0;
numOfMoves = 0; numOfMoves = 0;
numOfBadCaptures = 0; numOfBadCaptures = 0;
dc = p.discovered_check_candidates(p.side_to_move());
if (p.is_check()) if (p.is_check())
phaseIndex = EvasionsPhaseIndex; phaseIndex = EvasionsPhaseIndex;
else if (depth > Depth(0)) else if (depth > Depth(0))
phaseIndex = MainSearchPhaseIndex; phaseIndex = MainSearchPhaseIndex;
else if (depth == Depth(0)) else if (depth == Depth(0))
phaseIndex = QsearchWithChecksPhaseIndex; phaseIndex = QsearchWithChecksPhaseIndex;
else else
phaseIndex = QsearchWithoutChecksPhaseIndex; phaseIndex = QsearchWithoutChecksPhaseIndex;
pinned = p.pinned_pieces(p.side_to_move()); Color us = pos.side_to_move();
dc = p.discovered_check_candidates(us);
pinned = p.pinned_pieces(us);
finished = false; finished = false;
} }
@@ -128,40 +131,46 @@ Move MovePicker::get_next_move() {
assert(move_is_ok(mateKiller)); assert(move_is_ok(mateKiller));
if (move_is_legal(pos, mateKiller, pinned)) if (move_is_legal(pos, mateKiller, pinned))
return mateKiller; return mateKiller;
} }
break; break;
case PH_GOOD_CAPTURES: case PH_GOOD_CAPTURES:
numOfMoves = generate_captures(pos, moves); numOfMoves = generate_captures(pos, moves);
score_captures(); score_captures();
std::sort(moves, moves + numOfMoves);
movesPicked = 0; movesPicked = 0;
break; break;
case PH_BAD_CAPTURES: case PH_BAD_CAPTURES:
badCapturesPicked = 0; // It's probably a good idea to use SEE move ordering here. FIXME
movesPicked = 0;
break; break;
case PH_NONCAPTURES: case PH_NONCAPTURES:
numOfMoves = generate_noncaptures(pos, moves); numOfMoves = generate_noncaptures(pos, moves);
score_noncaptures(); score_noncaptures();
std::sort(moves, moves + numOfMoves);
movesPicked = 0; movesPicked = 0;
break; break;
case PH_EVASIONS: case PH_EVASIONS:
assert(pos.is_check()); assert(pos.is_check());
numOfMoves = generate_evasions(pos, moves); numOfMoves = generate_evasions(pos, moves, pinned);
score_evasions(); score_evasions();
std::sort(moves, moves + numOfMoves);
movesPicked = 0; movesPicked = 0;
break; break;
case PH_QCAPTURES: case PH_QCAPTURES:
numOfMoves = generate_captures(pos, moves); numOfMoves = generate_captures(pos, moves);
score_qcaptures(); score_qcaptures();
std::sort(moves, moves + numOfMoves);
movesPicked = 0; movesPicked = 0;
break; break;
case PH_QCHECKS: case PH_QCHECKS:
numOfMoves = generate_checks(pos, moves, dc); // Perhaps we should order moves move here? FIXME
numOfMoves = generate_non_capture_checks(pos, moves, dc);
movesPicked = 0; movesPicked = 0;
break; break;
@@ -211,6 +220,8 @@ void MovePicker::score_captures() {
// where it is possible to recapture with the hanging piece). Exchanging // where it is possible to recapture with the hanging piece). Exchanging
// big pieces before capturing a hanging piece probably helps to reduce // big pieces before capturing a hanging piece probably helps to reduce
// the subtree size. // the subtree size.
// While scoring captures it moves all captures with negative SEE values
// to the badCaptures[] array.
Move m; Move m;
int seeValue; int seeValue;
@@ -225,8 +236,15 @@ void MovePicker::score_captures() {
else else
moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m))) moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m)))
-int(pos.type_of_piece_on(move_from(m))); -int(pos.type_of_piece_on(move_from(m)));
} else }
else
{
// Losing capture, move it to the badCaptures[] array
assert(numOfBadCaptures < 63);
moves[i].score = seeValue; moves[i].score = seeValue;
badCaptures[numOfBadCaptures++] = moves[i];
moves[i--] = moves[--numOfMoves];
}
} }
} }
@@ -248,10 +266,11 @@ void MovePicker::score_noncaptures() {
else else
hs = H.move_ordering_score(pos.piece_on(move_from(m)), m); hs = H.move_ordering_score(pos.piece_on(move_from(m)), m);
// Ensure moves in history are always sorted as first // Ensure history is always preferred to pst
if (hs > 0) if (hs > 0)
hs += 1000; hs += 1000;
// pst based scoring
moves[i].score = hs + pos.mg_pst_delta(m); moves[i].score = hs + pos.mg_pst_delta(m);
} }
} }
@@ -270,7 +289,6 @@ void MovePicker::score_evasions() {
} else } else
moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m); moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), m);
} }
// FIXME try psqt also here
} }
void MovePicker::score_qcaptures() { void MovePicker::score_qcaptures() {
@@ -288,121 +306,41 @@ void MovePicker::score_qcaptures() {
} }
/// find_best_index() loops across the moves and returns index of
/// the highest scored one.
int MovePicker::find_best_index() {
int bestScore = -10000000, bestIndex = -1;
for (int i = movesPicked; i < numOfMoves; i++)
if (moves[i].score > bestScore)
{
bestIndex = i;
bestScore = moves[i].score;
}
return bestIndex;
}
/// MovePicker::pick_move_from_list() picks the move with the biggest score /// MovePicker::pick_move_from_list() picks the move with the biggest score
/// from a list of generated moves (moves[] or badCaptures[], depending on /// from a list of generated moves (moves[] or badCaptures[], depending on
/// the current move generation phase). It takes care not to return the /// the current move generation phase). It takes care not to return the
/// transposition table move if that has already been serched previously. /// transposition table move if that has already been serched previously.
/// While picking captures in the PH_GOOD_CAPTURES phase (i.e. while picking
/// non-losing captures in the main search), it moves all captures with
/// negative SEE values to the badCaptures[] array.
Move MovePicker::pick_move_from_list() { Move MovePicker::pick_move_from_list() {
int bestIndex; assert(movesPicked >= 0);
Move move; assert(!pos.is_check() || PhaseTable[phaseIndex] == PH_EVASIONS || PhaseTable[phaseIndex] == PH_STOP);
assert( pos.is_check() || PhaseTable[phaseIndex] != PH_EVASIONS);
switch (PhaseTable[phaseIndex]) { switch (PhaseTable[phaseIndex]) {
case PH_GOOD_CAPTURES: case PH_GOOD_CAPTURES:
assert(!pos.is_check());
assert(movesPicked >= 0);
while (movesPicked < numOfMoves)
{
int bestScore = -10000000;
bestIndex = -1;
for (int i = movesPicked; i < numOfMoves; i++)
{
if (moves[i].score < 0)
{
// Losing capture, move it to the badCaptures[] array
assert(numOfBadCaptures < 63);
badCaptures[numOfBadCaptures++] = moves[i];
moves[i--] = moves[--numOfMoves];
}
else if (moves[i].score > bestScore)
{
bestIndex = i;
bestScore = moves[i].score;
}
}
if (bestIndex != -1) // Found a good capture
{
move = moves[bestIndex].move;
moves[bestIndex] = moves[movesPicked++];
if ( move != ttMove
&& move != mateKiller
&& pos.pl_move_is_legal(move, pinned))
return move;
}
}
break;
case PH_NONCAPTURES: case PH_NONCAPTURES:
assert(!pos.is_check());
assert(movesPicked >= 0);
while (movesPicked < numOfMoves) while (movesPicked < numOfMoves)
{ {
// If this is a PV node or we have only picked a few moves, scan Move move = moves[movesPicked++].move;
// the entire move list for the best move. If many moves have already if ( move != ttMove
// been searched and it is not a PV node, we are probably failing low && move != mateKiller
// anyway, so we just pick the first move from the list. && pos.pl_move_is_legal(move, pinned))
bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked; return move;
if (bestIndex != -1)
{
move = moves[bestIndex].move;
moves[bestIndex] = moves[movesPicked++];
if ( move != ttMove
&& move != mateKiller
&& pos.pl_move_is_legal(move, pinned))
return move;
}
} }
break; break;
case PH_EVASIONS: case PH_EVASIONS:
assert(pos.is_check()); if (movesPicked < numOfMoves)
assert(movesPicked >= 0); return moves[movesPicked++].move;
while (movesPicked < numOfMoves) break;
{
bestIndex = find_best_index();
if (bestIndex != -1)
{
move = moves[bestIndex].move;
moves[bestIndex] = moves[movesPicked++];
return move;
}
}
break;
case PH_BAD_CAPTURES: case PH_BAD_CAPTURES:
assert(!pos.is_check()); while (movesPicked < numOfBadCaptures)
assert(badCapturesPicked >= 0);
// It's probably a good idea to use SEE move ordering here, instead
// of just picking the first move. FIXME
while (badCapturesPicked < numOfBadCaptures)
{ {
move = badCaptures[badCapturesPicked++].move; Move move = badCaptures[movesPicked++].move;
if ( move != ttMove if ( move != ttMove
&& move != mateKiller && move != mateKiller
&& pos.pl_move_is_legal(move, pinned)) && pos.pl_move_is_legal(move, pinned))
@@ -411,34 +349,13 @@ Move MovePicker::pick_move_from_list() {
break; break;
case PH_QCAPTURES: case PH_QCAPTURES:
assert(!pos.is_check());
assert(movesPicked >= 0);
while (movesPicked < numOfMoves)
{
bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
if (bestIndex != -1)
{
move = moves[bestIndex].move;
moves[bestIndex] = moves[movesPicked++];
// Remember to change the line below if we decide to hash the qsearch!
// Maybe also postpone the legality check until after futility pruning?
if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
return move;
}
}
break;
case PH_QCHECKS: case PH_QCHECKS:
assert(!pos.is_check());
assert(movesPicked >= 0);
// Perhaps we should do something better than just picking the first
// move here? FIXME
while (movesPicked < numOfMoves) while (movesPicked < numOfMoves)
{ {
move = moves[movesPicked++].move; Move move = moves[movesPicked++].move;
// Remember to change the line below if we decide to hash the qsearch! // Maybe postpone the legality check until after futility pruning?
if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned)) if ( move != ttMove
&& pos.pl_move_is_legal(move, pinned))
return move; return move;
} }
break; break;
@@ -450,19 +367,10 @@ Move MovePicker::pick_move_from_list() {
} }
/// MovePicker::current_move_type() returns the type of the just
/// picked next move. It can be used in search to further differentiate
/// according to the current move type: capture, non capture, escape, etc.
MovePicker::MovegenPhase MovePicker::current_move_type() const {
return PhaseTable[phaseIndex];
}
/// MovePicker::init_phase_table() initializes the PhaseTable[], /// MovePicker::init_phase_table() initializes the PhaseTable[],
/// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex /// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
/// and QsearchWithoutChecksPhaseIndex variables. It is only called once /// and QsearchWithoutChecksPhaseIndex. It is only called once during
/// during program startup, and never again while the program is running. /// program startup, and never again while the program is running.
void MovePicker::init_phase_table() { void MovePicker::init_phase_table() {
@@ -487,12 +395,14 @@ void MovePicker::init_phase_table() {
// Quiescence search with checks // Quiescence search with checks
QsearchWithChecksPhaseIndex = i - 1; QsearchWithChecksPhaseIndex = i - 1;
PhaseTable[i++] = PH_TT_MOVE;
PhaseTable[i++] = PH_QCAPTURES; PhaseTable[i++] = PH_QCAPTURES;
PhaseTable[i++] = PH_QCHECKS; PhaseTable[i++] = PH_QCHECKS;
PhaseTable[i++] = PH_STOP; PhaseTable[i++] = PH_STOP;
// Quiescence search without checks // Quiescence search without checks
QsearchWithoutChecksPhaseIndex = i - 1; QsearchWithoutChecksPhaseIndex = i - 1;
PhaseTable[i++] = PH_TT_MOVE;
PhaseTable[i++] = PH_QCAPTURES; PhaseTable[i++] = PH_QCAPTURES;
PhaseTable[i++] = PH_STOP; PhaseTable[i++] = PH_STOP;
} }
+7 -5
View File
@@ -34,6 +34,9 @@
//// Types //// Types
//// ////
struct EvalInfo;
struct SearchStack;
/// MovePicker is a class which is used to pick one legal move at a time from /// MovePicker is a class which is used to pick one legal move at a time from
/// the current position. It is initialized with a Position object and a few /// the current position. It is initialized with a Position object and a few
/// moves we have reason to believe are good. The most important method is /// moves we have reason to believe are good. The most important method is
@@ -44,6 +47,8 @@
class MovePicker { class MovePicker {
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
public: public:
enum MovegenPhase { enum MovegenPhase {
@@ -60,12 +65,11 @@ public:
PH_STOP PH_STOP
}; };
MovePicker(const Position& p, bool pvnode, Move ttm, Move mk, Move k1, Move k2, Depth d); MovePicker(const Position& p, bool pvnode, Move ttm, const SearchStack& ss, Depth d);
Move get_next_move(); Move get_next_move();
Move get_next_move(Lock &lock); Move get_next_move(Lock &lock);
int number_of_moves() const; int number_of_moves() const;
int current_move_score() const; int current_move_score() const;
MovegenPhase current_move_type() const;
Bitboard discovered_check_candidates() const; Bitboard discovered_check_candidates() const;
static void init_phase_table(); static void init_phase_table();
@@ -76,7 +80,6 @@ private:
void score_evasions(); void score_evasions();
void score_qcaptures(); void score_qcaptures();
Move pick_move_from_list(); Move pick_move_from_list();
int find_best_index();
const Position& pos; const Position& pos;
Move ttMove, mateKiller, killer1, killer2; Move ttMove, mateKiller, killer1, killer2;
@@ -86,7 +89,7 @@ private:
Depth depth; Depth depth;
int phaseIndex; int phaseIndex;
int numOfMoves, numOfBadCaptures; int numOfMoves, numOfBadCaptures;
int movesPicked, badCapturesPicked; int movesPicked;
bool finished; bool finished;
}; };
@@ -101,7 +104,6 @@ private:
/// a single reply to check. /// a single reply to check.
inline int MovePicker::number_of_moves() const { inline int MovePicker::number_of_moves() const {
return numOfMoves; return numOfMoves;
} }
+203 -194
View File
@@ -26,6 +26,7 @@
#include <cstring> #include <cstring>
#include "pawns.h" #include "pawns.h"
#include "position.h"
//// ////
@@ -86,50 +87,45 @@ namespace {
// Candidate passed pawn bonus by rank, middle game. // Candidate passed pawn bonus by rank, middle game.
const Value CandidateMidgameBonus[8] = { const Value CandidateMidgameBonus[8] = {
Value(0), Value(12), Value(12), Value(20), Value( 0), Value(12), Value(12), Value(20),
Value(40), Value(90), Value(0), Value(0) Value(40), Value(90), Value( 0), Value( 0)
}; };
// Candidate passed pawn bonus by rank, endgame. // Candidate passed pawn bonus by rank, endgame.
const Value CandidateEndgameBonus[8] = { const Value CandidateEndgameBonus[8] = {
Value(0), Value(24), Value(24), Value(40), Value( 0), Value(24), Value(24), Value(40),
Value(80), Value(180), Value(0), Value(0) Value(80), Value(180), Value(0), Value( 0)
}; };
// Evaluate pawn storms?
const bool EvaluatePawnStorms = true;
// Pawn storm tables for positions with opposite castling: // Pawn storm tables for positions with opposite castling:
const int QStormTable[64] = { const int QStormTable[64] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-22, -22, -22, -13, -4, 0, 0, 0, -22,-22,-22,-13,-4, 0, 0, 0,
-4, -9, -9, -9, -4, 0, 0, 0, -4, -9, -9, -9,-4, 0, 0, 0,
9, 18, 22, 18, 9, 0, 0, 0, 9, 18, 22, 18, 9, 0, 0, 0,
22, 31, 31, 22, 0, 0, 0, 0, 22, 31, 31, 22, 0, 0, 0, 0,
31, 40, 40, 31, 0, 0, 0, 0, 31, 40, 40, 31, 0, 0, 0, 0,
31, 40, 40, 31, 0, 0, 0, 0, 31, 40, 40, 31, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 0, 0, 0
}; };
const int KStormTable[64] = { const int KStormTable[64] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, -4, -13, -22, -27, -27, 0, 0, 0,-4,-13,-22,-27,-27,
0, 0, 0, -4, -9, -13, -18, -18, 0, 0, 0,-4, -9,-13,-18,-18,
0, 0, 0, 0, 9, 9, 9, 9, 0, 0, 0, 0, 9, 9, 9, 9,
0, 0, 0, 0, 9, 18, 27, 27, 0, 0, 0, 0, 9, 18, 27, 27,
0, 0, 0, 0, 9, 27, 40, 36, 0, 0, 0, 0, 9, 27, 40, 36,
0, 0, 0, 0, 0, 31, 40, 31, 0, 0, 0, 0, 0, 31, 40, 31,
0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 0, 0, 0
}; };
// Pawn storm open file bonuses by file: // Pawn storm open file bonuses by file
const int KStormOpenFileBonus[8] = { const int16_t KStormOpenFileBonus[8] = { 45, 45, 30, 0, 0, 0, 0, 0 };
45, 45, 30, 0, 0, 0, 0, 0 const int16_t QStormOpenFileBonus[8] = { 0, 0, 0, 0, 0, 30, 45, 30 };
};
const int QStormOpenFileBonus[8] = { // Pawn storm lever bonuses by file
0, 0, 0, 0, 0, 30, 45, 30 const int StormLeverBonus[8] = { 20, 20, 10, 0, 0, 10, 20, 20 };
};
} }
@@ -141,14 +137,16 @@ namespace {
/// Constructor /// Constructor
PawnInfoTable::PawnInfoTable(unsigned numOfEntries) { PawnInfoTable::PawnInfoTable(unsigned numOfEntries) {
size = numOfEntries; size = numOfEntries;
entries = new PawnInfo[size]; entries = new PawnInfo[size];
if(entries == NULL) { if (entries == NULL)
std::cerr << "Failed to allocate " << (numOfEntries * sizeof(PawnInfo)) {
<< " bytes for pawn hash table." << std::endl; std::cerr << "Failed to allocate " << (numOfEntries * sizeof(PawnInfo))
exit(EXIT_FAILURE); << " bytes for pawn hash table." << std::endl;
exit(EXIT_FAILURE);
} }
this->clear(); clear();
} }
@@ -173,6 +171,7 @@ void PawnInfoTable::clear() {
/// the same pawn structure occurs again. /// the same pawn structure occurs again.
PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) { PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
assert(pos.is_ok()); assert(pos.is_ok());
Key key = pos.get_pawn_key(); Key key = pos.get_pawn_key();
@@ -181,207 +180,217 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
// If pi->key matches the position's pawn hash key, it means that we // If pi->key matches the position's pawn hash key, it means that we
// have analysed this pawn structure before, and we can simply return the // have analysed this pawn structure before, and we can simply return the
// information we found the last time instead of recomputing it: // information we found the last time instead of recomputing it
if(pi->key == key) if (pi->key == key)
return pi; return pi;
// Clear the PawnInfo object, and set the key: // Clear the PawnInfo object, and set the key
pi->clear(); pi->clear();
pi->key = key; pi->key = key;
Value mgValue[2] = {Value(0), Value(0)}; Value mgValue[2] = {Value(0), Value(0)};
Value egValue[2] = {Value(0), Value(0)}; Value egValue[2] = {Value(0), Value(0)};
// Loop through the pawns for both colors: // Loop through the pawns for both colors
for(Color us = WHITE; us <= BLACK; us++) { for (Color us = WHITE; us <= BLACK; us++)
{
Color them = opposite_color(us); Color them = opposite_color(us);
Bitboard ourPawns = pos.pawns(us); Bitboard ourPawns = pos.pawns(us);
Bitboard theirPawns = pos.pawns(them); Bitboard theirPawns = pos.pawns(them);
Bitboard pawns = ourPawns; Bitboard pawns = ourPawns;
int bonus;
// Initialize pawn storm scores by giving bonuses for open files: // Initialize pawn storm scores by giving bonuses for open files
if(EvaluatePawnStorms) for (File f = FILE_A; f <= FILE_H; f++)
for(File f = FILE_A; f <= FILE_H; f++) if (pos.file_is_half_open(us, f))
if(pos.file_is_half_open(us, f)) { {
pi->ksStormValue[us] += KStormOpenFileBonus[f]; pi->ksStormValue[us] += KStormOpenFileBonus[f];
pi->qsStormValue[us] += QStormOpenFileBonus[f]; pi->qsStormValue[us] += QStormOpenFileBonus[f];
} }
// Loop through all pawns of the current color and score each pawn: // Loop through all pawns of the current color and score each pawn
while(pawns) { while (pawns)
Square s = pop_1st_bit(&pawns); {
File f = square_file(s); bool passed, doubled, isolated, backward, chain, candidate;
Rank r = square_rank(s); Square s = pop_1st_bit(&pawns);
bool passed, doubled, isolated, backward, chain, candidate; File f = square_file(s);
int bonus; Rank r = square_rank(s);
assert(pos.piece_on(s) == pawn_of_color(us)); assert(pos.piece_on(s) == piece_of_color_and_type(us, PAWN));
// The file containing the pawn is not half open: // The file containing the pawn is not half open
pi->halfOpenFiles[us] &= ~(1 << f); pi->halfOpenFiles[us] &= ~(1 << f);
// Passed, isolated or doubled pawn? // Passed, isolated or doubled pawn?
passed = pos.pawn_is_passed(us, s); passed = pos.pawn_is_passed(us, s);
isolated = pos.pawn_is_isolated(us, s); isolated = pos.pawn_is_isolated(us, s);
doubled = pos.pawn_is_doubled(us, s); doubled = pos.pawn_is_doubled(us, s);
if(EvaluatePawnStorms) {
// We calculate kingside and queenside pawn storm // We calculate kingside and queenside pawn storm
// scores for both colors. These are used when evaluating // scores for both colors. These are used when evaluating
// middle game positions with opposite side castling. // middle game positions with opposite side castling.
// //
// Each pawn is given a base score given by a piece square table // Each pawn is given a base score given by a piece square table
// (KStormTable[] or QStormTable[]). This score is increased if // (KStormTable[] or QStormTable[]). Pawns which seem to have good
// there are enemy pawns on adjacent files in front of the pawn. // chances of creating an open file by exchanging itself against an
// This is because we want to be able to open files against the // enemy pawn on an adjacent file gets an additional bonus.
// enemy king, and to avoid blocking the pawn structure (e.g. white
// pawns on h6, g5, black pawns on h7, g6, f7).
// Kingside pawn storms: // Kingside pawn storms
bonus = KStormTable[relative_square(us, s)]; bonus = KStormTable[relative_square(us, s)];
if(bonus > 0 && outpost_mask(us, s) & theirPawns) { if (f >= FILE_F)
switch(f) { {
Bitboard b = outpost_mask(us, s) & theirPawns & (FileFBB | FileGBB | FileHBB);
case FILE_F: while (b)
bonus += bonus / 4; {
break; Square s2 = pop_1st_bit(&b);
if (!(theirPawns & neighboring_files_bb(s2) & rank_bb(s2)))
case FILE_G: {
bonus += bonus / 2 + bonus / 4; // The enemy pawn has no pawn beside itself, which makes it
break; // particularly vulnerable. Big bonus, especially against a
// weakness on the rook file.
case FILE_H: if (square_file(s2) == FILE_H)
bonus += bonus / 2; bonus += 4*StormLeverBonus[f] - 8*square_distance(s, s2);
break; else
bonus += 2*StormLeverBonus[f] - 4*square_distance(s, s2);
default: } else
break; // There is at least one enemy pawn beside the enemy pawn we look
} // at, which means that the pawn has somewhat better chances of
// defending itself by advancing. Smaller bonus.
bonus += StormLeverBonus[f] - 2*square_distance(s, s2);
}
} }
pi->ksStormValue[us] += bonus; pi->ksStormValue[us] += bonus;
// Queenside pawn storms: // Queenside pawn storms
bonus = QStormTable[relative_square(us, s)]; bonus = QStormTable[relative_square(us, s)];
if(bonus > 0 && passed_pawn_mask(us, s) & theirPawns) { if (f <= FILE_C)
switch(f) { {
Bitboard b = outpost_mask(us, s) & theirPawns & (FileABB | FileBBB | FileCBB);
case FILE_A: while (b)
bonus += bonus / 2; {
break; Square s2 = pop_1st_bit(&b);
if (!(theirPawns & neighboring_files_bb(s2) & rank_bb(s2)))
case FILE_B: {
bonus += bonus / 2 + bonus / 4; // The enemy pawn has no pawn beside itself, which makes it
break; // particularly vulnerable. Big bonus, especially against a
// weakness on the rook file.
case FILE_C: if (square_file(s2) == FILE_A)
bonus += bonus / 2; bonus += 4*StormLeverBonus[f] - 16*square_distance(s, s2);
break; else
bonus += 2*StormLeverBonus[f] - 8*square_distance(s, s2);
default: } else
break; // There is at least one enemy pawn beside the enemy pawn we look
} // at, which means that the pawn has somewhat better chances of
// defending itself by advancing. Smaller bonus.
bonus += StormLeverBonus[f] - 4*square_distance(s, s2);
}
} }
pi->qsStormValue[us] += bonus; pi->qsStormValue[us] += bonus;
}
// Member of a pawn chain? We could speed up the test a little by // Member of a pawn chain (but not the backward one)? We could speed up
// introducing an array of masks indexed by color and square for doing // the test a little by introducing an array of masks indexed by color
// the test, but because everything is hashed, it probably won't make // and square for doing the test, but because everything is hashed,
// any noticable difference. // it probably won't make any noticable difference.
chain = (us == WHITE)? chain = ourPawns
(ourPawns & neighboring_files_bb(f) & (rank_bb(r) | rank_bb(r-1))) : & neighboring_files_bb(f)
(ourPawns & neighboring_files_bb(f) & (rank_bb(r) | rank_bb(r+1))); & (rank_bb(r) | rank_bb(r - (us == WHITE ? 1 : -1)));
// Test for backward pawn
// Test for backward pawn. //
// If the pawn is passed, isolated, or member of a pawn chain
// If the pawn is isolated, passed, or member of a pawn chain, it cannot // it cannot be backward. If can capture an enemy pawn or if
// be backward: // there are friendly pawns behind on neighboring files it cannot
if(passed || isolated || chain) // be backward either.
backward = false; if ( passed
// If the pawn can capture an enemy pawn, it's not backward: || isolated
else if(pos.pawn_attacks(us, s) & theirPawns) || chain
backward = false; || (pos.pawn_attacks(us, s) & theirPawns)
// Check for friendly pawns behind on neighboring files: || (ourPawns & behind_bb(us, r) & neighboring_files_bb(f)))
else if(ourPawns & in_front_bb(them, r) & neighboring_files_bb(f)) backward = false;
backward = false; else
else { {
// We now know that there is no friendly pawns beside or behind this // We now know that there are no friendly pawns beside or behind this
// pawn on neighboring files. We now check whether the pawn is // pawn on neighboring files. We now check whether the pawn is
// backward by looking in the forward direction on the neighboring // backward by looking in the forward direction on the neighboring
// files, and seeing whether we meet a friendly or an enemy pawn first. // files, and seeing whether we meet a friendly or an enemy pawn first.
Bitboard b; Bitboard b;
if(us == WHITE) { if (us == WHITE)
for(b=pos.pawn_attacks(us, s); !(b&(ourPawns|theirPawns)); b<<=8); {
backward = (b | (b << 8)) & theirPawns; for (b = pos.pawn_attacks(us, s); !(b & (ourPawns | theirPawns)); b <<= 8);
backward = (b | (b << 8)) & theirPawns;
}
else
{
for (b = pos.pawn_attacks(us, s); !(b & (ourPawns | theirPawns)); b >>= 8);
backward = (b | (b >> 8)) & theirPawns;
}
} }
else {
for(b=pos.pawn_attacks(us, s); !(b&(ourPawns|theirPawns)); b>>=8); // Test for candidate passed pawn
backward = (b | (b >> 8)) & theirPawns; candidate = !passed
&& pos.file_is_half_open(them, f)
&& ( count_1s_max_15(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns)
- count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns)
>= 0);
// In order to prevent doubled passed pawns from receiving a too big
// bonus, only the frontmost passed pawn on each file is considered as
// a true passed pawn.
if (passed && (ourPawns & squares_in_front_of(us, s)))
{
// candidate = true;
passed = false;
} }
}
// Test for candidate passed pawn. // Score this pawn
candidate = Value mv = Value(0), ev = Value(0);
(!passed && pos.file_is_half_open(them, f) && if (isolated)
count_1s_max_15(neighboring_files_bb(f) {
& (in_front_bb(them, r) | rank_bb(r)) mv -= IsolatedPawnMidgamePenalty[f];
& ourPawns) ev -= IsolatedPawnEndgamePenalty[f];
- count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) if (pos.file_is_half_open(them, f))
& theirPawns) {
>= 0); mv -= IsolatedPawnMidgamePenalty[f] / 2;
ev -= IsolatedPawnEndgamePenalty[f] / 2;
// In order to prevent doubled passed pawns from receiving a too big }
// bonus, only the frontmost passed pawn on each file is considered as
// a true passed pawn.
if(passed && (ourPawns & squares_in_front_of(us, s))) {
// candidate = true;
passed = false;
}
// Score this pawn:
Value mv = Value(0), ev = Value(0);
if(isolated) {
mv -= IsolatedPawnMidgamePenalty[f];
ev -= IsolatedPawnEndgamePenalty[f];
if(pos.file_is_half_open(them, f)) {
mv -= IsolatedPawnMidgamePenalty[f] / 2;
ev -= IsolatedPawnEndgamePenalty[f] / 2;
} }
} if (doubled)
if(doubled) { {
mv -= DoubledPawnMidgamePenalty[f]; mv -= DoubledPawnMidgamePenalty[f];
ev -= DoubledPawnEndgamePenalty[f]; ev -= DoubledPawnEndgamePenalty[f];
} }
if(backward) { if (backward)
mv -= BackwardPawnMidgamePenalty[f]; {
ev -= BackwardPawnEndgamePenalty[f]; mv -= BackwardPawnMidgamePenalty[f];
if(pos.file_is_half_open(them, f)) { ev -= BackwardPawnEndgamePenalty[f];
mv -= BackwardPawnMidgamePenalty[f] / 2; if (pos.file_is_half_open(them, f))
ev -= BackwardPawnEndgamePenalty[f] / 2; {
mv -= BackwardPawnMidgamePenalty[f] / 2;
ev -= BackwardPawnEndgamePenalty[f] / 2;
}
}
if (chain)
{
mv += ChainMidgameBonus[f];
ev += ChainEndgameBonus[f];
}
if (candidate)
{
mv += CandidateMidgameBonus[relative_rank(us, s)];
ev += CandidateEndgameBonus[relative_rank(us, s)];
} }
}
if(chain) {
mv += ChainMidgameBonus[f];
ev += ChainEndgameBonus[f];
}
if(candidate) {
mv += CandidateMidgameBonus[relative_rank(us, s)];
ev += CandidateEndgameBonus[relative_rank(us, s)];
}
mgValue[us] += mv; mgValue[us] += mv;
egValue[us] += ev; egValue[us] += ev;
// If the pawn is passed, set the square of the pawn in the passedPawns // If the pawn is passed, set the square of the pawn in the passedPawns
// bitboard: // bitboard
if(passed) if (passed)
set_bit(&(pi->passedPawns), s); set_bit(&(pi->passedPawns), s);
} } // while(pawns)
} } // for(colors)
pi->mgValue = int16_t(mgValue[WHITE] - mgValue[BLACK]); pi->mgValue = int16_t(mgValue[WHITE] - mgValue[BLACK]);
pi->egValue = int16_t(egValue[WHITE] - egValue[BLACK]); pi->egValue = int16_t(egValue[WHITE] - egValue[BLACK]);
return pi; return pi;
} }
+14 -12
View File
@@ -25,8 +25,8 @@
//// Includes //// Includes
//// ////
#include "position.h" #include "bitboard.h"
#include "value.h"
//// ////
//// Types //// Types
@@ -38,6 +38,7 @@
/// to add further information in the future. A lookup to the pawn hash table /// to add further information in the future. A lookup to the pawn hash table
/// (performed by calling the get_pawn_info method in a PawnInfoTable object) /// (performed by calling the get_pawn_info method in a PawnInfoTable object)
/// returns a pointer to a PawnInfo object. /// returns a pointer to a PawnInfo object.
class Position;
class PawnInfo { class PawnInfo {
@@ -49,9 +50,9 @@ public:
Value kingside_storm_value(Color c) const; Value kingside_storm_value(Color c) const;
Value queenside_storm_value(Color c) const; Value queenside_storm_value(Color c) const;
Bitboard passed_pawns() const; Bitboard passed_pawns() const;
bool file_is_half_open(Color c, File f) const; int file_is_half_open(Color c, File f) const;
bool has_open_file_to_left(Color c, File f) const; int has_open_file_to_left(Color c, File f) const;
bool has_open_file_to_right(Color c, File f) const; int has_open_file_to_right(Color c, File f) const;
private: private:
void clear(); void clear();
@@ -59,7 +60,7 @@ private:
Key key; Key key;
Bitboard passedPawns; Bitboard passedPawns;
int16_t mgValue, egValue; int16_t mgValue, egValue;
int8_t ksStormValue[2], qsStormValue[2]; int16_t ksStormValue[2], qsStormValue[2];
uint8_t halfOpenFiles[2]; uint8_t halfOpenFiles[2];
}; };
@@ -75,11 +76,11 @@ public:
PawnInfoTable(unsigned numOfEntries); PawnInfoTable(unsigned numOfEntries);
~PawnInfoTable(); ~PawnInfoTable();
void clear(); void clear();
PawnInfo *get_pawn_info(const Position &pos); PawnInfo* get_pawn_info(const Position& pos);
private: private:
unsigned size; unsigned size;
PawnInfo *entries; PawnInfo* entries;
}; };
@@ -107,21 +108,22 @@ inline Value PawnInfo::queenside_storm_value(Color c) const {
return Value(qsStormValue[c]); return Value(qsStormValue[c]);
} }
inline bool PawnInfo::file_is_half_open(Color c, File f) const { inline int PawnInfo::file_is_half_open(Color c, File f) const {
return (halfOpenFiles[c] & (1 << int(f))); return (halfOpenFiles[c] & (1 << int(f)));
} }
inline bool PawnInfo::has_open_file_to_left(Color c, File f) const { inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
return halfOpenFiles[c] & ((1 << int(f)) - 1); return halfOpenFiles[c] & ((1 << int(f)) - 1);
} }
inline bool PawnInfo::has_open_file_to_right(Color c, File f) const { inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1); return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
} }
inline void PawnInfo::clear() { inline void PawnInfo::clear() {
mgValue = egValue = 0;
passedPawns = EmptyBoardBB; passedPawns = EmptyBoardBB;
mgValue = egValue = 0;
ksStormValue[WHITE] = ksStormValue[BLACK] = 0; ksStormValue[WHITE] = ksStormValue[BLACK] = 0;
qsStormValue[WHITE] = qsStormValue[BLACK] = 0; qsStormValue[WHITE] = qsStormValue[BLACK] = 0;
halfOpenFiles[WHITE] = halfOpenFiles[BLACK] = 0xFF; halfOpenFiles[WHITE] = halfOpenFiles[BLACK] = 0xFF;
-34
View File
@@ -1,34 +0,0 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008 Marco Costalba
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(PHASE_H_INCLUDED)
#define PHASE_H_INCLUDED
////
//// Types
////
enum Phase {
PHASE_ENDGAME = 0,
PHASE_MIDGAME = 128
};
#endif // !defined(PHASE_H_INCLUDED)
+11 -57
View File
@@ -22,74 +22,28 @@
//// Includes //// Includes
//// ////
#include <cstring> #include <string>
#include "piece.h" #include "piece.h"
using namespace std;
////
//// Constants and variables
////
const int SlidingArray[18] = {
0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0
};
const SquareDelta Directions[16][16] = {
{DELTA_ZERO},
{DELTA_NW, DELTA_NE, DELTA_ZERO},
{DELTA_SSW, DELTA_SSE, DELTA_SWW, DELTA_SEE,
DELTA_NWW, DELTA_NEE, DELTA_NNW, DELTA_NNE, DELTA_ZERO},
{DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
{DELTA_S, DELTA_E, DELTA_W, DELTA_N, DELTA_ZERO},
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
{DELTA_ZERO},
{DELTA_ZERO},
{DELTA_SW, DELTA_SE, DELTA_ZERO},
{DELTA_SSW, DELTA_SSE, DELTA_SWW, DELTA_SEE,
DELTA_NWW, DELTA_NEE, DELTA_NNW, DELTA_NNE, DELTA_ZERO},
{DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
{DELTA_S, DELTA_E, DELTA_W, DELTA_N, DELTA_ZERO},
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
};
const SquareDelta PawnPush[2] = {
DELTA_N, DELTA_S
};
//// ////
//// Functions //// Functions
//// ////
/// Translating piece types to/from English piece letters: /// Translating piece types to/from English piece letters
static const char PieceChars[] = " pnbrqk"; static const string PieceChars(" pnbrqk PNBRQK");
char piece_type_to_char(PieceType pt, bool upcase = false) { char piece_type_to_char(PieceType pt, bool upcase) {
return upcase? toupper(PieceChars[pt]) : PieceChars[pt];
return PieceChars[pt + upcase * 7];
} }
PieceType piece_type_from_char(char c) { PieceType piece_type_from_char(char c) {
const char *ch = strchr(PieceChars, tolower(c));
return ch? PieceType(ch - PieceChars) : NO_PIECE_TYPE; size_t idx = PieceChars.find(c);
}
return idx != string::npos ? PieceType(idx % 7) : NO_PIECE_TYPE;
/// piece_is_ok() and piece_type_is_ok(), for debugging:
bool piece_is_ok(Piece pc) {
return
piece_type_is_ok(type_of_piece(pc)) &&
color_is_ok(color_of_piece(pc));
}
bool piece_type_is_ok(PieceType pc) {
return pc >= PAWN && pc <= KING;
} }
+15 -37
View File
@@ -26,7 +26,6 @@
//// ////
#include "color.h" #include "color.h"
#include "misc.h"
#include "square.h" #include "square.h"
@@ -47,15 +46,12 @@ enum Piece {
//// ////
//// Constants and variables //// Constants
//// ////
const PieceType PieceTypeMin = PAWN; const int SlidingArray[18] = {
const PieceType PieceTypeMax = KING; 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0
};
extern const int SlidingArray[18];
extern const SquareDelta Directions[16][16];
extern const SquareDelta PawnPush[2];
//// ////
@@ -83,40 +79,24 @@ inline Piece piece_of_color_and_type(Color c, PieceType pt) {
return Piece((int(c) << 3) | int(pt)); return Piece((int(c) << 3) | int(pt));
} }
inline Piece pawn_of_color(Color c) {
return piece_of_color_and_type(c, PAWN);
}
inline Piece knight_of_color(Color c) {
return piece_of_color_and_type(c, KNIGHT);
}
inline Piece bishop_of_color(Color c) {
return piece_of_color_and_type(c, BISHOP);
}
inline Piece rook_of_color(Color c) {
return piece_of_color_and_type(c, ROOK);
}
inline Piece queen_of_color(Color c) {
return piece_of_color_and_type(c, QUEEN);
}
inline Piece king_of_color(Color c) {
return piece_of_color_and_type(c, KING);
}
inline int piece_is_slider(Piece p) { inline int piece_is_slider(Piece p) {
return SlidingArray[int(p)]; return SlidingArray[int(p)];
} }
inline int piece_type_is_slider(PieceType pt) { inline int piece_is_slider(PieceType pt) {
return SlidingArray[int(pt)]; return SlidingArray[int(pt)];
} }
inline SquareDelta pawn_push(Color c) { inline SquareDelta pawn_push(Color c) {
return PawnPush[c]; return (c == WHITE ? DELTA_N : DELTA_S);
}
inline bool piece_type_is_ok(PieceType pc) {
return pc >= PAWN && pc <= KING;
}
inline bool piece_is_ok(Piece pc) {
return piece_type_is_ok(type_of_piece(pc)) && color_is_ok(color_of_piece(pc));
} }
@@ -124,10 +104,8 @@ inline SquareDelta pawn_push(Color c) {
//// Prototypes //// Prototypes
//// ////
extern char piece_type_to_char(PieceType pt, bool upcase); extern char piece_type_to_char(PieceType pt, bool upcase = false);
extern PieceType piece_type_from_char(char c); extern PieceType piece_type_from_char(char c);
extern bool piece_is_ok(Piece pc);
extern bool piece_type_is_ok(PieceType pt);
#endif // !defined(PIECE_H_INCLUDED) #endif // !defined(PIECE_H_INCLUDED)
+532 -582
View File
File diff suppressed because it is too large Load Diff
+107 -78
View File
@@ -27,6 +27,10 @@
// Forcing value to bool 'true' or 'false' (performance warning) // Forcing value to bool 'true' or 'false' (performance warning)
#pragma warning(disable: 4800) #pragma warning(disable: 4800)
// Conditional expression is constant
#pragma warning(disable: 4127)
#endif #endif
//// ////
@@ -38,7 +42,6 @@
#include "direction.h" #include "direction.h"
#include "move.h" #include "move.h"
#include "piece.h" #include "piece.h"
#include "phase.h"
#include "square.h" #include "square.h"
#include "value.h" #include "value.h"
@@ -61,7 +64,7 @@ const int MaxGameLength = 220;
//// Types //// Types
//// ////
/// Castle rights, encoded as bit fields: /// Castle rights, encoded as bit fields
enum CastleRights { enum CastleRights {
NO_CASTLES = 0, NO_CASTLES = 0,
@@ -72,22 +75,28 @@ enum CastleRights {
ALL_CASTLES = 15 ALL_CASTLES = 15
}; };
/// Game phase
enum Phase {
PHASE_ENDGAME = 0,
PHASE_MIDGAME = 128
};
/// The UndoInfo struct stores information we need to restore a Position
/// object to its previous state when we retract a move. Whenever a move
/// is made on the board (by calling Position::do_move), an UndoInfo object
/// must be passed as a parameter. When the move is unmade (by calling
/// Position::undo_move), the same UndoInfo object must be passed again.
struct UndoInfo { /// The StateInfo struct stores information we need to restore a Position
int castleRights; /// object to its previous state when we retract a move. Whenever a move
Square epSquare; /// is made on the board (by calling Position::do_move), an StateInfo object
Bitboard checkersBB; /// must be passed as a parameter.
struct StateInfo {
Key key, pawnKey, materialKey; Key key, pawnKey, materialKey;
int rule50; int castleRights, rule50;
Move lastMove; Square epSquare;
PieceType capture;
Value mgValue, egValue; Value mgValue, egValue;
PieceType capture;
Bitboard checkersBB;
Move lastMove;
StateInfo* previous;
}; };
@@ -122,13 +131,13 @@ class Position {
public: public:
// Constructors // Constructors
Position() {}; Position() {};
Position(const Position &pos); Position(const Position& pos);
Position(const std::string &fen); Position(const std::string& fen);
// Text input/output // Text input/output
void from_fen(const std::string &fen); void from_fen(const std::string& fen);
const std::string to_fen() const; const std::string to_fen() const;
void print() const; void print(Move m = MOVE_NONE) const;
// Copying // Copying
void copy(const Position &pos); void copy(const Position &pos);
@@ -174,13 +183,13 @@ public:
// Number of pieces of each color and type // Number of pieces of each color and type
int piece_count(Color c, PieceType pt) const; int piece_count(Color c, PieceType pt) const;
// The en passant square: // The en passant square
Square ep_square() const; Square ep_square() const;
// Current king position for each color // Current king position for each color
Square king_square(Color c) const; Square king_square(Color c) const;
// Castling rights. // Castling rights
bool can_castle_kingside(Color c) const; bool can_castle_kingside(Color c) const;
bool can_castle_queenside(Color c) const; bool can_castle_queenside(Color c) const;
bool can_castle(Color c) const; bool can_castle(Color c) const;
@@ -197,6 +206,7 @@ public:
// Bitboards for pinned pieces and discovered check candidates // Bitboards for pinned pieces and discovered check candidates
Bitboard discovered_check_candidates(Color c) const; Bitboard discovered_check_candidates(Color c) const;
Bitboard pinned_pieces(Color c, Bitboard& p) const;
Bitboard pinned_pieces(Color c) const; Bitboard pinned_pieces(Color c) const;
// Checking pieces // Checking pieces
@@ -215,7 +225,7 @@ public:
template<PieceType> template<PieceType>
Bitboard piece_attacks_square(Square f, Square t) const; // Dispatch at compile-time Bitboard piece_attacks_square(Square f, Square t) const; // Dispatch at compile-time
bool piece_attacks_square(Square f, Square t) const; // Dispatch at run-time bool piece_attacks_square(Piece p, Square f, Square t) const; // Dispatch at run-time
// Properties of moves // Properties of moves
bool pl_move_is_legal(Move m) const; bool pl_move_is_legal(Move m) const;
@@ -242,17 +252,17 @@ public:
bool square_is_weak(Square s, Color c) const; bool square_is_weak(Square s, Color c) const;
// Doing and undoing moves // Doing and undoing moves
void backup(UndoInfo &u) const; void setStartState(const StateInfo& st);
void restore(const UndoInfo &u); void do_move(Move m, StateInfo& st);
void do_move(Move m, UndoInfo &u); void do_move(Move m, StateInfo& st, Bitboard dcCandidates);
void do_move(Move m, UndoInfo &u, Bitboard dcCandidates); void undo_move(Move m);
void undo_move(Move m, const UndoInfo &u); void do_null_move(StateInfo& st);
void do_null_move(UndoInfo &u); void undo_null_move();
void undo_null_move(const UndoInfo &u);
// Static exchange evaluation // Static exchange evaluation
int see(Square from, Square to) const; int see(Square from, Square to) const;
int see(Move m) const; int see(Move m) const;
int see(Square to) const;
// Accessing hash keys // Accessing hash keys
Key get_key() const; Key get_key() const;
@@ -267,7 +277,7 @@ public:
Value mg_pst_delta(Move m) const; Value mg_pst_delta(Move m) const;
// Game termination checks // Game termination checks
bool is_mate(); bool is_mate() const;
bool is_draw() const; bool is_draw() const;
// Check if one side threatens a mate in one // Check if one side threatens a mate in one
@@ -291,6 +301,7 @@ public:
static void init_piece_square_tables(); static void init_piece_square_tables();
private: private:
// Initialization helper functions (used while setting up a position) // Initialization helper functions (used while setting up a position)
void clear(); void clear();
void put_piece(Piece p, Square s); void put_piece(Piece p, Square s);
@@ -298,17 +309,20 @@ private:
void allow_ooo(Color c); void allow_ooo(Color c);
// Helper functions for doing and undoing moves // Helper functions for doing and undoing moves
void do_capture_move(Move m, PieceType capture, Color them, Square to); void do_capture_move(PieceType capture, Color them, Square to);
void do_castle_move(Move m); void do_castle_move(Move m);
void do_promotion_move(Move m, UndoInfo &u); void do_promotion_move(Move m);
void do_ep_move(Move m); void do_ep_move(Move m);
void undo_castle_move(Move m); void undo_castle_move(Move m);
void undo_promotion_move(Move m, const UndoInfo &u); void undo_promotion_move(Move m);
void undo_ep_move(Move m); void undo_ep_move(Move m);
void find_checkers(); void find_checkers();
template<PieceType Piece, bool FindPinned> template<PieceType Piece>
Bitboard hidden_checks(Color c, Square ksq) const; void update_checkers(Bitboard* pCheckersBB, Square ksq, Square from, Square to, Bitboard dcCandidates);
template<bool FindPinned>
Bitboard hidden_checkers(Color c) const;
// Computing hash keys from scratch (for initialization and debugging) // Computing hash keys from scratch (for initialization and debugging)
Key compute_key() const; Key compute_key() const;
@@ -316,15 +330,16 @@ private:
Key compute_material_key() const; Key compute_material_key() const;
// Computing incremental evaluation scores and material counts // Computing incremental evaluation scores and material counts
Value mg_pst(Color c, PieceType pt, Square s) const; enum GamePhase {
Value eg_pst(Color c, PieceType pt, Square s) const; MidGame,
Value compute_mg_value() const; EndGame
Value compute_eg_value() const; };
template<GamePhase> Value pst(Color c, PieceType pt, Square s) const;
template<GamePhase> Value compute_value() const;
Value compute_non_pawn_material(Color c) const; Value compute_non_pawn_material(Color c) const;
// Bitboards // Bitboards
Bitboard byColorBB[2], byTypeBB[8]; Bitboard byColorBB[2], byTypeBB[8];
Bitboard checkersBB;
// Board // Board
Piece board[64]; Piece board[64];
@@ -337,16 +352,14 @@ private:
int index[64]; int index[64];
// Other info // Other info
Color sideToMove;
int castleRights;
File initialKFile, initialKRFile, initialQRFile;
Square epSquare;
Square kingSquare[2]; Square kingSquare[2];
Move lastMove; Color sideToMove;
Key key, pawnKey, materialKey, history[MaxGameLength]; int gamePly;
int rule50, gamePly; Key history[MaxGameLength];
Value mgValue, egValue;
Value npMaterial[2]; Value npMaterial[2];
File initialKFile, initialKRFile, initialQRFile;
StateInfo startState;
StateInfo* st;
// Static variables // Static variables
static int castleRightsMask[64]; static int castleRightsMask[64];
@@ -497,7 +510,7 @@ inline Square Position::piece_list(Color c, PieceType pt, int index) const {
} }
inline Square Position::ep_square() const { inline Square Position::ep_square() const {
return epSquare; return st->epSquare;
} }
inline Square Position::king_square(Color c) const { inline Square Position::king_square(Color c) const {
@@ -505,11 +518,11 @@ inline Square Position::king_square(Color c) const {
} }
inline bool Position::can_castle_kingside(Color side) const { inline bool Position::can_castle_kingside(Color side) const {
return castleRights & (1+int(side)); return st->castleRights & (1+int(side));
} }
inline bool Position::can_castle_queenside(Color side) const { inline bool Position::can_castle_queenside(Color side) const {
return castleRights & (4+4*int(side)); return st->castleRights & (4+4*int(side));
} }
inline bool Position::can_castle(Color side) const { inline bool Position::can_castle(Color side) const {
@@ -525,7 +538,12 @@ inline Square Position::initial_qr_square(Color c) const {
} }
inline Bitboard Position::pawn_attacks(Color c, Square s) const { inline Bitboard Position::pawn_attacks(Color c, Square s) const {
return StepAttackBB[pawn_of_color(c)][s]; return StepAttackBB[piece_of_color_and_type(c, PAWN)][s];
}
template<>
inline Bitboard Position::piece_attacks<PAWN>(Square s) const {
return StepAttackBB[piece_of_color_and_type(opposite_color(sideToMove), PAWN)][s];
} }
template<> template<>
@@ -554,11 +572,11 @@ inline Bitboard Position::piece_attacks<KING>(Square s) const {
} }
inline Bitboard Position::checkers() const { inline Bitboard Position::checkers() const {
return checkersBB; return st->checkersBB;
} }
inline bool Position::is_check() const { inline bool Position::is_check() const {
return checkersBB != EmptyBoardBB; return st->checkersBB != EmptyBoardBB;
} }
inline bool Position::pawn_attacks_square(Color c, Square f, Square t) const { inline bool Position::pawn_attacks_square(Color c, Square f, Square t) const {
@@ -570,6 +588,16 @@ Bitboard Position::piece_attacks_square(Square f, Square t) const {
return bit_is_set(piece_attacks<Piece>(f), t); return bit_is_set(piece_attacks<Piece>(f), t);
} }
inline Bitboard Position::attacks_to(Square s, Color c) const {
return attacks_to(s) & pieces_of_color(c);
}
inline bool Position::square_is_attacked(Square s, Color c) const {
return attacks_to(s, c) != EmptyBoardBB;
}
inline bool Position::pawn_is_passed(Color c, Square s) const { inline bool Position::pawn_is_passed(Color c, Square s) const {
return !(pawns(opposite_color(c)) & passed_pawn_mask(c, s)); return !(pawns(opposite_color(c)) & passed_pawn_mask(c, s));
} }
@@ -595,19 +623,21 @@ inline bool Position::square_is_weak(Square s, Color c) const {
} }
inline Key Position::get_key() const { inline Key Position::get_key() const {
return key; return st->key;
} }
inline Key Position::get_pawn_key() const { inline Key Position::get_pawn_key() const {
return pawnKey; return st->pawnKey;
} }
inline Key Position::get_material_key() const { inline Key Position::get_material_key() const {
return materialKey; return st->materialKey;
} }
inline Value Position::mg_pst(Color c, PieceType pt, Square s) const { template<Position::GamePhase Phase>
return MgPieceSquareTable[piece_of_color_and_type(c, pt)][s]; inline Value Position::pst(Color c, PieceType pt, Square s) const {
return (Phase == MidGame ? MgPieceSquareTable[piece_of_color_and_type(c, pt)][s]
: EgPieceSquareTable[piece_of_color_and_type(c, pt)][s]);
} }
inline Value Position::mg_pst_delta(Move m) const { inline Value Position::mg_pst_delta(Move m) const {
@@ -615,16 +645,12 @@ inline Value Position::mg_pst_delta(Move m) const {
-MgPieceSquareTable[piece_on(move_from(m))][move_from(m)]; -MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
} }
inline Value Position::eg_pst(Color c, PieceType pt, Square s) const {
return EgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
}
inline Value Position::mg_value() const { inline Value Position::mg_value() const {
return mgValue; return st->mgValue;
} }
inline Value Position::eg_value() const { inline Value Position::eg_value() const {
return egValue; return st->egValue;
} }
inline Value Position::non_pawn_material(Color c) const { inline Value Position::non_pawn_material(Color c) const {
@@ -633,15 +659,9 @@ inline Value Position::non_pawn_material(Color c) const {
inline Phase Position::game_phase() const { inline Phase Position::game_phase() const {
// The purpose of the Value(325) terms below is to make sure the difference // Values modified by Joona Kiiski
// between MidgameLimit and EndgameLimit is a power of 2, which should make static const Value MidgameLimit = Value(15713);
// the division at the end of the function a bit faster. static const Value EndgameLimit = Value(4428);
static const Value MidgameLimit = 2 * QueenValueMidgame
+ 2 * RookValueMidgame
+ 6 * BishopValueMidgame
+ Value(325);
static const Value EndgameLimit = 4 * RookValueMidgame - Value(325);
Value npm = non_pawn_material(WHITE) + non_pawn_material(BLACK); Value npm = non_pawn_material(WHITE) + non_pawn_material(BLACK);
@@ -656,34 +676,34 @@ inline Phase Position::game_phase() const {
inline bool Position::move_is_deep_pawn_push(Move m) const { inline bool Position::move_is_deep_pawn_push(Move m) const {
Color c = side_to_move(); Color c = side_to_move();
return piece_on(move_from(m)) == pawn_of_color(c) return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
&& relative_rank(c, move_to(m)) > RANK_4; && relative_rank(c, move_to(m)) > RANK_4;
} }
inline bool Position::move_is_pawn_push_to_7th(Move m) const { inline bool Position::move_is_pawn_push_to_7th(Move m) const {
Color c = side_to_move(); Color c = side_to_move();
return piece_on(move_from(m)) == pawn_of_color(c) return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
&& relative_rank(c, move_to(m)) == RANK_7; && relative_rank(c, move_to(m)) == RANK_7;
} }
inline bool Position::move_is_passed_pawn_push(Move m) const { inline bool Position::move_is_passed_pawn_push(Move m) const {
Color c = side_to_move(); Color c = side_to_move();
return piece_on(move_from(m)) == pawn_of_color(c) return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
&& pawn_is_passed(c, move_to(m)); && pawn_is_passed(c, move_to(m));
} }
inline bool Position::move_was_passed_pawn_push(Move m) const { inline bool Position::move_was_passed_pawn_push(Move m) const {
Color c = opposite_color(side_to_move()); Color c = opposite_color(side_to_move());
return piece_on(move_to(m)) == pawn_of_color(c) return piece_on(move_to(m)) == piece_of_color_and_type(c, PAWN)
&& pawn_is_passed(c, move_to(m)); && pawn_is_passed(c, move_to(m));
} }
inline int Position::rule_50_counter() const { inline int Position::rule_50_counter() const {
return rule50; return st->rule50;
} }
inline bool Position::opposite_colored_bishops() const { inline bool Position::opposite_colored_bishops() const {
@@ -698,5 +718,14 @@ inline bool Position::has_pawn_on_7th(Color c) const {
return pawns(c) & relative_rank_bb(c, RANK_7); return pawns(c) & relative_rank_bb(c, RANK_7);
} }
inline bool Position::move_is_capture(Move m) const {
// Move must not be MOVE_NONE !
return ( !square_is_empty(move_to(m))
&& (color_of_piece_on(move_to(m)) != color_of_piece_on(move_from(m)))
)
|| move_is_ep(m);
}
#endif // !defined(POSITION_H_INCLUDED) #endif // !defined(POSITION_H_INCLUDED)
-1
View File
@@ -25,7 +25,6 @@
//// Includes //// Includes
//// ////
#include "position.h"
#include "value.h" #include "value.h"
+262 -245
View File
@@ -31,6 +31,7 @@
#include "movepick.h" #include "movepick.h"
#include "san.h" #include "san.h"
extern SearchStack EmptySearchStack;
//// ////
//// Local definitions //// Local definitions
@@ -50,7 +51,7 @@ namespace {
/// Functions /// Functions
Ambiguity move_ambiguity(Position &pos, Move m); Ambiguity move_ambiguity(const Position& pos, Move m);
const std::string time_string(int milliseconds); const std::string time_string(int milliseconds);
const std::string score_string(Value v); const std::string score_string(Value v);
} }
@@ -61,206 +62,227 @@ namespace {
//// ////
/// move_to_san() takes a position and a move as input, where it is assumed /// move_to_san() takes a position and a move as input, where it is assumed
/// that the move is a legal move from the position. The return value is /// that the move is a legal move from the position. The return value is
/// a string containing the move in short algebraic notation. /// a string containing the move in short algebraic notation.
const std::string move_to_san(Position &pos, Move m) { const std::string move_to_san(const Position& pos, Move m) {
std::string str;
assert(pos.is_ok()); assert(pos.is_ok());
assert(move_is_ok(m)); assert(move_is_ok(m));
if(m == MOVE_NONE) { Square from, to;
str = "(none)"; PieceType pt;
return str;
}
else if(m == MOVE_NULL) {
str = "(null)";
return str;
}
else if(move_is_long_castle(m))
str = "O-O-O";
else if(move_is_short_castle(m))
str = "O-O";
else {
Square from, to;
Piece pc;
from = move_from(m); from = move_from(m);
to = move_to(m); to = move_to(m);
pc = pos.piece_on(move_from(m)); pt = type_of_piece(pos.piece_on(move_from(m)));
str = ""; std::string san = "";
if(type_of_piece(pc) == PAWN) { if (m == MOVE_NONE)
if(pos.move_is_capture(m)) return "(none)";
str += file_to_char(square_file(move_from(m))); else if (m == MOVE_NULL)
} return "(null)";
else { else if (move_is_long_castle(m) || (int(to - from) == -2 && pt == KING))
str += piece_type_to_char(type_of_piece(pc), true); san = "O-O-O";
else if (move_is_short_castle(m) || (int(to - from) == 2 && pt == KING))
Ambiguity amb = move_ambiguity(pos, m); san = "O-O";
switch(amb) { else
{
case AMBIGUITY_NONE: if (pt != PAWN)
break; {
san += piece_type_to_char(pt, true);
case AMBIGUITY_FILE: switch (move_ambiguity(pos, m)) {
str += file_to_char(square_file(from)); case AMBIGUITY_NONE:
break; break;
case AMBIGUITY_FILE:
case AMBIGUITY_RANK: san += file_to_char(square_file(from));
str += rank_to_char(square_rank(from)); break;
break; case AMBIGUITY_RANK:
san += rank_to_char(square_rank(from));
case AMBIGUITY_BOTH: break;
str += square_to_string(from); case AMBIGUITY_BOTH:
break; san += square_to_string(from);
break;
default: default:
assert(false); assert(false);
}
}
if (pos.move_is_capture(m))
{
if (pt == PAWN)
san += file_to_char(square_file(move_from(m)));
san += "x";
}
san += square_to_string(move_to(m));
if (move_promotion(m))
{
san += '=';
san += piece_type_to_char(move_promotion(m), true);
} }
}
if(pos.move_is_capture(m))
str += "x";
str += square_to_string(move_to(m));
if(move_promotion(m)) {
str += "=";
str += piece_type_to_char(move_promotion(m), true);
}
} }
// Is the move check? We don't use pos.move_is_check(m) here, because // Is the move check? We don't use pos.move_is_check(m) here, because
// Position::move_is_check doesn't detect all checks (not castling moves, // Position::move_is_check doesn't detect all checks (not castling moves,
// promotions and en passant captures). // promotions and en passant captures).
UndoInfo u; StateInfo st;
pos.do_move(m, u); Position p(pos);
if(pos.is_check()) p.do_move(m, st);
str += pos.is_mate()? "#" : "+"; if (p.is_check())
pos.undo_move(m, u); san += p.is_mate()? "#" : "+";
return str; return san;
} }
/// move_from_san() takes a position and a string as input, and tries to /// move_from_san() takes a position and a string as input, and tries to
/// interpret the string as a move in short algebraic notation. On success, /// interpret the string as a move in short algebraic notation. On success,
/// the move is returned. On failure (i.e. if the string is unparsable, or /// the move is returned. On failure (i.e. if the string is unparsable, or
/// if the move is illegal or ambiguous), MOVE_NONE is returned. /// if the move is illegal or ambiguous), MOVE_NONE is returned.
Move move_from_san(Position &pos, const std::string &movestr) { Move move_from_san(const Position& pos, const std::string& movestr) {
assert(pos.is_ok()); assert(pos.is_ok());
MovePicker mp = MovePicker(pos, false, MOVE_NONE, MOVE_NONE, MOVE_NONE, MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
MOVE_NONE, OnePly);
// Castling moves // Castling moves
if(movestr == "O-O-O") { if (movestr == "O-O-O" || movestr == "O-O-O+")
Move m; {
while((m = mp.get_next_move()) != MOVE_NONE) Move m;
if(move_is_long_castle(m) && pos.pl_move_is_legal(m)) while ((m = mp.get_next_move()) != MOVE_NONE)
return m; if (move_is_long_castle(m) && pos.pl_move_is_legal(m))
return MOVE_NONE; return m;
}
else if(movestr == "O-O") {
Move m;
while((m = mp.get_next_move()) != MOVE_NONE)
if(move_is_short_castle(m) && pos.pl_move_is_legal(m))
return m;
return MOVE_NONE;
}
// Normal moves
const char *cstr = movestr.c_str();
const char *c;
char *cc;
char str[10];
int i;
// Initialize str[] by making a copy of movestr with the characters
// 'x', '=', '+' and '#' removed.
cc = str;
for(i=0, c=cstr; i<10 && *c!='\0' && *c!='\n' && *c!=' '; i++, c++)
if(!strchr("x=+#", *c)) {
*cc = strchr("nrq", *c)? toupper(*c) : *c;
cc++;
}
*cc = '\0';
size_t left = 0, right = strlen(str) - 1;
PieceType pt = NO_PIECE_TYPE, promotion;
Square to;
File fromFile = FILE_NONE;
Rank fromRank = RANK_NONE;
// Promotion?
if(strchr("BNRQ", str[right])) {
promotion = piece_type_from_char(str[right]);
right--;
}
else
promotion = NO_PIECE_TYPE;
// Find the moving piece:
if(left < right) {
if(strchr("BNRQK", str[left])) {
pt = piece_type_from_char(str[left]);
left++;
}
else
pt = PAWN;
}
// Find the to square:
if(left < right) {
if(str[right] < '1' || str[right] > '8' ||
str[right-1] < 'a' || str[right-1] > 'h')
return MOVE_NONE; return MOVE_NONE;
to = make_square(file_from_char(str[right-1]), rank_from_char(str[right]));
right -= 2;
} }
else else if (movestr == "O-O" || movestr == "O-O+")
{
Move m;
while ((m = mp.get_next_move()) != MOVE_NONE)
if (move_is_short_castle(m) && pos.pl_move_is_legal(m))
return m;
return MOVE_NONE; return MOVE_NONE;
// Find the file and/or rank of the from square:
if(left <= right) {
if(strchr("abcdefgh", str[left])) {
fromFile = file_from_char(str[left]);
left++;
}
if(strchr("12345678", str[left]))
fromRank = rank_from_char(str[left]);
} }
// Look for a matching move: // Normal moves. We use a simple FSM to parse the san string.
enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
static const std::string pieceLetters = "KQRBN";
PieceType pt = NO_PIECE_TYPE, promotion = NO_PIECE_TYPE;
File fromFile = FILE_NONE, toFile = FILE_NONE;
Rank fromRank = RANK_NONE, toRank = RANK_NONE;
Square to;
int state = START;
for (size_t i = 0; i < movestr.length(); i++)
{
char type, c = movestr[i];
if (pieceLetters.find(c) != std::string::npos)
type = 'P';
else if (c >= 'a' && c <= 'h')
type = 'F';
else if (c >= '1' && c <= '8')
type = 'R';
else
type = c;
switch (type) {
case 'P':
if (state == START)
{
pt = piece_type_from_char(c);
state = TO_FILE;
}
else if (state == PROMOTION)
{
promotion = piece_type_from_char(c);
state = (i < movestr.length() - 1) ? CHECK : END;
}
else
return MOVE_NONE;
break;
case 'F':
if (state == START)
{
pt = PAWN;
fromFile = toFile = file_from_char(c);
state = TO_RANK;
}
else if (state == TO_FILE)
{
toFile = file_from_char(c);
state = TO_RANK;
}
else if (state == TO_RANK && toFile != FILE_NONE)
{
// Previous file was for disambiguation
fromFile = toFile;
toFile = file_from_char(c);
}
else
return MOVE_NONE;
break;
case 'R':
if (state == TO_RANK)
{
toRank = rank_from_char(c);
state = (i < movestr.length() - 1) ? PROMOTION_OR_CHECK : END;
}
else if (state == TO_FILE && fromRank == RANK_NONE)
{
// It's a disambiguation rank instead of a file
fromRank = rank_from_char(c);
}
else
return MOVE_NONE;
break;
case 'x': case 'X':
if (state == TO_RANK)
{
// Previous file was for disambiguation, or it's a pawn capture
fromFile = toFile;
state = TO_FILE;
}
else if (state != TO_FILE)
return MOVE_NONE;
break;
case '=':
if (state == PROMOTION_OR_CHECK)
state = PROMOTION;
else
return MOVE_NONE;
break;
case '+': case '#':
if (state == PROMOTION_OR_CHECK || state == CHECK)
state = END;
else
return MOVE_NONE;
break;
default:
return MOVE_NONE;
break;
}
}
if (state != END)
return MOVE_NONE;
// Look for a matching move
Move m, move = MOVE_NONE; Move m, move = MOVE_NONE;
to = make_square(toFile, toRank);
int matches = 0; int matches = 0;
while((m = mp.get_next_move()) != MOVE_NONE) { while ((m = mp.get_next_move()) != MOVE_NONE)
bool match = true; if ( pos.type_of_piece_on(move_from(m)) == pt
if(pos.type_of_piece_on(move_from(m)) != pt) && move_to(m) == to
match = false; && move_promotion(m) == promotion
else if(move_to(m) != to) && (fromFile == FILE_NONE || fromFile == square_file(move_from(m)))
match = false; && (fromRank == RANK_NONE || fromRank == square_rank(move_from(m))))
else if(move_promotion(m) != promotion) {
match = false; move = m;
else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m))) matches++;
match = false; }
else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m))) return (matches == 1 ? move : MOVE_NONE);
match = false;
if(match) {
move = m;
matches++;
}
}
if(matches == 1)
return move;
else
return MOVE_NONE;
} }
@@ -271,34 +293,31 @@ Move move_from_san(Position &pos, const std::string &movestr) {
/// length of 80 characters. After a line break, 'startColumn' spaces are /// length of 80 characters. After a line break, 'startColumn' spaces are
/// inserted at the beginning of the new line. /// inserted at the beginning of the new line.
const std::string line_to_san(const Position &pos, Move line[], int startColumn, const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
bool breakLines) {
Position p = Position(pos); StateInfo st;
UndoInfo u;
std::stringstream s; std::stringstream s;
std::string moveStr; std::string moveStr;
size_t length, maxLength; size_t length = 0;
size_t maxLength = 80 - startColumn;
Position p(pos);
length = 0; for (int i = 0; line[i] != MOVE_NONE; i++)
maxLength = 80 - startColumn; {
moveStr = move_to_san(p, line[i]);
length += moveStr.length() + 1;
if (breakLines && length > maxLength)
{
s << '\n' << std::setw(startColumn) << ' ';
length = moveStr.length() + 1;
}
s << moveStr << ' ';
for(int i = 0; line[i] != MOVE_NONE; i++) { if (line[i] == MOVE_NULL)
moveStr = move_to_san(p, line[i]); p.do_null_move(st);
length += moveStr.length() + 1; else
if(breakLines && length > maxLength) { p.do_move(line[i], st);
s << "\n";
for(int j = 0; j < startColumn; j++)
s << " ";
length = moveStr.length() + 1;
}
s << moveStr << " ";
if(line[i] == MOVE_NULL)
p.do_null_move(u);
else
p.do_move(line[i], u);
} }
return s.str(); return s.str();
} }
@@ -307,26 +326,26 @@ const std::string line_to_san(const Position &pos, Move line[], int startColumn,
/// It is used to write search information to the log file (which is created /// It is used to write search information to the log file (which is created
/// when the UCI parameter "Use Search Log" is "true"). /// when the UCI parameter "Use Search Log" is "true").
const std::string pretty_pv(const Position &pos, int time, int depth, const std::string pretty_pv(const Position& pos, int time, int depth,
uint64_t nodes, Value score, Move pv[]) { uint64_t nodes, Value score, Move pv[]) {
std::stringstream s; std::stringstream s;
// Depth // Depth
s << std::setw(2) << std::setfill(' ') << depth << " "; s << std::setw(2) << depth << " ";
// Score // Score
s << std::setw(8) << score_string(score); s << std::setw(8) << score_string(score);
// Time // Time
s << std::setw(8) << std::setfill(' ') << time_string(time) << " "; s << std::setw(8) << time_string(time) << " ";
// Nodes // Nodes
if(nodes < 1000000ULL) if (nodes < 1000000ULL)
s << std::setw(8) << std::setfill(' ') << nodes << " "; s << std::setw(8) << nodes << " ";
else if(nodes < 1000000000ULL) else if (nodes < 1000000000ULL)
s << std::setw(7) << std::setfill(' ') << nodes/1000ULL << 'k' << " "; s << std::setw(7) << nodes/1000ULL << 'k' << " ";
else else
s << std::setw(7) << std::setfill(' ') << nodes/1000000ULL << 'M' << " "; s << std::setw(7) << nodes/1000000ULL << 'M' << " ";
// PV // PV
s << line_to_san(pos, pv, 30, true); s << line_to_san(pos, pv, 30, true);
@@ -337,82 +356,80 @@ const std::string pretty_pv(const Position &pos, int time, int depth,
namespace { namespace {
Ambiguity move_ambiguity(Position &pos, Move m) { Ambiguity move_ambiguity(const Position& pos, Move m) {
Square from, to;
Piece pc;
from = move_from(m); Square from = move_from(m);
to = move_to(m); Square to = move_to(m);
pc = pos.piece_on(from); Piece pc = pos.piece_on(from);
// King moves are never ambiguous, because there is never two kings of // King moves are never ambiguous, because there is never two kings of
// the same color. // the same color.
if(type_of_piece(pc) == KING) if (type_of_piece(pc) == KING)
return AMBIGUITY_NONE; return AMBIGUITY_NONE;
MovePicker mp = MovePicker(pos, false, MOVE_NONE, MOVE_NONE, MOVE_NONE, MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
MOVE_NONE, OnePly);
Move mv, moveList[8]; Move mv, moveList[8];
int i, j, n;
n = 0; int n = 0;
while((mv = mp.get_next_move()) != MOVE_NONE) while ((mv = mp.get_next_move()) != MOVE_NONE)
if(move_to(mv) == to && pos.piece_on(move_from(mv)) == pc if (move_to(mv) == to && pos.piece_on(move_from(mv)) == pc && pos.pl_move_is_legal(mv))
&& pos.pl_move_is_legal(mv)) moveList[n++] = mv;
moveList[n++] = mv;
if(n == 1)
return AMBIGUITY_NONE;
j = 0; if (n == 1)
for(i = 0; i < n; i++) return AMBIGUITY_NONE;
if(square_file(move_from(moveList[i])) == square_file(from))
j++;
if(j == 1)
return AMBIGUITY_FILE;
j = 0; int f = 0, r = 0;
for(i = 0; i < n; i++) for (int i = 0; i < n; i++)
if(square_rank(move_from(moveList[i])) == square_rank(from)) {
j++; if (square_file(move_from(moveList[i])) == square_file(from))
if(j == 1) f++;
return AMBIGUITY_RANK;
if (square_rank(move_from(moveList[i])) == square_rank(from))
r++;
}
if (f == 1)
return AMBIGUITY_FILE;
if (r == 1)
return AMBIGUITY_RANK;
return AMBIGUITY_BOTH; return AMBIGUITY_BOTH;
} }
const std::string time_string(int milliseconds) { const std::string time_string(int milliseconds) {
std::stringstream s; std::stringstream s;
s << std::setfill('0');
int hours = milliseconds / (1000 * 60 * 60); int hours = milliseconds / (1000*60*60);
int minutes = (milliseconds - hours*1000*60*60) / (60*1000); int minutes = (milliseconds - hours*1000*60*60) / (1000*60);
int seconds = (milliseconds - hours*1000*60*60 - minutes*60*1000) / 1000; int seconds = (milliseconds - hours*1000*60*60 - minutes*1000*60) / 1000;
if(hours) if (hours)
s << hours << ':'; s << hours << ':';
s << std::setw(2) << std::setfill('0') << minutes << ':';
s << std::setw(2) << std::setfill('0') << seconds;
s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
return s.str(); return s.str();
} }
const std::string score_string(Value v) { const std::string score_string(Value v) {
std::stringstream s; std::stringstream s;
if(abs(v) >= VALUE_MATE - 200) { if (v >= VALUE_MATE - 200)
if(v < 0)
s << "-#" << (VALUE_MATE + v) / 2;
else
s << "#" << (VALUE_MATE - v + 1) / 2; s << "#" << (VALUE_MATE - v + 1) / 2;
} else if(v <= -VALUE_MATE + 200)
else { s << "-#" << (VALUE_MATE + v) / 2;
float floatScore = float(v) / float(PawnValueMidgame); else
if(v >= 0) {
s << '+'; float floatScore = float(v) / float(PawnValueMidgame);
s << std::setprecision(2) << std::fixed << floatScore; if (v >= 0)
s << '+';
s << std::setprecision(2) << std::fixed << floatScore;
} }
return s.str(); return s.str();
} }
} }
+4 -7
View File
@@ -36,12 +36,9 @@
//// Prototypes //// Prototypes
//// ////
extern const std::string move_to_san(Position &pos, Move m); extern const std::string move_to_san(const Position& pos, Move m);
extern Move move_from_san(Position &pos, const std::string &str); extern Move move_from_san(const Position& pos, const std::string& str);
extern const std::string line_to_san(const Position &pos, Move line[], extern const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines);
int startColumn, bool breakLines); extern const std::string pretty_pv(const Position& pos, int time, int depth, uint64_t nodes, Value score, Move pv[]);
extern const std::string pretty_pv(const Position &pos, int time, int depth,
uint64_t nodes, Value score, Move pv[]);
#endif // !defined(SAN_H_INCLUDED) #endif // !defined(SAN_H_INCLUDED)
+566 -319
View File
File diff suppressed because it is too large Load Diff
+7 -5
View File
@@ -25,7 +25,6 @@
//// Includes //// Includes
//// ////
#include "types.h"
#include "depth.h" #include "depth.h"
#include "history.h" #include "history.h"
#include "lock.h" #include "lock.h"
@@ -41,6 +40,7 @@
const int PLY_MAX = 100; const int PLY_MAX = 100;
const int PLY_MAX_PLUS_2 = 102; const int PLY_MAX_PLUS_2 = 102;
const int KILLER_MAX = 2;
//// ////
@@ -55,10 +55,13 @@ const int PLY_MAX_PLUS_2 = 102;
struct SearchStack { struct SearchStack {
Move pv[PLY_MAX]; Move pv[PLY_MAX];
Move currentMove; Move currentMove;
Value currentMoveCaptureValue; Move mateKiller;
Move mateKiller, killer1, killer2;
Move threatMove; Move threatMove;
Move killers[KILLER_MAX];
Depth reduction; Depth reduction;
void init(int ply);
void initKillers();
}; };
@@ -66,10 +69,9 @@ struct SearchStack {
//// Global variables //// Global variables
//// ////
extern SearchStack EmptySearchStack;
extern TranspositionTable TT; extern TranspositionTable TT;
extern int ActiveThreads; extern int ActiveThreads;
extern Lock SMPLock; extern Lock SMPLock;
// Perhaps better to make H local, and pass as parameter to MovePicker? // Perhaps better to make H local, and pass as parameter to MovePicker?
-85
View File
@@ -1,85 +0,0 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008 Marco Costalba
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////
//// Includes
////
#include <cassert>
#include <string>
#include "square.h"
////
//// Functions
////
/// Translating files, ranks and squares to/from characters and strings:
File file_from_char(char c) {
return File(c - 'a') + FILE_A;
}
char file_to_char(File f) {
return char(f - FILE_A) + 'a';
}
Rank rank_from_char(char c) {
return Rank(c - '1') + RANK_1;
}
char rank_to_char(Rank r) {
return char(r - RANK_1) + '1';
}
Square square_from_string(const std::string &str) {
return make_square(file_from_char(str[0]), rank_from_char(str[1]));
}
const std::string square_to_string(Square s) {
std::string str;
str += file_to_char(square_file(s));
str += rank_to_char(square_rank(s));
return str;
}
/// file_is_ok(), rank_is_ok() and square_is_ok(), for debugging:
bool file_is_ok(File f) {
return f >= FILE_A && f <= FILE_H;
}
bool rank_is_ok(Rank r) {
return r >= RANK_1 && r <= RANK_8;
}
bool square_is_ok(Square s) {
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
}
+34 -12
View File
@@ -159,21 +159,43 @@ inline int square_distance(Square s1, Square s2) {
return Max(file_distance(s1, s2), rank_distance(s1, s2)); return Max(file_distance(s1, s2), rank_distance(s1, s2));
} }
inline File file_from_char(char c) {
return File(c - 'a') + FILE_A;
}
//// inline char file_to_char(File f) {
//// Prototypes return char(f - FILE_A + int('a'));
//// }
extern File file_from_char(char c); inline Rank rank_from_char(char c) {
extern char file_to_char(File f); return Rank(c - '1') + RANK_1;
extern Rank rank_from_char(char c); }
extern char rank_to_char(Rank r);
extern Square square_from_string(const std::string &str);
extern const std::string square_to_string(Square s);
extern bool file_is_ok(File f); inline char rank_to_char(Rank r) {
extern bool rank_is_ok(Rank r); return char(r - RANK_1 + int('1'));
extern bool square_is_ok(Square s); }
inline Square square_from_string(const std::string& str) {
return make_square(file_from_char(str[0]), rank_from_char(str[1]));
}
inline const std::string square_to_string(Square s) {
std::string str;
str += file_to_char(square_file(s));
str += rank_to_char(square_rank(s));
return str;
}
inline bool file_is_ok(File f) {
return f >= FILE_A && f <= FILE_H;
}
inline bool rank_is_ok(Rank r) {
return r >= RANK_1 && r <= RANK_8;
}
inline bool square_is_ok(Square s) {
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
}
#endif // !defined(SQUARE_H_INCLUDED) #endif // !defined(SQUARE_H_INCLUDED)
-28
View File
@@ -1,28 +0,0 @@
/*
(c) Copyright 1992 Eric Backus
This software may be used freely so long as this copyright notice is
left intact. There is no warrantee on this software.
*/
#include <windows.h>
#include <time.h>
#include "dos.h"
int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
SYSTEMTIME systime;
if (tp) {
struct tm tmrec;
time_t theTime = time(NULL);
tmrec = *localtime(&theTime);
tp->tv_sec = mktime(&tmrec);
GetLocalTime(&systime); /* system time */
tp->tv_usec = systime.wMilliseconds * 1000;
}
return 0;
}
+59 -80
View File
@@ -33,20 +33,13 @@
//// Functions //// Functions
//// ////
/// Constructor TranspositionTable::TranspositionTable() {
TranspositionTable::TranspositionTable(unsigned mbSize) { size = writes = 0;
size = 0;
generation = 0;
writes = 0;
entries = 0; entries = 0;
set_size(mbSize); generation = 0;
} }
/// Destructor
TranspositionTable::~TranspositionTable() { TranspositionTable::~TranspositionTable() {
delete [] entries; delete [] entries;
@@ -58,33 +51,33 @@ TranspositionTable::~TranspositionTable() {
void TranspositionTable::set_size(unsigned mbSize) { void TranspositionTable::set_size(unsigned mbSize) {
assert(mbSize >= 4 && mbSize <= 1024); assert(mbSize >= 4 && mbSize <= 4096);
unsigned newSize = 1024; unsigned newSize = 1024;
// We store a cluster of 4 TTEntry for each position and newSize is // We store a cluster of 4 TTEntry for each position and newSize is
// the maximum number of storable positions // the maximum number of storable positions
for ( ; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20); newSize *= 2); while ((2 * newSize) * 4 * (sizeof(TTEntry)) <= (mbSize << 20))
newSize /= 2; newSize *= 2;
if (newSize != size) if (newSize != size)
{ {
size = newSize; size = newSize;
delete [] entries; delete [] entries;
entries = new TTEntry[size * 4]; entries = new TTEntry[size * 4];
if (!entries) if (!entries)
{ {
std::cerr << "Failed to allocate " << mbSize std::cerr << "Failed to allocate " << mbSize
<< " MB for transposition table." << " MB for transposition table." << std::endl;
<< std::endl; exit(EXIT_FAILURE);
exit(EXIT_FAILURE); }
} clear();
clear();
} }
} }
/// TranspositionTable::clear overwrites the entire transposition table /// TranspositionTable::clear overwrites the entire transposition table
/// with zeroes. It is called whenever the table is resized, or when the /// with zeroes. It is called whenever the table is resized, or when the
/// user asks the program to clear the table (from the UCI interface). /// user asks the program to clear the table (from the UCI interface).
/// Perhaps we should also clear it when the "ucinewgame" command is recieved? /// Perhaps we should also clear it when the "ucinewgame" command is recieved?
@@ -96,43 +89,44 @@ void TranspositionTable::clear() {
/// TranspositionTable::store writes a new entry containing a position, /// TranspositionTable::store writes a new entry containing a position,
/// a value, a value type, a search depth, and a best move to the /// a value, a value type, a search depth, and a best move to the
/// transposition table. The transposition table is organized in clusters /// transposition table. Transposition table is organized in clusters of
/// of four TTEntry objects, and when a new entry is written, it replaces /// four TTEntry objects, and when a new entry is written, it replaces
/// the least valuable of the four entries in a cluster. A TTEntry t1 is /// the least valuable of the four entries in a cluster. A TTEntry t1 is
/// considered to be more valuable than a TTEntry t2 if t1 is from the /// considered to be more valuable than a TTEntry t2 if t1 is from the
/// current search and t2 is from a previous search, or if the depth of t1 /// current search and t2 is from a previous search, or if the depth of t1
/// is bigger than the depth of t2. /// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
/// never replaces another entry for the same position.
void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d, Move m) {
void TranspositionTable::store(const Position &pos, Value v, Depth d,
Move m, ValueType type) {
TTEntry *tte, *replace; TTEntry *tte, *replace;
tte = replace = first_entry(pos); tte = replace = first_entry(p);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++, tte++)
{ {
if (!(tte+i)->key()) // still empty if (!tte->key() || tte->key() == p.get_key()) // empty or overwrite old
{ {
*(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation); // Do not overwrite when new type is VALUE_TYPE_EVAL
writes++; if (tte->key() && t == VALUE_TYPE_EVAL)
return; return;
}
if ((tte+i)->key() == pos.get_key()) // overwrite old
{
if (m == MOVE_NONE)
m = (tte+i)->move();
*(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation); if (m == MOVE_NONE)
return; m = tte->move();
}
if ( i == 0 // already is (replace == tte+i), common case
|| replace->generation() < (tte+i)->generation())
continue;
if ( replace->generation() > (tte+i)->generation() *tte = TTEntry(p.get_key(), v, t, d, m, generation);
|| (tte+i)->depth() < replace->depth()) return;
replace = tte+i; }
else if (i == 0) // replace would be a no-op in this common case
continue;
int c1 = (replace->generation() == generation ? 2 : 0);
int c2 = (tte->generation() == generation ? -2 : 0);
int c3 = (tte->depth() < replace->depth() ? 1 : 0);
if (c1 + c2 + c3 > 0)
replace = tte;
} }
*replace = TTEntry(pos.get_key(), v, type, d, m, generation); *replace = TTEntry(p.get_key(), v, t, d, m, generation);
writes++; writes++;
} }
@@ -141,15 +135,14 @@ void TranspositionTable::store(const Position &pos, Value v, Depth d,
/// transposition table. Returns a pointer to the TTEntry or NULL /// transposition table. Returns a pointer to the TTEntry or NULL
/// if position is not found. /// if position is not found.
const TTEntry* TranspositionTable::retrieve(const Position &pos) const { TTEntry* TranspositionTable::retrieve(const Position& pos) const {
TTEntry *tte = first_entry(pos); TTEntry *tte = first_entry(pos);
for (int i = 0; i < 4; i++, tte++) for (int i = 0; i < 4; i++, tte++)
{
if (tte->key() == pos.get_key()) if (tte->key() == pos.get_key())
return tte; return tte;
}
return NULL; return NULL;
} }
@@ -157,13 +150,13 @@ const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
/// TranspositionTable::first_entry returns a pointer to the first /// TranspositionTable::first_entry returns a pointer to the first
/// entry of a cluster given a position. /// entry of a cluster given a position.
inline TTEntry* TranspositionTable::first_entry(const Position &pos) const { inline TTEntry* TranspositionTable::first_entry(const Position& pos) const {
return entries + (int(pos.get_key() & (size - 1)) << 2); return entries + (int(pos.get_key() & (size - 1)) << 2);
} }
/// TranspositionTable::new_search() is called at the beginning of every new /// TranspositionTable::new_search() is called at the beginning of every new
/// search. It increments the "generation" variable, which is used to /// search. It increments the "generation" variable, which is used to
/// distinguish transposition table entries from previous searches from /// distinguish transposition table entries from previous searches from
/// entries from the current search. /// entries from the current search.
@@ -175,19 +168,19 @@ void TranspositionTable::new_search() {
/// TranspositionTable::insert_pv() is called at the end of a search /// TranspositionTable::insert_pv() is called at the end of a search
/// iteration, and inserts the PV back into the PV. This makes sure the /// iteration, and inserts the PV back into the PV. This makes sure
/// old PV moves are searched first, even if the old TT entries have been /// the old PV moves are searched first, even if the old TT entries
/// overwritten. /// have been overwritten.
void TranspositionTable::insert_pv(const Position &pos, Move pv[]) { void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
UndoInfo u; StateInfo st;
Position p(pos); Position p(pos);
for (int i = 0; pv[i] != MOVE_NONE; i++) for (int i = 0; pv[i] != MOVE_NONE; i++)
{ {
store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE); store(p, VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
p.do_move(pv[i], u); p.do_move(pv[i], st);
} }
} }
@@ -196,22 +189,8 @@ void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
/// entries which have received at least one write during the current search. /// entries which have received at least one write during the current search.
/// It is used to display the "info hashfull ..." information in UCI. /// It is used to display the "info hashfull ..." information in UCI.
int TranspositionTable::full() { int TranspositionTable::full() const {
double N = double(size) * 4.0; double N = double(size) * 4.0;
return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N)))); return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
} }
/// Constructors
TTEntry::TTEntry() {
}
TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
int generation) :
key_ (k), data((m & 0x7FFFF) | (t << 20) | (generation << 23)),
value_(v), depth_(int16_t(d)) {}
+29 -22
View File
@@ -34,18 +34,35 @@
//// Types //// Types
//// ////
/// The TTEntry class is the class of transposition table entries. /// The TTEntry class is the class of transposition table entries
///
/// A TTEntry needs 128 bits to be stored
///
/// bit 0-63: key
/// bit 64-95: data
/// bit 96-111: value
/// bit 112-127: depth
///
/// the 32 bits of the data field are so defined
///
/// bit 0-16: move
/// bit 17-19: not used
/// bit 20-22: value type
/// bit 23-31: generation
class TTEntry { class TTEntry {
public: public:
TTEntry(); TTEntry() {}
TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation); TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation)
: key_ (k), data((m & 0x1FFFF) | (t << 20) | (generation << 23)),
value_(int16_t(v)), depth_(int16_t(d)) {}
Key key() const { return key_; } Key key() const { return key_; }
Depth depth() const { return Depth(depth_); } Depth depth() const { return Depth(depth_); }
Move move() const { return Move(data & 0x7FFFF); } Move move() const { return Move(data & 0x1FFFF); }
Value value() const { return Value(value_); } Value value() const { return Value(value_); }
ValueType type() const { return ValueType((data >> 20) & 3); } ValueType type() const { return ValueType((data >> 20) & 7); }
int generation() const { return (data >> 23); } int generation() const { return (data >> 23); }
private: private:
@@ -62,32 +79,22 @@ private:
class TranspositionTable { class TranspositionTable {
public: public:
TranspositionTable(unsigned mbSize); TranspositionTable();
~TranspositionTable(); ~TranspositionTable();
void set_size(unsigned mbSize); void set_size(unsigned mbSize);
void clear(); void clear();
void store(const Position &pos, Value v, Depth d, Move m, ValueType type); void store(const Position& pos, Value v, ValueType type, Depth d, Move m);
const TTEntry* retrieve(const Position &pos) const; TTEntry* retrieve(const Position& pos) const;
void new_search(); void new_search();
void insert_pv(const Position &pos, Move pv[]); void insert_pv(const Position& pos, Move pv[]);
int full(); int full() const;
private: private:
inline TTEntry* first_entry(const Position &pos) const; inline TTEntry* first_entry(const Position& pos) const;
unsigned size; unsigned size, writes;
int writes;
TTEntry* entries; TTEntry* entries;
uint8_t generation; uint8_t generation;
}; };
////
//// Constants and variables
////
// Default transposition table size, in megabytes:
const int TTDefaultSize = 32;
#endif // !defined(TT_H_INCLUDED) #endif // !defined(TT_H_INCLUDED)
+15 -7
View File
@@ -22,6 +22,7 @@
//// Includes //// Includes
//// ////
#include <cassert>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
@@ -169,7 +170,7 @@ namespace {
} }
else if (token == "key") else if (token == "key")
{ {
std::cout << "key: " << RootPosition.get_key() std::cout << "key: " << std::hex << RootPosition.get_key()
<< " material key: " << RootPosition.get_material_key() << " material key: " << RootPosition.get_material_key()
<< " pawn key: " << RootPosition.get_pawn_key() << " pawn key: " << RootPosition.get_pawn_key()
<< std::endl; << std::endl;
@@ -219,15 +220,18 @@ namespace {
if (token == "moves") if (token == "moves")
{ {
Move move; Move move;
UndoInfo u; StateInfo st;
while (!uip.eof()) while (!uip.eof())
{ {
uip >> token; uip >> token;
move = move_from_string(RootPosition, token); move = move_from_string(RootPosition, token);
RootPosition.do_move(move, u); RootPosition.do_move(move, st);
if (RootPosition.rule_50_counter() == 0) if (RootPosition.rule_50_counter() == 0)
RootPosition.reset_game_ply(); RootPosition.reset_game_ply();
} }
// Our StateInfo st is about going out of scope,
// so save its content before they disappear.
RootPosition.setStartState(st);
} }
} }
} }
@@ -247,11 +251,13 @@ namespace {
if (token == "name") if (token == "name")
{ {
uip >> name; uip >> name;
uip >> token; while (!uip.eof())
while (!uip.eof() && token != "value")
{ {
name += (" " + token); uip >> token;
uip >> token; if (token == "value")
break;
name += (" " + token);
} }
if (token == "value") if (token == "value")
{ {
@@ -321,6 +327,8 @@ namespace {
if (moveTime) if (moveTime)
infinite = true; // HACK infinite = true; // HACK
assert(RootPosition.is_ok());
think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time, think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time,
inc, movesToGo, depth, nodes, moveTime, searchMoves); inc, movesToGo, depth, nodes, moveTime, searchMoves);
} }
+146 -164
View File
@@ -22,7 +22,9 @@
//// Includes //// Includes
//// ////
#include <algorithm>
#include <cassert> #include <cassert>
#include <map>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@@ -36,7 +38,7 @@
//// Variables //// Variables
//// ////
bool Chess960 = false; bool Chess960;
//// ////
@@ -57,90 +59,92 @@ namespace {
std::string name, defaultValue, currentValue; std::string name, defaultValue, currentValue;
OptionType type; OptionType type;
size_t idx;
int minValue, maxValue; int minValue, maxValue;
ComboValues comboValues; ComboValues comboValues;
Option(const char* name, const char* defaultValue, OptionType = STRING); Option();
Option(const char* name, bool defaultValue, OptionType = CHECK); Option(const char* defaultValue, OptionType = STRING);
Option(const char* name, int defaultValue, int minValue, int maxValue); Option(bool defaultValue, OptionType = CHECK);
Option(int defaultValue, int minValue, int maxValue);
bool operator<(const Option& o) const { return this->idx < o.idx; }
}; };
typedef std::vector<Option> Options; typedef std::map<std::string, Option> Options;
/// ///
/// Constants /// Constants
/// ///
// load_defaults populates the options vector with the hard // load_defaults populates the options map with the hard
// coded names and default values. // coded names and default values.
void load_defaults(Options& o) { void load_defaults(Options& o) {
o.push_back(Option("Use Search Log", false)); o["Use Search Log"] = Option(false);
o.push_back(Option("Search Log Filename", "SearchLog.txt")); o["Search Log Filename"] = Option("SearchLog.txt");
o.push_back(Option("Book File", "book.bin")); o["Book File"] = Option("book.bin");
o.push_back(Option("Mobility (Middle Game)", 100, 0, 200)); o["Mobility (Middle Game)"] = Option(100, 0, 200);
o.push_back(Option("Mobility (Endgame)", 100, 0, 200)); o["Mobility (Endgame)"] = Option(100, 0, 200);
o.push_back(Option("Pawn Structure (Middle Game)", 100, 0, 200)); o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
o.push_back(Option("Pawn Structure (Endgame)", 100, 0, 200)); o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
o.push_back(Option("Passed Pawns (Middle Game)", 100, 0, 200)); o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
o.push_back(Option("Passed Pawns (Endgame)", 100, 0, 200)); o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
o.push_back(Option("Aggressiveness", 100, 0, 200)); o["Space"] = Option(100, 0, 200);
o.push_back(Option("Cowardice", 100, 0, 200)); o["Aggressiveness"] = Option(100, 0, 200);
o.push_back(Option("King Safety Curve", "Quadratic", COMBO)); o["Cowardice"] = Option(100, 0, 200);
o["King Safety Curve"] = Option("Quadratic", COMBO);
o.back().comboValues.push_back("Quadratic"); o["King Safety Curve"].comboValues.push_back("Quadratic");
o.back().comboValues.push_back("Linear"); /*, "From File"*/ o["King Safety Curve"].comboValues.push_back("Linear"); /*, "From File"*/
o.push_back(Option("King Safety Coefficient", 40, 1, 100)); o["King Safety Coefficient"] = Option(40, 1, 100);
o.push_back(Option("King Safety X Intercept", 0, 0, 20)); o["King Safety X Intercept"] = Option(0, 0, 20);
o.push_back(Option("King Safety Max Slope", 30, 10, 100)); o["King Safety Max Slope"] = Option(30, 10, 100);
o.push_back(Option("King Safety Max Value", 500, 100, 1000)); o["King Safety Max Value"] = Option(500, 100, 1000);
o.push_back(Option("Queen Contact Check Bonus", 4, 0, 8)); o["Queen Contact Check Bonus"] = Option(3, 0, 8);
o.push_back(Option("Rook Contact Check Bonus", 2, 0, 4)); o["Queen Check Bonus"] = Option(2, 0, 4);
o.push_back(Option("Queen Check Bonus", 2, 0, 4)); o["Rook Check Bonus"] = Option(1, 0, 4);
o.push_back(Option("Rook Check Bonus", 1, 0, 4)); o["Bishop Check Bonus"] = Option(1, 0, 4);
o.push_back(Option("Bishop Check Bonus", 1, 0, 4)); o["Knight Check Bonus"] = Option(1, 0, 4);
o.push_back(Option("Knight Check Bonus", 1, 0, 4)); o["Discovered Check Bonus"] = Option(3, 0, 8);
o.push_back(Option("Discovered Check Bonus", 3, 0, 8)); o["Mate Threat Bonus"] = Option(3, 0, 8);
o.push_back(Option("Mate Threat Bonus", 3, 0, 8)); o["Check Extension (PV nodes)"] = Option(2, 0, 2);
o.push_back(Option("Check Extension (PV nodes)", 2, 0, 2)); o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
o.push_back(Option("Check Extension (non-PV nodes)", 1, 0, 2)); o["Single Reply Extension (PV nodes)"] = Option(2, 0, 2);
o.push_back(Option("Single Reply Extension (PV nodes)", 2, 0, 2)); o["Single Reply Extension (non-PV nodes)"] = Option(2, 0, 2);
o.push_back(Option("Single Reply Extension (non-PV nodes)", 2, 0, 2)); o["Mate Threat Extension (PV nodes)"] = Option(0, 0, 2);
o.push_back(Option("Mate Threat Extension (PV nodes)", 0, 0, 2)); o["Mate Threat Extension (non-PV nodes)"] = Option(0, 0, 2);
o.push_back(Option("Mate Threat Extension (non-PV nodes)", 0, 0, 2)); o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
o.push_back(Option("Pawn Push to 7th Extension (PV nodes)", 1, 0, 2)); o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
o.push_back(Option("Pawn Push to 7th Extension (non-PV nodes)", 1, 0, 2)); o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
o.push_back(Option("Passed Pawn Extension (PV nodes)", 1, 0, 2)); o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
o.push_back(Option("Passed Pawn Extension (non-PV nodes)", 0, 0, 2)); o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
o.push_back(Option("Pawn Endgame Extension (PV nodes)", 2, 0, 2)); o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
o.push_back(Option("Pawn Endgame Extension (non-PV nodes)", 2, 0, 2)); o["Full Depth Moves (PV nodes)"] = Option(14, 1, 100);
o.push_back(Option("Full Depth Moves (PV nodes)", 14, 1, 100)); o["Full Depth Moves (non-PV nodes)"] = Option(3, 1, 100);
o.push_back(Option("Full Depth Moves (non-PV nodes)", 3, 1, 100)); o["Threat Depth"] = Option(5, 0, 100);
o.push_back(Option("Threat Depth", 5, 0, 100)); o["Futility Pruning (Main Search)"] = Option(true);
o.push_back(Option("Selective Plies", 7, 0, 10)); o["Futility Pruning (Quiescence Search)"] = Option(true);
o.push_back(Option("Futility Pruning (Main Search)", true)); o["LSN filtering"] = Option(false);
o.push_back(Option("Futility Pruning (Quiescence Search)", true)); o["LSN Time Margin (sec)"] = Option(4, 1, 10);
o.push_back(Option("Futility Margin 0", 50, 0, 1000)); o["LSN Value Margin"] = Option(200, 100, 600);
o.push_back(Option("Futility Margin 1", 100, 0, 1000)); o["Randomness"] = Option(0, 0, 10);
o.push_back(Option("Futility Margin 2", 300, 0, 1000)); o["Minimum Split Depth"] = Option(4, 4, 7);
o.push_back(Option("Maximum Razoring Depth", 3, 0, 4)); o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
o.push_back(Option("Razoring Margin", 300, 150, 600)); o["Threads"] = Option(1, 1, 8);
o.push_back(Option("LSN filtering", false)); o["Hash"] = Option(32, 4, 4096);
o.push_back(Option("LSN Time Margin (sec)", 4, 1, 10)); o["Clear Hash"] = Option(false, BUTTON);
o.push_back(Option("LSN Value Margin", 200, 100, 600)); o["Ponder"] = Option(true);
o.push_back(Option("Randomness", 0, 0, 10)); o["OwnBook"] = Option(true);
o.push_back(Option("Minimum Split Depth", 4, 4, 7)); o["MultiPV"] = Option(1, 1, 500);
o.push_back(Option("Maximum Number of Threads per Split Point", 5, 4, 8)); o["UCI_ShowCurrLine"] = Option(false);
o.push_back(Option("Threads", 1, 1, 8)); o["UCI_Chess960"] = Option(false);
o.push_back(Option("Hash", 32, 4, 4096));
o.push_back(Option("Clear Hash", false, BUTTON)); // Any option should know its name so to be easily printed
o.push_back(Option("Ponder", true)); for (Options::iterator it = o.begin(); it != o.end(); ++it)
o.push_back(Option("OwnBook", true)); it->second.name = it->first;
o.push_back(Option("MultiPV", 1, 1, 500));
o.push_back(Option("UCI_ShowCurrLine", false));
o.push_back(Option("UCI_Chess960", false));
} }
/// ///
@@ -149,9 +153,6 @@ namespace {
Options options; Options options;
// Local functions
Options::iterator option_with_name(const std::string& optionName);
// stringify converts a value of type T to a std::string // stringify converts a value of type T to a std::string
template<typename T> template<typename T>
std::string stringify(const T& v) { std::string stringify(const T& v) {
@@ -161,13 +162,6 @@ namespace {
return ss.str(); return ss.str();
} }
// We want conversion from a bool value to be "true" or "false",
// not "1" or "0", so add a specialization for bool type.
template<>
std::string stringify<bool>(const bool& v) {
return v ? "true" : "false";
}
// get_option_value implements the various get_option_value_<type> // get_option_value implements the various get_option_value_<type>
// functions defined later, because only the option value // functions defined later, because only the option value
@@ -176,29 +170,15 @@ namespace {
template<typename T> template<typename T>
T get_option_value(const std::string& optionName) { T get_option_value(const std::string& optionName) {
T ret; T ret = T();
Options::iterator it = option_with_name(optionName); if (options.find(optionName) == options.end())
return ret;
if (it != options.end()) std::istringstream ss(options[optionName].currentValue);
{ ss >> ret;
std::istringstream ss(it->currentValue);
ss >> ret;
}
return ret; return ret;
} }
// Unfortunatly we need a specialization to convert "false" and "true"
// to proper bool values. The culprit is that we use a non standard way
// to store a bool value in a string, in particular we use "false" and
// "true" instead of "0" and "1" due to how UCI protocol works.
template<>
bool get_option_value<bool>(const std::string& optionName) {
Options::iterator it = option_with_name(optionName);
return it != options.end() && it->currentValue == "true";
}
} }
//// ////
@@ -217,24 +197,19 @@ void init_uci_options() {
// According to Ken Dail's tests, Glaurung plays much better with 7 than // According to Ken Dail's tests, Glaurung plays much better with 7 than
// with 8 threads. This is weird, but it is probably difficult to find out // with 8 threads. This is weird, but it is probably difficult to find out
// why before I have a 8-core computer to experiment with myself. // why before I have a 8-core computer to experiment with myself.
Options::iterator it = option_with_name("Threads"); assert(options.find("Threads") != options.end());
assert(options.find("Minimum Split Depth") != options.end());
assert(it != options.end()); options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
it->defaultValue = stringify(Min(cpu_count(), 7));
it->currentValue = stringify(Min(cpu_count(), 7));
// Increase the minimum split depth when the number of CPUs is big. // Increase the minimum split depth when the number of CPUs is big.
// It would probably be better to let this depend on the number of threads // It would probably be better to let this depend on the number of threads
// instead. // instead.
if(cpu_count() > 4) if (cpu_count() > 4)
{ {
it = option_with_name("Minimum Split Depth"); options["Minimum Split Depth"].defaultValue = "6";
options["Minimum Split Depth"].currentValue = "6";
assert(it != options.end());
it->defaultValue = "6";
it->currentValue = "6";
} }
} }
@@ -248,28 +223,40 @@ void print_uci_options() {
"spin", "combo", "check", "string", "button" "spin", "combo", "check", "string", "button"
}; };
for (Options::iterator it = options.begin(); it != options.end(); ++it) // Build up a vector out of the options map and sort it according to idx
{ // field, that is the chronological insertion order in options map.
std::cout << "option name " << it->name std::vector<Option> vec;
<< " type " << optionTypeName[it->type]; for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
vec.push_back(it->second);
if (it->type != BUTTON) std::sort(vec.begin(), vec.end());
{
for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
{
std::cout << "\noption name " << it->name
<< " type " << optionTypeName[it->type];
if (it->type == BUTTON)
continue;
if (it->type == CHECK)
std::cout << " default " << (it->defaultValue == "1" ? "true" : "false");
else
std::cout << " default " << it->defaultValue; std::cout << " default " << it->defaultValue;
if (it->type == SPIN) if (it->type == SPIN)
std::cout << " min " << it->minValue std::cout << " min " << it->minValue
<< " max " << it->maxValue; << " max " << it->maxValue;
else if (it->type == COMBO) else if (it->type == COMBO)
for(ComboValues::iterator itc = it->comboValues.begin(); for (ComboValues::const_iterator itc = it->comboValues.begin();
itc != it->comboValues.end(); ++itc) itc != it->comboValues.end(); ++itc)
std::cout << " var " << *itc; std::cout << " var " << *itc;
}
std::cout << std::endl;
} }
std::cout << std::endl;
} }
/// get_option_value_bool() returns the current value of a UCI parameter of /// get_option_value_bool() returns the current value of a UCI parameter of
/// type "check". /// type "check".
@@ -291,7 +278,7 @@ int get_option_value_int(const std::string& optionName) {
/// get_option_value_string() returns the current value of a UCI parameter as /// get_option_value_string() returns the current value of a UCI parameter as
/// a string. It is used with parameters of type "combo" and "string". /// a string. It is used with parameters of type "combo" and "string".
const std::string get_option_value_string(const std::string& optionName) { const std::string get_option_value_string(const std::string& optionName) {
@@ -299,32 +286,23 @@ const std::string get_option_value_string(const std::string& optionName) {
} }
/// button_was_pressed() tests whether a UCI parameter of type "button" has /// set_option_value() inserts a new value for a UCI parameter. Note that
/// been selected since the last time the function was called.
bool button_was_pressed(const std::string& buttonName) {
if (get_option_value<bool>(buttonName))
{
set_option_value(buttonName, "false");
return true;
}
return false;
}
/// set_option_value() inserts a new value for a UCI parameter. Note that
/// the function does not check that the new value is legal for the given /// the function does not check that the new value is legal for the given
/// parameter: This is assumed to be the responsibility of the GUI. /// parameter: This is assumed to be the responsibility of the GUI.
void set_option_value(const std::string& optionName, void set_option_value(const std::string& optionName,
const std::string& newValue) { const std::string& newValue) {
Options::iterator it = option_with_name(optionName); // UCI protocol uses "true" and "false" instead of "1" and "0", so convert
// newValue according to standard C++ convention before to store it.
std::string v(newValue);
if (v == "true")
v = "1";
else if (v == "false")
v = "0";
if (it != options.end()) if (options.find(optionName) != options.end())
it->currentValue = newValue; options[optionName].currentValue = v;
else else
std::cout << "No such option: " << optionName << std::endl; std::cout << "No such option: " << optionName << std::endl;
} }
@@ -339,29 +317,33 @@ void push_button(const std::string& buttonName) {
} }
/// button_was_pressed() tests whether a UCI parameter of type "button" has
/// been selected since the last time the function was called, in this case
/// it also resets the button.
bool button_was_pressed(const std::string& buttonName) {
if (!get_option_value<bool>(buttonName))
return false;
set_option_value(buttonName, "false");
return true;
}
namespace { namespace {
// Define constructors of Option class. // Define constructors of Option class.
Option::Option(const char* nm, const char* def, OptionType t) Option::Option() {} // To allow insertion in a std::map
: name(nm), defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
Option::Option(const char* nm, bool def, OptionType t) Option::Option(const char* def, OptionType t)
: name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(t), minValue(0), maxValue(0) {} : defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
Option::Option(const char* nm, int def, int minv, int maxv) Option::Option(bool def, OptionType t)
: name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), minValue(minv), maxValue(maxv) {} : defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
// option_with_name() tries to find a UCI option with a given Option::Option(int def, int minv, int maxv)
// name. It returns an iterator to the UCI option or to options.end(), : defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}
// depending on whether an option with the given name exists.
Options::iterator option_with_name(const std::string& optionName) {
for (Options::iterator it = options.begin(); it != options.end(); ++it)
if (it->name == optionName)
return it;
return options.end();
}
} }
+16 -13
View File
@@ -36,7 +36,8 @@ enum ValueType {
VALUE_TYPE_NONE = 0, VALUE_TYPE_NONE = 0,
VALUE_TYPE_UPPER = 1, // Upper bound VALUE_TYPE_UPPER = 1, // Upper bound
VALUE_TYPE_LOWER = 2, // Lower bound VALUE_TYPE_LOWER = 2, // Lower bound
VALUE_TYPE_EXACT = 3 // Exact score VALUE_TYPE_EXACT = 3, // Exact score
VALUE_TYPE_EVAL = 4 // Evaluation cache
}; };
@@ -58,17 +59,19 @@ enum Value {
/// Important: If the material values are changed, one must also /// Important: If the material values are changed, one must also
/// adjust the piece square tables, and the method game_phase() in the /// adjust the piece square tables, and the method game_phase() in the
/// Position class! /// Position class!
///
/// Values modified by Joona Kiiski
const Value PawnValueMidgame = Value(0xCC); const Value PawnValueMidgame = Value(0x0CC);
const Value PawnValueEndgame = Value(0x100); const Value PawnValueEndgame = Value(0x101);
const Value KnightValueMidgame = Value(0x340); const Value KnightValueMidgame = Value(0x332);
const Value KnightValueEndgame = Value(0x340); const Value KnightValueEndgame = Value(0x34E);
const Value BishopValueMidgame = Value(0x340); const Value BishopValueMidgame = Value(0x345);
const Value BishopValueEndgame = Value(0x340); const Value BishopValueEndgame = Value(0x356);
const Value RookValueMidgame = Value(0x505); const Value RookValueMidgame = Value(0x4F8);
const Value RookValueEndgame = Value(0x505); const Value RookValueEndgame = Value(0x500);
const Value QueenValueMidgame = Value(0xA00); const Value QueenValueMidgame = Value(0x9D5);
const Value QueenValueEndgame = Value(0xA00); const Value QueenValueEndgame = Value(0x9FB);
const Value PieceValueMidgame[17] = { const Value PieceValueMidgame[17] = {
Value(0), Value(0),
@@ -92,8 +95,8 @@ const Value PieceValueEndgame[17] = {
/// Bonus for having the side to move /// Bonus for having the side to move
const Value TempoValueMidgame = Value(50); const Value TempoValueMidgame = Value(48);
const Value TempoValueEndgame = Value(20); const Value TempoValueEndgame = Value(21);
//// ////