forked from MoonstoneLight/Fizbo-Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.cpp
1827 lines (1602 loc) · 59 KB
/
train.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// training set code
#include "chess.h"
#include <math.h>
#include <intrin.h>
#include <immintrin.h>
#include "threads.h"
#include "coeffs.h"
//#include "pgn_utils2.cpp" // CCRL logs
//#include "pgn_utils.cpp" // self-play logs
// ts entry data structure
typedef struct{
short int score; // deep evaluation score.
unsigned char piece[32]; // cell values are: 0=empty, 1=pawn, 2=knight, 3=bishop, 4=rook, 5=queen, 6=king. Plus 8 if black(top bit). So, 0-14. Use 1/2 byte.
unsigned char c1:1,c2:1,c3:1,c4:1,player:1,remarks:3;
// 4: castling possible: c1=white lower(Q), c2=white upper(K), c3=black lower(q), c4=black upper(k). 1=allowed, 0=not allowed.
// 1: player. 0/1 for w/b.
// 3: remarks: 0 is unsolved. 1 is fruit_231 for 3 sec. 2 is sf5 for 3 sec.
unsigned char last_move; // last move made to, for ep captures only
unsigned char fullmoveclock; // can be up to 100 or more - use byte for 0-255
unsigned char dummy[3];
} ts_entry; // 2+32+1*6=40 bytes.
typedef struct {
double mgw;
board b;
short int score_deep;
short int score_shallow;
unsigned char fullmoveclock;
} board_plus;
void convert_TS_to_board(board *b, ts_entry *ts){
unsigned int i;
static const unsigned char r[]={0,65,66,67,68,69,70,0,0,129,130,131,132,133,134};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// first, piece[] values
for(i=0;i<64;i+=2){
unsigned char v=ts->piece[i/2];
b->piece[i]=r[v&15];
b->piece[i+1]=r[v>>4];
}
// second, castling
b->castle=ts->c1;
b->castle+=2*ts->c2;
b->castle+=4*ts->c3;
b->castle+=8*ts->c4;
// third, player
b->player=ts->player+1; // turn 0/1 into 1/2
// fourth, last move
if( ts->last_move==0 || ts->last_move>=64 )
b->last_move=INVALID_LAST_MOVE;
else
b->last_move=(ts->last_move&56)+(b->player==2?2:5);// new format
// FYI - set hmc to 0
b->halfmoveclock=0;
}
static unsigned char convert_board_piece_to_TS_piece(unsigned char v){
unsigned int player=v>>7;// 0/1
return((v&7)+8*player);// 8 for player, 0-6 for piece
}
void convert_board_to_TS(board *b, ts_entry *ts){
unsigned int i;
// first, piece[] values
for(i=0;i<32;i++)// was 64!!!
ts->piece[i]=convert_board_piece_to_TS_piece(b->piece[i*2])+(convert_board_piece_to_TS_piece(b->piece[i*2+1])<<4);
// second, castling
ts->c1=b->castle&1;
ts->c2=(b->castle>>1)&1;
ts->c3=(b->castle>>2)&1;
ts->c4=(b->castle>>3)&1;
// third, player
ts->player=b->player-1; // turn 1/2 into 0/1
// fourth, last move
ts->last_move=b->last_move;
}
static unsigned int piece_count(ts_entry *p){
unsigned int i,j;
unsigned char c;
static const unsigned int pvl[]={0,1,1,1,1,1,1,1};
// count pieces
for(i=j=0;i<32;++i){
c=p->piece[i]&7;
j+=pvl[c];
c=(p->piece[i]>>4)&7;
j+=pvl[c];
}
return(j);
}
static int fmc_p_sort_function(const void *arg1, const void *arg2){
ts_entry *p1=(ts_entry *)arg1,*p2=(ts_entry *)arg2;
unsigned int pc1=piece_count(p1),pc2=piece_count(p2);
// on pieces
if( pc1<pc2 )
return(1);
if( pc1>pc2 )
return(-1);
// tie
return(0);
}
static void export_TS(void){
// load TS
FILE *f1=fopen("c://xde//chess//data//TS.bin","rb");// main TS file
unsigned int i0,i;
fread(&i0,sizeof(unsigned int),1,f1);
ts_entry *ts_all=(ts_entry*)malloc(sizeof(ts_entry)*i0); // storage of ts entries.
fread(ts_all,sizeof(ts_entry),i0,f1);
fclose(f1);
// here positions should be sorted by pieces.
qsort(ts_all,i0,sizeof(ts_entry),fmc_p_sort_function);
// now write summary file
f1=fopen("c://xde//chess//data//TS_summary.csv","w");
fprintf(f1,"pieces,count,c+,c-,c0\n");
unsigned fmc0=32,c=0,cp=0,cm=0,c0=0;
for(i=0;i<i0-5;++i){
if( piece_count(&ts_all[i])*2!=fmc0*2 ){// new line - init
fprintf(f1,"%d,%d,%d,%d,%d\n",fmc0,c,cp,cm,c0);
fmc0=piece_count(&ts_all[i]);
c=cp=cm=c0=0;// reset
}
c++;
if( ts_all[i].score>0 )
cp++;
if( ts_all[i].score<0 )
cm++;
if( ts_all[i].score==0 )
c0++;
}
// print the last one
fprintf(f1,"%d,%d,%d,%d,%d\n",fmc0,c,cp,cm,c0);
fclose(f1);
exit(0);
}
static unsigned char quad(ts_entry *p1){
unsigned int king_q[4]={0,0,0,0},i,kp[2],j;
// set king positions
i=j=0;
do{
UINT64 d=((UINT64*)&p1->piece[i])[0];// 8 cells
d=d&0x6666666666666666; // mask out all but 2 bits for each cell
d=d&(d<<1); // mask in the 2 bits
if(d){
char s=p1->piece[i]&15;
if( (s&7)==6 ){// king found
if( s>7 ) kp[1]=i*2;
else kp[0]=i*2;
j++;// increment king count
}
s=p1->piece[i]>>4;
if( (s&7)==6 ){// king found
if( s>7 ) kp[1]=i*2+1;
else kp[0]=i*2+1;
j++;// increment king count
}
i++;
}else
i+=8;
}while(j<2);// stop when 2 kings are found
if( kp[0]>=32 ) king_q[2]=1;// white king in Q2
else king_q[0]=1;// white king in Q0
if( kp[1]>=32 ) king_q[3]=1;// black king in Q3
else king_q[1]=1;// black king in Q1
return(king_q[0]*8+king_q[1]*4+king_q[2]*2+king_q[3]); // 1 2 4 8
}
static int pos_sort_function_PST(const void *arg1, const void *arg2){
ts_entry *p1=(ts_entry *)arg1;
ts_entry *p2=(ts_entry *)arg2;
unsigned int king_q1=p1->dummy[2];
unsigned int king_q2=p2->dummy[2];
// result
if( king_q1>king_q2 )
return(1);
else if( king_q1<king_q2 )
return(-1);
else
return(0);
}
static int pos_sort_function(const void *arg1, const void *arg2){
ts_entry *p1=(ts_entry *)arg1;
ts_entry *p2=(ts_entry *)arg2;
// first, on ALL pieces
for(unsigned int i=0;i<32;++i){
if( p1->piece[i] > p2->piece[i] )
return(1);
if( p1->piece[i] < p2->piece[i] )
return(-1);
}
// second, on player
if( p1->player > p2->player )
return(1);
if( p1->player < p2->player )
return(-1);
// third, on remarks, decreasing
if( p1->remarks < p2->remarks )
return(1);
if( p1->remarks > p2->remarks )
return(-1);
// tie
return(0);
}
static int pos_sort_function0(const void *arg1, const void *arg2){// no FMC
ts_entry *p1=(ts_entry *)arg1;
ts_entry *p2=(ts_entry *)arg2;
// first, on ALL pieces
for(unsigned int i=0;i<32;++i){
if( p1->piece[i] > p2->piece[i] )
return(1);
if( p1->piece[i] < p2->piece[i] )
return(-1);
}
// second, on player
if( p1->player > p2->player )
return(1);
if( p1->player < p2->player )
return(-1);
// tie
return(0);
}
UINT64 get_hash_from_TS(ts_entry *t){
UINT64 z;
unsigned int i;
// set main hash key.
if( t->player==1 )
z=player_zorb;// apply this for player==black only.
else
z=0;
// cell values are: 0=empty, 1=pawn, 2=knight, 3=bishop, 4=rook, 5=queen, 6=king. Plus 8 if black(top bit). So, 0-14. Use 1/2 byte.
for(i=0;i<32;++i){
if( t->piece[i]&15 )
z^=zorb[(t->piece[i]&7)-1][(t->piece[i]&15)>>3][i];
if( t->piece[i]>>4 )
z^=zorb[((t->piece[i]>>4)&7)-1][t->piece[i]>>7][i];
}
return(z);
}
int fmc_p_sort_function_hash(const void *arg1, const void *arg2){
ts_entry *p1=(ts_entry *)arg1;
ts_entry *p2=(ts_entry *)arg2;
// first, on hash
UINT64 h1=get_hash_from_TS(p1);
UINT64 h2=get_hash_from_TS(p2);
if( h1 < h2 )
return(1);
if( h1 > h2 )
return(-1);
// tie
return(0);
}
static void eliminate_duplicates(void){// eliminate duplicate TS entries. Do not drop anything else.
FILE *f1=fopen("c://xde//chess//data//TSnew.bin","rb");// main TS file
unsigned int i0,i,j;
fread(&i0,sizeof(unsigned int),1,f1);
ts_entry *ts_all=(ts_entry*)malloc(sizeof(ts_entry)*i0); // storage of ts entries.
if( ts_all==NULL ) exit(123);
i0=(unsigned int)fread(ts_all,sizeof(ts_entry),i0,f1);
fclose(f1);
// sort TS by position+player+remarks (decr)
qsort(ts_all,i0,sizeof(ts_entry),pos_sort_function);
// pass over it and mark dups as dummy[0]=127
//f1=fopen("c://xde//chess//out//dups.csv","w");
//fprintf(f1,"FEN,remarks,player,score,i,dup_c\n");
ts_all[0].dummy[0]=0;
unsigned int last_deleted=1000000000,dup_c=0;
for(i=1;i<i0;++i){
if( ts_all[i].remarks<2 || pos_sort_function0(ts_all+i-1,ts_all+i)==0 ){// dups at i-1 and i. Keep i-1, mark i for deletion.
//char sss[200];
// keep this one
/*if( last_deleted!=i-1 ){// do not display twice
convert_TS_to_board(&b_m,ts_all+i-1);
j=print_position(sss,&b_m);
sss[j-1]=0;
if( last_deleted!=100000000 )
fprintf(f1,"\n"); // new line
fprintf(f1,"%s,%d,%d,%d,%d,%d\n",sss,ts_all[i-1].remarks,ts_all[i-1].player,ts_all[i-1].score,i-1,dup_c);
}*/
// delete this one
dup_c++;
ts_all[i].dummy[0]=127;// mark i for deletion
/*last_deleted=i;
convert_TS_to_board(&b_m,ts_all+i);
j=print_position(sss,&b_m);
sss[j-1]=0;
fprintf(f1,"%s,%d,%d,%d,%d,%d\n",sss,ts_all[i].remarks,ts_all[i].player,ts_all[i].score,i,dup_c);
*/
}else
ts_all[i].dummy[0]=0;
}
//fclose(f1);
// mark more items for deletion: remarks=0
/*for(i=0;i<i0;++i){
if( abs(ts_all[i].remarks)==0 ){
ts_all[i].dummy[0]=127;
continue;
}
convert_TS_to_board(&b_m,ts_all+i);
set_bitboards(&b_m);
if( popcnt64l(b_m.colorBB[0]|b_m.colorBB[1])<7 || popcnt64l(b_m.colorBB[0])<2 || popcnt64l(b_m.colorBB[1])<2 )// exclude <7 pieces, or highly assimetric positions.
ts_all[i].dummy[0]=127;
}*/
// eliminate dups
for(j=i=0;i<i0;++i){
if( ts_all[i].dummy[0]==127 ){// dup at i. Replace it with [j], and inc j
ts_all[i]=ts_all[j];
j++;
}
}
i=i0-j; // new size - drop j elements
ts_all+=j; // new start - drop j elements
// sort TS by hash - to simulate random sorting.
int k,l,j0=j;
for(j=k=l=0;j<(int)i;++j){
int q=quad(ts_all+j); // populate king quadrant
ts_all[j].dummy[2]=q;
if( q<k ) l=1; // out of order - sort needed
k=q; //save as previous result
}
if( l ) qsort(ts_all,i,sizeof(ts_entry),pos_sort_function_PST);// sort TS by castling - so that PST does not change (much) between positions, and parallel search will work well.
qsort(ts_all,i,sizeof(ts_entry),fmc_p_sort_function_hash);
j=j0;//restore
// save it
f1=fopen("c://xde//chess//data//TSnew2.bin","wb");// main TS file
fwrite(&i,sizeof(unsigned int),1,f1); // write size - i
fwrite(ts_all,sizeof(ts_entry),i,f1); // data - i
fclose(f1);
// display results
char res[300];
sprintf(res,"Deleted %d records. New count is %d records.",j,i);
MessageBox( hWnd_global, res,TEXT("Training Set Dups"),MB_ICONERROR | MB_OK );
exit(0);
}
typedef struct {
double w1,w2;
unsigned char piece[64];// board pieces
short int resid; // residual score, +-5000
} b3t;
_declspec(thread) unsigned int use_hash;
_declspec(thread) unsigned int eval_counter; // count of eval calls
_declspec(thread) board_light *eval_b; // evaluation boards
_declspec(thread) short int *eval_score; // evaluation scores
_declspec(thread) int pawn_deriv_coeffs[330]; // here increase size if a lot of pawn coeffs are used. Keep it small to avoid long reset times.
static ts_entry *ts_all;
static unsigned int pos_count0,ii,pos_count1,*coeff_count,pos_cnt_2,pos_cnt;
static double *coeffs,*derivT1,sumsq2_2,sumsq2,d1,d2,*dir;
int ka[2];
static unsigned int get_mat_key2(board *b){// lower of B or W
unsigned int wN,bN,wB,bB,wR,bR,wQ,bQ,wP,bP,i,j;
wP=(unsigned int)popcnt64l(b->piececolorBB[0][0]);
bP=(unsigned int)popcnt64l(b->piececolorBB[0][1]);
wN=(unsigned int)popcnt64l(b->piececolorBB[1][0]);
bN=(unsigned int)popcnt64l(b->piececolorBB[1][1]);
wB=(unsigned int)popcnt64l(b->piececolorBB[2][0]);
bB=(unsigned int)popcnt64l(b->piececolorBB[2][1]);
wR=(unsigned int)popcnt64l(b->piececolorBB[3][0]);
bR=(unsigned int)popcnt64l(b->piececolorBB[3][1]);
wQ=(unsigned int)popcnt64l(b->piececolorBB[4][0]);
bQ=(unsigned int)popcnt64l(b->piececolorBB[4][1]);
i=wP*mat_key_mult[0]+bP*mat_key_mult[1]
+wN*mat_key_mult[2]+bN*mat_key_mult[3]
+wB*mat_key_mult[4]+bB*mat_key_mult[5]
+wR*mat_key_mult[6]+bR*mat_key_mult[7]
+wQ*mat_key_mult[8]+bQ*mat_key_mult[9];
j=bP*mat_key_mult[0]+wP*mat_key_mult[1]
+bN*mat_key_mult[2]+wN*mat_key_mult[3]
+bB*mat_key_mult[4]+wB*mat_key_mult[5]
+bR*mat_key_mult[6]+wR*mat_key_mult[7]
+bQ*mat_key_mult[8]+wQ*mat_key_mult[9];
return(min(i,j));
}
typedef struct {
unsigned int a[10];
int mat;
} ddd2;
void decomp_key(unsigned int key,ddd2* dd){
// decompose key into components
unsigned int ii=key;
dd->a[0]=ii/26244;//bP
ii-=dd->a[0]*26244;
dd->a[1]=ii/2916;//wP
ii-=dd->a[1]*2916;
dd->a[2]=ii/1458;//bQ
ii-=dd->a[2]*1458;
dd->a[3]=ii/729;//wQ
ii-=dd->a[3]*729;
dd->a[4]=ii/243;//bR
ii-=dd->a[4]*243;
dd->a[5]=ii/81;//wR
ii-=dd->a[5]*81;
dd->a[6]=ii/27;//bB
ii-=dd->a[6]*27;
dd->a[7]=ii/9;//wB
ii-=dd->a[7]*9;
dd->a[8]=ii/3;//bN
ii-=dd->a[8]*3;
dd->a[9]=ii/1;//wN
ii-=dd->a[9]*1;
// get mat diff
dd->mat=dd->a[1]-dd->a[0]+9*(dd->a[3]-dd->a[2])+5*(dd->a[5]-dd->a[4])+3*(dd->a[7]-dd->a[6])+3*(dd->a[9]-dd->a[8]);
}
extern unsigned short int index_pawn[];
extern unsigned short int convert2_3[];
extern unsigned short int *ind1;
static void analyze(void){
// load training set
FILE *f=fopen("c://xde//chess//data//TSn2.bin","rb");// main TS file, after Qs. Now just call eval on it.
fread(&pos_count0,sizeof(unsigned int),1,f);
ts_all=(ts_entry*)malloc(sizeof(ts_entry)*pos_count0); // storage of ts entries - 40 bytes each.
pos_count0=(unsigned int)fread(ts_all,sizeof(ts_entry),pos_count0,f);
fclose(f);
// loop over all entries
UINT64 ss=1000*64;
unsigned long bit;
unsigned int ii,i,index,bit2;
unsigned int *cc=(unsigned int*)malloc(ss*sizeof(unsigned int));
memset(cc,0,(ss/2)*sizeof(unsigned int));
memset(&cc[ss/2],0,(ss/2)*sizeof(unsigned int));
for(ii=0;ii<pos_count0;++ii){
board_plus ts_b;
// set board
convert_TS_to_board(&ts_b.b,ts_all+ii);
// set bitboards
set_bitboards(&ts_b.b);
// set king positions
_BitScanForward64(&bit,ts_b.b.piececolorBB[5][0]);ts_b.b.kp[0]=(unsigned char)bit;// white
_BitScanForward64(&bit,ts_b.b.piececolorBB[5][1]);ts_b.b.kp[1]=(unsigned char)bit;// black
// skip some positions.
if( popcnt64l(ts_b.b.piececolorBB[4][0])>1 || popcnt64l(ts_b.b.piececolorBB[4][1])>1 )// skip if >1 Q
continue;
if( popcnt64l(ts_b.b.piececolorBB[1][0])>2 || popcnt64l(ts_b.b.piececolorBB[1][1])>2 )// skip if >2 N
continue;
if( popcnt64l(ts_b.b.piececolorBB[2][0])>2 || popcnt64l(ts_b.b.piececolorBB[2][1])>2 )// skip if >2 B
continue;
if( popcnt64l(ts_b.b.piececolorBB[3][0])>2 || popcnt64l(ts_b.b.piececolorBB[3][1])>2 )// skip if >2 R
continue;
unsigned int j=(unsigned int)popcnt64l(ts_b.b.colorBB[0]|ts_b.b.colorBB[1]);
if( j<=5 )// skip if 5 or fewer pieces
continue;
board *b=&ts_b.b;
// get list of pawns
unsigned int sq[2][8],sq_cnt[2]; //sq: list of all pawns.
UINT64 w_bb=b->piececolorBB[0][0];//white pawn
UINT64 b_bb=flip_color(b->piececolorBB[0][1]);//black pawn, flipped so that it is from point of view of white
sq_cnt[0]=0;
while( w_bb ){// white
GET_BIT(w_bb)
sq[0][sq_cnt[0]++]=bit; // record position of this pawn
}
sq_cnt[1]=0;
while( b_bb ){// black
GET_BIT(b_bb)
sq[1][sq_cnt[1]++]=bit; // record position of this pawn - in flipped format(white's point of view)
}
// my pawn vs my pawn - new index development logic, with look-up table.
unsigned int i1,i2,f;
for(i=0;i+1<sq_cnt[0];++i){
bit=sq[0][i];
for(j=i+1;j<sq_cnt[0];++j){// loop over P2, P2>P1
bit2=sq[0][j];
index=index_pawn[bit*64+bit2]; // 0-695
i1=min(bit,bit2)+max(bit,bit2)*(max(bit,bit2)-1)/2;
i2=min(flips[bit][2],flips[bit2][2])+max(flips[bit][2],flips[bit2][2])*(max(flips[bit][2],flips[bit2][2])-1)/2;
if( i2<i1 ) f=2;else f=0; // flip
// look at friendly king
i1=flips[ts_b.b.kp[0]][f];
index=index*64+i1; //
cc[index]++;
}
}
for(i=0;i+1<sq_cnt[1];++i){// all bits are already from white's POV
bit=sq[1][i];
for(j=i+1;j<sq_cnt[1];++j){// loop over P2, P2>P1
bit2=sq[1][j];
index=index_pawn[bit*64+bit2]; // 0-695
i1=min(bit,bit2)+max(bit,bit2)*(max(bit,bit2)-1)/2;
i2=min(flips[bit][2],flips[bit2][2])+max(flips[bit][2],flips[bit2][2])*(max(flips[bit][2],flips[bit2][2])-1)/2;
if( i2<i1 ) f=3;else f=1; // flip
// look at friendly king
i1=flips[ts_b.b.kp[1]][f];
index=index*64+i1; //
cc[index]++;
}
}
}
// save it, for review only
f=fopen("c://xde//chess//out//tt.csv","w");
fprintf(f,"key,k1,k2,count\n");
for(i=0;i<ss;++i){
if( cc[i]==0 ) continue;
fprintf(f,"%u,%u,%u,%u\n",i,(i%65536),(i/65536),cc[i]);
}
fclose(f);
exit(0);
}
#define coeff_num (12+672+576*2+24*64*10*2+2048*2+3321*2) // number of regression coefficients. 43,294
static Spinlock lll; // spinlock
typedef struct {
float d1; // d o / d eval
float d2; // d2 o / d eval / d eval
float eval;
float A;
} regr_t;
typedef struct {
float eval0;
float nn;
float deep;
float egw;
} regr2_t;
UINT64 d1d_o; // offset into data
UINT64 *d1i; // offset to start
unsigned int *d1s; // size of each entry
short unsigned int *d1da;
unsigned char *d1db;
regr_t *rd; // regression data
regr2_t *rd2; // regression data v2
unsigned int pass;
static int __cdecl c3(const void *key, const void *datum){
if( *((unsigned int*)key)==*((unsigned int*)datum) )
return(0);
else if( *((unsigned int*)key)<*((unsigned int*)datum) )
return(-1);
else
return(1);
}
#define obj_type 0 // 0=square of actual-expected scores; 1=cross-entropy
void obj(double s0,double deep,double *res){// objective function. Return o, o', o''.
#if obj_type==0 // o=(A-S)^X
double z=s0/173.72;
double ez=exp(-z);
double E=1./(1.+ez); // expected score, sigmoid, based on 10^-x/400. Range is 0 to 1.
double y=(min(1,max(-1,deep))+1)/2; // 0-0.5-1
res[0]=100000000.*(E-y)*(E-y); // o
res[1]=100000000./173.72*2.*(E-y)*E*(1.-E); // o'
res[2]=100000000./173.72/173.72*2.*E*(1.-E)*(2.*E-3.*E*E-y+2.*E*y); // o''
#endif
#if obj_type==1 // o=-y*ln(a)-(1-y)*ln(1-a)=ln(1+exp(-z))+z*(1-y)
double z=s0/173.72;
double y=(min(1,max(-1,deep))+1)/2; // 0-0.5-1
double ez=exp(-z);
double v=log(1+ez)+z*(1-y);
double E=1./(1.+ez);
res[0]=10000000.*v; // o
res[1]=10000000./173.72*(E-y); // o'
res[2]=10000000./173.72/173.72*E*(1.-E); // o''
#endif
}
static unsigned int process_position(unsigned int i,double *d,unsigned int *ind2l){// return number of coeffs. And board_plus, by pointer.
board_plus ts_b;
DWORD bit;
unsigned int j,k,coeff_num2,ind[1000];
// skip unsolved. This comes into effect.
if( ts_all[i].remarks<2 ) return(0);
// set board
convert_TS_to_board(&ts_b.b,ts_all+i);
// set bitboards
set_bitboards(&ts_b.b);
// Get midgame weight
ts_b.mgw=1.-endgame_weight_all_i[get_piece_value(&ts_b.b)]/1024.;
// save score into TS
ts_b.score_deep=ts_all[i].score;
// set king positions
_BitScanForward64(&bit,ts_b.b.piececolorBB[5][0]);ts_b.b.kp[0]=(unsigned char)bit;// white
_BitScanForward64(&bit,ts_b.b.piececolorBB[5][1]);ts_b.b.kp[1]=(unsigned char)bit;// black
if( pass&1 ){
ts_b.score_shallow=(short)rd[i].eval; // restore
}else{
// init PST scores
get_scores(&ts_b.b);
// get material key
ts_b.b.mat_key=get_mat_key(&ts_b.b);
// get pawn hash. For first pass only.
if( pass==0) ts_b.b.pawn_hash_key=get_pawn_hash_key(&ts_b.b); // this is currently needed. But why?
// init search
eval_counter=0; // reset count
use_hash=1; // has to be 1.
ts_b.b.em_break=0; // reset
ts_b.b.slave_index=0;
ts_b.score_shallow=eval(&ts_b.b); // call eval directly
rd[i].eval=ts_b.score_shallow; //save
}
if( abs(ts_b.score_shallow)>2000 ) // skip if search sees a checkmate. This comes into effect.
return(0);
// skip some positions.
if( popcnt64l(ts_b.b.colorBB[0]|ts_b.b.colorBB[1])<=5 )// skip if 5 or fewer pieces
return(0);
if( fabs(ts_b.mgw)<0.001 ) ts_b.mgw=0.; // "round" to 0.001
double w1=ts_b.mgw*(ts_b.b.player==1?1.:-1.);// midgame weight, accounting for side to move
double w2=(1.-ts_b.mgw)*(ts_b.b.player==1?1.:-1.);// endgame weight, accounting for side to move
double w12=(ts_b.b.player==1?1.:-1.);// +-1, accounting for side to move
// use coeffs as already calculated on first iteration
if( pass ){
unsigned int cn3=d1s[i],offset=0;
UINT64 o1=d1i[i];
for(j=0;j<cn3;++j){
if( j && d1da[o1+j]<d1da[o1+j-1] )// if index decreases, add 2^16 to offset
offset+=65536;
ind2l[j]=((unsigned int)d1da[o1+j])+offset;
double v;
unsigned int x=d1db[o1+j];
if( x<49 ){
// get type: w1/w2/w3
unsigned int x3=(x-1)%3;
if( x3==0 ) v=w1;
else if( x3==1 ) v=w2;
else v=w12;
// get multiple: +-1-8
x3=(x-1)/3;
static const int m[]={1,-1,2,-2,3,-3,4,-4,5,-5,6,-6,7,-7,8,-8};
v*=m[x3];
}else if( x<24+58 )// KS, positive
v=w12/32.*(x-24-25);
else// KS, negative
v=-w12/32.*(x-24-58);
d[j]=v;
}
return(cn3);
}
// first pass - format true score
rd[i].A=ts_b.score_deep;
memset(pawn_deriv_coeffs,0,sizeof(pawn_deriv_coeffs));// reset: 330*4 bytes
use_hash=0;
pawn_score(&ts_b.b);//*********************************************************************************************************************************************
use_hash=1;
j=0;// coeff counter - init
// material
for(k=0;k<6;++k){
int cc=int(popcnt64l(ts_b.b.piececolorBB[k][0]))-int(popcnt64l(ts_b.b.piececolorBB[k][1]));
d[j+k]=w1*cc;
d[j+k+6]=w2*cc;
}
j+=12;
// isolated pawn
d[j++]=pawn_deriv_coeffs[1]*w1;
d[j++]=pawn_deriv_coeffs[1]*w2;
// passed pawns, on ranks 2-6=5*2=10, m+e
for(k=0;k<5;++k){
d[j+k*2]=pawn_deriv_coeffs[2+k]*w1;// midgame
d[j+k*2+1]=pawn_deriv_coeffs[2+k]*w2;// endgame
}
j+=10;
// passed protected pawn
d[j++]=pawn_deriv_coeffs[8]*w1;
d[j++]=pawn_deriv_coeffs[8]*w2;
//knight PST, 32 cells, mid+end.
UINT64 bb;
bb=ts_b.b.piececolorBB[1][0];
while( bb ){// loop over white knights
GET_BIT(bb)
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]+=w1;
d[j+bit+32]+=w2;
}
bb=ts_b.b.piececolorBB[1][1];
while( bb ){// loop over black knights
GET_BIT(bb)
bit=(7-(bit&7))+(bit&56);// convert bit to black format, flip 0-7
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]-=w1;
d[j+bit+32]-=w2;
}
j+=64;
//bishop PST, 32 cells, mid+end.
bb=ts_b.b.piececolorBB[2][0];
while( bb ){// loop over white bishops
GET_BIT(bb)
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]+=w1;
d[j+bit+32]+=w2;
}
bb=ts_b.b.piececolorBB[2][1];
while( bb ){// loop over black bishops
GET_BIT(bb)
bit=(7-(bit&7))+(bit&56);// convert bit to black format, flip 0-7
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]-=w1;
d[j+bit+32]-=w2;
}
j+=64;
//rook PST, 32 cells, mid+end.
bb=ts_b.b.piececolorBB[3][0];
while( bb ){// loop over white rooks
GET_BIT(bb)
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]+=w1;
d[j+bit+32]+=w2;
}
bb=ts_b.b.piececolorBB[3][1];
while( bb ){// loop over black rooks
GET_BIT(bb)
bit=(7-(bit&7))+(bit&56);// convert bit to black format, flip 0-7
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]-=w1;
d[j+bit+32]-=w2;
}
j+=64;
//queen PST, 32 cells, mid+end.
bb=ts_b.b.piececolorBB[4][0];
while( bb ){// loop over white queens
GET_BIT(bb)
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]+=w1;
d[j+bit+32]+=w2;
}
bb=ts_b.b.piececolorBB[4][1];
while( bb ){// loop over black queens
GET_BIT(bb)
bit=(7-(bit&7))+(bit&56);// convert bit to black format, flip 0-7
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]-=w1;
d[j+bit+32]-=w2;
}
j+=64;
//king PST, 32 cells, mid+end.
bit=ts_b.b.kp[0]; // white king
if( bit>=32 ) bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
if( (bit&7)<3 ) // only apply midgame for ranks 1-3
d[j+bit]+=w1;
else // put the rest in cell 4
d[j+4]+=w1;
d[j+bit+32]+=w2;
bit=ts_b.b.kp[1]; // black king
bit=(7-(bit&7))+(bit&56);// convert bit to black format, flip 0-7
if( bit>=32 ) bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
if( (bit&7)<3 ) // only apply midgame for ranks 1-3
d[j+bit]-=w1;
else // put the rest in cell 4
d[j+4]-=w1;
d[j+bit+32]-=w2;
j+=64;
//pawn PST, 32 cells, mid+1/2 mid noking+end.
bb=ts_b.b.piececolorBB[0][0];
while( bb ){// loop over white pawns
GET_BIT(bb)
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]+=w1;
d[j+bit+32]+=w2;
}
bb=ts_b.b.piececolorBB[0][1];
while( bb ){// loop over black pawns
GET_BIT(bb)
bit=(7-(bit&7))+(bit&56);// convert bit to white format, flip 0-7
if( bit>=32 )
bit=(bit&7)+((7-(bit>>3))<<3);// turn into 0-31
d[j+bit]-=w1;
d[j+bit+32]-=w2;
}
j+=64;// mid+end
// 23-28=candidate passed pawns, on ranks 2-6. 5 of them.
for(k=0;k<5;++k){
d[j+k*2]=pawn_deriv_coeffs[23+k]*w1;// midgame
d[j+k*2+1]=pawn_deriv_coeffs[23+k]*w2;// endgame
}
j+=5*2;
// insert additional pawn coeff code here**********************************************************************************************************************************
int pawn_deriv_coeffs_l[330];
memcpy(pawn_deriv_coeffs_l,pawn_deriv_coeffs,sizeof(pawn_deriv_coeffs));// save: 330*4 bytes
// get eval coeffs
memset(pawn_deriv_coeffs,0,sizeof(pawn_deriv_coeffs));// reset: 330*4 bytes
use_hash=0;
int score=eval(&ts_b.b);// eval coeffs************************************************************************************************************************************************
use_hash=1;
// knight mob, 9 of them.
for(k=0;k<9;++k){
d[j+k*2]+=pawn_deriv_coeffs[0+k]*w1;// midgame.
d[j+k*2+1]+=pawn_deriv_coeffs[0+k]*w2;// endgame.
}
j+=9*2;
// bishop mob, 14 of them.
for(k=0;k<14;++k){
d[j+k*2]+=pawn_deriv_coeffs[9+k]*w1;// midgame.
d[j+k*2+1]+=pawn_deriv_coeffs[9+k]*w2;// endgame.
}
j+=14*2;
// rook mob, 15 of them.
for(k=0;k<15;++k){
d[j+k*2]+=pawn_deriv_coeffs[23+k]*w1;// midgame.
d[j+k*2+1]+=pawn_deriv_coeffs[23+k]*w2;// endgame.
}
j+=15*2;
// queen mob, 28 of them.
for(k=0;k<28;++k){
d[j+k*2]+=pawn_deriv_coeffs[38+k]*w1;// midgame.
d[j+k*2+1]+=pawn_deriv_coeffs[38+k]*w2;// endgame.
}
j+=28*2;
// knight outposts, 22 of them. 11 base, 11 bonuses for no opp minors.
for(k=0;k<22;++k) d[j++]=pawn_deriv_coeffs[230+k]*w1; // midgame only
// rook pin - 1+1. Midgame only
d[j++]=pawn_deriv_coeffs[88]*w1; // Q
d[j++]=pawn_deriv_coeffs[183]*w1; // K
// king safety - 62. Turn them into 13 - 1 every 5, interpolate in between.
static const unsigned int kstr[]={0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9,10,10,10,10,10,11,11,11,11,11,11,12};
for(k=0;k<62;++k) d[j+kstr[k]]+=pawn_deriv_coeffs[99+k]*w12/32.;// always need /32 here.Apply to both mid and end game
j+=62;
// kings close to passed pawn (16)
for(k=0;k<16;++k) d[j++]=pawn_deriv_coeffs[161+k]*w12;
// unstoppable passed pawn (1)
d[j++]=pawn_deriv_coeffs[177]*w12;
// penalty for no pawns (1)
d[j++]=((ts_b.b.piececolorBB[0][1]==0)-(ts_b.b.piececolorBB[0][0]==0))*w12;
// penalty for no mating potential in pieces (1)
if( !(ts_b.b.piececolorBB[3][0]|ts_b.b.piececolorBB[4][0]) && ( !ts_b.b.piececolorBB[2][0] || popcnt64l(ts_b.b.piececolorBB[2][0]|ts_b.b.piececolorBB[1][0])<2 ) ) d[j]-=w12;
if( !(ts_b.b.piececolorBB[3][1]|ts_b.b.piececolorBB[4][1]) && ( !ts_b.b.piececolorBB[2][1] || popcnt64l(ts_b.b.piececolorBB[2][1]|ts_b.b.piececolorBB[1][1])<2 ) ) d[j]+=w12;
j++;
// bonus for bishop pair (1)
d[j++]+=((popcnt64l(ts_b.b.piececolorBB[2][0])>1)-(popcnt64l(ts_b.b.piececolorBB[2][1])>1))*w12;
// bishop pin - 1+1. (4)
d[j++]=pawn_deriv_coeffs[184]*w1; // Q
d[j++]=pawn_deriv_coeffs[184]*w2; // Q
d[j++]=pawn_deriv_coeffs[185]*w1; // K
d[j++]=pawn_deriv_coeffs[185]*w2; // K
// kings close to passed pawn (16)
for(k=0;k<16;++k) d[j++]+=pawn_deriv_coeffs[186+k]*w12;
// king mob=4
for(k=0;k<4;++k) d[j++]+=pawn_deriv_coeffs[203+k]*w2;
// rook protecting other rook
d[j++]=pawn_deriv_coeffs[207]*w1;
d[j++]=pawn_deriv_coeffs[207]*w2;
// get a list of non-empty coeffs******************************************************************
coeff_num2=0;// loop over 746 coeffs
for(UINT64 kk=0;kk<j;++kk){// this is slightly faster than conditional expression. Using UINT64 helps. This is the slowest part.
ind[coeff_num2]=(unsigned int)kk;
coeff_num2+=((((unsigned int*)d)[kk*2+1]&0x7ff00000)!=0);
}
// large pattern #1: my pawns vs my pawns
bb=ts_b.b.piececolorBB[0][0];
unsigned long bit2;
while( bb ){// loop over P1
GET_BIT(bb)
UINT64 bb2=bb;
while( bb2 ){// loop over P2, P2>P1
GET_BIT2(bb2)
unsigned int index=index_pawn[bit*64+bit2];
assert(index<576);
d[j+index*2]+=w1;
d[j+index*2+1]+=w2;
ind[coeff_num2++]=j+index*2;
ind[coeff_num2++]=j+index*2+1;
}
}
bb=ts_b.b.piececolorBB[0][1];
while( bb ){// loop over P1
GET_BIT(bb)
bit=flips[bit][1]; // change from black to white
UINT64 bb2=bb;
while( bb2 ){// loop over P2, P2>P1
GET_BIT2(bb2)
bit2=flips[bit2][1]; // change from black to white
unsigned int index=index_pawn[bit*64+bit2];
assert(index<576);
d[j+index*2]-=w1;