繁体   English   中英

使用python scipy.optimize.minimize进行优化

[英]Optimize with python scipy.optimize.minimize

我想优化泵存储工厂的时间表。 基本上有96个已知价格(一天中的每个季度),该模型应确定是(1)泵,(2)涡轮还是(3)每个季度都不进行任何操作。 因此,X有一些界限:-100

首先,我尝试了以下操作:

from scipy.optimize import minimize
import numpy as np

prices=np.array([[1.5,50,30]])
xp =np.array([[1.5,50,30]])

fun = lambda x: xp* prices #here xp and prices should be matrices

cons = ({'type': 'ineq', 'fun': lambda x:  (xp*0.25)<=500},
    {'type': 'ineq', 'fun': lambda x: (xp*0.25)>=0})

bnds = ((0, None), (0, None), (0, None))

res = minimize(fun, (2, 0,0), method='SLSQP', bounds=bnds, constraints=cons)

但是,这将引发错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-15c05e084977> in <module>()
     10 bnds = ((0, None), (0, None), (0, None))
     11 
---> 12 res = minimize(fun, (2, 0,0), method='SLSQP', bounds=bnds, constraints=cons)

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    450     elif meth == 'slsqp':
    451         return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 452                                constraints, callback=callback, **options)
    453     elif meth == 'dogleg':
    454         return _minimize_dogleg(fun, x0, args, jac, hess,

/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
    375 
    376             # Now combine c_eq and c_ieq into a single matrix
--> 377             c = concatenate((c_eq, c_ieq))
    378 
    379         if mode == 0 or mode == -1:  # gradient evaluation required

ValueError: all the input arrays must have same number of dimensions

我不知道为什么会出现此错误。 有人可以给我提示吗?

我将逐行介绍您的代码,并重点介绍一些问题:

from scipy.optimize import minimize
import numpy as np

prices=np.array([[1.5,50,30]])
xp =np.array([[1.5,50,30]])

pricesxp是向量,而不是矩阵,请使用np.array([1.5,50,30])声明向量


fun = lambda x: xp* prices #here xp and prices should be matrices

您函数的右侧不依赖x ,因此您的函数只是常量。 另外*在python中是元素明智的。 您可以使用np.dot来计算标量积。

fun = lambda x: np.dot(x, prices)

cons = ({'type': 'ineq', 'fun': lambda x:  (xp*0.25)<=500},
    {'type': 'ineq', 'fun': lambda x: (xp*0.25)>=0})

这不是定义约束的方式。 您可能要检查文档。 不等式由集合函数g_i(x) 其中所有i g_i(x) >= 0 与上面同样的问题:函数声明的右侧未使用x

cons = ({'type': 'ineq', 'fun': lambda x:  -x*0.25 + 500},
        {'type': 'ineq', 'fun': lambda x:  x*0.25})

bnds = ((0, None), (0, None), (0, None))

这很好,但是当向bnds = [(0,None)] * 3bnds = [(0,None)] * 3上用场。


res = minimize(fun, (2,0,0), method='SLSQP', bounds=bnds, constraints=cons)

函数和所有限制在x都是线性的。 因此,这是一个线性程序, SLSQP可能不是解决它的最佳方法。 对于此示例,您可能想要查看scipy.optimize.linprog


附带说明:我想这只是一个玩具示例。 显然,此优化的结果是零向量。

结果如下:

 njev: 3
       x: array([ 0.,  0.,  0.])
     nit: 3
  status: 0
 message: 'Optimization terminated successfully.'
     jac: array([  1.5,  50. ,  30. ,   0. ])
 success: True
     fun: 0.0
    nfev: 15

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM