Files
ortools-clone/ortools/sat/2d_distances_propagator.h

104 lines
3.5 KiB
C
Raw Permalink Normal View History

// 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.
2025-11-05 11:34:49 +01:00
#ifndef ORTOOLS_SAT_2D_DISTANCES_PROPAGATOR_H_
#define ORTOOLS_SAT_2D_DISTANCES_PROPAGATOR_H_
#include <cstdint>
#include <utility>
2025-07-21 17:25:33 +02:00
#include <variant>
#include <vector>
2025-07-21 17:25:33 +02:00
#include "absl/container/fixed_array.h"
#include "absl/container/flat_hash_map.h"
#include "ortools/sat/integer.h"
2025-07-21 17:25:33 +02:00
#include "ortools/sat/integer_base.h"
#include "ortools/sat/model.h"
#include "ortools/sat/no_overlap_2d_helper.h"
#include "ortools/sat/precedences.h"
2025-07-21 17:25:33 +02:00
#include "ortools/sat/sat_base.h"
#include "ortools/sat/synchronization.h"
namespace operations_research {
namespace sat {
// This class implements a propagator for non_overlap_2d constraints that uses
2025-07-21 17:25:33 +02:00
// the Linear2Bounds to detect precedences between pairs of boxes and
// detect a conflict if the precedences implies an overlap between the two
2025-07-21 17:25:33 +02:00
// boxes. For doing this efficiently, it keeps track of pairs of boxes that have
// non-fixed precedences in the Linear2Bounds and only check those in the
// propagation.
class Precedences2DPropagator : public PropagatorInterface {
public:
Precedences2DPropagator(NoOverlap2DConstraintHelper* helper, Model* model);
~Precedences2DPropagator() override;
bool Propagate() final;
int RegisterWith(GenericLiteralWatcher* watcher);
private:
2025-07-21 17:25:33 +02:00
void CollectNewPairsOfBoxesWithNonTrivialDistance();
void UpdateVarLookups();
IntegerValue UpperBound(
std::variant<LinearExpression2, LinearExpression2Index> linear2) const;
void AddOrUpdateDataForPairOfBoxes(int box1, int box2);
2025-07-21 17:25:33 +02:00
struct PairData {
// The condition must be true if ub(linear2) < ub.
struct Condition {
// If the expression is in the Linear2Indices it is represented by its
// index, otherwise it is represented by the expression itself.
std::variant<LinearExpression2, LinearExpression2Index> linear2;
IntegerValue ub;
};
absl::FixedArray<Literal, 4> pair_presence_literals;
int box1;
int box2;
// start1_before_end2[0==x, 1==y][0=start_1_end_2, 1=start_2_end_1]
Condition start_before_end[2][2];
};
absl::flat_hash_map<std::pair<int, int>, int> non_trivial_pairs_index_;
std::vector<PairData> pair_data_;
struct VarUsage {
// boxes[0=x, 1=y][0=start, 1=end]
std::vector<int> boxes[2][2];
};
absl::flat_hash_map<IntegerVariable, VarUsage> var_to_box_and_coeffs_;
NoOverlap2DConstraintHelper& helper_;
2025-07-21 17:25:33 +02:00
Linear2Bounds* linear2_bounds_;
Linear2Watcher* linear2_watcher_;
SharedStatistics* shared_stats_;
2025-07-21 17:25:33 +02:00
Linear2Indices* lin2_indices_;
Trail* trail_;
IntegerTrail* integer_trail_;
int last_helper_inprocessing_count_ = -1;
2025-07-21 17:25:33 +02:00
int num_known_linear2_ = 0;
int64_t last_linear2_timestamp_ = -1;
int64_t num_conflicts_ = 0;
int64_t num_calls_ = 0;
Precedences2DPropagator(const Precedences2DPropagator&) = delete;
Precedences2DPropagator& operator=(const Precedences2DPropagator&) = delete;
};
} // namespace sat
} // namespace operations_research
2025-11-05 11:34:49 +01:00
#endif // ORTOOLS_SAT_2D_DISTANCES_PROPAGATOR_H_