Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 数独解算器性能问题_Performance_Prolog_Solver_Sudoku - Fatal编程技术网

Performance 数独解算器性能问题

Performance 数独解算器性能问题,performance,prolog,solver,sudoku,Performance,Prolog,Solver,Sudoku,我的数独prolog解算器有问题。它在工作,但性能很差。对于小的,它工作得很好,但像9x9或更大的需要10分钟或更多时间。我想让它像现在一样无限大。有人能帮忙吗 solve_sudoku(Rows,Sol):- length(Rows,Max), maplist(same_length(Rows),Rows), append(Rows, List), List ins 1..Max, maplist(all_distinct, Rows), transpo

我的数独prolog解算器有问题。它在工作,但性能很差。对于小的,它工作得很好,但像9x9或更大的需要10分钟或更多时间。我想让它像现在一样无限大。有人能帮忙吗

solve_sudoku(Rows,Sol):-
    length(Rows,Max),
    maplist(same_length(Rows),Rows),
    append(Rows, List), List ins 1..Max,
    maplist(all_distinct, Rows),
    transpose(Rows, Columns),
    maplist(all_distinct,Columns),
    maplist(label,Rows),
    boxes(Boxes,Rows),
    maplist(all_distinct,Boxes),
    boxes_distinct(Boxes),
    Sol = Rows.

boxes(Bs,M) :-
    length(M,Len),
    Sq is round(sqrt(Len)),
    findall(B, (between(1, Sq, R),
            between(1, Sq, C),
            block(M, Sq, R, C, B)), Bs).

cell(M, R,C, V) :-
    nth1(R,M,Row), nth1(C,Row,V).

block(M, Sq, R,C, B) :-
    findall(V, (between(1, Sq, X),
            between(1, Sq, Y),
            I is (R-1) * Sq + X,
            J is (C-1) * Sq + Y,
            cell(M, I, J, V)), B).

boxes_distinct([]).
boxes_distinct([BH|BT]):-
    all_distinct(BH),
    boxes_distinct(BT).

输入是一个列表,其中包含要求解的数独,输出是作为列表的已求解数独。

我认为您应该调用

maplist(label,Rows)
之后

通常,您应该在声明所有约束后调用label或Labeling

并使用“带ff或ffc选项的标记谓词”代替“标签”
可以提高效率。

我想你应该打电话来

maplist(label,Rows)
之后

通常,您应该在声明所有约束后调用label或Labeling

并使用“带ff或ffc选项的标记谓词”代替“标签”
可能会提高效率。

box\u distinct(box)
似乎没有用,已经被
maplist(all\u distinct,box)
覆盖,并在子句末尾附近使用
标签(List)
(而不是
maplist(label,Rows)
)。无论如何,@gabrielete(+1)

框体不同(框体)
似乎没有用,已经被
映射列表(所有不同,框体)
覆盖,并在子句末尾附近使用
标签(列表)
(而不是
映射列表(标签,行)
)。不管怎样,主要的问题已经被@加布里埃特(+ 1)

指示,除了上面的,考虑尝试平坦化(行,ALVALL),标记([FFC],ALLVARS),而不是MAPLIST(标签,行),因为编译一个列表中的所有值,让CPLFD库选择哪个变量的域被缩小。除此之外,考虑尝试平坦化(行,ALALVALL),标记([ffc],AllVals)而不是映射列表(label,Rows),因为编译一个列表中的所有值可以让clpfd库选择下一个缩小哪个变量的域。