数学建模校内赛-整数规划模型-抛砖玉

此文转载自:https://blog.csdn.net/lu750310/article/details/112392080

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.数据对结果影响较大,导致算不出题目要求结果,数据的多少与各种食物的指标都在影响,影响的关系由于时间原因没有测量。

   

更多内容详见微信公众号:Python测试和开发

Python测试和开发

原文地址:https://www.cnblogs.com/phyger/p/14262018.html