-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticText.c
More file actions
225 lines (162 loc) · 4.07 KB
/
GeneticText.c
File metadata and controls
225 lines (162 loc) · 4.07 KB
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
/* by Kemeridis */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
// String size
#define SIZE 100
// Population Size
#define POP 100
// Mutation chance
#define MUT 10
// Population Struct
struct population {
char chr_so[SIZE];
int fit;
};
struct population pope[POP];
//Random population generation function
void gene_gen(char* word) {
char temp[strlen(word)];
temp[strlen(word)] = '\0';
//generate random genes
for(int j = POP; j--;) {
for(int i = 0; i < strlen(word); i++) {
temp[i] = rand() % (127 - 32) + 32;
}
memcpy(pope[j].chr_so, temp, strlen(temp));
strcpy(pope[j].chr_so, temp);
}
}
//fitness function
void pop_fitness(char* word) {
for(int i = POP; i--;){
pope[i].fit = 0;
}
//compare strings with the given word and give fitness
for(int j = POP; j--;) {
for(int i = 0; i < strlen(word); i++) {
if(pope[j].chr_so[i] != word[i]) {
pope[j].fit += 1;
}
}
}
}
// Mutation function
char* mutation(char* mut, char* word) {
int ch_rand = rand() % strlen(word);
//change 1 random gene from the kids
for(int i = 0; i < strlen(word); i++) {
if(i == ch_rand) {
mut[i] = rand() % (127 - 32) + 32;
}
}
mut[strlen(word)] = '\0';
//return the mutated string
return mut;
}
void crossover(char* father, char* mother,char* word) {
char temp[strlen(word)];
char kid1[strlen(word)];
char kid2[strlen(word)];
kid1[strlen(word)] = '\0';
kid2[strlen(word)] = '\0';
temp[strlen(word)] = '\0';
int ran_mut;
// Get a random crossover point
int ch_rand = rand() % strlen(word);;
//crossover parents
for(int i = ch_rand; i < strlen(word); i++) {
temp[i] = father[i];
father[i] = mother[i];
mother[i] = temp[i];
}
// copy parents string into the kids arrays
strcpy(kid1, father);
strcpy(kid2, mother);
// chance to get into mutation
ran_mut = 1 + rand() % 100;
//chance for the first kid to mutate
if(ran_mut <= MUT) {
strcpy(kid1, mutation(kid1, word));
}
ran_mut = 0;
// chance to get into mutation
ran_mut = 1 + rand() % 100;
//chance for the second kid to mutate
if(ran_mut <= MUT) {
strcpy(kid2, mutation(kid2, word));
}
//kill 1 random and store the kid ---->
ch_rand = rand() % POP;
strcpy(pope[ch_rand].chr_so, kid1);
ch_rand = 0;
ch_rand = rand() % POP;
strcpy(pope[ch_rand].chr_so, kid2);
// <------ kill 1 random and store the kid
// free up those arrays
for(int i = strlen(word); i--;){
kid1[i] = ' ';
kid2[i] = ' ';
temp[i] = ' ';
}
}
void selection(char* word) {
char* father = (char*) malloc(strlen(word));
char* mother = (char*) malloc(strlen(word));;
int fit_fath;
int fit_mo;
int choo_get;
int mesa;
int max = strlen(word);
// Choose random number of individuals from population
choo_get = rand() % POP;
// Get Parents -------->
for(int i = 0; i < choo_get; i++) {
mesa = rand() % choo_get;
if(pope[mesa].fit < max) {
strcpy(father, pope[mesa].chr_so);
max = pope[mesa].fit;
}
}
max = strlen(word);
// Choose random number of individuals from population
choo_get = rand() % POP;
for(int i = 0; i < choo_get; i++) {
mesa = rand() % choo_get;
if(pope[mesa].fit < max) {
strcpy(mother, pope[mesa].chr_so);
max = pope[mesa].fit;
}
}
// <--------- get parerts
// into crossover function --->
crossover(father, mother, word);
// free up those arrays
for(int i = strlen(word); i--;){
father[i] = ' ';
mother[i] = ' ';
}
}
int main(void) {
srand((unsigned int)time(NULL));
char* word = (char*) malloc(SIZE);
int True = 1;
printf("Give a word or a string or whatever: ");
gets(word);
word[SIZE] = '\0';
gene_gen(word);
pop_fitness(word);
int b = 0;
while(True) {
selection(word);
pop_fitness(word);
for(int i = POP; i--;) {
if(strcmp(pope[i].chr_so, word) == 0) {
printf("I found: %s in gene: %d", pope[i].chr_so, b);
True = 0;
}
}
b++;
}
}