运筹学实验3 运输问题的求解(一)————利用工具箱求最优解

实验目的:

学会使用Matlab工具箱实现求解运输问题的最优解。

实验内容:

1.学习Matlab工具箱;
2.熟练掌握运输问题的数学模型。
3.实验例题:某运输问题(产销不平衡)的单位运价表如下表所示:
在这里插入图片描述

需要word文件请访问 http://daxs.top 站内搜索实验名称或者实验内容访问文章并且下载附件即可。

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是利用最小元素法运筹学运输问题的初始解并用闭合回路法判断并调整为最优解的 Python 代码: ```python import numpy as np # 输入相关数据 supply = np.array([10, 20, 30]) # 供应商每个仓库的供应量 demand = np.array([20, 30, 10, 10]) # 需方每个销售点的需量 cost = np.array([[2, 3, 1, 4], [5, 4, 8, 1], [3, 6, 2, 5]]) # 运输成本矩阵 # 最小元素法初始解 allocation = np.zeros((3, 4)) while np.sum(supply) > 0 and np.sum(demand) > 0: # 找到成本最小的元素 min_cost = np.min(cost) min_cost_index = np.argwhere(cost == min_cost) row = min_cost_index[0][0] col = min_cost_index[0][1] # 分配货物 amount = min(supply[row], demand[col]) allocation[row, col] = amount supply[row] -= amount demand[col] -= amount # 将已分配的成本设为极大值 cost[row, col] = np.inf # 闭合回路法最优解 while True: # 计算各个销售点的运输量 sales_amount = np.sum(allocation, axis=0) # 计算各个供应商的运输量 supply_amount = np.sum(allocation, axis=1) # 找到存在缺货的销售点 shortage_index = np.argwhere(sales_amount < demand) if len(shortage_index) == 0: break # 找到缺货销售点的缺货量 shortage = demand[shortage_index[0][0]] - sales_amount[shortage_index[0][1]] # 找到从该销售点出发、经过未分配路径、返回该销售点的路径 for i in range(allocation.shape[0]): for j in range(allocation.shape[1]): if allocation[i, j] > 0: # 判断路径是否符合条件 if i in shortage_index and j not in shortage_index: path = [(i, j)] current_row = i current_col = j while current_col != shortage_index[0][1]: next_row, next_col = np.argwhere(allocation[current_row,:] > 0)[0] path.append((next_row, next_col)) current_row = next_row current_col = next_col # 找到回路中最小的分配量 min_amount = np.min([allocation[row, col] for row, col in path]) # 调整路径上的分配量 for row, col in path: allocation[row, col] -= min_amount elif i not in shortage_index and j in shortage_index: path = [(i, j)] current_row = i current_col = j while current_row != shortage_index[0][0]: next_row, next_col = np.argwhere(allocation[:,current_col] > 0)[0] path.append((next_row, next_col)) current_row = next_row current_col = next_col # 找到回路中最小的分配量 min_amount = np.min([allocation[row, col] for row, col in path]) # 调整路径上的分配量 for row, col in path: allocation[row, col] -= min_amount # 输出最优解 total_cost = np.sum(allocation * cost) print("最优解为:") print(allocation) print("总成本为:", total_cost) ``` 这段代码实现了利用最小元素法运筹学运输问题的初始解,并用闭合回路法判断并调整为最优解。你可以根据自己的需进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值