-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_initialization.c
More file actions
178 lines (136 loc) · 5.62 KB
/
algorithm_initialization.c
File metadata and controls
178 lines (136 loc) · 5.62 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
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "algorithm.h"
#include "algorithm_initialization.h"
// TSP Initialization
extern int nextFree;
int initialize(int distmx[CITIES][CITIES], int chromemx[AMOUNT][CITIES], int nextGen[AMOUNT][CITIES], int overallBest[1][CITIES], int amountGiven, int min, int max) {
int chCheck;
printf("Insert the amount of additional generations you want to be created(in range between 0 - 10000): ");
if (scanf("%d", &chCheck) == 0)
getchar();
else
amountGiven = chCheck;
puts("--------------------------------------------");
if (amountGiven > 0 && amountGiven <= 10000) {
printf("Insert the range of distances to be generated. Only positive integer values from minimum to maximum or vice versa should be entered: ");
if (scanf("%d", &chCheck) == 0)
getchar();
else
min = chCheck;
if (scanf("%d", &chCheck) == 0)
getchar();
else
max = chCheck;
puts("--------------------------------------------");
while(true) {
if(min <= 0 || max <= 0) {
printf("Enerted values are incorrect! Provide acceptable numbers: ");
if (scanf("%d", &chCheck) == 0)
getchar();
else
min = chCheck;
if (scanf("%d", &chCheck) == 0)
getchar();
else
max = chCheck;
puts("--------------------------------------------");
}
else
break;
}
if(min > max) {
int tmp;
tmp = min;
min = max;
max = tmp;
}
createDistmxRand(distmx, min, max);
createChromosomes(chromemx);
createChromosomesNg(nextGen);
puts("--------------------------------------------");
puts("Initial population is: ");
puts("--------------------------------------------");
printChromosomes(distmx, chromemx);
puts("--------------------------------------------");
for(int i = 0; i < CITIES; i++)
overallBest[0][i] = chromemx[0][i];
// Main part: 2 mutations, 2 reversed mutations, 4 crossovered chromosomes, 2 copied chromosomes with no changes.
for(int i = 1; i <= amountGiven; i++) {
for(int j = 0; j < CITIES / 2 - 3; j++) {
mutation(distmx, chromemx, nextGen);
mutationInv(distmx, chromemx, nextGen);
}
for (int k = 0; k < CITIES / 2 - 3; k++)
crossover(distmx, chromemx, nextGen);
while(nextFree < AMOUNT)
reproduction(distmx, chromemx, nextGen);
printf("Generation №[%d]: \n", i);
puts("--------------------------------------------");
printChromosomes(distmx, nextGen);
puts("--------------------------------------------");
copyMx(chromemx, nextGen);
pickBest(distmx, chromemx, overallBest);
}
puts("The best route from all generations is: ");
puts("--------------------------------------------");
for(int i = 0; i < CITIES; i++)
printf("%d ", overallBest[0][i]);
printf("Distance: %d\n",fitness(distmx, overallBest[0]));
puts("--------------------------------------------");
}
else {
puts("Warning, unacceptable value or \"0\" has been selected! The shortest route will be picked from the initial population, after the selection of the range of distances.");
puts("--------------------------------------------");
printf("Insert the range of distances to be generated. Only positive integer values from minimum to maximum or vice versa should be entered: ");
if (scanf("%d", &chCheck) == 0)
getchar();
else
min = chCheck;
if (scanf("%d", &chCheck) == 0)
getchar();
else
max = chCheck;
while(true) {
if(min <= 0 || max <= 0) {
puts("--------------------------------------------");
printf("Enerted values are incorrect! Provide acceptable numbers: ");
if (scanf("%d", &chCheck) == 0)
getchar();
else
min = chCheck;
if (scanf("%d", &chCheck) == 0)
getchar();
else
max = chCheck;
}
else
break;
}
if(min > max) {
int tmp;
tmp = min;
min = max;
max = tmp;
}
createDistmxRand(distmx, min, max);
createChromosomes(chromemx);
puts("Initial population is: ");
puts("--------------------------------------------");
printChromosomes(distmx, chromemx);
puts("--------------------------------------------");
puts("The best route is: ");
puts("--------------------------------------------");
for(int i = 0; i < CITIES; i++)
overallBest[0][i] = chromemx[0][i];
pickBest(distmx, chromemx, overallBest);
for(int i = 0; i < CITIES; i++)
printf("%d ", overallBest[0][i]);
printf("Distance: %d\n", fitness(distmx, overallBest[0]));
puts("--------------------------------------------");
}
return 0;
}