OR-Tools  8.2
synchronization.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 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 #if !defined(__PORTABLE_PLATFORM__)
17 #include "ortools/base/file.h"
19 #endif // __PORTABLE_PLATFORM__
20 
21 #include "absl/container/flat_hash_set.h"
22 #include "absl/random/random.h"
24 #include "ortools/base/stl_util.h"
27 #include "ortools/sat/integer.h"
29 #include "ortools/sat/model.h"
30 #include "ortools/sat/sat_base.h"
32 
33 ABSL_FLAG(bool, cp_model_dump_solutions, false,
34  "DEBUG ONLY. If true, all the intermediate solution will be dumped "
35  "under '\"FLAGS_cp_model_dump_prefix\" + \"solution_xxx.pb.txt\"'.");
36 
38  std::string, cp_model_load_debug_solution, "",
39  "DEBUG ONLY. When this is set to a non-empty file name, "
40  "we will interpret this as an internal solution which can be used for "
41  "debugging. For instance we use it to identify wrong cuts/reasons.");
42 
43 namespace operations_research {
44 namespace sat {
45 
47  const CpSolverResponse& response) {
48  // Note that the Add() method already applies mutex lock. So we don't need it
49  // here.
50  if (response.solution().empty()) return;
51 
52  // Add this solution to the pool.
54  solution.variable_values.assign(response.solution().begin(),
55  response.solution().end());
56  // For now we use the negated lower bound as the "internal objective" to
57  // prefer solution with an higher bound.
58  //
59  // Note: If the model doesn't have objective, the best_objective_bound is set
60  // to default value 0.
61  solution.rank = -response.best_objective_bound();
62 
63  Add(solution);
64 }
65 
67  std::vector<double> lp_solution) {
68  if (lp_solution.empty()) return;
69 
70  // Add this solution to the pool.
72  solution.variable_values = std::move(lp_solution);
73 
74  // We always prefer to keep the solution from the last synchronize batch.
75  absl::MutexLock mutex_lock(&mutex_);
76  solution.rank = -num_synchronization_;
77  AddInternal(solution);
78 }
79 
81  absl::MutexLock mutex_lock(&mutex_);
82  return !solutions_.empty();
83 }
84 
86  absl::MutexLock mutex_lock(&mutex_);
87  std::vector<double> solution;
88  if (solutions_.empty()) return solution;
89 
90  solution = std::move(solutions_.back());
91  solutions_.pop_back();
92  return solution;
93 }
94 
96  const std::vector<double>& lp_solution) {
97  absl::MutexLock mutex_lock(&mutex_);
98  solutions_.push_back(lp_solution);
99 }
100 
101 // TODO(user): Experiments and play with the num_solutions_to_keep parameter.
103  bool enumerate_all_solutions,
104  const CpModelProto* proto,
105  const WallTimer* wall_timer,
106  SharedTimeLimit* shared_time_limit)
107  : log_updates_(log_updates),
108  enumerate_all_solutions_(enumerate_all_solutions),
109  model_proto_(*proto),
110  wall_timer_(*wall_timer),
111  shared_time_limit_(shared_time_limit),
112  solutions_(/*num_solutions_to_keep=*/3) {}
113 
114 namespace {
115 
116 void LogNewSolution(const std::string& event_or_solution_count,
117  double time_in_seconds, double obj_best, double obj_lb,
118  double obj_ub, const std::string& solution_info) {
119  const std::string obj_next =
120  absl::StrFormat("next:[%.9g,%.9g]", obj_lb, obj_ub);
121  LOG(INFO) << absl::StrFormat("#%-5s %6.2fs best:%-5.9g %-15s %s",
122  event_or_solution_count, time_in_seconds,
123  obj_best, obj_next, solution_info);
124 }
125 
126 void LogNewSatSolution(const std::string& event_or_solution_count,
127  double time_in_seconds,
128  const std::string& solution_info) {
129  LOG(INFO) << absl::StrFormat("#%-5s %6.2fs %s", event_or_solution_count,
130  time_in_seconds, solution_info);
131 }
132 
133 } // namespace
134 
136  absl::MutexLock mutex_lock(&mutex_);
137  update_integral_on_each_change_ = set;
138 }
139 
141  absl::MutexLock mutex_lock(&mutex_);
142  UpdatePrimalIntegralInternal();
143 }
144 
145 void SharedResponseManager::UpdatePrimalIntegralInternal() {
146  if (!model_proto_.has_objective()) return;
147 
148  const double current_time = shared_time_limit_->GetElapsedDeterministicTime();
149  const double time_delta = current_time - last_primal_integral_time_stamp_;
150 
151  // We use the log of the absolute objective gap.
152  //
153  // Using the log should count no solution as just log(2*64) = 18, and
154  // otherwise just compare order of magnitude which seems nice. Also, It is
155  // more easy to compare the primal integral with the total time.
156  const CpObjectiveProto& obj = model_proto_.objective();
157  const double factor =
158  obj.scaling_factor() != 0.0 ? std::abs(obj.scaling_factor()) : 1.0;
159  const double bounds_delta = std::log(1 + factor * last_absolute_gap_);
160  primal_integral_ += time_delta * bounds_delta;
161 
162  // Update with new value.
163  last_primal_integral_time_stamp_ = current_time;
164  last_absolute_gap_ =
165  std::max(0.0, static_cast<double>(inner_objective_upper_bound_) -
166  static_cast<double>(inner_objective_lower_bound_));
167 }
168 
170  const SatParameters& parameters) {
171  absl::MutexLock mutex_lock(&mutex_);
172  if (!model_proto_.has_objective()) return;
173  absolute_gap_limit_ = parameters.absolute_gap_limit();
174  relative_gap_limit_ = parameters.relative_gap_limit();
175 }
176 
177 void SharedResponseManager::TestGapLimitsIfNeeded() {
178  // This is called on each internal limit change, so it is a good place to
179  // update the integral. Note that this is not called at the end of the search
180  // though.
181  if (update_integral_on_each_change_) UpdatePrimalIntegralInternal();
182 
183  if (absolute_gap_limit_ == 0 && relative_gap_limit_ == 0) return;
184  if (best_solution_objective_value_ >= kMaxIntegerValue) return;
185  if (inner_objective_lower_bound_ <= kMinIntegerValue) return;
186 
187  const CpObjectiveProto& obj = model_proto_.objective();
188  const double user_best =
189  ScaleObjectiveValue(obj, best_solution_objective_value_);
190  const double user_bound =
191  ScaleObjectiveValue(obj, inner_objective_lower_bound_);
192  const double gap = std::abs(user_best - user_bound);
193  if (gap <= absolute_gap_limit_) {
194  LOG_IF(INFO, log_updates_)
195  << "Absolute gap limit of " << absolute_gap_limit_ << " reached.";
196  best_response_.set_status(CpSolverStatus::OPTIMAL);
197 
198  // Note(user): Some code path in single-thread assumes that the problem
199  // can only be solved when they have proven infeasibility and do not check
200  // the ProblemIsSolved() method. So we force a stop here.
201  shared_time_limit_->Stop();
202  }
203  if (gap / std::max(1.0, std::abs(user_best)) < relative_gap_limit_) {
204  LOG_IF(INFO, log_updates_)
205  << "Relative gap limit of " << relative_gap_limit_ << " reached.";
206  best_response_.set_status(CpSolverStatus::OPTIMAL);
207 
208  // Same as above.
209  shared_time_limit_->Stop();
210  }
211 }
212 
214  const std::string& worker_info, IntegerValue lb, IntegerValue ub) {
215  absl::MutexLock mutex_lock(&mutex_);
216  CHECK(model_proto_.has_objective());
217 
218  // The problem is already solved!
219  //
220  // TODO(user): A thread might not be notified right away that the new bounds
221  // that it is pushing make the problem infeasible. Fix that. For now we just
222  // abort early here to avoid logging the "#Done" message multiple times.
223  if (inner_objective_lower_bound_ > inner_objective_upper_bound_) {
224  return;
225  }
226 
227  const bool change =
228  (lb > inner_objective_lower_bound_ || ub < inner_objective_upper_bound_);
229  if (lb > inner_objective_lower_bound_) {
230  // When the improving problem is infeasible, it is possible to report
231  // arbitrary high inner_objective_lower_bound_. We make sure it never cross
232  // the current best solution, so that we always report globablly valid lower
233  // bound.
234  DCHECK_LE(inner_objective_upper_bound_, best_solution_objective_value_);
235  inner_objective_lower_bound_ =
236  std::min(best_solution_objective_value_, lb.value());
237  }
238  if (ub < inner_objective_upper_bound_) {
239  inner_objective_upper_bound_ = ub.value();
240  }
241  if (inner_objective_lower_bound_ > inner_objective_upper_bound_) {
242  if (best_response_.status() == CpSolverStatus::FEASIBLE ||
243  best_response_.status() == CpSolverStatus::OPTIMAL) {
244  best_response_.set_status(CpSolverStatus::OPTIMAL);
245  } else {
246  best_response_.set_status(CpSolverStatus::INFEASIBLE);
247  }
248  if (update_integral_on_each_change_) UpdatePrimalIntegralInternal();
249  if (log_updates_) LogNewSatSolution("Done", wall_timer_.Get(), worker_info);
250  return;
251  }
252  if (log_updates_ && change) {
253  const CpObjectiveProto& obj = model_proto_.objective();
254  const double best =
255  ScaleObjectiveValue(obj, best_solution_objective_value_);
256  double new_lb = ScaleObjectiveValue(obj, inner_objective_lower_bound_);
257  double new_ub = ScaleObjectiveValue(obj, inner_objective_upper_bound_);
258  if (model_proto_.objective().scaling_factor() < 0) {
259  std::swap(new_lb, new_ub);
260  }
261  LogNewSolution("Bound", wall_timer_.Get(), best, new_lb, new_ub,
262  worker_info);
263  }
264  if (change) TestGapLimitsIfNeeded();
265 }
266 
267 // Invariant: the status always start at UNKNOWN and can only evolve as follow:
268 // UNKNOWN -> FEASIBLE -> OPTIMAL
269 // UNKNOWN -> INFEASIBLE
271  const std::string& worker_info) {
272  absl::MutexLock mutex_lock(&mutex_);
273  if (best_response_.status() == CpSolverStatus::FEASIBLE ||
274  best_response_.status() == CpSolverStatus::OPTIMAL) {
275  // We also use this status to indicate that we enumerated all solutions to
276  // a feasible problem.
277  best_response_.set_status(CpSolverStatus::OPTIMAL);
278  if (!model_proto_.has_objective()) {
279  best_response_.set_all_solutions_were_found(true);
280  }
281 
282  // We just proved that the best solution cannot be improved uppon, so we
283  // have a new lower bound.
284  inner_objective_lower_bound_ = best_solution_objective_value_;
285  if (update_integral_on_each_change_) UpdatePrimalIntegralInternal();
286  } else {
287  CHECK_EQ(num_solutions_, 0);
288  best_response_.set_status(CpSolverStatus::INFEASIBLE);
289  }
290  if (log_updates_) LogNewSatSolution("Done", wall_timer_.Get(), worker_info);
291 }
292 
293 void SharedResponseManager::AddUnsatCore(const std::vector<int>& core) {
294  absl::MutexLock mutex_lock(&mutex_);
295  best_response_.clear_sufficient_assumptions_for_infeasibility();
296  for (const int ref : core) {
297  best_response_.add_sufficient_assumptions_for_infeasibility(ref);
298  }
299 }
300 
302  absl::MutexLock mutex_lock(&mutex_);
303  return IntegerValue(inner_objective_lower_bound_);
304 }
305 
307  absl::MutexLock mutex_lock(&mutex_);
308  return IntegerValue(inner_objective_upper_bound_);
309 }
310 
312  absl::MutexLock mutex_lock(&mutex_);
313  synchronized_inner_objective_lower_bound_ =
314  IntegerValue(inner_objective_lower_bound_);
315  synchronized_inner_objective_upper_bound_ =
316  IntegerValue(inner_objective_upper_bound_);
317 }
318 
320  absl::MutexLock mutex_lock(&mutex_);
321  return synchronized_inner_objective_lower_bound_;
322 }
323 
325  absl::MutexLock mutex_lock(&mutex_);
326  return synchronized_inner_objective_upper_bound_;
327 }
328 
330  absl::MutexLock mutex_lock(&mutex_);
331  return IntegerValue(best_solution_objective_value_);
332 }
333 
335  absl::MutexLock mutex_lock(&mutex_);
336  return primal_integral_;
337 }
338 
340  std::function<void(const CpSolverResponse&)> callback) {
341  absl::MutexLock mutex_lock(&mutex_);
342  const int id = next_callback_id_++;
343  callbacks_.emplace_back(id, std::move(callback));
344  return id;
345 }
346 
348  absl::MutexLock mutex_lock(&mutex_);
349  for (int i = 0; i < callbacks_.size(); ++i) {
350  if (callbacks_[i].first == callback_id) {
351  callbacks_.erase(callbacks_.begin() + i);
352  return;
353  }
354  }
355  LOG(DFATAL) << "Callback id " << callback_id << " not registered.";
356 }
357 
359  absl::MutexLock mutex_lock(&mutex_);
360  FillObjectiveValuesInBestResponse();
361  return best_response_;
362 }
363 
364 void SharedResponseManager::FillObjectiveValuesInBestResponse() {
365  if (!model_proto_.has_objective()) return;
366  const CpObjectiveProto& obj = model_proto_.objective();
367 
368  if (best_response_.status() == CpSolverStatus::INFEASIBLE) {
369  best_response_.clear_objective_value();
370  best_response_.clear_best_objective_bound();
371  return;
372  }
373 
374  // Set the objective value.
375  // If we don't have any solution, we use our inner bound.
376  if (best_response_.status() == CpSolverStatus::UNKNOWN) {
377  best_response_.set_objective_value(
378  ScaleObjectiveValue(obj, inner_objective_upper_bound_));
379  } else {
380  best_response_.set_objective_value(
381  ScaleObjectiveValue(obj, best_solution_objective_value_));
382  }
383 
384  // Update the best bound in the response.
385  best_response_.set_best_objective_bound(
386  ScaleObjectiveValue(obj, inner_objective_lower_bound_));
387 
388  // Update the primal integral.
389  best_response_.set_primal_integral(primal_integral_);
390 }
391 
392 void SharedResponseManager::NewSolution(const CpSolverResponse& response,
393  Model* model) {
394  absl::MutexLock mutex_lock(&mutex_);
395 
396  if (model_proto_.has_objective()) {
397  const int64 objective_value =
398  ComputeInnerObjective(model_proto_.objective(), response);
399 
400  // Add this solution to the pool, even if it is not improving.
401  if (!response.solution().empty()) {
403  solution.variable_values.assign(response.solution().begin(),
404  response.solution().end());
405  solution.rank = objective_value;
406  solutions_.Add(solution);
407  }
408 
409  // Ignore any non-strictly improving solution.
410  if (objective_value > inner_objective_upper_bound_) return;
411 
412  // Our inner_objective_lower_bound_ should be a globaly valid bound, until
413  // the problem become infeasible (i.e the lb > ub) in which case the bound
414  // is no longer globally valid. Here, because we have a strictly improving
415  // solution, we shouldn't be in the infeasible setting yet.
416  DCHECK_GE(objective_value, inner_objective_lower_bound_);
417 
418  DCHECK_LT(objective_value, best_solution_objective_value_);
419  best_solution_objective_value_ = objective_value;
420 
421  // Update the new bound.
422  inner_objective_upper_bound_ = objective_value - 1;
423  }
424 
425  // Note that the objective will be filled by
426  // FillObjectiveValuesInBestResponse().
427  if (!model_proto_.has_objective() && !enumerate_all_solutions_) {
428  best_response_.set_status(CpSolverStatus::OPTIMAL);
429  } else {
430  best_response_.set_status(CpSolverStatus::FEASIBLE);
431  }
432 
433  best_response_.set_solution_info(response.solution_info());
434  *best_response_.mutable_solution() = response.solution();
435  *best_response_.mutable_solution_lower_bounds() =
436  response.solution_lower_bounds();
437  *best_response_.mutable_solution_upper_bounds() =
438  response.solution_upper_bounds();
439 
440  // Mark model as OPTIMAL if the inner bound crossed.
441  if (model_proto_.has_objective() &&
442  inner_objective_lower_bound_ > inner_objective_upper_bound_) {
443  best_response_.set_status(CpSolverStatus::OPTIMAL);
444  }
445 
446  // Logging.
447  ++num_solutions_;
448  if (log_updates_) {
449  std::string solution_info = response.solution_info();
450  if (model != nullptr) {
451  const int64 num_bool = model->Get<Trail>()->NumVariables();
452  const int64 num_fixed = model->Get<SatSolver>()->NumFixedVariables();
453  absl::StrAppend(&solution_info, " fixed_bools:", num_fixed, "/",
454  num_bool);
455  }
456 
457  if (model_proto_.has_objective()) {
458  const CpObjectiveProto& obj = model_proto_.objective();
459  const double best =
460  ScaleObjectiveValue(obj, best_solution_objective_value_);
461  double lb = ScaleObjectiveValue(obj, inner_objective_lower_bound_);
462  double ub = ScaleObjectiveValue(obj, inner_objective_upper_bound_);
463  if (model_proto_.objective().scaling_factor() < 0) {
464  std::swap(lb, ub);
465  }
466  LogNewSolution(absl::StrCat(num_solutions_), wall_timer_.Get(), best, lb,
467  ub, solution_info);
468  } else {
469  LogNewSatSolution(absl::StrCat(num_solutions_), wall_timer_.Get(),
470  solution_info);
471  }
472  }
473 
474  // Call callbacks.
475  // Note that we cannot call function that try to get the mutex_ here.
476  TestGapLimitsIfNeeded();
477  if (!callbacks_.empty()) {
478  FillObjectiveValuesInBestResponse();
479  SetStatsFromModelInternal(model);
480  for (const auto& pair : callbacks_) {
481  pair.second(best_response_);
482  }
483  }
484 
485 #if !defined(__PORTABLE_PLATFORM__)
486  // We protect solution dumping with log_updates as LNS subsolvers share
487  // another solution manager, and we do not want to dump those.
488  if (absl::GetFlag(FLAGS_cp_model_dump_solutions) && log_updates_) {
489  const std::string file =
490  absl::StrCat(dump_prefix_, "solution_", num_solutions_, ".pbtxt");
491  LOG(INFO) << "Dumping solution to '" << file << "'.";
492  CHECK_OK(file::SetTextProto(file, best_response_, file::Defaults()));
493  }
494 #endif // __PORTABLE_PLATFORM__
495 }
496 
498 #if !defined(__PORTABLE_PLATFORM__)
499  if (absl::GetFlag(FLAGS_cp_model_load_debug_solution).empty()) return;
500  if (model->Get<DebugSolution>() != nullptr) return; // Already loaded.
501 
502  CpSolverResponse response;
503  LOG(INFO) << "Reading solution from '"
504  << absl::GetFlag(FLAGS_cp_model_load_debug_solution) << "'.";
505  CHECK_OK(file::GetTextProto(absl::GetFlag(FLAGS_cp_model_load_debug_solution),
506  &response, file::Defaults()));
507 
508  const auto& mapping = *model->GetOrCreate<CpModelMapping>();
509  auto& debug_solution = *model->GetOrCreate<DebugSolution>();
510  debug_solution.resize(
511  model->GetOrCreate<IntegerTrail>()->NumIntegerVariables().value());
512  for (int i = 0; i < response.solution().size(); ++i) {
513  if (!mapping.IsInteger(i)) continue;
514  const IntegerVariable var = mapping.Integer(i);
515  debug_solution[var] = response.solution(i);
516  debug_solution[NegationOf(var)] = -response.solution(i);
517  }
518 
519  // The objective variable is usually not part of the proto, but it is still
520  // nice to have it, so we recompute it here.
521  auto* objective_def = model->Get<ObjectiveDefinition>();
522  if (objective_def == nullptr) return;
523 
524  const IntegerVariable objective_var = objective_def->objective_var;
525  const int64 objective_value =
526  ComputeInnerObjective(model_proto_.objective(), response);
527  debug_solution[objective_var] = objective_value;
528  debug_solution[NegationOf(objective_var)] = -objective_value;
529 #endif // __PORTABLE_PLATFORM__
530 }
531 
533  absl::MutexLock mutex_lock(&mutex_);
534  SetStatsFromModelInternal(model);
535 }
536 
537 void SharedResponseManager::SetStatsFromModelInternal(Model* model) {
538  if (model == nullptr) return;
539  auto* sat_solver = model->GetOrCreate<SatSolver>();
540  auto* integer_trail = model->Get<IntegerTrail>();
541  best_response_.set_num_booleans(sat_solver->NumVariables());
542  best_response_.set_num_branches(sat_solver->num_branches());
543  best_response_.set_num_conflicts(sat_solver->num_failures());
544  best_response_.set_num_binary_propagations(sat_solver->num_propagations());
545  best_response_.set_num_restarts(sat_solver->num_restarts());
546  best_response_.set_num_integer_propagations(
547  integer_trail == nullptr ? 0 : integer_trail->num_enqueues());
548  auto* time_limit = model->Get<TimeLimit>();
549  best_response_.set_wall_time(time_limit->GetElapsedTime());
550  best_response_.set_deterministic_time(
551  time_limit->GetElapsedDeterministicTime());
552 
553  int64 num_lp_iters = 0;
554  for (const LinearProgrammingConstraint* lp :
555  *model->GetOrCreate<LinearProgrammingConstraintCollection>()) {
556  num_lp_iters += lp->total_num_simplex_iterations();
557  }
558  best_response_.set_num_lp_iterations(num_lp_iters);
559 }
560 
562  absl::MutexLock mutex_lock(&mutex_);
563  return best_response_.status() == CpSolverStatus::OPTIMAL ||
564  best_response_.status() == CpSolverStatus::INFEASIBLE;
565 }
566 
568  : num_variables_(model_proto.variables_size()),
569  model_proto_(model_proto),
570  lower_bounds_(num_variables_, kint64min),
571  upper_bounds_(num_variables_, kint64max),
572  synchronized_lower_bounds_(num_variables_, kint64min),
573  synchronized_upper_bounds_(num_variables_, kint64max) {
574  changed_variables_since_last_synchronize_.ClearAndResize(num_variables_);
575  for (int i = 0; i < num_variables_; ++i) {
576  lower_bounds_[i] = model_proto.variables(i).domain(0);
577  const int domain_size = model_proto.variables(i).domain_size();
578  upper_bounds_[i] = model_proto.variables(i).domain(domain_size - 1);
579  synchronized_lower_bounds_[i] = lower_bounds_[i];
580  synchronized_upper_bounds_[i] = upper_bounds_[i];
581  }
582 }
583 
585  const CpModelProto& model_proto, const std::string& worker_name,
586  const std::vector<int>& variables,
587  const std::vector<int64>& new_lower_bounds,
588  const std::vector<int64>& new_upper_bounds) {
589  CHECK_EQ(variables.size(), new_lower_bounds.size());
590  CHECK_EQ(variables.size(), new_upper_bounds.size());
591  int num_improvements = 0;
592 
593  absl::MutexLock mutex_lock(&mutex_);
594  for (int i = 0; i < variables.size(); ++i) {
595  const int var = variables[i];
596  if (var >= num_variables_) continue;
597  const int64 old_lb = lower_bounds_[var];
598  const int64 old_ub = upper_bounds_[var];
599  const int64 new_lb = new_lower_bounds[i];
600  const int64 new_ub = new_upper_bounds[i];
601  const bool changed_lb = new_lb > old_lb;
602  const bool changed_ub = new_ub < old_ub;
603  CHECK_GE(var, 0);
604  if (!changed_lb && !changed_ub) continue;
605 
606  if (changed_lb) {
607  lower_bounds_[var] = new_lb;
608  }
609  if (changed_ub) {
610  upper_bounds_[var] = new_ub;
611  }
612  changed_variables_since_last_synchronize_.Set(var);
613  num_improvements++;
614  }
615  // TODO(user): Display number of bound improvements cumulatively per
616  // workers at the end of the search.
617  if (num_improvements > 0) {
618  VLOG(2) << worker_name << " exports " << num_improvements
619  << " modifications";
620  }
621 }
622 
624  absl::MutexLock mutex_lock(&mutex_);
625  for (const int var :
626  changed_variables_since_last_synchronize_.PositionsSetAtLeastOnce()) {
627  synchronized_lower_bounds_[var] = lower_bounds_[var];
628  synchronized_upper_bounds_[var] = upper_bounds_[var];
629  for (int j = 0; j < id_to_changed_variables_.size(); ++j) {
630  id_to_changed_variables_[j].Set(var);
631  }
632  }
633  changed_variables_since_last_synchronize_.ClearAll();
634 }
635 
637  absl::MutexLock mutex_lock(&mutex_);
638  const int id = id_to_changed_variables_.size();
639  id_to_changed_variables_.resize(id + 1);
640  id_to_changed_variables_[id].ClearAndResize(num_variables_);
641  for (int var = 0; var < num_variables_; ++var) {
642  const int64 lb = model_proto_.variables(var).domain(0);
643  const int domain_size = model_proto_.variables(var).domain_size();
644  const int64 ub = model_proto_.variables(var).domain(domain_size - 1);
645  if (lb != synchronized_lower_bounds_[var] ||
646  ub != synchronized_upper_bounds_[var]) {
647  id_to_changed_variables_[id].Set(var);
648  }
649  }
650  return id;
651 }
652 
654  int id, std::vector<int>* variables, std::vector<int64>* new_lower_bounds,
655  std::vector<int64>* new_upper_bounds) {
656  variables->clear();
657  new_lower_bounds->clear();
658  new_upper_bounds->clear();
659 
660  absl::MutexLock mutex_lock(&mutex_);
661  for (const int var : id_to_changed_variables_[id].PositionsSetAtLeastOnce()) {
662  variables->push_back(var);
663  new_lower_bounds->push_back(synchronized_lower_bounds_[var]);
664  new_upper_bounds->push_back(synchronized_upper_bounds_[var]);
665  }
666  id_to_changed_variables_[id].ClearAll();
667 }
668 
669 } // namespace sat
670 } // namespace operations_research
int64 min
Definition: alldiff_cst.cc:138
int64 max
Definition: alldiff_cst.cc:139
#define LOG_IF(severity, condition)
Definition: base/logging.h:479
#define CHECK(condition)
Definition: base/logging.h:495
#define DCHECK_LE(val1, val2)
Definition: base/logging.h:887
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:697
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
#define CHECK_OK(x)
Definition: base/logging.h:40
#define DCHECK_GE(val1, val2)
Definition: base/logging.h:889
#define DCHECK_LT(val1, val2)
Definition: base/logging.h:888
#define LOG(severity)
Definition: base/logging.h:420
#define VLOG(verboselevel)
Definition: base/logging.h:978
double Get() const
Definition: timer.h:45
double GetElapsedDeterministicTime() const
Definition: time_limit.h:383
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
IntegerVariable NumIntegerVariables() const
Definition: integer.h:565
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
SharedBoundsManager(const CpModelProto &model_proto)
void GetChangedBounds(int id, std::vector< int > *variables, std::vector< int64 > *new_lower_bounds, std::vector< int64 > *new_upper_bounds)
void ReportPotentialNewBounds(const CpModelProto &model_proto, const std::string &worker_name, const std::vector< int > &variables, const std::vector< int64 > &new_lower_bounds, const std::vector< int64 > &new_upper_bounds)
void AddNewSolution(const std::vector< double > &lp_solution)
void NewLPSolution(std::vector< double > lp_solution)
void NewRelaxationSolution(const CpSolverResponse &response)
SharedResponseManager(bool log_updates, bool enumerate_all_solutions, const CpModelProto *proto, const WallTimer *wall_timer, SharedTimeLimit *shared_time_limit)
void UpdateInnerObjectiveBounds(const std::string &worker_info, IntegerValue lb, IntegerValue ub)
void NewSolution(const CpSolverResponse &response, Model *model)
void NotifyThatImprovingProblemIsInfeasible(const std::string &worker_info)
void AddUnsatCore(const std::vector< int > &core)
void SetGapLimitsFromParameters(const SatParameters &parameters)
int AddSolutionCallback(std::function< void(const CpSolverResponse &)> callback)
void AddInternal(const Solution &solution) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_)
SatParameters parameters
CpModelProto proto
CpModelProto const * model_proto
SharedResponseManager * response
SharedTimeLimit * time_limit
WallTimer * wall_timer
IntVar * var
Definition: expr_array.cc:1858
GRBmodel * model
MPCallback * callback
static const int64 kint64max
int64_t int64
static const int64 kint64min
const int INFO
Definition: log_severity.h:31
Definition: file.cc:141
int Defaults()
Definition: base/file.h:119
absl::Status GetTextProto(const absl::string_view &filename, google::protobuf::Message *proto, int flags)
Definition: file.cc:275
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
Definition: file.cc:285
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
int64 ComputeInnerObjective(const CpObjectiveProto &objective, const CpSolverResponse &response)
double ScaleObjectiveValue(const CpObjectiveProto &proto, int64 value)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Definition: integer.cc:27
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
ABSL_FLAG(bool, cp_model_dump_solutions, false, "DEBUG ONLY. If true, all the intermediate solution will be dumped " "under '\"FLAGS_cp_model_dump_prefix\" + \"solution_xxx.pb.txt\"'.")