replace magic_square by magic_square_sat with much improved performance
This commit is contained in:
@@ -1,160 +0,0 @@
|
||||
// Copyright 2010-2018 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.
|
||||
|
||||
//
|
||||
// Magic square problem
|
||||
//
|
||||
// Solves the problem where all numbers in an nxn array have to be different
|
||||
// while the sums on diagonals, rows, and columns have to be the same.
|
||||
// The problem is trivial for odd orders, but not for even orders.
|
||||
// We do not handle odd orders with the trivial method here.
|
||||
|
||||
#include "ortools/base/commandlineflags.h"
|
||||
#include "ortools/base/integral_types.h"
|
||||
#include "ortools/base/logging.h"
|
||||
#include "ortools/base/stringprintf.h"
|
||||
#include "ortools/constraint_solver/constraint_solver.h"
|
||||
|
||||
DEFINE_int32(size, 0, "Size of the magic square.");
|
||||
DEFINE_bool(impact, false, "Use impact search.");
|
||||
DEFINE_int32(restart, -1, "Parameter for constant restart monitor.");
|
||||
DEFINE_bool(luby, false,
|
||||
"Use luby restart monitor instead of constant restart monitor.");
|
||||
DEFINE_bool(run_all_heuristics, false, "Run all heuristics.");
|
||||
DEFINE_int32(heuristics_period, 200, "Frequency to run all heuristics.");
|
||||
DEFINE_int32(choose_var_strategy, 0,
|
||||
"Selection strategy for variable: 0 = max sum impact, "
|
||||
"1 = max average impact, "
|
||||
"2 = max individual impact.");
|
||||
DEFINE_bool(select_max_impact_value, false,
|
||||
"Select the value with max impact instead of min impact.");
|
||||
DEFINE_double(restart_log_size, -1.0,
|
||||
"Threshold for automatic restarting the search in default"
|
||||
" phase.");
|
||||
DEFINE_bool(verbose_impact, false, "Verbose output of impact search.");
|
||||
DEFINE_bool(use_nogoods, false, "Use no goods in automatic restart.");
|
||||
|
||||
namespace operations_research {
|
||||
|
||||
void MagicSquare(int grid_size) {
|
||||
Solver solver("magicsquare");
|
||||
const int total_size = grid_size * grid_size;
|
||||
const int sum = grid_size * (total_size + 1) / 2;
|
||||
// create the variables
|
||||
std::vector<IntVar*> vars;
|
||||
solver.MakeIntVarArray(total_size, 1, total_size, "", &vars);
|
||||
solver.AddConstraint(solver.MakeAllDifferent(vars));
|
||||
|
||||
// create the constraints
|
||||
std::vector<IntVar*> diag1(grid_size);
|
||||
std::vector<IntVar*> diag2(grid_size);
|
||||
for (int n = 0; n < grid_size; ++n) {
|
||||
std::vector<IntVar*> sub_set(grid_size);
|
||||
|
||||
for (int m = 0; m < grid_size; ++m) { // extract row indices
|
||||
sub_set[m] = vars[m + n * grid_size];
|
||||
}
|
||||
solver.AddConstraint(solver.MakeSumEquality(sub_set, sum));
|
||||
|
||||
for (int m = 0; m < grid_size; ++m) {
|
||||
sub_set[m] = vars[m * grid_size + n]; // extract column indices
|
||||
}
|
||||
solver.AddConstraint(solver.MakeSumEquality(sub_set, sum));
|
||||
diag1[n] = vars[n + n * grid_size]; // extract first diagonal indices
|
||||
diag2[n] = vars[(grid_size - 1 - n) + n * grid_size]; // second diagonal
|
||||
}
|
||||
solver.AddConstraint(solver.MakeSumEquality(diag1, sum));
|
||||
solver.AddConstraint(solver.MakeSumEquality(diag2, sum));
|
||||
|
||||
// To break a simple symmetry: the upper right corner
|
||||
// must be less than the lower left corner
|
||||
solver.AddConstraint(
|
||||
solver.MakeLess(vars[grid_size - 1], vars[(grid_size - 1) * grid_size]));
|
||||
// TODO(user) use local search
|
||||
|
||||
DefaultPhaseParameters parameters;
|
||||
parameters.run_all_heuristics = FLAGS_run_all_heuristics;
|
||||
parameters.heuristic_period = FLAGS_heuristics_period;
|
||||
parameters.restart_log_size = FLAGS_restart_log_size;
|
||||
parameters.display_level = FLAGS_verbose_impact
|
||||
? DefaultPhaseParameters::VERBOSE
|
||||
: DefaultPhaseParameters::NORMAL;
|
||||
parameters.use_no_goods = FLAGS_use_nogoods;
|
||||
switch (FLAGS_choose_var_strategy) {
|
||||
case 0: {
|
||||
parameters.var_selection_schema =
|
||||
DefaultPhaseParameters::CHOOSE_MAX_SUM_IMPACT;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
parameters.var_selection_schema =
|
||||
DefaultPhaseParameters::CHOOSE_MAX_AVERAGE_IMPACT;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
parameters.var_selection_schema =
|
||||
DefaultPhaseParameters::CHOOSE_MAX_VALUE_IMPACT;
|
||||
break;
|
||||
}
|
||||
default: { LOG(FATAL) << "Should not be here"; }
|
||||
}
|
||||
parameters.value_selection_schema =
|
||||
FLAGS_select_max_impact_value ? DefaultPhaseParameters::SELECT_MAX_IMPACT
|
||||
: DefaultPhaseParameters::SELECT_MIN_IMPACT;
|
||||
|
||||
DecisionBuilder* const db =
|
||||
FLAGS_impact ? solver.MakeDefaultPhase(vars, parameters)
|
||||
: solver.MakePhase(vars, Solver::CHOOSE_FIRST_UNBOUND,
|
||||
Solver::ASSIGN_MIN_VALUE);
|
||||
|
||||
std::vector<SearchMonitor*> monitors;
|
||||
SearchMonitor* const log = solver.MakeSearchLog(100000);
|
||||
monitors.push_back(log);
|
||||
SearchMonitor* const restart =
|
||||
FLAGS_restart != -1
|
||||
? (FLAGS_luby ? solver.MakeLubyRestart(FLAGS_restart)
|
||||
: solver.MakeConstantRestart(FLAGS_restart))
|
||||
: nullptr;
|
||||
if (restart) {
|
||||
monitors.push_back(restart);
|
||||
}
|
||||
solver.NewSearch(db, monitors);
|
||||
if (solver.NextSolution()) {
|
||||
for (int n = 0; n < grid_size; ++n) {
|
||||
std::string output;
|
||||
for (int m = 0; m < grid_size; ++m) { // extract row indices
|
||||
int64 v = vars[n * grid_size + m]->Value();
|
||||
StringAppendF(&output, "%3lld ", v);
|
||||
}
|
||||
LOG(INFO) << output;
|
||||
}
|
||||
LOG(INFO) << "";
|
||||
} else {
|
||||
LOG(INFO) << "No solution found!";
|
||||
}
|
||||
solver.EndSearch();
|
||||
}
|
||||
|
||||
} // namespace operations_research
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
if (FLAGS_size != 0) {
|
||||
operations_research::MagicSquare(FLAGS_size);
|
||||
} else {
|
||||
for (int n = 3; n < 6; ++n) {
|
||||
operations_research::MagicSquare(n);
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
96
examples/cpp/magic_square_sat.cc
Normal file
96
examples/cpp/magic_square_sat.cc
Normal file
@@ -0,0 +1,96 @@
|
||||
// Copyright 2010-2018 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 <string>
|
||||
#include <vector>
|
||||
|
||||
#include "ortools/base/commandlineflags.h"
|
||||
#include "ortools/base/stringprintf.h"
|
||||
#include "ortools/sat/cp_model.h"
|
||||
#include "ortools/sat/model.h"
|
||||
|
||||
DEFINE_int32(size, 7, "Size of the magic square");
|
||||
DEFINE_string(params, "", "Sat paramters");
|
||||
|
||||
namespace operations_research {
|
||||
namespace sat {
|
||||
|
||||
void MagicSquare(int size) {
|
||||
CpModelBuilder builder;
|
||||
|
||||
std::vector<std::vector<IntVar>> square(size);
|
||||
std::vector<std::vector<IntVar>> transposed(size);
|
||||
std::vector<IntVar> diag1;
|
||||
std::vector<IntVar> diag2;
|
||||
std::vector<IntVar> all_variables;
|
||||
Domain domain(1, size * size);
|
||||
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);
|
||||
transposed[j].push_back(var);
|
||||
all_variables.push_back(var);
|
||||
if (i == j) {
|
||||
diag1.push_back(var);
|
||||
}
|
||||
if (i + j == size) {
|
||||
diag2.push_back(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// All Diff.
|
||||
for (int i = 0; i < size; ++i) {
|
||||
builder.AddAllDifferent(all_variables);
|
||||
}
|
||||
|
||||
const int sum = size * (size * size + 1) / 2;
|
||||
// Sum on rows.
|
||||
for (int i = 0; i < size; ++i) {
|
||||
builder.AddEquality(LinearExpr::Sum(square[i]), sum);
|
||||
}
|
||||
|
||||
// Sum on columns.
|
||||
for (int i = 0; i < size; ++i) {
|
||||
builder.AddEquality(LinearExpr::Sum(transposed[i]), sum);
|
||||
}
|
||||
|
||||
// Sum on diagonals.
|
||||
builder.AddEquality(LinearExpr::Sum(diag1), sum);
|
||||
builder.AddEquality(LinearExpr::Sum(diag2), sum);
|
||||
|
||||
const CpSolverResponse response = Solve(builder);
|
||||
if (response.status() == CpSolverStatus::FEASIBLE) {
|
||||
for (int n = 0; n < size; ++n) {
|
||||
std::string output;
|
||||
for (int m = 0; m < size; ++m) {
|
||||
StringAppendF(&output, "%3lld ",
|
||||
SolutionIntegerValue(response, square[n][m]));
|
||||
}
|
||||
LOG(INFO) << output;
|
||||
}
|
||||
} else {
|
||||
LOG(INFO) << "No solution found!";
|
||||
}
|
||||
LOG(INFO) << CpSolverResponseStats(response);
|
||||
}
|
||||
|
||||
} // namespace sat
|
||||
} // namespace sat
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
base::SetFlag(&FLAGS_logtostderr, true);
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
operations_research::sat::MagicSquare(FLAGS_size);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -500,7 +500,7 @@ test_cc_cpp: \
|
||||
rcc_linear_assignment_api \
|
||||
rcc_linear_solver_protocol_buffers \
|
||||
rcc_ls_api \
|
||||
rcc_magic_square \
|
||||
rcc_magic_square_sat \
|
||||
rcc_mps_driver \
|
||||
rcc_nqueens \
|
||||
rcc_random_tsp \
|
||||
|
||||
Reference in New Issue
Block a user