-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibgaALL.c
4424 lines (3729 loc) · 140 KB
/
libgaALL.c
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
/*============================================================================
| (c) Copyright Arthur L. Corcoran, 1992, 1993. All rights reserved.
| (c) Copyright IA UPM - Group 5, 2020. All rights reserved.
|
| Chromosome management
|
| Functions:
| CH_alloc() - allocate a chrom
| CH_resize() - resize a chrom
| CH_free() - deallocate a chrom
| CH_valid() - is a chrom valid?
| CH_reset() - reset a chrom
| CH_copy() - copy a chrom over another
| CH_cmp() - compare two chromosomes
| CH_print() - print a chrom
| CH_verify() - ensure chrom makes sense
============================================================================*/
#define MAXTOK 10 /* Maximum number of tokens on a line */
#define STRLEN 80 /* Length of an input line */
/* Number of chromosome pointers to alloc at a time */
#define PL_ALLOC_SIZE 10
#include "ga.h"
int GA_generational(), GA_steady_state();
static Chrom_Ptr child1, child2;
int X_simple(), X_uniform(), X_order1(), X_order2(), X_pos(), X_cycle(),
X_pmx(), X_uox(), X_rox(), X_asex();
int MU_simple_invert(), MU_simple_random(), MU_swap();
/* rnd float in [0..1] -- introduced by claudio 10/02/2004 */
int MU_float_random(), MU_float_rnd_pert(), MU_float_LS(), MU_float_gauss_pert();
double gaussian_random(void);
int obj_fun( Chrom_Ptr chrom);
int RE_append(), RE_by_rank(), RE_first_weaker(), RE_weakest();
int SE_uniform_random(), SE_roulette(), SE_rank_biased();
/*----------------------------------------------------------------------------
| Allocate a chromosome
----------------------------------------------------------------------------*/
Chrom_Ptr CH_alloc(
int length)
{
Chrom_Ptr chrom;
/*--- Error check ---*/
if(length <= 0) UT_error("CH_alloc: invalid length");
/*--- Allocate memory for chromosome ---*/
chrom = (Chrom_Ptr)calloc(1, sizeof(Chrom_Type));
if(chrom == NULL) UT_error("CH_alloc: chrom alloc failed");
chrom->length = length;
/*--- Allocate memory for genes ---*/
chrom->gene = (Gene_Ptr)calloc(length, sizeof(Gene_Type));
if(chrom->gene == NULL) UT_error("CH_alloc: gene alloc failed");
/*--- Put in magic cookie ---*/
chrom->magic_cookie = CH_cookie;
/*--- Reset the chromosome ---*/
CH_reset(chrom);
return chrom;
}
/*----------------------------------------------------------------------------
| Resize a chromosome
|
| NOTE: after the resize, the chromosome may no longer be valid,
| therefore it is reset
----------------------------------------------------------------------------*/
CH_resize(
Chrom_Ptr chrom,
int length)
{
/*--- Error check ---*/
if(!CH_valid(chrom)) UT_error("CH_resize: invalid chrom");
if(length <= 0) UT_error("CH_resize: invalid length");
/*--- Reallocate memory for genes ---*/
chrom->gene = (Gene_Ptr)realloc(chrom->gene, length * sizeof(Gene_Type));
if(chrom->gene == NULL) UT_error("CH_resize: gene realloc failed");
chrom->length = length;
/*--- Reset the chromosome ---*/
CH_reset(chrom);
}
/*----------------------------------------------------------------------------
| De-Allocate a chromosome
----------------------------------------------------------------------------*/
CH_free(
Chrom_Ptr chrom)
{
/*--- Error check ---*/
if(!CH_valid(chrom)) return 0; //CC
/*--- Free memory for genes ---*/
if(chrom->gene != NULL) {
free(chrom->gene);
chrom->gene = NULL;
}
/*--- Put in NULL magic cookie ---*/
chrom->magic_cookie = NL_cookie;
/*--- Free memory for chromosome ---*/
free(chrom);
}
/*----------------------------------------------------------------------------
| Is a chromosome valid, i.e., has it been allocated by CH_alloc()?
----------------------------------------------------------------------------*/
CH_valid(
Chrom_Ptr chrom)
{
/*--- Check for NULL pointers ---*/
if(chrom == NULL) return FALSE;
if(chrom->gene == NULL) return FALSE;
/*--- Check for magic cookie ---*/
if(chrom->magic_cookie != CH_cookie) return FALSE;
/*--- Otherwise valid ---*/
return TRUE;
}
/*----------------------------------------------------------------------------
| Reset a chromosome
----------------------------------------------------------------------------*/
CH_reset(
Chrom_Ptr chrom)
{
int i;
/*--- Error check ---*/
if(!CH_valid(chrom)) UT_error("CH_reset: invalid chrom");
/*--- Initialize genes ---*/
for(i=0; i<chrom->length; i++)
chrom->gene[i] = (Gene_Type)0;
/*--- Initialize chromosome ---*/
chrom->fitness = 0.0;
chrom->ptf = 0.0;
chrom->index = -1;
chrom->idx_min = 0;
chrom->idx_max = chrom->length;
chrom->parent_1 = -1;
chrom->parent_2 = -1;
chrom->xp1 = -1;
chrom->xp2 = -1;
}
/*----------------------------------------------------------------------------
| Copy a chromosome
----------------------------------------------------------------------------*/
CH_copy(
Chrom_Ptr src, Chrom_Ptr dst)
{
Gene_Ptr gene;
/*--- Error check ---*/
if(!CH_valid(src)) UT_error("CH_copy: invalid src");
if(!CH_valid(dst)) UT_error("CH_copy: invalid dst");
/*--- Resize if necessary ---*/
if(dst->length != src->length) CH_resize(dst, src->length);
/*--- Save memory pointed to by gene ---*/
gene = dst->gene;
/*--- Copy chrom ---*/
memcpy(dst, src, sizeof(Chrom_Type));
/*--- Restore memory pointed to by gene ---*/
dst->gene = gene;
/*--- Copy gene ---*/
memcpy(dst->gene, src->gene, src->length * sizeof(Gene_Type));
}
/*----------------------------------------------------------------------------
| Compare chromosomes A and B:
| -1 = A is better
| 1 = B is better
| 0 = Same
----------------------------------------------------------------------------*/
CH_cmp(
GA_Info_Ptr ga_info,
Chrom_Ptr a, Chrom_Ptr b)
{
if(ga_info->minimize)
if(a->fitness < b->fitness)
return -1;
else if(a->fitness > b->fitness)
return 1;
else
return 0;
else
if(a->fitness > b->fitness)
return -1;
else if(a->fitness < b->fitness)
return 1;
else
return 0;
}
/*----------------------------------------------------------------------------
| Print a chromosome
----------------------------------------------------------------------------*/
CH_print(
Chrom_Ptr chrom)
{
int i;
/*--- Error check ---*/
if(!CH_valid(chrom)) UT_error("CH_print: invalid chrom");
printf("==============================================================\n");
printf("\nChrom: \n");
for(i=0; i<chrom->length; i++)
printf("%G ", chrom->gene[i]);
printf("\n\n");
printf("fitness = %G, ptf = %G, index = %d, idx_min = %d, idx_max = %d\n",
chrom->fitness, chrom->ptf, chrom->index, chrom->idx_min, chrom->idx_max);
printf("parent_1 = %d, parent_2 = %d, xp1 = %d, xp2 = %d\n",
chrom->parent_1, chrom->parent_2, chrom->xp1, chrom->xp2);
printf("==============================================================\n");
}
/*----------------------------------------------------------------------------
| Verify a chromosome
----------------------------------------------------------------------------*/
CH_verify(
GA_Info_Ptr ga_info,
Chrom_Ptr chrom)
{
int i;
char *allele_count, err_str[80];
/*--- Error check ---*/
if(!CF_valid(ga_info)) UT_error("CH_verify: invalid ga_info");
if(!CH_valid(chrom)) UT_error("CH_verify: invalid chrom");
/*--- Check for invalid length ---*/
if(chrom->length <= 0) {
CH_print(chrom);
UT_error("CH_verify: bad length");
}
/*--- Check for invalid idx_min ---*/
if(chrom->idx_min < 0 || chrom->idx_min > chrom->length) {
CH_print(chrom);
UT_error("CH_verify: idx_min out of bounds");
}
/*--- Check for invalid idx_max ---*/
if(chrom->idx_max < 0 || chrom->idx_max > chrom->length) {
CH_print(chrom);
UT_error("CH_verify: idx_max out of bounds");
}
/*--- Check for invalid permutation ---*/
if(ga_info->datatype == DT_INT_PERM) {
/*--- Allocate allele_count vector ---*/
allele_count = calloc(chrom->length, sizeof(char));
if(allele_count == NULL)
UT_error("CH_verify: cannot alloc allele_count");
/*--- Check each gene in the chromosome ---*/
for(i=0; i<chrom->length; i++) {
/*--- Check for allele out of bounds ---*/
if(chrom->gene[i] < 1 || (int)chrom->gene[i] > chrom->length) {
CH_print(chrom);
sprintf(err_str,"CH_verify: gene[%d] = %G is out of bounds",
i, chrom->gene[i]);
UT_error(err_str);
/*--- Check for duplicate alleles ---*/
} else if(++(allele_count[((int)chrom->gene[i])-1]) > 1) {
CH_print(chrom);
sprintf(err_str,"CH_verify: gene[%d] = %G is a duplicate",
i, chrom->gene[i]);
UT_error(err_str);
}
}
free(allele_count);
}
}
/*============================================================================
| (c) Copyright Arthur L. Corcoran, 1992, 1993. All rights reserved.
| (c) Copyright IA UPM - Group 5, 2020. All rights reserved.
|
| Configuration file and ga_info management
|
| Functions:
| CF_alloc() - allocate a ga_info
| CF_free() - deallocate a ga_info
| CF_valid() - is ga_info valid?
| CF_reset() - reset config
| CF_report() - print out current config
| CF_read() - read config file
| CF_tokenize() - convert input line to tokens
| CF_verify() - ensure ga_info makes sense
============================================================================*/
/*----------------------------------------------------------------------------
| Allocate a GA_Info structure
----------------------------------------------------------------------------*/
GA_Info_Ptr CF_alloc()
{
GA_Info_Ptr ga_info;
/*--- Allocate memory for a ga_info ---*/
ga_info = (GA_Info_Ptr)calloc(1, sizeof(GA_Info_Type));
if(ga_info == NULL) UT_error("CF_alloc: alloc failed");
/*--- Ensure these are NULL ---*/
ga_info->old_pool = NULL;
ga_info->new_pool = NULL;
ga_info->best = NULL;
/*--- Put in a magic cookie ---*/
ga_info->magic_cookie = CF_cookie;
/*--- Reset ga_info ---*/
CF_reset(ga_info);
return ga_info;
}
/*----------------------------------------------------------------------------
| De-Allocate a GA_Info structure
----------------------------------------------------------------------------*/
CF_free(
GA_Info_Ptr ga_info)
{
/*--- Error check ---*/
if(!CF_valid(ga_info)) return GA_ERROR; //CCC
/*--- Free pools ---*/
if(ga_info->old_pool != NULL) PL_free(ga_info->old_pool);
if(ga_info->new_pool != NULL) PL_free(ga_info->new_pool);
ga_info->old_pool = ga_info->new_pool = NULL;
/*--- Free best chrom ---*/
if(ga_info->best != NULL) CH_free(ga_info->best);
ga_info->best = NULL;
/*--- Put in a NULL cookie ---*/
ga_info->magic_cookie = NL_cookie;
/*--- Free ga_info ---*/
free(ga_info);
}
/*----------------------------------------------------------------------------
| Is a ga_info valid, i.e., has it been allocated by CF_alloc()?
----------------------------------------------------------------------------*/
CF_valid(
GA_Info_Ptr ga_info)
{
/*--- Check for NULL pointers ---*/
if(ga_info == NULL) return FALSE;
/*--- Check for magic cookie ---*/
if(ga_info->magic_cookie != CF_cookie) return FALSE;
/*--- Otherwise valid ---*/
return TRUE;
}
/*----------------------------------------------------------------------------
| Set all ga_info to defaults
----------------------------------------------------------------------------*/
CF_reset(
GA_Info_Ptr ga_info)
{
/*--- Error check ---*/
if(!CF_valid(ga_info)) UT_error("CF_reset: invalid ga_info");
/*--- Default basic parameters ---*/
ga_info->user_data[0] = '\0';
ga_info->function_index = 1;
ga_info->rand_seed = 1;
ga_info->datatype = DT_INT_PERM;
ga_info->ip_flag = IP_RANDOM;
ga_info->ip_data[0] = '\0';
ga_info->chrom_len = 10;
ga_info->pool_size = 100;
ga_info->iter = -1;
ga_info->max_iter = -1;
ga_info->bias = 1.8;
ga_info->gap = 0.0;
ga_info->x_rate = 1.0;
ga_info->mu_rate = 0.0;
ga_info->scale_factor = 0.0;
ga_info->minimize = TRUE;
ga_info->elitist = TRUE;
ga_info->converged = FALSE;
ga_info->use_convergence = TRUE;
/*--- Default operators ---*/
SE_select(ga_info, "roulette");
X_select(ga_info, "order1");
MU_select(ga_info, "swap");
RE_select(ga_info, "append");
GA_select(ga_info, "generational");
ga_info->EV_fun = NULL;
/*--- Default report parameters ---*/
ga_info->rp_type = RP_SHORT;
ga_info->rp_interval = 1;
ga_info->rp_fid = stdout;
ga_info->rp_file[0] = '\0';
/*--- Reset pools ---*/
if(PL_valid(ga_info->old_pool)) PL_reset(ga_info->old_pool);
if(PL_valid(ga_info->new_pool)) PL_reset(ga_info->new_pool);
/*--- Reset best ---*/
if(CH_valid(ga_info->best)) CH_reset(ga_info->best);
}
/*----------------------------------------------------------------------------
| Print out all of the config information
----------------------------------------------------------------------------*/
CF_report(
GA_Info_Ptr ga_info)
{
FILE *fid;
char *sptr;
/*--- Error check ---*/
if(!CF_valid(ga_info)) UT_error("CF_report: invalid ga_info");
if(ga_info->rp_fid == NULL) UT_error("CF_report: invalid ga_info->rp_fid");
fid = ga_info->rp_fid;
/*--- Header ---*/
fprintf(fid,"\nLibGA Version %s\n%s\n%s\n\n", VERSION, COPYRIGHT, COPYRIGHT2);
fprintf(fid,"GA Configuration Information:\n");
fprintf(fid,"-----------------------------\n");
/*--- Basic info --- */
fprintf(fid,"Basic Info\n");
if(ga_info->user_data[0] != '\0')
fprintf(fid," User Data : %s\n", ga_info->user_data);
fprintf(fid," Fuction Index : %d\n", ga_info->function_index);
fprintf(fid," Random Seed : %d\n", ga_info->rand_seed);
fprintf(fid," Data Type : ");
switch(ga_info->datatype) {
case DT_BIT: fprintf(fid,"Bit\n"); break;
case DT_INT: fprintf(fid,"Integer\n"); break;
case DT_INT_PERM: fprintf(fid,"Integer Permutation\n"); break;
case DT_REAL: fprintf(fid,"Real\n"); break;
default: fprintf(fid,"Unspecified\n"); break;
}
fprintf(fid," Init Pool Entered : ");
switch(ga_info->ip_flag) {
case IP_RANDOM : fprintf(fid,"Randomly \n"); break;
case IP_FROM_FILE : fprintf(fid,"From File \n"); break;
case IP_INTERACTIVE: fprintf(fid,"Interactively\n"); break;
default : fprintf(fid,"Unspecified \n"); break;
}
if(ga_info->ip_flag == IP_FROM_FILE) {
fprintf(fid," Initial Pool File : ");
if(ga_info->ip_data[0] == 0)
fprintf(fid,"None\n");
else if(!strcmp(ga_info->ip_data, "UNSPECIFIED"))
fprintf(fid,"Unspecified\n");
else
fprintf(fid,"%s\n", ga_info->ip_data);
}
fprintf(fid," Chromosome Length : %d\n", ga_info->chrom_len);
fprintf(fid," Pool Size : %d\n", ga_info->pool_size);
fprintf(fid," Number of Trials : ");
if(ga_info->max_iter < 0)
fprintf(fid,"Run until convergence\n");
else
fprintf(fid,"%d iterations, %s\n",
ga_info->max_iter,
ga_info->use_convergence ? "or until convergence"
: "ignore convergence"
);
fprintf(fid," Minimize : %s\n",
ga_info->minimize ? "Yes" : "No");
fprintf(fid," Elitism : %s\n",
ga_info->elitist ? "Yes" : "No");
fprintf(fid," Scale Factor : %G\n", ga_info->scale_factor);
/*--- Functions ---*/
fprintf(fid,"\n");
fprintf(fid,"Functions\n");
fprintf(fid," GA : %s (Gap = %G)\n",
GA_name(ga_info), ga_info->gap);
fprintf(fid," Selection : %s ", sptr = SE_name(ga_info));
if(!strcmp(sptr,"rank_biased")) fprintf(fid,"(Bias = %G)", ga_info->bias);
fprintf(fid,"\n");
fprintf(fid," Crossover : %s (Rate = %G)\n",
X_name(ga_info), ga_info->x_rate);
if(ga_info->mu_rate > 0.0)
fprintf(fid," Mutation : %s (Rate = %G)\n",
MU_name(ga_info), ga_info->mu_rate);
fprintf(fid," Replacement : %s\n", RE_name(ga_info));
/*--- Reports ---*/
if(ga_info->rp_type != RP_NONE) {
fprintf(fid,"\n");
fprintf(fid,"Reports\n");
fprintf(fid," Type : %s\n",
ga_info->rp_type == RP_MINIMAL ? "Minimal" :
ga_info->rp_type == RP_SHORT ? "Short" :
ga_info->rp_type == RP_LONG ? "Long" :
"Unknown");
fprintf(fid," Interval : %d\n", ga_info->rp_interval);
if(ga_info->rp_file[0] != 0) {
fprintf(fid," File : ");
if(!strcmp(ga_info->rp_file, "UNSPECIFIED"))
fprintf(fid,"Unspecified\n");
else
fprintf(fid,"%s\n", ga_info->rp_file);
}
}
/*--- Footer ---*/
fprintf(fid,"-----------------------------\n");
/*--- Make sure it is printed immediately ---*/
fflush(fid);
}
/*----------------------------------------------------------------------------
| Read config file
----------------------------------------------------------------------------*/
CF_read(
GA_Info_Ptr ga_info,
char *cfg_name)
{
char str[STRLEN], token[MAXTOK][STRLEN];
int numtok, CF_tokenize();
FILE *fid;
/*--- Error check ---*/
if(!CF_valid(ga_info)) UT_error("CF_read: invalid ga_info");
/*--- Try to open config file ---*/
if((fid = fopen(cfg_name, "r")) == NULL) {
UT_warn("CF_read: Error opening config file.");
return GA_ERROR;
}
/*--- Read each line from config file ---*/
while(fgets(str, STRLEN, fid) != NULL) {
/*--- Convert to tokens ---*/
if((numtok=CF_tokenize(str, token)) <= 0) continue;
/*--- Which command? ---*/
switch(token[0][0]) {
case 'b':
if(!strcmp(token[0], "bias")) {
if(numtok >= 2 && sscanf(token[1], "%f", &ga_info->bias) == 1)
;
else
UT_warn("CF_read: Invalid bias response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'c':
if(!strcmp(token[0], "chrom_len")) {
if(numtok >= 2 && sscanf(token[1], "%d", &ga_info->chrom_len) == 1)
;
else
UT_warn("CF_read: Invalid chrom_len response");
} else if(!strcmp(token[0], "crossover")) {
if(numtok >= 2)
X_select(ga_info, token[1]);
else
UT_warn("CF_read: Invalid crossover response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'd':
if(!strcmp(token[0], "datatype")) {
if(numtok >= 2 && !strcmp(token[1], "bit"))
ga_info->datatype = DT_BIT;
else if(numtok >= 2 && !strcmp(token[1], "int"))
ga_info->datatype = DT_INT;
else if(numtok >= 2 && !strcmp(token[1], "int_perm"))
ga_info->datatype = DT_INT_PERM;
else if(numtok >= 2 && !strcmp(token[1], "real"))
ga_info->datatype = DT_REAL;
else
UT_warn("CF_read: Invalid datatype response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'e':
if(!strcmp(token[0], "elitism")) {
if(numtok >= 2 && !strcmp(token[1], "true"))
ga_info->elitist = TRUE;
else if(numtok >= 2 && !strcmp(token[1], "false"))
ga_info->elitist = FALSE;
else
UT_warn("CF_read: Invalid elitism response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'f':
if(!strcmp(token[0], "function_index")) {
if(numtok >= 2)
sscanf(token[1], "%d", &ga_info->function_index);
else
UT_warn("CF_read: Invalid function_index response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'g':
if(!strcmp(token[0], "gap")) {
if(numtok >= 2 && sscanf(token[1], "%f", &ga_info->gap) == 1)
;
else
UT_warn("CF_read: Invalid gap response");
} else if(!strcmp(token[0], "ga")) {
if(numtok >= 2) {
GA_select(ga_info, token[1]);
if(!strcmp(token[1], "generational")) {
SE_select(ga_info, "roulette");
RE_select(ga_info, "append");
ga_info->rp_interval = 1;
} else if(!strcmp(token[1], "steady_state")) {
SE_select(ga_info, "rank_biased");
RE_select(ga_info, "by_rank");
ga_info->rp_interval = 100;
}
} else
UT_warn("CF_read: Invalid ga response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'i':
if(!strcmp(token[0], "initpool")) {
if(numtok >= 2 && !strcmp(token[1], "random"))
ga_info->ip_flag = IP_RANDOM;
if(numtok >= 2 && !strcmp(token[1], "random01"))
ga_info->ip_flag = IP_RANDOM01;
else if(numtok >= 2 && !strcmp(token[1], "from_file")) {
ga_info->ip_flag = IP_FROM_FILE;
if(numtok >= 3) strcpy(ga_info->ip_data, token[2]);
} else if(numtok >= 2 && !strcmp(token[1], "interactive"))
ga_info->ip_flag = IP_INTERACTIVE;
else
UT_warn("CF_read: Invalid initpool response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'm':
if(!strcmp(token[0], "mutation")) {
if(numtok >= 2)
MU_select(ga_info, token[1]);
else
UT_warn("CF_read: Invalid mutation response");
} else if(!strcmp(token[0], "mu_rate")) {
if(numtok >= 2)
sscanf(token[1], "%f", &ga_info->mu_rate);
else
UT_warn("CF_read: Invalid mu_rate response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'o':
if(!strcmp(token[0], "objective")) {
if(numtok >= 2 && !strcmp(token[1], "minimize"))
ga_info->minimize = TRUE;
else if(numtok >= 2 && !strcmp(token[1], "maximize"))
ga_info->minimize = FALSE;
else
UT_warn("CF_read: Invalid objective response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'p':
if(!strcmp(token[0], "pool_size")) {
if(numtok >= 2)
sscanf(token[1], "%d", &ga_info->pool_size);
else
UT_warn("CF_read: Invalid pool_size response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'r':
if(!strcmp(token[0], "replacement")) {
if(numtok >= 2)
RE_select(ga_info, token[1]);
else
UT_warn("CF_read: Invalid replacement response");
} else if(!strcmp(token[0], "rp_interval")) {
if(numtok >= 2)
sscanf(token[1], "%d", &ga_info->rp_interval);
else
UT_warn("CF_read: Invalid rp_interval response");
} else if(!strcmp(token[0], "rp_type")) {
if(numtok >= 2 && !strcmp(token[1], "minimal"))
ga_info->rp_type = RP_MINIMAL;
else if(numtok >= 2 && !strcmp(token[1], "short"))
ga_info->rp_type = RP_SHORT;
else if(numtok >= 2 && !strcmp(token[1], "long"))
ga_info->rp_type = RP_LONG;
else if(numtok >= 2 && !strcmp(token[1], "none"))
ga_info->rp_type = RP_NONE;
else
UT_warn("CF_read: Invalid rp_type response");
} else if(!strcmp(token[0], "rp_file")) {
if(numtok >= 2) {
char *file_mode = "a";
/*--- Save file name ---*/
strcpy(ga_info->rp_file, token[1]);
/*--- Get file mode if provided ---*/
if(numtok >= 3)
file_mode = token[2];
/*--- Open report file ---*/
ga_info->rp_fid = fopen(ga_info->rp_file, file_mode);
if(ga_info->rp_fid == NULL)
UT_error("CF_read: error opening report file");
} else
UT_warn("CF_read: Invalid rp_file response");
} else if(!strcmp(token[0], "rand_seed")) {
if(numtok >= 2 && !strcmp(token[1], "my_pid"))
ga_info->rand_seed = getpid();
else if(numtok >= 2)
sscanf(token[1], "%d", &ga_info->rand_seed);
else
UT_warn("CF_read: Invalid rand_seed response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 's':
if(!strcmp(token[0], "selection")) {
if(numtok >= 2)
SE_select(ga_info, token[1]);
else
UT_warn("CF_read: Invalid selection response");
} else if(!strcmp(token[0], "stop_after")) {
if(numtok == 2 && !strcmp(token[1], "convergence")) {
ga_info->use_convergence = TRUE;
ga_info->max_iter = -1;
} else if(numtok >= 2) {
sscanf(token[1], "%d", &ga_info->max_iter);
if(ga_info->max_iter < 1)
UT_warn("CF_read: Invalid number for stop_after");
ga_info->use_convergence = TRUE;
if(numtok > 2 && !strcmp(token[2], "ignore_convergence"))
ga_info->use_convergence = FALSE;
} else {
UT_warn("CF_read: Invalid stop_after response");
}
} else
UT_warn("CF_read: Unknown config command");
break;
case 'u':
if(!strcmp(token[0], "user_data")) {
if(numtok >= 2)
strcpy(ga_info->user_data, token[1]);
else
UT_warn("CF_read: Invalid user_data response");
} else
UT_warn("CF_read: Unknown config command");
break;
case 'x':
if(!strcmp(token[0], "x_rate")) {
if(numtok >= 2 && sscanf(token[1], "%f", &ga_info->x_rate) == 1)
;
else
UT_warn("CF_read: Invalid x_rate response");
} else
UT_warn("CF_read: Unknown config command");
break;
default:
UT_warn("CF_read: Unknown config command");
}
}
/*--- PATCH 1 BEGIN ---*/
/* Many thanks to Paul-Erik Raue (peraue@cs.vu.nl)
* for finding this bug.
*
* Close the configuration file
*/
fclose(fid);
/*--- PATCH 1 END ---*/
}
/*----------------------------------------------------------------------------
| Convert input line to tokens
----------------------------------------------------------------------------*/
CF_tokenize(
char *line, char token[MAXTOK][STRLEN])
{
int numtok, i, j, len;
numtok = 0;
len = strlen(line);
for(i = 0; i < len; ) {
/*--- Find token ---*/
while(isspace(line[i]) && i < len) i++;
/*--- Skip comments & blank lines ---*/
if(i >= len || line[i] == '#' || line[i] == '\n') break;
/*--- Save token ---*/
for(j = 0; !isspace(line[i]) && i < len; i++, j++)
token[numtok][j] = line[i];
token[numtok++][j] = 0;
}
return numtok;
}
/*----------------------------------------------------------------------------
| Verify configuration
----------------------------------------------------------------------------*/
CF_verify(
GA_Info_Ptr ga_info)
{
/*--- Error check ---*/
if(!CF_valid(ga_info)) UT_error("CF_verify: invalid ga_info");
switch(ga_info->datatype) {
case DT_BIT:
case DT_INT:
case DT_INT_PERM:
case DT_REAL:
break;
default: UT_error("CF_verify: Invalid datatype");
}
switch(ga_info->ip_flag) {
case IP_FROM_FILE:
if(ga_info->ip_data[0] == '\0')
UT_error("CF_verify: no file specified for initpool");
break;
case IP_INTERACTIVE:
case IP_RANDOM:
case IP_NONE:
case IP_RANDOM01:
break;
default: UT_error("CF_verify: Invalid ip_flag");
}
if(ga_info->chrom_len <= 0)
UT_error("CF_verify: invalid chromosome length");
if(ga_info->pool_size <= 0)
UT_error("CF_verify: invalid pool size");
if(ga_info->SE_fun == NULL)
UT_error("CF_verify: no selection function specified");
if(ga_info->X_fun == NULL)
UT_error("CF_verify: no crossover function specified");
if(ga_info->x_rate < 0.0 || ga_info->x_rate > 1.0)
UT_error("CF_verify: invalid crossover rate");
if(ga_info->mu_rate < 0.0)
UT_error("CF_verify: invalid mutation rate");
if(ga_info->mu_rate > 0.0 && ga_info->MU_fun == NULL)
UT_error("CF_verify: no mutation function specified");
if(ga_info->RE_fun == NULL)
UT_error("CF_verify: no replacement function specified");
if(ga_info->EV_fun == NULL)
UT_error("CF_verify: no evaluation function specified");
if(ga_info->GA_fun == NULL)
UT_error("CF_verify: no ga function specified");
if(ga_info->gap < 0.0 || ga_info->gap > 1.0)
UT_error("CF_verify: invalid generation gap");
if(ga_info->minimize != TRUE && ga_info->minimize != FALSE)
UT_error("CF_verify: illegal value for minimize");
if(ga_info->elitist != TRUE && ga_info->elitist != FALSE)
UT_error("CF_verify: illegal value for elitism");
switch(ga_info->rp_type) {
case RP_NONE:
case RP_MINIMAL:
case RP_SHORT:
case RP_LONG:
break;
default: UT_error("CF_verify: Invalid report type");
}
if(ga_info->rp_interval <= 0)
UT_error("CF_verify: invalid report interval");
}
/*============================================================================
| (c) Copyright Arthur L. Corcoran, 1992, 1993. All rights reserved.
| (c) Copyright IA UPM - Group 5, 2020. All rights reserved.
|
| Crossover operators
|
| Bit String Representations
| X_simple() - simple crossover
| X_uniform() - uniform crossover
|
| Order-Based Integer Representations
| X_order1() - order1 (Starkweather, et. al., 1991 GA Conf.)
| X_order2() - order2 (Starkweather, et. al., 1991 GA Conf.)
| X_pos() - position (Starkweather, et. al., 1991 GA Conf.)
| X_cycle() - cycle (Starkweather, et. al., 1991 GA Conf.)
| X_pmx() - PMX (Starkweather, et. al., 1991 GA Conf.)
| X_uox() - uniform order crossover
| X_rox() - relative order crossover
| X_asex() - asexual crossover
| X_do_asex() - helper for X_asex()
|
| Interface
| X_table[] - used in selection of crossover method
| X_set_fun() - set and select user defined crossover function
| X_select() - select crossover method by name
| X_name() - get name of current crossover function
| X_fun() - setup and perform current crossover operator
|
| Utility
| X_gen_xp() - generate a random crossover point
| X_gen_2_xp() - generate two sorted, random crossover points
| X_gen_4_xp() - generate four sorted, random crossover points
| X_init_kids() - reset children for crossover
| X_map() - find allele in a chromosome
|
| NOTE: Crossover points should always be thought of as "inclusive"
============================================================================*/
/*============================================================================
| Crossover interface
============================================================================*/
/*----------------------------------------------------------------------------
| Table for selection of crossover method
----------------------------------------------------------------------------*/
FN_Table_Type X_table[] = {
{ NULL, NULL }, /* user defined function */
{ "simple", X_simple },
{ "uniform", X_uniform },
{ "order1", X_order1 },
{ "order2", X_order2 },
{ "position", X_pos, },
{ "cycle", X_cycle, },
{ "pmx", X_pmx, },
{ "uox", X_uox, },
{ "rox", X_rox, },
{ "asexual", X_asex, },