0

I have an arraylist of doubles that I update each time I iterate through an algorithm. I want to output the full list of numbers to a file but it as only outputting one value instead of the entire list. I use a simple simulated annealing local search. After each evaluation I store the new value in an array and I want to then output all values of the list to a file.

My code:

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

Code that writes only one entry to file:

    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();
    }
Kizzle
  • 101
  • 3
  • 17
  • 2
    What's the code that writes to the file? – Andrew S Nov 20 '17 at 20:28
  • @AndrewS that's what I'm asking for help on – Kizzle Nov 20 '17 at 20:52
  • There are many examples, what have you tried that isn't working? – Andrew S Nov 20 '17 at 21:06
  • @AndrewS I've tried a FileOutputStream and it only outputs one value and not the entire list. I'm just looking for a simple solution that will store all list values in a file – Kizzle Nov 20 '17 at 21:10
  • 1
    Post the code that's not working. – Andrew S Nov 20 '17 at 21:16
  • https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java – ajc Nov 20 '17 at 21:21
  • @ajc I've tried that, but again it does not print the full list. – Kizzle Nov 20 '17 at 21:24
  • Use a database to store data. – DwB Nov 20 '17 at 21:25
  • 1
    `simAnnealFit` appears to be of type double so `list = Arrays.asList(simAnnealFit);` will be a list with a single element. Perhaps the code needs to add to `list`, then after all iterations write to the file. Or write to the file on each iteration but don't overwrite the file. – Andrew S Nov 20 '17 at 21:32
  • There is nothing wrong with the code you have that writes a list to a file. The problem is that your list only contains one value – bhspencer Nov 20 '17 at 22:13
  • Rather than list = 'Arrays.asList(simAnnealFit);' try 'list.add(simAnnealFit);' – bhspencer Nov 20 '17 at 22:15
  • @bhspencer it now stores each value in the file but it stores them all on one line. How would I separate them so that each list entry is on a new line? – Kizzle Nov 20 '17 at 22:27
  • When I ran your save to file code it put each double on new line. That is what the + "\n" does – bhspencer Nov 20 '17 at 23:03
  • @bhspencer I fixed this issue by using the newLine() method after the write() because the + "\n" didn't seem to work – Kizzle Nov 21 '17 at 01:50

1 Answers1

0

Implementation used to write Array of doubles to file:

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 ();
            }
Kizzle
  • 101
  • 3
  • 17