Yijiang Huang

[MOSEK] Stupid things when using mosek

1.2016-8-14

我希望把一个qp问题的代码从conic constraints改为无外加约束,仅适用variable bounds的线性不等式约束

于是原来的约束代码为

if (r == MSK_RES_OK)
{
    if (_debug) { MYOUT << "Q: " << std::endl; }
    for (int i = 0; i < numvar/6; i++)
    {
        // the structure have (numvar/6) nodes
        MSKint32t qsubi[] = { 6 * i, 6 * i + 1, 6 * i + 2 };
        MSKint32t qsubj[] = { 6 * i, 6 * i + 1, 6 * i + 2 };
        double      qval[] = { 2 * x_w[i], 2 * x_w[i], 2 * x_w[i]};
        //double      qval[] = { 2, 2, 2};

        // Replaces all the quadratic entries in one constraint k
        // In our program, this specifies the deformation constrains:
        // d_{i}_t.norm < tol
        r = MSK_putqconk(task, i, 3, qsubi, qsubj, qval);
    }
}

 

for (j = 0; j<numvar && r == MSK_RES_OK; ++j)
{
    /* Set the bounds on variable j.
    blx[j] <= x_j <= bux[j] */
    if (r == MSK_RES_OK)
    {

        r = MSK_putvarbound(task,
            j,           /* Index of variable.*/
            MSK_BK_FR,      /* Bound key.*/
            -MYINF,      /* Numerical value of lower bound.*/
            MYINF);     /* Numerical value of upper bound.*/
    }
}

 

/* Set the bounds on constraints.
for i=1, ...,NUMCON : blc[i] <= constraint i <= buc[i] */
for (i = 0; i<numcon && r == MSK_RES_OK; ++i)
{
        r = MSK_putconbound(task,
            i,                            /* Index of constraint.*/
            MSK_BK_UP,    /* Bound key.*/
            -MYINF,            /* Numerical value of lower bound.*/
            pow(d_tol,2));            /* Numerical value of upper bound.*/
}

请注意上面的

        r = MSK_putvarbound(task,
            j,           /* Index of variable.*/
            MSK_BK_FR,      /* Bound key.*/
            -MYINF,      /* Numerical value of lower bound.*/
            MYINF);     /* Numerical value of upper bound.*/

虽然看起来无用,因为他是声明变量是free的,即无界变量,不要小看他哦!

 

okay我开始改代码,把上面的conic的约束换成简单的变量不等式约束:

/* variable bounds */
for (j = 0; j < numvar/6 && r == MSK_RES_OK; ++j)
{
    r = MSK_putvarbound(task,
        j * 6,           /* Index of variable.*/
        MSK_BK_RA,      /* Bound key.*/
        -temp_bound,      /* Numerical value of lower bound.*/
        temp_bound);     /* Numerical value of upper bound.*/

    r = MSK_putvarbound(task,
        j * 6 + 1,           /* Index of variable.*/
        MSK_BK_RA,      /* Bound key.*/
        -temp_bound,      /* Numerical value of lower bound.*/
        temp_bound);     /* Numerical value of upper bound.*/
   
    r = MSK_putvarbound(task,
        j * 6 + 2,           /* Index of variable.*/
        MSK_BK_RA,      /* Bound key.*/
        -temp_bound,      /* Numerical value of lower bound.*/
        temp_bound);     /* Numerical value of upper bound.*/

}
看起来很对对不?

 

但是程序崩了。

原因在于我们需要对每个变量都设置范围,

否则

/* Append 'NUMVAR' variables.
The variables will initially be b_fixed at zero (x=0). */
if (r == MSK_RES_OK)
    r = MSK_appendvars(task, numvar);

所以,正确的代码应该为:

/* variable bounds */
for (j = 0; j < numvar/6 && r == MSK_RES_OK; ++j)
{
    r = MSK_putvarbound(task,
        j * 6,           /* Index of variable.*/
        MSK_BK_RA,      /* Bound key.*/
        -temp_bound,      /* Numerical value of lower bound.*/
        temp_bound);     /* Numerical value of upper bound.*/

    r = MSK_putvarbound(task,
        j * 6 + 1,           /* Index of variable.*/
        MSK_BK_RA,      /* Bound key.*/
        -temp_bound,      /* Numerical value of lower bound.*/
        temp_bound);     /* Numerical value of upper bound.*/
   
    r = MSK_putvarbound(task,
        j * 6 + 2,           /* Index of variable.*/
        MSK_BK_RA,      /* Bound key.*/
        -temp_bound,      /* Numerical value of lower bound.*/
        temp_bound);     /* Numerical value of upper bound.*/

    r = MSK_putvarbound(task,
        j * 6 + 3,           /* Index of variable.*/
        MSK_BK_FR,      /* Bound key.*/
        -MYINF,      /* Numerical value of lower bound.*/
        MYINF);     /* Numerical value of upper bound.*/

    r = MSK_putvarbound(task,
        j * 6 + 4,           /* Index of variable.*/
        MSK_BK_FR,      /* Bound key.*/
        -MYINF,      /* Numerical value of lower bound.*/
        MYINF);     /* Numerical value of upper bound.*/

    r = MSK_putvarbound(task,
        j * 6 + 5,           /* Index of variable.*/
        MSK_BK_FR,      /* Bound key.*/
        -MYINF,      /* Numerical value of lower bound.*/
        MYINF);     /* Numerical value of upper bound.*/
}

 

#The end of 1.

posted on 2016-08-14 23:42  duckie  阅读(394)  评论(0编辑  收藏  举报

导航