Ich bin eine lineare Funktion mit scipy OptimierungAttribute: ‚schweben‘ Objekt hat kein Attribut ‚Gradient‘ scipy Python
def func(weights):
var = ['x1', 'x2', 'x3', 'x4']
if weights is None:
weights = np.ones(len(var))/len(var)
return len(set([var[i] for i in range(len(weights)) if weights[i]>0]))/len(var)
res = minimize(lambda x: func(x), x0=[0.25,0.25,0.25,0.25],method='SLSQP',
jac=ad.gh(lambda x: func(x))[0], bounds=((0.,1.),)*4,
options = {'disp':True, 'ftol': 1e-20, 'maxiter': 1000},
constraints= {'type': 'eq', 'fun': lambda x: sum(x) - 1.0})
ich die folgende Fehlermeldung zu bekommen.
Traceback (most recent call last):
File "D:/applicatio/Sub Applicatio/main.py", line 338, in <module>
constraints= {'type': 'eq', 'fun': lambda x: sum(x) - 1.0})
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\_minimize.py", line 455, in minimize
constraints, callback=callback, **options)
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\slsqp.py", line 383, in _minimize_slsqp
g = append(fprime(x),0.0)
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\scipy\optimize\optimize.py", line 289, in function_wrapper
return function(*(wrapper_args + args))
File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\ad\__init__.py", line 1090, in grad
return numpy.array(ans.gradient(list(xa)))
AttributeError: 'float' object has no attribute 'gradient'
Wie kann ich diese einfache lineare Funktion optimieren? Irgendwelche Vorschläge ? Vielen Dank.
Es sieht so aus, als ob Sie SLSQP verwenden; verlangt das * nicht, dass Sie die Jacobi in den Beschränkungen übergeben? – Prune
Der Fehler sieht so aus, als ob Ihre Ableitungsdefinition 1D ist, während Ihre Funktion 4D ist. Welche Größe hat 'ad.gh (Lambda x: func (x)) [0]'? – aTben0
Aufgrund der Kopplung von 'if',' set' und 'len' sind Sie sicher, dass die Ableitungen im definierten Raum existieren? – aTben0