基于python的数学建模---运输问题
2022/7/25 14:27:28
本文主要是介绍基于python的数学建模---运输问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
代码
import pulp import numpy as np from pprint import pprint def transport_problem(costs, x_max, y_max): row = len(costs) col = len(costs[0]) prob = pulp.LpProblem('Transportation Problem', sense=pulp.LpMaximize) var = [[pulp.LpVariable(f'x{i}{j}', lowBound=0, cat=pulp.LpInteger) for j in range(col)] for i in range(row)] flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]#定义一个x,x若为列表形式则执行for循环,flatten将多维数组转换为一维数组 prob += pulp.lpDot(flatten(var), costs.flatten())#costs是numpy定义的,有自己的函数 for i in range(row): prob += (pulp.lpSum(var[i])) <= x_max[i] for j in range(col): prob += (pulp.lpSum(var[i][j] for i in range(row)) <= y_max[j]) prob.solve() return {'objective': pulp.value(prob.objective), 'var': [[pulp.value(var[i][j]) for j in range(col)] for i in range(row)]} if __name__ == '__main__': costs = np.array([[500, 550, 630, 1000, 800, 700], [800, 700, 600, 950, 900, 930], [1000, 960, 840, 650, 600, 700], [1200, 1040, 980, 860, 880, 780]]) max_plant = [76, 88, 96, 40] max_cultivation = [42, 56, 44, 39, 60, 59] res = transport_problem(costs, max_plant, max_cultivation) print(f'最大值为{res["objective"]}') print('各变量的取值为: ') pprint(res['var'])
这篇关于基于python的数学建模---运输问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币