31#include "ortools/sat/sat_parameters.pb.h"
41 const std::vector<IntervalVariable>& vars,
45 if (vars.empty())
return;
56 for (
int i = 0; i < demands.size(); ++i) {
57 if (intervals->MaxSize(vars[i]) == 0)
continue;
60 builder.
AddTerm(demands[i], IntegerValue(1));
64 std::vector<Literal> enforcement_literals;
65 if (intervals->IsOptional(vars[i])) {
66 enforcement_literals.push_back(intervals->PresenceLiteral(vars[i]));
72 if (intervals->MinSize(vars[i]) == 0) {
73 enforcement_literals.push_back(encoder->GetOrCreateAssociatedLiteral(
74 intervals->Size(vars[i]).GreaterOrEqual(IntegerValue(1))));
77 if (enforcement_literals.empty()) {
84 if (vars.size() == 1)
return;
86 const SatParameters&
parameters = *(
model->GetOrCreate<SatParameters>());
90 if (
parameters.use_disjunctive_constraint_in_cumulative_constraint()) {
98 std::vector<IntervalVariable> in_disjunction;
99 for (
int i = 0; i < vars.size(); ++i) {
100 if (intervals->MinSize(vars[i]) > 0 &&
101 2 * integer_trail->LowerBound(demands[i]) >
102 integer_trail->UpperBound(
capacity)) {
103 in_disjunction.push_back(vars[i]);
124 if (in_disjunction.size() == vars.size())
return;
127 if (helper ==
nullptr) {
129 model->TakeOwnership(helper);
147 std::vector<IntegerVariable> index_to_end_vars;
148 std::vector<int> index_to_task;
149 std::vector<PrecedencesPropagator::IntegerPrecedences> before;
150 index_to_end_vars.clear();
151 for (
int t = 0; t < helper->
NumTasks(); ++t) {
156 index_to_end_vars.push_back(end_exp.
var);
157 index_to_task.push_back(t);
160 index_to_end_vars, &before);
161 const int size = before.size();
162 for (
int i = 0; i < size;) {
163 const IntegerVariable
var = before[i].var;
167 std::vector<int> subtasks;
168 for (; i < size && before[i].var ==
var; ++i) {
169 const int t = index_to_task[before[i].index];
170 subtasks.push_back(t);
180 if (subtasks.size() > 1) {
184 integer_trail, helper);
186 model->TakeOwnership(constraint);
197 model->TakeOwnership(time_tabling);
201 if (
parameters.use_overload_checker_in_cumulative_constraint()) {
208 if (
parameters.use_timetable_edge_finding_in_cumulative_constraint()) {
212 model->TakeOwnership(time_table_edge_finding);
218 const std::vector<IntervalVariable>& vars,
222 if (vars.empty())
return;
229 const int num_tasks = vars.size();
234 std::vector<IntegerVariable> start_vars;
235 std::vector<IntegerVariable> end_vars;
236 std::vector<IntegerValue> fixed_demands;
238 for (
int t = 0; t < num_tasks; ++t) {
239 start_vars.push_back(intervals->StartVar(vars[t]));
240 end_vars.push_back(intervals->EndVar(vars[t]));
242 fixed_demands.push_back(integer_trail->
LowerBound(demands[t]));
248 for (
int t = 0; t < num_tasks; ++t) {
253 for (IntegerValue
time = min_start;
time < max_end; ++
time) {
254 std::vector<LiteralWithCoeff> literals_with_coeff;
255 for (
int t = 0; t < num_tasks; ++t) {
264 std::vector<Literal> consume_condition;
268 if (intervals->IsOptional(vars[t])) {
269 consume_condition.push_back(intervals->PresenceLiteral(vars[t]));
273 consume_condition.push_back(encoder->GetOrCreateAssociatedLiteral(
275 consume_condition.push_back(encoder->GetOrCreateAssociatedLiteral(
277 IntegerValue(
time + 1))));
285 literals_with_coeff.push_back(
290 fixed_capacity, &literals_with_coeff);
299 const std::vector<IntervalVariable>& vars,
303 if (vars.empty())
return;
310 const IntegerValue fixed_capacity(
311 integer_trail->UpperBound(
capacity).value());
313 std::vector<AffineExpression> times;
314 std::vector<IntegerValue> deltas;
315 std::vector<Literal> presences;
317 const int num_tasks = vars.size();
318 for (
int t = 0; t < num_tasks; ++t) {
319 CHECK(integer_trail->IsFixed(demands[t]));
320 times.push_back(intervals->StartVar(vars[t]));
321 deltas.push_back(integer_trail->LowerBound(demands[t]));
322 times.push_back(intervals->EndVar(vars[t]));
323 deltas.push_back(-integer_trail->LowerBound(demands[t]));
324 if (intervals->IsOptional(vars[t])) {
325 presences.push_back(intervals->PresenceLiteral(vars[t]));
326 presences.push_back(intervals->PresenceLiteral(vars[t]));
328 presences.push_back(encoder->GetTrueLiteral());
329 presences.push_back(encoder->GetTrueLiteral());
#define DCHECK_NE(val1, val2)
void RegisterWith(GenericLiteralWatcher *watcher)
bool IsFixed(IntegerVariable i) const
IntegerValue UpperBound(IntegerVariable i) const
IntegerValue LowerBound(IntegerVariable i) const
void AddTerm(IntegerVariable var, IntegerValue coeff)
Class that owns everything related to a particular optimization model.
bool AddLinearConstraint(bool use_lower_bound, Coefficient lower_bound, bool use_upper_bound, Coefficient upper_bound, std::vector< LiteralWithCoeff > *cst)
bool IsModelUnsat() const
const std::vector< AffineExpression > & Ends() const
void RegisterWith(GenericLiteralWatcher *watcher)
void RegisterWith(GenericLiteralWatcher *watcher)
std::tuple< int64_t, int64_t, const double > Coefficient
void AddReservoirConstraint(std::vector< AffineExpression > times, std::vector< IntegerValue > deltas, std::vector< Literal > presences, int64_t min_level, int64_t max_level, Model *model)
void AddCumulativeOverloadChecker(const std::vector< AffineExpression > &demands, AffineExpression capacity, SchedulingConstraintHelper *helper, Model *model)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
std::function< BooleanVariable(Model *)> NewBooleanVariable()
void LoadConditionalLinearConstraint(const absl::Span< const Literal > enforcement_literals, const LinearConstraint &cst, Model *model)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
const IntegerVariable kNoIntegerVariable(-1)
std::function< void(Model *)> Cumulative(const std::vector< IntervalVariable > &vars, const std::vector< AffineExpression > &demands, AffineExpression capacity, SchedulingConstraintHelper *helper)
void LoadLinearConstraint(const ConstraintProto &ct, Model *m)
std::function< void(Model *)> Disjunctive(const std::vector< IntervalVariable > &vars)
std::function< void(Model *)> ReifiedBoolAnd(const std::vector< Literal > &literals, Literal r)
std::function< void(Model *)> CumulativeTimeDecomposition(const std::vector< IntervalVariable > &vars, const std::vector< AffineExpression > &demands, AffineExpression capacity, SchedulingConstraintHelper *helper)
std::function< void(Model *)> CumulativeUsingReservoir(const std::vector< IntervalVariable > &vars, const std::vector< AffineExpression > &demands, AffineExpression capacity, SchedulingConstraintHelper *helper)
Collection of objects used to extend the Constraint Solver library.
static IntegerLiteral LowerOrEqual(IntegerVariable i, IntegerValue bound)
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)