forked from luhantao/CVRP-GA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvrp.cpp
301 lines (267 loc) · 7.64 KB
/
cvrp.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;
struct Vertex{
int num;
int x;
int y;
int demand;
};
int vertex_num; //the number of clients
int capacity; //the capacity of each truck
int pop_size; //the population size
double transform_probability = 0.200; //the probability of individual transform
vector<Vertex> vertexs; //the vector of clients vertexs
vector<vector<int> > population; //the set of individuals in the population
double cur_best_distance; //the best solution in current generation
vector<int> cur_best_individual; //the best individual in current generation
double best_solution; //the best solution in all generations
vector<int> best_individual; //the best individual in all generations
void readFile(char dataFile[]){
ifstream input;
input.open(dataFile);
if (input.is_open()){
input >> vertex_num >> best_solution >> capacity;
best_solution = 99999999;
//initialize the vector
Vertex temp;
for(int i = 0; i <= vertex_num; i++){
vertexs.push_back(temp);
}
//depot coordinate (0,0)
vertexs[0].num = 0;
input >> vertexs[0].x >> vertexs[0].y;
for (int i = 1; i <= vertex_num; i++){
try{
input >> vertexs[i].num;
input >> vertexs[i].x;
input >> vertexs[i].y;
input >> vertexs[i].demand;
}
catch(string &err){
cout << err <<endl;
}
}
}
else{
cout << "open file failed!" <<endl;
}
}
//randomly initialize the population, according to the pop_size
void initialize_population(){
//truely random number
srand((int)time(0) + rand());
vector <int> sort_vector;
for (int i = 1; i <= vertex_num; i++){
sort_vector.push_back(i);
}
for (int i = 0; i < pop_size; i++){
vector <int> temp = sort_vector;
int cnt = 0;
while (cnt < vertex_num*2){
int first = rand()% vertex_num;
int second = rand()% vertex_num;
while (first == second){
second = rand()% vertex_num;
}
int exchange = temp[first];
temp[first] = temp[second];
temp[second] = exchange;
cnt ++;
}
population.push_back(temp);
}
}
vector <int> add_depot_coordinate(vector <int> individual){
vector <int> add_depot;
add_depot.push_back(0);
int capacity_used = 0;
//add zero depot in the individual vector, like: {0,3,2,0,1,0}
for (int i = 0; i < individual.size(); i++){
if (capacity_used + vertexs[individual[i]].demand < capacity){
add_depot.push_back(individual[i]);
capacity_used += vertexs[individual[i]].demand;
}
else{
add_depot.push_back(0);
add_depot.push_back(individual[i]);
capacity_used = vertexs[individual[i]].demand;
}
}
add_depot.push_back(0);
return add_depot;
}
double caculate_individual_distance(vector <int> individual){
vector <int> add_depot = add_depot_coordinate(individual);
//caculate the distance of the new vector with zero
double distance;
for (int i = 1; i < add_depot.size(); i++){
int first_x = vertexs[add_depot[i-1]].x;
int first_y = vertexs[add_depot[i-1]].y;
int second_x = vertexs[add_depot[i]].x;
int second_y = vertexs[add_depot[i]].y;
double edge_length = pow((first_x - second_x), 2) + pow((first_y - second_y), 2);
edge_length = pow(edge_length, 0.5);
distance += edge_length;
}
return distance * 1.0;
}
void choose_individuals(){
double population_fitness = 0;
vector <double> individuals_fitness;
vector <double> accumulate_probability;
//caculate all individuals' fitness, record them and caculate the fitness of whole population
cur_best_distance = 99999999;
for (int i = 0; i < pop_size; i++){
double cur_distance = caculate_individual_distance(population[i]);
if (cur_distance < cur_best_distance){
cur_best_distance = cur_distance;
cur_best_individual = population[i];
}
if (cur_distance < best_solution){
best_solution = cur_distance;
best_individual = population[i];
}
//shorter the distance is, larger fitness the individual has.
double cur_fitness = 1.0 / cur_distance;
individuals_fitness.push_back(cur_fitness);
population_fitness += cur_fitness;
}
//caculate the chosen probability of each individual, and accumulate them in 'accumulate_probability'
for (int i = 0; i < pop_size; i++){
double probability = individuals_fitness[i] / population_fitness;
if (i != 0){
accumulate_probability.push_back(accumulate_probability[i-1] + probability);
}
else{
accumulate_probability.push_back(probability);
}
}
//accroding to the 'accumulate_probability', choose the number of 'pop_size' new individuals of population
srand((int)time(0) + rand());
vector <vector<int> > newPopulation;
for (int i = 0; i < pop_size; i++){
double randomly = rand()%10000 * 0.0001;
//50% of chance push back the best individual in the population directly
if (i == 0 && randomly < 0.5){
newPopulation.push_back(best_individual);
}
else{
for (int j = 0; j < pop_size; j++){
if (randomly < accumulate_probability[j]){
newPopulation.push_back(population[j]);
break;
}
}
}
}
//use the new population to replace the old one
population.clear();
population = newPopulation;
}
void individuals_transform(){
srand((int)time(0) + rand());
//use the operation of 'Inver-Over'
for (int i = 0; i < pop_size; i++){
double start_num = rand()%75 + 1;
double end_num;
int start_index, end_index;
for (int j = 0; j < vertex_num; j++){
if (population[i][j] == start_num){
start_index = j;
break;
}
}
//two method of operation
double transform = rand()%10000 * 0.0001;
//likely mutation
if (transform < transform_probability){
end_num = rand()%75 + 1;
while (start_num == end_num){
end_num = rand()%75 + 1;
}
for (int j = 0; j < vertex_num; j++){
if (population[i][j] == end_num){
end_index = j;
break;
}
}
}
//likely crossover
else{
int other = rand() % pop_size;
while (other == i){
other = rand() % pop_size;
}
for (int j = 0; j < vertex_num; j++){
if (population[other][j] == start_num){
if (j == vertex_num - 1){
end_num = population[other][j-1];
}
else{
end_num = population[other][j+1];
}
break;
}
}
for (int j = 0; j < vertex_num; j++){
if (population[i][j] == end_num){
end_index = j;
break;
}
}
}
if (start_index > end_index){
int temp = start_index;
start_index = end_index;
end_index = temp;
}
//reverse the vertexs in individual between start_index and end_index
vector <int> reverse;
for (int j = start_index + 1; j <= end_index; j++){
reverse.push_back(population[i][j]);
}
int reverse_index = reverse.size() - 1;
for (int j = start_index + 1; j <= end_index; j++){
population[i][j] = reverse[reverse_index];
reverse_index --;
}
}
}
int main(){
char dataFile[100];
int max_generation;
cout << "please input the name of data file (e.g.: '../tc/tai75a.dat'): ";
cin >> dataFile;
cout << endl;
cout << "please input the size of population (e.g.: 300): ";
cin >> pop_size;
cout << endl;
cout << "please input the number of generation (e.g.: 10000): ";
cin >> max_generation;
cout << endl;
readFile(dataFile);
initialize_population();
int generation = 1;
while (generation < max_generation){
choose_individuals();
individuals_transform();
cout <<"the best in current generation: "<<cur_best_distance<< endl;
generation ++;
}
cout << endl;
cout << "The cost of the best solution is: "<< best_solution<< endl;
cout << endl;
cout << "The vertexs' order of the best solution is: "<< endl;
vector <int> result = add_depot_coordinate(best_individual);
for (int i = 0; i < result.size(); i++){
cout << result[i]<<" ";
}
cout << endl;
system("pause");
}