Skip to content

Commit

Permalink
constraint_solver: Export from google3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Jul 10, 2023
1 parent a0a6d34 commit 1dd922c
Show file tree
Hide file tree
Showing 20 changed files with 3,419 additions and 2,732 deletions.
35 changes: 32 additions & 3 deletions ortools/constraint_solver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_python//python:proto.bzl", "py_proto_library")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -301,6 +300,30 @@ cc_library(
],
)

cc_library(
name = "routing_utils",
srcs = ["routing_utils.cc"],
hdrs = ["routing_utils.h"],
visibility = ["//visibility:public"],
deps = [
"//ortools/base",
"//ortools/util:saturated_arithmetic",
],
)

cc_library(
name = "routing_neighborhoods",
srcs = ["routing_neighborhoods.cc"],
hdrs = ["routing_neighborhoods.h"],
visibility = ["//visibility:public"],
deps = [
":cp",
":routing_types",
":routing_utils",
"//ortools/base",
],
)

cc_library(
name = "routing_index_manager",
srcs = ["routing_index_manager.cc"],
Expand All @@ -319,18 +342,22 @@ cc_library(
srcs = [
"routing.cc",
"routing_breaks.cc",
"routing_constraints.cc",
"routing_decision_builders.cc",
"routing_filters.cc",
"routing_flow.cc",
"routing_insertion_lns.cc",
"routing_lp_scheduling.cc",
"routing_neighborhoods.cc",
"routing_sat.cc",
"routing_search.cc",
],
hdrs = [
"routing.h",
"routing_constraints.h",
"routing_decision_builders.h",
"routing_filters.h",
"routing_insertion_lns.h",
"routing_lp_scheduling.h",
"routing_neighborhoods.h",
"routing_search.h",
],
copts = select({
Expand All @@ -343,9 +370,11 @@ cc_library(
":cp",
":routing_enums_cc_proto",
":routing_index_manager",
":routing_neighborhoods",
":routing_parameters",
":routing_parameters_cc_proto",
":routing_types",
":routing_utils",
"//ortools/base",
"//ortools/base:adjustable_priority_queue",
"//ortools/base:dump_vars",
Expand Down
18 changes: 12 additions & 6 deletions ortools/constraint_solver/constraint_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,11 @@ class Solver {
SearchMonitor* MakeSearchLog(int branch_period, IntVar* var,
std::function<std::string()> display_callback);

/// At each solution, this monitor will display the 'vars' values and the
/// result of @p display_callback.
SearchMonitor* MakeSearchLog(int branch_period, std::vector<IntVar*> vars,
std::function<std::string()> display_callback);

/// OptimizeVar Search Logs
/// At each solution, this monitor will also display the 'opt_var' value.
SearchMonitor* MakeSearchLog(int branch_period, OptimizeVar* opt_var);
Expand All @@ -2436,15 +2441,15 @@ class Solver {
/// SearchMonitors will display a periodic search log every branch_period
/// branches explored.
int branch_period = 1;
/// SearchMonitors will display values of objective or variable (both cannot
/// be used together).
/// SearchMonitors will display values of objective or variables (both
/// cannot be used together).
OptimizeVar* objective = nullptr;
IntVar* variable = nullptr;
std::vector<IntVar*> variables;
/// When displayed, objective or var values will be scaled and offset by
/// the given values in the following way:
/// scaling_factor * (value + offset).
double scaling_factor = 1.0;
double offset = 0;
std::vector<double> scaling_factors;
std::vector<double> offsets;
/// SearchMonitors will display the result of display_callback at each new
/// solution found and when the search finishes if
/// display_on_new_solutions_only is false.
Expand Down Expand Up @@ -4425,6 +4430,7 @@ class ObjectiveMonitor : public SearchMonitor {
bool found_initial_solution_;

private:
friend class Solver;
const std::vector<IntVar*> objective_vars_;
std::vector<IntVar*> minimization_vars_;
std::vector<IntVar*> upper_bounds_;
Expand Down Expand Up @@ -4459,7 +4465,7 @@ class OptimizeVar : public ObjectiveMonitor {
void RefuteDecision(Decision* d) override;
bool AtSolution() override;
bool AcceptSolution() override;
virtual std::string Print() const;
virtual std::string Name() const;
std::string DebugString() const override;

void ApplyBound();
Expand Down
32 changes: 21 additions & 11 deletions ortools/constraint_solver/constraint_solveri.h
Original file line number Diff line number Diff line change
Expand Up @@ -1406,10 +1406,11 @@ class PathOperator : public IntVarLocalSearchOperator {
/// Returns the vector of path start nodes.
const std::vector<int64_t>& path_starts() const { return path_starts_; }
/// Returns the class of the path of the ith base node.
int PathClass(int i) const {
int PathClass(int i) const { return PathClassFromStartNode(StartNode(i)); }
int PathClassFromStartNode(int64_t start_node) const {
return iteration_parameters_.start_empty_path_class != nullptr
? iteration_parameters_.start_empty_path_class(StartNode(i))
: StartNode(i);
? iteration_parameters_.start_empty_path_class(start_node)
: start_node;
}

/// When the operator is being synchronized with a new solution (when Start()
Expand Down Expand Up @@ -1463,6 +1464,12 @@ class PathOperator : public IntVarLocalSearchOperator {
return ignore_path_vars_ ? 0LL : OldValue(node + number_of_nexts_);
}

int CurrentNodePathStart(int64_t node) const {
return node_path_starts_[node];
}

int CurrentNodePathEnd(int64_t node) const { return node_path_ends_[node]; }

/// Moves the chain starting after the node before_chain and ending at the
/// node chain_end after the node destination
bool MoveChain(int64_t before_chain, int64_t chain_end, int64_t destination);
Expand Down Expand Up @@ -1609,6 +1616,8 @@ class PathOperator : public IntVarLocalSearchOperator {
std::vector<int> base_sibling_alternatives_;
std::vector<int> end_nodes_;
std::vector<int> base_paths_;
std::vector<int> node_path_starts_;
std::vector<int> node_path_ends_;
std::vector<int> calls_per_base_node_;
std::vector<int64_t> path_starts_;
std::vector<int64_t> path_ends_;
Expand Down Expand Up @@ -2049,8 +2058,9 @@ class SymmetryBreaker : public DecisionVisitor {
/// the search is running.
class SearchLog : public SearchMonitor {
public:
SearchLog(Solver* s, OptimizeVar* obj, IntVar* var, double scaling_factor,
double offset, std::function<std::string()> display_callback,
SearchLog(Solver* solver, std::vector<IntVar*> vars, std::string vars_name,
std::vector<double> scaling_factors, std::vector<double> offsets,
std::function<std::string()> display_callback,
bool display_on_new_solutions_only, int period);
~SearchLog() override;
void EnterSearch() override;
Expand All @@ -2076,16 +2086,16 @@ class SearchLog : public SearchMonitor {

const int period_;
std::unique_ptr<WallTimer> timer_;
IntVar* const var_;
OptimizeVar* const obj_;
const double scaling_factor_;
const double offset_;
const std::vector<IntVar*> vars_;
const std::string vars_name_;
const std::vector<double> scaling_factors_;
const std::vector<double> offsets_;
std::function<std::string()> display_callback_;
const bool display_on_new_solutions_only_;
int nsol_;
int64_t tick_;
int64_t objective_min_;
int64_t objective_max_;
std::vector<int64_t> objective_min_;
std::vector<int64_t> objective_max_;
int min_right_depth_;
int max_depth_;
int sliding_min_depth_;
Expand Down
2 changes: 0 additions & 2 deletions ortools/constraint_solver/java/routing.i
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,9 @@ import java.util.function.LongUnaryOperator;

%rename (registerUnaryTransitVector) RoutingModel::RegisterUnaryTransitVector;
%rename (registerUnaryTransitCallback) RoutingModel::RegisterUnaryTransitCallback;
%rename (registerPositiveUnaryTransitCallback) RoutingModel::RegisterPositiveUnaryTransitCallback; // not tested

%rename (registerTransitMatrix) RoutingModel::RegisterTransitMatrix;
%rename (registerTransitCallback) RoutingModel::RegisterTransitCallback;
%rename (registerPositiveTransitCallback) RoutingModel::RegisterPositiveTransitCallback; // not tested

%rename (restoreAssignment) RoutingModel::RestoreAssignment;
%rename (routesToAssignment) RoutingModel::RoutesToAssignment;
Expand Down
Loading

0 comments on commit 1dd922c

Please sign in to comment.