您好,登錄后才能下訂單哦!
線性規(guī)劃標(biāo)準(zhǔn)形式:MATLAB
-------------
線性規(guī)劃求解主要分 兩個部分,目標(biāo)函數(shù)(max,min)和約束條件(s.t.),求解時一般要化為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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。