1.求解目标
2.源码实现
import math
from scipy.optimize import minimize
import numpy as np
# 定义优化函数
def func(args):
a = args
v = lambda x : math.exp(-x[0]**2) * math.sin(a*x[0])
return v
# 定义变量取值范围: ineq为大于等于0
def con(args):
min = args
cons = ({'type': 'ineq', 'fun': lambda x: x[0] - min})
return cons
args = 6
x = np.array(1);
cons = con(0)
res = minimize(func(args), x, method='SLSQP', constraints=cons)
if res.success == True:
print(res.x)
print(res.fun)
3.运行及其结果
$ python3 example.py
[ 0.74471414]
-0.5572767876175884
网友评论