小编Edu*_*eia的帖子

带约束规划的方形拼图问题解决方案

问题:用不接触或重叠的正方形(任何大小)填充网格,即使在角落处也不例外。下面和右侧的数字表示填充在相应列/行中的网格方块数。

为了解决这个问题,我应用了以下约束:放置的正方形应该是不相交的,为了确保网格正方形的数量是正确的,我将与给定行/列相交的正方形的长度总和限制为等于该行/列号。

但是,输出的解决方案是 [1, 0, 0, 1] ([NumSquares, X, Y, SquareSize],一个在坐标 (0, 0) 中长度为 1 的正方形,它应该是右图(13 个不同大小和坐标的方块)。

:- use_module(library(clpfd)).

:- include('utils.pl').

solve(Rows, Columns, Vars) :-
    % Domain and variables definition

    length(Rows, Size),   

    MaxNumSquares is Size * Size,                
    NumSquares #>= 0,                               
    NumSquares #< MaxNumSquares,      

    length(StartsX, NumSquares),                    
    length(StartsY, NumSquares),                   
    length(SquareSizes, NumSquares),                

    S is Size - 1,           
                           
    domain(StartsX, 0, S),                         
    domain(StartsY, 0, S),                          
    domain(SquareSizes, 1, Size),                  

    construct_squares(Size, StartsX, StartsY, SquareSizes, Squares), 

    % Constraints

    disjoint2(Squares, [margin(0, 0, 1, 1)]),
    lines_constraints(0, Rows, StartsX, SquareSizes), …
Run Code Online (Sandbox Code Playgroud)

prolog sicstus-prolog clpfd

7
推荐指数
1
解决办法
287
查看次数

Python 中的 math.copysign(x, y) 有什么用?

我正在查看 Python 中 math 模块的功能,并找到了 math.copysign (x,y) 函数。我想知道什么时候有人会使用这个功能。我查看了许多网站,它们展示了如何使用该功能。我了解如何使用它,但我想知道在哪些情况下获取第一个参数的绝对值和第二个参数的符号会很有用。

math.copysign(x, y)
Run Code Online (Sandbox Code Playgroud)

python math function

0
推荐指数
1
解决办法
61
查看次数

标签 统计

clpfd ×1

function ×1

math ×1

prolog ×1

python ×1

sicstus-prolog ×1