【发布时间】:2019-05-02 12:11:32
【问题描述】:
我将优化三个变量x、alpha 和R。
X 是一维向量,alpha 是二维向量,R 是标量值。我怎样才能最大化这个功能?
我写下面的代码:
#from scipy.optimize import least_squares
from scipy.optimize import minimize
import numpy as np
sentences_lengths =[6, 3]
length_constraint=5
sentences_idx=[0, 1]
sentences_scores=[.1,.2]
damping=1
pairwise_idx=[(0,0),(0,1),(1,0),(1,1)]
overlap_matrix=[[0,.01],[.02,0]]
def func(x, R, alpha, sign=1.0):
""" Objective function """
return sign*(sum(x[i] * sentences_scores[i] for i in sentences_idx) - damping * R * sum(alpha[i][j] * overlap_matrix[i][j] for i,j in pairwise_idx))
x0=np.array([1,0])
R0=.1
alpha0=np.array([1,0,0,0])
def func_deriv(x, R, alpha, sign=1.0):
""" Derivative of objective function """
#Partial derivative to x
dfdX = sign*(sum(sentences_scores[i] for i in sentences_idx))
#Partial derivative to R
dfdR= sign*(- damping * sum(alpha[i][j] * overlap_matrix[i][j] for i,j in pairwise_idx))
#Partial derivative to alpha
dfdAlpha= sign*(- damping * R * sum(alpha[i][j] * overlap_matrix[i][j] for i,j in pairwise_idx))
return [ dfdX, dfdR, dfdAlpha]
cons = ({'type': 'ineq',
## Constraints: one constraint for the size + consistency constraints
#sum(x[i] * sentences_lengths[i] for i in sentences_idx) <= length_constraint
'fun' : lambda x: length_constraint - sum(x[i] * sentences_lengths[i] for i in sentences_idx) ,
'jac' : lambda x: [-sum(sentences_lengths[i] for i in sentences_idx), 0, 0]}
,{'type': 'ineq',
#alpha[i][j] - x[i] <= 0
'fun' : lambda x: [x[i]-alpha[i][j] for i,j in pairwise_idx],
'jac' : lambda x: [1.0, 0.0, -1.0]}
,{'type': 'ineq',
#alpha[i][j] - x[j] <= 0
'fun' : lambda x: [x[j]-alpha[i][j] for i,j in pairwise_idx],
'jac' : lambda x: [1.0, 0.0, -1.0]}
,{'type': 'ineq',
#x[i] + x[j] - alpha[i][j] <= 1
'fun' : lambda x: [1+alpha[i][j]-x[i]-x[j] for i,j in pairwise_idx],
'jac' : lambda x: [-1.0-1.0, 0.0, 1.0]})
res = minimize(func, (x0,R0,alpha0)
, args=(sentences_lengths
,length_constraint
,sentences_idx
,sentences_scores
,damping
,pairwise_idx
,overlap_matrix,)
, jac=func_deriv
, constraints=cons
, method='SLSQP'
, options={'disp': True})
我得到错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-a1a91fdf2d13> in <module>()
55 , constraints=cons
56 , method='SLSQP'
---> 57 , options={'disp': True})
58
59 #res = least_squares(fun, (x,R,alpha), jac=jac, bounds=bounds, args=(sentences_scores, damping,overlap_matrix), verbose=1)
/usr/local/lib/python3.5/dist-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
456 elif meth == 'slsqp':
457 return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 458 constraints, callback=callback, **options)
459 elif meth == 'dogleg':
460 return _minimize_dogleg(fun, x0, args, jac, hess,
/usr/local/lib/python3.5/dist-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
305
306 # Transform x0 into an array.
--> 307 x = asfarray(x0).flatten()
308
309 # Set the parameters that SLSQP will need
/usr/local/lib/python3.5/dist-packages/numpy/lib/type_check.py in asfarray(a, dtype)
102 if not issubclass(dtype, _nx.inexact):
103 dtype = _nx.float_
--> 104 return asarray(a, dtype=dtype)
105
106
/usr/local/lib/python3.5/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
529
530 """
--> 531 return array(a, dtype, copy=False, order=order)
532
533
ValueError: setting an array element with a sequence.
【问题讨论】:
-
请在提出问题时尝试使用Minimal, Complete, and Verifiable example。
标签: python optimization constraints