1

我尝试使用 cvxpy 对优化变量施加一些元素约束。这是代码:

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)

根据我施加的元素约束(除第一个约束外的所有约束),我希望看到优化变量的相关元素等于零,但结果是:

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]]

你能帮我理解代码有什么问题吗?如何强加我的一组元素等式约束?

谢谢

4

0 回答 0