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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
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>();
List<Integer> array_number_of_strokes_dfs = new ArrayList<Integer>();
List<Integer> array_number_of_strokes_bfs = new ArrayList<Integer>();
List<Integer> array_number_of_strokes_A1 = new ArrayList<Integer>();
List<Integer> array_number_of_strokes_A2 = new ArrayList<Integer>();
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();
PrintWriter writer_strokes = new PrintWriter("strokes.txt", "UTF-8");
writer_strokes.print("d\t");
writer_strokes.print("BFS\t");
writer_strokes.print("DFS\t");
writer_strokes.print("A*(h1)\t");
writer_strokes.println("A*(h2)\t");
for(int i =0;i<problemSize;i++){
if(i<=1||i==3)continue;
if(i==2){
writer_strokes.print(i+"\t");
writer_strokes.print(array_number_of_strokes_bfs.get(0)+"\t");
writer_strokes.print(array_number_of_strokes_dfs.get(0)+"\t");
writer_strokes.print(array_number_of_strokes_A1.get(0)+"\t");
writer_strokes.println(array_number_of_strokes_A2.get(0)+"\t");
continue;
}
writer_strokes.print(i+"\t");
writer_strokes.print(array_number_of_strokes_bfs.get(i-2)+"\t");
writer_strokes.print(array_number_of_strokes_dfs.get(i-2)+"\t");
writer_strokes.print(array_number_of_strokes_A1.get(i-2)+"\t");
writer_strokes.println(array_number_of_strokes_A2.get(i-2)+"\t");
}
writer_strokes.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 (true){
// run DFS
// we lunch a timer to know how much time did the algorithm took
long startTimeDFS = System.nanoTime();
// if the number of unsolved games in the given time is greater than 3 we dismiss the algorithm
// otherwise we run the algorithm
if(number_of_unsolved_games_DFS<3){
eightpuzzle.dfs();
long finishTimeDFS = System.nanoTime();
//runtimes are pushed into an array
runTimesDFS.add(finishTimeDFS-startTimeDFS);
//if the time it took is greater than 10 seconds we increase the number of unsolved games
//otherwise we set the number of unsolved games back to 0 again
if(finishTimeDFS-startTimeDFS>1000000000*10){
number_of_unsolved_games_DFS +=1;
runTimesDFS.add((long) -1);
array_number_of_strokes_dfs.add(-1);
}else{
number_of_unsolved_games_DFS = 0;
runTimesDFS.add(finishTimeDFS-startTimeDFS);
array_number_of_strokes_dfs.addAll(eightpuzzle.get_number_of_strokes_dfs());
}
System.out.println("runTimesDFS : "+(runTimesDFS.get(0)/1000000000*10));
}else{
runTimesDFS.add((long) -1);
array_number_of_strokes_dfs.add(-1);
}
// run BFS
long startTimeBFS = System.nanoTime();
if(number_of_unsolved_games_BFS<3){
eightpuzzle.bfs();
long finishTimeBFS = System.nanoTime();
if(finishTimeBFS-startTimeBFS>1000000000*10){
number_of_unsolved_games_BFS +=1;
runTimesBFS.add((long) -1);
}
else{
number_of_unsolved_games_BFS = 0;
runTimesBFS.add(finishTimeBFS-startTimeBFS);
array_number_of_strokes_bfs.addAll(eightpuzzle.get_number_of_strokes_bfs());
}
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);
array_number_of_strokes_A1.add(eightpuzzle.number_of_strokes_A1);
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);
array_number_of_strokes_A2.addAll(eightpuzzle.get_number_of_strokes_A2());
System.out.println("number of strokes A2 : " + array_number_of_strokes_A2);
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) {
double res = 0;
int t = 1;
for (long val : l) {
res += (val - res) / t;
t++;
}
return res /1000000000;
}
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 List<Integer> get_number_of_strokes_dfs(List<Integer> l){
return null;
}
/*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);
}
} |
Partager