OR-Tools  9.1
timetable.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 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 #include "ortools/sat/timetable.h"
15 
16 #include <algorithm>
17 #include <cstdint>
18 #include <functional>
19 #include <memory>
20 
21 #include "ortools/base/int_type.h"
22 #include "ortools/base/logging.h"
23 #include "ortools/util/sort.h"
24 
25 namespace operations_research {
26 namespace sat {
27 
28 void AddReservoirConstraint(std::vector<AffineExpression> times,
29  std::vector<IntegerValue> deltas,
30  std::vector<Literal> presences, int64_t min_level,
31  int64_t max_level, Model* model) {
32  // We only create a side if it can fail.
33  IntegerValue min_possible(0);
34  IntegerValue max_possible(0);
35  for (const IntegerValue d : deltas) {
36  if (d > 0) {
37  max_possible += d;
38  } else {
39  min_possible += d;
40  }
41  }
42  if (max_possible > max_level) {
43  model->TakeOwnership(new ReservoirTimeTabling(
44  times, deltas, presences, IntegerValue(max_level), model));
45  }
46  if (min_possible < min_level) {
47  for (IntegerValue& ref : deltas) ref = -ref;
48  model->TakeOwnership(new ReservoirTimeTabling(
49  times, deltas, presences, IntegerValue(-min_level), model));
50  }
51 }
52 
54  const std::vector<AffineExpression>& times,
55  const std::vector<IntegerValue>& deltas,
56  const std::vector<Literal>& presences, IntegerValue capacity, Model* model)
57  : times_(times),
58  deltas_(deltas),
59  presences_(presences),
60  capacity_(capacity),
61  assignment_(model->GetOrCreate<Trail>()->Assignment()),
62  integer_trail_(model->GetOrCreate<IntegerTrail>()) {
63  auto* watcher = model->GetOrCreate<GenericLiteralWatcher>();
64  const int id = watcher->Register(this);
65  const int num_events = times.size();
66  for (int e = 0; e < num_events; e++) {
67  if (deltas_[e] > 0) {
68  watcher->WatchUpperBound(times_[e].var, id);
69  watcher->WatchLiteral(presences_[e], id);
70  }
71  if (deltas_[e] < 0) {
72  watcher->WatchLowerBound(times_[e].var, id);
73  watcher->WatchLiteral(presences_[e].Negated(), id);
74  }
75  }
76  watcher->NotifyThatPropagatorMayNotReachFixedPointInOnePass(id);
77 }
78 
80  const int num_events = times_.size();
81  if (!BuildProfile()) return false;
82  for (int e = 0; e < num_events; e++) {
83  if (assignment_.LiteralIsFalse(presences_[e])) continue;
84 
85  // For positive delta, we can maybe increase the min.
86  if (deltas_[e] > 0 && !TryToIncreaseMin(e)) return false;
87 
88  // For negative delta, we can maybe decrease the max.
89  if (deltas_[e] < 0 && !TryToDecreaseMax(e)) return false;
90  }
91  return true;
92 }
93 
94 // We compute the lowest possible profile at time t.
95 //
96 // TODO(user): If we have precedences between events, we should be able to do
97 // more.
98 bool ReservoirTimeTabling::BuildProfile() {
99  // Starts by copying the "events" in the profile and sort them by time.
100  profile_.clear();
101  const int num_events = times_.size();
102  profile_.emplace_back(kMinIntegerValue, IntegerValue(0)); // Sentinel.
103  for (int e = 0; e < num_events; e++) {
104  if (deltas_[e] > 0) {
105  // Only consider present event for positive delta.
106  if (!assignment_.LiteralIsTrue(presences_[e])) continue;
107  const IntegerValue ub = integer_trail_->UpperBound(times_[e]);
108  profile_.push_back({ub, deltas_[e]});
109  } else if (deltas_[e] < 0) {
110  // Only consider non-absent event for negative delta.
111  if (assignment_.LiteralIsFalse(presences_[e])) continue;
112  profile_.push_back({integer_trail_->LowerBound(times_[e]), deltas_[e]});
113  }
114  }
115  profile_.emplace_back(kMaxIntegerValue, IntegerValue(0)); // Sentinel.
116  std::sort(profile_.begin(), profile_.end());
117 
118  // Accumulate delta and collapse entries.
119  int last = 0;
120  for (const ProfileRectangle& rect : profile_) {
121  if (rect.start == profile_[last].start) {
122  profile_[last].height += rect.height;
123  } else {
124  ++last;
125  profile_[last].start = rect.start;
126  profile_[last].height = rect.height + profile_[last - 1].height;
127  }
128  }
129  profile_.resize(last + 1);
130 
131  // Conflict?
132  for (const ProfileRectangle& rect : profile_) {
133  if (rect.height <= capacity_) continue;
134  FillReasonForProfileAtGivenTime(rect.start);
135  return integer_trail_->ReportConflict(literal_reason_, integer_reason_);
136  }
137 
138  return true;
139 }
140 
141 // TODO(user): Minimize with how high the profile needs to be. We can also
142 // remove from the reason the absence of a negative event provided that the
143 // level zero min of the event is greater than t anyway.
144 //
145 // TODO(user): Make sure the code work with fixed time since pushing always
146 // true/false literal to the reason is not completely supported.
147 void ReservoirTimeTabling::FillReasonForProfileAtGivenTime(
148  IntegerValue t, int event_to_ignore) {
149  integer_reason_.clear();
150  literal_reason_.clear();
151  const int num_events = times_.size();
152  for (int e = 0; e < num_events; e++) {
153  if (e == event_to_ignore) continue;
154  if (deltas_[e] > 0) {
155  if (!assignment_.LiteralIsTrue(presences_[e])) continue;
156  if (integer_trail_->UpperBound(times_[e]) > t) continue;
157  integer_reason_.push_back(times_[e].LowerOrEqual(t));
158  literal_reason_.push_back(presences_[e].Negated());
159  } else if (deltas_[e] < 0) {
160  if (assignment_.LiteralIsFalse(presences_[e])) {
161  literal_reason_.push_back(presences_[e]);
162  } else if (integer_trail_->LowerBound(times_[e]) > t) {
163  integer_reason_.push_back(times_[e].GreaterOrEqual(t + 1));
164  }
165  }
166  }
167 }
168 
169 // Note that a negative event will always be in the profile, even if its
170 // presence is still not settled.
171 bool ReservoirTimeTabling::TryToDecreaseMax(int event) {
172  CHECK_LT(deltas_[event], 0);
173  const IntegerValue start = integer_trail_->LowerBound(times_[event]);
174  const IntegerValue end = integer_trail_->UpperBound(times_[event]);
175 
176  // We already tested for conflict in BuildProfile().
177  if (start == end) return true;
178 
179  // Find the profile rectangle that overlaps the start of the given event.
180  // The sentinel prevents out of bound exceptions.
181  DCHECK(std::is_sorted(profile_.begin(), profile_.end()));
182  int rec_id =
183  std::upper_bound(profile_.begin(), profile_.end(), start,
184  [&](IntegerValue value, const ProfileRectangle& rect) {
185  return value < rect.start;
186  }) -
187  profile_.begin();
188  --rec_id;
189 
190  bool push = false;
191  IntegerValue new_end = end;
192  for (; profile_[rec_id].start < end; ++rec_id) {
193  if (profile_[rec_id].height - deltas_[event] > capacity_) {
194  new_end = profile_[rec_id].start;
195  push = true;
196  break;
197  }
198  }
199  if (!push) return true;
200 
201  // The reason is simply why the capacity at new_end (without the event)
202  // would overflow.
203  FillReasonForProfileAtGivenTime(new_end, event);
204 
205  // Note(user): I don't think this is possible since it would have been
206  // detected at profile construction, but then, since the bound might have been
207  // updated, better be defensive.
208  if (new_end < start) {
209  integer_reason_.push_back(times_[event].GreaterOrEqual(new_end + 1));
210  return integer_trail_->ReportConflict(literal_reason_, integer_reason_);
211  }
212 
213  // First, the task MUST be present, otherwise we have a conflict.
214  //
215  // TODO(user): We actually need to look after 'end' to potentially push the
216  // presence in more situation.
217  if (!assignment_.LiteralIsTrue(presences_[event])) {
218  integer_trail_->EnqueueLiteral(presences_[event], literal_reason_,
219  integer_reason_);
220  }
221 
222  // Push new_end too. Note that we don't need the presence reason.
223  return integer_trail_->Enqueue(times_[event].LowerOrEqual(new_end),
224  literal_reason_, integer_reason_);
225 }
226 
227 bool ReservoirTimeTabling::TryToIncreaseMin(int event) {
228  CHECK_GT(deltas_[event], 0);
229  const IntegerValue start = integer_trail_->LowerBound(times_[event]);
230  const IntegerValue end = integer_trail_->UpperBound(times_[event]);
231 
232  // We already tested for conflict in BuildProfile().
233  if (start == end) return true;
234 
235  // Find the profile rectangle containing the end of the given event.
236  // The sentinel prevents out of bound exceptions.
237  //
238  // TODO(user): If the task is no present, we should actually look at the
239  // maximum profile after end to maybe push its absence.
240  DCHECK(std::is_sorted(profile_.begin(), profile_.end()));
241  int rec_id =
242  std::upper_bound(profile_.begin(), profile_.end(), end,
243  [&](IntegerValue value, const ProfileRectangle& rect) {
244  return value < rect.start;
245  }) -
246  profile_.begin();
247  --rec_id;
248 
249  bool push = false;
250  IntegerValue new_start = start;
251  if (profile_[rec_id].height + deltas_[event] > capacity_) {
252  if (!assignment_.LiteralIsTrue(presences_[event])) {
253  // Push to false since it wasn't part of the profile and cannot fit.
254  push = true;
255  new_start = end + 1;
256  } else if (profile_[rec_id].start < end) {
257  // It must be at end in this case.
258  push = true;
259  new_start = end;
260  }
261  }
262  if (!push) {
263  for (; profile_[rec_id].start > start; --rec_id) {
264  if (profile_[rec_id - 1].height + deltas_[event] > capacity_) {
265  push = true;
266  new_start = profile_[rec_id].start;
267  break;
268  }
269  }
270  }
271  if (!push) return true;
272 
273  // The reason is simply the capacity at new_start - 1;
274  FillReasonForProfileAtGivenTime(new_start - 1, event);
275  return integer_trail_->ConditionalEnqueue(
276  presences_[event], times_[event].GreaterOrEqual(new_start),
277  &literal_reason_, &integer_reason_);
278 }
279 
281  const std::vector<AffineExpression>& demands, AffineExpression capacity,
282  IntegerTrail* integer_trail, SchedulingConstraintHelper* helper)
283  : num_tasks_(helper->NumTasks()),
284  demands_(demands),
285  capacity_(capacity),
286  integer_trail_(integer_trail),
287  helper_(helper) {
288  // Each task may create at most two profile rectangles. Such pattern appear if
289  // the profile is shaped like the Hanoi tower. The additional space is for
290  // both extremities and the sentinels.
291  profile_.reserve(2 * num_tasks_ + 4);
292 
293  // Reversible set of tasks to consider for propagation.
294  forward_num_tasks_to_sweep_ = num_tasks_;
295  forward_tasks_to_sweep_.resize(num_tasks_);
296  backward_num_tasks_to_sweep_ = num_tasks_;
297  backward_tasks_to_sweep_.resize(num_tasks_);
298 
299  num_profile_tasks_ = 0;
300  profile_tasks_.resize(num_tasks_);
301  positions_in_profile_tasks_.resize(num_tasks_);
302 
303  // Reversible bounds and starting height of the profile.
304  starting_profile_height_ = IntegerValue(0);
305 
306  for (int t = 0; t < num_tasks_; ++t) {
307  forward_tasks_to_sweep_[t] = t;
308  backward_tasks_to_sweep_[t] = t;
309  profile_tasks_[t] = t;
310  positions_in_profile_tasks_[t] = t;
311  }
312 }
313 
315  const int id = watcher->Register(this);
316  helper_->WatchAllTasks(id, watcher);
317  watcher->WatchUpperBound(capacity_.var, id);
318  for (int t = 0; t < num_tasks_; t++) {
319  watcher->WatchLowerBound(demands_[t].var, id);
320  }
321  watcher->RegisterReversibleInt(id, &forward_num_tasks_to_sweep_);
322  watcher->RegisterReversibleInt(id, &backward_num_tasks_to_sweep_);
323  watcher->RegisterReversibleInt(id, &num_profile_tasks_);
324 
325  // Changing the times or pushing task absence migth have side effects on the
326  // other intervals, so we would need to be called again in this case.
328 }
329 
330 // Note that we relly on being called again to reach a fixed point.
332  // This can fail if the profile exceeds the resource capacity.
333  if (!BuildProfile()) return false;
334 
335  // Update the minimum start times.
336  if (!SweepAllTasks(/*is_forward=*/true)) return false;
337 
338  // We reuse the same profile, but reversed, to update the maximum end times.
339  if (!helper_->SynchronizeAndSetTimeDirection(false)) return false;
340  ReverseProfile();
341 
342  // Update the maximum end times (reversed problem).
343  if (!SweepAllTasks(/*is_forward=*/false)) return false;
344 
345  return true;
346 }
347 
348 bool TimeTablingPerTask::BuildProfile() {
349  if (!helper_->SynchronizeAndSetTimeDirection(true)) return false;
350 
351  // Update the set of tasks that contribute to the profile. Tasks that were
352  // contributing are still part of the profile so we only need to check the
353  // other tasks.
354  for (int i = num_profile_tasks_; i < num_tasks_; ++i) {
355  const int t1 = profile_tasks_[i];
356  if (helper_->IsPresent(t1) && helper_->StartMax(t1) < helper_->EndMin(t1)) {
357  // Swap values and positions.
358  const int t2 = profile_tasks_[num_profile_tasks_];
359  profile_tasks_[i] = t2;
360  profile_tasks_[num_profile_tasks_] = t1;
361  positions_in_profile_tasks_[t1] = num_profile_tasks_;
362  positions_in_profile_tasks_[t2] = i;
363  num_profile_tasks_++;
364  }
365  }
366 
367  const auto& by_decreasing_start_max = helper_->TaskByDecreasingStartMax();
368  const auto& by_end_min = helper_->TaskByIncreasingEndMin();
369 
370  // Build the profile.
371  // ------------------
372  profile_.clear();
373 
374  // Start and height of the highest profile rectangle.
375  profile_max_height_ = kMinIntegerValue;
376  IntegerValue max_height_start = kMinIntegerValue;
377 
378  // Add a sentinel to simplify the algorithm.
379  profile_.emplace_back(kMinIntegerValue, IntegerValue(0));
380 
381  // Start and height of the currently built profile rectangle.
382  IntegerValue current_start = kMinIntegerValue;
383  IntegerValue current_height = starting_profile_height_;
384 
385  // Next start/end of the compulsory parts to be processed. Note that only the
386  // task for which IsInProfile() is true must be considered.
387  int next_start = num_tasks_ - 1;
388  int next_end = 0;
389  while (next_end < num_tasks_) {
390  const IntegerValue old_height = current_height;
391 
392  IntegerValue t = by_end_min[next_end].time;
393  if (next_start >= 0) {
394  t = std::min(t, by_decreasing_start_max[next_start].time);
395  }
396 
397  // Process the starting compulsory parts.
398  while (next_start >= 0 && by_decreasing_start_max[next_start].time == t) {
399  const int task_index = by_decreasing_start_max[next_start].task_index;
400  if (IsInProfile(task_index)) current_height += DemandMin(task_index);
401  --next_start;
402  }
403 
404  // Process the ending compulsory parts.
405  while (next_end < num_tasks_ && by_end_min[next_end].time == t) {
406  const int task_index = by_end_min[next_end].task_index;
407  if (IsInProfile(task_index)) current_height -= DemandMin(task_index);
408  ++next_end;
409  }
410 
411  // Insert a new profile rectangle if any.
412  if (current_height != old_height) {
413  profile_.emplace_back(current_start, old_height);
414  if (current_height > profile_max_height_) {
415  profile_max_height_ = current_height;
416  max_height_start = t;
417  }
418  current_start = t;
419  }
420  }
421 
422  // Build the last profile rectangle.
423  DCHECK_GE(current_height, 0);
424  profile_.emplace_back(current_start, IntegerValue(0));
425 
426  // Add a sentinel to simplify the algorithm.
427  profile_.emplace_back(kMaxIntegerValue, IntegerValue(0));
428 
429  // Increase the capacity variable if required.
430  return IncreaseCapacity(max_height_start, profile_max_height_);
431 }
432 
433 void TimeTablingPerTask::ReverseProfile() {
434  // We keep the sentinels inchanged.
435  for (int i = 1; i + 1 < profile_.size(); ++i) {
436  profile_[i].start = -profile_[i + 1].start;
437  }
438  std::reverse(profile_.begin() + 1, profile_.end() - 1);
439 }
440 
441 bool TimeTablingPerTask::SweepAllTasks(bool is_forward) {
442  // Tasks with a lower or equal demand will not be pushed.
443  const IntegerValue demand_threshold(
444  CapSub(CapacityMax().value(), profile_max_height_.value()));
445 
446  // Select the correct members depending on the direction.
447  int& num_tasks =
448  is_forward ? forward_num_tasks_to_sweep_ : backward_num_tasks_to_sweep_;
449  std::vector<int>& tasks =
450  is_forward ? forward_tasks_to_sweep_ : backward_tasks_to_sweep_;
451 
452  // TODO(user): On some problem, a big chunk of the time is spend just checking
453  // these conditions below because it requires indirect memory access to fetch
454  // the demand/size/presence/start ...
455  for (int i = num_tasks - 1; i >= 0; --i) {
456  const int t = tasks[i];
457  if (helper_->IsAbsent(t) ||
458  (helper_->IsPresent(t) && helper_->StartIsFixed(t))) {
459  // This tasks does not have to be considered for propagation in the rest
460  // of the sub-tree. Note that StartIsFixed() depends on the time
461  // direction, it is why we use two lists.
462  std::swap(tasks[i], tasks[--num_tasks]);
463  continue;
464  }
465 
466  // Skip if demand is too low.
467  if (DemandMin(t) <= demand_threshold) {
468  if (DemandMax(t) == 0) {
469  // We can ignore this task for the rest of the subtree like above.
470  std::swap(tasks[i], tasks[--num_tasks]);
471  }
472 
473  // This task does not have to be considered for propagation in this
474  // particular iteration, but maybe it does later.
475  continue;
476  }
477 
478  // Skip if size is zero.
479  if (helper_->SizeMin(t) == 0) {
480  if (helper_->SizeMax(t) == 0) {
481  std::swap(tasks[i], tasks[--num_tasks]);
482  }
483  continue;
484  }
485 
486  if (!SweepTask(t)) return false;
487  }
488 
489  return true;
490 }
491 
492 bool TimeTablingPerTask::SweepTask(int task_id) {
493  const IntegerValue start_max = helper_->StartMax(task_id);
494  const IntegerValue size_min = helper_->SizeMin(task_id);
495  const IntegerValue initial_start_min = helper_->StartMin(task_id);
496  const IntegerValue initial_end_min = helper_->EndMin(task_id);
497 
498  IntegerValue new_start_min = initial_start_min;
499  IntegerValue new_end_min = initial_end_min;
500 
501  // Find the profile rectangle that overlaps the minimum start time of task_id.
502  // The sentinel prevents out of bound exceptions.
503  DCHECK(std::is_sorted(profile_.begin(), profile_.end()));
504  int rec_id =
505  std::upper_bound(profile_.begin(), profile_.end(), new_start_min,
506  [&](IntegerValue value, const ProfileRectangle& rect) {
507  return value < rect.start;
508  }) -
509  profile_.begin();
510  --rec_id;
511 
512  // A profile rectangle is in conflict with the task if its height exceeds
513  // conflict_height.
514  const IntegerValue conflict_height = CapacityMax() - DemandMin(task_id);
515 
516  // True if the task is in conflict with at least one profile rectangle.
517  bool conflict_found = false;
518 
519  // Last time point during which task_id was in conflict with a profile
520  // rectangle before being pushed.
521  IntegerValue last_initial_conflict = kMinIntegerValue;
522 
523  // Push the task from left to right until it does not overlap any conflicting
524  // rectangle. Pushing the task may push the end of its compulsory part on the
525  // right but will not change its start. The main loop of the propagator will
526  // take care of rebuilding the profile with these possible changes and to
527  // propagate again in order to reach the timetabling consistency or to fail if
528  // the profile exceeds the resource capacity.
529  IntegerValue limit = std::min(start_max, new_end_min);
530  for (; profile_[rec_id].start < limit; ++rec_id) {
531  // If the profile rectangle is not conflicting, go to the next rectangle.
532  if (profile_[rec_id].height <= conflict_height) continue;
533 
534  conflict_found = true;
535 
536  // Compute the next minimum start and end times of task_id. The variables
537  // are not updated yet.
538  new_start_min = profile_[rec_id + 1].start; // i.e. profile_[rec_id].end
539  if (start_max < new_start_min) {
540  if (IsInProfile(task_id)) {
541  // Because the task is part of the profile, we cannot push it further.
542  new_start_min = start_max;
543  } else {
544  // We have a conflict or we can push the task absence. In both cases
545  // we don't need more than start_max + 1 in the explanation below.
546  new_start_min = start_max + 1;
547  }
548  }
549 
550  new_end_min = std::max(new_end_min, new_start_min + size_min);
551  limit = std::min(start_max, new_end_min);
552 
553  if (profile_[rec_id].start < initial_end_min) {
554  last_initial_conflict = std::min(new_start_min, initial_end_min) - 1;
555  }
556  }
557 
558  if (!conflict_found) return true;
559 
560  if (initial_start_min != new_start_min &&
561  !UpdateStartingTime(task_id, last_initial_conflict, new_start_min)) {
562  return false;
563  }
564 
565  return true;
566 }
567 
568 bool TimeTablingPerTask::UpdateStartingTime(int task_id, IntegerValue left,
569  IntegerValue right) {
570  helper_->ClearReason();
571 
572  AddProfileReason(left, right);
573  if (capacity_.var != kNoIntegerVariable) {
574  helper_->MutableIntegerReason()->push_back(
575  integer_trail_->UpperBoundAsLiteral(capacity_.var));
576  }
577 
578  // State of the task to be pushed.
579  helper_->AddEndMinReason(task_id, left + 1);
580  helper_->AddSizeMinReason(task_id, IntegerValue(1));
581  if (demands_[task_id].var != kNoIntegerVariable) {
582  helper_->MutableIntegerReason()->push_back(
583  integer_trail_->LowerBoundAsLiteral(demands_[task_id].var));
584  }
585 
586  // Explain the increase of the minimum start and end times.
587  return helper_->IncreaseStartMin(task_id, right);
588 }
589 
590 void TimeTablingPerTask::AddProfileReason(IntegerValue left,
591  IntegerValue right) {
592  for (int i = 0; i < num_profile_tasks_; ++i) {
593  const int t = profile_tasks_[i];
594 
595  // Do not consider the task if it does not overlap for sure (left, right).
596  const IntegerValue start_max = helper_->StartMax(t);
597  if (right <= start_max) continue;
598  const IntegerValue end_min = helper_->EndMin(t);
599  if (end_min <= left) continue;
600 
601  helper_->AddPresenceReason(t);
602  helper_->AddStartMaxReason(t, std::max(left, start_max));
603  helper_->AddEndMinReason(t, std::min(right, end_min));
604  if (demands_[t].var != kNoIntegerVariable) {
605  helper_->MutableIntegerReason()->push_back(
606  integer_trail_->LowerBoundAsLiteral(demands_[t].var));
607  }
608  }
609 }
610 
611 bool TimeTablingPerTask::IncreaseCapacity(IntegerValue time,
612  IntegerValue new_min) {
613  if (new_min <= CapacityMin()) return true;
614 
615  helper_->ClearReason();
616  AddProfileReason(time, time + 1);
617  if (capacity_.var == kNoIntegerVariable) {
618  return helper_->ReportConflict();
619  }
620 
621  helper_->MutableIntegerReason()->push_back(
622  integer_trail_->UpperBoundAsLiteral(capacity_.var));
623  return helper_->PushIntegerLiteral(capacity_.GreaterOrEqual(new_min));
624 }
625 
626 } // namespace sat
627 } // namespace operations_research
int64_t CapSub(int64_t x, int64_t y)
int64_t min
Definition: alldiff_cst.cc:139
IntegerLiteral GreaterOrEqual(IntegerValue bound) const
Definition: integer.h:1330
ABSL_MUST_USE_RESULT bool SynchronizeAndSetTimeDirection(bool is_forward)
Definition: intervals.cc:295
ABSL_MUST_USE_RESULT bool PushIntegerLiteral(IntegerLiteral lit)
Definition: intervals.cc:426
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
IntegerLiteral UpperBoundAsLiteral(IntegerVariable i) const
Definition: integer.h:1392
#define CHECK_GT(val1, val2)
Definition: base/logging.h:703
bool ReportConflict(absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
Definition: integer.h:849
IntegerValue LowerBound(IntegerVariable i) const
Definition: integer.h:1345
bool LiteralIsFalse(Literal literal) const
Definition: sat_base.h:148
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Definition: id_map.h:263
bool LiteralIsTrue(Literal literal) const
Definition: sat_base.h:151
void RegisterWith(GenericLiteralWatcher *watcher)
Definition: timetable.cc:314
GRBmodel * model
void WatchAllTasks(int id, GenericLiteralWatcher *watcher, bool watch_start_max=true, bool watch_end_max=true) const
Definition: intervals.cc:508
ABSL_MUST_USE_RESULT bool IncreaseStartMin(int t, IntegerValue new_start_min)
Definition: intervals.cc:453
void AddStartMaxReason(int t, IntegerValue upper_bound)
Definition: intervals.h:568
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
IntegerLiteral LowerBoundAsLiteral(IntegerVariable i) const
Definition: integer.h:1387
void AddEndMinReason(int t, IntegerValue lower_bound)
Definition: intervals.h:575
ABSL_MUST_USE_RESULT bool Enqueue(IntegerLiteral i_lit, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
Definition: integer.cc:1028
#define CHECK_LT(val1, val2)
Definition: base/logging.h:701
std::function< void(Model *)> LowerOrEqual(IntegerVariable v, int64_t ub)
Definition: integer.h:1567
int64_t max
Definition: alldiff_cst.cc:140
double upper_bound
void WatchLowerBound(IntegerVariable var, int id, int watch_index=-1)
Definition: integer.h:1430
Rev< int64_t > start_max
TimeTablingPerTask(const std::vector< AffineExpression > &demands, AffineExpression capacity, IntegerTrail *integer_trail, SchedulingConstraintHelper *helper)
Definition: timetable.cc:280
Rev< int64_t > end_min
std::function< void(Model *)> GreaterOrEqual(IntegerVariable v, int64_t lb)
Definition: integer.h:1552
void EnqueueLiteral(Literal literal, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
Definition: integer.cc:1142
int64_t capacity
An Assignment is a variable -> domains mapping, used to report solutions to the user.
#define DCHECK_GE(val1, val2)
Definition: base/logging.h:890
ABSL_MUST_USE_RESULT bool ConditionalEnqueue(Literal lit, IntegerLiteral i_lit, std::vector< Literal > *literal_reason, std::vector< IntegerLiteral > *integer_reason)
Definition: integer.cc:1035
#define DCHECK(condition)
Definition: base/logging.h:885
int Register(PropagatorInterface *propagator)
Definition: integer.cc:1996
void AddReservoirConstraint(std::vector< AffineExpression > times, std::vector< IntegerValue > deltas, std::vector< Literal > presences, int64_t min_level, int64_t max_level, Model *model)
Definition: timetable.cc:28
IntegerValue UpperBound(IntegerVariable i) const
Definition: integer.h:1349
std::vector< IntegerLiteral > * MutableIntegerReason()
Definition: intervals.h:313
Collection of objects used to extend the Constraint Solver library.
const IntegerVariable kNoIntegerVariable(-1)
int64_t time
Definition: resource.cc:1691
void WatchUpperBound(IntegerVariable var, int id, int watch_index=-1)
Definition: integer.h:1448
IntVar * var
Definition: expr_array.cc:1874
const std::vector< TaskTime > & TaskByIncreasingEndMin()
Definition: intervals.cc:325
const std::vector< TaskTime > & TaskByDecreasingStartMax()
Definition: intervals.cc:337
int64_t value
ReservoirTimeTabling(const std::vector< AffineExpression > &times, const std::vector< IntegerValue > &deltas, const std::vector< Literal > &presences, IntegerValue capacity, Model *model)
Definition: timetable.cc:53