Genetic Algorithm NEED HELP

So I've developed my code near completion. Each function operates functionally on there own. However when run I get repeated values. I believe this is due to the fact that whenever I try to index my 'child' into my temporary child array in a for loop, only the first child given from breed and mutation is indexed for every index in the array. I AM SO LOST PLEASE HELP!!!
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <array>
#include <cstring>

 using namespace std;

 
 
 
 
 
void
buildPopulation (string population[], int length) 
{
  
int z;
  
int i;
  
int a;
  
string organism = "";
  
static const char alphanum[53] =
    { 32, 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, 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 };
  
srand (time (NULL));
  
i = 0;
  
z = 1;
  
while (z <= 200)
    
    {
      
organism = "";
      
for (int a = 1; a <= length; a++)
	
	{
	  
organism = organism + alphanum[rand () % 52];
	
} 
population[i] = organism;
      
i++;
      
z++;

} 
} 
 
void

calculatefitness (string population[], int length, string desired,
		  double percpop[], string numpop[]) 
{
  
double counter = 0;
  
string organism;
  
string numorgan;
  
double maxfit = 0;
  
for (int i = 0; i < 200; i++)
    
    {
      
organism = population[i];
      
for (int j = 0; j < length; j++)
	
	{
	  
if (organism.at (j) == desired.at (j))
	    
	    {
	      
organism[j] =
	      {
	      '1'};
	    
}
	  
	  else
	    
	    {
	      
organism[j] =
	      {
	      '0'};
	    
}
	
}
      
numpop[i] = organism;
    
}
  
for (int a = 0; a < 200; a++)
    
    {
      
counter = 0;
      
numorgan = numpop[a];
      
for (int b = 0; b < length; b++)
	
	{
	  
if (numorgan.at (b) == '1')
	    
	    {
	      
counter++;
	    
}
	
}
      
percpop[a] = (counter) / (length);
    
}

}


 
void
otherticketinsert (int matingpool[], int index, int copy) 
{
  
for (int i = 0; i < 2000; i++)
    
    {
      
if (matingpool[i] == -1)
	
	{
	  
for (int j = 0; j < copy; j++)
	    
matingpool[i + j] = index;
	  
break;
	
}
    
 
}

}


 
void
buildMatingPool (double percpop[], int matingpool[], double normalpop[],
		 int factor) 
{
  
double largest = 0;
  
int copy;
  
for (int i = 0; i < 200; i++)
    
    {
      
if (largest < percpop[i])
	
	{
	  
largest = percpop[i];
	
}
    
}
  
for (int j = 0; j < 200; j++)
    
    {
      
normalpop[j] = percpop[j] / largest;
  
} 
for (int a = 0; a < 200; a++)
    
    {
      
copy = normalpop[a] * factor;
      
otherticketinsert (matingpool, a, copy);

} 
} 
 
void

arrayrando (int array[]) 
{
  
srand (time (NULL));
  
int x = array[rand () % 2000];

} 
 
string breed (string population[], int matingpool[],
		    int desiredlength) 
{
  
int ticketsgiven;
  
string child;
  
srand (time (NULL));
  
int w, x, y, z;
  
string a, b, c, d;
  
for (int m = 0; m < 2000; m++)
    
    {
      
 
if (matingpool[m] == -1)
	
	{
	  
ticketsgiven = (m - 1);
	  
break;
	
}
      
      else
	
	{
	  
ticketsgiven = 2000;
	
}
    
}
  
int realparents[ticketsgiven] = { 0 };
  
for (int n = 0; n < ticketsgiven; n++)
    
    {
      
realparents[n] = matingpool[n];
    
} 
w = realparents[rand () % ticketsgiven + 0];
  
x = realparents[rand () % ticketsgiven + 0];
  
y = rand () % (desiredlength - 1) + 0;
  
a = population[w];
  
b = population[x];
  
a.erase ((y + 1), (desiredlength));
  
b.erase (0, y + 1);
  
c = a + b;
  
child = c;
  
return child;

}


 
 
string mutation (string child, int length) 
{
  
double length1 = length;
  
static const char alphanum[53] =
    { 32, 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, 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 };
  
srand (time (NULL));
  
int x = rand () % 100 + 1;
  
if (x <= 4)
    
    {
      
char z = alphanum[rand () % 52];
      
int y = rand () % (length - 1) + 0;
      
child[y] = z;
    
}
  
return child;

}


 
 
int
main () 
{
  
int counter;
  
int i;
  
int length;
  
int factor = 10;
  
string child;
  
string mutatedchild;
  
double largest = 0;
  
string desired =
  {
  ""};
  
int matingpool[2000];
  
for (int a = 0; a < 2000; a++)
    
    {
      
matingpool[a] = -1;
    
} 
double percpop[200] = { 0 };
  
double normalpop[200] = { 0 };
  
string population[200] =
  {
  ""};
  
string numpop[200] =
  {
  ""};
  
string children[200] =
  {
  ""};
  
cout << "What would you like me to say?" << endl;
  
getline (cin, desired);
  
length = desired.size ();
  
buildPopulation (population, length);
  
for (int u = 0; u < 2000; u++)
    
    {
      
counter++;
      
calculatefitness (population, length, desired, percpop, numpop);
      
buildMatingPool (percpop, matingpool, normalpop, factor);
      
for (int i = 0; i < 200; i++)
	
	{
	  
if (largest < percpop[i])
	    
	    {
	      
largest = percpop[i];
	    
}
	
}
      
cout << "Generation : " << counter << "Fitness : " << largest << endl;
      
for (int k = 0; k < 200; k++)
	
	{
	  
child = breed (population, matingpool, length);
	  
mutatedchild = mutation (child, length);
	  
children[k] = mutatedchild;
      
} 
for (int j = 0; j < 200; j++)
	
	{
	  
population[j] = children[j];
    
} 
} 
cout << "Fitness of 1 was reached at generation : " << counter <<
    endl;

 
} 
 
 
return 0;

}
Is the code as badly indented in your IDE as what you've posted us to look at.
https://en.wikipedia.org/wiki/Indentation_style

Your main appears broken, with un-matched braces.
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <array>
#include <cstring>
using namespace std;

void buildPopulation(string population[], int length)
{
  int z;
  int i;
  int a;

  string organism = "";
  static const char alphanum[53] =
      { 32, 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, 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
  };

  srand(time(NULL));
  i = 0;
  z = 1;

  while (z <= 200)
  {
    organism = "";
    for (int a = 1; a <= length; a++)
    {
      organism = organism + alphanum[rand() % 52];
    }
    population[i] = organism;
    i++;
    z++;
  }
}

void calculatefitness(string population[], int length, string desired, double percpop[], string numpop[])
{
  double counter = 0;
  string organism;
  string numorgan;
  double maxfit = 0;

  for (int i = 0; i < 200; i++)
  {
    organism = population[i];
    for (int j = 0; j < length; j++)
    {
      if (organism.at(j) == desired.at(j))
      {
        organism[j] = {'1'};
      }
      else
      {
        organism[j] = {'0'};
      }
    }
    numpop[i] = organism;
  }

  for (int a = 0; a < 200; a++)
  {
    counter = 0;
    numorgan = numpop[a];
    for (int b = 0; b < length; b++)
    {
      if (numorgan.at(b) == '1')
      {
        counter++;
      }
    }
    percpop[a] = (counter) / (length);
  }
}

void otherticketinsert(int matingpool[], int index, int copy)
{
  for (int i = 0; i < 2000; i++)
  {
    if (matingpool[i] == -1)
    {
      for (int j = 0; j < copy; j++)
        matingpool[i + j] = index;
      break;
    }
  }
}

void buildMatingPool(double percpop[], int matingpool[], double normalpop[], int factor)
{
  double largest = 0;
  int copy;

  for (int i = 0; i < 200; i++)
  {
    if (largest < percpop[i])
    {
      largest = percpop[i];
    }
  }

  for (int j = 0; j < 200; j++)
  {
    normalpop[j] = percpop[j] / largest;
  }

  for (int a = 0; a < 200; a++)
  {
    copy = normalpop[a] * factor;
    otherticketinsert(matingpool, a, copy);
  }
}

void arrayrando(int array[])
{
  srand(time(NULL));
  int x = array[rand() % 2000];
}

string breed(string population[], int matingpool[], int desiredlength)
{
  int ticketsgiven;
  string child;
  srand(time(NULL));

  int w, x, y, z;
  string a, b, c, d;
  for (int m = 0; m < 2000; m++)
  {
    if (matingpool[m] == -1)
    {
      ticketsgiven = (m - 1);
      break;
    }
    else
    {
      ticketsgiven = 2000;
    }
  }

  int realparents[ticketsgiven] = { 0 };
  for (int n = 0; n < ticketsgiven; n++)
  {
    realparents[n] = matingpool[n];

  }
  w = realparents[rand() % ticketsgiven + 0];
  x = realparents[rand() % ticketsgiven + 0];
  y = rand() % (desiredlength - 1) + 0;
  a = population[w];
  b = population[x];
  a.erase((y + 1), (desiredlength));
  b.erase(0, y + 1);
  c = a + b;
  child = c;
  return child;
}

string mutation(string child, int length)
{
  double length1 = length;

  static const char alphanum[53] =
      { 32, 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, 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
  };

  srand(time(NULL));
  int x = rand() % 100 + 1;

  if (x <= 4)
  {
    char z = alphanum[rand() % 52];
    int y = rand() % (length - 1) + 0;
    child[y] = z;
  }

  return child;
}


int main()
{
  int counter;
  int i;
  int length;
  int factor = 10;
  string child;
  string mutatedchild;
  double largest = 0;
  string desired = {""};
  int matingpool[2000];

  for (int a = 0; a < 2000; a++)
  {
    matingpool[a] = -1;
  }

  double percpop[200] = { 0 };
  double normalpop[200] = { 0 };

  string population[200] = {""};
  string numpop[200] = {""};
  string children[200] = {""};

  cout << "What would you like me to say?" << endl;

  getline(cin, desired);

  length = desired.size();

  buildPopulation(population, length);

  for (int u = 0; u < 2000; u++)
  {
    counter++;
    calculatefitness(population, length, desired, percpop, numpop);
    buildMatingPool(percpop, matingpool, normalpop, factor);
    for (int i = 0; i < 200; i++)
    {
      if (largest < percpop[i])
      {
        largest = percpop[i];
      }
    }

    cout << "Generation : " << counter << "Fitness : " << largest << endl;

    for (int k = 0; k < 200; k++)
    {
      child = breed(population, matingpool, length);
      mutatedchild = mutation(child, length);
      children[k] = mutatedchild;
    }

    for (int j = 0; j < 200; j++)
    {
      population[j] = children[j];
    }
  }

  cout << "Fitness of 1 was reached at generation : " << counter << endl;
}

//!! at this point, the braces no longer match up
return 0;

}


Other points.
1. Way too many calls to srand(time(NULL));
You do this ONCE at the start of main, not all over the place.
srand(time(NULL)); is like saying srand(3); in a program which takes less than a second to run.

2. Some oddly placed break statements (like line 87 and 136) which will cause loops to exit early.

3. arrayrando does nothing useful.

4. WAY too many single letter identifiers - what do they all mean?
Useless for anyone but you who has to read and comprehend the code.
Will also be useless to you as well if you get distracted for more than a couple of days away from staring at the code.



Besides this what salem_c said, the whole code chunk lacks of comments.
It's said that code should at best speaking for itself, but it would harder to understand the code.
A good approach is, putting over each function a brief comment which says what the function does,
and what its arguments will stand for, and what it returns, if so.

At the time I'm trying to detect the errors of your code, but it's somewhat hard to read without comments.
After some digging into the code, I guess I found the error. It was the matepool[] which had to be resetted after each loop turn.

Here the fixed code:
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <array>
#include <cstring>

using namespace std;

const int MAX_LOOP = 2000;  // max count of iterations
const int MAX_POOL = 2000;  // max size of matingpool[]
const int POP_COUNT = 200;  // max count of organisms in the pool

/* ASCII-values of SPACE, A-Z, a-z.
 */
const char alphanum[53] =
{   32, 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, 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
};
 
/* Creates strings of length 'length', filled with randomly choosen
 * alpanumeric characters.
 */
void
buildPopulation (string population[], int length)
{

    int z;
    int i;
    int a;
    string organism = "";

    i = 0;
    z = 1;
    while (z <= POP_COUNT)
    {
        organism = "";
        for (int a = 1; a <= length; a++)
        {
            organism = organism + alphanum[rand () % 52];
        }
        population[i] = organism;
        i++;
        z++;
    }
}

/* Calculates the divergence between each 'population[i] string
 *    and string 'desired'.
 * 'percpop' array becomes fitness level (between 0 and 1).
 * 'numpop' becomes for each letter which matches the letter of
 *    'desired' a 1, otherwhise a 0.
 * 'length holds the length of the organisms.
 */
void
calculatefitness (string population[], int length, string desired,
                  double percpop[], string numpop[])
{
    double counter = 0;
    string organism;
    string numorgan;
    double maxfit = 0;

    for (int i = 0; i < POP_COUNT; i++)
    {
        organism = population[i];
        for (int j = 0; j < length; j++)
        {
            if (organism.at (j) == desired.at (j))
            {
                organism[j] = {'1' };
            }
            else
            {
                organism[j] = { '0' };
            }
        }
        numpop[i] = organism;
    }
    for (int a = 0; a < POP_COUNT; a++)
    {
        counter = 0;
        numorgan = numpop[a];
        for (int b = 0; b < length; b++)
        {
            if (numorgan.at (b) == '1')
            {
                counter++;
            }
        }
        percpop[a] = (counter) / (length);
    }
}

/* Fills mating pool from first -1-found with 'copy' counts
 * of value 'index'.
 */
void
otherticketinsert (int matingpool[], int index, int copy)
{
    for (int i = 0; i < MAX_POOL; i++)
    {
        if (matingpool[i] == -1)
        {
            for (int j = 0; j < copy; j++)
            {
                matingpool[i + j] = index;
            }
            break;
        }
    }
}


/* Fills matingpool[] with copys of percpop[], taking account
 * of the fitness oft the populations.
 */
void
buildMatingPool (double percpop[], int matingpool[], double normalpop[],
                 int factor)
{
    double largest = 0;
    int copy;

    for (int i = 0; i < POP_COUNT; i++)
    {
        if (largest < percpop[i])
        {
            largest = percpop[i];
        }
    }
    for (int j = 0; j < POP_COUNT; j++)
    {
        normalpop[j] = percpop[j] / largest;
    }
    for (int index = 0; index < POP_COUNT; index++)
    {
        copy = normalpop[index] * factor;
        otherticketinsert (matingpool, index, copy);
    }
}

/* Working is still unclear to me!
 */
string breed (string population[], int matingpool[],
              int desiredlength)
{

    int ticketsgiven;
    string child;
    int w, x, y, z;
    string a, b, c, d;

    for (int m = 0; m < MAX_POOL; m++)
    {
        if (matingpool[m] == -1)
        {
            ticketsgiven = (m - 1);
            break;
        }
        else
        {
            ticketsgiven = MAX_LOOP;
        }
    }
    int realparents[ticketsgiven] = { 0 };
    for (int n = 0; n < ticketsgiven; n++)
    {
        realparents[n] = matingpool[n];
    }
    w = realparents[rand () % ticketsgiven + 0];
    x = realparents[rand () % ticketsgiven + 0];
    y = rand () % (desiredlength - 1) + 0;
    a = population[w];
    b = population[x];
    a.erase ((y + 1), (desiredlength));
    b.erase (0, y + 1);
    c = a + b;
    child = c;
    
    return child;
}

/* Possibly mutates the string at a random position with a random value.
 */
string mutation (string child, int length)
{
    int x = rand () % 100 + 1;

    if (x <= 4)
    {
        char z = alphanum[rand () % 52];
        int y = rand () % (length - 1) + 0;
        child[y] = z;
    }
    return child;
}

/* nuderobmonkey's added function!
 */
void
resetMatingPool ( int matingpool[] )
{
    for (int idx = 0; idx < MAX_POOL; idx++)
    {
        matingpool[idx] = -1;
    }
}

int
main ()
{
    srand ( time ( nullptr ) );
    int counter;
    int i;
    int length;
    int factor = 10;
    string child;
    string mutatedchild;
    double largest = 0;
    string desired = { "" };
    
    int matingpool[MAX_POOL];
    resetMatingPool (matingpool);
    

    double percpop[POP_COUNT] = { 0 };
    double normalpop[POP_COUNT] = { 0 };
    string population[POP_COUNT] = { "" };
    string numpop[POP_COUNT] = { "" };
    string children[POP_COUNT] = { "" };

    cout << "What would you like me to say?" << endl;
    getline (cin, desired);

    length = desired.size ();
    buildPopulation (population, length);

    for (int u = 0; u < MAX_LOOP; u++)
    {
        counter++;
        calculatefitness (population, length, desired, percpop, numpop);
        resetMatingPool( matingpool ); // added by nuderobmonkey!
        buildMatingPool (percpop, matingpool, normalpop, factor);

        for (int i = 0; i < POP_COUNT; i++)
        {
            if (largest < percpop[i])
            {
                largest = percpop[i];
            }
        }

        cout << "Generation : " << counter 
             << ", Fitness : " << largest << endl;

        for (int k = 0; k < POP_COUNT; k++)
        {
            child = breed (population, matingpool, length);
            mutatedchild = mutation (child, length);
            children[k] = mutatedchild;
        }
        for (int j = 0; j < POP_COUNT; j++)
        {
            population[j] = children[j];
        }
        if (largest >= 1.0) break;
    }
    if (counter < MAX_LOOP)
    {
        cout << "Fitness of 1 was reached at generation : " << counter <<
         endl;
    }
    else
    {
        cout << "A Fitness of 1 was within " << MAX_LOOP
             << " loops never reached!\n";
    }
}


I really am sorry for the lack of comments. In the moment I hadn't realized how difficult it may be to read and understand my code. But I am extremely grateful for the help provided!
Thank you!
Topic archived. No new replies allowed.