当前位置:网站首页>利用google or-tools 求解数独难题
利用google or-tools 求解数独难题
2022-07-23 05:43:00 【-派神-】
数独是一种常见的智力游戏,它的规则是:在一个9×9的网格内填入81个1至9的数字,要求每一行的数字不能出现重复,每一列的数字不能出现重新,同时在9×9的大网格内又划分为9个3×3的小网格要求每个小网格内的数字也不能出现重复。游戏开始时在大网格内已填入若干个数字,要求游戏的参与者填充剩余的数字如下图所示:

今天我们用google or-tools的建模语言cp_model来破解 手机app上的数独游戏enjoy sudoku上难度级别最高的maelstorm级的数独难题:

在建模之前,我们首先要明确数独游戏的规则,我们只需把游戏规则告诉cp_model,至于如何破解则不需要我们操心,cp_model会帮你搞定一切.再次确认游戏规则:
1.在9×9的大网格内:每行、每列元素都不能重复。
2.在3×3的小网格内:所有元素都不能重复。
初始化变量
明确了游戏规则后,我们首先要在代码中定义大网格和小网格的尺寸,以及一个待破解的数独网格,其中未填数字的元素我们会用0来代替:
from ortools.sat.python import cp_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 = [[2, 8, 1, 0, 0, 0, 0, 0, 0],
[7, 9, 6, 1, 0, 4, 0, 3, 5],
[5, 3, 4, 6, 0, 0, 0, 0, 0],
[3, 4, 0, 8, 0, 0, 6, 9, 7],
[8, 1, 0, 7, 0, 6, 0, 5, 0],
[6, 7, 0, 0, 0, 0, 0, 8, 1],
[0, 0, 7, 0, 0, 8, 0, 2, 0],
[1, 2, 8, 3, 0, 7, 5, 4, 0],
[0, 0, 3, 2, 0, 0, 7, 0, 8]]定义网格变量
接下来我们要定义一个9×9的大网格变量,它用来存储破解完成以后网格内的所有数字,然后对网格变量赋初始值:
#定义存储破解结果的网格变量
grid = {}
for i in line:
for j in line:
grid[(i, j)] = model.NewIntVar(1, line_size, 'grid %i %i' % (i, j))
#初始化网格变量
for i in line:
for j in line:
if initial_grid[i][j]:
model.Add(grid[(i, j)] == initial_grid[i][j]) 添加游戏规则
接下来我们要将游戏规则告诉cp_model,并往网格变量中添加游戏规则的约束:
1.行约束: 每行的元素都不一样。
2.列约束: 每列的元素都不一样。
3.cell(小网格)约束: 每个cell内的元素都不一样。
#行约束: 每行的元素都不一样
for i in line:
model.AddAllDifferent([grid[(i, j)] for j in line])
#列约束: 每列的元素都不一样
for j in line:
model.AddAllDifferent([grid[(i, j)] for i in line])
#cell约束: 每个cell内的元素都不一样
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.AddAllDifferent(one_cell) 求解
设定好游戏规则以后,我们就可以舒舒服服的让or-tools来替我们完成剩下的求解过程,就这么简单!最后我们的程序仅耗时3毫秒就破解了这个数独游戏。
%%time
# 求解
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])参考资料:
以上代码来自于:https://github.com/google/or-tools/blob/master/examples/python/sudoku_sat.py
边栏推荐
- Ninja file syntax learning
- 3D image classification of lung CT scan using propeller
- 论文解读:《BERT4Bitter:一种基于transformer(BERT)双向编码器表示用于改善苦肽预测的基础模型》
- CPC client installation tutorial
- Build "green computing" and interpret "Intelligent Computing Center"
- 深度学习-神经网络
- 绿色数据中心“东数西算”全面启动
- Interpretation of the paper: a convolutional neural network for identifying N6 methyladenine sites in rice genome using dinucleotide one hot encoder
- 生命科学领域下的医药研发通过什么技术?冷冻电镜?分子模拟?IND?
- 深度卷积生成对抗网络
猜你喜欢

All kinds of ice! Use paddegan of the propeller to realize makeup migration

“东数西算”数据中心下算力、AI智能芯片如何发展?

2021信息科学Top10发展态势。深度学习?卷积神经网络?

笔记 | 百度飞浆AI达人创造营:数据获取与处理(以CV任务为主)

Six trends and eight technologies of high-performance computing in data centers under "data center white paper 2022" and "computing from the east to the west"

Pytoch personal record (do not open)

NVIDIA 英伟达发布H100 GPU,水冷服务器适配在路上

方法的定义应用

VIO---Boundle Adjustment求解过程

Eigen多版本库安装
随机推荐
NLP自然语言处理-机器学习和自然语言处理介绍(一)
Service服务
飞桨高层API实现人脸关键点检测
Maybe I can't escape class! How to use paddlex to point the head?
strand
How to develop the computing power and AI intelligent chips in the data center of "digital computing in the East and digital computing in the west"?
對.h5文件的迭代顯示,h5py數據操作
机器学习/深度学习必备数学知识
读写文件数据
The data set needed to generate yolov3 from the existing voc207 data set, and the places that need to be modified to officially start the debugging program
论文解读:《功能基因组学transformer模型的可解释性》
Interpretation of yolov3 key code
Practical convolution correlation trick
2021信息科学Top10发展态势。深度学习?卷积神经网络?
MySQL uninstall
Necessary mathematical knowledge for machine learning / deep learning
论文解读:《利用注意力机制提高DNA的N6-甲基腺嘌呤位点的鉴定》
What is the difference between abstract classes and interfaces?
Static linked list
Service Service
