-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.java
148 lines (129 loc) · 3.18 KB
/
Solver.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Timer;
import java.util.TimerTask;
/**
* This class is responsible for solving the algorithm.
* @author Stephen Chambers
* @author Michaela Tremblay
*
*/
public class Solver {
/**
* A sorted list of students
*/
private PriorityQueue<Student> students;
/**
* The current solution cost
*/
private int solutionCost;
/**
* The FileIO object that reads the input file
* and stores information in data structures.
*/
private FileIO fileIO;
/**
* The timeout for the program
*/
private int timeout = 0;
/**
* Whether or not this program is being tested locally
*/
private static boolean localTesting = true;
/**
* Constructor
*/
public Solver(){
getInfo();
}
/**
* Constructor
*/
public Solver(int timeout){
this.timeout = timeout;
getInfo();
}
/**
* A method that spawns an ILDS object and calls its solve method.
* If other algorithms were used, this would be a good place to
* call their solve methods.
*/
public void solve(){
/* Create a depth first search instance */
ILDS dfs = new ILDS(
students,
fileIO.getGroupTimes());
if(timeout != 0){
Thread thread = new Thread(){
public void run(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
dfs.end();
}
}, timeout);
}
};
thread.start();
}
/* Solve and print the solution */
dfs.solve();
solutionCost = dfs.getSolutionCost();
printStudentSolution(dfs.getSolution());
}
/**
* Parse input and store results into FileInfo
*/
private void getInfo(){
fileIO = new FileIO();
fileIO.getInfo();
students = fileIO.getStudents();
}
/**
* Print the student solution
* @param studs
* The studly students assigned to their study groups
*/
private void printStudentSolution(ArrayList<Student> studs) {
System.out.println("Solution Format: 3");
System.out.println("Objective Function: " + Objective.getDescription());
System.out.println("Class Info: " + fileIO.getClassDesc());
System.out.println("Student Info: " + fileIO.getStudentDesc());
System.out.println("Number of students: " + studs.size());
for(Student s : studs){
System.out.println(s.getEmail());
System.out.println(s.getGroupAssignment().getTime());
System.out.println(s.getGroupAssignment().getTAEmail());
}
System.out.println("Solution cost: " + solutionCost);
}
/**
* The main method. Sets stdin to a file if local testing
* flag is set
* @param args
*/
public static void main(String[] args){
/* Populate initial world state */
//String filename = "test.txt";
//String filename = "bigTest";
//String filename = "real-students.txt";
String filename = "remove_students";
//String filename = "add_times";
if(localTesting){
try {
System.setIn(new FileInputStream(filename));
} catch (FileNotFoundException e) {
}
}
Solver s = null;
if(args.length != 0){
s = new Solver(Integer.parseInt(args[0]) * 1000);
} else{
s = new Solver();
}
s.solve();
}
}