学习笔记,仅作参考。
个人觉得配合步骤和建模,直接看代码就能入门pulp,所以没有啥解释,见谅。
https://blog.csdn.net/youcans/article/details/116371416
安装PuLp (pip install pulp)
导入PuLp (from pulp import * )或者(import pulp as pl)
定义线性规划问题
PB = LpProblem ( problem name , sense )"""problem name=问题名称sense=LpMinimize/LpMaximizee.g. MyProbLP = pulp.LpProblem("LPProbDemo1", sense=pulp.LpMaximize)"""
定义决策变量
小tips
当约束条件需要变量大于某个数或小于某个数,用lowBound和upBound,不需要在约束条件中写!!!
DV = pulp.LPVariable ( decision variable name , lowbound , upbound ,category )"""DV=decision variable name:变量名,lowBound和upBound:下界和上界, 默认分别是负无穷到正无穷,参数 cat 用来设定变量类型,可选参数值: ‘Continuous’ 表示连续变量(默认值)、 ’ Integer ’ 表示离散变量(用于整数规划问题)、 ’ Binary ’ 表示0/1变量(用于0/1规划问题) """
根据变量类型可以做不一样的变量定义
单个变量
x1 = pulp.LpVariable('x1', lowBound=0, upBound=7, cat='Continuous')
一维变量
就是添加range(数组长度)
x = pl.LpVariable.dict("x",range(4),lowBound=0)
二维变量
y_ij=pl.LpVariable.dict("y_ij",(range(len1),(range(len2))),cat=pl.LpBinary)
添加目标函数
添加目标函数使用 “问题名 += 目标函数式” 格式。
PB += linear objective in equantion from objective name"""e.g. MyProbLP+= 2*x1 + 3*x2 - 5*x3 # 设置目标函数"""
添加约束条件
添加约束条件使用 “问题名 += (约束条件表达式)” 格式。
PB += linear objective in equantion from constraint name"""e.g. MyProbLP+= (2*x1 - 5*x2 + x3 >= 10) # 不等式约束 MyProbLP+= (x1 + 3*x2 + x3 <= 12) # 不等式约束 MyProbLP+= (x1 + x2 + x3 == 7) # 等式约束"""
写入LP文件PB.writeLP ( filename )
模型求解PB.slove ( )
MyProbLP.solve() print("Status:", pulp.LpStatus[MyProbLP.status]) # 输出求解状态 for v in MyProbLP.variables(): print(v.name, "=", v.varValue) # 输出每个变量的最优值 print("F(x) = ", pulp.value(MyProbLP.objective)) #输出最优解的目标函数值
结果显示check status : pulp.LpStatus[PB.status]
import pulp as pldef rumen_1(): # 定义问题 et = pl.LpProblem("et", sense=pl.LpMinimize) # 定义变量 x = pl.LpVariable("x", lowBound=0, cat="Integer ") y = pl.LpVariable("y", lowBound=0, cat="Integer ") z = pl.LpVariable("z", lowBound=0, cat="Integer ") w = pl.LpVariable("w", lowBound=0, cat="Integer ") # 添加目标函数 et += 5 * x + 6 * y + 7 * z + 8 * w # 添加约束条件 et += (x + y + z + w == 100) et += (5 * x + 4 * y + 5 * z + 6 * w >= 530) et += (2 * x + y + z + 2 * w <= 160) # 求解 et.solve() for v in et.variables(): print(v.name, "=", v.varValue) print("F(x)=", pl.value(et.objective))
同样用例1的模型
def rumen_2(): # 定义问题 et = pl.LpProblem("et", sense=pl.LpMinimize) # 定义变量 x = pl.LpVariable.dict("x",range(4),lowBound=0) # 添加目标函数 et += 5 * x[0] + 6 * x[1] + 7 * x[2] + 8 * x[3] # 添加约束条件 et += (x[0] + x[1] + x[2] + x[3] == 100) et += (5 * x[0] + 4 * x[1] + 5 * x[2] + 6 * x[3] >= 530) et += (2 * x[0] + x[1] + x[2] + 2 * x[3] <= 160) # 求解 et.solve() for v in et.variables(): print(v.name, "=", v.varValue) print("F(x)=", pl.value(et.objective))
此处模型感谢Cathy友情提供
def jinjie_1(): n = 200 # residential areas -变量i居民区数量 m = 40 # shelters-变量j庇护所数量 areas = range(n) shelters = range(m) path = 'Pb2_areas.csv' areas_matrix = np.genfromtxt(path, dtype=float, delimiter=',', encoding='utf-8-sig') path = 'Pb2_shelters.csv' shelters_matrix = np.genfromtxt(path, dtype=float, delimiter=',', encoding='utf-8-sig') R = areas_matrix[:, 2] # 第三列的居民区居民数量-变量R_i C = shelters_matrix[:, 2] # 第三列的庇护所能庇护的容量-变量C_j # 计算居民到庇护所的距离 D = np.zeros((n, m)) # -变量D_ij距离 for i in areas: for j in shelters: D[i, j] = abs(areas_matrix[i, 0] - shelters_matrix[j, 0]) + abs(areas_matrix[i, 1] - shelters_matrix[j, 1]) #定义问题 m=pl.LpProblem("Cathy_exp",sense=pl.LpMinimize) #定义变量 x=pl.LpVariable.dict("x",areas,cat=pl.LpBinary) y_ij=pl.LpVariable.dict("y_ij",(areas,shelters),cat=pl.LpBinary) #目标函数 m+=pl.lpSum(pl.lpSum(D[i,j] * y_ij[i,j] for i in areas) for j in shelters) #约束条件 m += (pl.lpSum(x[j] for j in shelters) == 10) for i in areas: m+=(pl.lpSum(y_ij[i,j] for j in shelters) == 1) for j in shelters: m+=(pl.lpSum(R[i] * y_ij[i,j] for i in areas) <= C[j] * x[j]) m.solve() for v in m.variables(): print(v.name, "=", v.varValue) print("F(x)=", pl.value(m.objective))
来源地址:https://blog.csdn.net/weixin_46967285/article/details/128103935