-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcrossover.c
More file actions
165 lines (160 loc) · 5.15 KB
/
crossover.c
File metadata and controls
165 lines (160 loc) · 5.15 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
/* Crossover routines */
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include "nsga2.h"
# include "rand.h"
/* Function to cross two individuals */
void crossover (NSGA2Type *nsga2Params, individual *parent1, individual *parent2, individual *child1, individual *child2)
{
if (nsga2Params->nreal!=0)
{
realcross (nsga2Params, parent1, parent2, child1, child2);
}
if (nsga2Params->nbin!=0)
{
bincross (nsga2Params, parent1, parent2, child1, child2);
}
return;
}
/* Routine for real variable SBX crossover */
void realcross (NSGA2Type *nsga2Params, individual *parent1, individual *parent2, individual *child1, individual *child2)
{
int i;
double rand;
double y1, y2, yl, yu;
double c1, c2;
double alpha, beta, betaq;
if (randomperc() <= nsga2Params->pcross_real)
{
nsga2Params->nrealcross++;
for (i=0; i<nsga2Params->nreal; i++)
{
if (randomperc()<=0.5 )
{
if (fabs(parent1->xreal[i]-parent2->xreal[i]) > EPS)
{
if (parent1->xreal[i] < parent2->xreal[i])
{
y1 = parent1->xreal[i];
y2 = parent2->xreal[i];
}
else
{
y1 = parent2->xreal[i];
y2 = parent1->xreal[i];
}
yl = nsga2Params->min_realvar[i];
yu = nsga2Params->max_realvar[i];
rand = randomperc();
beta = 1.0 + (2.0*(y1-yl)/(y2-y1));
alpha = 2.0 - pow(beta,-(nsga2Params->eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(nsga2Params->eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(nsga2Params->eta_c+1.0)));
}
c1 = 0.5*((y1+y2)-betaq*(y2-y1));
beta = 1.0 + (2.0*(yu-y2)/(y2-y1));
alpha = 2.0 - pow(beta,-(nsga2Params->eta_c+1.0));
if (rand <= (1.0/alpha))
{
betaq = pow ((rand*alpha),(1.0/(nsga2Params->eta_c+1.0)));
}
else
{
betaq = pow ((1.0/(2.0 - rand*alpha)),(1.0/(nsga2Params->eta_c+1.0)));
}
c2 = 0.5*((y1+y2)+betaq*(y2-y1));
if (c1<yl)
c1=yl;
if (c2<yl)
c2=yl;
if (c1>yu)
c1=yu;
if (c2>yu)
c2=yu;
if (randomperc()<=0.5)
{
child1->xreal[i] = c2;
child2->xreal[i] = c1;
}
else
{
child1->xreal[i] = c1;
child2->xreal[i] = c2;
}
}
else
{
child1->xreal[i] = parent1->xreal[i];
child2->xreal[i] = parent2->xreal[i];
}
}
else
{
child1->xreal[i] = parent1->xreal[i];
child2->xreal[i] = parent2->xreal[i];
}
}
}
else
{
for (i=0; i<nsga2Params->nreal; i++)
{
child1->xreal[i] = parent1->xreal[i];
child2->xreal[i] = parent2->xreal[i];
}
}
return;
}
/* Routine for two point binary crossover */
void bincross (NSGA2Type *nsga2Params, individual *parent1, individual *parent2, individual *child1, individual *child2)
{
int i, j;
double rand;
int temp, site1, site2;
for (i=0; i<nsga2Params->nbin; i++)
{
rand = randomperc();
if (rand <= nsga2Params->pcross_bin)
{
nsga2Params->nbincross++;
site1 = rnd(0,nsga2Params->nbits[i]-1);
site2 = rnd(0,nsga2Params->nbits[i]-1);
if (site1 > site2)
{
temp = site1;
site1 = site2;
site2 = temp;
}
for (j=0; j<site1; j++)
{
child1->gene[i][j] = parent1->gene[i][j];
child2->gene[i][j] = parent2->gene[i][j];
}
for (j=site1; j<site2; j++)
{
child1->gene[i][j] = parent2->gene[i][j];
child2->gene[i][j] = parent1->gene[i][j];
}
for (j=site2; j<nsga2Params->nbits[i]; j++)
{
child1->gene[i][j] = parent1->gene[i][j];
child2->gene[i][j] = parent2->gene[i][j];
}
}
else
{
for (j=0; j<nsga2Params->nbits[i]; j++)
{
child1->gene[i][j] = parent1->gene[i][j];
child2->gene[i][j] = parent2->gene[i][j];
}
}
}
return;
}