Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model_checker.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 OR_TOOLS_SAT_CP_MODEL_CHECKER_H_
15#define OR_TOOLS_SAT_CP_MODEL_CHECKER_H_
16
17#include <cstdint>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/types/span.h"
25
26namespace operations_research {
27namespace sat {
28
29// Verifies that the given model satisfies all the properties described in the
30// proto comments. Returns an empty string if it is the case, otherwise fails at
31// the first error and returns a human-readable description of the issue.
32//
33// The extra parameter is internal and mainly for debugging. After the problem
34// has been presolved, we have a stricter set of properties we want to enforce.
35//
36// TODO(user): Add any needed overflow validation because we are far from
37// exhaustive. We could also run a small presolve that tighten variable bounds
38// before the overflow check to facilitate the lives of our users, but it is a
39// some work to put in place.
40std::string ValidateCpModel(const CpModelProto& model,
41 bool after_presolve = false);
42
43// Some validation (in particular the floating point objective) requires to
44// read parameters.
45//
46// TODO(user): Ideally we would have just one ValidateCpModel() function but
47// this was introduced after many users already use ValidateCpModel() without
48// parameters.
49std::string ValidateInputCpModel(const SatParameters& params,
50 const CpModelProto& model);
51
52// Check if a given linear expression can create overflow. If it doesn't,
53// sets `implied_domain` to the implied domain of the expression. It is exposed
54// to test new constraints created during the presolve.
56 const CpModelProto& model, absl::Span<const int> vars,
57 absl::Span<const int64_t> coeffs, int64_t offset = 0,
58 std::pair<int64_t, int64_t>* implied_domain = nullptr);
59
60// Verifies that the given variable assignment is a feasible solution of the
61// given model. The values vector should be in one to one correspondence with
62// the model.variables() list of variables.
63//
64// The last two arguments are optional and help debugging a failing constraint
65// due to presolve.
66bool SolutionIsFeasible(const CpModelProto& model,
67 absl::Span<const int64_t> variable_values,
68 const CpModelProto* mapping_proto = nullptr,
69 const std::vector<int>* postsolve_mapping = nullptr);
70
71// Verifies some invariants that any optimal solution must satisfy.
72bool SolutionCanBeOptimal(const CpModelProto& model,
73 absl::Span<const int64_t> variable_values);
74
75// Checks a single constraint for feasibility.
76// This has some overhead, and should only be used for debugging.
77// The full model is needed for scheduling constraints that refers to intervals.
78bool ConstraintIsFeasible(const CpModelProto& model,
79 const ConstraintProto& constraint,
80 absl::Span<const int64_t> variable_values);
81
82} // namespace sat
83} // namespace operations_research
84
85#endif // OR_TOOLS_SAT_CP_MODEL_CHECKER_H_
std::string ValidateInputCpModel(const SatParameters &params, const CpModelProto &model)
std::string ValidateCpModel(const CpModelProto &model, bool after_presolve)
bool SolutionIsFeasible(const CpModelProto &model, absl::Span< const int64_t > variable_values, const CpModelProto *mapping_proto, const std::vector< int > *postsolve_mapping)
bool SolutionCanBeOptimal(const CpModelProto &model, absl::Span< const int64_t > variable_values)
Verifies some invariants that any optimal solution must satisfy.
bool ConstraintIsFeasible(const CpModelProto &model, const ConstraintProto &constraint, absl::Span< const int64_t > variable_values)
bool PossibleIntegerOverflow(const CpModelProto &model, absl::Span< const int > vars, absl::Span< const int64_t > coeffs, int64_t offset, std::pair< int64_t, int64_t > *implied_domain)
In SWIG mode, we don't want anything besides these top-level includes.