使用到的是scipy庫
線性規(guī)劃指的是目標模型均為線性,除此以外的都是非線性規(guī)劃,使用scipy提供的方法對該類問題進行求解。文章來源:http://www.zghlxwxcb.cn/news/detail-733477.html
from scipy.optimize import minimize
import numpy as np
#定義目標函數(shù)
def fun(args):
a,b,c,d = args
v = lambda x: (a+x[0])/ (b+x[1]) - c*x[0] + d*x[2]
return v
#定義約束條件
def con(args):
# 約束條件 分為eq 和ineq
# eq表示 函數(shù)結果等于0 ; ineq 表示 表達式大于等于0
x1min,x1max,x2min,x2max,x3min,x3max = args
cons = ({'type':'ineq', 'fun': lambda x : x[0] - x1min},
{'type':'ineq', 'fun': lambda x : -x[0] + x1max},
{'type':'ineq', 'fun': lambda x : x[1] - x2min},
{'type':'ineq', 'fun': lambda x : -x[1] + x2min},
{'type':'ineq', 'fun': lambda x : x[2] - x3min},
{'type':'ineq', 'fun': lambda x : -x[2] + x3min},
)
return cons
#定義常量值
args = (2,1,3,4)
#設置變量約束條件
args2 = (0.1,0.9,0.1,0.9,0.1,0.9)
cons = con(args2)
#設置初始隨機值
x0 = np.asarray((0.5,0.5,0.5))
res = minimize(fun(args), x0, method='SLSQP', constraints=cons)
res
文章來源地址http://www.zghlxwxcb.cn/news/detail-733477.html
到了這里,關于數(shù)學建模__非線性規(guī)劃Python實現(xiàn)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!