Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gurobi_proto_solver.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 <algorithm>
17#include <cmath>
18#include <cstdint>
19#include <limits>
20#include <memory>
21#include <numeric>
22#include <string>
23#include <vector>
24
25#include "absl/base/attributes.h"
26#include "absl/cleanup/cleanup.h"
27#include "absl/log/check.h"
28#include "absl/status/status.h"
29#include "absl/status/statusor.h"
30#include "absl/strings/str_cat.h"
31#include "absl/strings/str_format.h"
32#include "absl/strings/str_join.h"
33#include "absl/strings/str_split.h"
34#include "absl/strings/string_view.h"
35#include "absl/time/clock.h"
36#include "absl/time/time.h"
37#include "absl/types/optional.h"
40#include "ortools/base/timer.h"
42#include "ortools/linear_solver/linear_solver.pb.h"
45
46namespace operations_research {
47
48namespace {
49constexpr int GRB_OK = 0;
50
51inline absl::Status GurobiCodeToUtilStatus(int error_code,
52 const char* source_file,
53 int source_line,
54 const char* statement,
55 GRBenv* const env) {
56 if (error_code == GRB_OK) return absl::OkStatus();
57 return absl::InvalidArgumentError(absl::StrFormat(
58 "Gurobi error code %d (file '%s', line %d) on '%s': %s", error_code,
59 source_file, source_line, statement, GRBgeterrormsg(env)));
60}
61
62int AddIndicatorConstraint(const MPGeneralConstraintProto& gen_cst,
63 GRBmodel* gurobi_model,
64 std::vector<int>* tmp_variables,
65 std::vector<double>* tmp_coefficients) {
66 CHECK(gurobi_model != nullptr);
67 CHECK(tmp_variables != nullptr);
68 CHECK(tmp_coefficients != nullptr);
69
70 const auto& ind_cst = gen_cst.indicator_constraint();
71 MPConstraintProto cst = ind_cst.constraint();
72 if (cst.lower_bound() > -std::numeric_limits<double>::infinity()) {
74 gurobi_model, gen_cst.name().c_str(), ind_cst.var_index(),
75 ind_cst.var_value(), cst.var_index_size(),
76 cst.mutable_var_index()->mutable_data(),
77 cst.mutable_coefficient()->mutable_data(),
78 cst.upper_bound() == cst.lower_bound() ? GRB_EQUAL : GRB_GREATER_EQUAL,
79 cst.lower_bound());
80 if (status != GRB_OK) return status;
81 }
82 if (cst.upper_bound() < std::numeric_limits<double>::infinity() &&
83 cst.lower_bound() != cst.upper_bound()) {
84 return GRBaddgenconstrIndicator(gurobi_model, gen_cst.name().c_str(),
85 ind_cst.var_index(), ind_cst.var_value(),
86 cst.var_index_size(),
87 cst.mutable_var_index()->mutable_data(),
88 cst.mutable_coefficient()->mutable_data(),
89 GRB_LESS_EQUAL, cst.upper_bound());
90 }
91
92 return GRB_OK;
93}
94
95int AddSosConstraint(const MPSosConstraint& sos_cst, GRBmodel* gurobi_model,
96 std::vector<int>* tmp_variables,
97 std::vector<double>* tmp_weights) {
98 CHECK(gurobi_model != nullptr);
99 CHECK(tmp_variables != nullptr);
100 CHECK(tmp_weights != nullptr);
101
102 tmp_variables->resize(sos_cst.var_index_size(), 0);
103 for (int v = 0; v < sos_cst.var_index_size(); ++v) {
104 (*tmp_variables)[v] = sos_cst.var_index(v);
105 }
106 tmp_weights->resize(sos_cst.var_index_size(), 0);
107 if (sos_cst.weight_size() == sos_cst.var_index_size()) {
108 for (int w = 0; w < sos_cst.weight_size(); ++w) {
109 (*tmp_weights)[w] = sos_cst.weight(w);
110 }
111 } else {
112 DCHECK_EQ(sos_cst.weight_size(), 0);
113 // Gurobi requires variable weights in their SOS constraints.
114 std::iota(tmp_weights->begin(), tmp_weights->end(), 1);
115 }
116
117 std::vector<int> types = {sos_cst.type() == MPSosConstraint::SOS1_DEFAULT
119 : GRB_SOS_TYPE2};
120 std::vector<int> begins = {0};
121 return GRBaddsos(gurobi_model, /*numsos=*/1,
122 /*nummembers=*/sos_cst.var_index_size(),
123 /*types=*/types.data(),
124 /*beg=*/begins.data(), /*ind=*/tmp_variables->data(),
125 /*weight*/ tmp_weights->data());
126}
127
128int AddQuadraticConstraint(const MPGeneralConstraintProto& gen_cst,
129 GRBmodel* gurobi_model) {
130 CHECK(gurobi_model != nullptr);
131 constexpr double kInfinity = std::numeric_limits<double>::infinity();
132
133 CHECK(gen_cst.has_quadratic_constraint());
134 const MPQuadraticConstraint& quad_cst = gen_cst.quadratic_constraint();
135
136 auto addqconstr = [](GRBmodel* gurobi_model, MPQuadraticConstraint quad_cst,
137 char sense, double rhs, const std::string& name) {
138 return GRBaddqconstr(
139 gurobi_model,
140 /*numlnz=*/quad_cst.var_index_size(),
141 /*lind=*/quad_cst.mutable_var_index()->mutable_data(),
142 /*lval=*/quad_cst.mutable_coefficient()->mutable_data(),
143 /*numqnz=*/quad_cst.qvar1_index_size(),
144 /*qrow=*/quad_cst.mutable_qvar1_index()->mutable_data(),
145 /*qcol=*/quad_cst.mutable_qvar2_index()->mutable_data(),
146 /*qval=*/quad_cst.mutable_qcoefficient()->mutable_data(),
147 /*sense=*/sense,
148 /*rhs=*/rhs,
149 /*QCname=*/name.c_str());
150 };
151
152 if (quad_cst.has_lower_bound() && quad_cst.lower_bound() > -kInfinity) {
153 const int grb_status =
154 addqconstr(gurobi_model, gen_cst.quadratic_constraint(),
155 GRB_GREATER_EQUAL, quad_cst.lower_bound(),
156 gen_cst.has_name() ? gen_cst.name() + "_lb" : "");
157 if (grb_status != GRB_OK) return grb_status;
158 }
159 if (quad_cst.has_upper_bound() && quad_cst.upper_bound() < kInfinity) {
160 const int grb_status =
161 addqconstr(gurobi_model, gen_cst.quadratic_constraint(), GRB_LESS_EQUAL,
162 quad_cst.upper_bound(),
163 gen_cst.has_name() ? gen_cst.name() + "_ub" : "");
164 if (grb_status != GRB_OK) return grb_status;
165 }
166
167 return GRB_OK;
168}
169
170int AddAndConstraint(const MPGeneralConstraintProto& gen_cst,
171 GRBmodel* gurobi_model, std::vector<int>* tmp_variables) {
172 CHECK(gurobi_model != nullptr);
173 CHECK(tmp_variables != nullptr);
174
175 auto and_cst = gen_cst.and_constraint();
176 return GRBaddgenconstrAnd(
177 gurobi_model,
178 /*name=*/gen_cst.name().c_str(),
179 /*resvar=*/and_cst.resultant_var_index(),
180 /*nvars=*/and_cst.var_index_size(),
181 /*vars=*/and_cst.mutable_var_index()->mutable_data());
182}
183
184int AddOrConstraint(const MPGeneralConstraintProto& gen_cst,
185 GRBmodel* gurobi_model, std::vector<int>* tmp_variables) {
186 CHECK(gurobi_model != nullptr);
187 CHECK(tmp_variables != nullptr);
188
189 auto or_cst = gen_cst.or_constraint();
190 return GRBaddgenconstrOr(gurobi_model,
191 /*name=*/gen_cst.name().c_str(),
192 /*resvar=*/or_cst.resultant_var_index(),
193 /*nvars=*/or_cst.var_index_size(),
194 /*vars=*/or_cst.mutable_var_index()->mutable_data());
195}
196
197int AddMinConstraint(const MPGeneralConstraintProto& gen_cst,
198 GRBmodel* gurobi_model, std::vector<int>* tmp_variables) {
199 CHECK(gurobi_model != nullptr);
200 CHECK(tmp_variables != nullptr);
201
202 auto min_cst = gen_cst.min_constraint();
203 return GRBaddgenconstrMin(
204 gurobi_model,
205 /*name=*/gen_cst.name().c_str(),
206 /*resvar=*/min_cst.resultant_var_index(),
207 /*nvars=*/min_cst.var_index_size(),
208 /*vars=*/min_cst.mutable_var_index()->mutable_data(),
209 /*constant=*/min_cst.has_constant()
210 ? min_cst.constant()
211 : std::numeric_limits<double>::infinity());
212}
213
214int AddMaxConstraint(const MPGeneralConstraintProto& gen_cst,
215 GRBmodel* gurobi_model, std::vector<int>* tmp_variables) {
216 CHECK(gurobi_model != nullptr);
217 CHECK(tmp_variables != nullptr);
218
219 auto max_cst = gen_cst.max_constraint();
220 return GRBaddgenconstrMax(
221 gurobi_model,
222 /*name=*/gen_cst.name().c_str(),
223 /*resvar=*/max_cst.resultant_var_index(),
224 /*nvars=*/max_cst.var_index_size(),
225 /*vars=*/max_cst.mutable_var_index()->mutable_data(),
226 /*constant=*/max_cst.has_constant()
227 ? max_cst.constant()
228 : -std::numeric_limits<double>::infinity());
229}
230} // namespace
231
232absl::Status SetSolverSpecificParameters(absl::string_view parameters,
233 GRBenv* gurobi) {
234 if (parameters.empty()) return absl::OkStatus();
235 std::vector<std::string> error_messages;
236 for (absl::string_view line : absl::StrSplit(parameters, '\n')) {
237 // Empty lines are simply ignored.
238 if (line.empty()) continue;
239 // Comment tokens end at the next new-line, or the end of the string.
240 // The first character must be '#'
241 if (line[0] == '#') continue;
242 for (absl::string_view token :
243 absl::StrSplit(line, ',', absl::SkipWhitespace())) {
244 if (token.empty()) continue;
245 std::vector<std::string> key_value =
246 absl::StrSplit(token, absl::ByAnyChar(" ="), absl::SkipWhitespace());
247 // If one parameter fails, we keep processing the list of parameters.
248 if (key_value.size() != 2) {
249 const std::string current_message =
250 absl::StrCat("Cannot parse parameter '", token,
251 "'. Expected format is 'ParameterName value' or "
252 "'ParameterName=value'");
253 error_messages.push_back(current_message);
254 continue;
255 }
256 const int gurobi_code =
257 GRBsetparam(gurobi, key_value[0].c_str(), key_value[1].c_str());
258 if (gurobi_code != GRB_OK) {
259 const std::string current_message = absl::StrCat(
260 "Error setting parameter '", key_value[0], "' to value '",
261 key_value[1], "': ", GRBgeterrormsg(gurobi));
262 error_messages.push_back(current_message);
263 continue;
264 }
265 VLOG(2) << absl::StrCat("Set parameter '", key_value[0], "' to value '",
266 key_value[1]);
267 }
268 }
269
270 if (error_messages.empty()) return absl::OkStatus();
271 return absl::InvalidArgumentError(absl::StrJoin(error_messages, "\n"));
272}
273
274absl::StatusOr<MPSolutionResponse> GurobiSolveProto(
275 LazyMutableCopy<MPModelRequest> request, GRBenv* gurobi_env) {
276 MPSolutionResponse response;
277 const absl::optional<LazyMutableCopy<MPModelProto>> optional_model =
278 GetMPModelOrPopulateResponse(request, &response);
279 if (!optional_model) return response;
280 const MPModelProto& model = **optional_model;
281
282 // We set `gurobi_env` to point to a new environment if no existing one is
283 // provided. We must make sure that we free this environment when we exit this
284 // function.
285 bool gurobi_env_was_created = false;
286 auto gurobi_env_deleter = absl::MakeCleanup([&]() {
287 if (gurobi_env_was_created && gurobi_env != nullptr) {
288 GRBfreeenv(gurobi_env);
289 }
290 });
291 if (gurobi_env == nullptr) {
292 ASSIGN_OR_RETURN(gurobi_env, GetGurobiEnv());
293 gurobi_env_was_created = true;
294 }
295
296 GRBmodel* gurobi_model = nullptr;
297 auto gurobi_model_deleter = absl::MakeCleanup([&]() {
298 const int error_code = GRBfreemodel(gurobi_model);
299 LOG_IF(DFATAL, error_code != GRB_OK)
300 << "GRBfreemodel failed with error " << error_code << ": "
301 << GRBgeterrormsg(gurobi_env);
302 });
303
304// `gurobi_env` references ther GRBenv argument.
305#define RETURN_IF_GUROBI_ERROR(x) \
306 RETURN_IF_ERROR( \
307 GurobiCodeToUtilStatus(x, __FILE__, __LINE__, #x, gurobi_env));
308
309 RETURN_IF_GUROBI_ERROR(GRBnewmodel(gurobi_env, &gurobi_model,
310 model.name().c_str(),
311 /*numvars=*/0,
312 /*obj=*/nullptr,
313 /*lb=*/nullptr,
314 /*ub=*/nullptr,
315 /*vtype=*/nullptr,
316 /*varnames=*/nullptr));
317 GRBenv* const model_env = GRBgetenv(gurobi_model);
318
321 request->enable_internal_solver_output()));
322 if (request->has_solver_specific_parameters()) {
323 const auto parameters_status = SetSolverSpecificParameters(
324 request->solver_specific_parameters(), model_env);
325 if (!parameters_status.ok()) {
326 response.set_status(MPSOLVER_MODEL_INVALID_SOLVER_PARAMETERS);
327 response.set_status_str(
328 std::string(parameters_status.message())); // NOLINT
329 return response;
330 }
331 }
332 if (request->solver_time_limit_seconds() > 0) {
335 request->solver_time_limit_seconds()));
336 }
337
338 const int variable_size = model.variable_size();
339 bool has_integer_variables = false;
340 {
341 std::vector<double> obj_coeffs(variable_size, 0);
342 std::vector<double> lb(variable_size);
343 std::vector<double> ub(variable_size);
344 std::vector<char> ctype(variable_size);
345 std::vector<const char*> varnames(variable_size);
346 for (int v = 0; v < variable_size; ++v) {
347 const MPVariableProto& variable = model.variable(v);
348 obj_coeffs[v] = variable.objective_coefficient();
349 lb[v] = variable.lower_bound();
350 ub[v] = variable.upper_bound();
351 ctype[v] = variable.is_integer() &&
352 request->solver_type() ==
353 MPModelRequest::GUROBI_MIXED_INTEGER_PROGRAMMING
356 if (variable.is_integer()) has_integer_variables = true;
357 if (!variable.name().empty()) varnames[v] = variable.name().c_str();
358 }
359
361 GRBaddvars(gurobi_model, variable_size, 0, nullptr, nullptr, nullptr,
362 /*obj=*/obj_coeffs.data(),
363 /*lb=*/lb.data(), /*ub=*/ub.data(), /*vtype=*/ctype.data(),
364 /*varnames=*/const_cast<char**>(varnames.data())));
365
366 // Set solution hints if any.
367 for (int i = 0; i < model.solution_hint().var_index_size(); ++i) {
369 gurobi_model, GRB_DBL_ATTR_START, model.solution_hint().var_index(i),
370 model.solution_hint().var_value(i)));
371 }
372 }
373
374 {
375 std::vector<int> ct_variables;
376 std::vector<double> ct_coefficients;
377 for (int c = 0; c < model.constraint_size(); ++c) {
378 const MPConstraintProto& constraint = model.constraint(c);
379 const int size = constraint.var_index_size();
380 ct_variables.resize(size, 0);
381 ct_coefficients.resize(size, 0);
382 for (int i = 0; i < size; ++i) {
383 ct_variables[i] = constraint.var_index(i);
384 ct_coefficients[i] = constraint.coefficient(i);
385 }
386 // Using GRBaddrangeconstr for constraints that don't require it adds
387 // a slack which is not always removed by presolve.
388 if (constraint.lower_bound() == constraint.upper_bound()) {
390 gurobi_model, /*numnz=*/size, /*cind=*/ct_variables.data(),
391 /*cval=*/ct_coefficients.data(),
392 /*sense=*/GRB_EQUAL, /*rhs=*/constraint.lower_bound(),
393 /*constrname=*/constraint.name().c_str()));
394 } else if (constraint.lower_bound() ==
395 -std::numeric_limits<double>::infinity()) {
397 gurobi_model, /*numnz=*/size, /*cind=*/ct_variables.data(),
398 /*cval=*/ct_coefficients.data(),
399 /*sense=*/GRB_LESS_EQUAL, /*rhs=*/constraint.upper_bound(),
400 /*constrname=*/constraint.name().c_str()));
401 } else if (constraint.upper_bound() ==
402 std::numeric_limits<double>::infinity()) {
404 gurobi_model, /*numnz=*/size, /*cind=*/ct_variables.data(),
405 /*cval=*/ct_coefficients.data(),
406 /*sense=*/GRB_GREATER_EQUAL, /*rhs=*/constraint.lower_bound(),
407 /*constrname=*/constraint.name().c_str()));
408 } else {
410 gurobi_model, /*numnz=*/size, /*cind=*/ct_variables.data(),
411 /*cval=*/ct_coefficients.data(),
412 /*lower=*/constraint.lower_bound(),
413 /*upper=*/constraint.upper_bound(),
414 /*constrname=*/constraint.name().c_str()));
415 }
416 }
417
418 for (const auto& gen_cst : model.general_constraint()) {
419 switch (gen_cst.general_constraint_case()) {
420 case MPGeneralConstraintProto::kIndicatorConstraint: {
421 RETURN_IF_GUROBI_ERROR(AddIndicatorConstraint(
422 gen_cst, gurobi_model, &ct_variables, &ct_coefficients));
423 break;
424 }
425 case MPGeneralConstraintProto::kSosConstraint: {
426 RETURN_IF_GUROBI_ERROR(AddSosConstraint(gen_cst.sos_constraint(),
427 gurobi_model, &ct_variables,
428 &ct_coefficients));
429 break;
430 }
431 case MPGeneralConstraintProto::kQuadraticConstraint: {
432 RETURN_IF_GUROBI_ERROR(AddQuadraticConstraint(gen_cst, gurobi_model));
433 break;
434 }
435 case MPGeneralConstraintProto::kAbsConstraint: {
437 gurobi_model,
438 /*name=*/gen_cst.name().c_str(),
439 /*resvar=*/gen_cst.abs_constraint().resultant_var_index(),
440 /*argvar=*/gen_cst.abs_constraint().var_index()));
441 break;
442 }
443 case MPGeneralConstraintProto::kAndConstraint: {
445 AddAndConstraint(gen_cst, gurobi_model, &ct_variables));
446 break;
447 }
448 case MPGeneralConstraintProto::kOrConstraint: {
450 AddOrConstraint(gen_cst, gurobi_model, &ct_variables));
451 break;
452 }
453 case MPGeneralConstraintProto::kMinConstraint: {
455 AddMinConstraint(gen_cst, gurobi_model, &ct_variables));
456 break;
457 }
458 case MPGeneralConstraintProto::kMaxConstraint: {
460 AddMaxConstraint(gen_cst, gurobi_model, &ct_variables));
461 break;
462 }
463 default:
464 return absl::UnimplementedError(
465 absl::StrFormat("General constraints of type %i not supported.",
466 gen_cst.general_constraint_case()));
467 }
468 }
469 }
470
472 model.maximize() ? -1 : 1));
474 model.objective_offset()));
475 if (model.has_quadratic_objective()) {
476 MPQuadraticObjective qobj = model.quadratic_objective();
477 if (qobj.coefficient_size() > 0) {
479 GRBaddqpterms(gurobi_model, /*numqnz=*/qobj.coefficient_size(),
480 /*qrow=*/qobj.mutable_qvar1_index()->mutable_data(),
481 /*qcol=*/qobj.mutable_qvar2_index()->mutable_data(),
482 /*qval=*/qobj.mutable_coefficient()->mutable_data()));
483 }
484 }
485
487
488 const absl::Time time_before = absl::Now();
489 UserTimer user_timer;
490 user_timer.Start();
491
492 RETURN_IF_GUROBI_ERROR(GRBoptimize(gurobi_model));
493
494 const absl::Duration solving_duration = absl::Now() - time_before;
495 user_timer.Stop();
496 VLOG(1) << "Finished solving in GurobiSolveProto(), walltime = "
497 << solving_duration << ", usertime = " << user_timer.GetDuration();
498 response.mutable_solve_info()->set_solve_wall_time_seconds(
499 absl::ToDoubleSeconds(solving_duration));
500 response.mutable_solve_info()->set_solve_user_time_seconds(
501 absl::ToDoubleSeconds(user_timer.GetDuration()));
502
503 int optimization_status = 0;
505 GRBgetintattr(gurobi_model, GRB_INT_ATTR_STATUS, &optimization_status));
506 int solution_count = 0;
508 GRBgetintattr(gurobi_model, GRB_INT_ATTR_SOLCOUNT, &solution_count));
509 switch (optimization_status) {
510 case GRB_OPTIMAL:
511 response.set_status(MPSOLVER_OPTIMAL);
512 break;
513 case GRB_INF_OR_UNBD:
514 DLOG(INFO) << "Gurobi solve returned GRB_INF_OR_UNBD, which we treat as "
515 "INFEASIBLE even though it may mean UNBOUNDED.";
516 response.set_status_str(
517 "The model may actually be unbounded: Gurobi returned "
518 "GRB_INF_OR_UNBD");
519 ABSL_FALLTHROUGH_INTENDED;
520 case GRB_INFEASIBLE:
521 response.set_status(MPSOLVER_INFEASIBLE);
522 break;
523 case GRB_UNBOUNDED:
524 response.set_status(MPSOLVER_UNBOUNDED);
525 break;
526 default: {
527 if (solution_count > 0) {
528 response.set_status(MPSOLVER_FEASIBLE);
529 } else {
530 response.set_status(MPSOLVER_NOT_SOLVED);
531 response.set_status_str(
532 absl::StrFormat("Gurobi status code %d", optimization_status));
533 }
534 break;
535 }
536 }
537
538 if (solution_count > 0 && (response.status() == MPSOLVER_FEASIBLE ||
539 response.status() == MPSOLVER_OPTIMAL)) {
540 double objective_value = 0;
543 response.set_objective_value(objective_value);
544 double best_objective_bound = 0;
545 const int error = GRBgetdblattr(gurobi_model, GRB_DBL_ATTR_OBJBOUND,
546 &best_objective_bound);
547 if (response.status() == MPSOLVER_OPTIMAL &&
549 // If the presolve deletes all variables, there's no best bound.
550 response.set_best_objective_bound(objective_value);
551 } else {
553 response.set_best_objective_bound(best_objective_bound);
554 }
555
556 response.mutable_variable_value()->Resize(variable_size, 0);
558 GRBgetdblattrarray(gurobi_model, GRB_DBL_ATTR_X, 0, variable_size,
559 response.mutable_variable_value()->mutable_data()));
560 // NOTE, GurobiSolveProto() is exposed to external clients via MPSolver API,
561 // which assumes the solution values of integer variables are rounded to
562 // integer values.
563 auto round_values_of_integer_variables_fn =
564 [&](google::protobuf::RepeatedField<double>* values) {
565 for (int v = 0; v < variable_size; ++v) {
566 if (model.variable(v).is_integer()) {
567 (*values)[v] = std::round((*values)[v]);
568 }
569 }
570 };
571 round_values_of_integer_variables_fn(response.mutable_variable_value());
572 if (!has_integer_variables && model.general_constraint_size() == 0) {
573 response.mutable_dual_value()->Resize(model.constraint_size(), 0);
575 gurobi_model, GRB_DBL_ATTR_PI, 0, model.constraint_size(),
576 response.mutable_dual_value()->mutable_data()));
577 }
578 const int additional_solutions = std::min(
579 solution_count, std::min(request->populate_additional_solutions_up_to(),
580 std::numeric_limits<int32_t>::max() - 1) +
581 1);
582 for (int i = 1; i < additional_solutions; ++i) {
585 MPSolution* solution = response.add_additional_solutions();
586 solution->mutable_variable_value()->Resize(variable_size, 0);
587 double objective_value = 0;
590 solution->set_objective_value(objective_value);
592 gurobi_model, GRB_DBL_ATTR_XN, 0, variable_size,
593 solution->mutable_variable_value()->mutable_data()));
594 round_values_of_integer_variables_fn(solution->mutable_variable_value());
595 }
596 }
597#undef RETURN_IF_GUROBI_ERROR
598
599 return response;
600}
601
602} // namespace operations_research
IntegerValue size
#define ASSIGN_OR_RETURN(lhs, rexpr)
absl::Duration GetDuration() const
Definition timer.h:49
void Start()
When Start() is called multiple times, only the most recent is used.
Definition timer.h:32
void Stop()
Definition timer.h:40
SatParameters parameters
const std::string name
A name for logging purposes.
absl::Status status
Definition g_gurobi.cc:44
Gurobi * gurobi
Definition g_gurobi.cc:45
#define GRB_DBL_ATTR_START
#define GRB_ERROR_DATA_NOT_AVAILABLE
Definition environment.h:73
#define GRB_INT_ATTR_MODELSENSE
struct _GRBenv GRBenv
Definition environment.h:32
#define GRB_GREATER_EQUAL
#define GRB_OPTIMAL
#define GRB_INTEGER
#define GRB_DBL_ATTR_PI
#define GRB_DBL_ATTR_OBJVAL
#define GRB_DBL_ATTR_XN
#define GRB_CONTINUOUS
#define GRB_SOS_TYPE1
struct _GRBmodel GRBmodel
Definition environment.h:31
#define GRB_DBL_ATTR_OBJCON
#define GRB_INF_OR_UNBD
#define GRB_DBL_ATTR_X
#define GRB_INFEASIBLE
#define GRB_EQUAL
#define GRB_SOS_TYPE2
#define GRB_UNBOUNDED
#define GRB_INT_ATTR_STATUS
#define GRB_LESS_EQUAL
#define GRB_DBL_ATTR_POOLOBJVAL
#define GRB_INT_PAR_SOLUTIONNUMBER
#define GRB_INT_ATTR_SOLCOUNT
#define GRB_INT_PAR_OUTPUTFLAG
#define GRB_DBL_PAR_TIMELIMIT
#define GRB_DBL_ATTR_OBJBOUND
GRBmodel * model
#define RETURN_IF_GUROBI_ERROR(x)
double solution
In SWIG mode, we don't want anything besides these top-level includes.
std::function< int(GRBmodel *model, int numnz, int *cind, double *cval, char sense, double rhs, const char *constrname)> GRBaddconstr
std::function< void(GRBenv *env)> GRBfreeenv
std::function< int(GRBmodel *model, const char *attrname, double *valueP)> GRBgetdblattr
std::function< int(GRBmodel *model, const char *attrname, int first, int len, double *values)> GRBgetdblattrarray
std::function< int(GRBmodel *model, const char *attrname, double newvalue)> GRBsetdblattr
std::function< int(GRBmodel *model, int numvars, int numnz, int *vbeg, int *vind, double *vval, double *obj, double *lb, double *ub, char *vtype, char **varnames)> GRBaddvars
std::function< int(GRBmodel *model, int numlnz, int *lind, double *lval, int numqnz, int *qrow, int *qcol, double *qval, char sense, double rhs, const char *QCname)> GRBaddqconstr
std::function< int(GRBmodel *model, const char *attrname, int newvalue)> GRBsetintattr
std::function< int(GRBenv *env, const char *paramname, const char *value)> GRBsetparam
std::function< int(GRBenv *env, GRBmodel **modelP, const char *Pname, int numvars, double *obj, double *lb, double *ub, char *vtype, char **varnames)> GRBnewmodel
std::function< int(GRBmodel *model)> GRBupdatemodel
std::function< GRBenv *(GRBmodel *model)> GRBgetenv
std::function< int(GRBmodel *model)> GRBfreemodel
std::function< int(GRBenv *env, const char *paramname, int value)> GRBsetintparam
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars, double constant)> GRBaddgenconstrMin
std::function< int(GRBmodel *model, int numnz, int *cind, double *cval, double lower, double upper, const char *constrname)> GRBaddrangeconstr
absl::StatusOr< MPSolutionResponse > GurobiSolveProto(LazyMutableCopy< MPModelRequest > request, GRBenv *gurobi_env)
std::function< int(GRBmodel *model, const char *name, int binvar, int binval, int nvars, const int *vars, const double *vals, char sense, double rhs)> GRBaddgenconstrIndicator
std::optional< LazyMutableCopy< MPModelProto > > GetMPModelOrPopulateResponse(LazyMutableCopy< MPModelRequest > &request, MPSolutionResponse *response)
std::function< int(GRBmodel *model)> GRBoptimize
std::function< int(GRBmodel *model, int numqnz, int *qrow, int *qcol, double *qval)> GRBaddqpterms
std::function< int(GRBmodel *model, const char *name, int resvar, int argvar)> GRBaddgenconstrAbs
std::function< int(GRBmodel *model, const char *attrname, int *valueP)> GRBgetintattr
std::function< const char *(GRBenv *env)> GRBgeterrormsg
absl::StatusOr< GRBenv * > GetGurobiEnv()
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars)> GRBaddgenconstrAnd
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars)> GRBaddgenconstrOr
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars, double constant)> GRBaddgenconstrMax
std::function< int(GRBmodel *model, int numsos, int nummembers, int *types, int *beg, int *ind, double *weight)> GRBaddsos
absl::Status SetSolverSpecificParameters(absl::string_view parameters, GRBenv *gurobi)
std::function< int(GRBenv *env, const char *paramname, double value)> GRBsetdblparam
std::function< int(GRBmodel *model, const char *attrname, int element, double newvalue)> GRBsetdblattrelement
trees with all degrees equal w the current value of w
int line
double objective_value
The value objective_vector^T * (solution - center_point).