-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
set_cover_mip.cc
153 lines (141 loc) · 5.54 KB
/
set_cover_mip.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/algorithms/set_cover_mip.h"
#include <cstdint>
#include <limits>
#include "absl/log/check.h"
#include "absl/types/span.h"
#include "ortools/algorithms/set_cover_invariant.h"
#include "ortools/algorithms/set_cover_model.h"
#include "ortools/base/logging.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/lp_data/lp_types.h"
namespace operations_research {
namespace {
// Returns the vector a - b.
ElementToIntVector Subtract(const ElementToIntVector& a,
const ElementToIntVector& b) {
ElementToIntVector delta(a.size());
DCHECK_EQ(a.size(), b.size());
for (const ElementIndex i : a.index_range()) {
delta[i] = a[i] - b[i];
}
return delta;
}
} // namespace
template <typename IndexType, typename ValueType>
using StrictVector = glop::StrictITIVector<IndexType, ValueType>;
bool SetCoverMip::NextSolution(bool use_integers,
double time_limit_in_seconds) {
return NextSolution(inv_->model()->all_subsets(), use_integers,
time_limit_in_seconds);
}
bool SetCoverMip::NextSolution(absl::Span<const SubsetIndex> focus,
bool use_integers,
double time_limit_in_seconds) {
SetCoverModel* model = inv_->model();
const SubsetIndex num_subsets(model->num_subsets());
const ElementIndex num_elements(model->num_elements());
SubsetBoolVector choices = inv_->is_selected();
MPSolver::OptimizationProblemType problem_type;
switch (mip_solver_) {
case SetCoverMipSolver::SCIP:
problem_type = MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING;
break;
case SetCoverMipSolver::GUROBI:
if (use_integers) {
problem_type = MPSolver::GUROBI_MIXED_INTEGER_PROGRAMMING;
} else {
problem_type = MPSolver::GUROBI_LINEAR_PROGRAMMING;
}
break;
case SetCoverMipSolver::SAT:
if (!use_integers) {
LOG(INFO) << "Defaulting to integer variables with SAT";
use_integers = true;
}
problem_type = MPSolver::SAT_INTEGER_PROGRAMMING;
break;
case SetCoverMipSolver::GLOP:
LOG(INFO) << "Defaulting to linear relaxation with GLOP";
use_integers = false;
problem_type = MPSolver::GLOP_LINEAR_PROGRAMMING;
break;
case SetCoverMipSolver::PDLP:
if (use_integers) {
LOG(INFO) << "Defaulting to linear relaxation with PDLP";
use_integers = false;
}
problem_type = MPSolver::PDLP_LINEAR_PROGRAMMING;
break;
default:
LOG(WARNING) << "Unknown solver value, defaulting to SCIP";
problem_type = MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING;
}
// We are using MPSolver, which is deprecated, because MathOpt does not
// provide an interface without using protobufs.
// We construct a restricted MIP, omitting all the parts of the problem
// that are already fixed in the invariant. The goal is to not spend time
// sending data, and having the MIP solver re-discover fixed variables.
MPSolver solver("set cover mip", problem_type);
solver.SuppressOutput();
MPObjective* const objective = solver.MutableObjective();
objective->SetMinimization();
StrictVector<ElementIndex, MPConstraint*> constraints(num_elements, nullptr);
StrictVector<SubsetIndex, MPVariable*> vars(num_subsets, nullptr);
ElementToIntVector coverage_outside_focus =
Subtract(inv_->coverage(), inv_->ComputeCoverageInFocus(focus));
for (const SubsetIndex subset : focus) {
vars[subset] = solver.MakeVar(0, 1, use_integers, "");
objective->SetCoefficient(vars[subset], model->subset_costs()[subset]);
for (const ElementIndex element : model->columns()[subset]) {
// The model should only contain elements that are not forcibly covered by
// subsets outside the focus.
if (coverage_outside_focus[element] == 0) continue;
if (constraints[element] == nullptr) {
constexpr double kInfinity = std::numeric_limits<double>::infinity();
constraints[element] = solver.MakeRowConstraint(1.0, kInfinity);
}
constraints[element]->SetCoefficient(vars[subset], 1);
}
}
// set_time_limit takes milliseconds as a unit.
solver.set_time_limit(static_cast<int64_t>(time_limit_in_seconds * 1000));
// Call the solver.
const MPSolver::ResultStatus solve_status = solver.Solve();
switch (solve_status) {
case MPSolver::OPTIMAL:
break;
case MPSolver::FEASIBLE:
break;
case MPSolver::INFEASIBLE:
LOG(ERROR) << "Did not find solution. Problem is infeasible.";
break;
case MPSolver::UNBOUNDED:
LOG(ERROR) << "Did not find solution. Problem is unbounded.";
break;
default:
LOG(ERROR) << "Solving resulted in an error.";
return false;
}
if (use_integers) {
for (const SubsetIndex subset : focus) {
choices[subset] = (vars[subset]->solution_value() > 0.9);
}
inv_->LoadSolution(choices);
} else {
lower_bound_ = solver.Objective().Value();
}
return true;
}
} // namespace operations_research