IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

Mettre les résultats d'un compteur dans un fichier


Sujet :

Entrée/Sortie Java

  1. #1
    Membre du Club
    Homme Profil pro
    Etudiant
    Inscrit en
    Novembre 2015
    Messages
    156
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Etudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 156
    Points : 52
    Points
    52
    Par défaut Mettre les résultats d'un compteur dans un fichier
    Je cherche à écrire le nombre de coup que la résolution d'un puzzle m'a pris à partir de quatre algorithmes. Ces quatre algorithmes sont écrits dans une classe Problem et sont appelés par un main qui réside dans une classe Project. Ils renvoient un état final. Dans ces algorithmes construits avec une boucle while (la méthode des appels récursifs posait problème dans mon souvenir), j'ai mis un compteur pour compter le nombre de coup.
    Je souhaite créer un fichier avec le nombre de coup pris pour chaque puzzle résolu, mais je n'arrive pas à l'écrire dans Problem. Cela génère l'erreur: Unhandled exception type UnsupportedEncodingException. Mais je ne sais pas comment le faire dans le main non plus car je retourne déjà quelque chose à partir des algorithmes.

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2008
    Messages
    757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2008
    Messages : 757
    Points : 572
    Points
    572
    Par défaut
    Bonjour,

    Pourrais-tu montrer ton code histoire que l'on puisse mieux t'aider ?

    Après, pour écrire tes données, tu peux les écrire en format texte avec du FileInputStream et FileOutputStream ou en xml avec du JAXB par exemple.
    OS : LinuxMint 20

  3. #3
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 710
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 710
    Points : 4 791
    Points
    4 791
    Par défaut
    Sais-tu déjà comment faire un fichier en java ?
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

  4. #4
    Membre du Club
    Homme Profil pro
    Etudiant
    Inscrit en
    Novembre 2015
    Messages
    156
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Etudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 156
    Points : 52
    Points
    52
    Par défaut Codes de mon projet
    Oui, j'ai réussi à en écrire deux déjà à partir du main et de fonction qui ne tournait qu'à l'intérieur. Je vous les montrerai tout à l'heure.

    Voici le code de Project, qui contient le main :

    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
    358
    359
    360
    361
    362
    363
    364
     
    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>();
    		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("test.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 (true){
    						// 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*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);
    							}
    							System.out.println("runTimesDFS : "+(runTimesDFS.get(0)/1000000000));
    						}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){
    							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*10){
    								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) {
    		  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 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);
     
    	  }       
    	}
    Et voici le code de Problem, qui contient les algorithmes:

    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
    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
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
     
     
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.PriorityQueue;
    import java.util.Queue;
    import java.util.Stack;
     
    /**
     * Generic class to solve graph problem, only in the special case where
     * there is a unique finalState
     * 
     */
    public class Problem<State extends Searchable<State,Action>,Action> {
    	int number_of_strokes_DFS=0;
    	int number_of_strokes_BFS=0;
    	int number_of_strokes_A1=0;
    	int number_of_strokes_A2=0;
    	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>();
     
     
     
     
        /** initial state of the search problem */
        State initialState;
        /** final state of the search problem */
        State finalState;
     
        /** method for checking whether a solution has been found by comparing a state with the final state
         * @param s is the state to be considered
         * @return true if the state s is the final state
         */
    	public boolean goal_test(State s){
    		return finalState.equals(s);
    	}
     
        /**
         * Constructeur initialize the initial state and the final state
         * @param start initial state
         * @param stop final state
         */
    	public Problem(State start, State stop){
    		initialState = start;
    		System.out.println("start :\n" + initialState);
    		finalState = stop;
    		System.out.println("stop :\n" + finalState);
    	}
     
        /** Breadth-first search algorithm
         * @return return a final state if it founds a path from the initial state to the final state
         */
     
     
     
        public State bfs(){
        	System.out.println("we entered BFS");
        	/** current state of dfs algorithm **/
        	State current_state;
        	current_state = initialState;
     
        	// get all possible actions from the given node
        	List<Action> actions = current_state.getActions();
     
        	//frontier is a Stack displaying not currently explored nodes
        	Stack<State> frontier = new Stack<State>();
        	// frontier already contains the first node
        	frontier.push(initialState);
     
        	// explored_nodes contains all explored nodes.
        	LinkedList<State> explored_nodes = new LinkedList<State>();
     
        	// this List is used to show the path
        	LinkedList<State> path = new LinkedList<State>();
        	long startTime=System.currentTimeMillis();
        	while(!frontier.isEmpty()){
        		number_of_strokes_BFS+=1;
        		if(System.currentTimeMillis()-startTime>1000)break;
     
        		// We remove the current state from the frontier
        		current_state = frontier.pop();
        		// We get all possible actions from the current state
        		actions = current_state.getActions();
        		// We add the current state to already explored nodes
        		explored_nodes.add(current_state);
        		System.out.println(current_state);
        		path.add(current_state);
     
        		if(goal_test(current_state)){
            		for(State visited :path){
            			System.out.println(visited);
     
            		}
        			System.out.println("nombre de coups BFS : "+number_of_strokes_BFS);
        			array_number_of_strokes_bfs.add(number_of_strokes_BFS);
        			number_of_strokes_BFS=0;
        			return current_state;
        		}
     
            	// We create every child
            	for (Action action : actions){
            		//System.out.println("action : " + action);
            		// we get a child from the execution of the current_state
            		State child = current_state.execute(action);
            		//System.out.println("we executed the action");
                	if(!explored_nodes.contains(child)&&!frontier.contains(child)){
                		// This child not being already explored nor int the frontier we add it to the last one
                		frontier.add(child);
                	}
            	}
     
        	}
    		array_number_of_strokes_dfs.add(-1);
    		return finalState;
        }
     
        /** Depth-first search algorithm
         * @return return a final state if it founds a path from the initial state to the final state
         */
        public State dfs(){
        	System.out.println("we entered DFS");
        	/** current state of dfs algorithm **/
        	State current_state;
        	current_state = initialState;
     
        	// get all possible actions from the given node
        	List<Action> actions = current_state.getActions();
     
        	//frontier is a Stack displaying not currently explored nodes
        	Stack<State> frontier = new Stack<State>();
        	// frontier already contains the first node
        	frontier.push(initialState);
     
        	// explored_nodes contains all explored nodes.
        	LinkedList<State> explored_nodes = new LinkedList<State>();
     
        	// this List is used to show the path
        	LinkedList<State> path = new LinkedList<State>();
     
        	long startTime=System.currentTimeMillis();
        	while(!frontier.isEmpty()){
    			number_of_strokes_DFS+=1;
        		if(System.currentTimeMillis()-startTime>1000)break;
        		// We remove the current state from the frontier
        		current_state = frontier.pop();
        		// We get all possible actions from the current state
        		actions = current_state.getActions();
        		// We add the current state to already explored nodes
        		explored_nodes.add(current_state);
        		//System.out.println(current_state);
        		path.add(current_state);
     
        		// we found the goal
        		if(goal_test(current_state)){
            		for(State visited :path){
            			System.out.println(visited);
     
            		}
    				array_number_of_strokes_dfs.add(number_of_strokes_DFS);
        			System.out.println("nombres de coups DFS"+number_of_strokes_DFS);
        			number_of_strokes_DFS=0;
        			return current_state;
        		}
     
            	// We create every child
            	for (Action action : actions){
            		//System.out.println("action : " + action);
            		// we get a child from the execution of the current_state
            		State child = current_state.execute(action);
            		//System.out.println("we executed the action");
                	if(!explored_nodes.contains(child)&&!frontier.contains(child)){
                		// This child not being already explored nor int the frontier we add it to the last one
                		frontier.push(child);
                	}
            	}
     
        	}
    		array_number_of_strokes_dfs.add(-1);
     
    		return finalState;
     
        }
     
        /** A star search algorithm. The heuristic is encoded in the state (State must implement
         * the interface Searchable that has a method computeHeuristic and getHeuristic
         * @return return a final state if it founds a path from the initial state to the final state
         */
     
        public State aStar(){
     
        	State child;
        	System.out.println("we entered A_star");
        	System.out.println("final:\n"+finalState);
        	/** current state of dfs algorithm **/
        	State current_state;
        	current_state = initialState;
    		current_state.computeHeuristic();
    		current_state.computeValueF();
        	//int best_f_value= current_state.getFValue();
        	int current_state_g_value;
        	System.out.println(initialState);
     
        	// get alwhile(!frontier.isEmpty()){l possible actions from the given node
        	List<Action> actions = current_state.getActions();
        	//frontier is a Stack displaying not currently explored nodes
        	LinkedList<State> frontier = new LinkedList<State>();
        	// frontier already contains the first node
        	frontier.push(initialState);
     
        	// explored_nodes contains all explored nodes.
        	LinkedList<State> explored_nodes = new LinkedList<State>();
     
        	// this List is used to show the path
        	LinkedList<State> path = new LinkedList<State>();
     
        	while(!frontier.isEmpty()){
        		number_of_strokes_A1+=1;
     
        		// we found the goal
        		if(goal_test(current_state)){
            		for(State visited :path){
            			System.out.println(visited);
     
            		}
    				array_number_of_strokes_A1.add(number_of_strokes_A1);
            		System.out.println("nombre de coups A1 : " +  number_of_strokes_A1);
            		int number_of_strokes_A1=0;
            		System.out.println("on a r�ussi : \n" + current_state);
        			return current_state;
        		}
        		// We remove the current state from the frontier
            	// VERIFY THIS IS OKAY !!!
        		frontier.remove(current_state);
        		// We get all possible actions from the current state
        		actions = current_state.getActions();
        		// We add the current state to already explored nodes
        		explored_nodes.add(current_state);
        		//System.out.println(current_state);
        		path.add(current_state);
     
     
        		current_state_g_value = current_state.getValueG();
        		//System.out.println("father , your f ! : "+ current_state.getFValue());
            	// We create every child
            	for (Action action : actions){
            		//System.out.println("action : " + action);
            		// we get a child from the execution of the current_state
            		child = current_state.execute(action);
            		child.setValueG(current_state_g_value);
            		child.computeHeuristic();
            		child.computeValueF();
            		//System.out.println(child);
            		//System.out.println("we executed the action");
            		//System.out.println("son , your f ! : "+ child.getFValue());
     
     
            		if(!explored_nodes.contains(child)&&!frontier.contains(child)){
                		// This child not being already explored nor int the frontier we add it to the last one
                		frontier.push(child);
                	}
            	}
     
            	int best_f_value=frontier.get(0).getFValue();
     
            	for(State s : frontier){
     
                	if(s.getFValue()<=best_f_value){
                		best_f_value=s.getFValue();
                		current_state=s;
     
                	}
     
                }
     
            		/*if(best_heuristic >= child.getHeuristic()){
            			best_heuristic = child.getHeuristic();
            			best_son = child;
            			System.out.println("We entered the chalet!");
                	}*/
     
     
     
            	// DO WE HAVE TO DEAL WITH THE CASE WHEN WE HAVE AN ELSE IF? THE BEST SON IS THE FATHER?
     
     
        	}
     
     
     
    		return finalState;
     
    	// to be completed
        }
     
        public State aStar2(){
     
        	State child;
        	System.out.println("we entered A_star with Manhattan distance");
        	System.out.println("final:\n"+finalState);
        	/** current state of dfs algorithm **/
        	State current_state;
        	current_state = initialState;
    		current_state.computeHeuristic();
    		current_state.computeValueF();
        	//int best_f_value= current_state.getFValue();
        	int current_state_g_value;
        	System.out.println(initialState);
     
        	// get alwhile(!frontier.isEmpty()){l possible actions from the given node
        	List<Action> actions = current_state.getActions();
        	//frontier is a Stack displaying not currently explored nodes
        	LinkedList<State> frontier = new LinkedList<State>();
        	// frontier already contains the first node
        	frontier.push(initialState);
     
        	// explored_nodes contains all explored nodes.
        	LinkedList<State> explored_nodes = new LinkedList<State>();
     
        	// this List is used to show the path
        	LinkedList<State> path = new LinkedList<State>();
     
        	while(!frontier.isEmpty()){
        		number_of_strokes_A2+=1;
     
        		// we found the goal
        		if(goal_test(current_state)){
            		for(State visited :path){
            			System.out.println(visited);
            		}
            		array_number_of_strokes_A2.add(number_of_strokes_A2);
            		System.out.println("nombre de coups A2 : " + number_of_strokes_A2);
            		number_of_strokes_A2=0;
            		System.out.println("on a réussi : \n" + current_state);
     
     
        			return current_state;
        		}
        		// We remove the current state from the frontier
            	// VERIFY THIS IS OKAY !!!
        		frontier.remove(current_state);
        		// We get all possible actions from the current state
        		actions = current_state.getActions();
        		// We add the current state to already explored nodes
        		explored_nodes.add(current_state);
        		//System.out.println(current_state);
        		path.add(current_state);
     
     
        		current_state_g_value = current_state.getValueG();
        		//System.out.println("father , your f ! : "+ current_state.getFValue());
            	// We create every child
            	for (Action action : actions){
            		//System.out.println("action : " + action);
            		// we get a child from the execution of the current_state
            		child = current_state.execute(action);
            		child.setValueG(current_state_g_value);
            		child.computeHeuristic2();
            		child.computeValueF2();
            		//System.out.println(child);
            		//System.out.println("we executed the action");
            		//System.out.println("son , your f ! : "+ child.getFValue());
     
     
            		if(!explored_nodes.contains(child)&&!frontier.contains(child)){
                		// This child not being already explored nor int the frontier we add it to the last one
                		frontier.push(child);
                	}
            	}
     
            	int best_f_value=frontier.get(0).getFValue();
     
            	for(State s : frontier){
     
                	if(s.getFValue()<=best_f_value){
                		best_f_value=s.getFValue();
                		current_state=s;
     
                	}
     
                }
     
            		/*if(best_heuristic >= child.getHeuristic()){
            			best_heuristic = child.getHeuristic();
            			best_son = child;
            			System.out.println("We entered the chalet!");
                	}*/
     
     
     
            	// DO WE HAVE TO DEAL WITH THE CASE WHEN WE HAVE AN ELSE IF? THE BEST SON IS THE FATHER?
     
     
        	}
     
     
     
    		return finalState;
     
        }
     
        public List<Integer> get_number_of_strokes_A1(){
    		return array_number_of_strokes_A1;
     
        }
     
        public List<Integer> get_number_of_strokes_A2(){
    		return array_number_of_strokes_A2;
     
        }
     
        public List<Integer> get_number_of_strokes_dfs(){
    		return array_number_of_strokes_dfs;
     
        }
     
        public List<Integer> get_number_of_strokes_bfs(){
    		return array_number_of_strokes_bfs;
     
        }
     
    }

  5. #5
    Membre du Club
    Homme Profil pro
    Etudiant
    Inscrit en
    Novembre 2015
    Messages
    156
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Etudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 156
    Points : 52
    Points
    52
    Par défaut C'est étrange le nouveau fichier n'affiche rien
    Je pense y être arrivé avec des getters et en reprenant la structure de création de fichier mais je ne parviens d'abord pas à faire apparaitre le fichier à la racine du workspace.
    Ca me force à aller voir au niveau des "files".
    Ensuite j'essaye deréaliser le calcul du temps en nombre de coup ainsi que la moyenne par difficultés et par algorithme.


    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
    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);
     
    	  }       
    	}

  6. #6
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 710
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 710
    Points : 4 791
    Points
    4 791
    Par défaut
    Dans ta classe Project, tu as 3 PrintWriter : writer, writer1 et writer_strokes
    Lequel est vide ?
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

  7. #7
    Membre du Club
    Homme Profil pro
    Etudiant
    Inscrit en
    Novembre 2015
    Messages
    156
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Etudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 156
    Points : 52
    Points
    52
    Par défaut Tout les fichiers sont remplis mais le dernier ne s'affiche pas dans Eclispe dans le projet
    Non, c'est bon désormais, même si c'est bizarre qu'il ne se mette pas à gauche.
    Je cherche désormais à faire la moyenne de coup par difficulté. Je ne sais pas encore comment faire je sais que je commence un niveau de difficulté dès que je lis %i ou i est le niveau de difficulté dans mon fichier games. txt. à chaque fois qu'un algorithme est appelé cela crée un nombre de coups dans les tableaux contenant le nombre de coup respectif ainsi que dans le dernier fichier strokes.

    Je cherche un moyen à faire un fichier de moyenne et d’écart type de nombre de coups.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Mettre les résultats d'une boucle dans un tableau
    Par TuNi54 dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 09/03/2011, 17h02
  2. Réponses: 18
    Dernier message: 06/02/2008, 11h51
  3. [SQL] Comment je peux mettre les résultat d'une requete dans un fichier
    Par Maria1505 dans le forum PHP & Base de données
    Réponses: 10
    Dernier message: 10/12/2006, 21h44
  4. Réponses: 3
    Dernier message: 04/07/2006, 16h34
  5. Mettre les valeurs d'un tableau dans un fichier
    Par ero-sennin dans le forum C++
    Réponses: 4
    Dernier message: 14/03/2006, 13h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo