Copying code

Discussion of chess software programming and technical issues.

Moderator: Ras

Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Copying code

Post by Sven »

bob wrote:
Rebel wrote:

Code: Select all

	if (board[A6]==WB && board[B5]==BP) score=score-64;
	if (board[H6]==WB && board[G5]==BP) score=score-64;
	if (board[A3]==BB && board[B4]==WP) score=score+64;
	if (board[H3]==BB && board[G4]==WP) score=score+64;
in my code, that would just be

if (trapped_mask[bishop_square] && pawns[enemy[) score -= 64;

And that would work for ANY bishop square and enemy pawn structure. Most of those trapped_mask[] values would be zero, for trapped_mask[a3] the thing would have one bit set for square b4. For trapped_mask[a4] you would find one bit set for b5. Etc.

is that a copy? Clearly not.
Of course not, now that you know how it works in R1.0beta (even slightly different than your example and also than Fruit, by doing it only once per color instead of once per bishop), and also considering that evaluating a "trapped bishop on a4" is quite unusual (neither Fruit nor R1.0 beta do so, and I guess that was a typo).

Here is the corresponding R1.0beta code again:

Code: Select all

0x4026d4: mov    $0x4020200000000,%rdx           Mask: B5, B6, C7
0x4026de: shr    $0x7,%rax
0x4026e2: and    %r10,%rax                       Mask with black pawns
0x4026e5: test   %rax,%rdx                       test with white bishops
0x4026e8: jne    0x402703
0x4026ea: mov    %rcx,%rax
0x4026ed: mov    $0x20404000000000,%rdx          Mask: G5, G6, F7
0x4026f7: shr    $0x9,%rax
0x4026fb: and    %r10,%rax
0x4026fe: test   %rax,%rdx                       if trapped bishop
0x402701: je     0x40270f                      
0x402703: sub    $0x70a,%esi                       subtract 1802 for opening
0x402709: sub    $0x70a,%edi                                     and endgame
And before I miss to mention it: "EVAL_COMP.pdf" attributes the "feature overlap" for the "trapped bishop" eval feature as "0.7" between Fruit 2.1 and R1.0beta, and as "0.5" between Fruit 2.1 and Crafty 19.0. I don't know where that comes from, since both did not copy that from Fruit :-)

Sven
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Copying code

Post by bob »

Sven Schüle wrote:
bob wrote:
Rebel wrote:

Code: Select all

	if (board[A6]==WB && board[B5]==BP) score=score-64;
	if (board[H6]==WB && board[G5]==BP) score=score-64;
	if (board[A3]==BB && board[B4]==WP) score=score+64;
	if (board[H3]==BB && board[G4]==WP) score=score+64;
in my code, that would just be

if (trapped_mask[bishop_square] && pawns[enemy[) score -= 64;

And that would work for ANY bishop square and enemy pawn structure. Most of those trapped_mask[] values would be zero, for trapped_mask[a3] the thing would have one bit set for square b4. For trapped_mask[a4] you would find one bit set for b5. Etc.

is that a copy? Clearly not.
Of course not, now that you know how it works in R1.0beta (even slightly different than your example and also than Fruit, by doing it only once per color instead of once per bishop), and also considering that evaluating a "trapped bishop on a4" is quite unusual (neither Fruit nor R1.0 beta do so, and I guess that was a typo).

Here is the corresponding R1.0beta code again:

Code: Select all

0x4026d4: mov    $0x4020200000000,%rdx           Mask: B5, B6, C7
0x4026de: shr    $0x7,%rax
0x4026e2: and    %r10,%rax                       Mask with black pawns
0x4026e5: test   %rax,%rdx                       test with white bishops
0x4026e8: jne    0x402703
0x4026ea: mov    %rcx,%rax
0x4026ed: mov    $0x20404000000000,%rdx          Mask: G5, G6, F7
0x4026f7: shr    $0x9,%rax
0x4026fb: and    %r10,%rax
0x4026fe: test   %rax,%rdx                       if trapped bishop
0x402701: je     0x40270f                      
0x402703: sub    $0x70a,%esi                       subtract 1802 for opening
0x402709: sub    $0x70a,%edi                                     and endgame
And before I miss to mention it: "EVAL_COMP.pdf" attributes the "feature overlap" for the "trapped bishop" eval feature as "0.7" between Fruit 2.1 and R1.0beta, and as "0.5" between Fruit 2.1 and Crafty 19.0. I don't know where that comes from, since both did not copy that from Fruit :-)

Sven
Crafty only considers a2/a7/h2/h7. No other squares. I tested the a3-a6-h3-h6 idea and got zero. And as a general rule, I leave anything out that produces nothing, UNLESS it simplifies the code.

And I don't do it as above today anyway. A long while back I combined the concept of "outpost bishops" with "trapped bishops" to get the test for free.
User avatar
Rebel
Posts: 7475
Joined: Thu Aug 18, 2011 12:04 pm
Full name: Ed Schröder

Re: Copying code

Post by Rebel »

Rebel wrote:There currently are discussions in CCC and RF about copying and it's much confusing what is allowed and what is not.

Here is the deal, provided a programmer already has an engine and he wants to copy code from another engine:

Verbatim copying code does not exists because the variable names, data structure are not compatible, so he will have to rewrite the code anyway to his own variable names, data structures.

Is that own written code ?

Data is another matter of course, this can be done by copy&paste without or minimal changes, thus copying.
bob wrote:That is ABSOLUTELY NOT "own written code". No more than copying an English page and just translating it to Spanish.
I agree, without adding content -> copying

And I am assuming that's the way copy-boys operate.

But......

In Crafty 2.22 you imported Fruit's MG/EG concept resulting in semantic equal code with Fruit. While I think it's OK what you did one can also conclude due to the semantically equal nature that it is a copy-case.

See the 2 examples at: http://www.top-5000.nl/temp.htm

Where do we draw the line?

Especially when the line is widening due to all the open sources that automatically lead to more semantically equal code without any bad intention of copying.

One A4 page as someone suggested ?
That is "plagiarism 101" for students. Copy a program. Change variable names. Reword comments. Maybe reformat using an auto-indent tool. Still a copy and still considered software plagiarism.
You are moving now outside the scope of my original question.
User avatar
Harvey Williamson
Posts: 2029
Joined: Sun May 25, 2008 11:12 pm
Location: Whitchurch. Shropshire, UK.
Full name: Harvey Williamson

Re: Copying code

Post by Harvey Williamson »

Rebel wrote:
Rebel wrote:There currently are discussions in CCC and RF about copying and it's much confusing what is allowed and what is not.

Here is the deal, provided a programmer already has an engine and he wants to copy code from another engine:

Verbatim copying code does not exists because the variable names, data structure are not compatible, so he will have to rewrite the code anyway to his own variable names, data structures.

Is that own written code ?

Data is another matter of course, this can be done by copy&paste without or minimal changes, thus copying.
bob wrote:That is ABSOLUTELY NOT "own written code". No more than copying an English page and just translating it to Spanish.
I agree, without adding content -> copying

And I am assuming that's the way copy-boys operate.

But......

In Crafty 2.22 you imported Fruit's MG/EG concept resulting in semantic equal code with Fruit. While I think it's OK what you did one can also conclude due to the semantically equal nature that it is a copy-case.

See the 2 examples at: http://www.top-5000.nl/temp.htm

Where do we draw the line?

Especially when the line is widening due to all the open sources that automatically lead more semantically equal code without any bad intention of copying.

One A4 page as someone suggested ?
That is "plagiarism 101" for students. Copy a program. Change variable names. Reword comments. Maybe reformat using an auto-indent tool. Still a copy and still considered software plagiarism.
You are moving now outside the scope of my original question.
Even if your accusation were correct, Ed, the Crafty source code is published as the Fruit GPL requires. So when is Vas going to comply with the Fruit GPL?
User avatar
Rebel
Posts: 7475
Joined: Thu Aug 18, 2011 12:04 pm
Full name: Ed Schröder

Re: Copying code

Post by Rebel »

zamar wrote:Why do you want to create confusion?
Joona,

I am not trying to create confusion, I am trying to shed some light on a fundamental point hardly discussed. Keep on reading.
User avatar
Rebel
Posts: 7475
Joined: Thu Aug 18, 2011 12:04 pm
Full name: Ed Schröder

Re: Copying code

Post by Rebel »

Harvey Williamson wrote:
Rebel wrote:
Rebel wrote:There currently are discussions in CCC and RF about copying and it's much confusing what is allowed and what is not.

Here is the deal, provided a programmer already has an engine and he wants to copy code from another engine:

Verbatim copying code does not exists because the variable names, data structure are not compatible, so he will have to rewrite the code anyway to his own variable names, data structures.

Is that own written code ?

Data is another matter of course, this can be done by copy&paste without or minimal changes, thus copying.
bob wrote:That is ABSOLUTELY NOT "own written code". No more than copying an English page and just translating it to Spanish.
I agree, without adding content -> copying

And I am assuming that's the way copy-boys operate.

But......

In Crafty 2.22 you imported Fruit's MG/EG concept resulting in semantic equal code with Fruit. While I think it's OK what you did one can also conclude due to the semantically equal nature that it is a copy-case.

See the 2 examples at: http://www.top-5000.nl/temp.htm

Where do we draw the line?

Especially when the line is widening due to all the open sources that automatically lead more semantically equal code without any bad intention of copying.

One A4 page as someone suggested ?
That is "plagiarism 101" for students. Copy a program. Change variable names. Reword comments. Maybe reformat using an auto-indent tool. Still a copy and still considered software plagiarism.
You are moving now outside the scope of my original question.
Even if your accusation were correct, Ed, the Crafty source code is published as the Fruit GPL requires. So when is Vas going to comply with the Fruit GPL?
We are having a technical discussion here, can you please stay out with off-topic comments?
User avatar
Harvey Williamson
Posts: 2029
Joined: Sun May 25, 2008 11:12 pm
Location: Whitchurch. Shropshire, UK.
Full name: Harvey Williamson

Re: Copying code

Post by Harvey Williamson »

Rebel wrote:
Harvey Williamson wrote:
Rebel wrote:
Rebel wrote:There currently are discussions in CCC and RF about copying and it's much confusing what is allowed and what is not.

Here is the deal, provided a programmer already has an engine and he wants to copy code from another engine:

Verbatim copying code does not exists because the variable names, data structure are not compatible, so he will have to rewrite the code anyway to his own variable names, data structures.

Is that own written code ?

Data is another matter of course, this can be done by copy&paste without or minimal changes, thus copying.
bob wrote:That is ABSOLUTELY NOT "own written code". No more than copying an English page and just translating it to Spanish.
I agree, without adding content -> copying

And I am assuming that's the way copy-boys operate.

But......

In Crafty 2.22 you imported Fruit's MG/EG concept resulting in semantic equal code with Fruit. While I think it's OK what you did one can also conclude due to the semantically equal nature that it is a copy-case.

See the 2 examples at: http://www.top-5000.nl/temp.htm

Where do we draw the line?

Especially when the line is widening due to all the open sources that automatically lead more semantically equal code without any bad intention of copying.

One A4 page as someone suggested ?
That is "plagiarism 101" for students. Copy a program. Change variable names. Reword comments. Maybe reformat using an auto-indent tool. Still a copy and still considered software plagiarism.
You are moving now outside the scope of my original question.
Even if your accusation were correct, Ed, the Crafty source code is published as the Fruit GPL requires. So when is Vas going to comply with the Fruit GPL?
We are having a technical discussion here, can you please stay out with off-topic comments?
You invited me here with your reply to me yesterday telling me about this thread. If the pressure of telling lies for so long is getting to you perhaps you should go and lie down.
User avatar
Rebel
Posts: 7475
Joined: Thu Aug 18, 2011 12:04 pm
Full name: Ed Schröder

Re: Copying code

Post by Rebel »

Rebel wrote:
zamar wrote:
Rebel wrote: Here is the deal, provided a programmer already has an engine and he wants to copy code from another engine:
Why would anyone want to copy code from another engine? Copyright laws in most Western countries prohibit copying anything without author's permission.
IPPO is freeware.
Reading another person's engine and writing your engine by using similar ideas is of course okay. But resulting functions, semantics, arrays are going to very different.
Here is an example that proofs you wrong, at least if you think I did nothing wrong, which I think you do :wink:

2-3 years ago I went through the Fruit source code because of the raging Rybka debate and I found something I did not have, penalizing a trapped bishop on the 6th rank. So I took it and it resulted in the following code.

Code: Select all

	if (board[A6]==WB && board[B5]==BP) score=score-64;
	if (board[H6]==WB && board[G5]==BP) score=score-64;
	if (board[A3]==BB && board[B4]==WP) score=score+64;
	if (board[H3]==BB && board[G4]==WP) score=score+64;
My own code, I copied nothing from Fruit. However, compare this with the Fruit source code, 100% identical, not only semantically equal, even code wise equal.

Did I plagiarize Fruit ?
bob wrote: Yes and No. Yes, because you did copy code. But No, because I do not believe anyone would say "you are a derivative because out of Nk lines of code, those 4 are identical.
Absolutely not.

Because with the same logic I can say, hey Fabien, you copied my old 7th rank trapped bishop code from the 80's, 100% equal on code level.

See all the nonsense of that?

There is no copyright on chess knowledge, not even if that produces 100% equal code.

Your Crafty 22.2 example as addressed above is much more interesting because it is somewhat more complex than my simple A6 trapped bishop example.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Copying code

Post by Sven »

bob wrote:Crafty only considers a2/a7/h2/h7. No other squares. I tested the a3-a6-h3-h6 idea and got zero. And as a general rule, I leave anything out that produces nothing, UNLESS it simplifies the code.

And I don't do it as above today anyway. A long while back I combined the concept of "outpost bishops" with "trapped bishops" to get the test for free.
At least you also used b1/b8/g1/g8 from Crafty 19.2 (2003) until you dropped it in 23.0 (2009). See the code history further down, which does also show the following:

1. Crafty did almost always have the trapped bishop eval term in some kind, even long before Fruit appeared. (Not denied by anone, of course, just to clarify.)

2. Crafty *never* implemented it in the way as you proposed in your example above (I already fixed the two bugs :-) ):

Code: Select all

if (trapped_mask[bishop_square] & pawns[enemy]) score -= 64;
but instead always used a form where pairs of explicit squares, like A7/B6 or B8/C7, were checked (with several improvements over time), as can also be seen in Fruit later on.

3. The Rybka 1.0beta implementation (see my previous post for the asm) was really different from all that, by doing the whole "trapped bishop" test once per corner [I previously wrote "once per color" but actually "corner" is more accurate] instead of once per bishop (Crafty) resp. on square-by-square base, and also by not using any additional memory space for it. Also: who else has something similar to that "B5/C6/C7" masking concept of R1.0beta?

So my basic point of the "trapped bishop" story w.r.t. its analysis in EVAL_COMP comes down to:

- Attributing "0.5" as feature overlap of Crafty 19.0 and Fruit 2.1 is highly exaggerated, the difference is significant so "0.0" or "0.1" would be more appropriate IN MY VIEW since even the set of tested squares differs a lot.

- Attributing "0.7" as feature overlap of R1.0beta and Fruit 2.1 is highly exaggerated as well, also here the difference is very significant. Since the set of tested squares is the same but everything else differs a lot one might perhaps want to assign "0.2" IN MY VIEW.

- Furthermore, this "trapped bishop" stuff is one of several examples where the whole "feature overlap" concept simply breaks down since we are talking about one of many elements of "common chess knowledge", only the details (only check bishop on A7, or also check B8, or even also A6) make tiny differences, so there is very low information content (IC) in any actual implementation of that term. Measuring "code overlap" might make sense, instead, but here R1.0 is certainly at "0.0" in this case.


Now, as promised, here is the relevant part of the "Crafty trapped bishop" code history, to back up some of my statements above.

Code: Select all

Crafty 17.0 (nov-1999) until 19.1 (oct-2002):

[once per color, example for White]

if (WhiteBishops) {
  if (WhiteBishops&mask_A7H7) {
    if (WhiteBishops&SetMask(A7) && SetMask(B6)&BlackPawns)
      score-=BISHOP_TRAPPED;
    else if (WhiteBishops&SetMask(H7) && SetMask(G6)&BlackPawns)
      score-=BISHOP_TRAPPED;
  }
}


Crafty 19.2 (jan-2003) until 20.0 (aug-2005):

[once per color, example for White; introduced B8/G8 test]

if (WhiteBishops) {
  if (WhiteBishops&mask_WBT) {
    if (WhiteBishops&SetMask(A7) && SetMask(B6)&BlackPawns)
      score-=BISHOP_TRAPPED;
    else if (WhiteBishops&SetMask(B8) && SetMask(C7)&BlackPawns)
      score-=BISHOP_TRAPPED;
    else if (WhiteBishops&SetMask(H7) && SetMask(G6)&BlackPawns)
      score-=BISHOP_TRAPPED;
    else if (WhiteBishops&SetMask(G8) && SetMask(F7)&BlackPawns)
      score-=BISHOP_TRAPPED;
  }
}


Crafty 20.1 (oct-2005) until at least 21.6 (oct-2007):

[done for each bishop within the bishop eval loop, example for White]

if (square == A7 && SetMask(B6) & BlackPawns)
  score -= bishop_trapped;
else if (square == B8 && SetMask(C7) & BlackPawns)
  score -= bishop_trapped;
else if (square == H7 && SetMask(G6) & BlackPawns)
  score -= bishop_trapped;
else if (square == G8 && SetMask(F7) & BlackPawns)
  score -= bishop_trapped;


Crafty 22.0 (feb-2008) until 22.1 (apr-2008):

[done for each bishop within the bishop eval loop; now generic code for both sides]

if (square == sqflip[side][A7] && SetMask(sqflip[side][B6]) & Pawns(enemy))
  score -= bishop_trapped;
else if (square == sqflip[side][B8] &&
    SetMask(sqflip[side][C7]) & Pawns(enemy))
  score -= bishop_trapped;
else if (square == sqflip[side][H7] &&
    SetMask(sqflip[side][G6]) & Pawns(enemy))
  score -= bishop_trapped;
else if (square == sqflip[side][G8] &&
    SetMask(sqflip[side][F7]) & Pawns(enemy))
  score -= bishop_trapped;


Crafty 22.2 (nov-2008) until 22.10 (jan-2009):

[same as before but introduced score_mg/score_eg concept in Crafty eval]

if (square == sqflip[side][A7] && SetMask(sqflip[side][B6]) & Pawns(enemy)) {
  score_eg -= bishop_trapped;
  score_mg -= bishop_trapped;
} else if (square == sqflip[side][B8] &&
    SetMask(sqflip[side][C7]) & Pawns(enemy)) {
  score_eg -= bishop_trapped;
  score_mg -= bishop_trapped;
} else if (square == sqflip[side][H7] &&
    SetMask(sqflip[side][G6]) & Pawns(enemy)) {
  score_eg -= bishop_trapped;
  score_mg -= bishop_trapped;
} else if (square == sqflip[side][G8] &&
    SetMask(sqflip[side][F7]) & Pawns(enemy)) {
  score_eg -= bishop_trapped;
  score_mg -= bishop_trapped;
}


Crafty 23.0 (feb-2009) until 23.4 (nov-2010):

[still done for each bishop within the bishop eval loop;
now combined with "bishop outpost" evaluation and also dropped B8/G8 from pattern]

if (square == sqflip[side][A7]) {
  if (SetMask(sqflip[side][B6]) & Pawns(enemy)) {
    score_eg -= bishop_trapped;
    score_mg -= bishop_trapped;
  }
} else if (SetMask(sqflip[side][G6]) & Pawns(enemy)) {
  score_eg -= bishop_trapped;
  score_mg -= bishop_trapped;
}
Sven
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Copying code

Post by bob »

Rebel wrote:
Rebel wrote:
zamar wrote:
Rebel wrote: Here is the deal, provided a programmer already has an engine and he wants to copy code from another engine:
Why would anyone want to copy code from another engine? Copyright laws in most Western countries prohibit copying anything without author's permission.
IPPO is freeware.
Reading another person's engine and writing your engine by using similar ideas is of course okay. But resulting functions, semantics, arrays are going to very different.
Here is an example that proofs you wrong, at least if you think I did nothing wrong, which I think you do :wink:

2-3 years ago I went through the Fruit source code because of the raging Rybka debate and I found something I did not have, penalizing a trapped bishop on the 6th rank. So I took it and it resulted in the following code.

Code: Select all

	if (board[A6]==WB && board[B5]==BP) score=score-64;
	if (board[H6]==WB && board[G5]==BP) score=score-64;
	if (board[A3]==BB && board[B4]==WP) score=score+64;
	if (board[H3]==BB && board[G4]==WP) score=score+64;
My own code, I copied nothing from Fruit. However, compare this with the Fruit source code, 100% identical, not only semantically equal, even code wise equal.

Did I plagiarize Fruit ?
bob wrote: Yes and No. Yes, because you did copy code. But No, because I do not believe anyone would say "you are a derivative because out of Nk lines of code, those 4 are identical.
Absolutely not.

Because with the same logic I can say, hey Fabien, you copied my old 7th rank trapped bishop code from the 80's, 100% equal on code level.

See all the nonsense of that?

There is no copyright on chess knowledge, not even if that produces 100% equal code.

Your Crafty 22.2 example as addressed above is much more interesting because it is somewhat more complex than my simple A6 trapped bishop example.
That's the main reason nobody would say "one piece of knowledge, implemented exactly the same in two programs, shows copying." However, MANY pieces of knowledge is something else, entirely.

BTW I have done this at LEAST 10-20 DIFFERENT ways over the years. Generally striving for both speed and accuracy. Chances are pretty good I would match SOMEONE, SOMEWHERE, if you look at all the different ways I have done this single term. There are others as well.