-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexer_5.cpp
156 lines (137 loc) · 3.69 KB
/
exer_5.cpp
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
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#define SIZE 50
#define N_MAX 3000
#define D 60
#define LAMBDA 60
using namespace std;
typedef struct ind {
int item[D];
int outcome;
} Indvl;
void crossover(Indvl *parent1, Indvl *parent2, Indvl *child);
void mutation(Indvl *child);
bool cmp(Indvl x, Indvl y) {
return x.outcome > y.outcome;
}
static double mut_rate = 1/D;
int main() {
int t = 1;
int mu = SIZE; // the population size
int lambda = LAMBDA; // the children size
int n = 1;
vector<Indvl> P;
Indvl *parent1;
Indvl *parent2;
Indvl *child = new Indvl();
Indvl *bsf = new Indvl(); // best-so-far solution
Indvl *individual = new Indvl();
int outcome;
vector<Indvl> R;
srand((unsigned int)(time(NULL)));
int i, j;
for(j=0; j<D; j++) { /* bsf initialization */
bsf->item[j] = 0;
}
bsf->outcome = 0;
// population initialization.
for(i=0; i<mu; i++) {
outcome = 0;
for(j=0; j<D; j++) {
if(rand()/(double)RAND_MAX < 0.5 ) {
individual->item[j] = 0;
} else {
individual->item[j] = 1;
outcome++;
}
}
individual->outcome = outcome;
P.push_back(*individual);
if(individual->outcome > bsf->outcome) {
for(j=0;j<D;j++) {
bsf->item[j] = individual->item[j];
}
bsf->outcome = individual->outcome;
}
}
while(n <= N_MAX) {
for(i=0;i<lambda;i++) {
int index1 = rand()%mu;
int index2;
while(index1 == (index2 = rand()%mu)) {
}
parent1 = &P[index1];
parent2 = &P[index2];
crossover(parent1, parent2, child);
mutation(child);
n++;
R.push_back(*child);
// step 5, update the best-so-far solution
if(child->outcome > bsf->outcome) {
for(j=0;j<D;j++) {
bsf->item[j] = child->item[j];
}
bsf->outcome = child->outcome;
}
}
// step 6, environmental selection
for(j=0;j<mu;j++) {
R.push_back(P[j]);
}
stable_sort(R.begin(), R.end(), cmp);
vector<Indvl>().swap(P); // empty P
int k;
mu = (mu+lambda)/2;
for(k=0;k<mu;k++) {
P.push_back(R[k]);
}
t++;
printf("The best-so-far solution: ");
for(j=0;j<D;j++) {
printf("%d",bsf->item[j]);
}
printf("\tObjective function value: %d\n", bsf->outcome);
vector<Indvl>().swap(R); // delete R
if(bsf->outcome == D) break;
}
delete child;
delete individual;
vector<Indvl>().swap(P);
delete bsf;
return 0;
}
void crossover(Indvl *parent1, Indvl *parent2, Indvl *child) {
int j;
int out = 0;
for(j=0;j<D;j++) {
if(rand()/(double)RAND_MAX < 0.5) {
child->item[j] = parent1->item[j];
out += child->item[j];
} else {
child->item[j] = parent2->item[j];
out += child->item[j];
}
}
child->outcome = out;
}
void mutation(Indvl *child) {
int j = 0;
int out = 0;
out = child->outcome;
for(;j<D;j++) {
if(rand()/(double)RAND_MAX < mut_rate) {
if(child->item[j] == 0) {
child->item[j] = 1;
out++;
} else {
child->item[j] = 0;
out--;
}
}
}
child->outcome = out;
}