Search code examples
pythonpython-3.xconstraintscvxpymixed-integer-programming

Mixed Integer Programming Constraints in CVXPY


Where: V :3x3 Matrix of complex numbers constants
V: scalar Complex number constant
The problem is to find a boolean matrix X that
Minimize Residules=cp.norm(cp.sum(cp.multiply(Vc,S))-V)

The following code works:

import numpy as np

import cvxpy as cp 



V= np.random.random(3)*10 + np.random.random(3)*10 * 1j
C=3+4j
X=cp.Variable((3,3), boolean=True)

Residules=cp.norm(cp.sum(cp.multiply(Vc,S))-V)
Objective= cp.Minimize(Residules)





Const1=cp.sum(X,0)<=1

Prob1= cp.Problem(Objective)



Prob1.solve() 
X=np.array(X.value)  
print(np.round(X))
print(Prob1.value)

The output:

[[ 1.  0.  0.]
 [ 1. -0. -0.]
 [-0.  1. -0.]]
1.39538277332097

My question: I want put a constraint on the problem so that for each column in Matrix X only one element can be '1' and the rest should be zeros. So that in each Column there is at maximum one element with the value 1. I tried :

Const1=cp.sum(X,0)<=1
Prob1= cp.Problem(Objective,[Const1])
Prob1.solve() 

The following error occured:

File "path\Anaconda3\lib\site-packages\cvxpy\reductions\complex2real\complex2real.py", line 95, in invert dvars[vid] = solution.dual_vars[cid]

KeyError: 11196
Any other way to set this constraint ??


Solution

  • I separated the complex from real part. I think it works.

    import numpy as np
    import cvxpy as cp
    
    
    Vr= np.random.random((3,3))
    Vi=np.random.random((3,3))
    Cr=3
    Ci=4
    
    X=cp.Variable((3,3),boolean=True)
    
    
    
    Real=cp.sum(cp.multiply(Vr,X))-Cr
    Imag=cp.sum(cp.multiply(Vi,X))-Ci
    
    
    Residules=cp.norm(cp.hstack([Real, Imag]), 2)
    Objective= cp.Minimize(Residules)
    
    
    
    
    const1=[cp.sum(X,axis = 0)<=1]
    
    
    Prob1= cp.Problem(Objective,const1)
    
    
    
    Prob1.solve() 
    X=np.array(X.value)  
    print(np.round(X))
    print(Prob1.value)