Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
symmetry.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_SAT_SYMMETRY_H_
15#define OR_TOOLS_SAT_SYMMETRY_H_
16
17#include <memory>
18#include <vector>
19
20#include "absl/types/span.h"
24#include "ortools/util/stats.h"
25
26namespace operations_research {
27namespace sat {
28
29// This class implements more or less the strategy described in the paper:
30// Devriendt J., Bogaerts B., De Cat B., Denecker M., Mears C. "Symmetry
31// propagation: Improved Dynamic Symmetry Breaking in SAT", 2012,
32// IEEE 24th International Conference on Tools with Artificial Intelligence.
33//
34// Basically, each time a literal is propagated, this class tries to detect
35// if another literal could also be propagated by symmetry. Note that this uses
36// a heuristic in order to be efficient and that it is not exhaustive in the
37// sense that it doesn't detect all possible propagations.
38//
39// Algorithm details:
40//
41// Given the current solver trail (i.e. the assigned literals and their
42// assignment order) the idea is to compute (as efficiently as possible) for
43// each permutation added to this class what is called the first (under the
44// trail assignment order) non-symmetric literal. A literal 'l' is said to be
45// non-symmetric under a given assignment and for a given permutation 'p' if
46// 'l' is assigned to true but not 'p(l)'.
47//
48// If a first non-symmetric literal 'l' for a permutation 'p' is not a decision,
49// then:
50// - Because it is not a decision, 'l' has been implied by a reason formed by
51// literals assigned to true at lower trail indices.
52// - Because this is the first non-symmetric literal for 'p', the permuted
53// reason only contains literal that are also assigned to true.
54// - Because of this, 'p(l)' is also implied by the current assignment.
55// Of course, this assume that p is a symmetry of the full problem.
56// Note that if it is already assigned to false, then we have a conflict.
57//
58// TODO(user): Implement the optimizations mentioned in the paper?
59// TODO(user): Instrument and see if the code can be optimized.
61 public:
63
64 // This type is neither copyable nor movable.
67
68 ~SymmetryPropagator() override;
69
70 bool Propagate(Trail* trail) final;
71 void Untrail(const Trail& trail, int trail_index) final;
72 absl::Span<const Literal> Reason(const Trail& trail, int trail_index,
73 int64_t conflict_id) const final;
74
75 // Adds a new permutation to this symmetry propagator. The ownership is
76 // transferred. This must be an integer permutation such that:
77 // - Its domain is [0, 2 * num_variables) and corresponds to the index
78 // representation of the literals over num_variables variables.
79 // - It must be compatible with the negation, for any literal l; not(p(l))
80 // must be the same as p(not(l)), where p(x) represents the image of x by
81 // the permutation.
82 //
83 // Remark: Any permutation which is a symmetry of the main SAT problem can be
84 // added here. However, since the number of permutations is usually not
85 // manageable, a good alternative is to only add the generators of the
86 // permutation group. It is also important to add permutations with a support
87 // as small as possible.
88 //
89 // TODO(user): Currently this can only be called before PropagateNext() is
90 // called (DCHECKed). Not sure if we need more incrementality though.
91 void AddSymmetry(std::unique_ptr<SparsePermutation> permutation);
92 int num_permutations() const { return permutations_.size(); }
93
94 // Visible for testing.
95 //
96 // Permutes a list of literals from input into output using the permutation
97 // with given index. This uses tmp_literal_mapping_ and has a complexity in
98 // O(permutation_support + input_size).
99 void Permute(int index, absl::Span<const Literal> input,
100 std::vector<Literal>* output) const;
101
102 private:
103 // Propagates the literal at propagation_trail_index_ from the trail.
104 bool PropagateNext(Trail* trail);
105
106 // The permutations.
107 // The index of a permutation is its position in this vector.
108 std::vector<std::unique_ptr<SparsePermutation>> permutations_;
109
110 // Reverse mapping (source literal) -> list of (permutation_index, image).
111 struct ImageInfo {
112 ImageInfo(int p, Literal i) : permutation_index(p), image(i) {}
113
114 int permutation_index;
115 Literal image;
116 };
118
119 // For each permutation p, we maintain the list of all assigned literals
120 // affected by p whose trail index is < propagation_trail_index_; sorted by
121 // trail index. Next to each such literal, we also store:
122 struct AssignedLiteralInfo {
123 AssignedLiteralInfo(Literal l, Literal i, int index)
124 : literal(l), image(i), first_non_symmetric_info_index_so_far(index) {}
125
126 // The literal in question (assigned to true and in the support of p).
127 Literal literal;
128
129 // The image by p of the literal above.
130 Literal image;
131
132 // Previous AssignedLiteralInfos are considered 'symmetric' iff both their
133 // 'literal' and 'image' were assigned to true at the time the current
134 // AssignedLiteralInfo's literal was assigned (i.e. earlier in the trail).
135 int first_non_symmetric_info_index_so_far;
136 };
137 std::vector<std::vector<AssignedLiteralInfo>> permutation_trails_;
138
139 // Adds an AssignedLiteralInfo to the given permutation trail.
140 // Returns false if there is a non-symmetric literal in this trail with its
141 // image not already assigned to true by the solver.
142 bool Enqueue(const Trail& trail, Literal literal, Literal image,
143 std::vector<AssignedLiteralInfo>* p_trail);
144
145 // The identity permutation over all the literals.
146 // This is temporary modified to encode a sparse permutation and then always
147 // restored to the identity.
148 mutable util_intops::StrongVector<LiteralIndex, Literal> tmp_literal_mapping_;
149
150 // Symmetry reason indexed by trail_index.
151 struct ReasonInfo {
152 int source_trail_index;
153 int symmetry_index;
154 };
155 std::vector<ReasonInfo> reasons_;
156
157 mutable StatsGroup stats_;
158 int num_propagations_;
159 int num_conflicts_;
160};
161
162} // namespace sat
163} // namespace operations_research
164
165#endif // OR_TOOLS_SAT_SYMMETRY_H_
Base class for all the SAT constraints.
Definition sat_base.h:533
void AddSymmetry(std::unique_ptr< SparsePermutation > permutation)
Definition symmetry.cc:45
SymmetryPropagator(const SymmetryPropagator &)=delete
This type is neither copyable nor movable.
void Untrail(const Trail &trail, int trail_index) final
Definition symmetry.cc:143
absl::Span< const Literal > Reason(const Trail &trail, int trail_index, int64_t conflict_id) const final
Definition symmetry.cc:156
SymmetryPropagator & operator=(const SymmetryPropagator &)=delete
void Permute(int index, absl::Span< const Literal > input, std::vector< Literal > *output) const
Definition symmetry.cc:201
int literal
int index
In SWIG mode, we don't want anything besides these top-level includes.
static int input(yyscan_t yyscanner)