How to handle scipy minimize ValueError: not enough values to unpack (expected 4, got 3)?
How to handle scipy minimize ValueError: not enough values to unpack (expected 4, got 3)?
I am trying to minimize the following function by use of the scipy library:
from scipy.optimize import minimize
def constraint1(bet):
a,b = bet
return 100 - a + b
con1 = {'type': 'ineq', 'fun': constraint1}
cons = [con1]
b0, b1 = (0,100), (0,100)
bnds = (b0, b1)
def f(bet, sign = -1, *args):
d0, d1, p0, p1 = args
a,b = bet
wins0 = a * (d0-1)
wins1 = b * (d1-1)
loss0 = b
loss1 = a
log0 = np.log(bank + wins0 - loss0)
log1 = np.log(bank + wins1 - loss1)
objective = (log0 * p0 + log1 * p1)
return sign * objective
bet = [0,0]
minimize(f, bet, args = (1,2,3,4,), method = 'trust-constr', bounds = bnds, constraints = cons)
然而,这会导致 ValueError:
d0, d1, p0, p1 = args (Think this is where the error occurs)
ValueError: not enough values to unpack (expected 4, got 3)
尝试省略 ,
所以它看起来像这样 :(1,2,3,4)
,但这也没有用。
任何事情都会有所帮助!
您不能 minimize
使用可选参数运行。该函数必须如下所示:
fun(x, *args)
没有可选参数的位置。所以你想要做的是调用你的函数,明确提供 -1
作为 args
:
之一
minimize(f, bet, args = (-1, 1, 2, 3, 4,),method = 'trust-constr',
bounds = bnds, constraints = cons)
这里是 link 到 documentation。
I am trying to minimize the following function by use of the scipy library:
from scipy.optimize import minimize
def constraint1(bet):
a,b = bet
return 100 - a + b
con1 = {'type': 'ineq', 'fun': constraint1}
cons = [con1]
b0, b1 = (0,100), (0,100)
bnds = (b0, b1)
def f(bet, sign = -1, *args):
d0, d1, p0, p1 = args
a,b = bet
wins0 = a * (d0-1)
wins1 = b * (d1-1)
loss0 = b
loss1 = a
log0 = np.log(bank + wins0 - loss0)
log1 = np.log(bank + wins1 - loss1)
objective = (log0 * p0 + log1 * p1)
return sign * objective
bet = [0,0]
minimize(f, bet, args = (1,2,3,4,), method = 'trust-constr', bounds = bnds, constraints = cons)
然而,这会导致 ValueError:
d0, d1, p0, p1 = args (Think this is where the error occurs)
ValueError: not enough values to unpack (expected 4, got 3)
尝试省略 ,
所以它看起来像这样 :(1,2,3,4)
,但这也没有用。
任何事情都会有所帮助!
您不能 minimize
使用可选参数运行。该函数必须如下所示:
fun(x, *args)
没有可选参数的位置。所以你想要做的是调用你的函数,明确提供 -1
作为 args
:
minimize(f, bet, args = (-1, 1, 2, 3, 4,),method = 'trust-constr',
bounds = bnds, constraints = cons)
这里是 link 到 documentation。