Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
infeasible_subsystem_validator.cc
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
15
16#include <cstdint>
17
18#include "absl/status/status.h"
21#include "ortools/math_opt/infeasible_subsystem.pb.h"
22#include "ortools/math_opt/result.pb.h"
25
27
28namespace {
29
30absl::Status CheckMapKeys(
31 const google::protobuf::Map<int64_t, ModelSubsetProto::Bounds>& bounds_map,
32 const IdNameBiMap& universe) {
33 for (const auto& [id, unused] : bounds_map) {
34 if (!universe.HasId(id)) {
35 return util::InvalidArgumentErrorBuilder() << "unrecognized id: " << id;
36 }
37 }
38 return absl::OkStatus();
39}
40
41absl::Status CheckRepeatedIds(
42 const google::protobuf::RepeatedField<int64_t>& ids,
43 const IdNameBiMap& universe) {
45 RETURN_IF_ERROR(CheckIdsSubset(ids, universe));
46 return absl::OkStatus();
47}
48
49} // namespace
50
51absl::Status ValidateModelSubset(const ModelSubsetProto& model_subset,
52 const ModelSummary& model_summary) {
54 CheckMapKeys(model_subset.variable_bounds(), model_summary.variables))
55 << "bad ModelSubsetProto.variable_bounds";
56 RETURN_IF_ERROR(CheckRepeatedIds(model_subset.variable_integrality(),
57 model_summary.variables))
58 << "bad ModelSubsetProto.variable_integrality";
59 RETURN_IF_ERROR(CheckMapKeys(model_subset.linear_constraints(),
60 model_summary.linear_constraints))
61 << "bad ModelSubsetProto.linear_constraints";
62 RETURN_IF_ERROR(CheckMapKeys(model_subset.quadratic_constraints(),
63 model_summary.quadratic_constraints))
64 << "bad ModelSubsetProto.quadratic_constraints";
65 RETURN_IF_ERROR(CheckRepeatedIds(model_subset.second_order_cone_constraints(),
66 model_summary.second_order_cone_constraints))
67 << "bad ModelSubsetProto.second_order_cone_constraints";
68 RETURN_IF_ERROR(CheckRepeatedIds(model_subset.sos1_constraints(),
69 model_summary.sos1_constraints))
70 << "bad ModelSubsetProto.sos1_constraints";
71 RETURN_IF_ERROR(CheckRepeatedIds(model_subset.sos2_constraints(),
72 model_summary.sos2_constraints))
73 << "bad ModelSubsetProto.sos2_constraints";
74 RETURN_IF_ERROR(CheckRepeatedIds(model_subset.indicator_constraints(),
75 model_summary.indicator_constraints))
76 << "bad ModelSubsetProto.indicator_constraints";
77 return absl::OkStatus();
78}
79
81 const ComputeInfeasibleSubsystemResultProto& result,
82 const ModelSummary& model_summary) {
84 if (result.feasibility() == FEASIBILITY_STATUS_INFEASIBLE) {
86 ValidateModelSubset(result.infeasible_subsystem(), model_summary));
87 }
88 return absl::OkStatus();
89}
90
92 const ComputeInfeasibleSubsystemResultProto& result) {
93 RETURN_IF_ERROR(ValidateFeasibilityStatus(result.feasibility()))
94 << "bad ComputeInfeasibleSubsystemResultProto.feasibility";
95 if (result.feasibility() != FEASIBILITY_STATUS_INFEASIBLE) {
96 // Check that the `infeasible_subsystem` is empty by validating against an
97 // empty ModelSummary.
98 if (!ValidateModelSubset(result.infeasible_subsystem(), ModelSummary())
99 .ok()) {
101 << "nonempty infeasible_subsystem with feasibility status: "
102 << FeasibilityStatusProto_Name(result.feasibility());
103 }
104 if (result.is_minimal()) {
106 << "is_minimal is true with feasibility status: "
107 << FeasibilityStatusProto_Name(result.feasibility());
108 }
109 }
110 return absl::OkStatus();
111}
112
113} // namespace operations_research::math_opt
#define RETURN_IF_ERROR(expr)
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::Status ValidateFeasibilityStatus(const FeasibilityStatusProto &status)
absl::Status ValidateModelSubset(const ModelSubsetProto &model_subset, const ModelSummary &model_summary)
absl::Status ValidateComputeInfeasibleSubsystemResult(const ComputeInfeasibleSubsystemResultProto &result, const ModelSummary &model_summary)
absl::Status CheckIdsSubset(absl::Span< const int64_t > ids, const IdNameBiMap &universe, std::optional< int64_t > upper_bound)
absl::Status ValidateComputeInfeasibleSubsystemResultNoModel(const ComputeInfeasibleSubsystemResultProto &result)
Validates the internal consistency of the fields.
absl::Status CheckIdsRangeAndStrictlyIncreasing(absl::Span< const int64_t > ids)
StatusBuilder InvalidArgumentErrorBuilder()