2021-04-02 10:08:51 +02:00
|
|
|
// Copyright 2010-2021 Google LLC
|
2018-11-22 04:17:54 -08: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.
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2021-01-14 10:48:19 +01:00
|
|
|
#include "absl/flags/flag.h"
|
|
|
|
|
#include "absl/flags/parse.h"
|
|
|
|
|
#include "absl/flags/usage.h"
|
|
|
|
|
#include "absl/strings/str_cat.h"
|
2018-11-28 11:32:45 +01:00
|
|
|
#include "absl/strings/str_format.h"
|
2021-01-14 10:48:19 +01:00
|
|
|
#include "ortools/base/integral_types.h"
|
|
|
|
|
#include "ortools/base/logging.h"
|
2018-11-22 04:17:54 -08:00
|
|
|
#include "ortools/sat/cp_model.h"
|
|
|
|
|
#include "ortools/sat/model.h"
|
|
|
|
|
|
2020-10-23 11:50:14 +02:00
|
|
|
ABSL_FLAG(int, size, 7, "Size of the magic square");
|
2021-01-14 10:48:19 +01:00
|
|
|
ABSL_FLAG(std::string, params, "", "Sat parameters");
|
2018-11-22 04:17:54 -08:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
namespace sat {
|
|
|
|
|
|
|
|
|
|
void MagicSquare(int size) {
|
|
|
|
|
CpModelBuilder builder;
|
|
|
|
|
|
2021-12-29 17:41:57 +01:00
|
|
|
std::vector<std::vector<IntVar>> square(size);
|
2018-11-22 04:17:54 -08:00
|
|
|
std::vector<IntVar> all_variables;
|
2021-12-29 17:41:57 +01:00
|
|
|
const Domain domain(1, size * size);
|
2018-11-22 04:17:54 -08:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
for (int j = 0; j < size; ++j) {
|
|
|
|
|
const IntVar var = builder.NewIntVar(domain);
|
|
|
|
|
square[i].push_back(var);
|
|
|
|
|
all_variables.push_back(var);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 17:41:57 +01:00
|
|
|
// All cells take different values.
|
2022-01-20 21:30:24 +01:00
|
|
|
builder.AddAllDifferent(all_variables);
|
2018-11-22 04:17:54 -08:00
|
|
|
|
2021-12-29 17:41:57 +01:00
|
|
|
// The sum on each row, columns and two main diagonals.
|
|
|
|
|
const int magic_value = size * (size * size + 1) / 2;
|
|
|
|
|
|
2018-11-22 04:17:54 -08:00
|
|
|
// Sum on rows.
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2021-12-29 17:41:57 +01:00
|
|
|
LinearExpr sum;
|
|
|
|
|
for (int j = 0; j < size; ++j) {
|
|
|
|
|
sum += square[i][j];
|
|
|
|
|
}
|
|
|
|
|
builder.AddEquality(sum, magic_value);
|
2018-11-22 04:17:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sum on columns.
|
2021-12-29 17:41:57 +01:00
|
|
|
for (int j = 0; j < size; ++j) {
|
|
|
|
|
LinearExpr sum;
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
sum += square[i][j];
|
|
|
|
|
}
|
|
|
|
|
builder.AddEquality(sum, magic_value);
|
2018-11-22 04:17:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sum on diagonals.
|
2021-12-29 17:41:57 +01:00
|
|
|
LinearExpr diag1_sum;
|
|
|
|
|
LinearExpr diag2_sum;
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
diag1_sum += square[i][i];
|
|
|
|
|
diag2_sum += square[i][size - 1 - i];
|
|
|
|
|
}
|
|
|
|
|
builder.AddEquality(diag1_sum, magic_value);
|
|
|
|
|
builder.AddEquality(diag2_sum, magic_value);
|
2018-11-28 11:32:45 +01:00
|
|
|
|
2018-11-24 16:01:12 +01:00
|
|
|
Model model;
|
2020-10-21 00:21:54 +02:00
|
|
|
model.Add(NewSatParameters(absl::GetFlag(FLAGS_params)));
|
2018-11-24 16:01:12 +01:00
|
|
|
|
2019-05-15 20:19:00 +02:00
|
|
|
const CpSolverResponse response = SolveCpModel(builder.Build(), &model);
|
2018-11-24 16:01:12 +01:00
|
|
|
|
2020-09-20 09:04:28 +02:00
|
|
|
if (response.status() == CpSolverStatus::OPTIMAL) {
|
2020-09-23 11:45:03 +02:00
|
|
|
for (int n = 0; n < size; ++n) {
|
2018-11-22 04:17:54 -08:00
|
|
|
std::string output;
|
|
|
|
|
for (int m = 0; m < size; ++m) {
|
2018-11-28 11:32:45 +01:00
|
|
|
absl::StrAppendFormat(&output, "%3d ",
|
|
|
|
|
SolutionIntegerValue(response, square[n][m]));
|
2018-11-22 04:17:54 -08:00
|
|
|
}
|
|
|
|
|
LOG(INFO) << output;
|
2018-11-28 11:32:45 +01:00
|
|
|
}
|
2018-11-22 04:17:54 -08:00
|
|
|
} else {
|
|
|
|
|
LOG(INFO) << "No solution found!";
|
|
|
|
|
}
|
2018-11-28 11:32:45 +01:00
|
|
|
LOG(INFO) << CpSolverResponseStats(response);
|
2018-11-22 04:17:54 -08:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace sat
|
|
|
|
|
} // namespace operations_research
|
2018-11-22 04:17:54 -08:00
|
|
|
|
2020-10-29 14:25:39 +01:00
|
|
|
int main(int argc, char** argv) {
|
2020-10-21 00:28:40 +02:00
|
|
|
absl::SetFlag(&FLAGS_logtostderr, true);
|
2021-01-14 10:48:19 +01:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
2020-10-23 11:50:14 +02:00
|
|
|
absl::ParseCommandLine(argc, argv);
|
2021-01-14 10:48:19 +01:00
|
|
|
|
2020-10-21 00:21:54 +02:00
|
|
|
operations_research::sat::MagicSquare(absl::GetFlag(FLAGS_size));
|
2018-11-22 04:17:54 -08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|