Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by maksimKorzh »

Harald wrote: Sat Sep 26, 2020 5:42 pm I dAn't know if you wish to have an evaluation with piece values and PST only or not. I may have missed a part of this thread. However here is my explanation of the tampered evaluation.

For (nearly) all evaluation terms you need two versions of bonusses or penalties. One for the midgame (MG) and one for the endgame (EG). You do not need two versions if the values are the same.

The game phase depends on piece material without pawns but for both players. You can count the pieces (Q, R, B, N, q, r, b, n) and sum them up with weights. The weights may be simple integers like (4, 3, 2, 2, 4, 3, 2, 2) or your normal material values like (1000, 500, 350, 300, 1000, 500, 350, 300). The maximum value of the sum is (36) or (6600) respecively in the opening and (0) in the pawn endgame. From now on I use the 6600 value in my example. Therefore the game phase is (float) piece_material_sum / 6600 between 1.0 and 0.0. In the engine you may want to use an integer value between 1000 and 0.

int game_phase = 1000 * piece_material_sum / 6600; // 1000 in opening/midgame slowly going to 0 in pawn endgame

All evaluation terms are detected once and then summed up in two eval values.

...
value_mg += piece_square_table_mg[K][sq]; // white king midgame
value_eg += piece_square_table_eg[K][sq]; // white king endgame
...
value_mg += num_white_rooks_on_open_files * rook_on_open_file_bonus_mg;
value_eg += num_white_rooks_on_open_files * rook_on_open_file_bonus_eg;
...

And finally at the end of evaluation() there is an interpolation

value = (value_mg * game_phase + value_eg * (1000 - game_phase)) / 1000; // interpolation midgame..endgame and reverse the scaling.

Then comes the normal side and +- score code and the score is returned.
Thanks Harald.
All clear apart from interpolation. I've already read CPW article on it but what my dumb brain still can't get is WHAT IS interpolation.
Here's what clear for me from Ronald's explanations:
PeSTO's interpolation is not between 6.766 and 0, but between MAXMG 6192 and MINEG = 518,
So a phase higher than 6192 uses full MG value, a phase lower than 518 already uses full EG value.
So what is called interpolation is something to apply in case if game phase value is between MAXMG and MINEG?
If so I DON'T UNDERSTAND WHICH SCORES (OR HOW THEY ARE CALCULATED) IN THIS CASE?
Do we calculate average score between MG and EG value?

I just stuck and get it AT ALL(((
Harald
Posts: 318
Joined: Thu Mar 09, 2006 1:07 am

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by Harald »

See https://en.wikipedia.org/wiki/Linear_interpolation
We have a mathematical function f(x) = y and we know it exactly at two arguments x1 and x1 with the values f(x1) = y1 and f(x2) = y2

In our case the function f is the evaluation that depends on the game stage. We know the evaluation for the opening/midgame and for the endgame. This is what we store in our program as extreme values of bonusses and penalties. Therefore we know this. But what is between is not so clear. We just assume that the values changes slowly in a linear way (on a straight line if you draw it).
eval(some_feature_in_mg) = ...known
eval(some_feature_in game_phase) = ...wanted
eval(some_feature_in_eg) = ...known
Now comes the formula for a game phase in between mg and eg

value = (value_mg * game_phase + value_eg * (1000 - game_phase)) / 1000; // interpolation midgame..endgame and reverse the scaling.

Note for game phase = mg = 1000 this becomes
value = (value_mg * 1000 + value_eg * (1000 - 1000)) / 1000;
value = (value_mg * 1000 + value_eg * 0) / 1000;
value = (value_mg * 1000) / 1000;
value = value_mg;
For game phase = eg = 0 this becomes
value = (value_mg * 0 + value_eg * (1000 - 0)) / 1000;
value = (value_eg * 1000) / 1000;
value = value_eg;
For game phase = 70%mg_and_30%eg = 700 this becomes
value = (value_mg * 700 + value_eg * (1000 - 700)) / 1000;
value = (value_mg * 700 + value_eg * 300) / 1000;
value = value_mg___7_______eg; // something in between but more to the mg than to the eg
This is not the simple average (50% or game_phase = 500) but a weighted average.

If for some reason the game phase is > 1000 or < 0 then the formula also works but it is an extrapolation.
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by maksimKorzh »

Harald wrote: Sat Sep 26, 2020 8:03 pm See https://en.wikipedia.org/wiki/Linear_interpolation
We have a mathematical function f(x) = y and we know it exactly at two arguments x1 and x1 with the values f(x1) = y1 and f(x2) = y2

In our case the function f is the evaluation that depends on the game stage. We know the evaluation for the opening/midgame and for the endgame. This is what we store in our program as extreme values of bonusses and penalties. Therefore we know this. But what is between is not so clear. We just assume that the values changes slowly in a linear way (on a straight line if you draw it).
eval(some_feature_in_mg) = ...known
eval(some_feature_in game_phase) = ...wanted
eval(some_feature_in_eg) = ...known
Now comes the formula for a game phase in between mg and eg

value = (value_mg * game_phase + value_eg * (1000 - game_phase)) / 1000; // interpolation midgame..endgame and reverse the scaling.

Note for game phase = mg = 1000 this becomes
value = (value_mg * 1000 + value_eg * (1000 - 1000)) / 1000;
value = (value_mg * 1000 + value_eg * 0) / 1000;
value = (value_mg * 1000) / 1000;
value = value_mg;
For game phase = eg = 0 this becomes
value = (value_mg * 0 + value_eg * (1000 - 0)) / 1000;
value = (value_eg * 1000) / 1000;
value = value_eg;
For game phase = 70%mg_and_30%eg = 700 this becomes
value = (value_mg * 700 + value_eg * (1000 - 700)) / 1000;
value = (value_mg * 700 + value_eg * 300) / 1000;
value = value_mg___7_______eg; // something in between but more to the mg than to the eg
This is not the simple average (50% or game_phase = 500) but a weighted average.

If for some reason the game phase is > 1000 or < 0 then the formula also works but it is an extrapolation.
OMG! Now this is clear! Thank you so much for such a detailed explanation! Yet another post for my chess programming theory collection!
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by mvanthoor »

Let's say you're a freelance software engineer, who wants to make money by creating web scrapers for customers. Just a hypothetical situation, as an example. You would like to know how much you will earn, so you ask around. After some asking and prodding, you come to the conclusion:

"When I just start out, I can't ask more than 10 dollars per hour because I'm a rookie."
"But at the end of my carreer, after 40 years, I'll be able to ask 200 dollars/hour because of my massive experience."

But... I want to know what I will earn after 25 years. Or after 10 years.

So we make this into a formula. (And we assume that your asking price can raise with the same amount each year.)

years -> x
dollars -> y

When you just start out, you have 0 years of experience and you will earn 10 dollars per hour.

So: (0, 10)

At the end of your career you have 40 years of experience, and you will earn 200 dollars per hour.

So: (40, 200)

How much will you earn after 24 years of working? We don't know, but we can interpolate it. If you would plot the points (0, 10) and (40, 200) on an xy-axis, you can draw a straight line through them. The formula for this line is (and this formula can be solved for any straight line for which you know two points):

y = (a * x) + b

The first point is (0, 10); so if x = 0, y must be 10:

10 = a * 0 + b (<- if x is 0, the result is 0 for any 'a')
10 = 0 + b (<- and thus 'b' must be 10)

y = ax + 10... so what must 'a' be? We know both x and y, and we also know b, so...

200 = (a * 40) + 10
200 - 10 = a * 40
190 = a * 40
190 / 40 = a
a = 4.75

Therefore:

y = (4.75 * x) + 10

Let's check, with x = 0, when you start out:
y = 4.75 * 0 + 10
y = 0 + 10
y = 10 dollars/hour.

And when you are working 40 years:
y = 4.75 * 40 + 10
y = 190 + 10
y = 200 dollars/hour.

Cool. Seems to work.

So now we can interpolate how much you will earn when you're working for 24 years:

y = 4.75 * 24 + 10
y = 114 + 10
y = 124 dollars/hour

This is interpolation.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
User avatar
MikeB
Posts: 4889
Joined: Thu Mar 09, 2006 6:34 am
Location: Pen Argyl, Pennsylvania

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by MikeB »

hey code Monkey — this might have been a better forum for your youtube series about programming — Computer Chess Club: Programming and Technical Discussions maybe one of the moderators will move it for you if you ask ...
Image
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by maksimKorzh »

MikeB wrote: Sun Sep 27, 2020 2:12 am hey code Monkey — this might have been a better forum for your youtube series about programming — Computer Chess Club: Programming and Technical Discussions maybe one of the moderators will move it for you if you ask ...
Hi Mike. I believe noone in tech room would be interested. Judge it yourself - Harald, Pedro, Sven and others are literally teaching me, so what can they learn from my videos? And here it's more likely to find noobs like me interested in these tutorials.

What do you think?
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by maksimKorzh »

I want to thank to GERD ISENBERG for maintaining BBC page on CPW (Thank you so much, Sir!):
https://www.chessprogramming.org/BBC

Now after initial release I've started getting some feedback most of which pointing out to some minor bugs related to timing and printing PV.
I would obviously fix them soon.

Now here's a little plan on the near future regarding BBC development:
1. Video series would CONTINUE IN THE SAME FORMAT.
2. I got very excited by tapered evaluation, and PeSTO idea, hence these are the closest topics to cover
3. Implement Texel's tuning
4. Set up a testing framework
5. Add CLI interface

Next goal is to beat VICE (assuming that BBC hits the same depth tuning evaluation should be enough)
As soon as it would be achieved I'll get back to fixing existing bugs on timing and PV.

Thanks for your interest and stay tuned!
We've just started!
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by maksimKorzh »

mvanthoor wrote: Sat Sep 26, 2020 8:59 pm Let's say you're a freelance software engineer, who wants to make money by creating web scrapers for customers. Just a hypothetical situation, as an example. You would like to know how much you will earn, so you ask around. After some asking and prodding, you come to the conclusion:

"When I just start out, I can't ask more than 10 dollars per hour because I'm a rookie."
"But at the end of my carreer, after 40 years, I'll be able to ask 200 dollars/hour because of my massive experience."

But... I want to know what I will earn after 25 years. Or after 10 years.

So we make this into a formula. (And we assume that your asking price can raise with the same amount each year.)

years -> x
dollars -> y

When you just start out, you have 0 years of experience and you will earn 10 dollars per hour.

So: (0, 10)

At the end of your career you have 40 years of experience, and you will earn 200 dollars per hour.

So: (40, 200)

How much will you earn after 24 years of working? We don't know, but we can interpolate it. If you would plot the points (0, 10) and (40, 200) on an xy-axis, you can draw a straight line through them. The formula for this line is (and this formula can be solved for any straight line for which you know two points):

y = (a * x) + b

The first point is (0, 10); so if x = 0, y must be 10:

10 = a * 0 + b (<- if x is 0, the result is 0 for any 'a')
10 = 0 + b (<- and thus 'b' must be 10)

y = ax + 10... so what must 'a' be? We know both x and y, and we also know b, so...

200 = (a * 40) + 10
200 - 10 = a * 40
190 = a * 40
190 / 40 = a
a = 4.75

Therefore:

y = (4.75 * x) + 10

Let's check, with x = 0, when you start out:
y = 4.75 * 0 + 10
y = 0 + 10
y = 10 dollars/hour.

And when you are working 40 years:
y = 4.75 * 40 + 10
y = 190 + 10
y = 200 dollars/hour.

Cool. Seems to work.

So now we can interpolate how much you will earn when you're working for 24 years:

y = 4.75 * 24 + 10
y = 114 + 10
y = 124 dollars/hour

This is interpolation.
Thank you, Marcel!

I have never had a college education for 2 reasons:
1. The way they teach doesn't fit my learning curve
2. I'm too noob for that)

But the way you're explaining this is just awesome!
If my high school teacher of math could ever imagine that I'll be getting math education in the foreign language on forum dedicated to chess programming she would most likely go insane)))

I can promise the only thing to the community:
One day when I learn all this stuff myself I'll be sharing it with others as long as I can.
And I'll do my best to explain the way so other code monkeys like me would be getting the very gist of it.
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by maksimKorzh »

mvanthoor wrote: Sat Sep 26, 2020 8:59 pm Let's say you're a freelance software engineer, who wants to make money by creating web scrapers for customers. Just a hypothetical situation, as an example. You would like to know how much you will earn, so you ask around. After some asking and prodding, you come to the conclusion:

"When I just start out, I can't ask more than 10 dollars per hour because I'm a rookie."
"But at the end of my carreer, after 40 years, I'll be able to ask 200 dollars/hour because of my massive experience."

But... I want to know what I will earn after 25 years. Or after 10 years.

So we make this into a formula. (And we assume that your asking price can raise with the same amount each year.)

years -> x
dollars -> y

When you just start out, you have 0 years of experience and you will earn 10 dollars per hour.

So: (0, 10)

At the end of your career you have 40 years of experience, and you will earn 200 dollars per hour.

So: (40, 200)

How much will you earn after 24 years of working? We don't know, but we can interpolate it. If you would plot the points (0, 10) and (40, 200) on an xy-axis, you can draw a straight line through them. The formula for this line is (and this formula can be solved for any straight line for which you know two points):

y = (a * x) + b

The first point is (0, 10); so if x = 0, y must be 10:

10 = a * 0 + b (<- if x is 0, the result is 0 for any 'a')
10 = 0 + b (<- and thus 'b' must be 10)

y = ax + 10... so what must 'a' be? We know both x and y, and we also know b, so...

200 = (a * 40) + 10
200 - 10 = a * 40
190 = a * 40
190 / 40 = a
a = 4.75

Therefore:

y = (4.75 * x) + 10

Let's check, with x = 0, when you start out:
y = 4.75 * 0 + 10
y = 0 + 10
y = 10 dollars/hour.

And when you are working 40 years:
y = 4.75 * 40 + 10
y = 190 + 10
y = 200 dollars/hour.

Cool. Seems to work.

So now we can interpolate how much you will earn when you're working for 24 years:

y = 4.75 * 24 + 10
y = 114 + 10
y = 124 dollars/hour

This is interpolation.
Hold on a sec...

Marcel, isn't y = (a * x) + b just an equation that can be drawn as straight line???
I remember from school this one y = kx + b
And the idea is that the more coefficient k is growing the more y is growing (sorry my English is too poor to express myself)
So it's what is known as linear dependency right?
User avatar
hgm
Posts: 27819
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Bitboard CHESS ENGINE in C: YouTube series by Code Monkey King

Post by hgm »

Indeed, this is called 'linear interpolation'.

Perhaps an even simpler way to look at it is as a weighted average. You have two evaluations, one applying to a late end-game, one applying to opening/early middle-game. If you are half-way in between (judjed by total non-Pawn material on the board) you could just take the 50-50 average of those. If you are 3/4 on the way to the end-game (because only 25% of the material is left) you would take 75% of the end-game evaluation and 25% of the opening evaluation, etc.