复现一篇分布式装配混合零空闲置换流水车间调度问题的代码

复现一篇分布式装配混合零空闲置换流水车间调度问题的代码

摘要

In this paper, we study the distributed assembly mixed no-idle permutation flowshop scheduling problem (DAMNIPFSP) with total tardiness objective. We first formulate the problem. Second, based on the characteristics of the DAMNIPFSP, an improved Iterated Greedy algorithm, named RIG (Referenced Iterated Greedy), with two novel destruction methods, four new reconstruction methods and six new local search methods is presented. Among them, two of the reconstruction methods and four of the local search methods are based on a reference, which proves key to performance. Finally, RIG is compared with the related algorithms through experiments. The results show that the new RIG algorithm is a new state-of-the-art procedure for the DAMNIPFSP with the total tardiness criterion.

说明

这篇文章使用的模型比较新,是分布式带装配混合零空闲置换流水车间调度模型。主要考虑到现实中并非所有机器都有零空闲约束的问题。本博客实现了其算法,模型并未使用其模型,而是使用了分布式装配置换流水车间调度模型,评价指标使用了Cmax,并未使用原文中的总延迟指标。感兴趣的请自行实现其代码。本人并不精通Java,出于研究需要,使用了Java作为编程语言,感兴趣的可使用C或C++,可进一步提高算法效率。另外,由于各种因素,本博客仅供参考,请大家批判阅读、使用,算法实现过程中难免会有疏漏和谬误,欢迎各位批评指正并与我进行交流,这将有助于改善我的工作。测试集可能不完全相同,有需要的请私信。

代码

测试集请在该网站自行下载:调度测试集

测试类

package practice.comparison;

import Instances.DataProcessing;
import practice.dapfsp.Heuristic1;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2022/3/30 9:22
 * @Author: 
 * @File: TestRIG.class
 * @Software: IDEA
 * @Email: 1532116227@qq.com
 */
public class TestRIG
{
    public static void main(String[] args)
    {
        //小规模========================================================================================================
        int count = 1;
        int jobs[] = {8, 12, 16, 20, 24};
        int machines[] = {2, 3, 4, 5};
        int factories[] = {2, 3, 4};
        int products[] = {2, 3, 4};
        int instnces[] = {1, 2, 3, 4, 5};
        int[][] p = new int[0][];
        ReadTxt readTxt = new ReadTxt();
        List<Integer> pi = new ArrayList<>();//工件序列
        RIG2 rig2 = new RIG2();
        List<Integer> apt = new ArrayList<>();
        int[] assignSet;

        for (int n = 0; n < jobs.length; n++)//job.length - 1
        {
            for (int m = 0; m < machines.length; m++)//mechine.length - 1
            {
                for (int k = 0; k < factories.length; k++)//num.length - 1
                {
                    for (int i = 0; i < products.length; i++)
                    {
                        for (int j = 0; j < instnces.length; j++)
                        {
                            p = readTxt.getProcessingTimes("D:\\Instances\\DAPFSPSmall\\ProcessingTime\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], machines[m]);
                            apt = readTxt.getAssemblyTimes("D:\\Instances\\DAPFSPSmall\\AssemblyTime\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", products[i]);
                            assignSet = new int[jobs[n]];
                            assignSet = readTxt.getAssignSet("D:\\Instances\\DAPFSPSmall\\AssignSet\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], products[i]);
                            System.out.println(count++);
                            pi = rig2.algorithm(p, apt, factories[k], products[i], assignSet);
                            System.out.println(pi);
                            System.out.println("============================================");
                        }
                    }

                }
            }
        }

        //==============================================================================================================
//        int[][] p = {{4, 6, 2}, {3, 4, 2}, {5, 2, 3}, {5, 4, 2}, {4, 2, 2}, {3, 3, 3},
//                {4, 3, 2}, {3, 5, 2}, {6, 2, 3}, {3, 5, 2}, {4, 4, 2}, {4, 3, 3}};//加工时间矩阵
//        int[] assignSet = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        //==============================================================================================================
//        int[][] p = {{1, 3}, {5, 8}, {7, 5}, {9, 7}, {9, 3}, {3, 4}, {8, 1}, {4, 3}, {2, 5}};
//        int[] assignSet = {2, 2, 1, 1, 3, 1, 3, 2, 2};
        //==============================================================================================================
//        int[][] p = {{3, 4}, {1, 3}, {5, 8}, {7, 5}, {8, 1}, {4, 3}, {9, 7}, {9, 3}, {2, 5}};
//        int[] assignSet = {1, 2, 2, 1, 3, 2, 1, 3, 2};
//        int numOfProduct = 4;//产品数
//        List<Integer> lambda = new ArrayList<>();//产品序列
//        RIG rig = new RIG();
//        apt = List.of(6, 19, 12);//集合中的元素个数已确定,不能再改变时才能使用
//        pi = rig.algorithm(p, pi, apt, lambda, F, t, assignSet);
        //==============================================================================================================
//        int F = 2;//工厂数
//        int t = 4;//产品数
//        List<Integer> pi = new ArrayList<>();//工件序列
//        RIG2 rig2 = new RIG2();
//        List<Integer> apt;
//        apt = List.of(6, 8, 5, 8);//装配时间
//        pi = rig2.algorithm(p, apt, F, t, assignSet);
//        System.out.println(pi);
    }
}

算法主体

package practice.comparison;

import practice.calculateCMAX.CalculateCmaxPFSP;
import practice.dapfsp.Heuristic1;

import java.util.*;

/**
 * @Date: 2022/4/6 20:36
 * @Author: 
 * @File: RIG2.class
 * @Software: IDEA
 * @Email: 1532116227@qq.com
 */
public class RIG2
{
    Random random = new Random();

    public List<Integer> algorithm(int[][] p, List<Integer> apt, int F, int t, int[] assignSet)
    {
        double beta = 0.05;
        List<Integer> piT;
        Heuristic1 h1 = new Heuristic1();
        List<Integer> pi_product;
        pi_product = Heuristic1.ruleOfSPT(apt);//产品的装配序列
        List<Integer> pi_product0 = listCopy(pi_product);
        List<Integer>[] sequence = new List[t];
        List<Integer>[] solution1 = new List[t];
        List<Integer>[] best_solution = new List[t];
        List<Integer> pi_product1;
        for (int i = 0; i < t; i++)
        {
            sequence[i] = new ArrayList<>();
            best_solution[i] = new ArrayList<>();
            solution1[i] = new ArrayList<>();
        }
        for (int i = 0; i < assignSet.length; i++)
        {
            sequence[assignSet[i] - 1].add(i + 1);
        }
        //==============================================================================================================
        //Obtain the initial solution by H11/H12
//        piT = h1.algorithm(p, t, apt, sequence, pi_product);
        sequence = h1.algorithm(p, t, apt, sequence, pi_product);
//        System.out.println(piT);
//        List<Integer>[][] assignment;//解码:将一个解解码为一个调度
//        assignment = new List[1][F];
//        assignment[0] = Heuristic1.assignByNR1(p, piT, F);//NR1
//        assignment[0] = Heuristic1.ecfRule(p, piT, F);//NR2
//        for (int i = 0; i < F; i++)
//        {
//            solution.addAll(assignment[0][i]);
//        }
//        best_solution.addAll(solution);
        for (int i = 0; i < t; i++)
        {
            best_solution[i].addAll(sequence[i]);
        }
        pi_product1 = listCopy(pi_product);
        //==============================================================================================================
        int LSType = 0;
        switch (LSType)
        {
            case 0:
                pi_product = insertProductLS(sequence, p, pi_product, t, F, apt);
//                System.out.println(pi_product);
                sequence = insertJobLS(sequence, p, pi_product, t, F, apt);
                break;
            case 1:
                pi_product = insertProductRfLS(sequence, pi_product1, p, pi_product, t, F, apt);
//                System.out.println(pi_product);
                sequence = insertJobRfLS(sequence, best_solution, p, pi_product, t, F, apt);
                break;
            case 2:
                pi_product = swapProductRfLS(sequence, pi_product1, p, pi_product, t, F, apt);
                sequence = swapJobRfLS(sequence, best_solution, p, pi_product, t, F, apt);
                break;
        }
        pi_product1 = listCopy(pi_product);
        for (int i = 0; i < t; i++)
        {
            best_solution[i] = listCopy(sequence[i]);
            solution1[i] = listCopy(sequence[i]);
//            best_solution[i].clear();
//            best_solution[i].addAll(sequence[i]);
//            solution1[i].addAll(sequence[i]);
        }
        int dp = 3;
        int dj = 5;
        int Gen = 100, gen = 0;
        int ConType = 1;
        List<Integer> lambdap = new ArrayList<>();
        List<Integer> lambda = new ArrayList<>();
        List<Integer> lambdaj = new ArrayList<>();
        List<List<Integer>> lists = new ArrayList<>();
        while (gen < Gen)
        {
            lists = destructionProduct(dp, pi_product);
            lambda = lists.get(0);
            lambdap = lists.get(1);
            switch (ConType)
            {
                case 0:
                    pi_product = reconstructionProduct(lambda, lambdap, sequence, apt, p, F);
                    break;
                case 1:
                    pi_product = reconstructionProductRF(lambda, lambdap, sequence, pi_product1, apt, p, F, t);
                    break;
            }
            lambdaj = destructionJob(dj, pi_product, solution1);
            switch (ConType)
            {
                case 0:
                    solution1 = reconstructionJob(lambdaj, pi_product, solution1, sequence, apt, p, F);
                    break;
                case 1:
                    solution1 = reconstructionJobRF(lambdaj, pi_product, solution1, sequence, best_solution, apt, p, F, t);
                    break;
            }
            switch (LSType)
            {
                case 0:
                    pi_product = insertProductLS(solution1, p, pi_product, t, F, apt);
                    solution1 = insertJobLS(solution1, p, pi_product, t, F, apt);
                    break;
                case 1:
//                    pi_product = insertProductRfLS(sequence, pi_product1, p, pi_product, t, F, apt);
//                    sequence = insertJobRfLS(sequence, best_solution, p, pi_product, t, F, apt);
                    pi_product = insertProductRfLS(solution1, pi_product1, p, pi_product, t, F, apt);
                    solution1 = insertJobRfLS(solution1, best_solution, p, pi_product, t, F, apt);
                    break;
                case 2:
//                    pi_product = swapProductRfLS(sequence, pi_product1, p, pi_product, t, F, apt);
//                    sequence = swapJobRfLS(sequence, best_solution, p, pi_product, t, F, apt);
                    pi_product = swapProductRfLS(solution1, pi_product1, p, pi_product, t, F, apt);
                    solution1 = swapJobRfLS(solution1, best_solution, p, pi_product, t, F, apt);
                    break;
            }
            int r;
            int cmax1;
            int cmax2;
            int cmax3;
            List<Integer>[] schedule = new List[F];
            List<Integer> list1 = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            List<Integer> list3 = new ArrayList<>();
            for (int i = 0; i < t; i++)
            {
                list1.addAll(solution1[pi_product.get(i) - 1]);
                list2.addAll(sequence[pi_product0.get(i) - 1]);
                list3.addAll(best_solution[pi_product1.get(i) - 1]);
            }
            //==============================================================================================================
//            schedule = Heuristic1.assignByNR1(p, list1, F);//NR1
            schedule = Heuristic1.ecfRule(p, list1, F);
            cmax1 = calAssembleCmax(schedule, p, F, pi_product, sequence, apt);
            //==============================================================================================================
//            schedule = Heuristic1.assignByNR1(p, list2, F);//NR1
            schedule = Heuristic1.ecfRule(p, list2, F);
            cmax2 = calAssembleCmax(schedule, p, F, pi_product0, sequence, apt);
            //==============================================================================================================
//            schedule = Heuristic1.assignByNR1(p, list3, F);//NR1
            schedule = Heuristic1.ecfRule(p, list3, F);
            cmax3 = calAssembleCmax(schedule, p, F, pi_product1, sequence, apt);
            //==============================================================================================================
            double RPD, TempFactor;
            if (cmax1 < cmax2)
            {
//                sequence = solution1;
                sequence = list1DimArrayCopy(solution1);
                pi_product0.clear();
                pi_product0.addAll(pi_product);
                if (cmax1 < cmax3)
                {
                    best_solution = list1DimArrayCopy(solution1);
                    pi_product1.clear();
                    pi_product1.addAll(pi_product);
                }
            }
            else if (beta > 0)
            {
                RPD = (cmax1 - cmax2) / cmax2 * 100;
                TempFactor = beta * (sumOfPij(p)) / 10 * p.length * p[0].length;
                r = random.nextInt();
                if (r < Math.exp(-RPD / TempFactor))
                {
                    sequence = list1DimArrayCopy(solution1);
                    pi_product0.clear();
                    pi_product0.addAll(pi_product);
                }
            }
            gen += 1;
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < best_solution.length; i++)
        {
            list.addAll(best_solution[pi_product1.get(i) - 1]);
        }
        int Cmax;
        List<Integer>[] schedule = new List[F];
//        schedule = Heuristic1.assignByNR1(p, list, F);//NR1
        schedule = Heuristic1.ecfRule(p, list, F);
        Cmax = calAssembleCmax(schedule, p, F, pi_product1, sequence, apt);
        System.out.println("Cmax = " + Cmax);
        return list;
    }

    private List<Integer>[] reconstructionJobRF(List<Integer> lambdaj, List<Integer> pi_product, List<Integer>[] solution1, List<Integer>[] sequence, List<Integer>[] best_solution, List<Integer> apt, int[][] p, int F, int t)
    {
        int cmaxTemp = 0;
        int cmax = 0;
        int index = 0;
        int r;
        int removed;
        List<Integer> list = new ArrayList<>();
        List<Integer>[] schedule = new List[F];

        for (int i = 0; i < t; i++)
        {
            for (int h = 0; h < best_solution[i].size(); h++)
            {
                if (lambdaj.contains(best_solution[i].get(h)))
                {
                    removed = lambdaj.remove(lambdaj.indexOf(best_solution[i].get(h)));
                }
                else
                {
                    continue;
                }

                if (sequence[i].contains(removed))
                {
                    cmax = Integer.MAX_VALUE;
                    for (int k = 0; k < solution1[i].size() + 1; k++)
                    {
                        solution1[i].add(k, removed);
                        list.clear();
                        for (int j = 0; j < solution1.length; j++)
                        {
                            list.addAll(solution1[pi_product.get(j) - 1]);
                        }
//                        schedule = Heuristic1.assignByNR1(p, list, F);//NR1
                        schedule = Heuristic1.ecfRule(p, list, F);
                        cmaxTemp = calAssembleCmax2(schedule, solution1, p, F, pi_product, sequence, apt);
                        solution1[i].remove(k);
                        if (cmaxTemp < cmax)
                        {
                            cmax = cmaxTemp;
                            index = k;
                        }
                    }
                    solution1[i].add(index, removed);
//                    break;
                }
            }
        }
        return solution1;
    }

    private List<Integer> reconstructionProductRF(List<Integer> lambda, List<Integer> lambdap, List<Integer>[] sequence, List<Integer> pi_product1, List<Integer> apt, int[][] p, int F, int t)
    {
        int r;
        int removed;
        int cmax;
//        List<Integer> copyLambdap = new ArrayList<>();
//        copyLambdap = listCopy(lambdap);
        List<Integer>[] schedule = new List[F];
        List<Integer> list1 = new ArrayList<>();
        int cmaxTemp = 0;
        int index = 0;

        for (int l = 0; l < t; l++)
        {
            cmax = Integer.MAX_VALUE;
            if (lambdap.contains(pi_product1.get(l)))
            {
                removed = lambdap.remove(lambdap.indexOf(pi_product1.get(l)));
                for (int k = 0; k < lambda.size() + 1; k++)
                {
                    list1.clear();
                    lambda.add(k, removed);
                    for (int i = 0; i < lambda.size(); i++)
                    {
                        list1.addAll(sequence[lambda.get(i) - 1]);
                    }
//                schedule = Heuristic1.assignByNR1(p, list1, F);//NR1
                    schedule = Heuristic1.ecfRule(p, list1, F);
//                if (schedule == null)
//                {
//                    System.out.println();
//                }
                    cmaxTemp = calAssembleCmax(schedule, p, F, lambda, sequence, apt);
                    lambda.remove(k);
                    if (cmaxTemp < cmax)
                    {
                        cmax = cmaxTemp;
                        index = k;
                    }
                }
                lambda.add(index, removed);
            }
        }
        return lambda;
    }

    private List<Integer>[] swapJobRfLS(List<Integer>[] sequence, List<Integer>[] best_solution, int[][] p, List<Integer> pi_product, int t, int F, List<Integer> apt)
    {
        List<Integer>[] schedule = new List[F];
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3 = new ArrayList<>();
        List<Integer>[] solution1 = new List[sequence.length];
        List<Integer>[] solution2 = new List[sequence.length];

        int Cnt = 0;
        int CntJob, h;
        int remove;
        int cmaxTemp = 0;
        int cmax = 0;
        cmax = Integer.MAX_VALUE;
        while (Cnt < t)
        {
            solution1 = list1DimArrayCopy(sequence);
            CntJob = 0;
            h = 0;
            while (CntJob < sequence[Cnt].size())
            {
                int bestIndex = 0;
                solution2 = list1DimArrayCopy(solution1);
//                remove = solution2[Cnt].remove(h);
//                remove = solution2[Cnt].remove(best_solution[Cnt].indexOf(best_solution[Cnt].get(h)));
                for (int k = 0; k < solution2[Cnt].size(); k++)
                {
                    if (k != h)
                    {
                        list1.clear();
                        Collections.swap(solution2[Cnt], k, best_solution[Cnt].indexOf(best_solution[Cnt].get(h)));
                        for (int l = 0; l < t; l++)
                        {
                            list1.addAll(solution2[pi_product.get(l) - 1]);
                        }
//                        schedule = Heuristic1.assignByNR1(p, list1, F);//NR1
                        schedule = Heuristic1.ecfRule(p, list1, F);
                        cmaxTemp = calAssembleCmax(schedule, p, F, pi_product, sequence, apt);
                        if (cmaxTemp < cmax)
                        {
                            cmax = cmaxTemp;
                            bestIndex = k;
                        }
                        Collections.swap(solution2[Cnt], k, best_solution[Cnt].indexOf(best_solution[Cnt].get(h)));
                    }
                }
                Collections.swap(solution2[Cnt], bestIndex, best_solution[Cnt].indexOf(best_solution[Cnt].get(h)));

                list2.clear();
                for (int i = 0; i < solution1.length; i++)
                {
                    list2.addAll(solution1[pi_product.get(i) - 1]);
                }
//                if (cmax < calAssembleCmax(Heuristic1.assignByNR1(p, list2, F), p, F, pi_product, sequence, apt))
                if (cmax < calAssembleCmax(Heuristic1.ecfRule(p, list2, F), p, F, pi_product, sequence, apt))
                {
                    solution1[Cnt].clear();
                    solution1[Cnt].addAll(solution2[Cnt]);
                    CntJob = 0;
                }
                else
                {
                    CntJob += 1;
                }
                h = h % solution1[Cnt].size();
            }
            list2.clear();
            list3.clear();
            for (int i = 0; i < t; i++)
            {
                list3.addAll(sequence[pi_product.get(i) - 1]);
                list2.addAll(solution1[pi_product.get(i) - 1]);
            }
//            if (calAssembleCmax(Heuristic1.assignByNR1(p, list2, F), p, F, pi_product, sequence, apt) < calAssembleCmax(Heuristic1.assignByNR1(p, list3, F), p, F, pi_product, sequence, apt))
            if (calAssembleCmax(Heuristic1.ecfRule(p, list2, F), p, F, pi_product, sequence, apt) < calAssembleCmax(Heuristic1.ecfRule(p, list3, F), p, F, pi_product, sequence, apt))
            {
                sequence = solution1;
                Cnt = 0;
            }
            else
            {
                Cnt += 1;
            }
        }
        return sequence;
    }

    private List<Integer> swapProductRfLS(List<Integer>[] sequence, List<Integer> pi_product1, int[][] p, List<Integer> pi_product, int t, int F, List<Integer> apt)
    {
        List<Integer>[] solution = new List[sequence.length];
        solution = list1DimArrayCopy(sequence);
        List<Integer> copyOfPiProduct = new ArrayList<>();
//        copyOfPiProduct = listCopy(pi_product);
        int cnt = 0;
        int j = 0;
        int removed;
        List<Integer> list = new ArrayList<>();
//        int temp = 0;
//        List<Integer> temp_solution = new ArrayList<>();
//        Map<List<Integer>, Integer> map = new HashMap<>();
        List<Integer>[] schedule = new List[F];
        int bestIndex = 0;
        int cmaxTemp = 0;
        List<Integer> list1 = new ArrayList<>();
        int cmax = 0;
        cmax = Integer.MAX_VALUE;
        while (cnt < t)
        {
            copyOfPiProduct = listCopy(pi_product);
            for (int k = 0; k < copyOfPiProduct.size(); k++)
            {
                if (k != j)
                {
                    list.clear();
                    list1.clear();
                    Collections.swap(copyOfPiProduct, k, copyOfPiProduct.indexOf(pi_product1.get(j)));
                    for (int i = 0; i < pi_product.size(); i++)
                    {
                        list.addAll(solution[copyOfPiProduct.get(i) - 1]);
                        list1.addAll(sequence[pi_product.get(i) - 1]);
                    }
                    schedule = Heuristic1.ecfRule(p, list, F);//NR1
                    cmaxTemp = calAssembleCmax(schedule, p, F, copyOfPiProduct, sequence, apt);
                    Collections.swap(copyOfPiProduct, k, pi_product.indexOf(pi_product1.get(j)));
                    if (cmaxTemp < cmax)
                    {
                        cmax = cmaxTemp;
                        bestIndex = k;
                    }
                }
            }
            Collections.swap(copyOfPiProduct, bestIndex, copyOfPiProduct.indexOf(pi_product1.get(j)));
            if (cmax < calAssembleCmax(Heuristic1.ecfRule(p, list1, F), p, F, pi_product, sequence, apt))
            {
                pi_product.clear();
                pi_product.addAll(copyOfPiProduct);
                cnt = 0;
            }
            else
            {
                cnt += 1;
            }
            j = (j + 1) % t;
        }
        return pi_product;
    }

    private List<Integer>[] insertJobRfLS(List<Integer>[] sequence, List<Integer>[] best_solution, int[][] p, List<Integer> pi_product, int t, int F, List<Integer> apt)
    {
        List<Integer>[] schedule = new List[F];
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3 = new ArrayList<>();
        List<Integer>[] solution1 = new List[sequence.length];
        List<Integer>[] solution2 = new List[sequence.length];

        int Cnt = 0;
        int CntJob, h;
        int remove;
        int cmaxTemp = 0;
        int cmax = 0;
        cmax = Integer.MAX_VALUE;
        while (Cnt < t)
        {
            solution1 = list1DimArrayCopy(sequence);
            CntJob = 0;
            h = 0;
            while (CntJob < sequence[Cnt].size())
            {
                int bestIndex = 0;
                solution2 = list1DimArrayCopy(solution1);
//                remove = solution2[Cnt].remove(h);
                remove = solution2[Cnt].remove(best_solution[Cnt].indexOf(best_solution[Cnt].get(h)));
                for (int k = 0; k < solution2[Cnt].size() + 1; k++)
                {
                    if (k != h)
                    {
                        list1.clear();
                        solution2[Cnt].add(k, remove);
                        for (int l = 0; l < t; l++)
                        {
                            list1.addAll(solution2[pi_product.get(l) - 1]);
                        }
//                        schedule = Heuristic1.assignByNR1(p, list1, F);//NR1
                        schedule = Heuristic1.ecfRule(p, list1, F);
                        cmaxTemp = calAssembleCmax(schedule, p, F, pi_product, sequence, apt);
                        if (cmaxTemp < cmax)
                        {
                            cmax = cmaxTemp;
                            bestIndex = k;
                        }
                        solution2[Cnt].remove(k);
                    }
                }
                solution2[Cnt].add(bestIndex, remove);
//                System.out.println(bestIndex);
//                System.out.println(Cnt);
                list2.clear();
                for (int i = 0; i < solution1.length; i++)
                {
                    list2.addAll(solution1[pi_product.get(i) - 1]);
                }
//                if (cmax < calAssembleCmax(Heuristic1.assignByNR1(p, list2, F), p, F, pi_product, sequence, apt))
                if (cmax < calAssembleCmax(Heuristic1.ecfRule(p, list2, F), p, F, pi_product, sequence, apt))
                {
                    solution1[Cnt].clear();
                    solution1[Cnt].addAll(solution2[Cnt]);
                    CntJob = 0;
                }
                else
                {
                    CntJob += 1;
                }
                h = h % solution1[Cnt].size();
            }
            list2.clear();
            list3.clear();
            for (int i = 0; i < t; i++)
            {
                list3.addAll(sequence[pi_product.get(i) - 1]);
                list2.addAll(solution1[pi_product.get(i) - 1]);
            }
//            if (calAssembleCmax(Heuristic1.assignByNR1(p, list2, F), p, F, pi_product, sequence, apt) < calAssembleCmax(Heuristic1.assignByNR1(p, list3, F), p, F, pi_product, sequence, apt))
            if (calAssembleCmax(Heuristic1.ecfRule(p, list2, F), p, F, pi_product, sequence, apt) < calAssembleCmax(Heuristic1.ecfRule(p, list3, F), p, F, pi_product, sequence, apt))
            {
                sequence = solution1;
                Cnt = 0;
            }
            else
            {
                Cnt += 1;
            }
        }
        return sequence;
    }

    private List<Integer> insertProductRfLS(List<Integer>[] sequence, List<Integer> pi_product1, int[][] p, List<Integer> pi_product, int t, int F, List<Integer> apt)
    {
        List<Integer>[] solution = new List[sequence.length];
        solution = list1DimArrayCopy(sequence);
        List<Integer> copyOfPiProduct = new ArrayList<>();
//        copyOfPiProduct = listCopy(pi_product);
        int cnt = 0;
        int j = 0;
        int removed;
        List<Integer> list = new ArrayList<>();
//        int temp = 0;
//        List<Integer> temp_solution = new ArrayList<>();
//        Map<List<Integer>, Integer> map = new HashMap<>();
        List<Integer>[] schedule = new List[F];
        int bestIndex = 0;
        int cmaxTemp = 0;
        List<Integer> list1 = new ArrayList<>();
        int cmax = 0;
        cmax = Integer.MAX_VALUE;
        while (cnt < t)
        {
            copyOfPiProduct = listCopy(pi_product);
            removed = copyOfPiProduct.remove(copyOfPiProduct.indexOf(pi_product1.get(j)));
            for (int k = 0; k < copyOfPiProduct.size() + 1; k++)
            {
                if (k != j)
                {
                    list.clear();
                    list1.clear();
                    copyOfPiProduct.add(k, removed);
                    for (int i = 0; i < pi_product.size(); i++)
                    {
                        list.addAll(solution[copyOfPiProduct.get(i) - 1]);
                        list1.addAll(sequence[pi_product.get(i) - 1]);
                    }
//                    schedule = Heuristic1.assignByNR1(p, list, F);//NR1
                    schedule = Heuristic1.ecfRule(p, list, F);
                    cmaxTemp = calAssembleCmax(schedule, p, F, copyOfPiProduct, sequence, apt);
                    copyOfPiProduct.remove(k);
                    if (cmaxTemp < cmax)
                    {
                        cmax = cmaxTemp;
                        bestIndex = k;
                    }
                }
            }
            copyOfPiProduct.add(bestIndex, removed);

//            if (cmax < calAssembleCmax(Heuristic1.assignByNR1(p, list1, F), p, F, pi_product, sequence, apt))
            if (cmax < calAssembleCmax(Heuristic1.ecfRule(p, list1, F), p, F, pi_product, sequence, apt))
            {
                pi_product.clear();
                pi_product.addAll(copyOfPiProduct);
                cnt = 0;
            }
            else
            {
                cnt += 1;
            }
            j = (j + 1) % t;
        }
        return pi_product;
    }

    private int sumOfPij(int[][] p)
    {
        int s = 0;
        for (int i = 0; i < p.length; i++)
        {
            for (int j = 0; j < p[0].length; j++)
            {
                s += p[i][j];
            }
        }
        return s;
    }

    private List<Integer>[] reconstructionJob(List<Integer> lambdaj, List<Integer> pi_product, List<Integer>[] solution1, List<Integer>[] sequence, List<Integer> apt, int[][] p, int F)
    {
        int cmaxTemp = 0;
        int cmax = 0;
        int index = 0;
        int r;
        int removed;
        List<Integer> list = new ArrayList<>();
        List<Integer>[] schedule = new List[F];
        while (lambdaj.size() > 0)
        {
            r = random.nextInt(lambdaj.size());
            removed = lambdaj.remove(r);
            for (int i = 0; i < sequence.length; i++)
            {
                if (sequence[i].contains(removed))
                {
                    cmax = Integer.MAX_VALUE;
                    for (int k = 0; k < solution1[i].size() + 1; k++)
                    {
                        solution1[i].add(k, removed);
                        list.clear();
                        for (int j = 0; j < solution1.length; j++)
                        {
                            list.addAll(solution1[pi_product.get(j) - 1]);
                        }
//                        schedule = Heuristic1.assignByNR1(p, list, F);//NR1
                        schedule = Heuristic1.ecfRule(p, list, F);
                        cmaxTemp = calAssembleCmax2(schedule, solution1, p, F, pi_product, sequence, apt);
                        solution1[i].remove(k);
                        if (cmaxTemp < cmax)
                        {
                            cmax = cmaxTemp;
                            index = k;
                        }
                    }
                    solution1[i].add(index, removed);
                    break;
                }
            }
        }
        return solution1;
    }

    private List<Integer> destructionJob(int dj, List<Integer> pi_product, List<Integer>[] solution1)
    {
        int r1;
        int r2;
        int count = 0;
        List<List<Integer>> list = new ArrayList<>();
//        List<Integer> list1 = new ArrayList<>();
//        for (int i = 0; i < pi_product.size(); i++)
//        {
//            list1.addAll(solution1[pi_product.get(i) - 1]);
//        }
        List<Integer> lambdaj = new ArrayList<>();
//        List<Integer> lambda = new ArrayList<>();
//        for (int i = 0; i < dj; i++)
        while (count < dj)
        {
            r1 = random.nextInt(pi_product.size());
            if (solution1[r1].size() > 0)
            {
                r2 = random.nextInt(solution1[r1].size());
                lambdaj.add(solution1[r1].remove(r2));
                count++;
            }//其中一个被取完,且未取够5个
        }
        list.add(lambdaj);
//        list.add(solution1);
        return lambdaj;

    }

    private List<Integer> reconstructionProduct(List<Integer> lambda, List<Integer> lambdap, List<Integer>[] sequence, List<Integer> apt, int[][] p, int F)
    {
        int r;
        int removed;
        int cmax;
//        List<Integer> copyLambdap = new ArrayList<>();
//        copyLambdap = listCopy(lambdap);
        List<Integer>[] schedule = new List[F];
        List<Integer> list1 = new ArrayList<>();
        int cmaxTemp = 0;
        int index = 0;
        while (lambdap.size() > 0)
        {
            cmax = Integer.MAX_VALUE;
            r = random.nextInt(lambdap.size());
            removed = lambdap.remove(r);
            for (int k = 0; k < lambda.size() + 1; k++)
            {
                list1.clear();
                lambda.add(k, removed);
                for (int i = 0; i < lambda.size(); i++)
                {
                    list1.addAll(sequence[lambda.get(i) - 1]);
                }
//                schedule = Heuristic1.assignByNR1(p, list1, F);//NR1
                schedule = Heuristic1.ecfRule(p, list1, F);
//                if (schedule == null)
//                {
//                    System.out.println();
//                }
                cmaxTemp = calAssembleCmax(schedule, p, F, lambda, sequence, apt);
                lambda.remove(k);
                if (cmaxTemp < cmax)
                {
                    cmax = cmaxTemp;
                    index = k;
                }
            }
            lambda.add(index, removed);
        }
        return lambda;
    }

    private List<List<Integer>> destructionProduct(int dp, List<Integer> pi_product)
    {
        int r;
        List<List<Integer>> list = new ArrayList<>();
        List<Integer> copyPiProduct = new ArrayList<>();
        copyPiProduct = listCopy(pi_product);
        List<Integer> lambdaP = new ArrayList<>();
        List<Integer> lambda = new ArrayList<>();
        while (lambdaP.size() < dp)
        {
            if (copyPiProduct.size() <= 0)
            {
                break;
            }
            else
            {
                r = random.nextInt(copyPiProduct.size());
                lambdaP.add(copyPiProduct.remove(r));
            }
        }
        list.add(copyPiProduct);
        list.add(lambdaP);
        return list;
    }

    private List<Integer>[] insertJobLS(List<Integer>[] sequence, int[][] p, List<Integer> pi_product, int t, int F, List<Integer> apt)
    {
//        int[] n_lambda = new int[t];
//        for (int i = 0; i < t; i++)
//        {
//            n_lambda[i] = list.get(i).size();
//        }
        List<Integer>[] schedule = new List[F];
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3 = new ArrayList<>();
        List<Integer>[] solution1 = new List[sequence.length];
        List<Integer>[] solution2 = new List[sequence.length];

        int Cnt = 0;
        int CntJob, h;
        int remove;
        int cmaxTemp = 0;
        int cmax = 0;
        cmax = Integer.MAX_VALUE;
        while (Cnt < t)
        {
            solution1 = list1DimArrayCopy(sequence);
            CntJob = 0;
            h = 0;
            while (CntJob < sequence[Cnt].size())
            {
                int bestIndex = 0;
                solution2 = list1DimArrayCopy(solution1);
                remove = solution2[Cnt].remove(h);
                for (int k = 0; k < solution2[Cnt].size() + 1; k++)
                {
                    if (k != h)
                    {
                        list1.clear();
                        solution2[Cnt].add(k, remove);
                        for (int l = 0; l < t; l++)
                        {
                            list1.addAll(solution2[pi_product.get(l) - 1]);
                        }
//                        schedule = Heuristic1.assignByNR1(p, list1, F);//NR1
                        schedule = Heuristic1.ecfRule(p, list1, F);
                        cmaxTemp = calAssembleCmax(schedule, p, F, pi_product, sequence, apt);
                        if (cmaxTemp < cmax)
                        {
                            cmax = cmaxTemp;
                            bestIndex = k;
                        }
                        solution2[Cnt].remove(k);
                    }
                }
                solution2[Cnt].add(bestIndex, remove);
//                System.out.println(bestIndex);
//                System.out.println(Cnt);
                list2.clear();
                for (int i = 0; i < solution1.length; i++)
                {
                    list2.addAll(solution1[pi_product.get(i) - 1]);
                }
//                if (cmax < calAssembleCmax(Heuristic1.assignByNR1(p, list2, F), p, F, pi_product, sequence, apt))
                if (cmax < calAssembleCmax(Heuristic1.ecfRule(p, list2, F), p, F, pi_product, sequence, apt))
                {
                    solution1[Cnt].clear();
                    solution1[Cnt].addAll(solution2[Cnt]);
                    CntJob = 0;
                }
                else
                {
                    CntJob += 1;
                }
                h = h % solution1[Cnt].size();
            }
            list2.clear();
            list3.clear();
            for (int i = 0; i < t; i++)
            {
                list3.addAll(sequence[pi_product.get(i) - 1]);
                list2.addAll(solution1[pi_product.get(i) - 1]);
            }
//            if (calAssembleCmax(Heuristic1.assignByNR1(p, list2, F), p, F, pi_product, sequence, apt) < calAssembleCmax(Heuristic1.assignByNR1(p, list3, F), p, F, pi_product, sequence, apt))
            if (calAssembleCmax(Heuristic1.ecfRule(p, list2, F), p, F, pi_product, sequence, apt) < calAssembleCmax(Heuristic1.ecfRule(p, list3, F), p, F, pi_product, sequence, apt))
            {
                sequence = solution1;
                Cnt = 0;
            }
            else
            {
                Cnt += 1;
            }
        }
        return sequence;

    }

    private List<Integer> insertProductLS(List<Integer>[] sequence, int[][] p, List<Integer> pi_product, int t, int F, List<Integer> apt)
    {
        List<Integer>[] solution = new List[sequence.length];
        solution = list1DimArrayCopy(sequence);
        List<Integer> copyOfPiProduct = new ArrayList<>();
//        copyOfPiProduct = listCopy(pi_product);
        int cnt = 0;
        int j = 0;
        int removed;
        List<Integer> list = new ArrayList<>();
//        int temp = 0;
//        List<Integer> temp_solution = new ArrayList<>();
//        Map<List<Integer>, Integer> map = new HashMap<>();
        List<Integer>[] schedule = new List[F];
        int bestIndex = 0;
        int cmaxTemp = 0;
        List<Integer> list1 = new ArrayList<>();
        int cmax = 0;
        cmax = Integer.MAX_VALUE;
        while (cnt < t)
        {
            copyOfPiProduct = listCopy(pi_product);
            removed = copyOfPiProduct.remove(j);
            for (int k = 0; k < copyOfPiProduct.size() + 1; k++)
            {
                if (k != j)
                {
                    list.clear();
                    list1.clear();
                    copyOfPiProduct.add(k, removed);
                    for (int i = 0; i < pi_product.size(); i++)
                    {
                        list.addAll(solution[copyOfPiProduct.get(i) - 1]);
                        list1.addAll(sequence[pi_product.get(i) - 1]);
                    }
//                    schedule = Heuristic1.assignByNR1(p, list, F);//NR1
                    schedule = Heuristic1.ecfRule(p, list, F);
                    cmaxTemp = calAssembleCmax(schedule, p, F, copyOfPiProduct, sequence, apt);
                    copyOfPiProduct.remove(k);
                    if (cmaxTemp < cmax)
                    {
                        cmax = cmaxTemp;
                        bestIndex = k;
                    }
                }
            }
            copyOfPiProduct.add(bestIndex, removed);

//            if (cmax < calAssembleCmax(Heuristic1.assignByNR1(p, list1, F), p, F, pi_product, sequence, apt))
            if (cmax < calAssembleCmax(Heuristic1.ecfRule(p, list1, F), p, F, pi_product, sequence, apt))
            {
                pi_product.clear();
                pi_product.addAll(copyOfPiProduct);
                cnt = 0;
            }
            else
            {
                cnt += 1;
            }
            j = (j + 1) % t;
        }
        return pi_product;
    }

    private int calAssembleCmax2(List<Integer>[] schedule, List<Integer>[] solution1, int[][] p, int F, List<Integer> pi_product, List<Integer>[] sequence, List<Integer> apt)
    {
        //分配工厂
        int[][] newp;
        int Cmax = 0;
//        List<Integer>[][] assignment = new List[1][F];
//        assignment[0] = Heuristic1.ecfRule(p, piT, F);//NR2
//        assignment[0] = Heuristic1.assignByNR1(p, piT, F);//NR1
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        List<Integer>[] lists = new List[F];
        for (int j = 0; j < F; j++)
        {
            newp = getNewp(p, schedule[j]);
            lists[j] = ccp.forwardMethod3(newp);
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < schedule.length; i++)
        {
            for (int j = 0; j < schedule[i].size(); j++)
            {
                map.put(schedule[i].get(j), lists[i].get(j));
            }
        }
        //计算装配完成时间
        int[] Cmax_Assemble = new int[pi_product.size()];
        int temp;
        for (int i = 0; i < pi_product.size(); i++)
//        for (int i = 0; i < assignment.length; i++)
        {
            temp = Integer.MIN_VALUE;
            for (int j = 0; j < solution1[pi_product.get(i) - 1].size(); j++)
            {
                temp = Math.max(temp, map.get(solution1[pi_product.get(i) - 1].get(j)));
            }
            Cmax_Assemble[i] = temp;
        }
//        Cmax = Cmax_Assemble[pi_product.get(0) - 1] + apt.get(pi_product.get(0) - 1);
        Cmax = Cmax_Assemble[0] + apt.get(pi_product.get(0) - 1);
        for (int i = 1; i < Cmax_Assemble.length; i++)
        {
//            if (Cmax > Cmax_Assemble[pi_product.get(i) - 1])
            if (Cmax > Cmax_Assemble[i])
            {
                Cmax += apt.get(pi_product.get(i) - 1);
            }
            else
            {
                Cmax = Cmax_Assemble[i] + apt.get(pi_product.get(i) - 1);
            }
        }
//        System.out.println("Cmax = " + Cmax);
        return Cmax;
    }

    public int calAssembleCmax(List<Integer>[] schedule, int[][] p, int F, List<Integer> pi_product, List<Integer>[] sequence, List<Integer> apt)
    {
        //分配工厂
        int[][] newp;
        int Cmax = 0;
//        List<Integer>[][] assignment = new List[1][F];
//        assignment[0] = Heuristic1.ecfRule(p, piT, F);//NR2
//        assignment[0] = Heuristic1.assignByNR1(p, piT, F);//NR1
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        List<Integer>[] lists = new List[F];
        for (int j = 0; j < F; j++)
        {
            if (schedule[j].size() == 0)
            {
                break;
            }
            else
            {
                newp = getNewp(p, schedule[j]);
            }
//            if (newp.length == 0 || newp[0].length == 0)
//            {
//                System.out.println();
//            }
            lists[j] = ccp.forwardMethod3(newp);
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < schedule.length; i++)
        {
            for (int j = 0; j < schedule[i].size(); j++)
            {
                map.put(schedule[i].get(j), lists[i].get(j));
            }
        }
        //计算装配完成时间
        int[] Cmax_Assemble = new int[pi_product.size()];
        int temp;
        for (int i = 0; i < pi_product.size(); i++)
//        for (int i = 0; i < assignment.length; i++)
        {
            temp = Integer.MIN_VALUE;
            for (int j = 0; j < sequence[pi_product.get(i) - 1].size(); j++)
            {
                temp = Math.max(temp, map.get(sequence[pi_product.get(i) - 1].get(j)));
            }
            Cmax_Assemble[i] = temp;
        }
//        Cmax = Cmax_Assemble[pi_product.get(0) - 1] + apt.get(pi_product.get(0) - 1);
        Cmax = Cmax_Assemble[0] + apt.get(pi_product.get(0) - 1);
        for (int i = 1; i < Cmax_Assemble.length; i++)
        {
//            if (Cmax > Cmax_Assemble[pi_product.get(i) - 1])
            if (Cmax > Cmax_Assemble[i])
            {
                Cmax += apt.get(pi_product.get(i) - 1);
            }
            else
            {
                Cmax = Cmax_Assemble[i] + apt.get(pi_product.get(i) - 1);
            }
        }
//        System.out.println("Cmax = " + Cmax);
        return Cmax;
    }

    private List<Integer>[] list1DimArrayCopy(List<Integer>[] solution)//一维List类型数组的拷贝
    {
        List<Integer>[] lists = new List[solution.length];
        for (int i = 0; i < solution.length; i++)
        {
            lists[i] = new ArrayList<>();
            lists[i].addAll(solution[i]);
        }
        return lists;
    }

    private List<Integer> listCopy(List<Integer> list)
    {
        List<Integer> copy = new ArrayList<>();
        copy.addAll(list);
        return copy;
    }

    private int[][] getNewp(int[][] p, List<Integer> pi)
    {
        int[][] newp = new int[pi.size()][];
        for (int i = 0; i < pi.size(); i++)
        {
            newp[i] = p[pi.get(i) - 1];
        }
        return newp;
    }

    private List<Integer>[][] list2DimArrayCopy(List<Integer>[][] solution)//二维List类型数组的拷贝
    {
        List<Integer>[][] lists = new List[solution.length][solution[0].length];
        for (int i = 0; i < solution.length; i++)
        {
            for (int j = 0; j < solution[0].length; j++)
            {
                lists[i][j] = new ArrayList<>();
                lists[i][j].addAll(solution[i][j]);
            }
        }
        return lists;
    }
}

启发式H11

package practice.dapfsp;

import practice.calculateCMAX.CalculateCmaxPFSP;

import java.util.*;

/**
 * @Date: 2022/3/30 20:58
 * @Author: 
 * @File: Heuristic1.class
 * @Software: IDEA
 * @Email: 1532116227@qq.com
 */
public class Heuristic1
{
    public List<Integer>[] algorithm(int[][] p, int t, List<Integer> apt, List<Integer>[] sequence, List<Integer> pi_product)
    {
//        List<Integer> pi_product;
        List<Integer> piT = new ArrayList<>();
//        pi_product = ruleOfSPT(apt);
        List<Integer>[] parSeq = new List[t];
        for (int i = 0; i < t; i++)
        {
            parSeq[i] = new ArrayList<>();
        }
        for (int i = 0; i < t; i++)
        {
//            parSeq[i] = heuristicFL(p, sequence[pi_product.get(i) - 1]);
            parSeq[i] = heuristicFL(p, sequence[i]);
        }
//        for (int i = 0; i < t; i++)
//        {
//            piT.addAll(parSeq[i]);
//        }

        return parSeq;
    }

    private List<Integer> heuristicFL(int[][] p, List<Integer> sequence)
    {
        int[] TPj;
        int[][] newp;
        List<Integer> R = new ArrayList<>();
        List<Integer> S = new ArrayList<>();
        List<Integer> copySeq = listCopy(sequence);
//        R = listCopy(copySeq);
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        newp = getNewp(p, sequence);
        TPj = getTP(newp);
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < sequence.size(); i++)
        {
            map.put(copySeq.get(i), TPj[i]);
        }
        List<Map.Entry<Integer, Integer>> lst = new ArrayList<>(map.entrySet());
        Collections.sort(lst, (o1, o2) -> o1.getValue() - o2.getValue());
        for (Map.Entry<Integer, Integer> entry : lst)
        {
            R.add(entry.getKey());
        }
        int[][] p1 = {p[R.get(0) - 1], p[R.get(1) - 1]};
        int[][] p2 = {p[R.get(1) - 1], p[R.get(0) - 1]};
        if (ccp.forwardMethod(p1) < ccp.forwardMethod(p2))
        {
            S.add(R.get(0));
            S.add(R.get(1));
        }
        else
        {
            S.add(R.get(1));
            S.add(R.get(0));
        }
        for (int i = 2; i < R.size(); i++)
        {
            S = insertAndEvaluation(p, S, R.get(i));
        }
        //两两交换位置
        List<Integer> copyS = new ArrayList<>();
        copyS = listCopy(S);
        int[][] newp1;
        int[][] newp2;
        newp2 = getNewp(p, S);
        Map<List<Integer>, Integer> map1 = new HashMap<>();
        for (int i = 0; i < copyS.size() - 1; i++)
        {
            for (int j = i + 1; j < copyS.size(); j++)
            {
                Collections.swap(copyS, i, j);
                newp1 = getNewp(p, copyS);
                map1.put(listCopy(copyS), ccp.forwardMethod(newp1));
                Collections.swap(copyS, i, j);
            }
        }
        List<Map.Entry<List<Integer>, Integer>> lst1 = new ArrayList<>(map1.entrySet());
        Collections.sort(lst1, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));//升序
        for (Map.Entry<List<Integer>, Integer> entry : lst1)
        {
            if (entry.getValue() < ccp.forwardMethod(newp2))
            {
                copyS.clear();
                copyS.addAll(entry.getKey());
            }
            break;
        }

        return copyS;
    }

    private static List<Integer> insertAndEvaluation(int[][] p, List<Integer> pi, int index)
    {
        int[] cmax = new int[pi.size() + 1];
        int[][] pp = new int[pi.size() + 1][];
//        List<Integer> list = new ArrayList<>();
        //插入得到所有的组合
        for (int i = 0; i <= pi.size(); i++)
        {
            pi.add(i, index);
            for (int j = 0; j < pi.size(); j++)
            {
                pp[j] = p[pi.get(j) - 1];
            }
//            ForwardCalculation fc = new ForwardCalculation();//前向计算
//            List<int[][]> listIE = fc.forwardMethod(pp);//前向计算
//            cmax[i] = getCmax(listIE);
            CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
            cmax[i] = ccp.forwardMethod(pp);
            pi.remove(i);

        }
        int elementIndex = minElementIndex(cmax);
        if (p.length == pi.size() + 1)
        {
            System.out.println("Cmax = " + cmax[elementIndex]);

        }
        pi.add(elementIndex, index);

        return pi;
    }

    private static int minElementIndex(int[] array)
    {
        int min = array[0];
        int index = 0;
        for (int i = 1; i < array.length; i++)
        {
            if (array[i] < min)
            {
                min = array[i];
                index = i;
            }
        }
        return index;
    }

    private List<Integer> listCopy(List<Integer> list)
    {
        List<Integer> copy = new ArrayList<>();
        copy.addAll(list);
        return copy;
    }

    private static int[][] getNewp(int[][] p, List<Integer> pi)
    {
        int[][] newp = new int[pi.size()][];
        for (int i = 0; i < pi.size(); i++)
        {
            newp[i] = p[pi.get(i) - 1];
        }
        return newp;
    }

    private static int[] getTP(int[][] p)
    {
        int[] TP = new int[p.length];
        for (int i = 0; i < p.length; i++)
        {
            for (int j = 0; j < p[0].length; j++)
            {
                TP[i] = TP[i] + p[i][j];
            }
        }
        return TP;
    }

    public static List<Integer> ruleOfSPT(List<Integer> apt)
    {
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> pi = new ArrayList<>();
        for (int i = 0; i < apt.size(); i++)
        {
            map.put(i + 1, apt.get(i));
        }
        List<Map.Entry<Integer, Integer>> lst = new ArrayList<>(map.entrySet());
        Collections.sort(lst, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));//升序
//        Collections.sort(list, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));//降序
        for (Map.Entry<Integer, Integer> entry : lst)
        {
            pi.add(entry.getKey());
        }
        return pi;
    }

    public static List<Integer>[] assignByNR1(int[][] p, List<Integer> pi, int F)//NR1
    {
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        int[][] newp;
        int Cmax;
        int Cmax_temp;
        int Factory_Index = 0;
        List<Integer>[] Lambda = new List[F];//List类型的数组

        for (int i = 0; i < F; i++)
        {
            Lambda[i] = new ArrayList<>();
        }
        for (int k = 0; k < F; k++)
        {
            if (k > pi.size() - 1)
            {
                break;
            }
            else
            {
                Lambda[k].add(pi.get(k));
            }
        }

        for (int k = F; k < pi.size(); k++)
        {
            Cmax = Integer.MAX_VALUE;
            for (int i = 0; i < F; i++)
            {
                newp = getNewp(p, Lambda[i]);
                Cmax_temp = ccp.forwardMethod(newp);
                if (Cmax_temp < Cmax)
                {
                    Cmax = Cmax_temp;
                    Factory_Index = i;
                }
            }
            Lambda[Factory_Index].add(pi.get(k));
        }
        return Lambda;
    }

    public static List<Integer>[] ecfRule(int[][] p, List<Integer> pi, int F)//NR2
    {
//        int[][] p = {{1, 4}, {86, 21}, {28, 67}, {32, 17}};
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
//        List<Integer> pi = List.of(1, 2, 3, 4);
//        int F = 2;
        int[][] newp;
        int Cmax;
        int Cmax_temp;
        int Factory_Index = 0;
        List<Integer>[] Lambda = new List[F];//List类型的数组

        for (int i = 0; i < F; i++)
        {
            Lambda[i] = new ArrayList<>();
        }
        for (int k = 0; k < F; k++)
        {
            if (k > pi.size() - 1)
            {
                break;
            }
            else
            {
                Lambda[k].add(pi.get(k));
            }
        }

        for (int k = F; k < p.length; k++)
        {
            if (k > pi.size() - 1)
            {
                break;
            }
            else
            {
                Cmax = Integer.MAX_VALUE;
                for (int i = 0; i < F; i++)
                {
                    Lambda[i].add(pi.get(k));
                    newp = getNewp(p, Lambda[i]);
                    Cmax_temp = ccp.forwardMethod(newp);
                    if (Cmax_temp < Cmax)
                    {
                        Cmax = Cmax_temp;
                        Factory_Index = i;
                    }
                    Lambda[i].remove(pi.get(k));
                }
                Lambda[Factory_Index].add(pi.get(k));
            }

        }
//        for (int i = 0; i < F; i++)
//        {
//            System.out.println(Lambda[i]);
//        }
        return Lambda;
    }
}

计算Cmax

package practice.calculateCMAX;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CalculateCmaxPFSP
{
    //    @Test
    public int forwardMethod(int[][] p)
    {
//        List<int[][]> list = new ArrayList<>();

        int[][] s = new int[p.length][p[0].length];
        int[][] c = new int[p.length][p[0].length + 1];

        c[0][0] = 0;
        for (int i = 0; i < p[0].length; i++)
        {
            s[0][i] = c[0][i];
            c[0][i + 1] = s[0][i] + p[0][i];
        }
        for (int j = 1; j < p.length; j++)
        {
            c[j][0] = c[j - 1][1];
            for (int i = 0; i < p[0].length; i++)
            {
                s[j][i] = Math.max(c[j][i], c[j - 1][i + 1]);
                c[j][i + 1] = s[j][i] + p[j][i];
            }
        }
//        list.add(s);
//        list.add(c);
//        return list;

        return c[c.length - 1][c[0].length - 1];
    }

    //    @Test
    public List<int[][]> forwardMethod2(int[][] p)
    {
        List<int[][]> list = new ArrayList<>();

        int[][] s = new int[p.length][p[0].length];
        int[][] c = new int[p.length][p[0].length + 1];

        c[0][0] = 0;
        for (int i = 0; i < p[0].length; i++)
        {
            s[0][i] = c[0][i];
            c[0][i + 1] = s[0][i] + p[0][i];
        }
        for (int j = 1; j < p.length; j++)
        {
            c[j][0] = c[j - 1][1];
            for (int i = 0; i < p[0].length; i++)
            {
                s[j][i] = Math.max(c[j][i], c[j - 1][i + 1]);
                c[j][i + 1] = s[j][i] + p[j][i];
            }
        }
        list.add(s);
        list.add(c);
        return list;

//        return c[c.length - 1][c[0].length - 1];
    }

    public List<Integer> forwardMethod3(int[][] p)//带装配
    {
        List<Integer> list = new ArrayList<>();

        int[][] s = new int[p.length][p[0].length];
        int[][] c = new int[p.length][p[0].length + 1];

        c[0][0] = 0;
        for (int i = 0; i < p[0].length; i++)
        {
            s[0][i] = c[0][i];
            c[0][i + 1] = s[0][i] + p[0][i];
        }
        for (int j = 1; j < p.length; j++)
        {
            c[j][0] = c[j - 1][1];
            for (int i = 0; i < p[0].length; i++)
            {
                s[j][i] = Math.max(c[j][i], c[j - 1][i + 1]);
                c[j][i + 1] = s[j][i] + p[j][i];
            }
        }
        for (int i = 0; i < p.length; i++)
        {
            list.add(c[i][c[0].length - 1]);
        }
//        list.add(s);
//        list.add(c);
        return list;

//        return c[c.length - 1][c[0].length - 1];
    }

    //    @Test
    public int reverseMethod(int[][] p)
    {
        List<int[][]> list = new ArrayList<>();

        int[][] ss = new int[p.length][p[0].length + 1];
        int[][] cc = new int[p.length][p[0].length];

        ss[p.length - 1][p[0].length] = 0;
        for (int i = p[0].length; i > 0; i--)
        {
            cc[p.length - 1][i - 1] = ss[p.length - 1][i];
            ss[p.length - 1][i - 1] = cc[p.length - 1][i - 1] + p[p.length - 1][i - 1];
        }
        for (int j = p.length; j > 1; j--)
        {
            ss[j - 2][p[0].length] = ss[j - 1][p[0].length - 1];
            for (int i = p[0].length; i > 0; i--)
            {
                cc[j - 2][i - 1] = Math.max(ss[j - 2][i], ss[j - 1][i - 1]);
                ss[j - 2][i - 1] = cc[j - 2][i - 1] + p[j - 2][i - 1];
            }
        }
//        list.add(ss);
//        list.add(cc);
//        return list;
        return ss[0][0];
    }

    //    @Test
    public List<int[][]> reverseMethod2(int[][] p)
    {
        List<int[][]> list = new ArrayList<>();

        int[][] ss = new int[p.length][p[0].length + 1];
        int[][] cc = new int[p.length][p[0].length];

        ss[p.length - 1][p[0].length] = 0;
        for (int i = p[0].length; i > 0; i--)
        {
            cc[p.length - 1][i - 1] = ss[p.length - 1][i];
            ss[p.length - 1][i - 1] = cc[p.length - 1][i - 1] + p[p.length - 1][i - 1];
        }
        for (int j = p.length; j > 1; j--)
        {
            ss[j - 2][p[0].length] = ss[j - 1][p[0].length - 1];
            for (int i = p[0].length; i > 0; i--)
            {
                cc[j - 2][i - 1] = Math.max(ss[j - 2][i], ss[j - 1][i - 1]);
                ss[j - 2][i - 1] = cc[j - 2][i - 1] + p[j - 2][i - 1];
            }
        }
        list.add(ss);
        list.add(cc);
        return list;
//        return ss[0][0];
    }
}

读取测试集文件

package practice.comparison;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * @Date: 2022/4/8 16:24
 * @Author: 
 * @File: ReadTxt.class
 * @Software: IDEA
 * @Email: 1532116227@qq.com
 */
public class ReadTxt
{
    public int[][] getProcessingTimes(String filePath, int n, int m)
    {
        File f = new File(filePath);
        if (!f.exists())
        {
            System.out.println("找不到文件");
            return null;
        }
        int[][] p = new int[n][m];
        int[] a = null;
        String[] values;
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(f));
            Scanner sc = new Scanner(br);
            for (int i = 0; i < n; i++)
            {
                values = sc.nextLine().split(",");
                for (int j = 0; j < m; j++)
                {
                    p[i][j] = Integer.parseInt(values[j]);
                }
            }
            br.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return p;
    }

    public List<Integer> getAssemblyTimes(String filePath, int l)
    {
        List<Integer> apt = new ArrayList<>();
        File f = new File(filePath);
        if (!f.exists())
        {
            System.out.println("找不到文件");
            return null;
        }
        int[] pp = new int[l];
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(f));
            Scanner sc = new Scanner(br);
            for (int i = 0; i < l; i++)
            {
                String[] values = sc.nextLine().split(",");
                pp[i] = Integer.parseInt(values[1]);
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        for (int i = 0; i < l; i++)
        {
            apt.add(pp[i]);
        }
        return apt;
    }

    public int[] getAssignSet(String filePath, int n, int l)
    {
        File f = new File(filePath);
        if (!f.exists())
        {
            System.out.println("找不到文件");
            return null;
        }
        int[] as = new int[n];
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(f));
            Scanner sc = new Scanner(br);
            for (int i = 0; i < n; i++)
            {
                String[] values = sc.nextLine().split(",");
                as[i] = Integer.parseInt(values[1]);
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return as;
    }
}

结果

测试集

在这里插入图片描述

参考文献

Y. Z. Li, Q. K. Pan, R. Ruiz, and H. Y. Sang, “A referenced iterated greedy algorithm for the distributed assembly mixed no-idle permutation flowshop scheduling problem with the total tardiness criterion,” Knowledge-Based Systems, vol. 239, Mar. 2022, doi: 10.1016/j.knosys.2021.108036.

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值