-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrxndb.cpp
4378 lines (3722 loc) · 104 KB
/
rxndb.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
// Please see license.txt for licensing and copyright information //
// Author: Paul Zimmerman, University of Michigan //
#include "rxndb.h"
using namespace std;
///NOTE: now 60kT
#define USE_KNNR 1
#define KNNR_K 4
#define DO_NOT_ADD_DUPLICATES 1
#define TEST_GA 1
#define MAX_N 1000
//for KNNR -- 686 for 1b?
#define TRAINSHIFT 15.0
#define PTHRESH0 0.4
#define LAMBDA 0.00
//no evidence NULL_ATOMS is helpful
#define NULL_ATOMS 0
//GA: need to implement pthresh calc
//GA: replace bad features with A-B connections?
//NOTE: charges on attaching atoms OFF
void RXNDB::make_decision_tree()
{
if (ga_inited<1) init_ga(1);
printf("\n creating simple decision tree \n\n");
naddbrktypes = 12; //a1, b1, a2, b2, a1b1, a2b1, a1b2, a2b2, a3b2, a2b3, a3b3, axbx
if (addbrk_layer!=NULL) delete [] addbrk_layer;
addbrk_layer = new double[naddbrktypes];
for (int i=0;i<naddbrktypes;i++) addbrk_layer[i] = 0.;
if (allow_add_brk_elem!=NULL) delete [] allow_add_brk_elem;
allow_add_brk_elem = new double[nelemf*nelemf];
for (int i=0;i<nelemf*nelemf;i++) allow_add_brk_elem[i] = 0.;
int maxc = 1;
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
int c = climit_h[e1];
if (c>maxc)
maxc = c;
}
maxc++;
double* weightc = new double[nelemf*maxc];
double* weightp = new double[nelemf*nelemf];
for (int i=0;i<nelemf*maxc;i++) weightc[i] = 0.;
for (int i=0;i<nelemf*nelemf;i++) weightp[i] = 0.;
double* weightaddc = new double[nelemf*maxc];
double* weightpadd = new double[nelemf*nelemf];
double* weightbrkc = new double[nelemf*maxc];
double* weightpbrk = new double[nelemf*nelemf];
for (int i=0;i<nelemf*maxc;i++) weightaddc[i] = 0.;
for (int i=0;i<nelemf*nelemf;i++) weightpadd[i] = 0.;
for (int i=0;i<nelemf*maxc;i++) weightbrkc[i] = 0.;
for (int i=0;i<nelemf*nelemf;i++) weightpbrk[i] = 0.;
int naddmax = 0;
int nbrkmax = 0;
for (int i=0;i<N;i++)
{
int wr = i;
int natoms1 = natoms[wr];
string* anames1 = anames[wr];
int* anumbers1 = anumbers[wr];
int* coordnr1 = coordnr[wr];
double* xyzr1 = xyzr[wr];
int nadd1 = nadd[wr];
int* add1 = add[wr];
int nbrks1 = nbrks[wr];
int* brks1 = brks[wr];
if (naddmax<nadd1) naddmax = nadd1;
if (nbrkmax<nbrks1) nbrkmax = nbrks1;
double val = Pr[i];
int abtype = get_abtype(i,nadd1,nbrks1);
//nadd/nbrk possible
addbrk_layer[abtype] += val;
if (abtype==11 && val>0.5) printf(" big mover (add: %i brk: %i): %4i/%4i \n",nadd1,nbrks1,i,ids[i]);
//printf(" i: %i Pr: %4.3f \n",i,val);
//add/brk possible at element combinations
for (int j=0;j<nadd1;j++)
{
int a1 = add1[2*j+0];
int a2 = add1[2*j+1];
int e1 = anumbers1[a1];
int e2 = anumbers1[a2];
int er1 = remap[e1];
int er2 = remap[e2];
int c1 = coordnr1[a1];
int c2 = coordnr1[a2];
weightc[er1*maxc+c1] += val; weightc[er2*maxc+c2] += val;
weightp[er1*nelemf+er2] += val;
if (er1!=er2)
weightp[er2*nelemf+er1] += val;
weightaddc[er1*maxc+c1] += val; weightaddc[er2*maxc+c2] += val;
weightpadd[er1*nelemf+er2] += val;
if (er1!=er2)
weightpadd[er2*nelemf+er1] += val;
}
for (int j=0;j<nbrks1;j++)
{
int b1 = brks1[2*j+0];
int b2 = brks1[2*j+1];
int e1 = anumbers1[b1];
int e2 = anumbers1[b2];
int er1 = remap[e1];
int er2 = remap[e2];
int c1 = coordnr1[b1];
int c2 = coordnr1[b2];
weightc[er1*maxc+c1] += val; weightc[er2*maxc+c2] += val;
weightp[er1*nelemf+er2] += val;
if (er1!=er2)
weightp[er2*nelemf+er1] += val;
weightbrkc[er1*maxc+c1] += val; weightbrkc[er2*maxc+c2] += val;
weightpbrk[er1*nelemf+er2] += val;
if (er1!=er2)
weightpbrk[er2*nelemf+er1] += val;
}
} //loop i over N data points
for (int i=0;i<nelemf*maxc;i++) weightc[i] = weightc[i] / N;
for (int i=0;i<nelemf*nelemf;i++) weightp[i] = weightp[i] / N;
for (int i=0;i<nelemf*maxc;i++) weightaddc[i] = weightaddc[i] / N;
for (int i=0;i<nelemf*nelemf;i++) weightpadd[i] = weightpadd[i] / N;
for (int i=0;i<nelemf*maxc;i++) weightbrkc[i] = weightbrkc[i] / N;
for (int i=0;i<nelemf*nelemf;i++) weightpbrk[i] = weightpbrk[i] / N;
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam = PTable::atom_name(e1);
for (int j=climit_l[e1];j<climit_h[e1]+1;j++)
printf(" element %s with coordn %i has weights: %4.3f %4.3f %4.3f \n",anam.c_str(),j,weightc[i*maxc+j],weightaddc[i*maxc+j],weightbrkc[i*maxc+j]);
}
printf("\n pair weights: \n ");
for (int i=0;i<nelemf;i++)
printf(" %5s",PTable::atom_name(emap[i]).c_str());
printf("\n");
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam1 = PTable::atom_name(e1);
printf(" %2s:",anam1.c_str());
for (int j=0;j<=i;j++)
{
//int e2 = emap[j];
//string anam2 = PTable::atom_name(e2);
printf(" %4.3f",weightp[i*nelemf+j]);
}
printf("\n");
}
printf("\n add weights: \n ");
for (int i=0;i<nelemf;i++)
printf(" %5s",PTable::atom_name(emap[i]).c_str());
printf("\n");
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam1 = PTable::atom_name(e1);
printf(" %2s:",anam1.c_str());
for (int j=0;j<=i;j++)
{
//int e2 = emap[j];
//string anam2 = PTable::atom_name(e2);
printf(" %4.3f",weightpadd[i*nelemf+j]);
}
printf("\n");
}
printf("\n break weights: \n ");
for (int i=0;i<nelemf;i++)
printf(" %5s",PTable::atom_name(emap[i]).c_str());
printf("\n");
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam1 = PTable::atom_name(e1);
printf(" %2s:",anam1.c_str());
for (int j=0;j<=i;j++)
{
//int e2 = emap[j];
//string anam2 = PTable::atom_name(e2);
printf(" %4.3f",weightpbrk[i*nelemf+j]);
}
printf("\n");
}
for (int i=0;i<nelemf;i++)
for (int j=0;j<nelemf;j++)
if (weightp[i*nelemf+j]>0.001) //CPMZ parameter
allow_add_brk_elem[i*nelemf+j] = 1.0;
printf("\n allow_add_brk_elem: \n ");
for (int i=0;i<nelemf;i++)
printf(" %s",PTable::atom_name(emap[i]).c_str());
printf("\n");
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam1 = PTable::atom_name(e1);
printf(" %2s:",anam1.c_str());
for (int j=0;j<=i;j++)
printf(" %1.0f",allow_add_brk_elem[i*nelemf+j]);
printf("\n");
}
printf("\n add1 brk1 add2 brk2 a1b1 a2b1 a1b2 a2b2 a3b2 a2b3 a3a3 axbx \n");
printf(" addbrk_layer:");
for (int i=0;i<naddbrktypes;i++)
printf(" %7.3f",addbrk_layer[i]);
printf("\n");
delete [] weightc;
delete [] weightp;
delete [] weightaddc;
delete [] weightpadd;
delete [] weightbrkc;
delete [] weightpbrk;
make_combo_tree(naddmax,nbrkmax);
printf("\n\n\n testing the Trees \n");
//test trees
int npass = 0;
for (int i=0;i<N;i++)
{
int wr = i;
int natoms1 = natoms[wr];
string* anames1 = anames[wr];
int* anumbers1 = anumbers[wr];
int* coordnr1 = coordnr[wr];
double* xyzr1 = xyzr[wr];
int nadd1 = nadd[wr];
int* add1 = add[wr];
int nbrks1 = nbrks[wr];
int* brks1 = brks[wr];
double* qrc1 = qrc[wr];
double val = Pr[wr];
int abtype = get_abtype(wr,nadd1,nbrks1);
int tree0 = tree_screen_0(nadd1,nbrks1);
double tree1 = tree_screen_1(natoms1,anames1,anumbers1,xyzr1,nadd1,add1,nbrks1,brks1,qrc1);
double tree2 = tree_screen_2(natoms1,anames1,anumbers1,coordnr1,xyzr1,nadd1,add1,nbrks1,brks1,qrc1);
printf(" rxn %i (ids: %4i nadd: %i nbrks: %i) tree0: %i tree1: %4.3f tree2: %4.3f \n",wr,ids[wr],nadd1,nbrks1,tree0,tree1,tree2);
if (tree0 && tree1 > 0.01 && tree2 > 0.01) npass++;
}
printf("\n pass: %i of %i fraction: %4.3f \n",npass,N,1.*npass/N);
printf("\n ending early ! \n");
exit(1);
return;
}
void RXNDB::make_combo_tree(int naddmax, int nbrkmax)
{
//not using naddmax, nbrkmax?
printf("\n\n in make_combo_tree \n");
if (trxns!=NULL) trxns = NULL;
trxns = new RTYPE*[naddbrktypes];
if (nrtypes!=NULL) delete [] nrtypes;
nrtypes = new int[naddbrktypes];
for (int i=0;i<naddbrktypes;i++) nrtypes[i] = 0;
int* maxabtypes = new int[naddbrktypes];
for (int i=0;i<naddbrktypes;i++) maxabtypes[i] = 0;
//count max # of each type for allocation
for (int i=0;i<N;i++)
{
int wr = i;
int nadd1 = nadd[wr];
int nbrks1 = nbrks[wr];
int abtype = get_abtype(i,nadd1,nbrks1);
maxabtypes[abtype]++;
}
for (int i=0;i<naddbrktypes;i++)
trxns[i] = new RTYPE[maxabtypes[i]];
int maxchgatoms = 20;
int* anumbers1 = new int[maxchgatoms];
string* anames1 = new string[maxchgatoms];
int* coordn1 = new int[maxchgatoms];
int* add1 = new int[maxchgatoms];
int* brks1 = new int[maxchgatoms];
for (int i=0;i<maxchgatoms;i++)
add1[i] = brks1[i] = i;
//get all unique of each naddbrktype
for (int i=0;i<N;i++)
{
int wr = i;
string* anames0 = anames[wr];
int* anumbers0 = anumbers[wr];
int* coordn0 = coordnr[wr];
int nadd1 = nadd[wr];
int* add0 = add[wr];
int nbrks1 = nbrks[wr];
int* brks0 = brks[wr];
int natoms1 = 2*nadd1 + 2*nbrks1;
if (natoms1>maxchgatoms)
{
printf(" ERROR: too many changes in rxn %i/%i \n",i,ids[i]);
exit(1);
}
int naf = 0;
for (int j=0;j<2*nadd1;j++)
{
int a1 = add0[j];
coordn1[naf] = coordn0[a1]; //alt: set to -1
coordn1[naf] = -1;
anames1[naf] = anames0[a1]; //alt: set to X
// anames1[naf] = "X";
anumbers1[naf] = anumbers0[a1]; //alt: set to -1
// anumbers1[naf] = -1;
//add1[j] = naf; //set by default
naf++;
}
for (int j=0;j<2*nbrks1;j++)
{
int b1 = brks0[j];
coordn1[naf] = coordn0[b1];
coordn1[naf] = -1;
anames1[naf] = anames0[b1];
// anames1[naf] = "X";
anumbers1[naf] = anumbers0[b1];
// anumbers1[naf] = -1;
brks1[j] = naf;
naf++;
}
if (naf!=natoms1)
{
printf("\n naf not equal to natoms1: %i %i \n",naf,natoms1);
exit(1);
}
double val = Pr[i];
int abtype = get_abtype(i,nadd1,nbrks1);
int nrt = nrtypes[abtype];
int found = -1;
for (int j=0;j<nrt;j++)
if (trxns[abtype][j].match(natoms1,anumbers1,coordn1,nadd1,add1,nbrks1,brks1))
{
//printf(" match found: %i \n",j);
found = j;
break;
}
if (found==-1)
{
//printf(" new rxn (%4i): natoms %i nadd %i nbrks %i \n",ids[i],natoms1,nadd1,nbrks1);
trxns[abtype][nrt].init();
trxns[abtype][nrt].id = ids[i];
trxns[abtype][nrt].set_pr(val);
trxns[abtype][nrt].set_rxn(natoms1,anames1,anumbers1,coordn1,nadd1,add1,nbrks1,brks1);
nrtypes[abtype]++;
}
else if (val>trxns[abtype][found].get_pr())
{
//printf(" is higher Pr than previous %4.3f/%4.3f \n",val,trxns[abtype][found].get_pr());
trxns[abtype][nrt].id = ids[i];
trxns[abtype][nrt].set_pr(val);
}
//else
// printf(" aleady found higher Pr than %4.3f/%4.3f \n",val,trxns[abtype][found].get_pr());
} //loop i over N
printf("\n printing rtypes: \n");
for (int i=0;i<naddbrktypes;i++)
if (nrtypes[i]>0)
{
int nadd0 = trxns[i][0].get_nadd();
int nbrks0 = trxns[i][0].get_nbrks();
printf("\n type %i is nadd %i nbrk %i \n",i,nadd0,nbrks0);
for (int j=0;j<nrtypes[i];j++)
{
// double val = trxns[i][j].get_pr();
// printf(" Pr: %4.3f \n",val);
trxns[i][j].print();
}
}
else
printf("\n no data for type %i \n",i);
delete [] add1;
delete [] brks1;
delete [] anames1;
delete [] anumbers1;
delete [] coordn1;
delete [] maxabtypes;
#if 0
printf("\n\n Now printing entire data set \n");
print_reactions_2();
#endif
return;
}
int RXNDB::tree_screen_0(int nadd0, int nbrks0)
{
if (addbrk_layer==NULL || naddbrktypes<1) return 1;
double val;
int abtype = get_abtype(0,nadd0,nbrks0);
val = addbrk_layer[abtype];
int pass = 0;
if (val>0.001)
pass = 1;
return pass;
}
double RXNDB::tree_screen_1(int natoms0, string* anames0, int* anumbers0, double* xyzr0, int nadd0, int* add0, int nbrks0, int* brks0, double* qrc0)
{
double val = 1.;
if (allow_add_brk_elem==NULL)
return 1.0;
//take product of allowed pairings..
for (int i=0;i<nadd0;i++)
{
int a1 = add0[2*i+0];
int a2 = add0[2*i+1];
int e1 = anumbers0[a1];
int e2 = anumbers0[a2];
int e1m = remap[e1];
int e2m = remap[e2];
string anam1 = PTable::atom_name(e1);
string anam2 = PTable::atom_name(e2);
val *= allow_add_brk_elem[e1m*nelemf+e2m];
//printf(" adding %s %s allowed: %2.1f \n",anam1.c_str(),anam2.c_str(),allow_add_brk_elem[e1m*nelemf+e2m]);
}
for (int i=0;i<nbrks0;i++)
{
int b1 = brks0[2*i+0];
int b2 = brks0[2*i+1];
int e1 = anumbers0[b1];
int e2 = anumbers0[b2];
int e1m = remap[e1];
int e2m = remap[e2];
string anam1 = PTable::atom_name(e1);
string anam2 = PTable::atom_name(e2);
val *= allow_add_brk_elem[e1m*nelemf+e2m];
//printf(" brking %s %s allowed: %2.1f \n",anam1.c_str(),anam2.c_str(),allow_add_brk_elem[e1m*nelemf+e2m]);
}
return val;
}
double RXNDB::tree_screen_2(int natoms0, string* anames0, int* anumbers0, int* coordn0, double* xyzr0, int nadd0, int* add0, int nbrks0, int* brks0, double* qrc0)
{
double val = 1.;
int maxchgatoms = 20;
int nadd1 = nadd0;
int nbrks1 = nbrks0;
int* anumbers1 = new int[maxchgatoms];
string* anames1 = new string[maxchgatoms];
int* coordn1 = new int[maxchgatoms];
int* add1 = new int[maxchgatoms];
int* brks1 = new int[maxchgatoms];
for (int i=0;i<maxchgatoms;i++)
add1[i] = brks1[i] = i;
int naf = 0;
for (int j=0;j<2*nadd0;j++)
{
int a1 = add0[j];
coordn1[naf] = coordn0[a1];
anames1[naf] = anames0[a1];
anumbers1[naf] = anumbers0[a1];
//add1[j] = naf; //set by default
naf++;
}
for (int j=0;j<2*nbrks0;j++)
{
int b1 = brks0[j];
coordn1[naf] = coordn0[b1];
anames1[naf] = anames0[b1];
anumbers1[naf] = anumbers0[b1];
brks1[j] = naf;
naf++;
}
int natoms1 = naf;
int abtype = get_abtype(nadd0,nbrks0);
int nrt = nrtypes[abtype];
int found = -1;
for (int j=0;j<nrt;j++)
if (trxns[abtype][j].match(natoms1,anumbers1,coordn1,nadd1,add1,nbrks1,brks1))
{
//printf(" match found: %i \n",j);
found = j;
break;
}
if (found==-1)
{
printf(" no info on this reaction! \n");
}
else
{
printf(" found match \n");
trxns[abtype][found].print();
val = trxns[abtype][found].get_pr();
}
delete [] add1;
delete [] brks1;
delete [] anumbers1;
delete [] anames1;
delete [] coordn1;
return val;
}
void RXNDB::element_analysis(int wg)
{
if (wg<0)
wg = best_fv[0];
printf("\n\n now analyzing elemental reactivity (wg: %i) \n",wg);
if (p[wg]<1)
{
printf(" cannot make estimate, p=0 \n");
return;
}
#if 1
ICoord ic1;
ic1.alloc(3);
int* anumbers1 = new int[3];
string* anames1 = new string[3];
double* xyz1 = new double[9];
for (int i=0;i<9;i++) xyz1[i] = 0.;
xyz1[3] = 3.0;
xyz1[6] = -1.0;
double* qrc1 = new double[3];
for (int i=0;i<3;i++) qrc1[i] = 0.;
ic1.q = qrc1;
int* add1 = new int[4];
add1[0] = 0; add1[1] = 1;
int* brk1 = new int[4];
brk1[0] = 0; brk1[1] = 1;
double* X1 = new double[p[wg]+1];
double* X2 = new double[p[wg]+1];
double* X3 = new double[p[wg]+1];
for (int i=0;i<nelemf;i++)
// for (int j=0;j<=i;j++)
{
int e1 = anumbers1[0] = emap[i]; //reacting atom
// int e2 = anumbers1[1] = emap[j]; //adding to
int e2 = anumbers1[1] = 1;
anames1[0] = PTable::atom_name(e1);
// anames1[1] = PTable::atom_name(e2);
anames1[1] = "H";
ic1.reset(2,anames1,anumbers1,xyz1);
ic1.ic_create();
//ic1.print_xyz();
//ic1.print_bonds();
e2 = anumbers1[1] = -1;
anames1[1] = "X";
ic1.reset(2,anames1,anumbers1,xyz1);
ic1.coordn[1] = -1;
int ch = climit_h[e1];
int cl = climit_l[e1];
int extent = ch - cl;
for (int c1x=-1;c1x<=extent;c1x++)
{
int c1 = c1x + cl;
if (c1x==-1) c1 = -1;
printf("\n doing c1: %i for element %s \n",c1,anames1[0].c_str());
ic1.coordn[0] = c1;
for (int j=0;j<p[wg]+1;j++) X1[j] = X2[j] = X3[j] = 0.;
X1[p[wg]] = X2[p[wg]] = X3[p[wg]] = 1.0;
double norm1 = get_rowX_norm(wg,X1,ic1,1,add1,0,NULL);
double norm2 = get_rowX_norm(wg,X2,ic1,0,NULL,1,add1);
double norm3 = get_rowX_norm(wg,X3,ic1,1,add1,1,brk1);
for (int k=0;k<p[wg];k++) X3[k] -= X1[k] + X2[k];
double norm3x = norm(X3,p[wg]+1);
for (int k=0;k<p[wg]+1;k++) X1[k] = X1[k] / norm1;
for (int k=0;k<p[wg]+1;k++) X2[k] = X2[k] / norm2;
for (int k=0;k<p[wg]+1;k++) X3[k] = X3[k] / norm3x;
double val1 = knnrs[wg].predict_point(X1,KNNR_K);
double val2 = knnrs[wg].predict_point(X2,KNNR_K);
double val3 = knnrs[wg].predict_point(X3,KNNR_K);
printf(" add: %4.3f brk: %4.3f add/brk: %4.3f \n",val1,val2,val3);
#if 1
printf(" %s-%s add:",anames1[0].c_str(),anames1[1].c_str());
// for (int k=0;k<p[wg]+1;k++)
// printf(" %3.1f",X1[k]*10.);
printf(" n: %4.2f \n",norm1);
printf(" %s-%s brk:",anames1[0].c_str(),anames1[1].c_str());
// for (int k=0;k<p[wg]+1;k++)
// printf(" %3.1f",X2[k]*10.);
printf(" n: %4.2f \n",norm2);
printf(" %s(-%s) swp:",anames1[0].c_str(),anames1[1].c_str());
// for (int k=0;k<p[wg]+1;k++)
// printf(" %3.1f",X3[k]*10.);
printf(" n: %4.2f \n",norm3x);
#endif
} //loop c1 over climits
} //loop i,j over elements
printf("\n");
ic1.freemem();
delete [] qrc1;
delete [] add1;
delete [] brk1;
delete [] X1;
delete [] X2;
delete [] X3;
#endif
double* weight = new double[nelemf];
double* weightp = new double[nelemf*nelemf];
for (int i=0;i<nelemf;i++) weight[i] = 0.;
for (int i=0;i<nelemf*nelemf;i++) weightp[i] = 0.;
for (int i=0;i<N;i++)
{
int wr = i;
int natoms1 = natoms[wr];
string* anames2 = anames[wr];
int* anumbers2 = anumbers[wr];
double* xyzr2 = xyzr[wr];
int nadd2 = nadd[wr];
int* add2 = add[wr];
int nbrks2 = nbrks[wr];
int* brks2 = brks[wr];
for (int j=0;j<nadd2;j++)
{
int a1 = add2[2*j+0];
int a2 = add2[2*j+1];
int e1 = anumbers2[a1];
int e2 = anumbers2[a2];
int er1 = remap[e1];
int er2 = remap[e2];
weight[er1] += Pr[i];
weight[er2] += Pr[i];
weightp[er1*nelemf+er2] += Pr[i];
if (er1!=er2)
weightp[er2*nelemf+er1] += Pr[i];
}
for (int j=0;j<nbrks2;j++)
{
int b1 = brks2[2*j+0];
int b2 = brks2[2*j+1];
int e1 = anumbers2[b1];
int e2 = anumbers2[b2];
int er1 = remap[e1];
int er2 = remap[e2];
weight[er1] += Pr[i];
weight[er2] += Pr[i];
weightp[er1*nelemf+er2] += Pr[i];
if (er1!=er2)
weightp[er2*nelemf+er1] += Pr[i];
}
}
for (int i=0;i<nelemf;i++)
weight[i] = weight[i] / N;
for (int i=0;i<nelemf*nelemf;i++)
weightp[i] = weightp[i] / N;
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam = PTable::atom_name(e1);
printf(" element %s has weight: %4.3f \n",anam.c_str(),weight[i]);
}
printf("\n pair weights: \n ");
for (int i=0;i<nelemf;i++)
printf(" %5s",PTable::atom_name(emap[i]).c_str());
printf("\n");
for (int i=0;i<nelemf;i++)
{
int e1 = emap[i];
string anam1 = PTable::atom_name(e1);
printf(" %2s:",anam1.c_str());
for (int j=0;j<nelemf;j++)
{
//int e2 = emap[j];
//string anam2 = PTable::atom_name(e2);
printf(" %4.3f",weightp[i*nelemf+j]);
}
printf("\n");
}
if (allow_add_brk_elem!=NULL) delete [] allow_add_brk_elem;
allow_add_brk_elem = new double[nelemf*nelemf];
for (int i=0;i<nelemf*nelemf;i++) allow_add_brk_elem[i] = 0.;
for (int i=0;i<nelemf;i++)
for (int j=0;j<nelemf;j++)
if (weightp[i*nelemf+j]>0.001) //CPMZ parameter
allow_add_brk_elem[i*nelemf+j] = 1.0;
printf(" allow_add_brk_elem: \n");
for (int i=0;i<nelemf;i++)
{
for (int j=0;j<=i;j++)
printf(" %1.0f",allow_add_brk_elem[i*nelemf+j]);
printf("\n");
}
delete [] weight;
delete [] weightp;
return;
}
void RXNDB::ga_run(int ncycles)
{
int stable_criterion = 3;
double conv_crit = 0.0001;
double lambda = LAMBDA; //was using 0.002 then 0.0005
double mut_prob = 0.5;
int nmut = p[0] / 6; // # of mutation attempts
// int nmut = p[0] / 4; // # of mutation attempts
if (nmut<2) nmut = 2;
int nbestg = ng / 2; //keep 50%
// int nbestg = ng / 4; //keep 25%
if (nbestg%2==1) nbestg--;
if (nbestg<2) nbestg = 2;
if (ng<nbestg) nbestg = ng;
int* bestg = new int[nbestg];
for (int i=0;i<nbestg;i++)
bestg[i] = i;
double* biasl = new double[ng];
double rmsep = 100.;
int stable = 0;
printf("\n\n ******** Start of GA Run! ****** \n\n");
printf(" will perform up to %i mutations on each of %i vectors \n",nmut,ng);
for (int n=0;n<ncycles;n++)
{
printf("\n selecting the strongest, splicing and mutating \n");
for (int i=0;i<ng;i++)
biasl[i] = lambda * count_parameters(i);
for (int i=0;i<ng;i++)
ga_error[i] += biasl[i];
#if 1
int ndeadtot = 0;
for (int i=0;i<ng;i++)
ndeadtot += replace_dead_features(i);
printf(" ndeadtot: %i \n",ndeadtot);
#endif
selection(nbestg,bestg);
#if 0
printf(" current best feature set: \n");
int wgb = bestg[nbestg-1];
for (int j=0;j<p[wgb];j++)
rxnftrs[wgb][j].print_atoms();
#endif
double rmse = 0.;
int nskip = 0;
for (int i=0;i<nbestg;i++)
{
int wg = bestg[i];
if (ga_error[wg]<99999.)
rmse += ga_error[wg]*ga_error[wg];
else
nskip++;
}
if (nbestg-nskip<1)
{
printf("\n ERROR: something is wrong with regressions \n");
printf(" reseting all features to climits \n");
//printf(" nbestg: %i nskip: %i \n",nbestg,nskip);
p[0] = create_features_climit(0);
for (int i=1;i<ng;i++)
copy_features(i,0);
}
rmse = sqrt( rmse / (nbestg-nskip) );
printf(" RMS error over best (%i): %5.4f best(#%i): %5.4f \n",nbestg-nskip,rmse,bestg[nbestg-1],ga_error[bestg[nbestg-1]]);
printf(" GA errors (%2i):",n);
for (int i=0;i<ng;i++)
if (ga_error[i]<99999.)
printf(" %4.2f",ga_error[i]);
else
printf(" N/A");
printf("\n");
printf(" penalty (x10):");
for (int i=0;i<ng;i++)
printf(" %4.2f",biasl[i]*10);
printf("\n");
//convergence criteria
if (close_val(rmse,rmsep,conv_crit))
stable++;
else
stable = 0;
rmsep = rmse;
if (stable>stable_criterion) break;
#if 1
if (ncycles>1)
{
spawn_and_splice(nbestg,bestg);
for (int i=0;i<ng;i++)
for (int j=0;j<nmut;j++)
{
if (randomf(1.0)>mut_prob)
mutate(i);
}
}
else
printf(" skipped generational update! \n");
#endif
quiet = 1;
//quiet = 0;
#if 1
for (int i=0;i<ng;i++)
ga_error[i] = create_regression(i);
#else
for (int i=0;i<ng;i++)
ga_error[i] = randomf(1.0);
sleep(1.15);
#endif
quiet = 0;
} //loop n over ncycles
printf("\n\n ********** GA run over ********* \n\n");
selection(nbestg,bestg);
for (int i=0;i<nbestg;i++)
best_fv[i] = bestg[nbestg-1-i];
nbest_fv = nbestg;
#if 0
printf(" printing the best \n\n");
for (int i=0;i<nbestg;i++)
{
int wg = bestg[i];
int natoms0 = p[wg];
printf("\n feature vector %2i with %i features \n",wg,natoms0);
for (int j=0;j<natoms0;j++)
rxnftrs[wg][j].print_atoms();
}
#elif 1
printf(" printing the best (%i) \n\n",best_fv[0]);
int wg = best_fv[0];
for (int j=0;j<p[wg];j++)
rxnftrs[wg][j].print_atoms();
ga_error[wg] = create_regression(wg);
int nparam = 0;
for (int j=0;j<p[wg];j++)
nparam += rxnftrs[wg][j].count_parameters();
printf(" feature vector has %2i total parameters \n",nparam);
#endif
delete [] bestg;
delete [] biasl;
return;
}
void RXNDB::spawn_and_splice(int nbestg, int* bestg)
{
//printf(" in spawn_and_splice for ng: %i nbestg: %i \n",ng,nbestg);
int nneeded = ng - nbestg;
int nmake = nneeded/2;
int fn = nbestg;
int on = fn + nmake;
//printf(" ng: %i fn: %i on: %i \n",ng,fn,on);
//first copy fittest to first positions
int* sizes = new int[nbestg];
RXNFTR** tmpftr = new RXNFTR*[nbestg];
for (int i=0;i<nbestg;i++)
{
int wg = bestg[i];
sizes[i] = p[wg];
tmpftr[i] = NULL;
copy_features(tmpftr[i],rxnftrs[wg],0,sizes[i]);
}
for (int i=0;i<nbestg;i++)
copy_features(rxnftrs[i],tmpftr[i],p[i],sizes[i]);
for (int i=0;i<nbestg;i++)
p[i] = sizes[i];