Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void simulatedAnnealing(int noIteration, ProblemData problemData) throws IOException {
- Date start = new Date();
- ArrayList<Solution> bestSolutions = new ArrayList<>();
- double cost = 0;
- for (int k = 0; k < 10; k++) {
- /*
- * Parameters section
- */
- double T = 10000;
- double alpha = 0.9999;
- double minT = 0.00001;
- Solution c = UtilFunctions.createRandomSolution(problemData);
- UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
- "Starting random point:" + "\n" + c);
- ArrayList<Solution> solutions = new ArrayList<>();
- solutions.add(c);
- System.out.println(String.format("Initial Solution: %s", c));
- Random rnd = new Random();
- while (T > minT) {
- int i = 1;
- do {
- Solution x = UtilFunctions.getVecin(c, problemData);
- double delta = x.getTotalCost() - c.getTotalCost();
- if (delta < 0) {
- c = new Solution(i, x.getCitiesVisited(), x.getTotalCost());
- solutions.add(c);
- } else if (rnd.nextDouble() < Math.exp(-delta / T)) {
- c = new Solution(i, x.getCitiesVisited(), x.getTotalCost());
- }
- i++;
- } while (i < noIteration);
- T = alpha * T;
- }
- solutions.sort(new Comparator<Solution>() {
- @Override
- public int compare(Solution sol0, Solution sol1) {
- if (sol0.getTotalCost() > sol1.getTotalCost())
- return 1;
- else if (sol0.getTotalCost() < sol1.getTotalCost())
- return -1;
- return 0;
- }
- });
- cost += solutions.get(0).getTotalCost();
- bestSolutions.add(solutions.get(0));
- System.out.println(String.format("Final Solution: %s", solutions.get(0)));
- UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
- "Best ending point:" + "\n" + c);
- UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt", "\n \n");
- }
- bestSolutions.sort(new Comparator<Solution>() {
- @Override
- public int compare(Solution sol0, Solution sol1) {
- if (sol0.getTotalCost() > sol1.getTotalCost())
- return 1;
- else if (sol0.getTotalCost() < sol1.getTotalCost())
- return -1;
- return 0;
- }
- });
- UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
- "Best Solution Found: " + bestSolutions.get(0));
- UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
- "Average cost(distance): " + Math.round((cost / 10)/100.0)*100.0);
- Date end = new Date();
- UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
- "Timp " + (end.getTime() - start.getTime()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement