0
알고리즘을 반복 할 때마다 업데이트되는 복식의 arraylist가 있습니다. 나는 파일의 전체 목록을 파일에 출력하려고하지만 전체 목록 대신 하나의 값만 출력한다고 가정합니다. 간단한 시뮬레이션 어닐링 로컬 검색을 사용합니다. 각 평가 후 배열에 새 값을 저장하고 목록의 모든 값을 파일로 출력하려고합니다.파일로 복식 목록을 작성하십시오.
내 코드 :
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();
}
코드의 무슨 파일에 씁니 까? –
@AndrewS 내가 도움을 청하고 있습니다. – Kizzle
많은 예제가 있는데, 어떻게 작동하지 않았습니까? –