OR-Tools  9.6
cumulative_energy.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_CUMULATIVE_ENERGY_H_
15 #define OR_TOOLS_SAT_CUMULATIVE_ENERGY_H_
16 
17 #include <functional>
18 #include <utility>
19 #include <vector>
20 
21 #include "ortools/sat/integer.h"
22 #include "ortools/sat/intervals.h"
23 #include "ortools/sat/model.h"
24 #include "ortools/sat/theta_tree.h"
25 #include "ortools/sat/util.h"
26 
27 namespace operations_research {
28 namespace sat {
29 
30 // Enforces the existence of a preemptive schedule where every task is executed
31 // inside its interval, using energy units of the resource during execution.
32 //
33 // Important: This only uses the energies min/max and not the actual demand
34 // of a task. It can thus be used in some non-conventional situation.
35 //
36 // All energy expression are assumed to take a non-negative value;
37 // if the energy of a task is 0, the task can run anywhere.
38 // The schedule never uses more than capacity units of energy at a given time.
39 //
40 // This is mathematically equivalent to making a model with energy(task)
41 // different tasks with demand and size 1, but is much more efficient,
42 // since it uses O(|tasks|) variables instead of O(sum_{task} |energy(task)|).
43 void AddCumulativeOverloadChecker(AffineExpression capacity,
44  SchedulingConstraintHelper* helper,
45  SchedulingDemandHelper* demands,
46  Model* model);
47 
48 // Implementation of AddCumulativeOverloadChecker().
50  public:
54 
55  bool Propagate() final;
56  void RegisterWith(GenericLiteralWatcher* watcher);
57 
58  private:
59  const AffineExpression capacity_;
60  IntegerTrail* integer_trail_;
62  SchedulingDemandHelper* demands_;
63 
65 
66  // Task characteristics.
67  std::vector<int> task_to_start_event_;
68 
69  // Start event characteristics, by nondecreasing start time.
70  std::vector<TaskTime> start_event_task_time_;
71  std::vector<bool> start_event_is_present_;
72 };
73 
74 // Given that the "tasks" are part of a cumulative constraint, this adds a
75 // constraint that propagate the fact that: var >= max(end of substasks) +
76 // offset.
77 //
78 // TODO(user): I am not sure this is the best way, but it does at least push
79 // the level zero bound on the large cumulative instances.
81  public:
82  CumulativeIsAfterSubsetConstraint(IntegerVariable var,
84  const std::vector<int>& subtasks,
85  const std::vector<IntegerValue>& offsets,
87  SchedulingDemandHelper* demands,
88  Model* model);
89 
90  bool Propagate() final;
91  void RegisterWith(GenericLiteralWatcher* watcher);
92 
93  private:
94  const IntegerVariable var_to_push_;
95  const AffineExpression capacity_;
96  const std::vector<int> subtasks_;
97 
98  // Computed at construction time, this is const.
99  std::vector<bool> is_in_subtasks_;
100  std::vector<IntegerValue> task_offsets_;
101 
102  // Temporary data used by the algorithm.
104  std::vector<std::pair<IntegerValue, IntegerValue>> energy_changes_;
105 
106  IntegerTrail* integer_trail_;
108  SchedulingDemandHelper* demands_;
109 };
110 
111 } // namespace sat
112 } // namespace operations_research
113 
114 #endif // OR_TOOLS_SAT_CUMULATIVE_ENERGY_H_
CumulativeEnergyConstraint(AffineExpression capacity, SchedulingConstraintHelper *helper, SchedulingDemandHelper *demands, Model *model)
void RegisterWith(GenericLiteralWatcher *watcher)
CumulativeIsAfterSubsetConstraint(IntegerVariable var, AffineExpression capacity, const std::vector< int > &subtasks, const std::vector< IntegerValue > &offsets, SchedulingConstraintHelper *helper, SchedulingDemandHelper *demands, Model *model)
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
IntVar * var
Definition: expr_array.cc:1874
GRBmodel * model
void AddCumulativeOverloadChecker(AffineExpression capacity, SchedulingConstraintHelper *helper, SchedulingDemandHelper *demands, Model *model)
Collection of objects used to extend the Constraint Solver library.
int64_t capacity