1

I try to impose some elementwise constraints to the optimization variable using cvxpy. Here is the code:

import cvxpy as cv
import numpy as np
import scipy.io as spio


mat = spio.loadmat('data.mat', squeeze_me=True)

zigma_bar_region1_normal = mat['zigma_bar_region1_normal'] # array    
J1_multi=mat['Q1'] # array
multiplier1_multi=mat['multiplier1_multi'] # array
J1_bar_multi=mat['J1_bar_multi'] # array
J_multi_reg1_normal_part2=mat['J_multi_reg1_normal_part2'] # array


# Construct the problem.
J_reg1_normal = cv.Variable(12,12)
objective = cv.Maximize( - 0.5*cv.trace(zigma_bar_region1_normal[0:12,0:12]*J_reg1_normal) + 0.5 * cv.log_det(J_reg1_normal) )

constraints = [0 <= J_reg1_normal , J_reg1_normal[[0],[2,3,4,5,6,7,8,9,11]] == 0 , J_reg1_normal[[1],[3,5,6,7,8,9,10,11]] == 0 , J_reg1_normal[[2],[4,5,6,8,9,10,11]] == 0 , J_reg1_normal[[3],[5,6,7,8,9,10,11]] == 0 , J_reg1_normal[[4],[6,7,8,9,10,11]] == 0 , J_reg1_normal[[5],[6,7,8,9,10,11]] == 0 , J_reg1_normal[[6],[8,10]] == 0 , J_reg1_normal[[7],[9,10,11]] == 0 , J_reg1_normal[[8],[10,11]] == 0 , J_reg1_normal[[9],[11]] == 0 , J_reg1_normal[[10],[11]] == 0]

# solution
prob =cv.Problem(objective, constraints)
result = prob.solve()
print (J_reg1_normal.value)

Based on the elementwise constraints (all the constraints except for the first one) that I imposed, I expect to see the related elements of the optimization variable equal to zero but the reslut is:

J_reg1_normal.value=
[[  5.00766997e-02  -5.37660541e-08  -7.46538281e-07  -8.10192632e-07
   -7.64060978e-07   1.25086461e-06  -1.03525218e-06  -7.51147527e-07
   -9.97192725e-07  -1.24346873e-06  -5.76822200e-07   1.21530067e-07]
 [ -7.45516570e-07   7.37738183e-02   1.72064920e-07   3.36427991e-07
    2.49936553e-07   7.95798210e-07   8.55689658e-08   1.84734980e-07
    5.75522200e-08   4.81760532e-07  -5.45853914e-07   4.76049220e-07]
 [ -1.09635151e-06   8.07142981e-08   8.19960687e-02   2.11452883e-07
    3.22655657e-07   1.19484242e-07   5.36718902e-07   2.61031577e-07
    4.87627330e-07   1.03295724e-06  -1.44733384e-07   1.01243499e-06]
 [ -1.35830309e-06   7.32755264e-08   6.94062445e-08   7.25212176e-02
    1.91768276e-07   7.59513789e-07  -4.95620587e-08   1.12145754e-07
   -7.68547755e-08   3.59067914e-07  -5.56034313e-07   4.15399606e-07]
 [ -1.39314051e-06  -6.79025204e-08   1.55459007e-07  -1.45776549e-07
    6.81346578e-02   8.31850234e-07  -4.35358420e-07  -1.81866568e-07
   -4.60469965e-07   7.40559772e-08  -6.77514459e-07   1.32725166e-07]
 [  2.05858432e-06  -9.48101424e-08  -1.14338605e-06  -4.12623122e-09
    2.67424252e-07   3.76979109e-02   8.60749039e-07   6.57189508e-07
    8.92744912e-07   1.62642738e-06   1.24010151e-06   1.99010028e-06]
 [ -2.36195268e-06  -3.28843200e-07   4.09843074e-07  -5.14056768e-07
   -1.07897936e-06   6.30116484e-07   6.23269342e-02  -4.37659393e-07
   -5.97223838e-07   1.64670805e-07  -7.71374907e-07   2.45341202e-07]
 [ -1.36533277e-06  -6.90559257e-08   1.70176795e-07  -1.65537164e-07
   -5.72663106e-07   1.89104285e-07  -1.01273014e-06   6.77787610e-02
   -4.70360657e-07   9.88740255e-08  -6.99962084e-07   1.71014478e-07]
 [ -2.27967232e-06  -2.62741831e-07   4.35173601e-07  -4.46220999e-07
   -1.00605758e-06   7.53784772e-07  -1.54479230e-06  -1.01758995e-06
    6.24639431e-02   2.38503887e-07  -7.65754957e-07   3.21363999e-07]
 [ -3.43939698e-06   3.53561379e-07   1.30204048e-06   1.49493315e-07
   -3.71476002e-07   2.11241070e-06  -6.15268816e-07  -3.04934473e-07
   -4.33420341e-07   5.78667319e-02  -2.64161958e-07   1.20573563e-06]
 [ -1.55413591e-06  -1.02842282e-06  -4.62530301e-07  -1.08225026e-06
   -1.33228975e-06   2.83160202e-06  -1.62281474e-06  -1.30821976e-06
   -1.53569613e-06  -7.74595167e-07   4.83412805e-02  -1.60988038e-07]
 [ -1.28773821e-06   1.16552805e-07   1.14797581e-06   7.58012666e-08
   -4.47639123e-07   2.52974522e-06  -7.82197583e-07  -3.87727738e-07
   -6.11770448e-07   1.16146295e-06  -1.15407365e-06   5.91020607e-02]]

Could you please help me undersand what is wrong with the code? How can I impose my set of elementwise equality constraints?

Thanks

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
farid
  • 11
  • 3
  • What is the status? Try `print prob.status`. – Rodrigo de Azevedo Apr 12 '18 at 09:58
  • If you use `numpy.set_printoptions(linewidth=150)` you may be able to print all 12 columns without carriage return. – Rodrigo de Azevedo Apr 12 '18 at 10:02
  • The status is optimal. Any suggestion about how to impose an elementwise constraint? What is wrong with the way I defined the elementwise constraints in the code? – farid Apr 12 '18 at 19:52
  • Are you sure there is anything wrong? Due to numerical error, you should not expect to get exactly `0.0`. Take a look at [this](https://math.stackexchange.com/a/2414847/339790) code and its output. Once the entries of the matrix are rounded, it does not look that bad anymore. – Rodrigo de Azevedo Apr 12 '18 at 19:57
  • Maybe you're right. What makes me confused is that my results using Matlab CVX are different than the results using CVXPY, whereas both results are optimal. Have you ever experienced that? – farid Apr 16 '18 at 20:56
  • I no longer use CVX. Cannot compare it to CVXPY. – Rodrigo de Azevedo Apr 16 '18 at 21:00
  • Is there any way to improve the precision in CVXPY? I use Jupyter. I studied the CVXPY advanced tutorial. Based on the information there, I used the following EPS = 1e-8 prob =cv.Problem(objective, constraints) result = prob.solve(solver=ECOS, feastol=EPS, abstol=EPS, reltol=EPS, feastol_inacc=EPS, abstol_inacc=EPS, reltol_inacc=EPS, max_iters=20, verbose=True, warm_start=True) But I confronted with the error: "name 'ECOS' is not defined". Could you please inform me how I can solve this problem to be able to set both the solver and precision? – farid Apr 17 '18 at 15:51
  • Try cv.ECOS. A problem can have more than one optimal solution. All that matters is that they have the same optimal value. – steven May 21 '18 at 23:17

0 Answers0