Skip to content

Commit

Permalink
[CP-SAT] more bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jan 27, 2025
1 parent 3e02d3b commit 5e68e51
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions ortools/sat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3445,6 +3445,7 @@ cc_library(
"//ortools/base:stl_util",
"//ortools/graph:connected_components",
"//ortools/util:adaptative_parameter_value",
"//ortools/util:bitset",
"//ortools/util:integer_pq",
"//ortools/util:saturated_arithmetic",
"//ortools/util:sorted_interval_list",
Expand Down
3 changes: 1 addition & 2 deletions ortools/sat/all_different.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ bool AllDifferentConstraint::Propagate() {
}

AllDifferentBoundsPropagator::AllDifferentBoundsPropagator(
const std::vector<AffineExpression>& expressions,
IntegerTrail* integer_trail)
absl::Span<const AffineExpression> expressions, IntegerTrail* integer_trail)
: integer_trail_(integer_trail) {
CHECK(!expressions.empty());

Expand Down
2 changes: 1 addition & 1 deletion ortools/sat/all_different.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class AllDifferentConstraint : PropagatorInterface {
// https://cs.uwaterloo.ca/~vanbeek/Publications/ijcai03_TR.pdf
class AllDifferentBoundsPropagator : public PropagatorInterface {
public:
AllDifferentBoundsPropagator(const std::vector<AffineExpression>& expressions,
AllDifferentBoundsPropagator(absl::Span<const AffineExpression> expressions,
IntegerTrail* integer_trail);

// This type is neither copyable nor movable.
Expand Down
10 changes: 6 additions & 4 deletions ortools/sat/cp_model_lns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "ortools/sat/synchronization.h"
#include "ortools/sat/util.h"
#include "ortools/util/adaptative_parameter_value.h"
#include "ortools/util/bitset.h"
#include "ortools/util/integer_pq.h"
#include "ortools/util/saturated_arithmetic.h"
#include "ortools/util/sorted_interval_list.h"
Expand Down Expand Up @@ -949,7 +950,8 @@ NeighborhoodGeneratorHelper::GetSchedulingPrecedences(
return result;
}

std::vector<std::vector<int>> NeighborhoodGeneratorHelper::GetRoutingPaths(
std::vector<std::vector<int>>
NeighborhoodGeneratorHelper::GetRoutingPathLiterals(
const CpSolverResponse& initial_solution) const {
struct HeadAndArcLiteral {
int head;
Expand Down Expand Up @@ -2596,7 +2598,7 @@ Neighborhood RoutingRandomNeighborhoodGenerator::Generate(
const CpSolverResponse& initial_solution, SolveData& data,
absl::BitGenRef random) {
const std::vector<std::vector<int>> all_paths =
helper_.GetRoutingPaths(initial_solution);
helper_.GetRoutingPathLiterals(initial_solution);

// Collect all unique variables.
absl::flat_hash_set<int> all_path_variables;
Expand All @@ -2617,7 +2619,7 @@ Neighborhood RoutingPathNeighborhoodGenerator::Generate(
const CpSolverResponse& initial_solution, SolveData& data,
absl::BitGenRef random) {
std::vector<std::vector<int>> all_paths =
helper_.GetRoutingPaths(initial_solution);
helper_.GetRoutingPathLiterals(initial_solution);

// Collect all unique variables.
absl::flat_hash_set<int> all_path_variables;
Expand Down Expand Up @@ -2663,7 +2665,7 @@ Neighborhood RoutingFullPathNeighborhoodGenerator::Generate(
const CpSolverResponse& initial_solution, SolveData& data,
absl::BitGenRef random) {
std::vector<std::vector<int>> all_paths =
helper_.GetRoutingPaths(initial_solution);
helper_.GetRoutingPathLiterals(initial_solution);
// Remove a corner case where all paths are empty.
if (all_paths.empty()) {
return helper_.NoNeighborhood();
Expand Down
4 changes: 3 additions & 1 deletion ortools/sat/cp_model_lns.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define OR_TOOLS_SAT_CP_MODEL_LNS_H_

#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
Expand All @@ -39,6 +40,7 @@
#include "ortools/sat/synchronization.h"
#include "ortools/sat/util.h"
#include "ortools/util/adaptative_parameter_value.h"
#include "ortools/util/bitset.h"

namespace operations_research {
namespace sat {
Expand Down Expand Up @@ -250,7 +252,7 @@ class NeighborhoodGeneratorHelper : public SubSolver {
// self-looping arcs. Path are sorted, starting from the arc with the lowest
// tail index, and going in sequence up to the last arc before the circuit is
// closed. Each entry correspond to the arc literal on the circuit.
std::vector<std::vector<int>> GetRoutingPaths(
std::vector<std::vector<int>> GetRoutingPathLiterals(
const CpSolverResponse& initial_solution) const;

// Returns all precedences extracted from the scheduling constraint and the
Expand Down
4 changes: 4 additions & 0 deletions ortools/sat/cp_model_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,10 @@ void RestrictObjectiveUsingHint(CpModelProto* model_proto) {
bool SolutionHintIsCompleteAndFeasible(
const CpModelProto& model_proto, SolverLogger* logger = nullptr,
SharedResponseManager* manager = nullptr) {
if (!model_proto.has_solution_hint() && model_proto.variables_size() > 0) {
return false;
}

int num_active_variables = 0;
int num_hinted_variables = 0;
for (int var = 0; var < model_proto.variables_size(); ++var) {
Expand Down
3 changes: 2 additions & 1 deletion ortools/sat/linear_relaxation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ void AppendNoOverlap2dRelaxation(const ConstraintProto& ct, Model* model,
const IntegerValue area_min =
integer_trail->LevelZeroLowerBound(x_sizes[i]) *
integer_trail->LevelZeroLowerBound(y_sizes[i]);
if (area_min != 0) {
if (area_min > 0) {
// Note that intervals that must be absent can have negative sizes.
// Not including the term if we don't have a view is ok.
(void)lc.AddLiteralTerm(presence_literal, area_min);
}
Expand Down

0 comments on commit 5e68e51

Please sign in to comment.