Skip to content

Commit

Permalink
big graph cleaning; rewrite CP-SAT python layer; rewrite model_builde…
Browse files Browse the repository at this point in the history
…r python layer; reorganize CP-SAT scheduling and packing code
  • Loading branch information
lperron committed Jan 15, 2025
1 parent 036a0a0 commit 54b8c24
Show file tree
Hide file tree
Showing 150 changed files with 9,294 additions and 9,208 deletions.
8 changes: 4 additions & 4 deletions cmake/python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ if(BUILD_MATH_OPT)
endif()
file(COPY
ortools/sat/python/cp_model.py
ortools/sat/python/cp_model_helper.py
ortools/sat/python/cp_model_numbers.py
DESTINATION ${PYTHON_PROJECT_DIR}/sat/python)
file(COPY
ortools/sat/colab/flags.py
Expand Down Expand Up @@ -637,7 +637,7 @@ add_custom_command(
$<IF:$<TARGET_EXISTS:pdlp_pybind11>,copy,true>
$<$<TARGET_EXISTS:pdlp_pybind11>:$<TARGET_FILE:pdlp_pybind11>> ${PYTHON_PROJECT}/pdlp/python
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:swig_helper_pybind11> ${PYTHON_PROJECT}/sat/python
$<TARGET_FILE:cp_model_helper_pybind11> ${PYTHON_PROJECT}/sat/python
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:rcpsp_pybind11> ${PYTHON_PROJECT}/scheduling/python
COMMAND ${CMAKE_COMMAND} -E copy
Expand All @@ -657,7 +657,7 @@ add_custom_command(
model_builder_helper_pybind11
math_opt_pybind11
$<TARGET_NAME_IF_EXISTS:pdlp_pybind11>
swig_helper_pybind11
cp_model_helper_pybind11
rcpsp_pybind11
sorted_interval_list_pybind11
WORKING_DIRECTORY python
Expand Down Expand Up @@ -694,7 +694,7 @@ add_custom_command(
COMMAND ${stubgen_EXECUTABLE} -p pybind11_abseil.status --output .
COMMAND ${stubgen_EXECUTABLE} -p ortools.math_opt.core.python.solver --output .
COMMAND ${stubgen_EXECUTABLE} -p ortools.pdlp.python.pdlp --output .
COMMAND ${stubgen_EXECUTABLE} -p ortools.sat.python.swig_helper --output .
COMMAND ${stubgen_EXECUTABLE} -p ortools.sat.python.cp_model_helper --output .
COMMAND ${stubgen_EXECUTABLE} -p ortools.scheduling.python.rcpsp --output .
COMMAND ${stubgen_EXECUTABLE} -p ortools.util.python.sorted_interval_list --output .
COMMAND ${CMAKE_COMMAND} -E touch ${PROJECT_BINARY_DIR}/python/stub_timestamp
Expand Down
2 changes: 1 addition & 1 deletion cmake/samples/python/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# from ortools.graph.python import min_cost_flow
from ortools.linear_solver import pywraplp
# from ortools.linear_solver import linear_solver_pb2
# from ortools.sat.python import swig_helper
# from ortools.sat.python import cp_model_helper
# from ortools.sat.python import cp_model
# from ortools.scheduling import rcpsp
# from ortools.util.python import sorted_interval_list
Expand Down
5 changes: 1 addition & 4 deletions examples/cpp/dimacs_assignment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ CostValue BuildAndSolveHungarianInstance(

template <typename GraphType>
void DisplayAssignment(const LinearSumAssignment<GraphType>& assignment) {
for (typename LinearSumAssignment<GraphType>::BipartiteLeftNodeIterator
node_it(assignment);
node_it.Ok(); node_it.Next()) {
const NodeIndex left_node = node_it.Index();
for (const auto left_node : assignment.BipartiteLeftNodes()) {
const ArcIndex matching_arc = assignment.GetAssignmentArc(left_node);
const NodeIndex right_node = assignment.Head(matching_arc);
VLOG(5) << "assigned (" << left_node << ", " << right_node
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/flow_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void MinCostFlowOn4x4Matrix() {
min_cost_flow.SetNodeSupply(kNumSources + target, -1);
}
CHECK(min_cost_flow.Solve());
CHECK_EQ(MinCostFlow::OPTIMAL, min_cost_flow.status());
CHECK_EQ(GenericMinCostFlow<Graph>::OPTIMAL, min_cost_flow.status());
CostValue total_flow_cost = min_cost_flow.GetOptimalCost();
CHECK_EQ(kExpectedCost, total_flow_cost);
}
Expand Down
13 changes: 7 additions & 6 deletions examples/cpp/min_cost_flow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@

namespace operations_research {
struct Arc {
std::pair<NodeIndex, NodeIndex> nodes;
FlowQuantity capacity;
FlowQuantity unit_cost;
std::pair<SimpleMinCostFlow::NodeIndex, SimpleMinCostFlow::NodeIndex> nodes;
SimpleMinCostFlow::FlowQuantity capacity;
SimpleMinCostFlow::FlowQuantity unit_cost;
};

void SolveMinCostFlow() {
// Define supply of each node.
const std::vector<std::pair<NodeIndex, FlowQuantity> > supplies = {
{0, 20}, {1, 0}, {2, 0}, {3, -5}, {4, -15}};
const std::vector<
std::pair<SimpleMinCostFlow::NodeIndex, SimpleMinCostFlow::FlowQuantity> >
supplies = {{0, 20}, {1, 0}, {2, 0}, {3, -5}, {4, -15}};

// Define each arc
// Can't use std::tuple<NodeIndex, NodeIndex, FlowQuantity>
Expand Down Expand Up @@ -58,7 +59,7 @@ void SolveMinCostFlow() {
if (status != SimpleMinCostFlow::OPTIMAL) {
LOG(FATAL) << "Solving the max flow is not optimal!";
}
FlowQuantity total_flow_cost = min_cost_flow.OptimalCost();
SimpleMinCostFlow::FlowQuantity total_flow_cost = min_cost_flow.OptimalCost();
LOG(INFO) << "Minimum cost flow: " << total_flow_cost;
LOG(INFO) << "";
LOG(INFO) << "Arc : Flow / Capacity / Cost";
Expand Down
7 changes: 3 additions & 4 deletions examples/cpp/print_dimacs_assignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ void PrintDimacsAssignmentProblem(
absl::StrFormat("p asn %d %d\n", graph.num_nodes(), graph.num_arcs());
CHECK_OK(file::WriteString(output, output_line, file::Defaults()));

for (typename LinearSumAssignment<GraphType>::BipartiteLeftNodeIterator
node_it(assignment);
node_it.Ok(); node_it.Next()) {
output_line = absl::StrFormat("n %d\n", node_it.Index() + 1);
for (const typename GraphType::NodeIndex left_node :
assignment.BipartiteLeftNodes()) {
output_line = absl::StrFormat("n %d\n", left_node + 1);
CHECK_OK(file::WriteString(output, output_line, file::Defaults()));
}

Expand Down
38 changes: 38 additions & 0 deletions ortools/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ cc_library(
],
)

cc_library(
name = "fuzztest",
testonly = 1,
hdrs = ["fuzztest.h"],
deps = [
"@com_google_absl//absl/log:check",
"@com_google_fuzztest//fuzztest",
"@com_google_fuzztest//fuzztest:googletest_fixture_adapter",
"@com_google_fuzztest//fuzztest:init_fuzztest",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "status_matchers",
hdrs = ["status_matchers.h"],
Expand Down Expand Up @@ -309,6 +322,31 @@ cc_library(
deps = [":base"],
)

cc_library(
name = "constant_divisor",
srcs = ["constant_divisor.cc"],
hdrs = ["constant_divisor.h"],
visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/numeric:int128",
],
)

cc_test(
name = "constant_divisor_test",
srcs = ["constant_divisor_test.cc"],
deps = [
":constant_divisor",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/random",
"@com_google_absl//absl/random:bit_gen_ref",
"@com_google_absl//absl/random:distributions",
"@com_google_benchmark//:benchmark",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "linked_hash_map",
hdrs = ["linked_hash_map.h"],
Expand Down
56 changes: 56 additions & 0 deletions ortools/base/constant_divisor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/base/constant_divisor.h"

#include <cstdint>
#include <limits>

#include "absl/log/check.h"
#include "absl/numeric/int128.h"

namespace util {
namespace math {

// Fast div/mod implementation based on
// "Faster Remainder by Direct Computation: Applications to Compilers and
// Software Libraries" Daniel Lemire, Owen Kaser, Nathan Kurz arXiv:1902.01961
ConstantDivisor<uint64_t>::ConstantDivisor(value_type d)
: ConstantDivisorBase((absl::Uint128Max() / d) + 1, d) {
CHECK_GT(d, 1) << "ConstantDivisor<uint64_t> only supports denominators > 1.";
}

// If we hardcode shift_amount to 32, the 32-bit formula is:
// magic_number = 2 ^ 64 / d
// value / d = value * magic_number >> 64
//
// One caveat is that for d == 1, magic_number takes 65 bits overflowing a
// uint64_t. So, we again disallow inputs with d == 1.
ConstantDivisor<uint32_t>::ConstantDivisor(value_type d)
: ConstantDivisorBase((std::numeric_limits<MagicValueType>::max() / d) + 1,
d) {
CHECK_GT(d, 1) << "ConstantDivisor<uint32_t> only supports denominators > 1.";
}

ConstantDivisor<uint16_t>::ConstantDivisor(value_type d)
: ConstantDivisorBase((MagicValueType{1} << kShift) / d + 1, d) {
CHECK_GT(d, 0);
}

ConstantDivisor<uint8_t>::ConstantDivisor(value_type d)
: ConstantDivisorBase((MagicValueType{1} << kShift) / d + 1, d) {
CHECK_GT(d, 0);
}

} // namespace math
} // namespace util
Loading

0 comments on commit 54b8c24

Please sign in to comment.