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. js的数据类型

    关于js中的几大数据类型,估计大家都很熟悉:String.Number.Array.Bollean.Null.Undifine.Object: 从这其中,可以延伸出一个感念:基本数据类型和引用类型: ...

  2. 转:认识MyBean

    1. 初步体验 我们先看一个框架自带的例子,以增加感性认识.打开samples\singleDEMO示例项目.这个示例演示了在一个EXE程序内,使用插件的概念调用两个窗口.其中包括一个主窗体 ufrm ...

  3. xml对象的序列化和反序列化

    对象序列化: /// <summary>        /// 将一个对象序列化为XML字符串        /// </summary>        /// <par ...

  4. 去掉IE下input的叉号

    IE10下的Input Text和谷歌下面的 input search 一旦输入内容,会在最右端出现一个叉号,点击后,内容就会自动清空,看似方便,其实有些场景并不需要,需要写代码清除掉. 代码如下: ...

  5. XproerUI控件工厂代码优化-使用C++11特性优化

    优化前的代码,比较冗余,通常实现一个工厂类的创建器需要三个步骤. 代码截图: 优化后的代码,更简洁,对开发人员更加友好,实现一个工厂类创建器只需要一个步骤. 代码截图:

  6. linux下mv命令使用方法

    1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或目录 目标 ...

  7. nagios二次开发(五)---nagios和nagiosql的关系

    根据对nagios和nagiosql的了解,笔者简要的将二者的关系粗略的梳理了一下,具体情况如下图所示: 从上面的关系图中可以看出,nagios与nagiosql共享了主机.主机组.服务.服务组等.c ...

  8. WCF Routing 服务

    WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...

  9. extjs ajax java简单精美验证码实现 有图

    前端:利用ExtJs的autoEl功能加载图片. var imgCheckValid = new Ext.create('Ext.Component',{ width: 70, //图片宽度 heig ...

  10. VFP自定义函数StringFormat (仿.NET String.Format 方法)

    VFP仿.NET String.Format 方法 将指定字符串中的每个{x}替换为相应值,并返回文本 *-- 调用格式 StringFormat("日期{2},字符{1}",&q ...