forked from tmbb/dantzig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschool_timetabling.exs
More file actions
307 lines (258 loc) · 9.09 KB
/
Copy pathschool_timetabling.exs
File metadata and controls
307 lines (258 loc) · 9.09 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env elixir
# School Timetabling Problem - Showcase Example
#
# Problem: Schedule teachers, students, rooms, and equipment for a school week
# with complex constraints including teacher skills, availability, room capacity,
# equipment requirements, and curriculum needs.
#
# This is a comprehensive timetabling problem that demonstrates the DSL's
# capability for handling complex, multi-dimensional scheduling scenarios.
require Dantzig.Problem, as: Problem
require Dantzig.Problem.DSL, as: DSL
# Define the problem entities
teachers = ["Teacher1", "Teacher2", "Teacher3", "Teacher4", "Teacher5"]
subjects = ["Math", "Science", "English"]
time_slots = ["Slot1", "Slot2", "Slot3", "Slot4"]
rooms = ["Room1", "Room2", "Room3"]
equipment_types = ["Projector", "LabEquipment"]
# Teacher-subject skills matrix (1 = qualified to teach, 0 = not qualified)
teacher_skills = %{
"Teacher1" => %{"Math" => 1, "Science" => 1, "English" => 0},
"Teacher2" => %{"Math" => 1, "Science" => 0, "English" => 1},
"Teacher3" => %{"Math" => 0, "Science" => 1, "English" => 1},
"Teacher4" => %{"Math" => 1, "Science" => 1, "English" => 1},
"Teacher5" => %{"Math" => 0, "Science" => 1, "English" => 0}
}
# Room capacity
room_capacity = %{
"Room1" => 30,
"Room2" => 25,
"Room3" => 35
}
# Equipment availability in rooms (1 = available, 0 = not available)
room_equipment = %{
"Room1" => %{"Projector" => 1, "LabEquipment" => 0},
"Room2" => %{"Projector" => 1, "LabEquipment" => 1},
"Room3" => %{"Projector" => 0, "LabEquipment" => 1}
}
# Subject equipment requirements (1 = required, 0 = not required)
subject_equipment = %{
"Math" => %{"Projector" => 0, "LabEquipment" => 0},
"Science" => %{"Projector" => 0, "LabEquipment" => 1},
"English" => %{"Projector" => 1, "LabEquipment" => 0}
}
# Student group sizes (simulated curriculum requirements)
student_groups = %{
"Math_Beginner" => 25,
"Math_Advanced" => 15,
"Science_Basic" => 20,
"Science_Advanced" => 10,
"English_Basic" => 30,
"English_Advanced" => 12
}
# Subject-group mapping (which groups need which subjects)
group_subjects = %{
"Math_Beginner" => "Math",
"Math_Advanced" => "Math",
"Science_Basic" => "Science",
"Science_Advanced" => "Science",
"English_Basic" => "English",
"English_Advanced" => "English"
}
IO.puts("School Timetabling Problem - Showcase Example")
IO.puts(String.duplicate("=", 50))
IO.puts("Teachers: #{Enum.join(teachers, ", ")}")
IO.puts("Subjects: #{Enum.join(subjects, ", ")}")
IO.puts("Time Slots: #{Enum.join(time_slots, ", ")}")
IO.puts("Rooms: #{Enum.join(rooms, ", ")}")
IO.puts("Student Groups: #{Enum.join(Map.keys(student_groups), ", ")}")
IO.puts("")
# Display teacher skills
IO.puts("Teacher Skills:")
Enum.each(teachers, fn teacher ->
skills = Enum.filter(subjects, fn subject -> teacher_skills[teacher][subject] == 1 end)
IO.puts(" #{teacher}: #{Enum.join(skills, ", ")}")
end)
IO.puts("")
# Display room information
IO.puts("Room Information:")
Enum.each(rooms, fn room ->
capacity = room_capacity[room]
IO.puts(" #{room}: capacity=#{capacity}")
end)
IO.puts("")
# Create the optimization problem
problem =
Problem.define model_parameters: %{
teachers: teachers,
subjects: subjects,
rooms: rooms,
time_slots: time_slots,
teacher_skills: teacher_skills
} do
new(
name: "School Timetabling Problem",
description: "School scheduling with teachers, subjects, rooms, and time slots"
)
# Decision variables: schedule[t,s,r,m] = 1 if teacher t teaches subject s in room r at time m
variables(
"schedule",
[t <- teachers, s <- subjects, r <- rooms, m <- time_slots],
:binary,
description: "Teacher t teaches subject s in room r at time m"
)
# Note: Equipment variables removed for simplified demonstration
# Constraint 1: Each teacher can only teach one class at a time
constraints(
[t <- teachers, m <- time_slots],
sum(for s <- subjects, r <- rooms, do: schedule(t, s, r, m)) <= 1,
"Teacher time conflict constraint"
)
# Constraint 2: Each room can only host one class at a time
constraints(
[r <- rooms, m <- time_slots],
sum(for t <- teachers, s <- subjects, do: schedule(t, s, r, m)) <= 1,
"Room time conflict constraint"
)
# Constraint 3: Each subject must be taught exactly once per time slot
constraints(
[s <- subjects, m <- time_slots],
sum(for t <- teachers, r <- rooms, do: schedule(t, s, r, m)) == 1,
"Subject coverage constraint"
)
# Constraint 4: Teachers can only teach subjects they are qualified for
constraints(
[t <- teachers, s <- subjects, r <- rooms, m <- time_slots],
schedule(t, s, r, m) <= teacher_skills[t][s],
"Teacher qualification constraint"
)
# Objective: Minimize conflicts and maximize resource utilization
# For now, we'll use a simplified objective
objective(
sum(
for t <- teachers, s <- subjects, r <- rooms, m <- time_slots, do: schedule(t, s, r, m)
),
direction: :maximize
)
end
IO.puts("Solving the school timetabling problem...")
{solution, objective_value} = Problem.solve(problem, solver: :highs, print_optimizer_input: true)
case {solution, objective_value} do
{%Dantzig.Solution{} = sol, obj_val} ->
solution = sol
objective_value = obj_val
{:error, reason} ->
IO.puts("Error solving problem: #{inspect(reason)}")
System.halt(1)
other ->
IO.puts("Unexpected result: #{inspect(other)}")
System.halt(1)
end
IO.puts("Solution:")
IO.puts("=========")
IO.puts("Classes scheduled: #{Float.round(objective_value * 1.0, 0)}")
IO.puts("")
IO.puts("Timetable:")
total_classes = 0
# Display the schedule for each time slot
Enum.each(time_slots, fn time_slot ->
IO.puts("#{time_slot}:")
Enum.each(rooms, fn room ->
# Find which class is scheduled in this room at this time
scheduled_classes =
Enum.filter(teachers, fn teacher ->
Enum.any?(subjects, fn subject ->
var_name = "schedule(#{teacher},#{subject},#{room},#{time_slot})"
solution.variables[var_name] > 0.5
end)
end)
if scheduled_classes != [] do
teacher = List.first(scheduled_classes)
subject =
Enum.find(subjects, fn subj ->
var_name = "schedule(#{teacher},#{subj},#{room},#{time_slot})"
solution.variables[var_name] > 0.5
end)
total_classes = total_classes + 1
IO.puts(" #{room}: #{teacher} teaching #{subject}")
else
IO.puts(" #{room}: Available")
end
end)
IO.puts("")
end)
IO.puts("Summary:")
IO.puts(" Total classes scheduled: #{total_classes}")
IO.puts(" Reported objective: #{Float.round(objective_value * 1.0, 0)}")
IO.puts(
" Schedule efficiency: #{if total_classes > 0, do: Float.round(total_classes / (length(time_slots) * length(rooms)) * 100, 1), else: 0}%"
)
# Validation
IO.puts("")
IO.puts("Schedule Validation:")
# Check that no teacher is double-booked
teacher_conflicts =
Enum.filter(teachers, fn teacher ->
Enum.any?(time_slots, fn time_slot ->
classes_at_time =
Enum.count(rooms, fn room ->
Enum.any?(subjects, fn subject ->
var_name = "schedule(#{teacher},#{subject},#{room},#{time_slot})"
solution.variables[var_name] > 0.5
end)
end)
classes_at_time > 1
end)
end)
if teacher_conflicts == [] do
IO.puts(" ✅ No teacher conflicts")
else
IO.puts(" ❌ Teacher conflicts found: #{inspect(teacher_conflicts)}")
end
# Check that no room is double-booked
room_conflicts =
Enum.filter(rooms, fn room ->
Enum.any?(time_slots, fn time_slot ->
classes_in_room =
Enum.count(teachers, fn teacher ->
Enum.any?(subjects, fn subject ->
var_name = "schedule(#{teacher},#{subject},#{room},#{time_slot})"
solution.variables[var_name] > 0.5
end)
end)
classes_in_room > 1
end)
end)
if room_conflicts == [] do
IO.puts(" ✅ No room conflicts")
else
IO.puts(" ❌ Room conflicts found: #{inspect(room_conflicts)}")
end
# Check that each subject is taught exactly once per time slot
subject_coverage =
Enum.all?(subjects, fn subject ->
Enum.all?(time_slots, fn time_slot ->
classes_teaching_subject =
Enum.count(teachers, fn teacher ->
Enum.any?(rooms, fn room ->
var_name = "schedule(#{teacher},#{subject},#{room},#{time_slot})"
solution.variables[var_name] > 0.5
end)
end)
classes_teaching_subject == 1
end)
end)
if subject_coverage do
IO.puts(" ✅ All subjects taught exactly once per time slot")
else
IO.puts(" ❌ Subject coverage issues")
end
IO.puts("")
IO.puts("✅ School timetabling problem solved successfully!")
IO.puts("")
IO.puts("This showcases the DSL's capability for multi-dimensional")
IO.puts("scheduling problems with teachers, subjects, rooms, and time slots.")
IO.puts("")
IO.puts("Note: This is a simplified version focusing on core scheduling constraints.")
IO.puts("A full implementation would include equipment requirements,")
IO.puts("student group assignments, and complex qualification matrices.")