-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.java
More file actions
181 lines (158 loc) · 6.47 KB
/
Solver.java
File metadata and controls
181 lines (158 loc) · 6.47 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.ArrayList;
/**
* Created by krejcir on 29.4.14.
*/
public class Solver {
private boolean lectureCoversLimitation = false;
private boolean lectureCoversLecture = false;
private boolean exerciseCoversLecture = false;
public static final int WEEK_LENGTH = 5;
public static final int DAY_LENGTH = 15;
private ArrayList<Subject> subjects;
private Schedule schedule;
private ArrayList<Integer> disallowedDays;
private int earliestItem = 0;
private int latestItem = Solver.DAY_LENGTH;
public Solver(ArrayList<Subject> subjects) {
this.subjects = subjects;
this.schedule = new Schedule();
this.disallowedDays = new ArrayList<Integer>();
}
public void forbidDay(int day) throws SmartScheduleException {
switch (day) {
case Day.MONDAY:
case Day.TUESDAY:
case Day.WEDNESDAY:
case Day.THURSDAY:
case Day.FRIDAY:
case Day.SATURDAY:
case Day.SUNDAY:
this.disallowedDays.add(day);
break;
default:
throw new SmartScheduleException("Undefined day of week.");
}
}
public void setEarliestItem(int earliestItem) {
this.earliestItem = earliestItem;
}
public void setLatestItem(int latestItem) {
this.latestItem = latestItem;
}
public Schedule solve() throws SmartScheduleException {
int schedulingTable[][] = new int[Solver.WEEK_LENGTH][Solver.DAY_LENGTH + 1];
if (this.disallowedDays.size() > 0) {
this.applyDisallowedDays(schedulingTable);
}
if (this.earliestItem > 0 || this.latestItem < Solver.DAY_LENGTH) {
this.applyDisallowedHours(schedulingTable);
}
this.generateLectureSchedule(schedulingTable);
if (this.exerciseCoversLecture) {
schedulingTable = new int[Solver.WEEK_LENGTH][Solver.DAY_LENGTH + 1];
if (this.disallowedDays.size() > 0) {
this.applyDisallowedDays(schedulingTable);
}
if (this.earliestItem > 0 || this.latestItem < Solver.DAY_LENGTH) {
this.applyDisallowedHours(schedulingTable);
}
}
this.generateExerciseSchedule(schedulingTable);
return this.schedule;
}
private void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
private int[][] applyDisallowedDays(int[][] schedulingTable) {
for (int day : this.disallowedDays) {
for (int i = 0; i < schedulingTable[day].length; i++) {
schedulingTable[day][i] = -1;
}
}
return schedulingTable;
}
private int[][] applyDisallowedHours(int[][] schedulingTable) {
for (int i = 0; i < schedulingTable.length; i++) {
for (int j = 0; j < schedulingTable[i].length; j++) {
if (j < this.earliestItem || j > this.latestItem) {
schedulingTable[i][j] = -1;
}
}
}
return schedulingTable;
}
private int[][] generateLectureSchedule(int[][] schedulingTable) throws SmartScheduleException {
for (Subject subject : this.subjects) {
Lecture lecture = subject.getLecture();
for (int i = 0; i < lecture.getLength(); i++) {
if (schedulingTable[lecture.getDay()][lecture.getStart() + i] > 0 && (!this.lectureCoversLecture)) {
throw new SmartScheduleException("Schedule not exist, covering lectures found.");
} else if (schedulingTable[lecture.getDay()][lecture.getStart() + i] < 0 && (!this.lectureCoversLimitation)) {
throw new SmartScheduleException("Schedule not exist, covering lectures found.");
}
schedulingTable[lecture.getDay()][lecture.getStart() + i] = 1;
}
this.schedule.addItem(lecture);
}
return schedulingTable;
}
private int[][] generateExerciseSchedule(int[][] schedulingTable) throws SmartScheduleException {
try {
schedulingTable = this.checkExercises(schedulingTable.clone(), 0);
} catch (SmartScheduleException e) {
throw new SmartScheduleException("Schedule not exist, covering exercises found.");
}
return schedulingTable;
}
private int[][] checkExercises(int schedulingTable[][], int index) throws SmartScheduleException {
Subject subject = this.subjects.get(index);
for (Exercise exercise : subject.getExercises()) {
boolean collision = false;
for (int i = 0; i < exercise.getLength(); i++) {
if (schedulingTable[exercise.getDay()][exercise.getStart() + i] == 1 && (!this.exerciseCoversLecture)) {
collision = true;
} else if (schedulingTable[exercise.getDay()][exercise.getStart() + i] < 0) {
collision = true;
} else if (schedulingTable[exercise.getDay()][exercise.getStart() + i] == 2) {
collision = true;
}
}
if (collision) {
continue;
}
for (int i = 0; i < exercise.getLength(); i++) {
schedulingTable[exercise.getDay()][exercise.getStart() + i] = 2;
}
this.schedule.addItem(exercise);
try {
if (index + 1 < this.subjects.size()) {
return this.checkExercises(schedulingTable.clone(), index + 1);
} else {
return schedulingTable;
}
} catch (SmartScheduleException e) {
this.schedule.popItem();
for (int i = 0; i < exercise.getLength(); i++) {
schedulingTable[exercise.getDay()][exercise.getStart() + i] = 0;
}
continue;
}
}
throw new SmartScheduleException("No possible schedule in this branch.");
}
public void allowLectureCoversLimitation() {
this.lectureCoversLimitation = true;
}
public void allowLectureCoversLecture() {
this.lectureCoversLecture = true;
}
public void allowExerciseCoversLecture() {
this.exerciseCoversLecture = true;
}
}