1 //
2 // chromosome_example.c
3 //
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "liquid.internal.h"
9
main()10 int main() {
11 unsigned int bits_per_trait[] = {4, 8, 8, 4};
12 chromosome p1 = chromosome_create(bits_per_trait, 4);
13 chromosome p2 = chromosome_create(bits_per_trait, 4);
14 chromosome c = chromosome_create(bits_per_trait, 4);
15
16 // 0000 11111111 00000000 1111
17 p1->traits[0] = 0x0;
18 p1->traits[1] = 0xFF;
19 p1->traits[2] = 0x00;
20 p1->traits[3] = 0xF;
21
22 // 0101 01010101 01010101 0101
23 p2->traits[0] = 0x5;
24 p2->traits[1] = 0x55;
25 p2->traits[2] = 0x55;
26 p2->traits[3] = 0x5;
27
28 printf("parent [1]:\n");
29 chromosome_print(p1);
30
31 printf("parent [2]:\n");
32 chromosome_print(p2);
33
34 printf("\n\n");
35
36 //
37 // test crossover
38 //
39
40 printf("testing crossover...\n");
41
42 chromosome_crossover(p1, p2, c, 0);
43 // .... ........ ........ ....
44 // 0101 01010101 01010101 0101
45 chromosome_print(c);
46
47 chromosome_crossover(p1, p2, c, 4);
48 // 0000 ........ ........ ....
49 // .... 01010101 01010101 0101
50 chromosome_print(c);
51
52 chromosome_crossover(p1, p2, c, 6);
53 // 0000 11...... ........ ....
54 // .... ..010101 01010101 0101
55 chromosome_print(c);
56
57 chromosome_crossover(p1, p2, c, 14);
58 // 0000 11111111 00...... ....
59 // .... ........ ..010101 0101
60 chromosome_print(c);
61
62 chromosome_crossover(p1, p2, c, 24);
63 // 0000 11111111 00000000 1111
64 // .... ........ ........ ....
65 chromosome_print(c);
66
67 //
68 // test mutation
69 //
70
71 printf("testing mutation...\n");
72
73 unsigned int i;
74 for (i=0; i<24; i++) {
75 chromosome_reset(c);
76 chromosome_mutate(c,i);
77 // 0000 01000000 00000000 0000
78 chromosome_print(c);
79 }
80
81 chromosome_destroy(p1);
82 chromosome_destroy(p2);
83 chromosome_destroy(c);
84
85 return 0;
86 }
87
88