-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAprogram.java
More file actions
132 lines (102 loc) · 3.65 KB
/
SAprogram.java
File metadata and controls
132 lines (102 loc) · 3.65 KB
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
//21012040 - Tayyeb Rafique
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.util.*;
import java.lang.Math;
public class SAprogram {
public static void main(String[] args) {
//Start Timer
long startTime = System.nanoTime();
//initialise size variables and counter for reader file
int y = 2;
int size = 0;
//declare new Tournament to fill
Tournament T = new Tournament();
//Readfile
try {
File myObj = new File(args[0]);
Scanner myReader = new Scanner(myObj);
size = Integer.parseInt(myReader.nextLine());
T.Tournamentu(size);
while (myReader.hasNextLine()){
String data = myReader.nextLine();
//Store competitor names in hashmap
if (y>1 && y<size+2){
String[] name = data.split(",");
T.setName(Integer.parseInt(name[0])-1,name[1]);
}
//Store tournment results in a 2D matrix
if (y>size+2){
int[] edge = Arrays.stream(data.split(",")).mapToInt(Integer::parseInt).toArray();
for(int i= 0; i<3; i++){
}
T.setT(edge[1]-1,edge[2]-1,edge[0]);
}
y++;
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
//Create an initial ranking
Ranking R = new Ranking(T);
//Score the initial ranking
int initKemeny = R.calcKem();
Random rand = new Random();
//SA algorithm starts here (TL declared later on as 5000)
int num_non_improve = 0;
int num_non_improve_limit = 80;
double ti = 1;
double a = 0.9;
//outer loop
while(num_non_improve < num_non_improve_limit){
//inner loop
for (double tl= 5000; tl>0; tl--){
//choose an random index in the ranking
int index = rand.nextInt(T.size()-1);
//swap two adjacent competitors in the ranking and get the new score
int newKs = R.swapADJ(index);
//compare the old and new scores to get deltaC
int deltaC = newKs - R.getKemeny();
//if no change in kScore, the swap stays, but num_non_improve increased
if (deltaC == 0){
R.setKemeny();
++num_non_improve;
}
//if the change is negative, the new ranking is better, so it replaces the current ranking
else if(deltaC < 0){
R.setKemeny();
num_non_improve = 0;
}
//if the change is positive, the new ranking is worse, and we
//accept it based of a function of deltaC and current temp
//as well as incrementing num_non_improve
else if ( Math.random()<(Math.exp(-deltaC/ti))){
R.storeBest();
R.setKemeny();
++num_non_improve;
}
if (num_non_improve >= num_non_improve_limit){
break;
}
}
//decay tempreture
ti *= a;
}
R.restoreBest();
int [] bestRank = R.getRank();
long endTime = System.nanoTime();
System.out.println("Runtime: "+((endTime-startTime)/1000000) + "ms");
System.out.println("Final Kemeny Score of this ranking: " + R.getKemeny());
System.out.print("Position | Competitor No. | Name |");
System.out.println(" ");
for(int i = 0; i<T.size(); i++){
System.out.print(" "+(i+1)+" " + " "+(bestRank[i]+1)+" " +T.getName(bestRank[i]));
System.out.println(" ");
}
System.out.println("Please open 'GRAPHS.html' to see full size graphs.");
}
}