Skip to content

Commit

Permalink
[CP-SAT] improve no_overlap_2d cuts; more no_overlap_2d cuts; bugfixe…
Browse files Browse the repository at this point in the history
…s; add sat/c_api subdirectory
  • Loading branch information
lperron committed Feb 10, 2025
1 parent 9f4bf2a commit 7e98e7b
Show file tree
Hide file tree
Showing 31 changed files with 931 additions and 304 deletions.
2 changes: 1 addition & 1 deletion ortools/base/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <cstdlib> // for size_t.

#include "ortools/base/base_export.h" // for OR_DLL
#include "ortools/base/base_export.h" // for OR_DLL

#define COMPILE_ASSERT(x, msg)

Expand Down
68 changes: 10 additions & 58 deletions ortools/graph/shortest_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ class GenericPathContainer {
using NodeIndex = typename GraphType::NodeIndex;
using Impl = internal::PathContainerImpl<NodeIndex, GraphType::kNilNode>;

// TODO(b/385094969): Remove this when all clients are migrated, and use
// factory functions instead.
GenericPathContainer();

// This type is neither copyable nor movable.
GenericPathContainer(const GenericPathContainer&) = delete;
GenericPathContainer& operator=(const GenericPathContainer&) = delete;
Expand Down Expand Up @@ -153,9 +149,6 @@ class GenericPathContainer {
// Builds a path container which only stores distances between path nodes.
static GenericPathContainer BuildPathDistanceContainer();

ABSL_DEPRECATED("Use factory function BuildPathDistanceContainer instead.")
static void BuildPathDistanceContainer(GenericPathContainer* path_container);

// Builds a path container which stores explicit paths and distances between
// path nodes in a memory-compact representation.
// In this case `GetPenultimateNodeInPath()` is `O(log(path_tree_size))`,
Expand All @@ -168,11 +161,6 @@ class GenericPathContainer {
// `O(log(path_tree_size) * path_size)`.
static GenericPathContainer BuildInMemoryCompactPathContainer();

ABSL_DEPRECATED(
"Use factory function BuildInMemoryCompactPathContainer instead.")
static void BuildInMemoryCompactPathContainer(
GenericPathContainer* path_container);

// TODO(user): Add save-to-disk container.
// TODO(user): Add `BuildInMemoryFastPathContainer()`, which does
// `GetPenultimateNodeInPath()` in `O(1)`.
Expand Down Expand Up @@ -230,12 +218,12 @@ void ComputeOneToAllShortestPaths(
// Computes shortest paths from the node `source` to nodes in `destinations`.
// TODO(b/385094969): Remove second template parameter when all clients are
// migrated.
template <class GraphType, class PathContainerGraphType>
template <class GraphType>
void ComputeOneToManyShortestPaths(
const GraphType& graph, const std::vector<PathDistance>& arc_lengths,
typename GraphType::NodeIndex source,
const std::vector<typename GraphType::NodeIndex>& destinations,
GenericPathContainer<PathContainerGraphType>* const path_container) {
GenericPathContainer<GraphType>* const path_container) {
std::vector<typename GraphType::NodeIndex> sources(1, source);
ComputeManyToManyShortestPathsWithMultipleThreads(
graph, arc_lengths, sources, destinations, 1, path_container);
Expand Down Expand Up @@ -282,13 +270,10 @@ void ComputeManyToAllShortestPathsWithMultipleThreads(
}

// Computes shortest paths between all nodes of the graph.
// TODO(b/385094969): Remove second template parameter when all clients are
// migrated.
template <class GraphType, class PathContainerGraphType>
template <class GraphType>
void ComputeAllToAllShortestPathsWithMultipleThreads(
const GraphType& graph, const std::vector<PathDistance>& arc_lengths,
int num_threads,
GenericPathContainer<PathContainerGraphType>* const path_container) {
int num_threads, GenericPathContainer<GraphType>* const path_container) {
std::vector<typename GraphType::NodeIndex> all_nodes;
GetGraphNodesFromGraph<GraphType>(graph, &all_nodes);
ComputeManyToManyShortestPathsWithMultipleThreads(
Expand Down Expand Up @@ -635,15 +620,13 @@ bool InsertOrUpdateEntry(
// using a binary heap-based Dijkstra algorithm.
// TODO(user): Investigate alternate implementation which wouldn't use
// AdjustablePriorityQueue.
// TODO(b/385094969): Remove second template parameter when all clients are
// migrated.
template <class GraphType, class PathContainerGraphType>
template <class GraphType>
void ComputeOneToManyOnGraph(
const GraphType* const graph,
const std::vector<PathDistance>* const arc_lengths,
typename GraphType::NodeIndex source,
const std::vector<typename GraphType::NodeIndex>* const destinations,
typename GenericPathContainer<PathContainerGraphType>::Impl* const paths) {
typename GenericPathContainer<GraphType>::Impl* const paths) {
using NodeIndex = typename GraphType::NodeIndex;
using ArcIndex = typename GraphType::ArcIndex;
using NodeEntryT = NodeEntry<NodeIndex, GraphType::kNilNode>;
Expand Down Expand Up @@ -715,9 +698,6 @@ void ComputeOneToManyOnGraph(

} // namespace internal

template <class GraphType>
GenericPathContainer<GraphType>::GenericPathContainer() = default;

template <class GraphType>
GenericPathContainer<GraphType>::~GenericPathContainer() = default;

Expand All @@ -744,22 +724,6 @@ void GenericPathContainer<GraphType>::GetPath(
container_->GetPath(from, to, path);
}

template <class GraphType>
void GenericPathContainer<GraphType>::BuildPathDistanceContainer(
GenericPathContainer* const path_container) {
CHECK(path_container != nullptr);
path_container->container_ = std::make_unique<
internal::DistanceContainer<NodeIndex, GraphType::kNilNode>>();
}

template <class GraphType>
void GenericPathContainer<GraphType>::BuildInMemoryCompactPathContainer(
GenericPathContainer* const path_container) {
CHECK(path_container != nullptr);
path_container->container_ = std::make_unique<
internal::InMemoryCompactPathContainer<NodeIndex, GraphType::kNilNode>>();
}

template <class GraphType>
GenericPathContainer<GraphType>
GenericPathContainer<GraphType>::BuildPathDistanceContainer() {
Expand All @@ -776,22 +740,12 @@ GenericPathContainer<GraphType>::BuildInMemoryCompactPathContainer() {
NodeIndex, GraphType::kNilNode>>());
}

// TODO(b/385094969): Remove second template parameter when all clients are
// migrated.
template <class GraphType, class PathContainerGraphType>
template <class GraphType>
void ComputeManyToManyShortestPathsWithMultipleThreads(
const GraphType& graph, const std::vector<PathDistance>& arc_lengths,
const std::vector<typename GraphType::NodeIndex>& sources,
const std::vector<typename GraphType::NodeIndex>& destinations,
int num_threads,
GenericPathContainer<PathContainerGraphType>* const paths) {
static_assert(std::is_same_v<typename GraphType::NodeIndex,
typename PathContainerGraphType::NodeIndex>,
"use an explicit `GenericPathContainer<T>` instead of using "
"`PathContainer`");
static_assert(GraphType::kNilNode == PathContainerGraphType::kNilNode,
"use an explicit `GenericPathContainer<T>` instead of using "
"`PathContainer`");
int num_threads, GenericPathContainer<GraphType>* const paths) {
if (graph.num_nodes() > 0) {
CHECK_EQ(graph.num_arcs(), arc_lengths.size())
<< "Number of arcs in graph must match arc length vector size";
Expand All @@ -812,10 +766,8 @@ void ComputeManyToManyShortestPathsWithMultipleThreads(
pool->StartWorkers();
for (int i = 0; i < unique_sources.size(); ++i) {
pool->Schedule(absl::bind_front(
&internal::ComputeOneToManyOnGraph<GraphType,
PathContainerGraphType>,
&graph, &arc_lengths, unique_sources[i], &unique_destinations,
container));
&internal::ComputeOneToManyOnGraph<GraphType>, &graph, &arc_lengths,
unique_sources[i], &unique_destinations, container));
}
}
container->Finalize();
Expand Down
2 changes: 1 addition & 1 deletion ortools/init/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ package(default_visibility = ["//visibility:public"])

cc_library(
name = "init",
hdrs = ["init.h"],
srcs = ["init.cc"],
hdrs = ["init.h"],
deps = [
"//ortools/base",
"//ortools/gurobi:environment",
Expand Down
33 changes: 16 additions & 17 deletions ortools/init/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@
#include "ortools/sat/cp_model_solver_helpers.h"

namespace operations_research {
void CppBridge::InitLogging(const std::string& usage) {
absl::SetProgramUsageMessage(usage);
absl::InitializeLog();
}
void CppBridge::InitLogging(const std::string& usage) {
absl::SetProgramUsageMessage(usage);
absl::InitializeLog();
}

void CppBridge::SetFlags(const CppFlags& flags) {
absl::SetFlag(&FLAGS_stderrthreshold, flags.stderrthreshold);
absl::EnableLogPrefix(flags.log_prefix);
if (!flags.cp_model_dump_prefix.empty()) {
absl::SetFlag(&FLAGS_cp_model_dump_prefix, flags.cp_model_dump_prefix);
}
absl::SetFlag(&FLAGS_cp_model_dump_models, flags.cp_model_dump_models);
absl::SetFlag(&FLAGS_cp_model_dump_submodels,
flags.cp_model_dump_submodels);
absl::SetFlag(&FLAGS_cp_model_dump_response, flags.cp_model_dump_response);
void CppBridge::SetFlags(const CppFlags& flags) {
absl::SetFlag(&FLAGS_stderrthreshold, flags.stderrthreshold);
absl::EnableLogPrefix(flags.log_prefix);
if (!flags.cp_model_dump_prefix.empty()) {
absl::SetFlag(&FLAGS_cp_model_dump_prefix, flags.cp_model_dump_prefix);
}
absl::SetFlag(&FLAGS_cp_model_dump_models, flags.cp_model_dump_models);
absl::SetFlag(&FLAGS_cp_model_dump_submodels, flags.cp_model_dump_submodels);
absl::SetFlag(&FLAGS_cp_model_dump_response, flags.cp_model_dump_response);
}

bool CppBridge::LoadGurobiSharedLibrary(const std::string& full_library_path) {
return LoadGurobiDynamicLibrary({full_library_path}).ok();
}
bool CppBridge::LoadGurobiSharedLibrary(const std::string& full_library_path) {
return LoadGurobiDynamicLibrary({full_library_path}).ok();
}

} // namespace operations_research
22 changes: 11 additions & 11 deletions ortools/linear_solver/model_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class MPModelProtoExporter {
// into two constraints, one for the left hand side (_lhs) and one for right
// hand side (_rhs).
bool AppendConstraint(const MPConstraintProto& ct_proto,
const std::string& name, LineBreaker& line_breaker,
absl::string_view name, LineBreaker& line_breaker,
std::vector<bool>& show_variable, std::string* output);

// Clears "output" and writes a term to it, in "LP" format. Returns false on
Expand All @@ -159,8 +159,8 @@ class MPModelProtoExporter {

// Same as AppendMpsLineHeader. Appends an extra new-line at the end the
// string pointed to by output.
void AppendMpsLineHeaderWithNewLine(const std::string& id,
const std::string& name,
void AppendMpsLineHeaderWithNewLine(absl::string_view id,
absl::string_view name,
std::string* output) const;

// Appends an MPS term in various contexts. The term consists of a head name,
Expand All @@ -186,7 +186,7 @@ class MPModelProtoExporter {
// Appends a line describing the bound of a variablenew-line if two columns
// are already present on the MPS line.
// Used by and in complement to AppendMpsTermWithContext.
void AppendMpsBound(const std::string& bound_type, const std::string& name,
void AppendMpsBound(absl::string_view bound_type, absl::string_view name,
double value, std::string* output) const;

const MPModelProto& proto_;
Expand Down Expand Up @@ -392,7 +392,7 @@ std::string DoubleToString(double d) { return absl::StrCat((d)); }
} // namespace

bool MPModelProtoExporter::AppendConstraint(const MPConstraintProto& ct_proto,
const std::string& name,
absl::string_view name,
LineBreaker& line_breaker,
std::vector<bool>& show_variable,
std::string* output) {
Expand All @@ -414,7 +414,7 @@ bool MPModelProtoExporter::AppendConstraint(const MPConstraintProto& ct_proto,
absl::StrAppend(output, " ", name, ": ", line_breaker.GetOutput());
} else {
if (ub != +kInfinity) {
std::string rhs_name = name;
std::string rhs_name(name);
if (lb != -kInfinity) {
absl::StrAppend(&rhs_name, "_rhs");
}
Expand All @@ -427,7 +427,7 @@ bool MPModelProtoExporter::AppendConstraint(const MPConstraintProto& ct_proto,
absl::StrAppend(output, relation);
}
if (lb != -kInfinity) {
std::string lhs_name = name;
std::string lhs_name(name);
if (ub != +kInfinity) {
absl::StrAppend(&lhs_name, "_lhs");
}
Expand Down Expand Up @@ -462,7 +462,7 @@ bool IsBoolean(const MPVariableProto& var) {
floor(var.upper_bound()) == 1.0;
}

void UpdateMaxSize(const std::string& new_string, int* size) {
void UpdateMaxSize(absl::string_view new_string, int* size) {
const int new_size = new_string.size();
if (new_size > *size) *size = new_size;
}
Expand Down Expand Up @@ -687,7 +687,7 @@ void MPModelProtoExporter::AppendMpsLineHeader(absl::string_view id,
}

void MPModelProtoExporter::AppendMpsLineHeaderWithNewLine(
const std::string& id, const std::string& name, std::string* output) const {
absl::string_view id, absl::string_view name, std::string* output) const {
AppendMpsLineHeader(id, name, output);
absl::StripTrailingAsciiWhitespace(output);
absl::StrAppend(output, "\n");
Expand All @@ -704,8 +704,8 @@ void MPModelProtoExporter::AppendMpsTermWithContext(absl::string_view head_name,
AppendNewLineIfTwoColumns(output);
}

void MPModelProtoExporter::AppendMpsBound(const std::string& bound_type,
const std::string& name, double value,
void MPModelProtoExporter::AppendMpsBound(absl::string_view bound_type,
absl::string_view name, double value,
std::string* output) const {
AppendMpsLineHeader(bound_type, "BOUND", output);
AppendMpsPair(name, value, output);
Expand Down
2 changes: 1 addition & 1 deletion ortools/pdlp/primal_dual_hybrid_gradient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ std::optional<TerminationReason> PreprocessSolver::ApplyPresolveIfEnabled(
// set it for completeness.
presolved_qp->objective_scaling_factor = glop_lp.objective_scaling_factor();
sharded_qp_ = ShardedQuadraticProgram(std::move(*presolved_qp), num_threads_,
num_shards_);
num_shards_, params.scheduler_type());
// A status of `INIT` means the preprocessor created a (usually) smaller
// problem that needs solving. Other statuses mean the preprocessor solved
// the problem completely.
Expand Down
Loading

0 comments on commit 7e98e7b

Please sign in to comment.