Move course_scheduling from examples to ortools/scheduling (#4972)

Also export tests for Bazel
This commit is contained in:
Guillaume Chatelet
2026-01-07 15:51:08 +01:00
committed by Corentin Le Molgat
parent a7ffad1950
commit 74a9ed242d
8 changed files with 1526 additions and 11 deletions

View File

@@ -17,7 +17,9 @@
load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@protobuf//bazel:proto_library.bzl", "proto_library")
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
package(default_visibility = ["//visibility:public"])
@@ -51,6 +53,29 @@ cc_library(
],
)
cc_test(
name = "rcpsp_parser_test",
size = "small",
srcs = ["rcpsp_parser_test.cc"],
data = [
"//ortools/scheduling/testdata:c1510_1.mm.txt",
"//ortools/scheduling/testdata:j301_1.sm",
"//ortools/scheduling/testdata:mmlib100_j100100_1.mm.txt",
"//ortools/scheduling/testdata:psp1.sch",
"//ortools/scheduling/testdata:psp10_1.sch",
"//ortools/scheduling/testdata:rg300_1.rcp",
"//ortools/scheduling/testdata:rg30_set1_pat1.rcp",
"//ortools/scheduling/testdata:rip1.sch",
"//ortools/scheduling/testdata:ubo_10_psp2.sch",
],
deps = [
":rcpsp_parser",
"//ortools/base:gmock_main",
"//ortools/base:path",
"@abseil-cpp//absl/strings:string_view",
],
)
### Jobshop Scheduling ###
proto_library(
name = "jobshop_scheduling_proto",
@@ -81,6 +106,27 @@ cc_library(
],
)
cc_test(
name = "jobshop_scheduling_parser_test",
size = "small",
srcs = ["jobshop_scheduling_parser_test.cc"],
data = [
"//ortools/scheduling/testdata:02a.fjs",
"//ortools/scheduling/testdata:1010_1_3",
"//ortools/scheduling/testdata:50_10_01_ta041.txt",
"//ortools/scheduling/testdata:SDST10_ta001.txt",
"//ortools/scheduling/testdata:ft06",
"//ortools/scheduling/testdata:jb1.txt",
"//ortools/scheduling/testdata:taillard-jobshop-15_15-1_225_100_150-1",
],
deps = [
":jobshop_scheduling_parser",
"//ortools/base:gmock_main",
"//ortools/base:path",
"@abseil-cpp//absl/strings:string_view",
],
)
### Course Scheduling ###
proto_library(
name = "course_scheduling_proto",
@@ -91,3 +137,47 @@ cc_proto_library(
name = "course_scheduling_cc_proto",
deps = [":course_scheduling_proto"],
)
cc_library(
name = "course_scheduling",
srcs = ["course_scheduling.cc"],
hdrs = ["course_scheduling.h"],
deps = [
":course_scheduling_cc_proto",
"//ortools/base:mathutil",
"//ortools/linear_solver:linear_solver_base",
"//ortools/linear_solver:linear_solver_scip",
"//ortools/sat:cp_model_cc_proto",
"@abseil-cpp//absl/container:flat_hash_set",
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/strings:str_format",
"@abseil-cpp//absl/types:span",
],
)
cc_binary(
name = "course_scheduling_run",
srcs = ["course_scheduling_run.cc"],
deps = [
":course_scheduling",
":course_scheduling_cc_proto",
"//ortools/base",
"//ortools/base:file",
"//ortools/base:timer",
"@abseil-cpp//absl/flags:flag",
"@abseil-cpp//absl/log",
],
)
cc_test(
name = "course_scheduling_test",
srcs = ["course_scheduling_test.cc"],
deps = [
":course_scheduling",
":course_scheduling_cc_proto",
"//ortools/base:gmock_main",
"//ortools/base:mutable_memfile",
"//ortools/base:parse_test_proto",
],
)

View File

@@ -11,15 +11,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
file(GLOB _SRCS "*.h" "*.cc")
list(APPEND _SRCS
course_scheduling.cc
course_scheduling.h
jobshop_scheduling_parser.cc
jobshop_scheduling_parser.h
rcpsp_parser.cc
rcpsp_parser.h
)
set(NAME ${PROJECT_NAME}_scheduling)
# Will be merge in libortools.so
#add_library(${NAME} STATIC ${_SRCS})
add_library(${NAME} OBJECT ${_SRCS})
set_target_properties(${NAME} PROPERTIES
POSITION_INDEPENDENT_CODE ON
)
set_target_properties(${NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(${NAME} PRIVATE
${PROJECT_SOURCE_DIR}
${PROJECT_BINARY_DIR})

View File

@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "examples/cpp/course_scheduling.h"
#include "ortools/scheduling/course_scheduling.h"
#include <algorithm>
#include <cmath>
@@ -21,10 +21,10 @@
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "absl/types/span.h"
#include "ortools/base/logging.h"
#include "ortools/base/mathutil.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/scheduling/course_scheduling.pb.h"

View File

@@ -11,8 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ORTOOLS_EXAMPLES_COURSE_SCHEDULING_H_
#define ORTOOLS_EXAMPLES_COURSE_SCHEDULING_H_
#ifndef ORTOOLS_SCHEDULING_COURSE_SCHEDULING_H_
#define ORTOOLS_SCHEDULING_COURSE_SCHEDULING_H_
#include <utility>
#include <vector>
@@ -83,4 +83,4 @@ class CourseSchedulingSolver {
} // namespace operations_research
#endif // ORTOOLS_EXAMPLES_COURSE_SCHEDULING_H_
#endif // ORTOOLS_SCHEDULING_COURSE_SCHEDULING_H_

View File

@@ -23,11 +23,11 @@
#include "absl/flags/flag.h"
#include "absl/log/log.h"
#include "examples/cpp/course_scheduling.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/options.h"
#include "ortools/base/timer.h"
#include "ortools/scheduling/course_scheduling.h"
#include "ortools/scheduling/course_scheduling.pb.h"
ABSL_FLAG(std::string, input, "",

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,102 @@
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/scheduling/jobshop_scheduling_parser.h"
#include <string>
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
#include "ortools/base/path.h"
namespace operations_research {
namespace scheduling {
namespace jssp {
namespace {
std::string GetPath(absl::string_view filename) {
constexpr absl::string_view kTestDataDir =
"_main/ortools/scheduling/testdata/";
return file::JoinPath(::testing::SrcDir(), kTestDataDir, filename);
}
TEST(RcpspParserTest, Jssp) {
JsspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("ft06")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(6, problem.jobs_size());
EXPECT_EQ(6, problem.machines_size());
}
TEST(RcpspParserTest, Taillard) {
JsspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("50_10_01_ta041.txt")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(50, problem.jobs_size());
EXPECT_EQ(10, problem.machines_size());
}
TEST(RcpspParserTest, Flexible) {
JsspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("02a.fjs")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(10, problem.jobs_size());
EXPECT_EQ(5, problem.machines_size());
}
TEST(RcpspParserTest, Sdst) {
JsspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("SDST10_ta001.txt")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(20, problem.jobs_size());
EXPECT_EQ(5, problem.machines_size());
for (const Machine& m : problem.machines()) {
ASSERT_TRUE(m.has_transition_time_matrix());
EXPECT_EQ(m.transition_time_matrix().transition_time_size(),
problem.jobs_size() * problem.jobs_size());
}
}
TEST(RcpspParserTest, Tardiness) {
JsspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("jb1.txt")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(10, problem.jobs_size());
EXPECT_EQ(5, problem.machines_size());
}
TEST(RcpspParserTest, Pss) {
JsspParser parser;
ASSERT_TRUE(
parser.ParseFile(GetPath("taillard-jobshop-15_15-1_225_100_150-1")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(15, problem.jobs_size());
EXPECT_EQ(15, problem.machines_size());
}
TEST(RcpspParserTest, EarlyTardy) {
JsspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("1010_1_3")));
const JsspInputProblem problem = parser.problem();
EXPECT_EQ(10, problem.jobs_size());
EXPECT_EQ(10, problem.machines_size());
EXPECT_EQ(1033, problem.jobs(0).early_due_date());
EXPECT_EQ(1033, problem.jobs(0).late_due_date());
EXPECT_EQ(3, problem.jobs(0).earliness_cost_per_time_unit());
EXPECT_EQ(10, problem.jobs(0).lateness_cost_per_time_unit());
}
} // namespace
} // namespace jssp
} // namespace scheduling
} // namespace operations_research

View File

@@ -0,0 +1,115 @@
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/scheduling/rcpsp_parser.h"
#include <string>
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
#include "ortools/base/path.h"
namespace operations_research {
namespace scheduling {
namespace rcpsp {
namespace {
std::string GetPath(absl::string_view filename) {
constexpr absl::string_view kTestDataDir =
"_main/ortools/scheduling/testdata/";
return file::JoinPath(::testing::SrcDir(), kTestDataDir, filename);
}
TEST(RcpspParserTest, SingleMode) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("j301_1.sm")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(32, problem.tasks_size());
EXPECT_EQ(4, problem.resources_size());
}
TEST(RcpspParserTest, MultiMode) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("c1510_1.mm.txt")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(18, problem.tasks_size());
EXPECT_EQ(4, problem.resources_size());
}
TEST(RcpspParserTest, MultiModeMax) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("psp1.sch")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(12, problem.tasks_size());
EXPECT_EQ(7, problem.resources_size());
EXPECT_TRUE(problem.is_rcpsp_max());
}
TEST(RcpspParserTest, SingleModeMax) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("ubo_10_psp2.sch")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(12, problem.tasks_size());
EXPECT_EQ(5, problem.resources_size());
EXPECT_TRUE(problem.is_rcpsp_max());
EXPECT_FALSE(problem.is_consumer_producer());
}
TEST(RcpspParserTest, SingleModeMaxReservoir) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("psp10_1.sch")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(12, problem.tasks_size());
EXPECT_EQ(5, problem.resources_size());
EXPECT_TRUE(problem.is_rcpsp_max());
EXPECT_TRUE(problem.is_consumer_producer());
}
TEST(RcpspParserTest, SingleModeInvestment) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("rip1.sch")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(12, problem.tasks_size());
EXPECT_EQ(1, problem.resources_size());
EXPECT_TRUE(problem.is_resource_investment());
EXPECT_EQ(19, problem.deadline());
}
TEST(RcpspParserTest, SingleModePatterson) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("rg30_set1_pat1.rcp")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(32, problem.tasks_size());
EXPECT_EQ(4, problem.resources_size());
}
TEST(RcpspParserTest, SingleModeLargePatterson) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("rg300_1.rcp")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(302, problem.tasks_size());
EXPECT_EQ(4, problem.resources_size());
}
TEST(RcpspParserTest, MultiModeMmLib) {
RcpspParser parser;
ASSERT_TRUE(parser.ParseFile(GetPath("mmlib100_j100100_1.mm.txt")));
const RcpspProblem problem = parser.problem();
EXPECT_EQ(102, problem.tasks_size());
EXPECT_EQ(4, problem.resources_size());
}
} // namespace
} // namespace rcpsp
} // namespace scheduling
} // namespace operations_research