0

我正在尝试使用 python 中的 cvxopt 库对线性目标函数和凸约束进行约束优化(最大化)问题。目前,约束是二次的,但我想最终用一个通用的凸多项式来做。问题基本上是:最大化 c_1*x_1 + c_2*x_2 + c_3*x_3 受约束 k_1*x_1^(alpha+1) + k_2*x_2^(alpha+1) + k_3*x_3^(alpha+1) <= 预算,并且 x_i 非负数。我的代码:

import numpy as np
from cvxopt import solvers, matrix, spdiag, mul

c = -matrix([1.,2.,3.]) #minimize negative for maximization
alpha = 1.
rate_vec = matrix([.1,.2,.3])
budget = 1000
def F(x = None, z = None):
    if x is None: return 1, matrix([1.,1.,1.])
    if min(x) <= 0: return None
    f = matrix(rate_vec.trans() * x**(alpha + 1.) - budget)
    Df = matrix((alpha + 1.)*mul(rate_vec, x**alpha)).trans()
    if z is None: return f, Df
    H = spdiag(z[0,0]*(alpha + 1.)*alpha*mul(rate_vec, x**(alpha -1.)))
    return f, Df, H

t = solvers.cpl(c,F)

我的输出是:

pcost       dcost       gap    pres   dres
 0: -6.0000e+00 -1.0054e+03  1e+00  1e+00  1e+00
 1: -7.3931e+00 -1.7384e+01  2e-02  1e+00  1e+00
 2: -1.1174e+01 -1.1274e+01  4e-04  1e+00  1e+00
 3: -2.1707e+01 -2.1904e+01  8e-06  1e+00  1e+00
 4: -2.2126e+01 -2.2519e+01  2e-07  1e+00  1e+00
 5: -2.2667e+01 -2.3448e+01  3e-09  1e+00  1e+00
 6: -2.3665e+01 -2.5217e+01  6e-11  1e+00  1e+00
 7: -2.5861e+01 -2.8941e+01  1e-12  1e+00  1e+00
 8: -3.1961e+01 -3.8037e+01  2e-14  1e+00  1e+00
 9: -5.9255e+01 -7.0625e+01  5e-16  9e-01  1e+00
 10: -1.0993e+02 -1.2780e+02  9e-18  8e-01  1e+00
Terminated (singular KKT matrix).

关于出了什么问题的任何提示?

4

1 回答 1

2

看起来只是 0 附近间隙的舍入误差(e-12 -14 -16)。要查看收敛,请print输入F

print "f: %.3g  x: %s  Df: %s" % (f[0], np.squeeze(x), np.squeeze(Df))
=>
...
 6: -2.4088e+02 -2.4485e+02  2e-11  3e-02  4e-02

f: -33  x: [ 40.1  40.1  40.1]  Df: [  8.   16.1  24.1]
f: -0.629  x: [ 40.8  40.8  40.8]  Df: [  8.2  16.3  24.5] 
  ...

 7: -2.4487e+02 -2.4495e+02  2e-13  6e-04  8e-04
f: -0.629  x: [ 40.8  40.8  40.8]  Df: [  8.2  16.3  24.5]
   ...     

 8: -2.4495e+02 -2.4495e+02  2e-15  6e-06  8e-06
f: -0.00639  x: [ 40.8  40.8  40.8]  Df: [  8.2  16.3  24.5]
Terminated (singular KKT matrix).

(与您的值略有不同,不知道为什么)。此外,cpl还有六个参数,包括“细化:求解 KKT(Karush-Kuhn-Tucker)方程时的迭代细化步骤数”。

于 2014-04-20T10:17:32.787 回答