-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.c
59 lines (44 loc) · 883 Bytes
/
select.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
#include <stdio.h>
#include "type.h"
#include <time.h>
#include <stdlib.h>
float f_random();
int tournament(int popSize);
int TCrnd(int low, int high);
int roulette(int sumFitness, int popSize)
{
float rnd;
int p;
// select parents randomly with respect to fitness
rnd = f_random();
printf("%f\n", rnd);
for (int i=0; i<popSize; i++){
if (rnd > comulative_fitness[i] && rnd <= comulative_fitness[i+1]){
p = i+1;
i = popSize;
}
else if (rnd <= comulative_fitness[0]){
p = 0;
i = popSize;
}
else {
continue;
}
}
return p;
}
int tournament(int popSize)
{
int rnd1,rnd2;
double f1,f2;
// select k parents randomly from all parents
rnd1 = TCrnd(0, popSize);
f1 = fitness[rnd1];
rnd2 = TCrnd(0, popSize);
f2 = fitness[rnd2];
if (f1 > f2){
return rnd1;
}else{
return rnd2;
}
}