我有一个相对简单的受限制优化问题,但是根据我采取的方式会得到不同的答案。让我们先导入必要的库和漂亮打印函数:
import numpy as np
from scipy.optimize import minimize, LinearConstraint, NonlinearConstraint, SR1
def print_res( res, label ):
print("\n\n ***** ", label, " ***** \n")
print(res.message)
print("obj func value at solution", obj_func(res.x))
print("starting values: ", x0)
print("ending values: ", res.x.astype(int) )
print("% diff", (100.*(res.x-x0)/x0).astype(int) )
print("target achieved?",target,res.x.sum())
样例数据非常简单:
n = 5
x0 = np.arange(1,6) * 10_000
target = x0.sum() + 5_000 # increase sum from 15,000 to 20,000
以下是受限优化问题(包括雅可比矩阵)的内容。简而言之,我要最小化的目标函数只是初始值到最终值的平方百分比变化之和。线性等式约束只要求x.sum()
等于一个常数。
def obj_func(x):
return ( ( ( x - x0 ) / x0 ) ** 2 ).sum()
def obj_jac(x):
return 2. * ( x - x0 ) / x0 ** 2
def constr_func(x):
return x.sum() - target
def constr_jac(x):
return np.ones(n)
为了比较,我将其重构为无约束最小化问题,通过使用等式约束将x [0]
替换为x [1:]
的函数。请注意,无约束函数传递了x0 [1:]
,而受约束函数传递了x0
。
def unconstr_func(x):
x_one = target - x.sum()
first_term = ( ( x_one - x0[0] ) / x0[0] ) ** 2
second_term = ( ( ( x - x0[1:] ) / x0[1:] ) ** 2 ).sum()
return first_term + second_term
我接下来尝试三种最小化方式:
- 使用'Nelder-Mead'进行无限制最小化
- 使用'信赖域算法(trust-constr)'进行有限制最小化(带/不带jacobian)
- 使用'SLSQP'进行有限制最小化(带/不带jacobian)
代码:
##### (1) unconstrained
res0 = minimize( unconstr_func, x0[1:], method='Nelder-Mead') # OK, but weird note
res0.x = np.hstack( [target - res0.x.sum(), res0.x] )
print_res( res0, 'unconstrained' )
##### (2a) constrained -- trust-constr w/ jacobian
nonlin_con = NonlinearConstraint( constr_func, 0., 0., constr_jac )
resTCjac = minimize( obj_func, x0, method='trust-constr',
jac='2-point', hess=SR1(), constraints = nonlin_con )
print_res( resTCjac, 'trust-const w/ jacobian' )
##### (2b) constrained -- trust-constr w/o jacobian
nonlin_con = NonlinearConstraint( constr_func, 0., 0. )
resTC = minimize( obj_func, x0, method='trust-constr',
jac='2-point', hess=SR1(), constraints = nonlin_con )
print_res( resTC, 'trust-const w/o jacobian' )
##### (3a) constrained -- SLSQP w/ jacobian
eq_cons = { 'type': 'eq', 'fun' : constr_func, 'jac' : constr_jac }
resSQjac = minimize( obj_func, x0, method='SLSQP',
jac = obj_jac, constraints = eq_cons )
print_res( resSQjac, 'SLSQP w/ jacobian' )
##### (3b) constrained -- SLSQP w/o jacobian
eq_cons = { 'type': 'eq', 'fun' : constr_func }
resSQ = minimize( obj_func, x0, method='SLSQP',
jac = obj_jac, constraints = eq_cons )
print_res( resSQ, 'SLSQP w/o jacobian' )
这里是简化的输出内容(当然您可以运行代码以获得完整的输出):
starting values: [10000 20000 30000 40000 50000]
***** (1) unconstrained *****
Optimization terminated successfully.
obj func value at solution 0.0045454545454545305
ending values: [10090 20363 30818 41454 52272]
***** (2a) trust-const w/ jacobian *****
The maximum number of function evaluations is exceeded.
obj func value at solution 0.014635854609684874
ending values: [10999 21000 31000 41000 51000]
***** (2b) trust-const w/o jacobian *****
`gtol` termination condition is satisfied.
obj func value at solution 0.0045454545462939935
ending values: [10090 20363 30818 41454 52272]
***** (3a) SLSQP w/ jacobian *****
Optimization terminated successfully.
obj func value at solution 0.014636111111111114
ending values: [11000 21000 31000 41000 51000]
***** (3b) SLSQP w/o jacobian *****
Optimization terminated successfully.
obj func value at solution 0.014636111111111114
ending values: [11000 21000 31000 41000 51000]
注:
(1)和(2b)都是合理的解决方案,它们可以实现显著较低的目标函数值, 从直觉上讲,我们会期望具有更大起始值的变量移动得更多(无论是绝对值还是百分比方面)。
将雅可比矩阵添加到 "trust-const" 中会导致它给出错误答案(或者至少是更差的答案),并且超过最大迭代次数。也许雅可比矩阵是错误的,但是功能非常简单,我相当确定它是正确的 (?)
'SLSQP'似乎无论是否提供雅可比矩阵都不能正常工作,但速度非常快,并声称成功终止。这似乎非常令人担忧,因为得到错误答案并声称已成功终止几乎是最坏的结果。
最初我使用了非常小的起始值和目标值(只有上文提到值的千分之一),在这种情况下,上面的所有5种方法都能正常工作并给出相同的答案。我的样本数据仍然非常小,似乎很奇怪它可以处理1,2,..,5但无法处理1000,2000,..5000。
值得一提的是,所有3个错误的结果都通过将每个初始值加1,000来达到目标 - 这符合约束条件,但与最小化平方百分比差异总和相去甚远(因为具有更高起始值的变量应该比较低的变量增加更多)。
所以我的问题实际上是这里发生了什么事情,为什么只有(1)和(2b)似乎有效?
更一般地说,我想找到一种解决此类优化问题的好的基于Python的方法,并且将考虑使用除scipy之外的其他软件包的答案,尽管最好的答案理想上也应该解决这里的scipy问题(例如,这是用户错误还是我应该在github上发布的错误?)。
fatol=1e-8
,您会得到什么结果? - user545424nlopt
库尝试了您的示例,并使用SLSQP获得了正确的结果,因此可以排除您的雅可比函数或局部最小值存在问题的可能性。 - user545424