表上作业法-运输问题(Java)

该博客介绍了一种使用Java编程解决产销平衡运输问题的方法,通过最小元素法获取初始基本可行解,位势法计算检验数,深度优先搜索寻找闭回路并调整,最终得出最优解。程序包括GreedyAlgori、ReducedCost、ImprovedClosedLoop和TableDispatchingMethod等类。运行结果显示了最优解。
摘要由CSDN通过智能技术生成

1 前言

  设计Java程序使用表上作业法求解产销平衡运输问题。本文思路:

  1. 最小元素法确定初始基可行解(最小元素法本质贪婪算法)。
  2. 位势法求解检验数。
  3. 闭回路调整法调整初始基可行解,获得最优解。其中闭回路使用深度优先搜索寻找。

若对以上方法、概念不熟悉请自行补充。

2 运输问题例题

采用清华大学第四版运输问题,题目如下:

单位运价

初始基本可行解

检验数表

最优解

3 程序

pacakage algori:算法

  1. GreedyAlgori:最小元素法求解初始基本可行解
  2. ReducedCost:求解检验数
  3. ImprovedClosedLoop:闭合回路
  4. DepthFirstSearch:深度优先搜索寻找闭回路
  5. 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},等号前的数字为检验数的位置,后的数字为搜索时的顺序。 

5 附录

(1条消息) 表上作业法-运输问题(Java)-Java文档类资源-CSDN下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值