Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
diophantine.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_DIOPHANTINE_H_
15#define OR_TOOLS_SAT_DIOPHANTINE_H_
16
17#include <cstdint>
18#include <vector>
19
20#include "absl/numeric/int128.h"
21#include "absl/types/span.h"
22
24
25// Reduces v modulo the elements_to_consider first elements of the (normal
26// form) basis. The leading coeff of a basis element is the last one. In other
27// terms, basis has the form:
28// * A 0 0 0 0 0
29// * * B 0 0 0 0
30// * * * C 0 0 0
31// .............
32// with non-zero pivots elements A, B, C, ... and the reduction is performed in
33// such a way that for a pivot P of the basis and the correspond entry x of v at
34// the end of the reduction, we have
35// -floor(|P|/2) <= v < ceil(|P|/2).
36void ReduceModuloBasis(absl::Span<const std::vector<absl::int128>> basis,
37 int elements_to_consider, std::vector<absl::int128>& v);
38
39// Returns an ordering of the indices of coefficients such that the GCD of its
40// initial segments decreases fast. As the product of the 15 smallest prime
41// numbers is the biggest fitting in an int64_t, it is guaranteed that the GCD
42// becomes stationary after at most 15 steps. Returns an empty vector if the GCD
43// is equal to the absolute value of one of the coefficients.
44std::vector<int> GreedyFastDecreasingGcd(absl::Span<const int64_t> coeffs);
45
46// The comments here describe basic feature of the fields. See more details in
47// the description of the function below SolveDiophantine.
49 // One of the coefficients is equal to the GCD of all coefficients.
51
52 // false if the equation is proven infeasible.
54
55 // Order of indices of the next fields.
56 // This is a permutation of [0, num_vars_of_initial_equation). It starts by
57 // the chosen pivots.
58 std::vector<int> index_permutation;
59
60 // Special (reduced) solution of the constraint. Only coefficients of pivots
61 // are specified. Further coefficients are 0.
62 // All coefficients except the first one are guaranteed to be int64_t (see
63 // ReductionModuloBasis).
64 std::vector<absl::int128> special_solution;
65
66 // Reduced basis of the kernel.
67 // All coefficients except the first one are guaranteed to be int64_t (see
68 // ReductionModuloBasis).
69 // Size is index_order.size() - 1.
70 std::vector<std::vector<absl::int128>> kernel_basis;
71
72 // Bounds of kernel multiples.
73 // Same size as kernel_basis.
74 std::vector<absl::int128> kernel_vars_lbs;
75 std::vector<absl::int128> kernel_vars_ubs;
76};
77
78// Gives a parametric description of the solutions of the Diophantine equation
79// with n variables:
80// sum(coeffs[i] * x[i]) = rhs.
81// var_lbs and var_ubs are bounds on desired values for variables x_i's.
82//
83// It is known that, ignoring variable bounds, the set of solutions of such an
84// equation is
85// 1. either empty if the gcd(coeffs[i]) does not divide rhs;
86// 2. or the sum of a special solution and an element of the kernel of the
87// equation.
88// In case 1, the function return .has_solution = false;
89// In case 2, if one coefficient is equal to the GCD of all (in absolute value),
90// returns .no_reformulation_needed = true. Otherwise, it behaves as follows:
91//
92// The kernel of the equation as dimension n-1.
93//
94// We assume we permute the variable by index_permutation, such that the first k
95// k terms have a gcd equal to the gcd of all coefficient (it is possible to do
96// this with k <= 15).
97// Under this assumption, we can find:
98// * a special solution that is entirely supported by the k first variables;
99// * a basis {b[0], b[1], ..., b[n-2]} of the kernel such that:
100// - for i = 0 ... k-2, b[i][j] = 0 if j > i+1;
101// - for i >= k-1, b[i][j] = 0 if j >= k except b[i][i+1] = 1.
102// The function returns the k first coefficients of the special solution and the
103// at most k first non-zero coefficients of each elements of the basis.
104//
105// In other terms, solutions have the form, for i in [0, k):
106// x[i] = special_solution[i] + sum(sum linear_basis[j][i] * y[j])
107// where:
108// * y[j] is a newly created variable for 0 <= j < k - 1;
109// * y[j] = x[index_permutation[j + 1]] otherwise.
110//
111// The function reduces the basis and the special solution in such a way that
112// the only coefficients that could get outside the range of input coefficients
113// are the first coefficient of the special solution and the first coefficient
114// of each element of the basis (see ReduceModuloBasis for more specific
115// conditions).
116//
117// Moreover, the function compute bounds for the newly created variables using
118// bounds of the variables passed as input. Note that:
119// * It can happen that a computed upper bound is lower than the corresponding
120// lower bound. It happens when a newly created variable can be bounded on an
121// interval containing no integer. In such a case, the function returns
122// .has_solution = false.
123// * The returned bounds describe a necessary condition for
124// x[i] in [var_lbs[i], var_ubs[i]]
125// but not a sufficient one.
126DiophantineSolution SolveDiophantine(absl::Span<const int64_t> coeffs,
127 int64_t rhs,
128 absl::Span<const int64_t> var_lbs,
129 absl::Span<const int64_t> var_ubs);
130
131} // namespace operations_research::sat
132
133#endif // OR_TOOLS_SAT_DIOPHANTINE_H_
DiophantineSolution SolveDiophantine(absl::Span< const int64_t > coeffs, int64_t rhs, absl::Span< const int64_t > var_lbs, absl::Span< const int64_t > var_ubs)
void ReduceModuloBasis(absl::Span< const std::vector< absl::int128 > > basis, const int elements_to_consider, std::vector< absl::int128 > &v)
std::vector< int > GreedyFastDecreasingGcd(const absl::Span< const int64_t > coeffs)
bool no_reformulation_needed
One of the coefficients is equal to the GCD of all coefficients.
Definition diophantine.h:50
std::vector< absl::int128 > kernel_vars_lbs
Definition diophantine.h:74
std::vector< std::vector< absl::int128 > > kernel_basis
Definition diophantine.h:70
std::vector< absl::int128 > special_solution
Definition diophantine.h:64
bool has_solutions
false if the equation is proven infeasible.
Definition diophantine.h:53
std::vector< absl::int128 > kernel_vars_ubs
Definition diophantine.h:75