Проблема
Думаю, scipy.optimize.minimize не может использовать 2D-границы.
Я могу обойти проблему следующим образом:
minimize в одномерные массивыНо это утомительно.
Я сделал что-то не так? Или minimize действительно не может использовать 2D-границы?
В моем случае переменные должны быть от 0 до 1.
Спасибо.
Док
док для версии 1.1.0 говорит, что границы могут быть массивом:
scipy.optimize.Bounds
...
Parameters:
lb, ub : array_like, optional
Lower and upper bounds on independent variables. Each array must have the same size as x or be a scalar, in which case a bound will be the same for all the variables. ...
Версии
Контрольная работа
import numpy as np
import scipy
import scipy.optimize as opt
def obj(x):
return x.sum()
def main():
x = np.ones((3, 4))
bounds = opt.Bounds(np.zeros(x.shape),
np.ones(x.shape))
r = opt.minimize(obj, x, bounds=bounds)
print(r)
main()
Результат
Traceback (most recent call last):
File "scipy_bounds.py", line 16, in <module>
main()
File "scipy_bounds.py", line 12, in main
r = opt.minimize(obj, x, bounds=bounds)
File "<...>/site-packages/scipy/optimize/_minimize.py", line 584, in minimize
bounds = new_bounds_to_old(bounds.lb, bounds.ub, x0.shape[0])
File "<...>/site-packages/scipy/optimize/_constraints.py", line 259, in new_bounds_to_old
lb = [x if x > -np.inf else None for x in lb]
File "<...>/site-packages/scipy/optimize/_constraints.py", line 259, in <listcomp>
lb = [x if x > -np.inf else None for x in lb]
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Я обнаружил, что minimize также не позволяет мне использовать скалярные границы.
Получаю 'length of x0 != length of bounds'.






От документы
x0 : ndarray, shape (n,)
Initial guess. Array of real elements of size (n,), where ‘n’ is the number of independent variables.
Очевидно, да, в качестве входных данных принимаются только одномерные массивы.