[英]Scipy.optimize: how should I write constraints correctly?
我想使用 scipy.optimize.minimize 求解 (2+x_1)/(1+x_2)-3x_1+4x_3 的最小值,x_1、x_2 和 x_3 的约束在 0.1 到 0.9 的范围内。 我的代码如下:
rest = [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9]]
cons = [{'type': 'ineq', 'fun': lambda x: x[i]-0.1 if j == 0 else lambda x: 0.9-x[i]} for i in range(3) for j in range(2)]
# cons = [{'type': 'ineq', 'fun': lambda x: x[0]-0.1},
# {'type': 'ineq', 'fun': lambda x: 0.9-x[0]},
# {'type': 'ineq', 'fun': lambda x: x[1]-0.1},
# {'type': 'ineq', 'fun': lambda x: 0.9-x[1]},
# {'type': 'ineq', 'fun': lambda x: x[2]-0.1},
# {'type': 'ineq', 'fun': lambda x: 0.9-x[2]}]
res2 = minimize(lambda x: (2 + x[0]) / (1 + x[1]) - 3 * x[0] + 4 * x[2], np.array((0.5, 0.5, 0.5)), method='SLSQP', constraints=cons)
res2.fun, res2.success, res2.x
运行它并报告 TypeError:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-3caab45922c2> in <module>
7 # {'type': 'ineq', 'fun': lambda x: x[2]-0.1},
8 # {'type': 'ineq', 'fun': lambda x: 0.9-x[2]}]
----> 9 res2 = minimize(lambda x: (2 + x[0]) / (1 + x[1]) - 3 * x[0] + 4 * x[2], np.array((0.5, 0.5, 0.5)), method='SLSQP', constraints=cons)
10 res2.fun, res2.success, res2.x
~\anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
623 return _minimize_cobyla(fun, x0, args, constraints, **options)
624 elif meth == 'slsqp':
--> 625 return _minimize_slsqp(fun, x0, args, jac, bounds,
626 constraints, callback=callback, **options)
627 elif meth == 'trust-constr':
~\anaconda3\lib\site-packages\scipy\optimize\slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, finite_diff_rel_step, **unknown_options)
410 g = append(sf.grad(x), 0.0)
411 c = _eval_constraint(x, cons)
--> 412 a = _eval_con_normals(x, cons, la, n, m, meq, mieq)
413
414 while 1:
~\anaconda3\lib\site-packages\scipy\optimize\slsqp.py in _eval_con_normals(x, cons, la, n, m, meq, mieq)
484
485 if cons['ineq']:
--> 486 a_ieq = vstack([con['jac'](x, *con['args'])
487 for con in cons['ineq']])
488 else: # no inequality constraint
~\anaconda3\lib\site-packages\scipy\optimize\slsqp.py in <listcomp>(.0)
484
485 if cons['ineq']:
--> 486 a_ieq = vstack([con['jac'](x, *con['args'])
487 for con in cons['ineq']])
488 else: # no inequality constraint
~\anaconda3\lib\site-packages\scipy\optimize\slsqp.py in cjac(x, *args)
282 rel_step=finite_diff_rel_step)
283 else:
--> 284 return approx_derivative(fun, x, method='2-point',
285 abs_step=epsilon, args=args)
286
~\anaconda3\lib\site-packages\scipy\optimize\_numdiff.py in approx_derivative(fun, x0, method, rel_step, abs_step, f0, bounds, sparsity, as_linear_operator, args, kwargs)
424
425 if sparsity is None:
--> 426 return _dense_difference(fun_wrapped, x0, f0, h,
427 use_one_sided, method)
428 else:
~\anaconda3\lib\site-packages\scipy\optimize\_numdiff.py in _dense_difference(fun, x0, f0, h, use_one_sided, method)
495 x = x0 + h_vecs[i]
496 dx = x[i] - x0[i] # Recompute dx as exactly representable number.
--> 497 df = fun(x) - f0
498 elif method == '3-point' and use_one_sided[i]:
499 x1 = x0 + h_vecs[i]
TypeError: unsupported operand type(s) for -: 'function' and 'function'
我不知道为什么非评论的cons
和评论的cons
是不等价的。 谢谢回答!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.