-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic_no_parallelism.cpp
231 lines (216 loc) · 6.5 KB
/
genetic_no_parallelism.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
#include <stdio.h> //to use the printf function
#include <conio.h> //to use the getche function
#include <stdlib.h> //to use the rand fuFnction
#include <time.h> //to use the time function
#include <math.h> //to use the pow function
#define GENE_COUNT 10
#define POPULATION 100
#define ITER 500
#define STATIC_POP_FILE "population.txt"
#define MUTATION_FAC 5
typedef struct Chrom
{ // creating the chrom structure
short int bit[GENE_COUNT];
int fit;
double prob;
} chrom;
void init_pop(chrom popcurrent[POPULATION]); // defining the functions that we will use
int fitness(chrom chrom);
void select(chrom popcurrent[POPULATION], chrom selected[POPULATION]);
void crossover(chrom popnext[POPULATION]);
void mutation(chrom popnext[POPULATION]);
void sort(chrom popcurrent[POPULATION]);
void print_pop(chrom pop[POPULATION]);
int main()
{
chrom popcurrent[POPULATION];
chrom popnext[POPULATION];
printf("Starting...\n");
init_pop(popcurrent);
printf("Initial Population:\n");
print_pop(popcurrent);
for (int i = 0; i < ITER; i++)
{
select(popcurrent, popnext);
crossover(popnext);
mutation(popnext);
for (int j = 0; j < POPULATION; j++)
{
popcurrent[j] = popnext[j];
}
}
print_pop(popcurrent);
return 0;
}
void print_pop(chrom pop[POPULATION])
{ // prints a given population(array of chromosomes)
long long int total_fitness = 0;
int max_fit = 0;
int min_fit = 101;
for (int i = 0; i < POPULATION; i++)
{
for (int j = GENE_COUNT - 1; j >= 0; j--)
{
printf("%d", pop[i].bit[j]);
}
printf(" %d %f\n", pop[i].fit, pop[i].prob);
total_fitness += pop[i].fit;
if (pop[i].fit > max_fit)
{
max_fit = pop[i].fit;
}
if (pop[i].fit < min_fit)
{
min_fit = pop[i].fit;
}
}
printf("Total Fitness is: %lld\n", total_fitness);
printf("Average Fitness is: %f\n", total_fitness / (double)POPULATION);
printf("Maximum Fitness is: %d\n", max_fit);
printf("Minimum Fitness is: %d\n", min_fit);
printf("\n");
for (int i = 0; i < 20; i++)
{
printf("-");
}
printf("\n\n");
}
chrom convert_str_2_chrom(char *chrom_str)
{ // Accepts a string of 1s & 0s to the length of GENE_COUNT.
chrom chromo; // Will return the corresponding chromosome.
for (int i = 0; chrom_str[i]; i++)
{
if (i >= GENE_COUNT || (chrom_str[i] != '0' && chrom_str[i] != '1'))
{
printf("Error!\nInvalid string as a chromosome.\n");
exit(EXIT_FAILURE);
}
chromo.bit[i] = chrom_str[i] - '0';
}
return chromo;
}
void init_pop(chrom popcurrent[POPULATION])
{ // Read Initial population from a static file. STATIC_POP_FILE
FILE *population;
printf("Opening input file...\n");
population = fopen(STATIC_POP_FILE, "rt");
if (population == NULL)
{
printf("Error!\nCouldn't open population file.\n");
exit(EXIT_FAILURE);
}
printf("File opened successfuly.\nReading data...\n");
for (int i = 0; i < POPULATION; i++)
{
char chrom_str[GENE_COUNT + 2];
fscanf(population, "%s", chrom_str);
chrom_str[GENE_COUNT] = 0;
popcurrent[i] = convert_str_2_chrom(chrom_str);
}
printf("Data extracted.\n");
long long int total_fitness = 0;
for (int i = 0; i < POPULATION; i++)
{
popcurrent[i].fit = fitness(popcurrent[i]);
total_fitness += popcurrent[i].fit;
}
for (int i = 0; i < POPULATION; i++)
{
popcurrent[i].prob = (double)popcurrent[i].fit / total_fitness;
}
printf("Initial population ready.\n");
}
int fitness(chrom chrom)
{
int bit_number = 0;
int sum = 0;
while (bit_number < GENE_COUNT)
{
sum += (chrom.bit[bit_number] * ((int)pow(2, bit_number)));
bit_number++;
}
sum = sum % 101;
return (sum); // return the value of sum
}
void select(chrom popcurrent[POPULATION], chrom selected[POPULATION])
{ // perfom selection on the curr pop considering prob.
sort(popcurrent);
srand((unsigned)time(NULL));
for (int j = 0; j < POPULATION; j++)
{
double random = (double)rand() / RAND_MAX;
double cumulative_prob = 0;
for (int i = 0; i < POPULATION; i++)
{
cumulative_prob += popcurrent[i].prob;
if (random < cumulative_prob)
{
selected[j] = popcurrent[i];
break;
}
}
}
}
void sort(chrom popcurrent[POPULATION])
{ // sort pop by their prob
chrom temp;
for (int i = 0; i < POPULATION; i++)
{
for (int j = 0; j < POPULATION - 1; j++)
{
if (popcurrent[j].fit > popcurrent[j + 1].fit)
{
temp = popcurrent[j + 1];
popcurrent[j + 1] = popcurrent[j];
popcurrent[j] = temp;
}
}
}
}
void crossover(chrom popnext[POPULATION])
{ // crossover function takes a pointer to array of chromes
int random;
random = rand();
chrom temp_child;
random = ((random % (GENE_COUNT - 1)) + 1); // random cross over for first child (child of first popnext chrom and the last one)
for (int i = 0; i < random; i++)
{
temp_child.bit[i] = popnext[0].bit[i];
}
for (int i = random; i < GENE_COUNT - 1; i++)
{
temp_child.bit[i] = popnext[POPULATION - 1].bit[i];
}
for (int chrom_counter = 0; chrom_counter < POPULATION; chrom_counter++)
{
random = rand();
random = ((random % (GENE_COUNT - 1)) + 1);
for (int i = random; i < GENE_COUNT; i++)
{
popnext[chrom_counter].bit[i] = popnext[chrom_counter + 1].bit[i];
}
}
popnext[POPULATION - 1] = temp_child; // the last popnext chrom now becomes the new child
long long int total_fitness = 0;
for (int i = 0; i < POPULATION; i++)
{
popnext[i].fit = fitness(popnext[i]);
total_fitness += popnext[i].fit;
}
for (int i = 0; i < POPULATION; i++)
{
popnext[i].prob = (double)popnext[i].fit / total_fitness;
}
}
void mutation(chrom popnext[POPULATION])
{
srand((unsigned)time(NULL));
int prob = rand() % 101;
if (prob <= MUTATION_FAC)
{
int chrom = rand() % (POPULATION);
int gene = rand() % (GENE_COUNT);
popnext[chrom].bit[gene] = (popnext[chrom].bit[gene] == 1) ? 0 : 1;
popnext[chrom].fit = fitness(popnext[chrom]);
}
}