Skip to content

Commit

Permalink
Merge branch 'main' of github.com:google/or-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 31, 2023
2 parents fd55458 + 237a6f3 commit a4debe4
Show file tree
Hide file tree
Showing 25 changed files with 756 additions and 675 deletions.
32 changes: 18 additions & 14 deletions ortools/constraint_solver/constraint_solveri.h
Original file line number Diff line number Diff line change
Expand Up @@ -1532,14 +1532,14 @@ class PathOperator : public IntVarLocalSearchOperator {
}
#ifndef SWIG
/// Adds all sets of node alternatives of a vector of alternative pairs. No
/// node can be in two altrnatives.
/// node can be in two alternatives.
template <typename PairType>
void AddPairAlternativeSets(
const std::vector<std::pair<std::vector<int64_t>, std::vector<int64_t>>>&
pair_alternative_sets) {
for (const auto& pair_alternative_set : pair_alternative_sets) {
const int alternative = AddAlternativeSet(pair_alternative_set.first);
sibling_alternative_.back() = alternative + 1;
AddAlternativeSet(pair_alternative_set.second);
const std::vector<PairType>& pair_alternative_sets) {
for (const auto& [alternative_set, sibling_alternative_set] :
pair_alternative_sets) {
sibling_alternative_.back() = AddAlternativeSet(alternative_set) + 1;
AddAlternativeSet(sibling_alternative_set);
}
}
#endif // SWIG
Expand Down Expand Up @@ -1678,18 +1678,21 @@ LocalSearchOperator* MakeLocalSearchOperatorWithNeighbors(
// in that case, the tightening function will return false, and the state will
// be marked as invalid. No other operations than Revert() can be called on an
// invalid state: in particular, an invalid state cannot be saved.
class LocalSearchVariable;

class LocalSearchState {
public:
LocalSearchVariable AddVariable(int64_t initial_min, int64_t initial_max);
class Variable;
// Adds a variable to this state, return a handler to the new variable.
int AddVariable(int64_t initial_min, int64_t initial_max);
void ChangeInitialVariableBounds(int variable_index, int64_t min,
int64_t max);
// Makes an object with restricted operations on the variable identified by
// variable_index: only Relax, Tighten and read operations are available.
Variable MakeVariable(int variable_index);
void Commit();
void Revert();
bool StateIsValid() const { return state_is_valid_; }

private:
friend class LocalSearchVariable;

struct Bounds {
int64_t min;
int64_t max;
Expand All @@ -1701,6 +1704,7 @@ class LocalSearchState {
int64_t VariableMin(int variable_index) const;
int64_t VariableMax(int variable_index) const;

// TODO(user): turn these into strong vectors.
std::vector<Bounds> initial_variable_bounds_;
std::vector<Bounds> variable_bounds_;
std::vector<std::pair<Bounds, int>> saved_variable_bounds_trail_;
Expand All @@ -1713,7 +1717,7 @@ class LocalSearchState {
// LocalSearchState pointers is too expensive, we could switch to index only,
// and the user would have to know the relevant state. The present setup allows
// to ensure that variable users will not misuse the state.
class LocalSearchVariable {
class LocalSearchState::Variable {
public:
int64_t Min() const { return state_->VariableMin(variable_index_); }
int64_t Max() const { return state_->VariableMax(variable_index_); }
Expand All @@ -1729,7 +1733,7 @@ class LocalSearchVariable {
// Only LocalSearchState can construct LocalSearchVariables.
friend class LocalSearchState;

LocalSearchVariable(LocalSearchState* state, int variable_index)
Variable(LocalSearchState* state, int variable_index)
: state_(state), variable_index_(variable_index) {}

LocalSearchState* const state_;
Expand Down
16 changes: 13 additions & 3 deletions ortools/constraint_solver/local_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3839,15 +3839,16 @@ IntVarLocalSearchFilter* Solver::MakeSumObjectiveFilter(
}
}

LocalSearchVariable LocalSearchState::AddVariable(int64_t initial_min,
int64_t initial_max) {
int LocalSearchState::AddVariable(int64_t initial_min, int64_t initial_max) {
DCHECK(state_is_valid_);
DCHECK_LE(initial_min, initial_max);
initial_variable_bounds_.push_back({initial_min, initial_max});
variable_bounds_.push_back({initial_min, initial_max});
variable_is_relaxed_.push_back(false);
return variable_bounds_.size() - 1;
}

const int variable_index = variable_bounds_.size() - 1;
LocalSearchState::Variable LocalSearchState::MakeVariable(int variable_index) {
return {this, variable_index};
}

Expand Down Expand Up @@ -3900,6 +3901,15 @@ bool LocalSearchState::TightenVariableMax(int variable_index,
return state_is_valid_;
}

void LocalSearchState::ChangeInitialVariableBounds(int variable_index,
int64_t min, int64_t max) {
DCHECK(state_is_valid_);
DCHECK_GE(variable_index, 0);
DCHECK_LT(variable_index, variable_bounds_.size());
DCHECK(!variable_is_relaxed_[variable_index]);
initial_variable_bounds_[variable_index] = {min, max};
}

// TODO(user): When the class has more users, find a threshold ratio of
// saved/total variables under which a sparse clear would be more efficient
// for both Commit() and Revert().
Expand Down
Loading

0 comments on commit a4debe4

Please sign in to comment.