-
Notifications
You must be signed in to change notification settings - Fork 0
/
xover.c
144 lines (119 loc) · 2.44 KB
/
xover.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
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
#include <stdio.h>
#include "type.h"
int rnd(int low, int high);
float f_random();
int muteX(int pa);
int TCrnd(int low, int high);
void swapMuteX(int flag);
void crossover(int om1[], int om2[]){ //one point crossover
int xp, i;
//xp = xover point
xp = rnd(0, lchrom-1);
printf("xp = %d\n", xp);
if (modes == 2){ //MuteX
for(i = 0; i < xp; i++){
ci1[i] = muteX(om1[i]);
ci2[i] = muteX(om2[i]);
}
for(i = xp; i < lchrom; i++){
ci1[i] = muteX(om2[i]);
ci2[i] = muteX(om1[i]);
}
}
/*
//swapMuteX
for(i = 0; i < xp; i++){
ci1[i] = om1[i];
ci2[i] = om2[i];
}
for(i = xp; i < lchrom; i++){
ci1[i] = om2[i];
ci2[i] = om1[i];
}
swapMuteX(1);
swapMuteX(2);
*/
}
void twoPointCrossover(int om1[], int om2[]){
int xp1, xp2, i;
//xp = xover point
xp1 = TCrnd(1, lchrom-3);
xp2 = TCrnd(xp1+2, lchrom);
printf("xp1 = %d\n", xp1);
printf("xp2 = %d\n", xp2);
if (modes == 1){ //MuteX
for(i = 0; i < xp1; i++){
ci1[i] = muteX(om1[i]);
ci2[i] = muteX(om2[i]);
}
for(i = xp1; i < xp2; i++){
ci1[i] = muteX(om2[i]);
ci2[i] = muteX(om1[i]);
}
for(i = xp2; i < lchrom; i++){
ci1[i] = muteX(om1[i]);
ci2[i] = muteX(om2[i]);
}
}
if (modes == 3){ //swapMuteX
for(i = 0; i < xp1; i++){
ci1[i] = om1[i];
ci2[i] = om2[i];
}
for(i = xp1; i < xp2; i++){
ci1[i] = om2[i];
ci2[i] = om1[i];
}
for(i = xp2; i < lchrom; i++){
ci1[i] = om1[i];
ci2[i] = om2[i];
}
swapMuteX(1);
swapMuteX(2);
}
}
int muteX(int pa){
float rndm;
int random_number;
rndm = f_random();
if (maxFitness > 0.97) {
pMut = 0.01;
} else pMut = pMutTemp;
if (rndm <= pMut) {//pMut
random_number = rnd(0, lchrom);
cnt++;
return random_number;
}
else {
return pa;
}
}
void swapMuteX(int flag){
float rndm;
int temp;
int swap1, swap2;
swap1 = TCrnd(0, lchrom-2);
swap2 = TCrnd(swap1+1, lchrom);
//printf("swap1 = %d\n", swap1);
//printf("swap2 = %d\n", swap2);
rndm = f_random();
if (maxFitness > 0.97) {
pMut = 0.01;
} else pMut = 0.001;
if (flag == 1){
if (rndm <= pMut) {//pMut
temp = ci1[swap1];
ci1[swap1] = ci1[swap2];
ci1[swap2] = temp;
cnt++;
}
}
else if (flag == 2){
if (rndm <= pMut) {//pMut
temp = ci2[swap1];
ci2[swap1] = ci2[swap2];
ci2[swap2] = temp;
cnt++;
}
}
}