-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.c
More file actions
141 lines (114 loc) · 3.09 KB
/
gen.c
File metadata and controls
141 lines (114 loc) · 3.09 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
#include <stdio.h>
#include "type.h"
int roulette(int sumFitness, int popSize);
void crossover(int om1[], int om2[]);
void twoPointCrossover(int om1[], int om2[]);
double eval(int chromosome[], int lchrom);
void statistics(int popSize);
float f_random();
int tournament(int popSize);
void generation(){
int p1, p2;
int k = 0;
int om1[200] = {0};
int om2[200] = {0};
float rndm;
for(int i = 0; i < popSize; i += 2){
if (modes == 2){ // mode = 2 means roulette wheel
p1 = roulette(sumFitness, popSize);
p2 = roulette(sumFitness, popSize);
}
if (modes == 1 || modes == 3){ // mode = 1 or 3 means tournament
p1 = tournament(popSize);
p2 = tournament(popSize);
}
for (int i = 0; i< lchrom; i++){
om1[i] = population[p1][i];
om2[i] = population[p2][i];
}
rndm = f_random();
printf("RNDM -> %f\n", rndm);
if (rndm <= pCross) {//pCross
if (modes == 2) crossover(om1,om2); //one point crossover
if (modes == 1 || modes == 3) twoPointCrossover(om1,om2); //two points crossover
}
else {
for (int i = 0; i < lchrom; i++){
ci1[i] = om1[i];
ci2[i] = om2[i];
}
}
for (int i = 0; i < lchrom; i++){
children[k][i] = ci1[i];
children[k+1][i] = ci2[i];
}
for (int i = 0; i < lchrom; i++){
printf("%d ", om1[i]);
}
printf("\n");
for (int i = 0; i < lchrom; i++){
printf("%d ", om2[i]);
}
printf("\nnew children =>\n");
for (int i = 0; i < lchrom; i++){
printf("%d ", ci1[i]);
}
printf("\n");
for (int i = 0; i < lchrom; i++){
printf("%d ", ci2[i]);
}
printf("\n");
k += 2;
}
for(int i=0; i<popSize; i++) {
fitness_children[i] = eval(children[i], lchrom); //calculate fitness
}
/*
printf("\n\children =>\n\n");
for(int i=0; i<popSize; i++) {
printf("%d: ", i+1);
for(int j=0; j<lchrom; j++) {
printf("%d ", children[i][j]);
if(j == (lchrom-1)){
printf(" fitness => %f\n", fitness_children[i]);
}
}
}*/
}
void nextGen(){
//Turning children into parents
for(int i=0; i<popSize; i++) {
for(int j=0; j<lchrom; j++) {
population[i][j] = children[i][j];
}
}
//Turning children fitness into parents fitness
for(int i=0; i<popSize; i++) {
fitness[i] = fitness_children[i];
}
printf("\n\nnew parents =>\n\n");
for(int i=0; i<popSize; i++) {
printf("%d: ", i+1);
for(int j=0; j<lchrom; j++) {
printf("%d ", population[i][j]);
if(j == (lchrom-1)){
printf(" fitness => %f\n", fitness[i]);
if (fitness[i] == 1.000000)
{
for (int k=0; k<lchrom; k++){
solution[k] = population[i][k];
}
}
}
}
}
comulative = 0;
normal_fitness = 0;
sumFitness = 0;
statistics(popSize);
for (int i=0; i < popSize; i++){
normal_fitness = (fitness[i]/sumFitness);
comulative += normal_fitness;
comulative_fitness[i] = comulative;
}
}