0

更新:这篇文章中的 ValueError 原始值来自变量下限和上限之外的一些 xi 值,修复后,模型运行但立即以初始值结束。

我正在尝试在pytyon中使用nlopt来解决非线性问题(线性约束和非线性目标),问题是这样的

obj: max sum (a[i] x[i]^2 + b[i]*x[i]+ c[i]) * x[i]
st: lhs <= AX <= rhs
lb<=x<=ub

该模型有 180 个变量和 30 个约束(包括 15 个 lhs 和 15 个 rhs)这里是 python 代码:

import numpy
import nlopt
from numpy import *

import numpy
import nlopt
from numpy import *

xi = [1.709,0,0,0,0,0,0,0,0.321,0,0,0,1.905,0,0,0,0,0,0,0,0.703,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.995,0,0,0,0,0,0,0,0,1.1,2.5,0,0.1323,0,0,0,0,0,0,0,0.4443,0,0,0,2.3709,0,0,0.3495,0,0,0,0,0,0,0,0,1.62,0,0,0,0.307,0,0,0,0,0,0,0,3.9013,0,0,0,0,0,0,0,0,28.9163,4.0047,0,0.7889,0,0,0,0.2224,0.184,0.3,0,0,0,0,0,2.4779,0,0,0,0,0,0,0,0,0.6128,0.938,0,1.913,0,0,0,0,0,0,0,0,4.1947,6.5579,0,3.9395,0,0,0,0,0,0,0,3.972,12.1696,6.0582,0,0,0,0,0,0,0,0,0,0,21.3331,10.2199,0,2.6877,0,0,0,0,0,0,0,1.7192,2.371,2.8781,0,3.086,0,0,0,0,0,0,0,1.7192,2.371,3.1083,0]
xi = numpy.array(xi) * 1.0
lhs = [1.8992, 2.4808, 0, 5.17672, 0.46128, 2.43608, 1.66928, 30.0444, 1.24384, 3.7572, 10.94624, 22.41152, 26.15152, 8.2488, 9.41408]
lhs = numpy.array(lhs) * 1.0
rhs = [2.8488, 3.7212, 0, 7.76508, 0.69192, 3.65412, 2.50392, 45.0666, 1.86576, 5.6358, 16.41936, 33.61728, 39.22728, 12.3732, 14.12112]
rhs = numpy.array(rhs) * 1.0
lb = [1.5381, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.7145, 0, 0, 0, 0, 0, 0, 0, 0.3515, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.9975, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.25, 0, 0.06615, 0, 0, 0, 0, 0, 0, 0, 0.39987, 0, 0, 0, 2.13381, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.458, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26.02467, 0, 0, 0.47334, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.48674, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.09735, 3.93474, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7.30176, 3.0291, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19.19979, 5.10995, 0, 1.34385, 0, 0, 0, 0, 0, 0, 0, 0, 1.1855, 1.43905, 0, 1.543, 0, 0, 0, 0, 0, 0, 0, 0, 1.1855, 1.55415, 0]
lb = numpy.array(lb) * 1.0
ub = [1.8799, 0, 0, 0, 0, 0, 0, 0, 0.642, 0, 0, 0, 2.0955, 0, 0, 0, 0, 0, 0, 0, 1.0545, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.9925, 0, 0, 0, 0, 0, 0, 0, 0, 2.2, 3.75, 0, 0.19845, 0, 0, 0, 0, 0, 0, 0, 0.48873, 0, 0, 0, 2.60799, 0, 0, 0.699, 0, 0, 0, 0, 0, 0, 0, 0, 1.782, 0, 0, 0, 0.614, 0, 0, 0, 0, 0, 0, 0, 7.8026, 0, 0, 0, 0, 0, 0, 0, 0, 31.80793, 8.0094, 0, 1.10446, 0, 0, 0, 0.4448, 0.368, 0.6, 0, 0, 0, 0, 0, 3.46906, 0, 0, 0, 0, 0, 0, 0, 0, 1.2256, 1.876, 0, 3.826, 0, 0, 0, 0, 0, 0, 0, 0, 6.29205, 9.18106, 0, 7.879, 0, 0, 0, 0, 0, 0, 0, 7.944, 17.03744, 9.0873, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23.46641, 15.32985, 0, 4.03155, 0, 0, 0, 0, 0, 0, 0, 3.4384, 3.5565, 4.31715, 0, 4.629, 0, 0, 0, 0, 0, 0, 0, 3.4384, 3.5565, 4.66245, 0]
ub = numpy.array(ub) * 1.0
ac = [-7.0356144E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -9.4545595E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.96564758E-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2.8851131E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.44438762E-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.079118E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -6.754091E-14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.5078676E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -8.4841085E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.93717863E-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3.8088705E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.1658432E-13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.72022E-14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
ac = numpy.array(ac) * 1.0
bc = [1.35708245335152E-06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.17903067234446E-06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.21180531673852E-06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4.85381925053E-07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.3697092385509E-06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.7438719187058E-07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.2026564748183E-07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.4060986316015E-07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.672353873107E-08, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.41111686125172E-06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.00574066339843E-06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.3715792175388E-07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3.9414789383179E-07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bc=numpy.array(bc)*1.0
cc = [1.70899768074814, 0.535, 0.73, 1.44, 1.37, 1.605, 1.3375, 1.3375, 0, 0, 0, 0, 1.90499775395, 0.275, 1.01, 1.44, 1.37, 0.825, 0.575, 0.575, 0, 0, 0, 0, 0, 0.53, 1.484, 1.44, 1.37, 1.59, 1.24, 1.24, 0, 0, 0, 0, 1.99499903166421, 0.825, 2.31, 1.782, 3.3, 2.475, 1.8975, 1.8975, 0.58, 1.04, 1.7, 0.45, 0.132273791185637, 0.24, 0.672, 0.52, 1.48, 0.72, 0.6, 0.6, 0, 0, 0, 0, 2.37087235567695, 0.24, 0.672, 0.52, 1.48, 0.72, 0.7125, 0.7125, 1.43, 0, 0, 0, 1.61999964316983, 0.13, 0.364, 0.52, 1.48, 1.23, 0.325, 0.325, 0.89, 0, 0, 0, 3.90133911626214, 0.31, 0.65, 0.65, 1.25, 0.93, 0.3, 0.3, 0.8, 0.39, 0.12, 0, 0.788897082901522, 0.15, 0.42, 0.324, 2.31, 0.7, 1.11, 1.11, 1.15, 0, 0.27, 0, 2.47793948495081, 0.745, 2.086, 1.61, 2.31, 0.48, 1.6675, 1.6675, 1.15, 0, 0, 0, 1.9130129661104, 0.86, 2.44, 0.23, 2.31, 2.15, 2.15, 2.15, 1.15, 1.37, 0.71, 0, 3.9394882011312, 0.28, 0.784, 0.61, 2.31, 1.53, 1.11, 1.11, 1.15, 0.41, 0.19, 0, 1.13637847508418, 0.98, 2.45, 0.14, 2.31, 2.45, 2.254, 2.254, 1.15, 0.21, 0.18, 0, 2.68768154431287, 0.395, 1.106, 1.185, 4.49, 1.93, 0.7935, 0.7935, 0.72, 1.22, 0.76, 0.34, 3.08603664188619, 0.105, 0.294, 0.39, 0.86, 1.73, 0.525, 0.525, 0.35, 1.72, 0.64, 0.34]
cc=numpy.array(cc)*1.0

n = 180
A =[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]

def myfunc(x, grad, ac, bc, cc, n):
    rev = 0.0
    i=0
    while i< n:
        grad[i] = 6 * ac[i]* (x[i]*1000000)**2 + 2*bc[i]*(x[i]*1000000) + cc[i]
        rev = rev +  ac[i] * (x[i]*1000000)**3 + bc[i] *(x[i]*1000000)**2 + cc[i] * (x[i]*1000000)
        i=i+1
    return rev

def myconstraint(x, grad, a, b):
    grad[:] = a
    cs = numpy.array(a).dot(x) + b
    cs= cs*1.0
    return cs

opt = nlopt.opt(nlopt.LD_MMA, n)
opt.set_lower_bounds(lb)
opt.set_upper_bounds(ub)

opt.set_max_objective(myfunc)

A= numpy.array(A) * (1.0)
rside = numpy.array(rhs) *(-1.0)

opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[0],rside[0]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[1],rside[1]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[2],rside[2]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[3],rside[3]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[4],rside[4]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[5],rside[5]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[6],rside[6]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[7],rside[7]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[8],rside[8]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[9],rside[9]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[10],rside[10]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[11],rside[11]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[12],rside[12]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[13],rside[13]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[14],rside[14]), 1e-8)

A= numpy.array(A) * (-1.0)
rside = numpy.array(lhs) * 1.0

opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[0],rside[0]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[1],rside[1]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[2],rside[2]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[3],rside[3]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[4],rside[4]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[5],rside[5]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[6],rside[6]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[7],rside[7]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[8],rside[8]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[9],rside[9]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[10],rside[10]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[11],rside[11]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[12],rside[12]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[13],rside[13]), 1e-8)
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,A[14],rside[14]), 1e-8)

opt.set_xtol_rel(1e-4)

xopt = opt.optimize(xi)

以 xi 为起点,求解是即时的,并且始终为下限 lb。求解状态为 4,即“优化停止,因为已达到 xtol_rel 或 xtol_abs(上)”。

result = opt.last_optimize_result()
print(result)

任何提示?谢谢。

4

0 回答 0