Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
variable_expand.h
Go to the documentation of this file.
1// Copyright 2010-2025 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 ORTOOLS_SAT_VARIABLE_EXPAND_H_
15#define ORTOOLS_SAT_VARIABLE_EXPAND_H_
16
17#include <cstdint>
18#include <optional>
19#include <vector>
20
21#include "absl/container/btree_map.h"
22#include "absl/container/flat_hash_set.h"
26
27namespace operations_research {
28namespace sat {
29
31 public:
32 ValueEncoding(int var, PresolveContext* context);
33 // Build the set of observed values. This cannot be called after
34 // CanonicalizeEncodedValuesAndAddEscapeValues() has been called.
35 void AddReferencedValueToEncode(int64_t value);
36
37 // Add an optional value to encode. An optional value is a value that is not
38 // referenced by a linear1 constraint, but that is needed to support hint
39 // manipulation.
40 void AddOptionalValueToEncode(int64_t value);
41
42 // This method is called after all values from lit => var ==/!= value have
43 // been added. It canonicalizes the encoded values and adds an escape value
44 // if needed. It there is an objective, the escape value is the min or the max
45 // of the residual domain of the variable depending on the objective
46 // coefficient of the variable. It there are no objective, the escape value is
47 // the smallest value of the residual domain.
48 // With this escape value, we can safely reduce the domain of the variable to
49 // observed + escape values, and add an exactly_one constraint on all the
50 // literals involved.
52 bool push_down_when_unconstrained, bool has_le_ge_linear1);
53
54 // Getters on the observed values.
55 bool empty() const;
56 bool is_fully_encoded() const;
57 const std::vector<int64_t>& encoded_values() const;
58
59 // A unique escape value is defined only if the linear1 are var == value and
60 // var != value. In this case, only one escape value is needed.
61 std::optional<int64_t> unique_escape_value() const;
62
63 // Setters and getters on the value encoding.
64 void ForceFullEncoding();
66 int literal(int64_t value) const;
67 const absl::btree_map<int64_t, int>& encoding() const;
68
69 private:
70 const int var_;
71 const Domain var_domain_;
72 PresolveContext* context_;
73 std::vector<int64_t> encoded_values_;
74 std::vector<int64_t> referenced_encoded_values_;
75 absl::btree_map<int64_t, int> encoding_;
76 std::optional<int64_t> unique_escape_value_;
77 bool is_closed_ = false;
78 bool is_fully_encoded_ = false;
79};
80
82 public:
83 OrderEncoding(int var, PresolveContext* context,
84 SolutionCrush& solution_crush);
85 void InsertLeLiteral(int64_t value, int literal);
86 void InsertGeLiteral(int64_t value, int literal);
88 int ge_literal(int64_t value) const;
89 int le_literal(int64_t value) const;
90 int num_encoded_values() const;
91
92 private:
93 void CollectAllOrderEncodingValues();
94
95 const int var_;
96 const Domain var_domain_;
97 PresolveContext* context_;
98 SolutionCrush& solution_crush_;
99 absl::btree_map<int64_t, absl::flat_hash_set<int>> tmp_ge_to_literals_;
100 absl::btree_map<int64_t, absl::flat_hash_set<int>> tmp_le_to_literals_;
101 absl::btree_map<int64_t, int> encoded_le_literal_;
102};
103
105 SolutionCrush& solution_crush);
106
107} // namespace sat
108} // namespace operations_research
109
110#endif // ORTOOLS_SAT_VARIABLE_EXPAND_H_
OrderEncoding(int var, PresolveContext *context, SolutionCrush &solution_crush)
void CreateAllOrderEncodingLiterals(const ValueEncoding &values)
void InsertLeLiteral(int64_t value, int literal)
void InsertGeLiteral(int64_t value, int literal)
const absl::btree_map< int64_t, int > & encoding() const
const std::vector< int64_t > & encoded_values() const
std::optional< int64_t > unique_escape_value() const
ValueEncoding(int var, PresolveContext *context)
void CanonicalizeEncodedValuesAndAddEscapeValue(bool push_down_when_unconstrained, bool has_le_ge_linear1)
void TryToReplaceVariableByItsEncoding(int var, PresolveContext *context, SolutionCrush &solution_crush)
OR-Tools root namespace.