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 =