数独优化求解C库tdoku-lib的使用

tdoku-lib是基于优化求解器tdoku改造的动态库和静态库,它的存储库地址 https://github.com/hackerzhuli/tdoku-lib
1.拉取源代码

root@DESKTOP-59T6U68:/mnt/c/d# git clone https://github.com/hackerzhuli/tdoku-lib.git Cloning into 'tdoku-lib'... remote: Enumerating objects: 1230, done. remote: Counting objects: 100% (339/339), done. remote: Compressing objects: 100% (77/77), done. remote: Total 1230 (delta 288), reused 284 (delta 255), pack-reused 891 (from 1) Receiving objects: 100% (1230/1230), 72.96 MiB | 3.20 MiB/s, done. Resolving deltas: 100% (885/885), done. Updating files: 100% (27/27), done.

2.编译

root@DESKTOP-59T6U68:/mnt/c/d# cd tdoku-lib root@DESKTOP-59T6U68:/mnt/c/d/tdoku-lib# docker start gcc gcc root@DESKTOP-59T6U68:/mnt/c/d/tdoku-lib# docker exec -it gcc bash root@6ae32a5ffcde:/# cd /par/tdoku-lib/ root@6ae32a5ffcde:/par/tdoku-lib# ./BUILD.sh -- The C compiler identification is GNU 14.2.0 -- The CXX compiler identification is GNU 14.2.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/local/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /par/tdoku-lib/build [ 6%] Building CXX object CMakeFiles/tdoku_object.dir/src/solver_dpll_triad_simd.cc.o [ 12%] Building CXX object CMakeFiles/tdoku_object.dir/src/solver_basic.cc.o [ 18%] Building CXX object CMakeFiles/tdoku_object.dir/src/solver_dpll_triad_scc.cc.o /par/tdoku-lib/src/solver_dpll_triad_scc.cc:20:11: error: 'uint16_t' does not name a type 20 | constexpr uint16_t kNumLiterals = kNumBoxes * kNumPosClausesPerBox * kNumValues * 2; | ^~~~~~~~ /par/tdoku-lib/src/solver_dpll_triad_scc.cc:11:1: note: 'uint16_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>' 10 | #include <vector> +++ |+#include <cstdint> 11 | ... /par/tdoku-lib/src/solver_dpll_triad_scc.cc:526:38: error: 'Literal' was not declared in this scope 526 | if (result_.asserted[Literal(box, elm, val)]) { | ^~~~~~~ /par/tdoku-lib/src/solver_dpll_triad_scc.cc: At global scope: /par/tdoku-lib/src/solver_dpll_triad_scc.cc:540:65: error: 'uint32_t' has not been declared 540 | size_t TdokuSolverDpllTriadScc(const char *input, size_t limit, uint32_t configuration, | ^~~~~~~~ make[2]: *** [CMakeFiles/tdoku_object.dir/build.make:104: CMakeFiles/tdoku_object.dir/src/solver_dpll_triad_scc.cc.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:93: CMakeFiles/tdoku_object.dir/all] Error 2 make: *** [Makefile:91: all] Error 2

编译出错。
按照编译的提示,在solver_dpll_triad_scc.cc的开头加上#include <cstdint>,重新编译通过

root@6ae32a5ffcde:/par/tdoku-lib# ./BUILD.sh ... [ 6%] Building CXX object CMakeFiles/tdoku_object.dir/src/solver_dpll_triad_simd.cc.o [ 12%] Building CXX object CMakeFiles/tdoku_object.dir/src/solver_basic.cc.o [ 18%] Building CXX object CMakeFiles/tdoku_object.dir/src/solver_dpll_triad_scc.cc.o [ 25%] Building CXX object CMakeFiles/tdoku_object.dir/src/util.cc.o [ 31%] Building CXX object CMakeFiles/tdoku_object.dir/src/generate.cc.o [ 37%] Building CXX object CMakeFiles/tdoku_object.dir/src/solve.cc.o make[2]: warning: Clock skew detected. Your build may be incomplete. [ 37%] Built target tdoku_object [ 43%] Linking CXX static library libtdoku_static.a [ 43%] Built target tdoku_static [ 50%] Linking CXX shared library libtdoku_shared.so [ 50%] Built target tdoku_shared [ 56%] Building CXX object CMakeFiles/run_tests.dir/test/run_tests.cc.o [ 62%] Building CXX object CMakeFiles/run_tests.dir/src/util.cc.o [ 68%] Building CXX object CMakeFiles/run_tests.dir/src/solver_dpll_triad_simd.cc.o [ 75%] Linking CXX executable run_tests [ 75%] Built target run_tests [ 81%] Building CXX object CMakeFiles/grid_lib.dir/src/grid_lib.cc.o [ 87%] Linking CXX static library libgrid_lib.a [ 87%] Built target grid_lib [ 93%] Building CXX object CMakeFiles/grid_tools.dir/src/grid_tools.cc.o [100%] Linking CXX executable grid_tools [100%] Built target grid_tools make[1]: warning: Clock skew detected. Your build may be incomplete. make: warning: Clock skew detected. Your build may be incomplete. root@6ae32a5ffcde:/par/tdoku-lib#

3.执行
执行编译出来的run_tests文件,显示PASS: tdoku

root@6ae32a5ffcde:/par/tdoku-lib/build# ./run_tests Error opening test/test_puzzles root@6ae32a5ffcde:/par/tdoku-lib/build# cd .. root@6ae32a5ffcde:/par/tdoku-lib# build/run_tests PASS: tdoku

再编译示例文件,

#include"../include/tdoku.h"#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,constchar**argv){size_tlimit=argc>1?atoll(argv[1]):10000;char*puzzle=NULL;charsolution[81];size_tsize,guesses;while(getline(&puzzle,&size,stdin)!=-1){if(strlen(puzzle)<81||puzzle[0]=='#')continue;solution[0]='\0';size_tcount=SolveSudoku(puzzle,limit,0,solution,&guesses);if(limit>1&&count==1){SolveSudoku(puzzle,1,0,solution,&guesses);}printf("%.81s:%ld:%.81s\n",puzzle,count,solution);}}

slove求解文件中的数独题目,空格用“.”表示。结果输出到控制台,格式是题目:解序号:解

root@6ae32a5ffcde:/par/tdoku-lib# gcc example/solve.c build/libtdoku_static.a -O3 -o solve -lstdc++ -lm root@6ae32a5ffcde:/par/tdoku-lib# unzip data.zip root@6ae32a5ffcde:/par/tdoku-lib# ./solve < data/puzzles0_kaggle ..2..76416712.....5..3..28.46......9.35.18.6...7.93..4.9.5.61...5..7...3..48...2.:1:382957641671284935549361287468725319935418762127693854293546178856172493714839526 ...6.2..9.3..1.6..1.238.5.4.15...96....94.....27..38.58.42..3.7.....8.....6.7125.:1:748652139539714682162389574415827963683945721927163845854296317271538496396471258 ^C

示例还有python程序

importsysfromctypesimport*classTdoku:def__init__(self):self.__tdoku=CDLL("build/libtdoku_shared.so")self.__solve=self.__tdoku.TdokuSolverDpllTriadSimd self.__solve.restype=c_ulonglong self.__constrain=self.__tdoku.TdokuConstrain self.__constrain.restype=c_bool self.__minimize=self.__tdoku.TdokuMinimize self.__minimize.restype=c_booldefSolve(self,puzzle):iftype(puzzle)isstr:puzzle=str.encode(puzzle)limit=c_ulonglong(1)config=c_ulong(0)solution=create_string_buffer(81)guesses=c_ulonglong(0)count=self.__solve(c_char_p(puzzle),limit,config,solution,pointer(guesses))ifcount:returncount,solution.value.decode(),guesses.valueelse:return0,"",guessesdefCount(self,puzzle,limit=2):iftype(puzzle)isstr:puzzle=str.encode(puzzle)limit=c_ulonglong(limit)config=c_ulong(0)solution=create_string_buffer(81)guesses=c_ulonglong(0)count=self.__solve(c_char_p(puzzle),limit,config,solution,pointer(guesses))returncountdefConstrain(self,partial_puzzle):iftype(partial_puzzle)isstr:buffer=create_string_buffer(str.encode(partial_puzzle))else:buffer=create_string_buffer(partial_puzzle)pencilmark=c_bool(False)self.__constrain(pencilmark,buffer)returnbuffer.valuedefMinimize(self,non_minimal_puzzle):iftype(non_minimal_puzzle)isstr:buffer=create_string_buffer(str.encode(non_minimal_puzzle))else:buffer=create_string_buffer(non_minimal_puzzle)pencilmark=c_bool(False)monotonic=c_bool(False)self.__minimize(pencilmark,monotonic,buffer)returnbuffer.valueif__name__=='__main__':tdoku=Tdoku()iflen(sys.argv)>1:filename=sys.argv[1]else:filename='data/puzzles2_17_clue'withopen(filename,'r')asf:forpuzzleinf.readlines():iflen(puzzle)>=81andnotpuzzle.startswith('#'):count,solution,guesses=tdoku.Solve(puzzle)print("%.81s:%lu:%.81s:%lu"%(puzzle,count,solution,guesses))

这次还多出来一列猜测数

root@6ae32a5ffcde:/par/tdoku-lib# python3 example/solve.py data/puzzles0_kaggle|more .5..83.17...1..4..3.4..56.8....3...9.9.8245....6....7...9....5...729..861.36.72.4:1:652483917978162435314975628825736149791824563436519872269348751547291386183657294:0 2.6.3......1.65.7..471.8.5.5......29..8.194.6...42...1....428..6.93....5.7.....13:1:256734198891265374347198652514683729728519436963427581135942867689371245472856913:0 Traceback (most recent call last): File "/par/tdoku-lib/example/solve.py", line 73, in <module> print("%.81s:%lu:%.81s:%lu" % (puzzle, count, solution, guesses)) BrokenPipeError: [Errno 32] Broken pipe Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> BrokenPipeError: [Errno 32] Broken pip

用49150道17个未知数的题目测试性能,并与dlx程序比较

root@6ae32a5ffcde:/par/tdoku-lib# time ./solve < data/puzzles2_17_clue >17result.txt real 0m0.622s user 0m0.229s sys 0m0.017s root@6ae32a5ffcde:/par/1230# time ./dlxline <sudoku17.txt >sudoku17outdlx.txt real 0m1.568s user 0m1.122s sys 0m0.073s

tdoku用时只有dlx的40%。采用不同的文件是因为两个程序要求的空格符不同,一个是.,另一个是0。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/1160598.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI原生应用云端推理的故障排查与恢复

AI原生应用云端推理的故障排查与恢复:让智能服务“不掉线”的秘密 关键词:AI原生应用、云端推理、故障排查、恢复机制、AIOps 摘要:当你用手机拍照识别植物品种时,当智能客服秒级回复你的问题时,当电商APP精准推荐商品时——这些“丝滑”体验的背后,是AI原生应用在云端高…

dlx求解数独duckdb插件的编写和使用

1.将网上下载的dlx求解c程序添加int sudoku(const char *s,char *r)函数处理81个字符长的数独题目字符串 #include <cstdio> #include <cstring> #include <ctime> int cnt0; const int XSIZE 3; const int SIZE XSIZE * XSIZE; const int MAX_C SIZE *…

我用 XinServer 做了个文件系统,比想象简单

我用 XinServer 做了个文件系统&#xff0c;比想象简单 最近有个朋友找我帮忙&#xff0c;说他们团队想做个内部文件管理系统&#xff0c;让不同部门的同事能上传、下载、共享文档&#xff0c;还要有权限控制。他问我&#xff1a;“这个后端大概要搞多久&#xff1f;我们前端倒…

大数据领域数据产品的安全保障策略

大数据领域数据产品的安全保障策略&#xff1a;从全生命周期到体系化防御 引言&#xff1a;当大数据产品遇到安全“灰犀牛” 清晨打开手机&#xff0c;你收到一条推送&#xff1a;“某电商平台2000万用户信息泄露&#xff0c;含手机号、地址、购物记录”&#xff1b;下午参加…

避坑指南:通义千问2.5-7B-Instruct本地部署常见问题解决

避坑指南&#xff1a;通义千问2.5-7B-Instruct本地部署常见问题解决 1. 引言 1.1 业务场景描述 随着大模型在企业级应用和开发者项目中的普及&#xff0c;越来越多团队选择将高性能、可商用的开源模型部署至本地环境&#xff0c;以实现数据隐私保护、低延迟响应和定制化功能…

【RuoYi-SpringBoot3-Pro】:使用 Dify + AI 快速生成多数据库建表语句

【RuoYi-SpringBoot3-Pro】&#xff1a;使用 Dify AI 快速生成多数据库建表语句告别手写 SQL&#xff0c;一句话生成标准化建表语句&#xff0c;支持 MySQL、PostgreSQL、openGauss、SQLite 多种数据库&#xff0c;再也不用为给字段起名字发愁了。GitHub:https://github.com/u…

AnimeGANv2版本回滚机制:模型更新失败应急部署教程

AnimeGANv2版本回滚机制&#xff1a;模型更新失败应急部署教程 1. 引言 1.1 业务场景描述 在AI图像风格迁移应用中&#xff0c;AnimeGANv2 因其轻量高效、画风唯美的特性&#xff0c;广泛应用于二次元头像生成、社交内容创作等场景。随着模型迭代加速&#xff0c;开发者常通…

这份无线联网智能门锁系统清单非常专业,清晰地勾勒出了一套适用于多业态、高流动性、强管理场景的现代化出入口解决方案。这不仅是设备清单,更是一套“去中心化部署、云端化管理”的智慧运营蓝图。

无线联网智能门锁系统——专为公寓、办公、宿舍、民宿与酒店打造的全场景智能门禁解决方案这套无线联网智能门锁系统&#xff0c;专为高流动性、多权限管理的场景设计&#xff0c;已广泛应用于公寓、企业宿舍、办公场所、民宿及酒店等环境&#xff0c;实现“一卡通行、远程管控…

【RuoYi-SpringBoot3-Pro】:多租户功能上手指南

【RuoYi-SpringBoot3-Pro】&#xff1a;多租户功能上手指南 做 SaaS 系统最头疼的是什么&#xff1f;肯定是数据隔离。 RuoYi-SpringBoot3-Pro[1] 直接集成了 MyBatis-Plus 的多租户插件&#xff08;TenantLineInnerInterceptor&#xff09;&#xff0c;不用再关注租户 ID&am…

提示工程架构师经验:如何用Prompt解决客服复杂问题?

提示工程架构师经验&#xff1a;如何用Prompt解决客服复杂问题&#xff1f; 一、引言&#xff1a;客服AI的「尴尬时刻」&#xff0c;你遇到过吗&#xff1f; 上周晚饭后&#xff0c;我帮妈妈处理网购纠纷——她买的养生壶收到时底座裂了&#xff0c;联系客服AI得到回复&#xf…

对象库未注册-VB6企业版控件加载不了MSCOMCTL.ocx

关于WIN7下VB6中MicrosoftWindowsCommonControls6.0(SP6)加载提示“对象库未注册”的一种解决办法​​我之前在另外一台电脑上加上了进度条控件&#xff0c;使用正常&#xff1b;换了一台电脑之后&#xff0c;去“部件”中加入Microsoft Windows Common Controls 6.0 (SP6)时&a…

动漫生成服务SLA保障:AnimeGANv2高可用部署架构

动漫生成服务SLA保障&#xff1a;AnimeGANv2高可用部署架构 1. 引言 1.1 业务场景描述 随着AI图像风格迁移技术的普及&#xff0c;用户对“照片转动漫”类应用的需求迅速增长。尤其在社交娱乐、头像生成、内容创作等领域&#xff0c;基于AnimeGANv2的二次元转换服务因其画风…

go语言对phone脱敏显示

在Go语言中实现手机号脱敏显示主要有以下几种方式&#xff0c;从简单到完整逐步推荐&#xff1a; 一、基础实现&#xff08;字符串切片&#xff09; 最常用且高效的方式是直接使用字符串切片操作&#xff0c;保留前3位和后4位&#xff0c;中间用*替换&#xff1a; go 复制 …

通义千问2.5-7B-Instruct优化技巧:RTX 3060流畅运行指南

通义千问2.5-7B-Instruct优化技巧&#xff1a;RTX 3060流畅运行指南 1. 引言&#xff1a;为何在RTX 3060上部署Qwen2.5-7B-Instruct成为可能 随着大模型技术的快速演进&#xff0c;70亿参数级别的语言模型已逐步从“云端专属”走向本地化部署。通义千问2.5-7B-Instruct作为阿…

农业机械收割机拖拉机数据集6340张VOC+YOLO格式

农业机械收割机拖拉机数据集6340张VOCYOLO格式数据集格式&#xff1a;VOC格式YOLO格式压缩包内含&#xff1a;3个文件夹&#xff0c;分别存储图片、xml、txt文件JPEGImages文件夹中jpg图片总计&#xff1a;6340Annotations文件夹中xml文件总计&#xff1a;6340labels文件夹中tx…

AnimeGANv2如何提高边缘清晰度?后处理滤波技术实战

AnimeGANv2如何提高边缘清晰度&#xff1f;后处理滤波技术实战 1. 背景与问题分析 在基于AnimeGANv2的图像风格迁移应用中&#xff0c;尽管模型本身具备较强的动漫化能力&#xff0c;尤其在人脸保留和色彩渲染方面表现优异&#xff0c;但其生成结果常存在边缘模糊、轮廓失真、…

开源模型新选择:AnimeGANv2宫崎骏风格迁移实战指南

开源模型新选择&#xff1a;AnimeGANv2宫崎骏风格迁移实战指南 1. 引言 随着深度学习在图像生成领域的持续突破&#xff0c;风格迁移技术已从实验室走向大众应用。其中&#xff0c;AnimeGANv2 作为轻量级、高效率的动漫风格迁移模型&#xff0c;凭借其出色的画质表现和极低的…

AnimeGANv2优化指南:处理高分辨率图片的配置建议

AnimeGANv2优化指南&#xff1a;处理高分辨率图片的配置建议 1. 背景与挑战&#xff1a;高分辨率输入下的性能瓶颈 随着用户对图像质量要求的不断提升&#xff0c;将高清照片&#xff08;如1080p、4K&#xff09;转换为二次元风格的需求日益增长。AnimeGANv2 以其轻量级结构和…

keil5安装包下载通俗解释:新手也能轻松掌握

从零开始搭建嵌入式开发环境&#xff1a;Keil5安装与配置实战指南 你是不是也曾在搜索引擎里输入“ keil5安装包下载 ”&#xff0c;却被五花八门的链接、版本号和破解教程搞得一头雾水&#xff1f; 别担心&#xff0c;这不只是你在经历。几乎每一个刚接触STM32或ARM开发的…

车辆及人数据集(汽车公交车收割机拖拉机卡车人)12819张

车辆及人数据集&#xff08;汽车公交车收割机拖拉机卡车人&#xff09;12819张数据集格式&#xff1a;VOC格式YOLO格式压缩包内含&#xff1a;3个文件夹&#xff0c;分别存储图片、xml、txt文件JPEGImages文件夹中jpg图片总计&#xff1a;12819Annotations文件夹中xml文件总计&…