forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_overlap_sample_sat.cc
90 lines (74 loc) · 3.13 KB
/
no_overlap_sample_sat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdint>
#include "ortools/sat/cp_model.h"
namespace operations_research {
namespace sat {
void NoOverlapSampleSat() {
CpModelBuilder cp_model;
const int64_t kHorizon = 21; // 3 weeks.
const Domain horizon(0, kHorizon);
// Task 0, duration 2.
const IntVar start_0 = cp_model.NewIntVar(horizon);
const IntVar duration_0 = cp_model.NewConstant(2);
const IntVar end_0 = cp_model.NewIntVar(horizon);
const IntervalVar task_0 =
cp_model.NewIntervalVar(start_0, duration_0, end_0);
// Task 1, duration 4.
const IntVar start_1 = cp_model.NewIntVar(horizon);
const IntVar duration_1 = cp_model.NewConstant(4);
const IntVar end_1 = cp_model.NewIntVar(horizon);
const IntervalVar task_1 =
cp_model.NewIntervalVar(start_1, duration_1, end_1);
// Task 2, duration 3.
const IntVar start_2 = cp_model.NewIntVar(horizon);
const IntVar duration_2 = cp_model.NewConstant(3);
const IntVar end_2 = cp_model.NewIntVar(horizon);
const IntervalVar task_2 =
cp_model.NewIntervalVar(start_2, duration_2, end_2);
// Week ends.
const IntervalVar weekend_0 =
cp_model.NewIntervalVar(cp_model.NewConstant(5), cp_model.NewConstant(2),
cp_model.NewConstant(7));
const IntervalVar weekend_1 =
cp_model.NewIntervalVar(cp_model.NewConstant(12), cp_model.NewConstant(2),
cp_model.NewConstant(14));
const IntervalVar weekend_2 =
cp_model.NewIntervalVar(cp_model.NewConstant(19), cp_model.NewConstant(2),
cp_model.NewConstant(21));
// No Overlap constraint.
cp_model.AddNoOverlap(
{task_0, task_1, task_2, weekend_0, weekend_1, weekend_2});
// Makespan.
IntVar makespan = cp_model.NewIntVar(horizon);
cp_model.AddLessOrEqual(end_0, makespan);
cp_model.AddLessOrEqual(end_1, makespan);
cp_model.AddLessOrEqual(end_2, makespan);
cp_model.Minimize(makespan);
// Solving part.
Model model;
const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);
LOG(INFO) << CpSolverResponseStats(response);
if (response.status() == CpSolverStatus::OPTIMAL) {
LOG(INFO) << "Optimal Schedule Length: " << response.objective_value();
LOG(INFO) << "Task 0 starts at " << SolutionIntegerValue(response, start_0);
LOG(INFO) << "Task 1 starts at " << SolutionIntegerValue(response, start_1);
LOG(INFO) << "Task 2 starts at " << SolutionIntegerValue(response, start_2);
}
}
} // namespace sat
} // namespace operations_research
int main() {
operations_research::sat::NoOverlapSampleSat();
return EXIT_SUCCESS;
}