目录

  • 1.pulp库解决简单线性规划问题
  • 2.pulp库解决大型线性规划问题
  • 3.scipy库解决线性规划


1.pulp库解决简单线性规划问题

'''
max		fx = 2*x1 + 3*x2 - 5*x3
s.t.	x1 + 3*x2 + x3 <= 12
		2*x1 - 5*x2 + x3 >= 10
		x1 + x2 + x3 = 7
		x1, x2, x3 >=0
'''
import pulp

#1.定义一个规划问题
#Demo1是定义的问题名(用于输出信息);sense可选LpMinimize或LpMaximize
Problem = pulp.LpProblem("Demo1", sense=pulp.LpMaximize)

#2.定义决策变量,调用pulp.LpVariable函数
#lowBound、upBound用来设定决策变量的下界、上界;默认为负无穷/正无穷
#cat用来设定变量类型,可选参数值:Continuous表示连续变量(默认)、Integer表示离散变量(用于整数规划问题)、Binary表示0/1变量(用于0/1规划问题)
x1 = pulp.LpVariable('x1', lowBound=0, upBound=7, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7, cat='Continuous')
x3 = pulp.LpVariable('x3', lowBound=0, upBound=7, cat='Continuous')

#3.添加目标函数
Problem += 2*x1 + 3*x2 - 5*x3  #设置目标函数

#4.添加约束条件
Problem += (x1 + 3*x2 + x3 <= 12)  #不等式约束
Problem += (2*x1 - 5*x2 + x3 >= 10)  #不等式约束
Problem += (x1 + x2 + x3 == 7)  #等式约束

#5.求解
Problem.solve()
for v in Problem.variables():#依次输出每个变量的最优值
    print(v.name, "=", v.varValue)
print("F(x) = ", pulp.value(Problem.objective))#输出最优解的目标函数值

2.pulp库解决大型线性规划问题

使用字典定义变量、目标函数、约束条件参数:

import pulp
AlloyModel = pulp.LpProblem("钢材生产问题", pulp.LpMinimize) #1.定义一个规划问题
#2.定义决策变量
material = ['废料1', '废料2', '废料3', '废料4', '镍', '铬', '钼']
mass = pulp.LpVariable.dicts("原料", material, lowBound=0, cat='Continuous')
#3.添加目标函数
cost = {'废料1': 16,'废料2': 10, '废料3': 8,'废料4': 9,'镍': 48,'铬': 60,'钼': 53}
AlloyModel += pulp.lpSum([cost[item] * mass[item] for item in material]), "总生产成本"
#4.添加约束
carbonPercent = {'废料1': 0.8,'废料2': 0.7,'废料3': 0.85,'废料4': 0.4,'镍': 0,'铬': 0,'钼': 0}
NiPercent = {'废料1': 18,'废料2': 3.2,'废料3': 0,'废料4': 0,'镍': 100,'铬': 0,'钼': 0}
CrPercent = {'废料1': 12,'废料2': 1.1,'废料3': 0,'废料4': 0,'镍': 0, '铬': 100,'钼': 0}
MoPercent = {'废料1': 0,'废料2': 0.1,'废料3': 0,'废料4': 0,'镍': 0,'铬': 0,'钼': 100}
AlloyModel += pulp.lpSum([mass[item] for item in material]) == 1000, "质量约束"
AlloyModel += pulp.lpSum([carbonPercent[item] * mass[item] for item in material]) >= 0.65 * 1000, "碳最小占比"
AlloyModel += pulp.lpSum([carbonPercent[item] * mass[item] for item in material]) <= 0.75 * 1000, "碳最大占比"
AlloyModel += pulp.lpSum([NiPercent[item] * mass[item] for item in material]) >= 3.0 * 1000, "镍最小占比"
AlloyModel += pulp.lpSum([NiPercent[item] * mass[item] for item in material]) <= 3.5 * 1000, "镍最大占比"
AlloyModel += pulp.lpSum([CrPercent[item] * mass[item] for item in material]) >= 1.0 * 1000, "铬最小占比"
AlloyModel += pulp.lpSum([CrPercent[item] * mass[item] for item in material]) <= 1.2 * 1000, "铬最大占比"
AlloyModel += pulp.lpSum([MoPercent[item] * mass[item] for item in material]) >= 1.1 * 1000, "钼最小占比"
AlloyModel += pulp.lpSum([MoPercent[item] * mass[item] for item in material]) <= 1.3 * 1000, "钼最大占比"
AlloyModel += mass['废料1'] <= 75, "废料1可用量"
AlloyModel += mass['废料2'] <= 250, "废料2可用量"
AlloyModel.solve()#5.求解
# print(AlloyModel)#输出问题设定参数和条件
for v in AlloyModel.variables():#依次输出每个变量的最优值
    print(v.name, "=", v.varValue)
print("F(x)= ", pulp.value(AlloyModel.objective))#输出最优解的目标函数值

3.scipy库解决线性规划

import numpy as np
from scipy import optimize as op
# 给出变量取值范围
x1=(0,None)
x2=(0,None)
x3=(0,None)
c = np.array([2,1,1])# 目标函数系数,3x1列向量
A_ub = np.array([[0,2,-1],[0,1,-1]]) # 不等式约束系数A,2x3维矩阵
B_ub = np.array([-2,1])# 等式约束系数B, 2x1维列向量
A_eq = np.array([[1,-1,1]])# 等式约束系数Aeq,3x1维列向量
B_eq = np.array([2])# 等式约束系数beq,1x1数值
res=op.linprog(c,A_ub,B_ub,A_eq,B_eq,bounds=(x1,x2,x3))#调用函数进行求解
#c代表规划最小值
print(res)