* remove python script * remove RTE actions * fix test_xpress_interface.cc * remove callback_xpress.py * revert writing colnames and rownames * accept suggestion from Mizux * clean * change cmake/README.md * try fix build bazel * try fix build bazel add MPSWriteError.h * xpress tests gracefully exit if Xpress not found * add integer and linear programming test for dotnet python and java * remove MPSWriteError * try fix Window build * remove useless line from CMakeLists.txt * try fix test under windows * reformat * use XPRESS_LP instead of XPRESS for linear programming examples * tools: add --platform arg when possible make script more resilient/cross-platform * [CP-SAT] convert to PEP8 convention * use XPRSmipoptimize and XPRSlpoptimize instead of XPRSminim and XPRSmaxim (#114) * use XPRSmipoptimize and XPRSlpoptimize instead of XPRSminim and XPRSmaxim * clean xpress/environment files * accept changes: empty char* parameter for XPRS*optimize * Add test on number iterations with LP basis * fix gtests flags * refactor * suggestions by @flomnes * remove unwanted files --------- Co-authored-by: Andrea Sgattoni <andrea.sgattoni@rte-france.com> Co-authored-by: Laurent Perron <lperron@google.com> Co-authored-by: Corentin Le Molgat <corentinl@google.com> Co-authored-by: Andrea Sgattoni <andrea.sgattoni@gmail.com>
80 lines
2.3 KiB
Python
Executable File
80 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright 2010-2022 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.
|
|
|
|
"""This model implements a sudoku solver."""
|
|
|
|
from ortools.sat.python import cp_model
|
|
|
|
|
|
def solve_sudoku():
|
|
"""Solves the sudoku problem with the CP-SAT solver."""
|
|
# Create the model.
|
|
model = cp_model.CpModel()
|
|
|
|
cell_size = 3
|
|
line_size = cell_size**2
|
|
line = list(range(0, line_size))
|
|
cell = list(range(0, cell_size))
|
|
|
|
initial_grid = [
|
|
[0, 6, 0, 0, 5, 0, 0, 2, 0],
|
|
[0, 0, 0, 3, 0, 0, 0, 9, 0],
|
|
[7, 0, 0, 6, 0, 0, 0, 1, 0],
|
|
[0, 0, 6, 0, 3, 0, 4, 0, 0],
|
|
[0, 0, 4, 0, 7, 0, 1, 0, 0],
|
|
[0, 0, 5, 0, 9, 0, 8, 0, 0],
|
|
[0, 4, 0, 0, 0, 1, 0, 0, 6],
|
|
[0, 3, 0, 0, 0, 8, 0, 0, 0],
|
|
[0, 2, 0, 0, 4, 0, 0, 5, 0],
|
|
]
|
|
|
|
grid = {}
|
|
for i in line:
|
|
for j in line:
|
|
grid[(i, j)] = model.new_int_var(1, line_size, "grid %i %i" % (i, j))
|
|
|
|
# AllDifferent on rows.
|
|
for i in line:
|
|
model.add_all_different(grid[(i, j)] for j in line)
|
|
|
|
# AllDifferent on columns.
|
|
for j in line:
|
|
model.add_all_different(grid[(i, j)] for i in line)
|
|
|
|
# AllDifferent on cells.
|
|
for i in cell:
|
|
for j in cell:
|
|
one_cell = []
|
|
for di in cell:
|
|
for dj in cell:
|
|
one_cell.append(grid[(i * cell_size + di, j * cell_size + dj)])
|
|
|
|
model.add_all_different(one_cell)
|
|
|
|
# Initial values.
|
|
for i in line:
|
|
for j in line:
|
|
if initial_grid[i][j]:
|
|
model.add(grid[(i, j)] == initial_grid[i][j])
|
|
|
|
# Solves and prints out the solution.
|
|
solver = cp_model.CpSolver()
|
|
status = solver.solve(model)
|
|
if status == cp_model.OPTIMAL:
|
|
for i in line:
|
|
print([int(solver.value(grid[(i, j)])) for j in line])
|
|
|
|
|
|
solve_sudoku()
|