Scipy优化错误:遇到零除零

时间:2018-02-24 22:22:57

标签: python optimization scipy scientific-computing

我很困惑在尝试最大化函数时如何得到除零误差。

solution = minimize(objective,x0, method='SLSQP', bounds = bnds, constraints = cons)
__main__:36: RuntimeWarning: invalid value encountered in double_scalars
C:\Users\loren\AppData\Roaming\Python\Python36\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in greater
  return (self.a < x) & (x < self.b)
C:\Users\loren\AppData\Roaming\Python\Python36\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in less
  return (self.a < x) & (x < self.b)
C:\Users\loren\AppData\Roaming\Python\Python36\site-packages\scipy\stats\_distn_infrastructure.py:1738: RuntimeWarning: invalid value encountered in greater_equal
  cond2 = (x >= self.b) & cond0
__main__:38: RuntimeWarning: invalid value encountered in double_scalars
__main__:40: RuntimeWarning: invalid value encountered in double_scalars
__main__:29: RuntimeWarning: invalid value encountered in double_scalars
__main__:31: RuntimeWarning: invalid value encountered in double_scalars
__main__:33: RuntimeWarning: invalid value encountered in double_scalars
__main__:36: RuntimeWarning: divide by zero encountered in double_scalars
__main__:29: RuntimeWarning: divide by zero encountered in double_scalars
__main__:38: RuntimeWarning: divide by zero encountered in double_scalars
__main__:31: RuntimeWarning: divide by zero encountered in double_scalars
__main__:40: RuntimeWarning: divide by zero encountered in double_scalars
__main__:33: RuntimeWarning: divide by zero encountered in double_scalars

代码仍在运行,但我没有得到任何有用的东西。

 fun: nan
 jac: array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan])
 message: 'Iteration limit exceeded'
 nfev: 2212
 nit: 101
 njev: 101
 status: 9
 success: False
 x: array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan])

这是我的目标函数:

def objective(x): #x <-> pl1 pl2 pl3 ph1 ph2 ph3 r prob1 prob2 prob3
    return -x[6]*(x[0]*NL1(x)+x[1]*NL2(x)+x[2]*NL3(x)+x[3]*NH1(x)+x[4]*NH2(x)+x[5]*NH3(x))

所以我最大化了10个变量。

这是函数定义为的形式(没有一个可笑的长目标函数):

def CDF(x, parameter):
    return expon.cdf(x, scale = 1/parameter)
def NH1(x): #Number of high buyers at location 1
    return 1-CDF((x[3]-x[0])/(x[7]), lambdaB)
def NL1(x): #Number of low buyers at location 1
    return CDF((x[3]-x[0])/x[7], lambdaB) - CDF((s-x[0])/(1+x[7]), lambdaB)
def NS1(x): #Number of sellers at location 1
    return 1- CDF((NH1(x)*(x[3]-x[6])+NL1(x)*(x[0]-x[6]))/(NH1(x) + (1-x[7])*NL1(x)+2*x[7]*NL1(x)), lambdaS)
def constraintGenEq1(x):
    return NS1(x)- NH1(x) -(1+x[7])*NL1(x)
def constraintProbLow1(x):
    x[7] =  (x[3]-x[0])/(dist) 
    return x[7] - (x[3]-x[0])/(dist) 

conGen1= {'type': 'eq', 'fun': constraintGenEq1}
conGen2= {'type': 'eq', 'fun': constraintGenEq2}
conGen3= {'type': 'eq', 'fun': constraintGenEq3}
conProb1= {'type': 'eq', 'fun': constraintProbLow1}
conProb2= {'type': 'eq', 'fun': constraintProbLow2}
conProb3= {'type': 'eq', 'fun': constraintProbLow3}

upper = 150
cons = ([conGen1, conGen2, conGen3, conProb1, conProb2, conProb3])
bnds = ((0,upper),(0,upper),(0,upper),(0,upper),(0,upper),(0,upper),(0,1),(0,1),(0,1),(0,1))

n=len(bnds)
x0 = np.zeros(n)+0.1
solution = minimize(objective,x0, method='SLSQP', bounds = bnds, constraints = cons)

我理解这是否有点密集阅读。但我已经尝试过使用界限,限制,让它返回一些有用的东西,但我还没有运气。我猜我在某些子功能中有分区操作的地方我可能会遇到问题。最后,我对参数做出了一些假设。

lambdaB = 1 #Buyer's exponential distribution parameter
lambdaS = 1 #Seller's exponential distribution parameter
dist = 1
s = 4

0 个答案:

没有答案