SIGN IN SIGN UP
google / or-tools UNCLAIMED

Google's Operations Research tools:

0 0 1 C++
2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2021-01-14 10:48:19 +01: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.
// Magic sequence problem
//
// Compute a sequence of numbers such that the number of occurrences of i
// in the sequence is equal to the value of the ith number.
2021-04-23 14:55:51 +02:00
#include <cstdint>
#include <cstdlib>
2022-09-09 16:49:24 +02:00
#include <numeric>
#include <string>
#include <vector>
2021-01-14 10:48:19 +01:00
2025-02-25 16:03:40 +01:00
#include "absl/base/log_severity.h"
2021-01-14 10:48:19 +01:00
#include "absl/flags/flag.h"
2025-02-25 16:03:40 +01:00
#include "absl/log/check.h"
#include "absl/log/globals.h"
2025-03-04 21:10:09 +01:00
#include "absl/log/log.h"
2021-01-14 10:48:19 +01:00
#include "absl/strings/str_format.h"
2022-02-25 09:47:52 +01:00
#include "ortools/base/init_google.h"
2021-01-14 10:48:19 +01:00
#include "ortools/sat/cp_model.h"
ABSL_FLAG(int, size, 50, "Size of the problem.");
ABSL_FLAG(std::string, params, "log_search_progress:true,num_search_workers:8",
"Sat parameters.");
namespace operations_research {
namespace sat {
void MagicSequence(int size) {
CHECK_GE(size, 1);
CpModelBuilder cp_model;
std::vector<std::vector<BoolVar>> var_domains(size);
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
var_domains[i].push_back(cp_model.NewBoolVar());
}
}
// Domain constraint on each position.
for (int i = 0; i < size; ++i) {
cp_model.AddEquality(LinearExpr::Sum(var_domains[i]), 1);
2021-01-14 10:48:19 +01:00
}
// The number of variables equal to j shall be the value of vars[j].
2021-04-02 14:58:16 +02:00
std::vector<int64_t> values(size);
2021-01-14 10:48:19 +01:00
std::iota(values.begin(), values.end(), 0); // [0, 1, 2, .., size - 1]
std::vector<BoolVar> vars_equal_to_j;
for (int j = 0; j < size; ++j) {
vars_equal_to_j.clear();
for (int i = 0; i < size; ++i) {
vars_equal_to_j.push_back(var_domains[i][j]);
}
2022-01-04 19:35:22 +01:00
cp_model.AddEquality(LinearExpr::WeightedSum(var_domains[j], values),
LinearExpr::Sum(vars_equal_to_j));
2021-01-14 10:48:19 +01:00
}
const CpSolverResponse response =
SolveWithParameters(cp_model.Build(), absl::GetFlag(FLAGS_params));
if (response.status() == CpSolverStatus::OPTIMAL ||
response.status() == CpSolverStatus::FEASIBLE) {
std::string output = "[";
for (int i = 0; i < size; ++i) {
if (i != 0) {
output.append(", ");
}
for (int j = 0; j < size; ++j) {
if (SolutionBooleanValue(response, var_domains[i][j])) {
absl::StrAppendFormat(&output, "%d", j);
break;
}
}
}
output.append("]");
LOG(INFO) << "Solution = " << output;
}
}
} // namespace sat
} // namespace operations_research
int main(int argc, char** argv) {
2025-02-25 16:03:40 +01:00
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
2022-02-25 09:47:52 +01:00
InitGoogle(argv[0], &argc, &argv, true);
2021-01-14 10:48:19 +01:00
operations_research::sat::MagicSequence(absl::GetFlag(FLAGS_size));
return EXIT_SUCCESS;
}