Bonsoir

Mon problème c'est que j'ai créer un algorithme génétique pour résoudre le problème de tournée de véhicule mais je crois que la boucle de l'iteration est mal placé car pour une seul iteration tout fonctionne correctement et si j'augmente le nombre d'iteration la distance se multiplie fois la nombre d'iteration et le nombre de chemin qui doit être 50 ( 50 parents donnent 50 enfants) se multiplie aussi fois le nombre d'iteration. La boucle pour les iterations est (ff)
Je suis confus j'espère que l'un de vous peut m'aider et merci d'avance.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
out.println("\n -----------------------Crossover------------------- \n");%><br><%
%><br><%	
out.println("\n Chemins Séléctionnés Aléatoirement \n");%><br><%
 
long before = System.currentTimeMillis();
 
 
 
int[] t3= new int[51];
int[] pere= new int[f+1];int[] mere= new int[f+1];
int[] fils= new int[f+1];int[] fille= new int[f+1];
 
int ff=1;
while(ff<5){
 
 
ArrayList<Integer> tx = new ArrayList<Integer>();
for(i=1;i<51;i++){
	tx.add(i);
	}
 
Collections.shuffle(tx);
for (i=0;i<tx.size();i++){
t3[i+1]=tx.get(i);	
}
 
 
%><br><%
 
int v,w;
v=1;w=2;
while(v<51){
for (i=1;i<f;i++){
	q="select id_client"+i+" from fitness where id_fitness="+t3[v]+";";
r=s.executeQuery(q);
while (r.next()){
 
	pere[i]=r.getInt(1);
 
}}
 
 
for (i=1;i<f;i++){
	q="select id_client"+i+" from fitness where id_fitness="+t3[w]+";";
r=s.executeQuery(q);
while (r.next()){
 
	mere[i]=r.getInt(1);
 
}}
 
 
for(i=1;i<(f/4)+1;i++){
fils[i]=pere[i];
}
for(i=((f/4)+1);i<(3*(f/4)+1);i++){
fils[i]=mere[i];
}	
for(i=(3*(f/4))+1;i<f;i++){
fils[i]=pere[i];
}
 
for(i=1;i<(f/4)+1;i++){
fille[i]=mere[i];
}
for(i=((f/4)+1);i<(3*(f/4)+1);i++){
fille[i]=pere[i];
}		
for(i=(3*(f/4))+1;i<f;i++){
fille[i]=mere[i];
}
 
x=1;j=f;
for(i=1;i<(f/4)+1;i++){
j=3*(f/4);
while((j>f/4) && (j<(3*(f/4))+1)) {
if((fils[i]==fils[j])||(fils[j]==0)){
	fils[j]=-1;
}
j--;
}
}
 
for(i=(3*(f/4)+1);i<f;i++){
j=3*(f/4);
while((j>f/4) && (j<(3*(f/4)+1))){
if((fils[i]==fils[j])||(fils[j]==0)){
	fils[j]=-1;
}
j--;
}
}
 
int inter[]= new int[f+1]; 
for(i=1;i<f;i++){
inter[i]=pere[i];
}
for(i=1;i<f;i++){
for(j=1;j<f;j++){
	if(fils[i]==inter[j]){
		inter[j]=0;
	}
 
}}
for(i=1;i<f;i++){
for(j=1;j<f;j++){
if(fils[i]==-1 && inter[j]!=0){
	fils[i]=inter[j];
	inter[j]=0;
}
}}
 
x=1;j=f;
for(i=1;i<(f/4)+1;i++){
j=3*(f/4);
while((j>f/4) && (j<(3*(f/4))+1)) {
if((fille[i]==fille[j])||(fille[j]==0)){
	fille[j]=-1;
}
j--;
}
}
 
for(i=(3*(f/4))+1;i<f;i++){
j=3*(f/4);
while((j>f/4) && (j<(3*(f/4))+1)){
if((fille[i]==fille[j])||(fille[j]==0)){
	fille[j]=-1;
}
j--;
}
}
 
 
for(i=1;i<f;i++){
inter[i]=pere[i];
}
for(i=1;i<f;i++){
for(j=1;j<f;j++){
	if(fille[i]==inter[j]){
		inter[j]=0;
	}
 
}}
for(i=1;i<f;i++){
for(j=1;j<f;j++){
if(fille[i]==-1 && inter[j]!=0){
	fille[i]=inter[j];
	inter[j]=0;
}
}}
 
 
 
q2="Insert into crossover values('"+v+"','0',";
j=1;
while(j<f){
	q2=q2+"'"+fils[j]+"',";
	j++;
}
q2=q2+"'0','-1','-1');";
s1.executeUpdate(q2);
 
q2="Insert into crossover values('"+w+"','0',";
j=1;
while(j<f){
	q2=q2+"'"+fille[j]+"',";
	j++;
}
q2=q2+"'0','-1','-1');";
s1.executeUpdate(q2);
 
 
 
 
 
 
 
 
 
distance=0.0;i=0;x=0;y=0;z=0;o=0;
 
q="select id_client1 from crossover where id_fitness='"+v+"';";
r=s.executeQuery(q);
while(r.next()){
z=r.getInt(1);
}
 
 
q1="SELECT distance from vecteur where id_client1='0' AND id_client2='"+z+"';";
r1=s1.executeQuery(q1);
while(r1.next()){
distance=distance+r1.getDouble(1);
}
 
 
q1="select id_client"+(f-1)+" from crossover where id_fitness='"+v+"';";
r1=s1.executeQuery(q1);
while(r.next()){
o=r1.getInt(1);
}
 
q1="SELECT distance from vecteur where id_client1='0' AND id_client2='"+o+"';";
r1=s1.executeQuery(q1);
while(r1.next()){
distance=distance+r1.getDouble(1);
}
 
for(i=1;i<f-1;i++){
q="select id_client"+i+" from crossover where id_fitness='"+v+"';";
r=s.executeQuery(q);
while(r.next()){
x=r.getInt(1);
 
q="select id_client"+(i+1)+" from crossover where id_fitness='"+v+"';";
r=s.executeQuery(q);
while(r.next()){
y=r.getInt(1);
 
q1="SELECT distance from vecteur where id_client1='"+x+"' AND id_client2='"+y+"';";
r1=s1.executeQuery(q1);
while(r1.next()){
distance=distance+r1.getDouble(1);
}
 
}}}
q="update crossover set distance='"+distance+"' where id_fitness='"+v+"';";
s.executeUpdate(q);
v=v+2;
 
distance=0.0;i=0;x=0;y=0;z=0;o=0;
q="select id_client1 from crossover where id_fitness='"+w+"';";
r=s.executeQuery(q);
while(r.next()){
z=r.getInt(1);
}
 
q1="SELECT distance from vecteur where id_client1='0' AND id_client2='"+z+"';";
r1=s1.executeQuery(q1);
while(r1.next()){
distance=distance+r1.getDouble(1);
 
}
q1="select id_client"+(f-1)+" from crossover where id_fitness='"+w+"';";
r1=s1.executeQuery(q1);
while(r.next()){
o=r1.getInt(1);
 
 
}
q1="SELECT distance from vecteur where id_client1='0' AND id_client2='"+o+"';";
r1=s1.executeQuery(q1);
while(r1.next()){
distance=distance+r1.getDouble(1);
 
}
for(i=1;i<f-1;i++){
q="select id_client"+i+" from crossover where id_fitness='"+w+"';";
r=s.executeQuery(q);
while(r.next()){
x=r.getInt(1);
 
q="select id_client"+(i+1)+" from crossover where id_fitness='"+w+"';";
r=s.executeQuery(q);
while(r.next()){
y=r.getInt(1);
 
q1="SELECT distance from vecteur where id_client1='"+x+"' AND id_client2='"+y+"';";
r1=s1.executeQuery(q1);
while(r1.next()){
distance=distance+r1.getDouble(1);
 
}
}}}
q="update crossover set distance='"+distance+"' where id_fitness='"+w+"';";
s.executeUpdate(q);
 
w=w+2;
}
 
 
 
%><br><%
out.println("--------------------Mutation-------------------------");%><br><%
 
int mut[]=new int[f+1];Double tt[]=new Double[3];int c;Double a;
 
for(int u=1;u<51;u++){
for(i=1;i<f;i++){
	q="select id_client"+i+" from crossover where id_fitness='"+u+"';";
	r=s.executeQuery(q);
	while(r.next()){
		mut[i]=r.getInt(1);
 
	}
}
 
....
 
if(tt[0]<a){
q1="Update inter set distance='"+tt[0]+"' where id_fitness='"+c+"';";
s2.executeUpdate(q1);}
c++;}
 
q2="";
for(i=1;i<f;i++){
q2=q2+"id_client"+i+",";
}
 
q="INSERT INTO mutation ("+q2+"distance) SELECT "+q2+"distance FROM inter where id_fitness ='"+(c-1)+"';";
s.executeUpdate(q);
}
 
%><br><center><%
out.println("--------------------remplacemnt-------------------------");%><br><%
 
q="SELECT * FROM fitness ORDER BY distance;";
r1=s1.executeQuery(q);
q="SELECT * FROM mutation ORDER BY distance;";
r1=s1.executeQuery(q);
q="SELECT * FROM crossover ORDER BY distance;";
r1=s1.executeQuery(q);
 
q="Truncate inter;";
s.executeUpdate(q);
q="Create table IF NOT EXISTS inter LIKE fitness;";
s.executeUpdate(q);
 
q="SELECT * FROM mutation ORDER BY distance LIMIT 25;";
r1=s1.executeQuery(q);
 
q="SELECT * FROM fitness ORDER BY distance LIMIT 25;";
r1=s1.executeQuery(q);
 
q="insert into inter select * FROM mutation ORDER BY distance LIMIT 25;";
s.executeUpdate(q);
 
q="insert into inter select * FROM fitness ORDER BY distance LIMIT 25;";
s.executeUpdate(q);
 
q="ALTER TABLE `inter` DROP `id_fitness`;";
s.executeUpdate(q);
q="ALTER TABLE `inter` ADD `id_fitness` INT NOT NULL FIRST; ";
s.executeUpdate(q);
 
q="truncate fitness;";
s.executeUpdate(q);
q="insert into fitness select * from inter;";
s.executeUpdate(q);
 
ff++;
}