这个问题可以映射到线性多面体上的采样问题,常见的方法是蒙特卡洛方法、随机游走和即时运行方法(参见https://www.jmlr.org/papers/volume19/18-158/18-158.pdf 的简短比较示例)。它与线性规划有关,可以扩展到流形。
在成分数据分析中也有对多面体的分析,例如https://link.springer.com/content/pdf/10.1023/A:1023818214614.pdf,提供平面和多面体之间的可逆变换,可用于采样。
如果您正在处理低维度,您也可以使用拒绝抽样。这意味着您首先在包含多面体的平面上采样(由您的不等式定义)。后一种方法很容易实现(当然也很浪费),下面的 GNU Octave(我让问题的作者用 C 重新实现)代码就是一个例子。
第一个要求是获得与超平面正交的向量。对于 N 个变量的总和,这是 n = (1,...,1)。第二个要求是平面上的一个点。对于您的示例,可能是 p = (S,...,S)/N。
现在平面上的任意点满足 n^T * (x - p) = 0
我们还假设 x_i >= 0
有了这些给定,您可以计算平面上的正交基(向量 n 的零点),然后在该基上创建随机组合。最后,您映射回原始空间并对生成的样本应用约束。
# Example in 3D
dim = 3;
S = 1;
n = ones(dim, 1); # perpendicular vector
p = S * ones(dim, 1) / dim;
# null-space of the perpendicular vector (transposed, i.e. row vector)
# this generates a basis in the plane
V = null (n.');
# These steps are just to reduce the amount of samples that are rejected
# we build a tight bounding box
bb = S * eye(dim); # each column is a corner of the constrained region
# project on the null-space
w_bb = V \ (bb - repmat(p, 1, dim));
wmin = min (w_bb(:));
wmax = max (w_bb(:));
# random combinations and map back
nsamples = 1e3;
w = wmin + (wmax - wmin) * rand(dim - 1, nsamples);
x = V * w + p;
# mask the points inside the polytope
msk = true(1, nsamples);
for i = 1:dim
msk &= (x(i,:) >= 0);
endfor
x_in = x(:, msk); # inside the polytope (your samples)
x_out = x(:, !msk); # outside the polytope
# plot the results
scatter3 (x(1,:), x(2,:), x(3,:), 8, double(msk), 'filled');
hold on
plot3(bb(1,:), bb(2,:), bb(3,:), 'xr')
axis image