OR-Tools  9.6
timetable_edgefinding.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #ifndef OR_TOOLS_SAT_TIMETABLE_EDGEFINDING_H_
15 #define OR_TOOLS_SAT_TIMETABLE_EDGEFINDING_H_
16 
17 #include <vector>
18 
19 #include "ortools/base/macros.h"
20 #include "ortools/sat/integer.h"
21 #include "ortools/sat/intervals.h"
22 #include "ortools/sat/sat_base.h"
24 
25 namespace operations_research {
26 namespace sat {
27 
28 // TimeTableEdgeFinding implements the timetable edge finding filtering rule
29 // presented in Vilim Petr, "Timetable edge finding filtering algorithm for
30 // discrete cumulative resources", CPAIOR 2011,
31 // http://vilim.eu/petr/cpaior2011.pdf.
32 //
33 // This propagator runs in O(n^2) where n is the number of tasks. It increases
34 // both the start times and decreases the ending times of the tasks.
35 //
36 // Note that this propagator does not ensure that the cumulative constraint
37 // holds. It should thus always be used with at least a timetable propagator.
38 //
39 // ALOGRITHM:
40 //
41 // The algorithm relies on free tasks. A free task is basically a task without
42 // its mandatory part. For instance:
43 //
44 // s_min s_max e_min e_max
45 // v v v v
46 // task: =============================
47 // ^ ^ ^
48 // | free part | Mandatory part |
49 //
50 // Obviously, the free part of a task that has no mandatory part is equal to the
51 // task itself. Also, a free part cannot have a mandatory part by definition. A
52 // fixed task thus have no free part.
53 //
54 // The idea of the algorithm is to use free and mandatory parts separately to
55 // have a better estimation of the energy contained in a task interval.
56 //
57 // If the sum of the energy of all the free parts and mandatory subparts
58 // contained in a task interval exceeds the amount of energy available, then the
59 // problem is unfeasible. A task thus cannot be scheduled at its minimum start
60 // time if this would cause an overload in one of the task intervals.
62  public:
66 
67  bool Propagate() final;
68 
69  void RegisterWith(GenericLiteralWatcher* watcher);
70 
71  private:
72  // Build the timetable and fills the mandatory_energy_before_start_min_ and
73  // mandatory_energy_before_end_max_.
74  //
75  // TODO(user): Share the profile building code with TimeTablingPerTask ! we do
76  // not really need the mandatory_energy_before_* vectors and can recompute the
77  // profile integral in a window efficiently during TimeTableEdgeFindingPass().
78  void BuildTimeTable();
79 
80  // Performs a single pass of the Timetable Edge Finding filtering rule to
81  // updates the start time of the tasks. This same function can be used to
82  // update the end times by calling the SwitchToMirrorProblem method first.
83  bool TimeTableEdgeFindingPass();
84 
85  // Fills the reason for the energy in [window_min, window_max].
86  // We exclude the given task_index mandatory energy and uses
87  // tasks_contributing_to_free_energy_.
88  void FillEnergyInWindowReason(IntegerValue window_min,
89  IntegerValue window_max, int task_index);
90 
91  IntegerValue CapacityMax() const {
92  return integer_trail_->UpperBound(capacity_);
93  }
94 
95  const int num_tasks_;
96  const AffineExpression capacity_;
98  SchedulingDemandHelper* demands_;
99  IntegerTrail* integer_trail_;
100 
101  // Start (resp. end) of the compulsory parts used to build the profile.
102  std::vector<TaskTime> scp_;
103  std::vector<TaskTime> ecp_;
104 
105  // Sizes and energy of the free parts.
106  std::vector<IntegerValue> size_free_;
107  std::vector<IntegerValue> energy_free_;
108 
109  // Energy contained in the time table before the start min (resp. end max)
110  // of each task.
111  std::vector<IntegerValue> mandatory_energy_before_start_min_;
112  std::vector<IntegerValue> mandatory_energy_before_end_max_;
113 
114  // List of task that should participate in the reason.
115  std::vector<int> reason_tasks_fully_included_in_window_;
116  std::vector<int> reason_tasks_partially_included_in_window_;
117 
118  DISALLOW_COPY_AND_ASSIGN(TimeTableEdgeFinding);
119 };
120 
121 } // namespace sat
122 } // namespace operations_research
123 
124 #endif // OR_TOOLS_SAT_TIMETABLE_EDGEFINDING_H_
IntegerValue UpperBound(IntegerVariable i) const
Definition: integer.h:1561
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
TimeTableEdgeFinding(AffineExpression capacity, SchedulingConstraintHelper *helper, SchedulingDemandHelper *demands, Model *model)
void RegisterWith(GenericLiteralWatcher *watcher)
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.
int64_t capacity