想用 sympy 做多变最小化
Want to do multi-variation minimize with sympy
我想使用符号化字符
对scipy.optimize进行最小化
from scipy.optimize import minimize
from sympy.utilities.lambdify import lambdify
import sympy as sp
x1, x2, x3, x4 = sp.symbols('x1 x2 x3 x4')
FormulaMain = sp.symbols('-2*x1**2*x3+6*x1**2*x4+13*x1**2-3*x1*x2**2+x1*x2+3*x1*x3**2-3*x4+103')
HandleMain = lambdify((x1,x2,x3,x4),FormulaMain,'numpy')
bnds = ((-1, 1), (-1, 1), (-1, 1), (-1, 1))
PrintParams = minimize(HandleMain,[1,1,1,1],method='SLSQP',bounds=bnds)
print PrintParams
当我 运行 代码时,我得到
<lambda>() takes exactly 4 arguments (1 given)
我想我用 [1,1,1,1] 输入了 4 个参数
我必须对代码进行任何更改吗?
首先:欢迎来到SO!
据我所知,lambdify()
无法处理向量。此外,使用 Sympy 时,确定雅可比矩阵很容易。你可以试试:
import numpy as np
from scipy.optimize import minimize
from sympy.utilities.lambdify import lambdify
import sympy as sy
sy.init_printing() # LaTeX like pretty printing for IPython
x1, x2, x3, x4 = sy.symbols('x1 x2 x3 x4')
xx = (x1, x2, x3, x4)
f = -2*x1**2*x3+6*x1**2*x4+13*x1**2-3*x1*x2**2+x1*x2+3*x1*x3**2-3*x4+103
f_n = lambdify(xx, f, modules='numpy')
# Build Jacobian:
jac_f = [f.diff(x) for x in xx]
jac_fn = [lambdify(xx, jf, modules='numpy') for jf in jac_f]
def f_v(zz):
""" Helper for receiving vector parameters """
return f_n(zz[0], zz[1], zz[2], zz[3])
def jac_v(zz):
""" Jacobian Helper for receiving vector parameters """
return np.array([jfn(zz[0], zz[1], zz[2], zz[3]) for jfn in jac_fn])
bnds = ((-1, 1), (-1, 1), (-1, 1), (-1, 1))
zz0 = np.array([1, 1, 1, 1])
rslts = minimize(f_v, zz0, method='SLSQP', jac=jac_v, bounds=bnds)
print(rslts)
我想使用符号化字符
对scipy.optimize进行最小化from scipy.optimize import minimize
from sympy.utilities.lambdify import lambdify
import sympy as sp
x1, x2, x3, x4 = sp.symbols('x1 x2 x3 x4')
FormulaMain = sp.symbols('-2*x1**2*x3+6*x1**2*x4+13*x1**2-3*x1*x2**2+x1*x2+3*x1*x3**2-3*x4+103')
HandleMain = lambdify((x1,x2,x3,x4),FormulaMain,'numpy')
bnds = ((-1, 1), (-1, 1), (-1, 1), (-1, 1))
PrintParams = minimize(HandleMain,[1,1,1,1],method='SLSQP',bounds=bnds)
print PrintParams
当我 运行 代码时,我得到
<lambda>() takes exactly 4 arguments (1 given)
我想我用 [1,1,1,1] 输入了 4 个参数 我必须对代码进行任何更改吗?
首先:欢迎来到SO!
据我所知,lambdify()
无法处理向量。此外,使用 Sympy 时,确定雅可比矩阵很容易。你可以试试:
import numpy as np
from scipy.optimize import minimize
from sympy.utilities.lambdify import lambdify
import sympy as sy
sy.init_printing() # LaTeX like pretty printing for IPython
x1, x2, x3, x4 = sy.symbols('x1 x2 x3 x4')
xx = (x1, x2, x3, x4)
f = -2*x1**2*x3+6*x1**2*x4+13*x1**2-3*x1*x2**2+x1*x2+3*x1*x3**2-3*x4+103
f_n = lambdify(xx, f, modules='numpy')
# Build Jacobian:
jac_f = [f.diff(x) for x in xx]
jac_fn = [lambdify(xx, jf, modules='numpy') for jf in jac_f]
def f_v(zz):
""" Helper for receiving vector parameters """
return f_n(zz[0], zz[1], zz[2], zz[3])
def jac_v(zz):
""" Jacobian Helper for receiving vector parameters """
return np.array([jfn(zz[0], zz[1], zz[2], zz[3]) for jfn in jac_fn])
bnds = ((-1, 1), (-1, 1), (-1, 1), (-1, 1))
zz0 = np.array([1, 1, 1, 1])
rslts = minimize(f_v, zz0, method='SLSQP', jac=jac_v, bounds=bnds)
print(rslts)