Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
course_scheduling_run.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// This file implements the main function for the Course Scheduling solver. It
15// reads the problem specification from an input file specified via command-line
16// flags, and prints the time slots for each course.
17//
18// Example usage:
19// ./course_scheduling_run --input_file=testdata/my_input_proto.textproto
20
21#include <cstdlib>
22#include <string>
23
24#include "absl/flags/flag.h"
25#include "absl/log/log.h"
29#include "ortools/base/timer.h"
32
33ABSL_FLAG(std::string, input, "",
34 "Input file containing a CourseSchedulingModel in text format.");
35
36namespace operations_research {
37
38void Main() {
40 const auto proto_status =
41 file::GetTextProto(absl::GetFlag(FLAGS_input), &input, file::Defaults());
42 if (!proto_status.ok()) {
43 LOG(ERROR) << proto_status.message();
44 return;
45 }
46
48 WallTimer timer;
49 timer.Start();
50 const CourseSchedulingResult result = solver.Solve(input);
51 timer.Stop();
52
53 LOG(INFO) << "Solver result status: "
55 << result.message();
56
57 for (const ClassAssignment& class_assignment : result.class_assignments()) {
58 const int course_index = class_assignment.course_index();
59 const int section_number = class_assignment.section_number();
60
61 int teacher_index = 0;
62 const Course& course = input.courses(course_index);
63 int sections = 0;
64 for (int section_index = 0;
65 section_index < course.teacher_section_counts_size();
66 ++section_index) {
67 sections += course.teacher_section_counts(section_index);
68 if (section_number < sections) {
69 teacher_index = course.teacher_indices(section_index);
70 break;
71 }
72 }
73
74 LOG(INFO) << course.display_name();
75 LOG(INFO) << " Section: " << section_number;
76 LOG(INFO) << " Teacher: " << input.teachers(teacher_index).display_name();
77 for (int i = 0; i < class_assignment.time_slots_size(); ++i) {
78 if (input.rooms_size() > 0) {
79 LOG(INFO)
80 << " Scheduled for time slot " << class_assignment.time_slots(i)
81 << " in room "
82 << input.rooms(class_assignment.room_indices(i)).display_name();
83 } else {
84 LOG(INFO) << " Scheduled for time slot "
85 << class_assignment.time_slots(i);
86 }
87 }
88 }
89
90 for (const StudentAssignment& student_assignment :
91 result.student_assignments()) {
92 const int student_index = student_assignment.student_index();
93
94 LOG(INFO) << input.students(student_index).display_name();
95 for (int i = 0; i < student_assignment.course_indices_size(); ++i) {
96 LOG(INFO)
97 << " "
98 << input.courses(student_assignment.course_indices(i)).display_name()
99 << " " << student_assignment.section_indices(i);
100 }
101 }
102
103 LOG(INFO) << "Solved model in " << timer.GetDuration();
104}
105
106} // namespace operations_research
107
108int main(int argc, char** argv) {
109 InitGoogle(argv[0], &argc, &argv, /*remove_flags=*/true);
111 return EXIT_SUCCESS;
112}
absl::Duration GetDuration() const
Definition timer.h:47
void Start()
Definition timer.h:30
void Stop()
Definition timer.h:38
const ::operations_research::ClassAssignment & class_assignments(int index) const
const ::operations_research::StudentAssignment & student_assignments(int index) const
::operations_research::CourseSchedulingResultStatus solver_status() const
CourseSchedulingResult Solve(const CourseSchedulingModel &model)
::int32_t teacher_indices(int index) const
::int32_t teacher_section_counts(int index) const
const ::std::string & display_name() const
int main(int argc, char **argv)
ABSL_FLAG(std::string, input, "", "Input file containing a CourseSchedulingModel in text format.")
void InitGoogle(absl::string_view usage, int *argc, char ***argv, bool deprecated)
Definition init_google.h:49
absl::Status GetTextProto(absl::string_view file_name, google::protobuf::Message *proto, Options options)
Definition file.cc:409
Options Defaults()
Definition file.h:86
OR-Tools root namespace.
const ::std::string & CourseSchedulingResultStatus_Name(T value)
static int input(yyscan_t yyscanner)