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.

[MOSEK] Stupid things when using mosek的更多相关文章

  1. [MOSEK] Mosek求解中遇到的奇葩内存问题

    在使用mosek优化库的时候,使用http://docs.mosek.com/7.0/capi/MSK_getxx_.html的 MSKrescodee MSK_getxx ( MSKtask_t t ...

  2. yalmip + lpsolve + matlab 求解混合整数线性规划问题(MIP/MILP)

    最近建立了一个网络流模型,是一个混合整数线性规划问题(模型中既有连续变量,又有整型变量).当要求解此模型的时候,发现matlab优化工具箱竟没有自带的可以求解这类问题的算法(只有bintprog求解器 ...

  3. Shogun网站上的关于主流机器学习工具包的比较

    Shogun网站上的关于主流机器学习工具包的比较: http://www.shogun-toolbox.org/page/features/   created last updated main l ...

  4. SCIP | 数学规划求解器SCIP超详细的使用教程

    前言 小伙伴们大家好呀!继上次lp_solve规划求解器的推文出来以后,大家都期待着更多求解器的具体介绍和用法.小编哪敢偷懒,这不,赶在考试周之际,又在忙里偷闲中给大家送上一篇SCIP规划求解的推文教 ...

  5. CVX安装使用

    CVX下载 下载地址 使用手册 Using Gurobi with CVX Using MOSEK with CVX CVX安装 下载压缩文件后解压缩至任意地址,打开matlab,进入解压缩后的地址, ...

  6. 万字教你如何用 Python 实现线性规划

    摘要:线性规划是一组数学和计算工具,可让您找到该系统的特定解,该解对应于某些其他线性函数的最大值或最小值. 本文分享自华为云社区<实践线性规划:使用 Python 进行优化>,作者: Yu ...

随机推荐

  1. Sublime Text 全程指引

    Sublime Text 全程指引 by Lucida 包含sublime 的常用快捷操作

  2. cmd中用PING命令时,出现'Ping' 不是内部或外部命令 解决方案

    在cmd中用PING命令时,出现'Ping' 不是内部或外部命令,也不是可运行的程序或批处理文件.先了解一下内容:1.可执行文件.命令文件和批处理文件以.exe或者.com或者.bat为扩展名的文件分 ...

  3. window10系统安装SQL数据库和小蝴蝶的问题

    最近刚刚升了windows10系统.由于以前一直使用的是SQL2008数据库,所以也就没有下载最新的数据库,但是在安装的过程中一直提示让重启,重启了很多回也没有用. 在启动SQL2008安装程序的时候 ...

  4. 自己写的 限制文本框TEdit中只能输入数字

    procedure TForm4.Edit1KeyPress(Sender: TObject; var Key: Char); begin , #]) then begin Key := #; end ...

  5. 为了解决mysqlbing翻译表字段问题而分析frm文件(持续更新)

    出处:kelvin19840813 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但 ...

  6. MVC Cookie的使用

    1.创建Cookies有两种方法: Response.Cookies["userName"].Value = "patrick"; Response.Cooki ...

  7. 关于struts2中的相对路径与绝对路径

    从昨天开始复习了struts2的课程,之所以重新走上java的道路,是觉得写了一年的go程序,并没有感觉到学习了什么,反而把java给忘得干干净净的.想想我的计划,年后就要换工作了,至于要换到什么方向 ...

  8. VB.net 调用dll

    Public Declare Function APlayer_OpenA Lib "APlayerCaller.dll" (ByVal aplayer As Integer, B ...

  9. jquery写插件

    http://www.cnblogs.com/ajianbeyourself/p/5815689.html

  10. PowerDesigner 16.5 反向PostgreSQL9.01 中 Unable to list the columns. SQLSTATE = 22003不良的类型值 short : t 解决方法

    Database➙Edit Current DBMS… General tab➙PostgreSQL 9.x➙Script➙Objects➙Column➙SqlListQuery or Tools➙R ...