这可以分两步解决。对于以性别和地区为维度的示例,我将描述如何做到这一点。然后我将描述更一般的情况。在第一步中,我们求解一个包含 8 个变量的方程组,然后我们采用由第一步中找到的解限制的 8 个选择语句的不相交并集。请注意,任何行只有 8 种可能性。他们可以是男性或女性,然后该地区是北方,南方,东方或西方之一。现在让,
X1 equal the number of rows that are male and from the north,
X2 equal the number of rows that are male and from the south,
X3 equal the number of rows that are male and from the east,
X4 equal then number that are male and from the west
X5 equal the number of rows that are female and from the north,
X6 equal the number of rows that are female and from the south,
X7 equal the number of rows that are female and from the east,
X8 equal then number that are female and from the west
方程式是:
X1+X2+X3+X4=600
X5+X6+X7+X8=400
X1+X5=100
X2+X6=200
X3+X7=300
X4+X8=400
现在求解上面的 X1,X2,...X8。有很多解决方案(我会描述如何解决)这里有一个解决方案:
X1=60, X2=120, X3=180,X4=240,X5=40,X6=80,X7=120,X8=160.
现在我们可以通过 8 个选择的简单联合得到结果:
(select * from user where gender='m' and region="north" limit 60)
union distinct(select * from user where gender='m' and region='south' limit 120)
union distinct(select * from user where gender='m' and region='east' limit 180)
union distinct(select * from user where gender='m' and region='west' limit 240)
union distinct(select * from user where gender='f' and region='north' limit 40)
union distinct(select * from user where gender='f' and region='south' limit 80)
union distinct(select * from user where gender='f' and region='east' limit 120)
union distinct(select * from user where gender='f' and region='west' limit 160);
请注意,如果数据库中没有 60 行满足上面的第一个选择,那么给出的特定解决方案将不起作用。所以我们必须添加其他约束,LT:
0<X1 <= (select count(*) from user where from user where gender='m' and region="north")
0<X2 <= (select count(*) from user where gender='m' and region='south')
0<X3 <= (select count(*) from user where gender='m' and region='east' )
0<X4 <= (select count(*) from user where gender='m' and region='west')
0<X5 <= (select count(*) from user where gender='f' and region='north' )
0<X6 <= (select count(*) from user where gender='f' and region='south')
0<X7 <= (select count(*) from user where gender='f' and region='east' )
0<X8 <= (select count(*) from user where gender='f' and region='west');
现在让我们概括一下这种允许任何拆分的情况。方程为 E:
X1+X2+X3+X4=n1
X5+X6+X7+X8=n2
X1+X5=m1
X2+X6=m2
X3+X7=m3
X4+X8=m4
Th 数 n1,n2,m1,m2,m3,m4 被给定并且满足 n1+n2=(m1+m2+m3+m4)。因此,我们将问题简化为求解上述方程 LT 和 E。这只是一个线性规划问题,可以使用单纯形法或其他方法解决。另一种可能性是将其视为线性丢番图方程组并使用方法来找到解决方案。无论如何,我已经将问题简化为找到上述方程的解。 (鉴于方程是一种特殊形式,可能有比使用单纯形法或求解线性丢番图方程组更快的方法)。一旦我们求解 Xi,最终的解是:
(select * from user where gender='m' and region="north" limit :X1)
union distinct(select * from user where gender='m' and region='south' limit :X2)
union distinct(select * from user where gender='m' and region='east' limit :X3)
union distinct(select * from user where gender='m' and region='west' limit :X4)
union distinct(select * from user where gender='f' and region='north' limit :X5)
union distinct(select * from user where gender='f' and region='south' limit :X6)
union distinct(select * from user where gender='f' and region='east' limit :X7)
union distinct(select * from user where gender='f' and region='west' limit :X8);
让我们将具有 n 个可能性的维度 D 表示为 D:n。假设您有 D1:n1、D2:n2、...DM:nM 维度。将生成 n1*n2*...nM 变量。生成的方程数为 n1+n2+...nM。而不是定义通用方法,让我们以另一种 3 维、4 维和 2 维的情况为例;让我们将 D1 的可能值称为 d11、d12、d13,D2 是 d21、d22、d23、d24,而 D3 的值是 d31、d32。我们将有 24 个变量,方程是:
X1 + X2 + ...X8=n11
X9 + X10 + ..X16=n12
X17+X18 + ...X24=n13
X1+X2+X9+x10+x17+x18=n21
X3+X4+X11+x12+x19+x20=n22
X5+X6+X13+x14+x21+x22=n23
X7+X8+X15+x116+x23+x24=n24
X1+X3+X5+...X23=n31
X2+X4+......X24=n32
在哪里
X1 equals number with D1=d11 and D2=d21 and D3=d31
X2 equals number with D1=d11 and D2=d21 and D3 = d31
....
X24 equals number with D1=D13 and D2=d24, and D3=d32.
添加小于限制。然后求解 X1,X2,... X24。创建 24 个选择语句并采用不相交的联合。
我们可以对任何维度进行类似的求解。
总而言之:给定维度 D1:n1, D2:n2, ...DM:nM 我们可以解决上述针对 n1*n2*...nM 变量的相应线性规划问题,然后通过以下方式生成解决方案对 n1*n2*...nM 选择语句进行不相交的联合。所以是的,我们可以通过 select 语句生成解决方案,但首先我们必须通过获取每个 n1*n2*...nM 变量的计数来求解方程并确定限制。
即使赏金已经结束,我也会为您感兴趣的人添加更多内容。我在这里声称,如果有解决方案,我已经完全展示了如何解决这个问题。
澄清我的方法。在 3 维的情况下,假设我们将年龄分成 3 种可能性之一。然后很好地使用问题中的性别和地区。每个用户有 24 种不同的可能性,对应于他们在这些类别中的位置。设 Xi 为最终结果中每种可能性的数量。让我写一个矩阵,其中每一行代表每种可能性中的一种。每个用户最多将贡献 1 到 m 或 f,1 到北、南、东或西,以及 1 到年龄类别。用户只有 24 种可能性。让我们展示一个矩阵:(abc) 3 个年龄,(nsew) 地区和
(mf) 男性或女性:a 为小于或等于 10 岁,b 为 11 至 30 岁,c 为 31 至 50 岁。
abc nsew mf
X1 100 1000 10
X2 100 1000 01
X3 100 0100 10
X4 100 0100 01
X5 100 0010 10
X6 100 0010 01
X7 100 0001 10
X8 100 0001 01
X9 010 1000 10
X10 010 1000 01
X11 010 0100 10
X12 010 0100 01
X13 010 0010 10
X14 010 0010 01
X15 010 0001 10
X16 010 0001 01
X17 001 1000 10
X18 001 1000 01
X19 001 0100 10
X20 001 0100 01
X21 001 0010 10
X22 001 0010 01
X23 001 0001 10
X24 001 0001 01
每一行代表一个用户,如果它对结果有贡献,则列中有一个 1。例如,第一行显示 1 代表 a,1 代表 n,1 代表 m。这意味着用户的年龄小于或等于10,来自北方并且是男性。
Xi 表示最终结果中有多少这样的行。因此,假设 X1 为 10,这意味着我们说最终结果有 10 个结果,所有这些结果都来自北方,都是男性,小于或等于 10。好吧,现在我们只需要将它们相加。请注意,前 8 个X1+X2+X3+X4+X5+X6+X7+X8 是年龄小于或等于 10 的所有行。它们必须加起来就是我们为该类别选择的任何内容。接下来的 2 组 8 组也是如此。
到目前为止,我们得到了方程式:(na 是年龄小于 10 的数字,nb 是 10 到 20 之间的年龄,nc 是年龄小于 50 的数字
X1+X2+X3+X4+X5+X6+X7+X8 = na
X9+X10+X11 + .... X16 = nb
X17+X18+X19+... X24=nc
这些是年龄划分。现在让我们看看区域拆分。只需将“n”列中的变量相加,
X1+X2+X9+X10+X17+X18 = nn
X3+X4+X11+X12+X19+20=ns
...
等等。
你知道我是如何通过向下看列来得到这些方程的吗?
继续 ew 和 mf。总共给出 3+4+2 个方程。所以我在这里做的很简单。我推断您选择的任何行都会对 3 个维度中的每个维度贡献一个,并且只有 24 种可能性。然后让 Xi 成为每种可能性的数字,你就得到了需要求解的方程。在我看来,无论你想出什么方法,都必须是这些方程的解。换句话说,我只是根据求解这些方程重新表述了问题。
现在我们需要一个整数解,因为我们不能有小数行。请注意,这些都是线性方程。但我们想要一个整数解。以下是描述如何解决这些问题的论文的链接:https://www.math.uwaterloo.ca/~wgilbert/Research/GilbertPathria.pdf