在解决约束问题时需要帮助

时间:2011-05-15 10:26:36

标签: algorithm artificial-intelligence constraints constraint-programming

我想使用约束来解决以下问题,但实际上我不知道从哪里开始,所以我决定在这里发帖求助。

*** Fitting squares ***

    Given the set of black squares of Figure 1 (a 2x2, 3x3, 4x4 and a 5x5 square),
    fit them all into the white rectangle of Figure 1 (a 7x9 rectangle), and this in
    such a way that there is no overlap between the squares.
    Note that the black squares can only be put on integer coordinates. 

    Formulate the problem above as a constraint problem. Come up with a useful
    representation of the problem in terms of the constraint processing tool.
   Provide an explanation of indices, variables and domains. Furthermore, 
   provide for every introduced constraint, 
    the meaning of the constraint in natural language.

请这不是作业,因为我已经解决了自己添加方块和圆圈的问题。但是这个我不知道如何以及从哪里开始。我作为一个初学者学习这个约束的东西,并不知道如何解决这个问题并需要帮助。我假设使用以下语法来定义变量和约束:

* VARIABLES *

1)

名称必须以大写[A-Z]开头,并且仅使用[A-Za-z0-9_]。 示例:X或MyVar_1。

2)

Domain是[from,...,to]范围内所有整数的集合。确保从≤到。

第3)

索引是指索引变量的(单个!)索引。例如,如果为变量“X”输入1到8的索引范围,则“X(1)”,“X(2)”,...,“X(8)”中的每一个都是正常变量使用相同的域名。指定索引范围,使得从≤到。如果将“索引”字段留空,则假定您的变量是非索引变量。 (注意:使用from = to是可能的,虽然它没有多大意义:你可以使用非索引变量。) 不要使用相同的名称两次,也不要使用一次正常,一次使用索引变量!也不要重复使用部分变量名称,例如如果您已经拥有MyVar_1,请不要使用Var。

约束

如果需要,您可以输入算术约束,并为已使用的索引指定范围。算术约束的形式为Expr~Expr,其中Expr是一个使用整数,声明变量和一些算术的表达式,其中〜是关系运算符。

1)

使用索引变量时,必须指定索引

  • 格式为MyVar(索引),即在变量名后面使用“(”和“)”,中间没有空格。
  • 请注意,index必须是变量,因此不允许使用MyVar(37)之类的东西。改写MyVar(i),输入i = 37的范围。
  • 此外,索引必须出现在“范围”字段中。提示:如果您的约束必须适用于整个索引范围,例如i = 1..10,然后输入一个非常满意的范围,例如i> 0。
  • 索引名称可以在约束中显示,即不作为索引。
  • 也允许格式为MyVar(index + x)或MyVar(index-x),其中x是整数。不要在'+'或' - '之前或之后使用空格! 的 2)

算术使用运算符'+',' - ','*','/','mod',其中'X / Y'是X和Y(向下舍入)的商。也允许'min(X,Y)','max(X,Y)','abs(X)'。

第3) 可用的关系运算符是'=','\ =','<','>','=<','> =',具有明显的含义。

4) 使用圆括号消除歧义! 实例:X(i)+ Y(j)< 12或min(X(i),X(j))\ = Z或X(i)* i = 20.

也可以使用布尔表达式。允许的布尔连接词分别为'&lt; =&gt;','=&gt;','/ \'和'\ /',分别用于等价,暗示,连接和析取。

范围是一个表达式,其中包含约束中使用的每个索引。例子:i&lt; j或i = j或(i = j / \ k \ = l)。范围表达式不应包含“&lt; =&gt;”,“=&gt;”或“\ /”。 注意:范围是一个逻辑表达式(隐式普遍量化),而不是像i = 1..10那样的集合表达式。

以下示例使用以上语法:

You can solve, e.g., some linear integer equations using normal variables only:

Name: X, domain: 1..10000
Name: Y, domain: 1..10000
Name: Z, domain: 1..1000000
Constraint: 29*X + 43*Y = Z
Constraint: X * Y = Z

Another example is the N-queens problem which uses index variables:

Name: Q(i), i=1..4, domain: 1..4
Constraint: Q(i) \= Q(j), range: i < j
Constraint: abs(Q(i) - Q(j)) \= j - i, range: i < j

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

假设放置i×i平方(i = 2,3,4或5)使得其左下角位于(X(i),Y(i))。因此,正方形占据了从左下角的(X(i),Y(i))到右上角的(X(i)+ i,Y(i)+ i))的区域。现在你想说j×j方块与这个方块不重叠。只有当它完全位于右侧,完全位于左侧,完全位于该正方形之上或完全位于该正方形之下时,才会发生这种情况。因此:

Name X(i), i=2..5, domain: 1..7
Name Y(i), i=2..5, domain: 1..9
Constraint: X(i)+i=<X(j) \/ X(j)+j=<X(i) \/ Y(i)+i=<Y(j) \/ Y(j)+j=<Y(i), range: i<j
相关问题