Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_utils.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// Utility functions to interact with an lp solver from the SAT context.
15
16#ifndef ORTOOLS_SAT_LP_UTILS_H_
17#define ORTOOLS_SAT_LP_UTILS_H_
18
19#include <stdint.h>
20
21#include <limits>
22#include <utility>
23#include <vector>
24
25#include "absl/status/status.h"
26#include "absl/strings/string_view.h"
27#include "absl/types/span.h"
34
35namespace operations_research {
36namespace sat {
37
38// Returns the smallest factor f such that f * abs(x) is integer modulo the
39// given tolerance relative to f (we use f * tolerance). It is only looking
40// for f smaller than the given limit. Returns zero if no such factor exist
41// below the limit.
42//
43// The complexity is a lot less than O(limit), but it is possible that we might
44// miss the smallest such factor if the tolerance used is too low. This is
45// because we only rely on the best rational approximations of x with increasing
46// denominator.
47int64_t FindRationalFactor(double x, int64_t limit = 1e4,
48 double tolerance = 1e-6);
49
50// Given a linear expression Sum_i c_i * X_i with each X_i in [lb_i, ub_i],
51// this returns a scaling factor f such that
52// 1/ the rounded expression cannot overflow given the domains of the X_i:
53// Sum |std::round(f * c_i) * X_i| <= max_absolute_activity
54// 2/ the error is bounded:
55// | Sum_i (std::round(f * c_i) - f * c_i) |
56// < f * wanted_absolute_activity_precision
57//
58// This also fills the exact errors made by using the returned scaling factor.
59// The heuristics try to minimize the magnitude of the scaled expression while
60// satisfying the requested precision.
61//
62// Returns 0.0 if no scaling factor can be found under the condition 1/. Note
63// that we try really hard to satisfy 2/ but we still return our best shot even
64// when 2/ is not satisfied. One can check this by comparing the returned
65// scaled_sum_error / f with wanted_absolute_activity_precision.
66//
67// TODO(user): unit test this and move to fp_utils.
68// TODO(user): Ideally the lower/upper should be int64_t so that we can have
69// an exact definition for the max_absolute_activity allowed.
71 absl::Span<const double> coefficients,
72 absl::Span<const double> lower_bounds,
73 absl::Span<const double> upper_bounds, int64_t max_absolute_activity,
74 double wanted_absolute_activity_precision, double* relative_coeff_error,
75 double* scaled_sum_error);
76
77// Helper to scale linear constraints with floating point coefficients to
78// CpModelProto::ConstraintProto. We use a class to reuse the temporary memory
79// when we scale many constraints.
80//
81// Note that this can be used to scale any constraint, it provides an API for
82// MPConstraintProto, but also directly from spans of CpModelProto variable
83// indices, coefficients and lower and upper bounds.
85 // Scales an individual constraint and add it to the given CpModelProto.
86 //
87 // We use the domain of the variables to derive error bounds and scale the
88 // constraint as best as we can within "wanted_precision" and
89 // "scaling_target". We usually scale with power of two scaling factor or
90 // a rational scaling factor if we detect a good one via FindRationalFactor().
91 //
92 // Returns an error if the given constraint contained huge coefficient or
93 // infinity. Note that we do not consider it an error if the wanted precision
94 // is not reached (best effort). One can check the error statistics field
95 // below and decide when there are too high and report an error separately.
96 absl::Status ScaleAndAddConstraint(
97 absl::Span<const int> vars, absl::Span<const double> coeffs,
98 double lower_bound, double upper_bound, absl::string_view name,
99 absl::Span<const IntegerVariableProto* const> var_domains,
100 ConstraintProto* constraint);
101
102 // Scales an individual MPConstraintProto constraint and add it to the given
103 // CpModelProto.
104 //
105 // This is a wrapper around the other ScaleAndAddConstraint() that use the
106 // var_index, coefficient, lower_bound and upper_bound fields of the given
107 // mp_constraint.
108 absl::Status ScaleAndAddConstraint(const MPConstraintProto& mp_constraint,
109 CpModelProto* cp_model);
110
111 // Statistics over all scaled constraints. This can be inspected to know the
112 // final error produced by ScaleAndAddConstraint().
115 double max_scaling_factor = 0.0;
116 double min_scaling_factor = std::numeric_limits<double>::infinity();
117
118 // Parameters. Whether we ignore or copy the mp_constraint.name() field.
119 bool keep_names = false;
120
121 // Parameters passed to FindBestScalingAndComputeErrors(), see documentation
122 // there to understand their meaning.
123 double wanted_precision = 1e-6;
124 int64_t scaling_target = int64_t{1} << 50;
125
126 // Private temporary field to reuse memory.
127 std::vector<int> var_indices;
128 std::vector<double> coefficients;
129 std::vector<double> lower_bounds;
130 std::vector<double> upper_bounds;
131};
132
133// Multiplies all continuous variable by the given scaling parameters and change
134// the rest of the model accordingly. The returned vector contains the scaling
135// of each variable (will always be 1.0 for integers) and can be used to recover
136// a solution of the unscaled problem from one of the new scaled problems by
137// dividing the variable values.
138//
139// We usually scale a continuous variable by scaling, but if its domain is going
140// to have larger values than max_bound, then we scale to have the max domain
141// magnitude equal to max_bound.
142//
143// Note that it is recommended to call DetectImpliedIntegers() before this
144// function so that we do not scale variables that do not need to be scaled.
145//
146// TODO(user): Also scale the solution hint if any.
147std::vector<double> ScaleContinuousVariables(double scaling, double max_bound,
148 MPModelProto* mp_model);
149
150// This simple step helps and should be done first. Returns false if the model
151// is trivially infeasible because of crossing bounds.
153 MPModelProto* mp_model,
154 SolverLogger* logger);
155
156// This function changes bounds of variables or constraints that have a
157// magnitude greater than mip_max_valid_magnitude.
158void ChangeLargeBoundsToInfinity(double max_magnitude, MPModelProto* mp_model,
159 SolverLogger* logger);
160
161// Performs some extra tests on the given MPModelProto and returns false if one
162// is not satisfied. These are needed before trying to convert it to the native
163// CP-SAT format.
165 const MPModelProto& mp_model,
166 SolverLogger* logger);
167
168// To satisfy our scaling requirements, any terms that is almost zero can just
169// be set to zero. We need to do that before operations like
170// DetectImpliedIntegers(), because really low coefficients can cause issues
171// and might lead to less detection.
172void RemoveNearZeroTerms(const SatParameters& params, MPModelProto* mp_model,
173 SolverLogger* logger);
174
175// This will mark implied integer as such. Note that it can also discover
176// variable of the form coeff * Integer + offset, and will change the model
177// so that these are marked as integer. It is why we return both a scaling and
178// an offset to transform the solution back to its original domain.
179//
180// TODO(user): Actually implement the offset part. This currently only happens
181// on the 3 neos-46470* miplib problems where we have a non-integer rhs.
182std::vector<double> DetectImpliedIntegers(MPModelProto* mp_model,
183 SolverLogger* logger);
184
185// Converts a MIP problem to a CpModel. Returns false if the coefficients
186// couldn't be converted to integers with a good enough precision.
187//
188// There is a bunch of caveats and you can find more details on the
189// SatParameters proto documentation for the mip_* parameters.
191 const MPModelProto& mp_model,
192 CpModelProto* cp_model,
193 SolverLogger* logger);
194
195// Converts a CP-SAT model to a MPModelProto one.
196// This only works for pure linear model (otherwise it returns false). This is
197// mainly useful for debugging or using CP-SAT presolve and then trying other
198// MIP solvers.
199//
200// TODO(user): This first version do not even handle basic Boolean constraint.
201// Support more constraints as needed.
203 MPModelProto* output);
204
205// Scales a double objective to its integer version and fills it in the proto.
206// The variable listed in the objective must be already defined in the cp_model
207// proto as this uses the variables bounds to compute a proper scaling.
208//
209// This uses params.mip_wanted_tolerance() and
210// params.mip_max_activity_exponent() to compute the scaling. Note however that
211// if the wanted tolerance is not satisfied this still scale with best effort.
212// You can see in the log the tolerance guaranteed by this automatic scaling.
213//
214// This will almost always returns true except for really bad cases like having
215// infinity in the objective.
216bool ScaleAndSetObjective(const SatParameters& params,
217 absl::Span<const std::pair<int, double>> objective,
218 double objective_offset, bool maximize,
219 CpModelProto* cp_model, SolverLogger* logger);
220
221// Given a CpModelProto with a floating point objective, and its scaled integer
222// version with a known lower bound, this uses the variable bounds to derive a
223// correct lower bound on the original objective.
224//
225// Note that the integer version can be way different, but then the bound is
226// likely to be bad. For now, we solve this with a simple LP with one
227// constraint.
228//
229// TODO(user): Code a custom algo with more precision guarantee?
231 const CpModelProto& model_proto_with_floating_point_objective,
232 const CpObjectiveProto& integer_objective,
233 int64_t inner_integer_objective_lower_bound);
234
235// Converts an integer program with only binary variables to a Boolean
236// optimization problem. Returns false if the problem didn't contains only
237// binary integer variable, or if the coefficients couldn't be converted to
238// integer with a good enough precision.
240 LinearBooleanProblem* problem);
241
242// Converts a Boolean optimization problem to its lp formulation.
245
246} // namespace sat
247} // namespace operations_research
248
249#endif // ORTOOLS_SAT_LP_UTILS_H_
double FindBestScalingAndComputeErrors(absl::Span< const double > coefficients, absl::Span< const double > lower_bounds, absl::Span< const double > upper_bounds, int64_t max_absolute_activity, double wanted_absolute_activity_precision, double *relative_coeff_error, double *scaled_sum_error)
Definition lp_utils.cc:868
bool ConvertMPModelProtoToCpModelProto(const SatParameters &params, const MPModelProto &mp_model, CpModelProto *cp_model, SolverLogger *logger)
Definition lp_utils.cc:929
int64_t FindRationalFactor(double x, int64_t limit, double tolerance)
Definition lp_utils.cc:135
void ConvertBooleanProblemToLinearProgram(const LinearBooleanProblem &problem, glop::LinearProgram *lp)
Definition lp_utils.cc:1650
bool MakeBoundsOfIntegerVariablesInteger(const SatParameters &params, MPModelProto *mp_model, SolverLogger *logger)
Definition lp_utils.cc:207
void ChangeLargeBoundsToInfinity(double max_magnitude, MPModelProto *mp_model, SolverLogger *logger)
Definition lp_utils.cc:240
bool ScaleAndSetObjective(const SatParameters &params, absl::Span< const std::pair< int, double > > objective, double objective_offset, bool maximize, CpModelProto *cp_model, SolverLogger *logger)
Definition lp_utils.cc:1365
std::vector< double > DetectImpliedIntegers(MPModelProto *mp_model, SolverLogger *logger)
Definition lp_utils.cc:485
void RemoveNearZeroTerms(const SatParameters &params, MPModelProto *mp_model, SolverLogger *logger)
Definition lp_utils.cc:311
bool ConvertBinaryMPModelProtoToBooleanProblem(const MPModelProto &mp_model, LinearBooleanProblem *problem)
Definition lp_utils.cc:1471
double ComputeTrueObjectiveLowerBound(const CpModelProto &model_proto_with_floating_point_objective, const CpObjectiveProto &integer_objective, const int64_t inner_integer_objective_lower_bound)
Definition lp_utils.cc:1714
bool ConvertCpModelProtoToMPModelProto(const CpModelProto &input, MPModelProto *output)
Definition lp_utils.cc:1161
std::vector< double > ScaleContinuousVariables(double scaling, double max_bound, MPModelProto *mp_model)
Definition lp_utils.cc:112
bool MPModelProtoValidationBeforeConversion(const SatParameters &params, const MPModelProto &mp_model, SolverLogger *logger)
Definition lp_utils.cc:421
OR-Tools root namespace.
static int input(yyscan_t yyscanner)
absl::Status ScaleAndAddConstraint(absl::Span< const int > vars, absl::Span< const double > coeffs, double lower_bound, double upper_bound, absl::string_view name, absl::Span< const IntegerVariableProto *const > var_domains, ConstraintProto *constraint)
Definition lp_utils.cc:747