How to maximize the determinant of a N by N Matrix variable
AnsweredHi,
Below are the notations of my problem:
- variable of interest: N*m*m binary array (B_1,...,B_N). Each B_i's is subjected to the constraint that row sum=1.
- the distance matrix of these N m*m arrays (D: N by N), each (i,j) element is the L1 distance between matrices B_i and B_j
- a given constant matrix (K: k by k), the distance matrix of some K*m*m arrays
- the distance matrix of the N arrays and the K arrays (R: N by K)
Now I hope to find B_1,..,B_N that maximizes det(D-RK^{-1}R), where D and R are functions of B's. I can code D, R, K. However, I have had a hard time calculating the determinant. This post (answered 2 years ago) provided a solution to calculate the determinant when N=2. But in general, my N is around 10.
Does Gurobi now support general determinant calculation? or is there a way to do it without coding the 10-dim determinant formula? Thank you.
0
-
Hi Yen-Chun,
If you have a matrix variable x, then you can calculate a determinant with the following code. You are still encoding a formula for the determinant but using recursion to keep it tidy:
import gurobipy as gp import numpy as np def det(A): v = m.addVar(lb=-float("inf")) if A.shape == (2,2): m.addConstr(v == A[0,0]*A[1,1]-A[1,0]*A[0,1]) return v expr = gp.QuadExpr() cofactor = 1 for i in range(A.shape[1]): cols = [c for c in range(A.shape[1]) if c != i] expr += cofactor*A[0,i]*det(A[1:][:,cols]) cofactor = -cofactor m.addConstr(v == expr) return v
n = 10 m = gp.Model() x = m.addMVar((n,n), lb=-float("inf"))
determinant = det(x) # determinant will be a gurobipy.Var- Riley
0 -
Thank you!
0
Please sign in to leave a comment.
Comments
2 comments