可以根据变量的特定总和计算变量值的算法

Algorithm that can compute values of variables based on the result of specific sums of those

这可能是一个重复的问题,但我无法在堆栈溢出时找到它。

取一些变量,它们都代表某事的可能性。这意味着所有变量的值都在零和一之间(包括两者)。这些的值是未知的。但是,我有一些方程式,其中部分或全部这些变量的结果是已知的。

例如,取变量abcxy。它们的值都是介于 0 和 1(包括两者)之间的未知数。我有以下等式:

1: a + b + c = 1
2: a + b + x = 2
3: b + c + y = 2
4: a + x <= 1    ->    0 <= a + x <= 1
5: c + y <= 1    ->    0 <= c + y <= 1

解决这个问题得到以下结果:

2: a + x + b = 2
   (something between 0 and 1) + b = 2
   b = 2 - (something between 0 and 1)
   1 <= b <= 2
   b = 1 (since 0 <= b <= 1 applies too)

1: a + b + c = 1
   a + 1 + c = 1
   a + c = 0
   a = -c
   a = 0  (since 0 <= a <= 1 and 0 <= c <= 1 apply)
   c = 0

2: a + b + x = 2   |   3: b + c + y = 2
   0 + 1 + x = 2   |      1 + 0 + y = 2
   x = 1           |      y = 1

-> a = 0, b = 1, c = 0, x = 1 and y = 1

我在纸上解决了这个问题,我的实际目的是通过为每个未暴露的单元格分别分配一个变量来证明以下扫雷情况x a b c y。由于 xyb 结果是一个,因此可以标记它们:

我的一般目的是使用此技术解决扫雷板,但它可以用于其他软件。但是,如果我想让计算机使用这种技术解决扫雷板问题,则计算机必须使用一种算法来解决具有任意数量的变量和任意数量的方程式的情况。有没有通用算法可以做到这一点?如果有,我应该如何实现该算法?

To make things obvious

  • An equation is one variable or a sum of some variables, having a limited or constant result, which is always positive.
  • A variable is an unknown value between zero and one (both inclusive).
  • The algorithm takes in the amount of variables with the respective variable names, and the equations that define some variables.
  • The algorithm tries to compute as many values as possible. Values that could not be determined remain undetermined.

通常,您有一个 N 维向量 space,N 个自变量可以在该向量上变化。您的每个约束都定义了矢量 space 的一个区域,所有这些区域的交集就是您希望确定的区域。实际上,您正在寻找该区域的最简单(最不复杂)的描述,因为该区域已经由约束集合描述。存在三种可能:第一,该地区没有解决方案;其次,该区域中的解决方案数量有限;第三,该区域有无限多个解。

您的第一步可能是将方程(如果有)视为方程组,并使用求解方程组的算法尽可能多地仅从方程求解。如果不出意外,消除一些变量将有助于下一步。

1: a + b + c = 1
2: a + b + x = 2
3: b + c + y = 2

1: a = 1 - b - c
2: 1 - b - c + b + x = 2
3: b + c + y = 2

1: a = 1 - b - c
2: c = x - 1
3: b + x - 1 + y = 2

1: a = 1 - b - c
2: c = x - 1
3: b = 3 - x - y

1: a = y - 1
2: c = x - 1
3: b = 3 - x - y

我们已经到此为止了。接下来,我们代入我们完整的不等式系统:

A: 0 <= a <= 1
B: 0 <= b <= 1
C: 0 <= c <= 1
D: 0 <= x <= 1
E: 0 <= y <= 1
F: 0 <= a + x <= 1
G: 0 <= c + y <= 1

A: 0 <= y - 1 <= 1
B: 0 <= 3 - x - y <= 1
C: 0 <= x - 1 <= 1
D: 0 <= x <= 1
E: 0 <= y <= 1
F: 0 <= y - 1 + x <= 1
G: 0 <= x - 1 + y <= 1

A: 1 <= y <= 2
B: 3 >= x + y >= 2
C: 1 <= x <= 2
D: 0 <= x <= 1
E: 0 <= y <= 1
F: 1 <= y + x <= 2
G: 1 <= x + y <= 2

此时,您需要单独查找每个变量的约束(如果有)并找到这些约束的交集。

A: 1 <= y <= 2 \
                > taken together, the only solution is y = 1
E: 0 <= y <= 1 /  this is the intersection of intervals [0,1] and [1,2]

C: 1 <= x <= 2 \
                > taken together, the only solution is x = 1
D: 0 <= x <= 1 /  this is the intersection of intervals [0,1] and [1,2]

B: 3 >= x + y >= 2 \  taken together, this means x + y = 2
F: 1 <= y + x <= 2  > this is the intersection of [1,2] and [2,3]
G: 1 <= x + y <= 2 /  note that G turns out to be redundant after subbing

解 x = 1, y = 1 与我们的不等式系统一致。这是唯一的解决方案。我们可以在我们的方程组中反向代入以获得其他变量的值:

1: a = y - 1
     = 1 - 1
     = 0
2: c = x - 1
     = 1 - 1
     = 0
3: b = 3 - x - y
     = 3 - 1 - 1
     = 1

所以解 a = 0, b = 1, c = 0, x = 1, y = 1 有效并且是唯一的解。几乎所有这些步骤都应该是您可以自动化的事情。