我正在寻找用于解决linear complementarity problem的Projected Gauss-Seidel算法的C#实现。到目前为止,我已经在Bullet库中找到了用C ++编写的文件,但遗憾的是它已经过高度优化(因此很难将其翻译成C#)。
在similar问题中,建议您查看numerical libraries for .NET。所有这些都只包含解算systems of linear equations的算法。
编辑:即使我找到了,但它似乎并不完整,所以问题仍然存在。
答案 0 :(得分:9)
经过一周的搜索,我终于找到了this出版物(俄语,基于Kenny Erleben的作品)。在那里描述了一个投射的Gauss-Seidel算法,然后用SOR和终止条件进行扩展。所有这些都与C ++中的示例有关,我将其用于此基本C#实现:
public static double[] Solve (double[,] matrix, double[] right,
double relaxation, int iterations)
{
// Validation omitted
var x = right;
double delta;
// Gauss-Seidel with Successive OverRelaxation Solver
for (int k = 0; k < iterations; ++k) {
for (int i = 0; i < right.Length; ++i) {
delta = 0.0f;
for (int j = 0; j < i; ++j)
delta += matrix [i, j] * x [j];
for (int j = i + 1; j < right.Length; ++j)
delta += matrix [i, j] * x [j];
delta = (right [i] - delta) / matrix [i, i];
x [i] += relaxation * (delta - x [i]);
}
}
return x;
}
答案 1 :(得分:8)
你没有投影就实现了高斯赛德尔。对于预测的Gauss Seidel,您需要在下限和上限内投影(或钳制)解决方案:
public static double[] Solve (double[,] matrix, double[] right,
double relaxation, int iterations,
double[] lo, double[] hi)
{
// Validation omitted
var x = right;
double delta;
// Gauss-Seidel with Successive OverRelaxation Solver
for (int k = 0; k < iterations; ++k) {
for (int i = 0; i < right.Length; ++i) {
delta = 0.0f;
for (int j = 0; j < i; ++j)
delta += matrix [i, j] * x [j];
for (int j = i + 1; j < right.Length; ++j)
delta += matrix [i, j] * x [j];
delta = (right [i] - delta) / matrix [i, i];
x [i] += relaxation * (delta - x [i]);
// Project the solution within the lower and higher limits
if (x[i]<lo[i])
x[i]=lo[i];
if (x[i]>hi[i])
x[i]=hi[i];
}
}
return x;
}
这是一个小修改。这是一个要点,展示如何从Bullet物理库中提取A矩阵和b矢量,并使用投影Gauss Seidel解决它:https://gist.github.com/erwincoumans/6666160