媽媽再也不用擔心我的矩陣運算了!Mosek學習筆記5,矩陣。

Mosek學習筆記系列就要接近尾聲了。

(Mosek學習筆記系列之前的鏈接:https://blog.csdn.net/aliexken/article/details/104443873

之所以把矩陣放的比較靠後,是因爲其重要性。作爲優化過程中承載數據的載體,矩陣的相關運算直接決定了建立優化問題模型的方式。經過研究後,非常遺憾,我還是沒能找到能夠捨棄Eigen的Mosek矩陣運算方法。Expr提供的矩陣運算形式,更多的是一種方程約束形式的表示,而不是直接的矩陣運算。即你不能通過Expr直接算出兩個矩陣的和,但是卻可以表示兩個矩陣的和,並放入約束中,使用優化來獲取值。感覺很繞,我猜想這和mosek在設計之初,以優化爲第一目的的設計理念有關。

儘管如此,我還是非常認真的做了mosek學習筆記的原因,該工具還是能夠將矩陣與線性代數運算和優化系統結合在一起,提供了完備的解決方案,而且最重要的是,提供了良好的C++接口!對基於VS開發C++算法的我來說簡直就是福音!

閒話少說,上乾貨。

1. 矩陣初始化

矩陣初始化可以分爲稠密矩陣和稀疏矩陣兩種

預定了shape形態的矩陣。

auto A = new_array_ptr<double,2>({ {1,2,3,4}, {5,6,7,8} });
auto Ad= Matrix::dense(A);

基於一維向量模式。

auto Af = new_array_ptr<double,1>({ 1,2,3,4,5,6,7,8 });
auto Aff= Matrix::dense(2,4,Af);

稀疏矩陣

 auto rows   = new_array_ptr<int,1>({ 0, 0, 1, 1 });
 auto cols   = new_array_ptr<int,1>({ 0, 3, 1, 3 });
 auto values = new_array_ptr<double,1>({ 1.0, 2.0, 3.0, 4.0 });    
 auto ms = Matrix::sparse(rows->size(), cols->size(), rows, cols, values);
Members:
 

Matrix.get – Get a single entry 獲得唯一入口。

Matrix.getDataAsArray – Return a dense array of values 返回一個稠密數組的值。

Matrix.getDataAsTriplets – Return the matrix data in sparse triplet format 返回矩陣數據的稀疏三重格式。

Matrix.isSparse – Returns true if the matrix is sparse. 判斷是否稀疏。

Matrix.numColumns – Returns the number of columns in the matrix 返回矩陣中的列。

Matrix.numNonzeros – Returns the number of non-zeros in the matrix 返回矩陣中非0的數目。

Matrix.numRows – Returns the number of rows in the matrix 返回矩陣中的行。

Matrix.toString – Get a string representation of the matrix 獲得。

Matrix.transpose – Transpose the matrix 轉置.

矩陣轉置的方法有點怪,給一個實例

//vector<vector<double>> LX

auto LX_Ma = monty::new_array_ptr<double>(LX);         
auto LX_M = mosek::fusion::Matrix::dense(LX_Ma);
auto LX_M_T = mosek::fusion::Matrix::t(LX_M)->transpose();

Static members:
 

Matrix.antidiag – Create a sparse square matrix with a given vector as anti-diagonal

用給定的矢量創建一個稀疏的正方形矩陣作爲對角線。

Matrix.dense – Create a dense matrix from the given data.

從提供的數據中創建一個稠密矩陣。

Matrix.diag – Create a sparse square matrix with a given vector as diagonal.

用給定的矢量創建一個稀疏的正方形矩陣作爲對角線

Matrix.eye – Create the identity matrix.

創建單位矩陣

Matrix.ones – Create a matrix filled with all ones.

創建一個全是1的矩陣

Matrix.sparse – Create a sparse matrix from the given data.

利用給定數據創建一個稀疏矩陣

 

總結起來,也就是一個轉置矩陣操作。

2. 結合線性代數的運算

Expr.add Element-wise addition of two matrices 加法
Expr.sub Element-wise subtraction of two matrices 減法
Expr.mul Matrix or matrix-scalar multiplication 乘法
Expr.neg Sign inversion 符號反轉
Expr.outer Vector outer-product  適量外積
Expr.dot Dot product 點積
Expr.sum Sum over a given dimension 給定維度的總和
Expr.mulElm Element-wise multiplication 逐元素乘法
Expr.mulDiag

Sum over the diagonal of a matrix which is the result of a matrix multiplication 

矩陣對角線的總和,這是矩陣相乘的結果

Expr.constTerm Return a constant term 返回常數項
Expr::add(Expr::mul(A,x), Expr::mul(B,y));
Expr::add(new_array_ptr<Variable::t,1>({x, y, z, w}));

3. 約束域Domain的運算

*Domain.equalsTo – Defines the domain consisting of a fixed point. 創建一個等式

*Domain.greaterThan – Defines the domain specified by a lower bound in each dimension. 創建一個大於不等式

*Domain.lessThan – Defines the domain specified by an upper bound in each dimension. 建一個小於不等式

Domain.axis – Set the dimension along which the cones are created. 在建立錐優化時設置維度

Domain.binary – Creates a domain of binary variables. 創建一個二進制變量域

Domain.inDExpCone – Defines the dual exponential cone. 定義雙指數錐

Domain.inDPowerCone – Defines the dual power cone.  定義雙冪錐

Domain.inPExpCone – Defines the primal exponential cone. 定義原始指數錐

Domain.inPPowerCone – Defines the primal power cone. 定義原始冪錐

Domain.inPSDCone – Creates a domain of Positive Semidefinite matrices. 創建一個正半定矩陣的域

Domain.inQCone – Defines the domain of quadratic cones. 定義二次錐的域

Domain.inRange – Creates a domain specified by a range in each dimension. 創建一個由每個維度中的範圍指定的域

Domain.inRotatedQCone – Defines the domain of rotated quadratic cones.

Domain.integral – Creates a domain of integral variables.

Domain.isLinPSD – Creates a domain of Positive Semidefinite matrices.

Domain.isTrilPSD – Creates a domain of Positive Semidefinite matrices.

Domain.sparse – Use a sparse representation.

Domain.symmetric – Impose symmetry on a given linear domain.

Domain.unbounded – Creates a domain in which variables are unbounded.

有了矩陣的初始化,線性代數計算以及約束域方法,我們就可以建立相應的約束規則。

在實際編程的時候,發現這個矩陣計算還是有點問題,Expr這個線性代數的計算不是直接針對矩陣的,看起來像是針對優化問題的,所以還是不能像eigen一樣那麼方便,除非是所有的矩陣都求好了才能夠列在限制條件裏,這個確實有點坑。如果有空的話,在補一期mosek與eigen配合使用的說明教程。

上面所有的Mosek介紹全部都是基於Fusion Model來進行的,也就是:MOSEK Fusion API for C++ 9.1.13。當我真正使用這套工具來做多元優化問題的時候,我傻了!使用幾何規劃實在是非常麻煩,在經過一天半的努力嘗試後,我幾乎要放棄了,都準備要轉qpOASES學習。所謂踏破鐵鞋無覓處,我找到了MOSEK Optimizer API for C 9.1.13。我原來以爲Mosek的功能實現是相同的,只不過是基於C++或C的編譯器,做不同的接口罷了,但是!我看了MOSEK Optimizer API for C 9.1.13,這裏分明有多元優化問題的成熟解決方案!好吧,哥們今天就跟你幹上了,新開一個mosek學習筆記! 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章