weixin_45801598
weixin_45801598
2020-02-13 21:24

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

  • c++
  • 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;

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐