2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2020-06-05 16:11:35 +02:00
|
|
|
// 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]
|
|
|
|
|
// [START import]
|
2022-02-15 18:00:11 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2025-05-16 14:34:40 +02:00
|
|
|
#include "absl/base/log_severity.h"
|
|
|
|
|
#include "absl/log/globals.h"
|
|
|
|
|
#include "ortools/base/init_google.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/base/logging.h"
|
2020-06-05 16:11:35 +02:00
|
|
|
#include "ortools/sat/cp_model.h"
|
2022-02-15 18:00:11 +01:00
|
|
|
#include "ortools/sat/cp_model.pb.h"
|
|
|
|
|
#include "ortools/sat/cp_model_solver.h"
|
|
|
|
|
|
2020-06-05 16:11:35 +02:00
|
|
|
// [END import]
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
|
|
|
|
void IntegerProgrammingExample() {
|
|
|
|
|
// Data
|
|
|
|
|
// [START data_model]
|
2021-12-09 15:29:49 +01:00
|
|
|
const std::vector<std::vector<int>> costs{
|
2020-06-05 16:11:35 +02:00
|
|
|
{90, 80, 75, 70}, {35, 85, 55, 65}, {125, 95, 90, 95},
|
|
|
|
|
{45, 110, 95, 115}, {50, 100, 90, 100},
|
|
|
|
|
};
|
2022-01-01 19:26:39 +01:00
|
|
|
const int num_workers = static_cast<int>(costs.size());
|
|
|
|
|
const int num_tasks = static_cast<int>(costs[0].size());
|
2020-06-05 16:11:35 +02:00
|
|
|
// [END data_model]
|
|
|
|
|
|
|
|
|
|
// Model
|
|
|
|
|
// [START model]
|
|
|
|
|
CpModelBuilder cp_model;
|
|
|
|
|
// [END model]
|
|
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
|
// [START variables]
|
|
|
|
|
// x[i][j] is an array of Boolean variables. x[i][j] is true
|
|
|
|
|
// if worker i is assigned to task j.
|
|
|
|
|
std::vector<std::vector<BoolVar>> x(num_workers,
|
|
|
|
|
std::vector<BoolVar>(num_tasks));
|
|
|
|
|
for (int i = 0; i < num_workers; ++i) {
|
|
|
|
|
for (int j = 0; j < num_tasks; ++j) {
|
|
|
|
|
x[i][j] = cp_model.NewBoolVar();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// [END variables]
|
|
|
|
|
|
|
|
|
|
// Constraints
|
|
|
|
|
// [START constraints]
|
|
|
|
|
// Each worker is assigned to at most one task.
|
|
|
|
|
for (int i = 0; i < num_workers; ++i) {
|
2022-01-01 19:26:39 +01:00
|
|
|
cp_model.AddAtMostOne(x[i]);
|
2020-06-05 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
// Each task is assigned to exactly one worker.
|
|
|
|
|
for (int j = 0; j < num_tasks; ++j) {
|
2022-01-01 19:26:39 +01:00
|
|
|
std::vector<BoolVar> tasks;
|
2020-06-05 16:11:35 +02:00
|
|
|
for (int i = 0; i < num_workers; ++i) {
|
2022-01-01 19:26:39 +01:00
|
|
|
tasks.push_back(x[i][j]);
|
2020-06-05 16:11:35 +02:00
|
|
|
}
|
2022-01-01 19:26:39 +01:00
|
|
|
cp_model.AddExactlyOne(tasks);
|
2020-06-05 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
// [END constraints]
|
|
|
|
|
|
|
|
|
|
// Objective
|
|
|
|
|
// [START objective]
|
|
|
|
|
LinearExpr total_cost;
|
|
|
|
|
for (int i = 0; i < num_workers; ++i) {
|
|
|
|
|
for (int j = 0; j < num_tasks; ++j) {
|
2021-12-09 15:29:49 +01:00
|
|
|
total_cost += x[i][j] * costs[i][j];
|
2020-06-05 16:11:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cp_model.Minimize(total_cost);
|
|
|
|
|
// [END objective]
|
|
|
|
|
|
|
|
|
|
// Solve
|
|
|
|
|
// [START solve]
|
|
|
|
|
const CpSolverResponse response = Solve(cp_model.Build());
|
|
|
|
|
// [END solve]
|
|
|
|
|
|
|
|
|
|
// Print solution.
|
|
|
|
|
// [START print_solution]
|
|
|
|
|
if (response.status() == CpSolverStatus::INFEASIBLE) {
|
|
|
|
|
LOG(FATAL) << "No solution found.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << "Total cost: " << response.objective_value();
|
|
|
|
|
LOG(INFO);
|
|
|
|
|
for (int i = 0; i < num_workers; ++i) {
|
|
|
|
|
for (int j = 0; j < num_tasks; ++j) {
|
|
|
|
|
if (SolutionBooleanValue(response, x[i][j])) {
|
|
|
|
|
LOG(INFO) << "Task " << i << " assigned to worker " << j
|
|
|
|
|
<< ". Cost: " << costs[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// [END print_solution]
|
|
|
|
|
}
|
|
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|
|
|
|
|
|
2025-05-16 14:34:40 +02:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
InitGoogle(argv[0], &argc, &argv, true);
|
|
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
2020-06-05 16:11:35 +02:00
|
|
|
operations_research::sat::IntegerProgrammingExample();
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
2021-02-15 12:26:37 +01:00
|
|
|
// [END program]
|