weixin_45801598 2020-02-13 21:24 采纳率: 0%
浏览 433

关于cvxopt在C语言中使用的问题?

最近想用C语言进行凸优化,凸优化工具箱用的是python中的cvxopt(因为官网上有using from C的example)。
但是将cvxopt安装到python后,打开官网的测试代码还是报错无法打开cvxopt.h
提问:安装到python后,是否已生成cvxopt.h?如果有,路径一般在哪里?如果没有,应该怎么生成?
编译环境:vs2019+python3.7(64bits)+boost1.72.0
附官网代码:

/*
   A modified version of the example in the Python documentation:
   http://docs.python.org/ext/pure-embedding.html

   Solves the LP:

   minimize -4*x1 -5*x2
   subject  2*x1 + x2 <= 3
            x1 + 2*x2 <= 3
            x1 >= 0,  x2 >= 0

   and prints the solution from C.


   On Ubuntu Linux compile with:

   gcc -o embed_cvxopt embed_cvxopt.c \
     -I${CVXOPT_SRC}/C \
     -I/usr/include/python2.4 -lpython2.4
*/

#include "cvxopt.h"

int
main(int argc, char *argv[])
{
  Py_Initialize();

  if (import_cvxopt() < 0) {
    fprintf(stderr, "error importing cvxopt");
    return 1;
  }

  /* import cvxopt.solvers */
  PyObject *solvers = PyImport_ImportModule("cvxopt.solvers");
  if (!solvers) {
    fprintf(stderr, "error importing cvxopt.solvers");
    return 1;
  }

  /* get reference to solvers.solvelp */
  PyObject *lp = PyObject_GetAttrString(solvers, "lp");
  if (!lp) {
    fprintf(stderr, "error referencing cvxopt.solvers.lp");
    Py_DECREF(solvers);
    return 1;
  }

  /* create matrices */
  PyObject *c = (PyObject *)Matrix_New(2, 1, DOUBLE);
  PyObject *G = (PyObject *)Matrix_New(4, 2, DOUBLE);
  PyObject *h = (PyObject *)Matrix_New(4, 1, DOUBLE);
  PyObject *pArgs = PyTuple_New(3);
  if (!c || !G || !h || !pArgs) {
    fprintf(stderr, "error creating matrices");
    Py_DECREF(solvers); Py_DECREF(lp);
    Py_XDECREF(c); Py_XDECREF(G); Py_XDECREF(h); Py_XDECREF(pArgs);
    return 1;
  }

  MAT_BUFD(c)[0] = -4;
  MAT_BUFD(c)[1] = -5;

  MAT_BUFD(G)[0] = 2;
  MAT_BUFD(G)[1] = 1;
  MAT_BUFD(G)[2] = -1;
  MAT_BUFD(G)[4] = 1;
  MAT_BUFD(G)[5] = 2;
  MAT_BUFD(G)[7] = -1;

  MAT_BUFD(h)[0] = 3;
  MAT_BUFD(h)[1] = 3;

  /* pack matrices into an argument tuple - references are stolen*/
  PyTuple_SetItem(pArgs, 0, c);
  PyTuple_SetItem(pArgs, 1, G);
  PyTuple_SetItem(pArgs, 2, h);

  PyObject *sol = PyObject_CallObject(lp, pArgs);
  if (!sol) {
    PyErr_Print();
    Py_DECREF(solvers); Py_DECREF(lp);
    Py_DECREF(pArgs);
    return 1;
  }

  PyObject *x = PyDict_GetItemString(sol, "x");
  fprintf(stdout, "\n\nx = (%5.4e, %5.4e)\n", MAT_BUFD(x)[0], MAT_BUFD(x)[1]);

  Py_DECREF(solvers);
  Py_DECREF(lp);
  Py_DECREF(pArgs);
  Py_DECREF(sol);

  Py_Finalize();
  return 0;

}
  • 写回答

1条回答 默认 最新

  • weixin_44041304 2021-06-24 16:10
    关注

    时隔多年给你一个回答吧。

    从以下编译命令
    
    
     

    gcc -o embed_cvxopt embed_cvxopt.c \

    
     

    -I${CVXOPT_SRC}/C \

    
     

    -I/usr/include/python2.4 -lpython2.4

     

    可以推断,仅需要include cvxopt的src/C文件对应目录与Python对应目录即可。

    /usr/include/python2.4是linux中的python目录。Windows中肯定在不同位置。

     

    这个代码的本质是通过c调用python,cvxopt实际上在python中运行。不过呢,cvxopt所依赖的稀疏矩阵库suite sparse又是c的。

     

    是不是感觉很绕?

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制