c++ - 查找对象 float 状态的优化方法

标签 c++ algorithm math mathematical-optimization nonlinear-optimization

要解决的问题是在给定重量和重心的情况下找到浮体的漂浮状态。

我使用的函数计算在给定下沉、脚跟和纵倾情况下 body 的位移体积和浮力中心。 其中下沉是长度单位,鞋跟/纵倾是限制在 -90 到 90 之间的值的角度。

Illustration of the three variables

当排量等于重量且重心与浮力中心在一条垂直线上时,即为漂浮状态。

我将此实现为具有 3 个变量(下沉、纵倾、鞋跟)和 3 个方程的非线性 Newton-Raphson 求根问题。 此方法有效,但需要良好的初始猜测。所以我希望为此找到更好的方法,或者找到初始值的好方法。

下面是用于 Newton-Raphson 迭代的 newton 和 jacobian 算法的代码。函数 volume 采用参数 sinkage、heel 和 trim。并返回体积和浮力中心的坐标。

我还包括了 maxabs 和 GSolve2 算法,我相信这些算法取自 Numerical Recipies。

void jacobian(float x[], float weight, float vcg, float tcg, float lcg, float jac[][3], float f0[]) {
    float h = 0.0001f;
    float temp;
    float j_volume, j_vcb, j_lcb, j_tcb;
    float f1[3];

    volume(x[0], x[1], x[2], j_volume, j_lcb, j_vcb, j_tcb);
    f0[0] = j_volume-weight;
    f0[1] = j_tcb-tcg;
    f0[2] = j_lcb-lcg;

    for (int i=0;i<3;i++) {
        temp = x[i];
        x[i] = temp + h;
        volume(x[0], x[1], x[2], j_volume, j_lcb, j_vcb, j_tcb);

        f1[0] = j_volume-weight;
        f1[1] = j_tcb-tcg;
        f1[2] = j_lcb-lcg;
        x[i] = temp;

        jac[0][i] = (f1[0]-f0[0])/h;
        jac[1][i] = (f1[1]-f0[1])/h;
        jac[2][i] = (f1[2]-f0[2])/h;
    }
}


void newton(float weight, float vcg, float tcg, float lcg, float &sinkage, float &heel, float &trim) {
    float x[3] = {10,1,1};

    float accuracy = 0.000001f;
    int ntryes = 30;
    int i = 0;
    float jac[3][3];
    float max;
    float f0[3];
    float gauss_f0[3];

    while (i < ntryes) {
        jacobian(x, weight, vcg, tcg, lcg, jac, f0);

        if (sqrt((f0[0]*f0[0]+f0[1]*f0[1]+f0[2]*f0[2])/2) < accuracy) {
            break;
        }

        gauss_f0[0] = -f0[0];
        gauss_f0[1] = -f0[1];
        gauss_f0[2] = -f0[2];

        GSolve2(jac, 3, gauss_f0);

        x[0] = x[0]+gauss_f0[0];
        x[1] = x[1]+gauss_f0[1];
        x[2] = x[2]+gauss_f0[2];

        // absmax(x) - Return absolute max value from an array
        max = absmax(x);
        if (max < 1) max = 1;

        if (sqrt((gauss_f0[0]*gauss_f0[0]+gauss_f0[1]*gauss_f0[1]+gauss_f0[2]*gauss_f0[2])) < accuracy*max) {
            x[0]=x2[0];
            x[1]=x2[1];
            x[2]=x2[2];
            break;
        }

        i++;
    }

    sinkage = x[0];
    heel = x[1];
    trim = x[2];
}

int GSolve2(float a[][3],int n,float b[]) {
    float x,sum,max,temp;
    int i,j,k,p,m,pos;

    int nn = n-1; 

    for (k=0;k<=n-1;k++)
    {
        /* pivot*/
        max=fabs(a[k][k]);
        pos=k;


        for (p=k;p<n;p++){
            if (max < fabs(a[p][k])){
                max=fabs(a[p][k]);
                pos=p;
            }
        }

        if (ABS(a[k][pos]) < EPS) {
            writeLog("Matrix is singular");
            break;
        }

        if (pos != k) {
            for(m=k;m<n;m++){
                temp=a[pos][m];
                a[pos][m]=a[k][m];
                a[k][m]=temp;
            }
        }

        /*   convert to upper triangular form */
        if ( fabs(a[k][k])>=1.e-6)
        {
            for (i=k+1;i<n;i++)
            {
            x = a[i][k]/a[k][k];
            for (j=k+1;j<n;j++) a[i][j] = a[i][j] -a[k][j]*x;
            b[i] = b[i] - b[k]*x;
            }
        }
        else
        {
            writeLog("zero pivot found in line:%d",k);
            return 0;
       }
     }

/*   back substitution */
     b[nn] = b[nn] / a[nn][nn];
     for (i=n-2;i>=0;i--)
     {
        sum = b[i];
         for (j=i+1;j<n;j++) 
           sum = sum - a[i][j]*b[j];
        b[i] = sum/a[i][i];
     }
     return 0;
}

float absmax(float x[]) {
    int i = 1;
    int n = sizeof(x);
    float max = x[0];
    while (i < n) {
        if (max < x[i]) {
            max = x[i];
        }
        i++;
    }
    return max;
}

最佳答案

您是否考虑过一些随机搜索方法来找到初始值,然后使用 Newton Raphson 进行微调?一种可能性是进化计算,你可以使用 Inspyred 包。对于在许多方面与您描述的问题相似的物理问题,请查看此示例:http://inspyred.github.com/tutorial.html#lunar-explorer

关于c++ - 查找对象 float 状态的优化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14549698/

相关文章:

c++ - 通过 IL2CPP 的 Unity3d native API 是否可行?

algorithm - 使用 Fisher-Yates shuffle 从链表中获取 k 个随机值

c++ - 对每个簇大小具有上限要求的聚类算法

algorithm - 在单链表时间复杂度中查找节点

python : efficient bytearray incrementation

c++ - 是否有解决方法来解决 C++ 中智能指针对指针算术的限制?

c++ - 如何存储数据/我们如何获取存储在 unordered_map c++​​11 中存储桶中的数据

c++ - Rcpp:如果声明了 IntegerVector,数组上的段错误 > 698152

algorithm - 180° 偏移无关紧要时的角度加权平均值 (MATLAB)

algorithm - 谷歌组合优化面试题