简体   繁体   English

Mosek 矢量化,缓慢添加约束

[英]Mosek Vectorization, Slow Adding Constraints

I am trying to solve a semi-definite program in Mosek with C++ Fusion with constraints of the form我正在尝试使用 C++ Fusion 解决 Mosek 中的半定程序,并具有以下形式的约束

$$ \sum_j A_{i,j} M_j - \vec{c}_i^T \vec{y} + x \le b_i \forall i, $$ (links to picture of equation) $$ \sum_j A_{i,j} M_j - \vec{c}_i^T \vec{y} + x \le b_i \forall i, $$ (链接到方程的图片)

where each M_j is a positive semi-definite matrix.其中每个 M_j 是一个半正定矩阵。 Currently I am creating the constraints using a for loop to sum over A_{i,j} M_j and another for loop to add all of the constraints.目前,我正在使用 for 循环创建约束来对 A_{i,j} M_j 求和,并使用另一个 for 循环来添加所有约束。 However, the number of constraints is fairly large and using M->constraint(...) that many times is by far the slowest part of my program.但是,约束的数量相当大,并且多次使用 M->constraint(...) 是迄今为止我程序中最慢的部分。 I was looking through the Mosek documentation, and it seems like vectorization could speed up the program.我正在查看 Mosek 文档,似乎矢量化可以加速程序。 However, I'm struggling with the vectorization for summing A_{i,j} M_j.但是,我正在为求和 A_{i,j} M_j 的矢量化而苦苦挣扎。 The data type for each variable are:每个变量的数据类型是:

A: vector < vector < Matrix::t > > 
M: vector < Variable::t >
c: vector < shared_ptr < ndarray < int,1 > > >
y: Variable::t
x: Variable::t
b: vector < int >  

I've tried using new_array_ptr on both A and M, and then using Expr::add and Expr::dot, but neither of those worked.我尝试在 A 和 M 上使用 new_array_ptr,然后使用 Expr::add 和 Expr::dot,但这些都不起作用。 Any help either with the vectorization or speeding up M->constraint(...) would be greatly appreciated!任何有关矢量化或加速 M->constraint(...) 的帮助将不胜感激!

It is a bit tricky.这有点棘手。 Since each of your constraints involves a product of matrices, vectorizing it further would require the ability to multiply higher-dimensional objects (tensors?) and Fusion does not have an interface for that.由于您的每个约束都涉及矩阵的乘积,因此进一步对其进行矢量化需要能够乘以更高维的对象(张量?),而 Fusion 没有用于此的接口。

One thing you could do is to make sure your A are represented as sparse matrices, if they indeed are sparse.您可以做的一件事是确保您的 A 表示为稀疏矩阵,如果它们确实是稀疏的。

Another solution is to rewrite everything using the C API.另一种解决方案是使用 C API 重写所有内容。 It seems your data is already in a format that would allow to do the translation relatively easily.您的数据似乎已经采用了可以相对轻松地进行翻译的格式。

Last but not least, if you could package your code into a reproducible example and send to MOSEK support (email here https://www.mosek.com/support/ ) then we [I work there] can check more concretely if there is some inefficiency in Mosek or if anything can be improved in your code.最后但并非最不重要的一点是,如果您可以将 package 您的代码转换为可重现的示例并发送给 MOSEK 支持(在此处发送电子邮件https://www.mosek.com/support/ ),那么我们 [我在那里工作] 可以更具体地检查是否有Mosek 中的一些低效率,或者您的代码中是否有任何可以改进的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM