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