0

私は、アルゴリズムを反復するたびに私が更新するdouble型のarraylistを持っています。私は数字の完全なリストをファイルに出力したいが、リスト全体ではなく1つの値を出力するだけである。私は単純なシミュレーテッドアニーリングローカル検索を使用します。各評価の後、私は新しい値を配列に格納して、リストのすべての値をファイルに出力したいと思います。ファイルへのダブルリストの書き込み

マイコード:ファイルへの唯一のエントリを書き込み

 while (eval < 50) { 
      //Pick two positions on the grid 
      int Pos1 = (int) (Math.random() * size()); 
      int Pos2 = (int) (Math.random() * size()); 


      boolean swap = array1[Pos1]; 
      array2[Pos1] = array1[Pos2]; 
      array2[Pos2] = swap; 


      Fitness = evaluate(array2); 
      double current = fits[value]; 
      System.out.println(eval + " " +Fitness); 
      list = Arrays.asList(Fitness); 
      //I WANT TO SAVE ALL VALUES IN THIS LIST TO A FILE 



      //Keep track of best solution by only replacing it if a better fitness 
      //value is found by the algorithm 
      if(Fitness <= current) 
      { 
       for(int i=0; i<grid.size();i++) { 
        individuals[value][i] = array2[i]; 
        fits[value] = Fitness; 
       } 

      } 
      else { 
       //value for the current solution 
       double difference = Fitness - current; 
       double random = Math.random(); 
       double prob = Math.exp(-(double)Math.abs(difference)/Temp); 


       if(prob > random) 
       { 
        for(int i=0; i<grid.size();i++) { 
         individuals[value][i] = array1[i]; 
         fits[value] = current; 
        } 

       } 
      } 

      //Cool the system 
      Temp *= 1-coolingRate; 
      eval++; 
     } 

コード:

list = Arrays.asList(Fitness); 

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(FNAME))) { 
     for (Double line : list) { 
      bw.write(line + "\n"); 
     } 
     bw.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+2

そのコードは何ファイルに書き込みますか? –

+0

@AndrewSそれは私が助けを求めているのです – Kizzle

+0

多くの例がありますが、あなたはそれがうまくいかないことを試しましたか? –

答えて

0

実装をファイルにdoubleの配列を書き込むために使用:

ArrayList<Double> yourArray = new ArrayList<>(); 
final String FILENAME = "sample.txt"; 
      list.add(arrayValue) 
      try (BufferedWriter bw = new BufferedWriter (new FileWriter (FILENAME))) 
      {   
       for (Double line : yourArray) { 
        bw.write (line + "\n"); 
        bw.newLine(); 
       } 

       bw.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
関連する問題