Bonjour!
J'essaye de calculer le temps d'exécution des algorithmes BFS, DFS, A* sur des jeux de taquin de 3*" qui sont contenu dans un fichier games dont je vous donne l'extrait suivant:
Je suis capable de calculer les résultats (quand ca prend plus d'une seconde je mets -1). Comme je mets les résultat dans une liste je suis capable d'en tirer la moyenne pour DFS mais pas pour BFS ni pour les A*. Pouvez vous m'aider? J'ai ajouter mon code en .zip en pièce jointe.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 % 2 142305678 120345678 % 4 125304678 125304678 % 5 301452678 301452678 142358607 % 6 312458067 310642785
Voici le code du fichier contenant le main:
Voici le résultat en temps pour chaque jeu issu du fichier result.txt
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
353
354
355
356
357 import java.io.BufferedReader; import java.awt.LayoutManager; import java.awt.BorderLayout; import javax.swing.JLabel; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; /** * this class contains only a main method that reads a file containing games and * solve the games using BFS, DFS and A star and compute statistics of running * times (average over instances of the same path length and standard deviation). * */ public class Project { public static int counter;//idee :utiliser counter pour ecrire les resultats au fur et a mesure public static int number_of_unsolved_games_DFS=0; public static int number_of_unsolved_games_BFS=0; public static void main(String[] args) { BufferedReader reader; List<Long> runTimesDFS = new ArrayList<Long>(); List<Long> runTimesBFS = new ArrayList<Long>(); List<Long> runTimesAStar1 = new ArrayList<Long>(); List<Long> runTimesAStar2 = new ArrayList<Long>(); Window w= new Window(); LinkedList<Double> meanDFS=new LinkedList<Double>(); LinkedList<Double> meanBFS=new LinkedList<Double>(); LinkedList<Double> meanAStar1=new LinkedList<Double>(); LinkedList<Double> meanAStar2=new LinkedList<Double>(); try{ /** * The following lines are the code to get the result */ reader = new BufferedReader(new FileReader("games.txt")); String line = reader.readLine(); System.out.println("line : " + line); int problemSize=0; while (line!=null){ /** * if we haven't reach the end of the file */ if (line.charAt(0)=='%'){ /** * If the given line is the complexity of the game */ if (!runTimesDFS.isEmpty()){ // completed one size // why do we use System.out.print(problemSize + "\t" + mean(runTimesDFS) +"("+std(runTimesDFS)+")\t"); System.out.print(mean(runTimesBFS) +"("+std(runTimesBFS)+")\t"); System.out.print(mean(runTimesAStar1) +"("+std(runTimesAStar1)+")"); System.out.print(mean(runTimesAStar2) +"("+std(runTimesAStar2)+")"); System.out.println(); PrintWriter writer = new PrintWriter("result.txt", "UTF-8"); writer.println("runTime DFS" + runTimesDFS); writer.println("run time BFS : " + runTimesBFS); writer.println("run Times AStar1" + runTimesAStar1); writer.println("run Times AStar2" + runTimesAStar2); writer.close(); meanDFS.addLast((double)mean(runTimesDFS)); meanBFS.addLast((double)mean(runTimesBFS)); meanAStar1.addLast((double)mean(runTimesAStar1)); meanAStar2.addLast((double)mean(runTimesAStar2)); PrintWriter writer1 = new PrintWriter("mean.txt", "UTF-8"); writer1.print("d\t"); writer1.print("BFS\t"); writer1.print("DFS\t"); writer1.print("A*(h1)\t"); writer1.println("A*(h2)\t"); for(int i =0;i<problemSize;i++){ if(i<=1||i==3)continue; if(i==2){ writer1.print(i+"\t"); writer1.print(meanDFS.get(0)+"\t"); writer1.print(meanBFS.get(0)+"\t"); writer1.print(meanAStar1.get(0)+"\t"); writer1.println(meanAStar2.get(0)+"\t"); continue; } writer1.print(i+"\t"); writer1.print(meanDFS.get(i-2)+"\t"); writer1.print(meanBFS.get(i-2)+"\t"); writer1.print(meanAStar1.get(i-2)+"\t"); writer1.println(meanAStar2.get(i-2)+"\t"); } writer1.close(); /*JFrame window = new Window(); //JPanel interieur = new Panneau(); JPanel pan = new Panneau(); //Definit un titre pour notre fenetre window.setTitle("Resultats"); //Definit sa taille : 400 pixels de large et 100 pixels de haut window.setSize(400, 100); //Nous demandons maintenant a notre objet de se positionner au centre window.setLocationRelativeTo(null); //Termine le processus lorsqu'on clique sur la croix rouge window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Et enfin, la rendre visible w.setLayout(new BorderLayout()); JLabel jLabelDFS =new JLabel(""+mean(runTimesDFS)); pan.add(jLabelDFS); JLabel jLabelBFS =new JLabel(""+mean(runTimesBFS)); pan.add(jLabelBFS); JLabel jLabelA1 =new JLabel(""+mean(runTimesAStar1)); pan.add(jLabelA1); JLabel jLabelA2 =new JLabel(""+mean(runTimesAStar2)); pan.add(jLabelA2); w.getContentPane().add(pan); w.setVisible(true); /*JPanel pan = new Panneau(); JLabel jLabelDFS =new JLabel("hello0"+mean(runTimesDFS)); pan.add(jLabelDFS); //JPanel pan1 = new Panneau(); /*JLabel jLabelBFS =new JLabel("hello1"+mean(runTimesBFS)); pan1.add(jLabelBFS,BorderLayout.CENTER); JPanel pan2 = new Panneau(); JLabel jLabelA1 =new JLabel("hello2"+mean(runTimesAStar1)); pan2.add(jLabelA1,BorderLayout.); JPanel pan3 = new Panneau(); JLabel jLabelA2 =new JLabel("hello3"+mean(runTimesAStar2)); pan3.add(jLabelA2,1); w.getContentPane().add(pan,BorderLayout.SOUTH); w.setVisible(true);*/ }else{ System.out.println("runTimeDFS is empty"); } problemSize = Integer.parseInt(line.substring(2)); } else { /** * If the given line is an actual game */ System.out.println("We entered the else"); Problem<Puzzle, PuzzleAction> eightpuzzle = new Problem<Puzzle,PuzzleAction>(new Puzzle(line), new Puzzle("012345678")); System.out.println("Did we entered problem?"); if (problemSize<10){ // run DFS long startTimeDFS = System.nanoTime(); if(number_of_unsolved_games_DFS<3){ eightpuzzle.dfs(); long finishTimeDFS = System.nanoTime(); runTimesDFS.add(finishTimeDFS-startTimeDFS); if(finishTimeDFS-startTimeDFS>1000000000){ number_of_unsolved_games_DFS +=1; runTimesDFS.add((long) -1); }else{ number_of_unsolved_games_DFS = -1; runTimesDFS.add(finishTimeDFS-startTimeDFS); } System.out.println("runTimesDFS : "+(runTimesDFS.get(0)/1000000000)); }else{ runTimesDFS.add((long) -1); } // run BFS long startTimeBFS = System.nanoTime(); if(number_of_unsolved_games_BFS<3){ System.out.println("**************************************************************************"); System.out.println("**************************************************************************"); System.out.println("we get into BFS"); System.out.println("**************************************************************************"); System.out.println("**************************************************************************"); eightpuzzle.bfs(); long finishTimeBFS = System.nanoTime(); if(finishTimeBFS-startTimeBFS>1000000000){ number_of_unsolved_games_BFS +=1; runTimesBFS.add((long) -1); } else{ number_of_unsolved_games_BFS = 0; runTimesBFS.add(finishTimeBFS-startTimeBFS); } System.out.println("runTimesBFS : "+(runTimesBFS.get(0)/1000000000)); }else{ runTimesBFS.add((long) -1); } // run A star long startTimeAStar = System.nanoTime(); eightpuzzle.aStar(); long finishTimeAStar = System.nanoTime(); runTimesAStar1.add(finishTimeAStar-startTimeAStar); System.out.println("runTimesAStar(nbMalPlace): "+(runTimesAStar1.get(0)/1000000000)); // run A star Manhattan long startTimeAStarManhattan = System.nanoTime(); eightpuzzle.aStar2(); long finishTimeAStarManhattan = System.nanoTime(); runTimesAStar2.add(finishTimeAStarManhattan-startTimeAStarManhattan); System.out.println("runTimesAStar(Manhattan) : "+(runTimesAStar2.get(0)/1000000000)); } } line = reader.readLine(); counter++; //w.setLayout(new BorderLayout()); /*JPanel pan = new Panneau(); JLabel jLabelDFS =new JLabel("hello0"+mean(runTimesDFS)); pan.add(jLabelDFS); //JPanel pan1 = new Panneau(); /*JLabel jLabelBFS =new JLabel("hello1"+mean(runTimesBFS)); pan1.add(jLabelBFS,BorderLayout.CENTER); JPanel pan2 = new Panneau(); JLabel jLabelA1 =new JLabel("hello2"+mean(runTimesAStar1)); pan2.add(jLabelA1,BorderLayout.); JPanel pan3 = new Panneau(); JLabel jLabelA2 =new JLabel("hello3"+mean(runTimesAStar2)); pan3.add(jLabelA2,1); w.getContentPane().add(pan,BorderLayout.SOUTH); w.setVisible(true);*/ } }catch(IOException e){ System.err.println(e); e.printStackTrace(); } } public static double mean(List<Long> l){ long res=0; for (Long val: l) res+=val/1000000000; return (1.0*res) / l.size(); } public static double std(List<Long> l){ double m = mean(l); double res=0; for (Long val: l) res+= Math.pow(val/1000000000-m,2); return Math.sqrt(res / l.size()); } /*public static double meanMean(List<Double> l){ double res=0; for(Double val:l) if(res+=val; } */ } class Panneau extends JPanel { /** * */ private static final long serialVersionUID = 1L; public void paintComponent(Graphics g){ //reglages distances int largeur1=105;//largeur colonne 1, cad colonne des d int largeur2=214;//largeur colonne 2, des algo int hauteur1=700;//hauteur des colonnes int ecart=2;//ecart de base int starthauteur=10;//depart du grand rectangle dans la fenetre int startlargeur=100; int interligne=35; //dessin des colonnes g.drawRect(startlargeur, starthauteur, largeur1+4*largeur2+6*ecart, hauteur1+2*ecart); g.drawRect(startlargeur+ecart, starthauteur+ecart, largeur1, hauteur1); g.drawRect(startlargeur+largeur1+2*ecart,starthauteur+ecart,largeur2,hauteur1); g.drawRect(startlargeur+largeur1+largeur2+3*ecart,starthauteur+ecart,largeur2,hauteur1); g.drawRect(startlargeur+largeur1+2*largeur2+4*ecart,starthauteur+ecart,largeur2,hauteur1); g.drawRect(startlargeur+largeur1+3*largeur2+5*ecart,starthauteur+ecart,largeur2,hauteur1); g.drawLine(startlargeur+ecart, starthauteur+ecart+interligne, startlargeur+largeur1+4*largeur2+5*ecart, starthauteur+ecart+interligne); //ecriture des legendes g.drawString("d", 64+startlargeur, 35); g.drawString("DFS", 213+startlargeur, 35); g.drawString("BFS", 433+startlargeur, 35); g.drawString("A*(h1)", 645+startlargeur, 35); //revoir comment faire l'indice g.drawString("A*(h2)", 861+startlargeur, 35); } } class Window extends JFrame { public Window(){ this.setTitle("Tableau des temps moyens de resolution selon le nombre de coups necessaires pour atteindre une solution"); this.setSize(1100, 750); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setAlwaysOnTop(true); //Instanciation d'un objet JPanel JPanel pan = new Panneau(); //Definition de sa couleur de fond pan.setBackground(Color.BLUE); //On previent notre JFrame que notre JPanel sera son content pane this.setContentPane(pan); //this.setVisible(true); //this.setVisible(false); //this.setVisible(true); } }
Voici les moyennes en temps issus de mean.txt Comme vous pouvez le voir il n'y a que des éros pour DFS et les deux 1* (le premier à l'heurisitque des pièces mal placées et le deuxième celui de la distance de Manhattan pour arriver à l'état final).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 runTime DFS[1002295049, -1, 30957, 30957, 455220, 455220, 116489, 116489, 1540408, 1540408, 151622, 151622, 224510973, 224510973, 54330, 54330, 1002845286, -1, 1000811211, -1, 1002153700, -1, 1000507805, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] run time BFS : [-1, 62492, 2913491, 288094, 299567, 259676, 213662716, 53274, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] run Times AStar1[311938, 108303, 488198, 501302, 877224, 1172742, 469968, 1314740, 445932, 1744554, 1370667, 5971295, 1902890, 1626686, 1684501, 1001609, 13621826, 766396, 971394, 1062511, 616579, 2268740, 948728, 709454, 2124530, 1304379, 2217377, 1798562, 9125622, 10574586, 1450648, 9365521, 4434043, 1357076, 1472849, 2808003, 1692987, 1458864, 1065424, 1650042, 3176876, 1568081, 2010499, 3480285, 2164791, 1434226, 980115, 4200342, 2985414, 20147944, 11415692, 5731999, 11791098, 9854690, 3983726, 2450401, 4165418, 27075221, 5674561, 12138065, 9652475, 8287789, 6891576, 6568603, 3458611, 3897299, 8155902, 11422254, 18141524, 19695801, 17405429, 2931068, 4527073, 3736763, 2739682, 72693922, 5713354, 9955784, 13327889, 2804753, 5547959, 4655578, 6388600, 57014788, 10549794] run Times AStar2[191183, 118830, 539824, 1533348, 1069733, 1091966, 428076, 843770, 423838, 4680445, 1160073, 5490908, 1117869, 1229210, 2337939, 1847807, 4170612, 1329101, 2113129, 3856197, 1758476, 933563, 5210733, 2636335, 1016186, 1271975, 2051445, 1768509, 14933576, 8500577, 7905137, 4789375, 2826824, 1384736, 1535367, 2796932, 2180312, 7884610, 1007815, 978059, 3021608, 1576844, 1899243, 4196973, 926747, 1443280, 999672, 4303972, 2836407, 22764341, 7589729, 6543118, 8460001, 12982804, 5401215, 3751923, 15768554, 9621391, 5354070, 8325253, 9748571, 4977163, 6524610, 7183410, 3649802, 3351638, 31808584, 10832552, 24242690, 21309285, 6369734, 2899731, 4782296, 3596467, 2919028, 11327532, 4893191, 11959242, 4814950, 2874965, 4874081, 6350705, 3275162, 8223655, 1048384
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 d BFS DFS A*(h1) A*(h2) 2 0.25 0.0 0.0 0.0 4 0.07142857142857142 0.0 0.0 0.0 5 0.1111111111111111 0.0 0.0 0.0 6 0.13513513513513514 0.0 0.0 0.0 7 0.0847457627118644 0.0 0.0 0.0 8 0.05154639175257732 0.0 0.0 0.0 9 0.05154639175257732 0.0 0.0 0.0 10 0.05154639175257732 0.0 0.0 0.0 11 0.05154639175257732 0.0 0.0 0.0 12 0.05154639175257732 0.0 0.0 0.0 13 0.05154639175257732 0.0 0.0 0.0 14 0.05154639175257732 0.0 0.0 0.0 15 0.05154639175257732 0.0 0.0 0.0 16 0.05154639175257732 0.0 0.0 0.0 17 0.05154639175257732 0.0 0.0 0.0 18 0.05154639175257732 0.0 0.0 0.0 19 0.05154639175257732 0.0 0.0 0.0 20 0.05154639175257732 0.0 0.0 0.0 21 0.05154639175257732 0.0 0.0 0.0 22 0.05154639175257732 0.0 0.0 0.0 23 0.05154639175257732 0.0 0.0 0.0 24 0.05154639175257732 0.0 0.0 0.0 25 0.05154639175257732 0.0 0.0 0.0 26 0.05154639175257732 0.0 0.0 0.0 27 0.05154639175257732 0.0 0.0 0.0 28 0.05154639175257732 0.0 0.0 0.0 29 0.05154639175257732 0.0 0.0 0.0 30 0.05154639175257732 0.0 0.0 0.0 31 0.05154639175257732 0.0 0.0 0.0
Partager