數學建模校內賽-整數規劃模型-拋磚玉
1.先記錄一下自己踏進去的坑
1.首先是python的scipy對應的線性規劃庫不可以以二維陣列作為引數,cvxpy的決策變數也最高為二維
2.cvxpy庫最好還是安裝37版本,如何建立多環境並安裝包在我的另一篇部落格有介紹
3.cvxpy,一定要用resluts=prob.solve(solver=cp.CPLEX),另外那個GLPK_MI別用
我也看不懂那個求解器好點
賽題
模型
Objective function(1
): Meet the minimum cost
Constraint condition(2): Meet carbohydrate intake constraints
Constraint condition(3): Meet the restriction on fat intake
Constraint condition(4): Meet the restriction on calorie intake
Constraint condition(5): Meet the restriction on calcium intake
Constraint condition(6): Meet the restriction on phosphorus intake
Constraint condition(7): Meet the restriction of iron intake
Constraint condition(8): Meet the restriction of vitamin A intake
Constraint condition(9): Meet the restriction on vitamin B2 intake
The intake data of various nutrients are from the reasonable dietary composition index recommended by Chinese Nutrition Society and Baidu Encyclopedia.
程式碼
#from scipy.optimize import linprog
import xlrd
import numpy as np
import cvxpy as cp
from scipy.optimize import linprog
workbook = xlrd.open_workbook('fo.xlsx')#Extract data from a data set
sheet1= workbook.sheet_by_index(0)
Afat=sheet1.col_values(colx = 0)
Acar=sheet1.col_values(colx = 1)
Apro= sheet1.col_values(colx = 2)
Ava=sheet1.col_values(colx = 3)
Avb2=sheet1.col_values(colx = 4)
Avc=sheet1.col_values(colx = 5)
Ak=sheet1.col_values(colx = 6)
Aca=sheet1.col_values(colx = 7)
Ap=sheet1.col_values(colx = 8)
Afe=sheet1.col_values(colx = 9)
Aze=sheet1.col_values(colx = 10)
Aka=sheet1.col_values( colx = 11)
Ama=sheet1.col_values(colx = 12)
end1=sheet1.col_values(colx = 13)
L=len(Ama)
d = [0]*49
d[0:2]=[1]*2
e = [0]*49
e[2:36]=[1]*34
f = [0]*49
f[36:42]=[1]*6
g = [0]*49
g[42:49]=[1]*7
c = np.array(Ama)#定義目標向量
a=np.array([np.array(Acar)*-1,np.array(Afat)*-1,np.array(Aka)*-1,np.array(Aka),np.array(Aca)*-1,
np.array(Ap)*-1,np.array(Afe)*-1,np.array(Ava)*-1,np.array(Avb2)*-1,
np.array(d)*-1,np.array(e)*-1,np.array(f)*-1,np.array(g)*-1])#定義約束矩陣
b=np.array([-122.5,-122.5,-1980,2340,-600,-720,-10,-1.5,-1.5,-1,-1,-1,-1])#定義約束條件右邊向量
res = linprog(c, A_ub=a, b_ub=b,
options={"disp": True})#線性規劃(由於非整數,後期棄用)
x=cp.Variable((7,L),integer=True)#定義整數決策變數
objective=cp.Minimize(cp.sum(x*c))#構造目標函式
constriants=[0<=x,x*a[4]<=b[4],x*a[5]<=b[5],
x*a[6]<=b[6],x*a[7]<=b[7],
x*a[9]<=b[9],x*a[10]<=b[10],x*a[11]<=b[11],
x*a[12]<=b[12],x<=1]#構造約束條件(通過調節最後x<=的值對每種食物當天最大購買量進行限制,值越小種類越多(第二問);值越大,哪種食物購買量最大,則對結果影響最大(第三問))
prob=cp.Problem(objective,constriants)#構造問題模型
resluts=prob.solve(solver=cp.CPLEX)#求解問題
print(prob.value)#目標函式的值
print(x.value)#各x的值
def maxqq():#最大花銷(針對第3問)的改變
x=cp.Variable((7,L),integer=True)#定義整數決策變數
objective=cp.Maximize(cp.sum(x*c))#構造目標函式
constriants=[0<=x,x*a[0]<=b[0],x*a[3]<=b[3],x*a[4]<=b[4],x*a[5]<=b[5],
x*a[6]<=b[6],x*a[7]<=b[7],x*a[8]<=b[8],x*a[9]<=b[9],x*a[10]<=b[10],x*a[11]<=b[11],
x*a[12]<=b[12],x<=2]#構造約束條件(通過調節最後x<=的值對每種食物當天最大購買量進行限制,值越小種類越多;值越大,哪種食物購買量最大,則對結果影響最大)
prob=cp.Problem(objective,constriants)#構造問題模型
resluts=prob.solve(solver=cp.CPLEX)#求解問題
print(prob.value)#目標函式的值
print(x.value)#各x的值
end2=[]
end3=[]
for i in x.value:
for j in range(L):
end2.append(end1[j]*int(i[j]))
end3.append(end2)
end2=[]
print(end3)
return 0
return 0
print("不考慮花費時的建議")
end2=[]
end3=[]
for i in x.value:
for j in range(L):
end2.append(end1[j]*int(i[j]))
end3.append(end2)
end2=[]
print(end3)
#maxqq()
遺留問題
1.無法自動生成每日三餐的,可能還需要一些針對個別食物的約束,比如牛奶不能出現在中午,但是那樣可能需要一個三維矩陣,不過應該可以通過約束條件的修改來降低維度,比如每行的長度乘三。
2.資料對結果影響較大,導致算不出題目要求結果,資料的多少與各種食物的指標都在影響,影響的關係由於時間原因沒有測量。