* bump abseil to 20250814 * bump protobuf to v32.0 * cmake: add ccache auto support * backport flatzinc, math_opt and sat update
99 lines
3.3 KiB
C++
99 lines
3.3 KiB
C++
// Copyright 2010-2025 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.
|
|
|
|
// [START program]
|
|
#include <stdlib.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include "absl/base/log_severity.h"
|
|
#include "absl/log/globals.h"
|
|
#include "absl/types/span.h"
|
|
#include "ortools/base/init_google.h"
|
|
#include "ortools/base/logging.h"
|
|
#include "ortools/sat/cp_model.h"
|
|
#include "ortools/sat/cp_model.pb.h"
|
|
#include "ortools/sat/cp_model_solver.h"
|
|
#include "ortools/sat/model.h"
|
|
#include "ortools/util/sorted_interval_list.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 int64_t duration_0 = 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 int64_t duration_1 = 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 int64_t duration_2 = 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(5, 2, 7);
|
|
const IntervalVar weekend_1 = cp_model.NewIntervalVar(12, 2, 14);
|
|
const IntervalVar weekend_2 = cp_model.NewIntervalVar(19, 2, 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(int argc, char* argv[]) {
|
|
InitGoogle(argv[0], &argc, &argv, true);
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
|
operations_research::sat::NoOverlapSampleSat();
|
|
return EXIT_SUCCESS;
|
|
}
|
|
// [END program]
|