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. jsp中两种include的区别【转】

    引用文章:http://www.ibm.com/developerworks/cn/java/j-jsp04293/ http://www.cnblogs.com/lazycoding/archive ...

  2. An exception occurred while acquiring a poolable resource. Will retry.

    我的是mysql-connector-java-5.1.20.jar加到lib下面就行了.或者在pom中加入配置也行.

  3. VUE 入门基础(7)

    八,事件处理器 监听事件 可以用v-on 指令监听DOM 事件来触发一些javaScript <div id="example-1"> <button v-on: ...

  4. ConCurrent in Practice小记 (2)

    Java-ConCurrent2.html :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0 ...

  5. string、Empty和null三者的区别

    string.Empty和null三者的区别 本文转自  http://www.bitscn.com/pdb/dotnet/201003/181883.html 时间:2010-03-01 00:00 ...

  6. phpMyAdmin登录出错

    问题:在网页上登录phpMyAdmin后出错 You don't have permission to access /phpMyAdmin/ on this server. 查询找到解决方法,记录一 ...

  7. http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html

    http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html

  8. SQL优化 CREATE STATISTICS

    CREATE STATISTICS 语法: https://msdn.microsoft.com/zh-cn/library/ms188038.aspx STATISTICS优化中的使用案例: htt ...

  9. vb.net字符串格式转为日期型

    vb.net字符串格式转为日期型  比如 "20080815" 转换为"2008-05-15"Dim a As Date  Dim s As String = ...

  10. sqlserver存取过程游标

    ALTER proc [dbo].[common_proc_temp2] as begin declare @id varchar(50); declare @cbcontractid varchar ...