-
Notifications
You must be signed in to change notification settings - Fork 3
/
gs.cc
1545 lines (1356 loc) · 57 KB
/
gs.cc
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
/*
$Id: gs.cc,v 1.55 2014/06/12 01:44:07 mp Exp $
AutoDock
Copyright (C) 2009 The Scripps Research Institute. All rights reserved.
AutoDock is a Trade Mark of The Scripps Research Institute.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/********************************************************************
These are the methods for Global_Search and its derivations.
rsh 9/95
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "gs.h"
#include "ranlib.h"
#include "eval.h"
#include "rep.h"
#include "rep_constants.h"
#include "assert.h"
#ifdef sgi
#include <ieeefp.h>
#endif
#ifdef sun
#include <ieeefp.h>
#endif
#include "constants.h"
#include "autocomm.h"
#include "writePDBQT.h"
#include "qmultiply.h"
extern int sel_prop_count;//debug
extern int debug;//debug
//#define DEBUG
//#define DEBUG2
//#define DEBUG3
//#define DEBUG_MUTATION
static double
worst_in_window(const double *const window, const int size, int outlev, FILE *logFile)
{
register int i;
double worst = window[0];
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/double worst_in_window(double *window, int size)_________________________\n");//debug
#endif
for (i=1; i<size; i++) {
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/window[%d]= %.3f\tworst= %.3f\n", i, window[i], worst);//debug
#endif
if (window[i]>worst) {
worst = window[i];
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/i= %d\t(window[i]>worst)\tUpdating: worst= %.3f\n", i, worst);//debug
#endif
}
}// for i
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/Returning: worst= %.3f\n\n", worst);//debug
#endif
return(worst);
}
static double
avg_in_window(const double *const window, const int size, FILE *logFile)
{
register int i;
double mysum = 0.0;
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/avg_in_window(double *window, int size)_________________________\n");//debug
#endif
for (i=0; i<size; i++) {
mysum += window[i];
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/mysum= %.3f\twindow[%d]= %.3f\n",mysum, i, window[i]);//debug
#endif
}
const double myavg = mysum / size;
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/Returning: myavg= %.3f\n\n",myavg);//debug
#endif
return(myavg);
}
// Also set avg -- and because of avg this is not a const function
double Genetic_Algorithm::worst_this_generation(const Population &pop, int outlev, FILE *logFile)
{
register unsigned int i;
double worstval, avgval;
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/worst_this_generation(Population &pop, logFile)_________________________\n");
#endif
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/double Genetic_Algorithm::worst_this_generation(Population &pop)\n");
#endif /* DEBUG */
avgval = worstval = pop[0].value(Normal_Eval);
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/avgval= %.3f\tworstval= %.3f\n", avgval, worstval);
#endif
for (i=1; i<pop.num_individuals(); i++) {
avgval += pop[i].value(Normal_Eval);
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/avgval= %.3f\tpop[%d].value(Normal_Eval)= %.3f\n", avgval, i, pop[i].value(Normal_Eval));
#endif
if (pop[i].value(Normal_Eval)>worstval) {
worstval = pop[i].value(Normal_Eval);
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/(pop[i].value(Normal_Eval)>worstval): Updating: worstval= %.3f\n", worstval);
#endif
}
}
avg = avgval/pop.num_individuals();
#ifdef DEBUG2
(void)fprintf(logFile, "gs.cc/Returning: avg= %.3f, worstval= %.3f\n\n", avg, worstval);
#endif
return(worstval);
}
// This could be made inline
Genetic_Algorithm::Genetic_Algorithm( const EvalMode init_e_mode,
const Selection_Mode init_s_mode,
const Xover_Mode init_c_mode,
const Worst_Mode init_w_mode,
const int init_elitism,
ConstReal init_c_rate,
ConstReal init_m_rate,
ConstReal init_localsearch_freq,
const int init_window_size,
const unsigned int init_max_evals,
const unsigned int init_max_generations,
const Output_pop_stats& init_output_pop_stats)
: Global_Search(init_max_evals, init_max_generations),
e_mode(init_e_mode),
s_mode(init_s_mode),
c_mode(init_c_mode),
w_mode(init_w_mode),
elitism(init_elitism),
c_rate(init_c_rate),
m_rate(init_m_rate),
localsearch_freq(init_localsearch_freq),
window_size(init_window_size),
alpha(1.0),
beta(0.0),
tranStep(2.0), // 2 Angstroms
quatStep( DegreesToRadians( 30.0 ) ), // 30 degrees
torsStep( DegreesToRadians( 30.0 ) ), // 30 degrees
low(-100),
high(100),
generations(0),
output_pop_stats(init_output_pop_stats),
converged(0),
alloc(NULL),
mutation_table(NULL),
ordering(NULL),
m_table_size(0),
worst(0.0L),
avg(0.0L),
linear_ranking_selection_probability_ratio(2.0)
{
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/Genetic_Algorithm::Genetic_Algorithm(EvalMode init_e_mode,...\n");
#endif /* DEBUG */
worst_window = new double[window_size];
}
int Genetic_Algorithm::set_linear_ranking_selection_probability_ratio(ConstReal r)
{
if (r<0.) return -1; //ERROR!
linear_ranking_selection_probability_ratio = r;
return 1;
}
void Genetic_Algorithm::set_worst(const Population ¤tPop, int outlev, FILE *logFile)
{
double temp = 0.0;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::set_worst(Population ¤tPop)\n");
#endif /* DEBUG */
worst_window[generations%window_size] = worst_this_generation(currentPop, outlev, logFile);
switch(w_mode)
{
// Assume for this case that there's a window_size of 1
case Ever:
if (generations!=0) {
if (temp>worst)
worst = worst_window[0];
} else {
worst = worst_window[0];
}
break;
case OfN:
if (generations>=window_size) {
worst = worst_in_window(worst_window, window_size, outlev, logFile);
} else {
worst = worst_in_window(worst_window, generations+1, outlev, logFile);
}
break;
case AverageOfN:
if (generations>=window_size) {
worst = avg_in_window(worst_window, window_size, logFile);
} else {
worst = avg_in_window(worst_window, generations+1, logFile);
}
break;
default:
(void)fprintf(logFile,"gs.cc/Unable to set the individual with the worst fitness!\n");
}
}
M_mode Genetic_Algorithm::m_type(const RepType type) const
{
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/M_mode Genetic_Algorithm::m_type(RepType type)\n");
#endif /* DEBUG */
switch(type)
{
case T_BitV:
return(BitFlip);
case T_RealV:
case T_CRealV:
return(CauchyDev);
case T_IntV:
return(IUniformSub);
default:
stop("gs.cc/Unrecognized Type (The allowed types are: T_BitV, T_RealV, T_CRealV and T_IntV)!\n");
return(ERR); // NOTREACHED
}
}
void Genetic_Algorithm::make_table(const int size, ConstReal prob, int outlev, FILE *logFile)
{
register int i, j;
double L = 0.0L;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::make_table(int size=%d, Real prob=%f)\n",size, prob);
#endif /* DEBUG */
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, "\ngs.cc/void Genetic_Algorithm::make_table(int size=%d, Real prob=%f)\n",size, prob);
#endif /* DEBUG */
m_table_size = size;
mutation_table = new Real[size+1];
mutation_table[0] = pow(1-prob, size);
mutation_table[size] = 1;
#ifdef DEBUG_MUTATION
fprintf(logFile,"mutation_table[0] = %.3f\n", mutation_table[0]);
fprintf(logFile,"mutation_table[%d] = %.3f\n", size,mutation_table[size]);
#endif
i = 1;
while (i<=(int)size*prob) {
L = 0.0;
for (j=1; j<=i; j++) {
L += log(size+1-j) - log(j);
#ifdef DEBUG_MUTATION
fprintf(logFile,"j= %d (size+1-j)= %d log(_)= %.4f log(j)=%.4f L= %.4f\n", j, (size+1-j), log(size+1-j), log(j), L);
#endif
}
L += i*log(prob) + (size-i)*log(1-prob);
#ifdef DEBUG_MUTATION
fprintf(logFile,"j= %d (size+1-j)= %d log(_)= %.4f log(j)=%.4f L= %.4f\n", j, (size+1-j), log(size+1-j), log(j), L);
#endif
assert( i > 0 && i<=size); // M Pique 2009-12 TODO - suspect problem if m_rate>1
mutation_table[i] = mutation_table[i-1]+exp(L);
#ifdef DEBUG_MUTATION
fprintf(logFile,"mutation_table[%d] = %.3f\n", i, mutation_table[i]);
#endif
i++;
} // end while
#ifdef DEBUG_MUTATION
fprintf(logFile,"L= %.3f exp(L)= %.3f\n", L, exp(L) );
#endif
L = exp(L);
for (; i<size; i++) {
#ifdef DEBUG_MUTATION
fprintf(logFile,"i= %d L= %.3f prob= %.3f size= %d (size+1-i)= %d i*(1-prob)= %.3f\n", i, L, prob, size, (size+1-i), i*(1-prob) );
#endif
L = (L*prob*(size+1-i))/(i*(1-prob));
#ifdef DEBUG_MUTATION
fprintf(logFile,"L= %.3f\n", L );
#endif
mutation_table[i] = mutation_table[i-1]+L;
#ifdef DEBUG_MUTATION
fprintf(logFile,"mutation_table[%d] = %.3f\n", i, mutation_table[i]);
#endif
}
#ifdef DEBUG_MUTATION
fflush(logFile);
#endif
}
int Genetic_Algorithm::check_table(ConstReal prob, int outlev, FILE *logFile)
{
int low, high;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/int Genetic_Algorithm::check_table(Real prob=%f)\n",prob);
#endif /* DEBUG */
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, "\ngs.cc/int Genetic_Algorithm::check_table(Real prob=%f)\n",prob);
#endif /* DEBUG */
low = 0; high = m_table_size;
#ifdef DEBUG_MUTATION
fprintf(logFile,"prob= %.3f low= %d high= %d\n", prob, low, high );
#endif
while (high-low>1) {
#ifdef DEBUG_MUTATION
fprintf(logFile,"high-low= %d\n", high-low );
fprintf(logFile,"(high+low)/2= %d mutation_table[(high+low)/2]= %.3f prob= %.3f\n", (high+low)/2, mutation_table[(high+low)/2], prob );
#endif
if (mutation_table[(high+low)/2]<prob) {
low = (high+low)/2;
#ifdef DEBUG_MUTATION
fprintf(logFile,"low= %d\n", low );
#endif
} else if (mutation_table[(high+low)/2]>prob) {
high = (high+low)/2;
#ifdef DEBUG_MUTATION
fprintf(logFile,"high= %d\n", high );
#endif
} else {
high = low = (high+low)/2;
#ifdef DEBUG_MUTATION
fprintf(logFile,"low= %d high= %d\n", low, high );
#endif
}
}
return(low);
}
void Genetic_Algorithm::initialize(const unsigned int pop_size, const unsigned int num_poss_mutations, int outlev, FILE *logFile)
{
register unsigned int i;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::initialize(unsigned int pop_size=%d, ",pop_size);
(void)fprintf(logFile, "unsigned int num_poss_mutations=%d)\n",num_poss_mutations);
#endif /* DEBUG */
if (alloc!=NULL) {
delete [] alloc;
}
if (ordering!=NULL) {
delete [] ordering;
}
if (mutation_table!=NULL) {
delete [] mutation_table;
}
alloc = new Real[pop_size];
ordering = new unsigned int[pop_size];
for (i=0; i<pop_size; i++) {
ordering[i] = i;
assert(ordering[i] < pop_size);//debug
alloc[i] = 1.0; // changed by gmm, 12-sep-1997.
}
make_table(pop_size*num_poss_mutations, m_rate, outlev, logFile);
}
void Genetic_Algorithm::mutate(Genotype &mutant, const int gene_number, int outlev, FILE *logFile)
{
Element tempvar;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::mutate(Genotype &mutant, int gene_number=%d)\n",gene_number);
#endif /* DEBUG */
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::mutate(Genotype &mutant, int gene_number=%d)\n",gene_number);
#endif /* DEBUG */
switch(m_type(mutant.gtype(gene_number))) {
case BitFlip:
#ifdef DEBUG_MUTATION
fprintf( logFile, "case BitFlip: gene_number=%d\n", gene_number );
#endif
//((unsigned char *)gene)[point] = 1 - ((unsigned char *)gene)[point];
// Read the bit
tempvar = mutant.gread(gene_number);
// Flip it
tempvar.bit = 1 - tempvar.bit;
// write it
mutant.write(tempvar, gene_number);
break;
case CauchyDev:
#ifdef DEBUG_MUTATION
fprintf( logFile, "case CauchyDev: gene_number=%d\n", gene_number );
#endif
// gene_numbers 3, 4, 5 and 6 correspond to the
// four components of the quaternion, (x,y,z,w)
if ( is_rotation_index( gene_number ) ) {
#ifdef DEBUG_MUTATION
fprintf( logFile, "is_rotation_index( gene_number=%d )\n", gene_number );
#endif
// Mutate all four comopnents of the quaternion, (x,y,z,w) simultaneously:
// Generate a uniformly-distributed quaternion
Quat q_change;
// MP TODO here we could use quat by amount for better mutations
q_change = randomQuat();
#ifdef DEBUG_MUTATION
fprintf( logFile, "q_change -- after randomQuat\n" );
printQuat_q( logFile, q_change );
#endif
Quat q_current = mutant.readQuat();
#ifdef DEBUG_MUTATION
fprintf( logFile, "q_current -- after mutant.readQuat()\n" );
printQuat_q( logFile, q_current );
#endif
Quat q_new;
#ifdef DEBUG_MUTATION
fprintf( logFile, "q_current -- about to call qmultiply\n" );
#endif
#ifdef DEBUG_QUAT
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: mutate() -- q_current\n" );
(void) fflush(logFile);
#endif // DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
assertQuatOK( q_current );
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: mutate() -- q_change\n" );
(void) fflush(logFile);
#endif // DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
assertQuatOK( q_change );
#endif // DEBUG_QUAT
// Multiply the quaternions, applying the rotation to the current orientation
qmultiply( &q_new, &q_current, &q_change );
#ifdef DEBUG_QUAT
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: mutate() -- q_current\n" );
(void) fflush(logFile);
#endif
// Make sure the quaternion is suitable for 3D rotation
assertQuatOK( q_new );
#endif // DEBUG_QUAT
#ifdef DEBUG_MUTATION
fprintf( logFile, "q_new - after qmultiply\n" );
printQuat_q( logFile, q_new );
#endif
mutant.writeQuat( q_new );
} else {
// Read the real
tempvar = mutant.gread(gene_number);
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, " ---CauchyDev---\n" );
(void)fprintf(logFile, " Before mutating: tempvar= %.3f\n", tempvar.real );
(void)fprintf(logFile, " gene_number= %d\n", gene_number );
(void)fprintf(logFile, " tempvar.real += scauchy2()\n" );
#endif
// Add deviate
// We never vary alpha and beta, so just use the faster "scauchy2()" function:
tempvar.real += scauchy2();
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, " Add Cauchy deviate: tempvar= %.3f\n", tempvar.real );
(void)fflush(logFile );
#endif
// Write it
mutant.write(tempvar, gene_number);
}
break;
case IUniformSub:
#ifdef DEBUG_MUTATION
fprintf( logFile, "case IUniformSub: gene_number=%d\n", gene_number );
#endif
//((int *)gene)[point] = ignuin(low, high);
// Generate the new integer
tempvar.integer = ignuin(low, high);
// Write it
//mutant.write((void *)(&tempvar.integer), gene_number);
mutant.write(tempvar, gene_number);
break;
default:
(void)fprintf(logFile,"gs.cc/Unrecognized mutation Mode!\n");
break;
}
}
void Genetic_Algorithm::mutation(Population &pure, int outlev, FILE *logFile)
{
int num_mutations, individual, gene_number;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::mutation(Population &pure)\n");
#endif /* DEBUG */
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::mutation(Population &pure)\n");
#endif /* DEBUG */
num_mutations = check_table(ranf(), outlev, logFile);
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, "num_mutations= %d\n", num_mutations );
#endif /* DEBUG */
if(num_mutations<=0) return;
Boole *individual_mutated = new Boole[pure.num_individuals()]; // for statistics only
for(unsigned int i=0;i<pure.num_individuals();i++) individual_mutated[i]=FALSE;
// Note we don't check to see if we mutate the same gene twice.
// So, effectively we are lowering the mutation rate, etc...
// Might want to check out Bentley's chapter on selection.
for (; num_mutations>0; num_mutations--) {
individual = ignlgi()%pure.num_individuals();
gene_number = ignlgi()%pure[individual].genotyp.num_genes();
#ifdef DEBUG_MUTATION
(void)fprintf(logFile, " @__@ mutate(pure[individual=%d].genotyp, gene_number=%d);\n\n", individual, gene_number );
#endif /* DEBUG */
mutate(pure[individual].genotyp, gene_number, outlev, logFile);
pure[individual].age = 0L;
pure[individual].mapping();//map genotype to phenotype and evaluate
mg_count++; // update statistics: count of mutated genes per run
individual_mutated[individual] = TRUE; // for statistics only
}
// update statistics: count of mutated individuals per run
for(unsigned int i=0;i<pure.num_individuals();i++) \
if(individual_mutated[i]) mi_count++;
delete [] individual_mutated;
}
void Genetic_Algorithm::crossover(Population &original_population, int outlev, FILE *logFile)
{
register unsigned int i;
int first_point, second_point, temp_index, temp_ordering;
Real alpha = 0.5;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover(Population &original_population)\n");
#endif /* DEBUG */
// Shuffle the population
// Permute the ordering of the population, "original_population"
for (i=0; i<original_population.num_individuals(); i++) {
temp_ordering = ordering[i];
// ignlgi is GeNerate LarGe Integer and is in com.cc
temp_index = ignlgi()%original_population.num_individuals();
ordering[i] = ordering[temp_index];
assert(ordering[i] < original_population.num_individuals());//debug
assert(ordering[i] >= 0);//debug
ordering[temp_index] = temp_ordering;
assert(ordering[temp_index] < original_population.num_individuals());//debug
assert(ordering[temp_index] >= 0);//debug
}
// How does Goldberg implement crossover?
// Loop over individuals in population
for (i=0; i<original_population.num_individuals()-1; i=i+2) {
// The two individuals undergoing crossover are original_population[ordering[i]] and original_population[ordering[i+1]]
if (ranf() < c_rate) {
// Perform crossover with a probability of c_rate
int fi = ordering[i]; //index of father
int mi = ordering[i+1]; //index of mother
// Assert the quaternions of the mother and father are okay:
Genotype father = original_population[fi].genotyp;
Genotype mother = original_population[mi].genotyp;
Quat q_father, q_mother;
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: crossover() q_father (individual=%d)\n", fi );
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_father = father.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_father );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_father );
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: crossover() q_mother (individual=%d)\n", mi );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_mother = mother.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_mother );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_mother );
// Pos. Orient. Conf.
// 0 1 2 3 4 5 6 7 8 9
// X Y Z x y z w t t t
// num_genes() = 10
switch(c_mode) {
case OnePt:
// To guarantee 1-point always creates 2 non-empty partitions,
// the crossover point must lie in range from 1 to num_genes-1.
// Choose one random integer in this range
// Make sure we do not crossover inside a quaternion...
do {
// first_point = ignlgi()%original_population[i].genotyp.num_genes();
first_point = ignuin(1, original_population[i].genotyp.num_genes()-1);
} while ( is_within_rotation_index( first_point ) ) ;
// We can accomplish one point crossover by using the 2pt crossover operator
crossover_2pt( original_population[fi].genotyp,
original_population[mi].genotyp,
first_point,
original_population[fi].genotyp.num_genes(), outlev, logFile);//either one works
break;
case TwoPt:
// To guarantee 2-point always creates 3 non-empty partitions,
// each crossover point must lie in range from 1 to num_genes-1.
// Choose two different random integers in this range
// Make sure we do not crossover inside a quaternion...
do {
first_point = ignuin(1, original_population[i].genotyp.num_genes()-1);
} while ( is_within_rotation_index( first_point ) ) ;
do {
second_point = ignuin(1, original_population[i].genotyp.num_genes()-1);
} while ( is_within_rotation_index( second_point ) || second_point == first_point );
// Do two-point crossover, with the crossed-over offspring replacing the parents in situ:
crossover_2pt( original_population[fi].genotyp,
original_population[mi].genotyp,
min( first_point, second_point ),
max( first_point, second_point), outlev, logFile );
break;
case Branch:
// New crossover mode, designed to exchange just one corresponding sub-trees (or "branch")
// between two individuals.
// If there are only position and orientation genes, there will be only
// 7 genes; this mode would not change anything in such rigid-body dockings.
if (original_population[i].genotyp.num_genes() <= 7) {
// Rigid body docking, so no torsion genes to crossover.
continue; //TODO raise a fatal error if branch crossover with no torsions
} else {
// Pick a random torsion gene
first_point = ignuin(7, original_population[i].genotyp.num_genes()-1);
second_point = original_population.get_eob( first_point - 7 );
// Do two-point crossover, with the crossed-over offspring replacing the parents in situ:
crossover_2pt( original_population[fi].genotyp,
original_population[mi].genotyp,
min( first_point, second_point ),
max( first_point, second_point ), outlev, logFile );
break;
}
case Uniform:
crossover_uniform( original_population[fi].genotyp,
original_population[mi].genotyp,
original_population[mi].genotyp.num_genes(), outlev, logFile);
break;
case Arithmetic:
// select the parents A and B
// create new offspring, a and b, where
// a = x*A + (1-x)*B, and b = (1-x)*A + x*B -- note: x is alpha in the code
alpha = (Real) ranf();
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/ alpha = " FDFMT "\n", alpha);
(void)fprintf(logFile, "gs.cc/ About to call crossover_arithmetic with original_population[%d] & [%d]\n", fi, mi);
#endif /* DEBUG */
crossover_arithmetic( original_population[fi].genotyp,
original_population[mi].genotyp,
alpha, outlev, logFile );
break;
default:
(void)fprintf(logFile,"gs.cc/ Unrecognized crossover mode!\n");
}
original_population[fi].age = 0L;
original_population[mi].age = 0L;
//map genotype to phenotype and evaluate energy
original_population[fi].mapping();
original_population[mi].mapping();
// update statistics
ci_count++; // count of crossovers, individ-by-individ
}
}
}
void Genetic_Algorithm::crossover_2pt(Genotype &father, Genotype &mother, const unsigned int pt1, const unsigned int pt2, int outlev, FILE *logFile)
{
// Assumes that 0<=pt1<=pt2<=number_of_pts
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_2pt(Genotype");
(void)fprintf(logFile, "&father, Genotype &mother, unsigned int pt1, unsigned int pt2)\n");
(void)fprintf(logFile,"gs.cc/Performing crossover from %d to %d \n", pt1,pt2);
#endif /* DEBUG */
// loop over genes to be crossed over
for (unsigned int i=pt1; i<pt2; i++) {
#ifdef DEBUG
//(void)fprintf(logFile,"gs.cc/1::At pt %d father: %.3lf mother: %.3lf\n",
//i, *((double *)father.gread(i)), *((double *)mother.gread(i)) );
#endif /* DEBUG */
Element temp = father.gread(i);
father.write(mother.gread(i), i);
mother.write(temp, i);
cg_count++; // count of crossovers, gene-by-gene
#ifdef DEBUG
//(void)fprintf(logFile,"gs.cc/1::At pt %d father: %.3lf mother: %.3lf\n",
//i, *((double *)father.gread(i)), *((double *)mother.gread(i)) );
#endif /* DEBUG */
}
#ifdef DEBUG_QUAT
Quat q_father, q_mother;
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: crossover_2pt() q_father\n" );
pr( logFile, "DEBUG_QUAT: crossover_2pt() pt1=%d, pt2=%d\n", pt1, pt2 );
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_father = father.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_father );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_father );
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: crossover_2pt() q_mother\n" );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_mother = mother.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_mother );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_mother );
#endif // endif DEBUG_QUAT
}
void Genetic_Algorithm::crossover_uniform(Genotype &father, Genotype &mother, const unsigned int num_genes, int outlev, FILE *logFile)
{
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_uniform(Genotype");
(void)fprintf(logFile, "&father, Genotype &mother, unsigned int num_genes)\n");
#endif /* DEBUG */
for (unsigned int i=0; i<num_genes; i++) {
// Choose either father's or mother's gene/rotation gene set, with a 50/50 probability
if (ranf() > 0.5 ) continue; // 50% probability of crossing a gene or gene-set; next i
if ( ! is_rotation_index( i ) ) {
// Exchange parent's genes
Element temp = father.gread(i);
father.write(mother.gread(i), i);
mother.write(temp, i);
} else if ( is_first_rotation_index(i) ) {
// don't crossover within a quaternion, only at the start; next i
#ifdef DEBUG_QUAT
Quat q_father, q_mother;
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: pre-crossover_uniform() q_father\n" );
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_father = father.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_father );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_father );
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: pre-crossover_uniform() q_mother\n" );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_mother = mother.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_mother );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_mother );
#endif // endif DEBUG_QUAT
// Exchange father's or mother's set of rotation genes
for (unsigned int j=i; j<i+4; j++) {
Element temp = father.gread(j);
father.write(mother.gread(j), j);
mother.write(temp, j);
}
// Increment gene counter, i, by 3, to skip the 3 remaining rotation genes
i=i+3;
#ifdef DEBUG_QUAT
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: post-crossover_uniform() q_father\n" );
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_father = father.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_father );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_father );
#ifdef DEBUG_QUAT_PRINT
pr( logFile, "DEBUG_QUAT: post-crossover_uniform() q_mother\n" );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
// Make sure the quaternion is suitable for 3D rotation
q_mother = mother.readQuat();
#ifdef DEBUG_QUAT_PRINT
printQuat( logFile, q_mother );
(void) fflush(logFile);
#endif // endif DEBUG_QUAT_PRINT
assertQuatOK( q_mother );
#endif // endif DEBUG_QUAT
} // is_rotation_index( i )
cg_count++; // count of crossovers, gene-by-gene
} // next i
}
void Genetic_Algorithm::crossover_arithmetic(Genotype &A, Genotype &B, ConstReal alpha, int outlev, FILE *logFile)
{
register unsigned int i;
Element temp_A, temp_B;
Real one_minus_alpha;
one_minus_alpha = 1.0 - alpha;
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_arithmetic(Genotype");
(void)fprintf(logFile, "&A, Genotype &B, Real alpha)\n");
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_arithmetic");
(void)fprintf(logFile, "/Trying to perform arithmetic crossover using alpha = " FDFMT "\n", alpha);
(void)fflush(logFile);
#endif /* DEBUG */
// loop over genes to be crossed over
// a = alpha*A + (1-alpha)*B
// b = (1-alpha)*A + alpha*B
for (i=0; i<A.num_genes(); i++) {
if ( is_translation_index(i) ) {
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_arithmetic");
(void)fprintf(logFile, "/looping over genes to be crossed over, i = %d\n", i);
(void)fflush(logFile);
#endif /* DEBUG */
temp_A = A.gread(i);
temp_B = B.gread(i);
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_arithmetic");
(void)fprintf(logFile, "/temp_A = %.3f & temp_B = %.3f\n", temp_A.real, temp_B.real);
(void)fflush(logFile);
#endif
A.write( (one_minus_alpha * temp_A.real + alpha * temp_B.real), i);
B.write( (alpha * temp_A.real + one_minus_alpha * temp_B.real), i);
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_arithmetic");
(void)fprintf(logFile, "/A = %.3f & B = %.3f\n", A.gread(i).real, B.gread(i).real);
(void)fflush(logFile);
#endif
} else if ( is_rotation_index(i) ) {
// Interpolate the father's and mother's set of 4 quaternion genes
Quat q_A;
q_A = slerp( A.readQuat(), B.readQuat(), alpha );
B.writeQuat( slerp( A.readQuat(), B.readQuat(), one_minus_alpha ) );
A.writeQuat( q_A );
// Increment gene counter, i, by 3, to skip the 3 remaining quaternion genes
i=i+3;
} else if ( is_conformation_index(i) ) {
// Use anglular interpolation, alerp(a,b,fract), to generate properly interpolated torsion angles
temp_A = A.gread(i);
temp_B = B.gread(i);
A.write( alerp(temp_A.real, temp_B.real, alpha), i);
B.write( alerp(temp_A.real, temp_B.real, one_minus_alpha), i);
} else {
// MP: BUG CHECK!
char msg[100];
(void)sprintf(msg, "BUG: Invalid gene type at i=%d\n", i);
stop(msg); // exits
}
cg_count++; // count of crossovers, gene-by-gene
}
}
/* * */
/*
* Proportional Selection
*
*
* We want to perform minimization on a function. To do so, we
* take fitness values in a given generation and normalize them s.t.
* they are non-negative, and such that smaller f's are given a higher
* weighting.
* If f in [a,b], then f' in [A,B] s.t. f(a) => f'(B) and f(b) => f'(A) is
*
* f' = (B-A) * (1 - (f-a)/(b-a)) + A
*
* = (B-A) * (b-f)/(b-a) + A
*
* Note that it suffices to map f into [0,1], giving
*
* f' = (b-f)/(b-a)
*
* Now the expected number of samples generated from a given point is
*
* N * f'_i / \sum f'_i = N * ((b-f_i)/(b-a)) / \sum ((b-f_i)/(b-a))
*
* = N * (b-f_i) / (N*b - \sum f_i)
*
* Within a given generation, let 'b' be the maximal (worst) individual and
* let 'a' be the minimal individual.
*
* Note:
* (1) This calculation is invariant to the value of B, but _not_
* invariant to the value of A.
* (2) This selection strategy works fine for functions bounded above,
* but it won't necessarily work for functions which are unbounded
* above.
*
* The 'b' parameter is represented as 'Worst' and is selected by the
* scale_fitness method. If a value is greater than Worst, it is given a
* value of zero for it's expectation.
*/
/* not static */
void Genetic_Algorithm::selection_proportional(Population &original_population, Individual *const new_pop, int outlev, FILE *logFile)
{
register unsigned int i=0, start_index = 0;
int temp_ordering, temp_index;
#ifdef DEBUG2
int allzero = 1;//debug
Molecule *individualMol;//debug
#endif
#undef CHECK_ISNAN
#ifdef CHECK_ISNAN
int allEnergiesEqual = 1;
double diffwa = 0.0, invdiffwa = 0.0, firstEnergy = 0.0;
#endif
#ifdef DEBUG
(void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::");
(void)fprintf(logFile, "selection_proportional(Population &original_population, Individual *new_pop)\n");
#endif /* DEBUG */