Johnny Que
Johnny Que

Reputation: 125

Implement LMI constraint with CVXPY

So I am trying to implement a simple optimization code in Python using the CVXPY package (an optimization problem with a linear matrix inequality constraint). The code is show below.

I have tried running the code using Python 3.6.

import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt
import control as cs

gamma = cp.Variable();
MAT1 = np.array([[1, gamma], [1+gamma, 3]])

constraints_2 = [MAT1 >> 0]
prob = cp.Problem(cp.Minimize(gamma),constraints_2)
prob.solve()

Every time I try to run this code, I get the following error:

"Non-square matrix in positive definite constraint."

But the matrix is clearly square! So I don't know what is happening. Any ideas? Your help is much appreciated!

Upvotes: 1

Views: 1993

Answers (2)

Bob
Bob

Reputation: 14654

There is a technical and a conceptual problem here

Tecnical problem

The problem is that your MAT1 is not a numpy array

You could have written

MAT1=cvxpy.vstack([cvxpy.hstack([1 , gamma]), cvxpy.hstack([1+gamma, 3])])

Or more concisely

MAT1=cvxpy.vstack([cvxpy.hstack([1 , gamma]), cvxpy.hstack([1+gamma, 3])])

This way the cvxpy will accept your code, but will not give the correct answer.

Conceptual problem

The SDP problems are convex only for symmetric matrices, what cvxpy will do is to make it symmetric (apparently by adding it to it's transpose). The solution given is the minimum gamma such that [[1, 0.5+gamma], [0.5+gamma, 3]] >> 0.

Upvotes: 1

Jacques Kvam
Jacques Kvam

Reputation: 3056

MAT1 is a numpy array, you'll need to make it a cvxpy Variable to use the semidefinite constraint. Try this:

MAT1 = cp.Variable((2, 2))
constraints_2 = [MAT1 >> 0, MAT1[0, 0] == 1, MAT1[1, 0] == 1 + MAT1[0, 1], MAT1[1, 1] == 3]
prob = cp.Problem(cp.Minimize(MAT1[0, 1]), constraints_2)

gamma is then about -2.73

Upvotes: 2

Related Questions