線性規(guī)劃標(biāo)準(zhǔn)形式:MATLAB
創(chuàng)新互聯(lián)從2013年開始,先為福綿等服務(wù)建站,福綿等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為福綿企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
-------------
線性規(guī)劃求解主要分 兩個(gè)部分,目標(biāo)函數(shù)(max,min)和約束條件(s.t.),求解時(shí)一般要化為MATLAB標(biāo)準(zhǔn)形式:
求解用到的模塊(scipy 和 numpy):
from scipy import optimize import numpy as np
例題:
轉(zhuǎn)換成標(biāo)準(zhǔn)系數(shù)格式:
c = [2, 3, -5] A = [[-2, 5, -1], [1, 3, 1]] b = [-10, 12] Aeq = [[1, 1, 1]] beq = [7] x1 = (0, None) x2 = (0, None) x3 = (0, None)
LP求解函數(shù):
#-*- coding:utf-8 -*- #導(dǎo)入包 from scipy import optimize import numpy as np def LP(m='',clist=[],Alist=[],blist=[],Aeqlist=[],beqlist=[],all_x=()): #c,A,b,Aeq,beq,LB,UB,X0,OPTIONS c = np.array(clist) A = np.array(Alist) b = np.array(blist) Aeq = np.array(Aeqlist) beq = np.array(beqlist) #求解 if m == 'min': res = optimize.linprog(c, A, b, Aeq, beq, bounds=all_x) fun = res.fun x = res.x else: res = optimize.linprog(-c, A, b, Aeq, beq, bounds=all_x) fun = -(res.fun) x = res.x return fun,x
main函數(shù),方便其它調(diào)用:
#-*- coding:utf-8 -*- import LP import sys if __name__ == '__main__': m = sys.argv[1] clist = list(eval(sys.argv[2])) Alist = list(eval(sys.argv[3])) blist = list(eval(sys.argv[4])) Aeqlist = list(eval(sys.argv[5])) beqlist =list(eval(sys.argv[6])) all_x = tuple(eval(sys.argv[7])) r=LP.LP(m=m,clist=clist,Alist=Alist,blist=blist,Aeqlist=Aeqlist,beqlist=beqlist,all_x=all_x) print(r)
說明: (1)因?yàn)橄到y(tǒng)參數(shù)傳入的都是字符串格式,所以main文件中,將傳入?yún)?shù)都轉(zhuǎn)換成列表。
(2)標(biāo)準(zhǔn)是最小值,如果是最大值,c應(yīng)該換成-c
最后執(zhí)行結(jié)果:
紅圈里就是最大值,和最優(yōu)解。
C#調(diào)用,參考:
https://blog.csdn.net/qq_42063091/article/details/82418630