1 前言
设计Java程序使用表上作业法求解产销平衡运输问题。本文思路:
- 最小元素法确定初始基可行解(最小元素法本质贪婪算法)。
- 位势法求解检验数。
- 闭回路调整法调整初始基可行解,获得最优解。其中闭回路使用深度优先搜索寻找。
若对以上方法、概念不熟悉请自行补充。
2 运输问题例题
采用清华大学第四版运输问题,题目如下:
单位运价
初始基本可行解
检验数表
最优解
3 程序
pacakage algori:算法
- GreedyAlgori:最小元素法求解初始基本可行解
- ReducedCost:求解检验数
- ImprovedClosedLoop:闭合回路
- DepthFirstSearch:深度优先搜索寻找闭回路
- TableDispatchingMethod:算法运行类
pacakage main:程序运行接口
pacakage transproblem:运输问题初始参数
pacakage util:工具方法
package algori;
import java.util.Arrays;
public class TableDispathcingMethod {
//运价表
int[][] cost;
//存放调运方案
int[][] initialSolution;
//检验数表
int[][] locationOfBasicVar;
// 检验数
int[][] reducedCost;
int[][] optimalSolution;
public TableDispathcingMethod(int[][] cost, int[] supply, int[] demand) {
this.cost = new int[supply.length][demand.length];
for (int i = 0; i < cost.length; i++) {
this.cost[i] = Arrays.copyOf(cost[i], cost[i].length);
}
this.initialSolution = new int[supply.length][demand.length];
this.reducedCost = new int[supply.length][demand.length];
for (int[] ints : reducedCost) {
Arrays.fill(ints, Integer.MAX_VALUE);
}
//最小元素法 获得初始基本可行解
GreedyAlgori greedyAlgori = new GreedyAlgori(cost, supply, demand, initialSolution);
//基变量位置
this.locationOfBasicVar = greedyAlgori.basicVar;
//求检验数
new ReducedCost(this.cost, reducedCost, locationOfBasicVar);
//寻找闭合回路
ImprovedClosedLoop ilp = new ImprovedClosedLoop(reducedCost);
optimalSolution = ilp.optimize(initialSolution);
writeAns();
}
public void writeAns() {
System.out.println("\n最优解: ");
for (int i = 0; i < optimalSolution.length; i++) {
System.out.println(Arrays.toString(optimalSolution[i]));
}
}
}
4 运行结果
{7=1, 3=2, 2=3, 6=4},等号前的数字为检验数的位置,后的数字为搜索时的顺序。