复现一篇分布式置换流水车间调度问题的代码——分布估计算法

复现一篇分布式置换流水车间调度问题的代码——分布估计算法

摘要

In this paper, an effective estimation of distribution algorithm (EDA) is proposed to solve the distributed permutation flow-shop scheduling problem (DPFSP). First, the earliest completion factory rule is employed for the permutation based encoding to generate feasible schedules and calculate the schedule objective value. Then, a probability model is built for describing the probability distribution of the solution space, and a mechanism is provided to update the probability model with superior individuals. By sampling the probability model, new individuals can be generated among the promising search region. Moreover, to enhance the local exploitation, some local search operators are designed based on the problem characteristics and utilized for the promising individuals. In addition, the influence of parameter setting of the EDA is investigated based on the Taguchi method of design of experiments, and a suitable parameter setting is suggested. Finally, numerical simulations based on 420 small-sized instances and 720 large-sized instances are carried out. The comparative results with some existing algorithms demonstrate the effectiveness of the proposed EDA in solving the DPFSP. In addition, the new best-known solutions for 17 out of 420 small instances and 589 out of 720 large instances are found.

说明

这篇文章是解决分布式置换流水车间调度问题的经典文章。整体上没得说,期刊也是好期刊,但是经过我的仔细研究和复现代码,还是有些不足的地方,因为作者也没提供代码,不能准确复现,我最后的结果与作者有偏差,请大家自行判断。这篇文章中好多东西的描述不是很清楚,比如概率模型的更新和关于编解码规则的说明,还有其他小瑕疵,本篇博客仅供大家参考,欢迎各位评论交流。关于车间调度的问题,大家可参考
请添加图片描述
(附言,不是代做广告,只是我自己做这个方向,有兴趣的请自行参考;另外,这本书中也有很多谬误,请大家自行批判阅读)另外,摘要就不翻译了哈,比较简单。出于工作需求,本人自学Java2个月,实属小白一枚,请各位大佬批评指正,这样有助于我自己工作的推进。测试类中注释部分可用,前面的是一个小例子,后面的是测试集。由于本人能力有限,写代码过程中可能出现未预料到的问题,由于各种因素的存在,复现一篇文章实属不易,懂的人都懂。代码没有放到GitHub上是因为嫌麻烦(请见谅),最后,再次诚请各位批评指正,有需要的私聊,大家沟通交流,共同进步。

代码

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

测试类

package practice.demo19;

import practice.calculateCMAX.CalculateCmaxPFSP;

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

/**
 * @date: 2022/3/15 9:27
 * @author: 
 * @file: TEST_EDA_DPFSP.class
 * @software: IDEA
 */
public class TEST_EDA_DPFSP
{
    public static void main(String[] args)
    {
//        int[][] p = {{1, 4}, {86, 21}, {28, 67}, {32, 17}};
        int[][] p = {{4, 1, 3}, {2, 3, 3}, {2, 2, 3}, {2, 1, 3}, {4, 3, 2}};
//        int[][] p = {{77, 2, 74}, {62, 6, 66}, {36, 11, 53}, {32, 94, 27}, {60, 47, 16}, {43, 19, 22}, {92, 59, 15}, {58, 59, 6}, {68, 76, 62}, {61, 66, 66}};//3_10_3_1
//        int[][] p = {{87, 9, 69, 7, 27}, {33, 41, 45, 68, 9}, {11, 47, 14, 87, 74}, {62, 97, 4, 16, 89}, {46, 39, 46, 17, 45}, {25, 74, 57, 50, 85}, {37, 49, 48, 82, 19}, {49, 44, 36, 41, 33}
//                , {27, 17, 7, 83, 47}, {25, 16, 44, 21, 21}, {94, 44, 10, 71, 16}, {40, 72, 4, 83, 31}, {78, 7, 15, 33, 22}, {17, 56, 23, 89, 56}, {97, 86, 52, 42, 86}, {40, 1, 69, 42, 1}};//2_16_5_1

        //==============================================================================================================
//        List<Integer> pi = new ArrayList<>();
//        List<Integer>[] assignment;
//        int maxValue;
//        int[][] newp;
//        int F = 2;
//        int cmax_temp;
//        int index;
//        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
//        EDA_DPFSP eda_dpfsp = new EDA_DPFSP();
//        //==============================================================================================================
        int[] factory = {2};
//        int[] job = {4, 6, 8, 10, 12, 14, 16};
        int[] job = {16};
        int[] job = {6};
//        int[] mechine = {2, 3, 4, 5};
        int[] mechine = {2};
        int[] mechine = {5};
//        int[] num = {1, 2, 3, 4, 5};
        int[] num = {1};
        int[][] co/st = new int[job.length * mechine.length * num.length][1];
//        int[][] p;
//        int count = 1;
//        ReadTxt readTxt = new ReadTxt();
//        for (int n = 0; n < job.length; n++)//job.length - 1
//        {
//            for (int m = 0; m < mechine.length; m++)//mechine.length - 1
//            {
//                for (int k = 0; k < num.length; k++)//num.length - 1
//                {
//                    p = readTxt.readDPFSPBenchmarkTxt("D:\\迅雷下载\\DPFSP\\DPFSP_Small\\2\\I" + "_" +
//                            F + "_" + job[n] + "_" + mechine[m] + "_" + num[k] + ".txt", mechine[m], job[n]);
//                    System.out.println(count++);
//                    System.out.println("D:\\迅雷下载\\DPFSP\\DPFSP_Small\\2\\I" + "_" + F + "_" + job[n] + "_" + mechine[m] + "_" + num[k] + ".txt");
//                    for (int i = 0; i < p.length; i++)
//                    {
//                        pi.add(i + 1);
//                    }
//                    pi = eda_dpfsp.algorithm(p, pi, F);
//                    System.out.println(pi);
//                    assignment = ecfRule(p, pi, F);//解码
//                    maxValue = Integer.MIN_VALUE;
//                    for (int j = 0; j < F; j++)
//                    {
//                        newp = getNewp(p, assignment[j]);
//                        cmax_temp = ccp.forwardMethod(newp);
//                        if (cmax_temp > maxValue)
//                        {
//                            maxValue = cmax_temp;
//                            index = j;
//                        }
//                    }
//                    System.out.println("Cmax = " + maxValue);
//                    System.out.println("============================================");
//                    pi.clear();//重置排列
//                }
//            }
//        }
        //==============================================================================================================

        List<Integer> pi = new ArrayList<>();
        List<Integer>[] assignment;
        int maxValue;
        int[][] newp;
        int F = 3;
        int cmax_temp;
        int index;
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        for (int i = 0; i < p.length; i++)
        {
            pi.add(i + 1);
        }
        EDA_DPFSP eda_dpfsp = new EDA_DPFSP();
        pi = eda_dpfsp.algorithm(p, pi, F);
        System.out.println(pi);
        assignment = ecfRule(p, pi, F);//解码
        //==============================================================================================================
        System.out.println("===================================================");
        for (int i = 0; i < assignment.length; i++)
        {
            System.out.println(assignment[i]);
        }
        //==============================================================================================================
        maxValue = Integer.MIN_VALUE;
        for (int j = 0; j < F; j++)
        {
            newp = getNewp(p, assignment[j]);
            cmax_temp = ccp.forwardMethod(newp);
            if (cmax_temp > maxValue)
            {
                maxValue = cmax_temp;
                index = j;
            }
        }
        System.out.println("Cmax = " + maxValue);

    }

    public static List<Integer>[] ecfRule(int[][] p, List<Integer> pi, int F)
    {
//        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++)
        {
            Lambda[k].add(pi.get(k));
        }

        for (int k = F; k < p.length; k++)
        {
            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;
    }

    public 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;
    }

}

算法主体

package practice.demo19;

import org.junit.Test;
import practice.calculateCMAX.CalculateCmaxPFSP;

import java.util.*;

//import org.ujmp.commonsmath.*;

/**
 * @date: 2022/3/15 9:26
 * @author: 
 * @file: EDA_DPFSP.class
 * @software: IDEA
 */
public class EDA_DPFSP
{
    Random random = new Random();

    public List<Integer> algorithm(int[][] p, List<Integer> pi, int F)
    {
        //参数设置
        int P_Size = 150;
        double nta = 10;
        double alpha = 0.1;
        int Gen = 1000;
        int gen = 0;
//        List<Integer> pi = new ArrayList<>();

        //初始化概率矩阵
        double[][] p_Matrix = new double[p.length][p.length];
        double[][] p_Matrix_copy;
        //产生种群
        double sum;
        double m;
        double r;
//        int F = 2;
        List<Integer>[] Population = new List[P_Size];
        for (int i = 0; i < P_Size; i++)
        {
            Population[i] = new ArrayList<>();
        }
        for (int h = 0; h < P_Size; h++)//产生P_Size个个体
        {
            for (int i = 0; i < p.length; i++)
            {
                for (int j = 0; j < p.length; j++)
                {
                    p_Matrix[i][j] = 1.0 / p.length;
                }
            }
            //拷贝
            p_Matrix_copy = getCopyOfTwoDimensionArray(p_Matrix);
            for (int i = 0; i < p.length; i++)
            {
                r = random.nextDouble();
                m = 0.0;
                for (int j = 0; j < p.length; j++)
                {
                    m += p_Matrix_copy[i][j];
                    if (r < m)
                    {
                        Population[h].add(pi.get(j));
                        //清零
                        for (int k = 0; k < p.length; k++)
                        {
                            p_Matrix_copy[k][j] = 0;
                        }
                        //归一化
                        for (int k = 0; k < p.length; k++)
                        {
                            sum = 0.0;
                            for (int l = 0; l < p.length; l++)
                            {
                                sum += p_Matrix_copy[k][l];
                            }
                            for (int l = 0; l < p.length; l++)
                            {
                                p_Matrix_copy[k][l] = p_Matrix_copy[k][l] / sum;
                            }
                        }
                        break;
                    }
                }
            }
        }
        int index_best = 0;
        while (gen < Gen)
        {
            //计算Cmax
            CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
            List<Integer>[][] assignment = new List[P_Size][F];
//        int[][] newp1;
//        int[][] newp2;
//        int cmax1;
//        int cmax2;
            int[][] newp;
            int cmax_temp;
            int maxValue;
            int index;
            int[] collection_cmax = new int[P_Size];
            for (int i = 0; i < P_Size; i++)
            {
                maxValue = Integer.MIN_VALUE;
                assignment[i] = ecfRule(p, Population[i], F);
                for (int j = 0; j < F; j++)
                {
                    newp = getNewp(p, assignment[i][j]);
                    cmax_temp = ccp.forwardMethod(newp);
                    if (cmax_temp > maxValue)
                    {
                        maxValue = cmax_temp;
                        index = j;
                    }
                }
                collection_cmax[i] = maxValue;
            }
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < P_Size; i++)
            {
                map.put(i, collection_cmax[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()));//降序
//            int index_best;
            index_best = lst.get(0).getKey();
            //局部搜索(使用当前最好解)
            List<Integer>[] best_solution = new List[F];
            for (int i = 0; i < F; i++)
            {
                best_solution[i] = new ArrayList<>();
            }
            for (int i = 0; i < best_solution.length; i++)
            {
                best_solution[i].addAll(assignment[index_best][i]);
            }
            int t = 0;
            while (t < 200)
            {
                //Job-swap
                best_solution = swapOperation(p, best_solution);
                //Job-insert
                best_solution = insertOperation(p, best_solution);
                //Job-inverse
                best_solution = reverseOperation(p, best_solution);
                //Factory-swap
                best_solution = factorySwap(p, best_solution);
                t += 1;
            }
            //===========================================================================
//        int cmax;
//        int cmax_temp2;
//        int index2;
//        cmax = Integer.MIN_VALUE;
//        for (int j = 0; j < best_solution.length; j++)
//        {
//            newp = getNewp(p, best_solution[j]);
//            cmax_temp2 = ccp.forwardMethod(newp);
//            if (cmax_temp2 > cmax)
//            {
//                cmax = cmax_temp2;
//                index2 = j;
//            }
//        }
//        System.out.println("Cmax = " + cmax);
            //===========================================================================
            //更新概率矩阵
            p_Matrix = updateProbabilityMatrix(p, p_Matrix, alpha, nta, lst, Population);
            //产生新种群
            for (int h = 0; h < P_Size; h++)//产生P_Size个个体
            {
                Population[h].clear();//清空原来的解
//                for (int i = 0; i < p.length; i++)
//                {
//                    for (int j = 0; j < p.length; j++)
//                    {
//                        p_Matrix[i][j] = 1.0 / p.length;
//                    }
//                }
                //拷贝
                p_Matrix_copy = getCopyOfTwoDimensionArray(p_Matrix);
                for (int i = 0; i < p.length; i++)
                {
                    r = random.nextDouble();
                    m = 0.0;
                    for (int j = 0; j < p.length; j++)
                    {
                        m += p_Matrix_copy[i][j];
                        if (r < m)
                        {
                            Population[h].add(pi.get(j));
                            //清零
                            for (int k = 0; k < p.length; k++)
                            {
                                p_Matrix_copy[k][j] = 0;
                            }
                            //归一化
                            for (int k = 0; k < p.length; k++)
                            {
                                sum = 0.0;
                                for (int l = 0; l < p.length; l++)
                                {
                                    sum += p_Matrix_copy[k][l];
                                }
                                for (int l = 0; l < p.length; l++)
                                {
                                    p_Matrix_copy[k][l] = p_Matrix_copy[k][l] / sum;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            gen += 1;
        }
        return Population[index_best];
    }

    private double[][] updateProbabilityMatrix(int[][] p, double[][] p_matrix, double alpha, double nta, List<Map.Entry<Integer, Integer>> lst, List<Integer>[] Population)
    {
        double[][] copy_p_matrix;
        int SP_Size = (int) ((nta / 100) * lst.size());
        copy_p_matrix = getCopyOfTwoDimensionArray(p_matrix);
        List<Integer>[] superior_population = new List[SP_Size];//优势种群
        int sum = 0;
        int[][] I = new int[p_matrix.length][p_matrix[0].length];
        for (int i = 0; i < SP_Size; i++)
        {
            superior_population[i] = new ArrayList<>();
        }
        for (int i = 0; i < SP_Size; i++)//优势种群
        {
            superior_population[i].addAll(Population[lst.get(i).getKey()]);
        }

        for (int i = 0; i < Population[0].size(); i++)
        {
            for (int j = 0; j < Population[0].size(); j++)
            {
                for (int k = 0; k < SP_Size; k++)
                {
                    if (superior_population[k].subList(0, i + 1).contains(j + 1))
                    {
                        I[i][j] += 1;
                    }
                }
            }
        }
        for (int i = 0; i < p_matrix.length; i++)
        {
            for (int j = 0; j < p_matrix[0].length; j++)
            {
                copy_p_matrix[i][j] = (1 - alpha) * copy_p_matrix[i][j] + (alpha / ((i + 1) * SP_Size)) * I[i][j];
            }
        }
        return copy_p_matrix;
    }

    private List<Integer>[] factorySwap(int[][] p, List<Integer>[] best_solution)
    {
        //对Cmax较大的工厂进行操作
        int[][] newp;
        int cmax_temp;
        int maxValue1;
        int maxValue2;
        int index = 0;
        int randomValue1;
        int randomValue2;
        int randomFactory;
        int temp1;
        int temp2;
        List<Integer>[] best = new List[best_solution.length];
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i] = new ArrayList<>();
        }
//        System.arraycopy(best_solution, 0, best, 0, best_solution.length);
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i].addAll(best_solution[i]);
        }
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        maxValue1 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp = ccp.forwardMethod(newp);
            if (cmax_temp > maxValue1)
            {
                maxValue1 = cmax_temp;
                index = j;
            }
        }
        randomValue1 = random.nextInt(best_solution[index].size());
        randomFactory = random.nextInt(best_solution.length);
        while (randomFactory == index)
        {
            randomFactory = random.nextInt(best_solution.length);
        }
        randomValue2 = random.nextInt(best_solution[randomFactory].size());
        temp1 = best_solution[index].remove(randomValue1);
        temp2 = best_solution[randomFactory].remove(randomValue2);
        best_solution[index].add(randomValue1, temp2);
        best_solution[randomFactory].add(randomValue2, temp1);

        //重新计算Cmax,若有改进,则接受此解
        maxValue2 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp = ccp.forwardMethod(newp);
//            System.out.println(cmax_temp);
            if (cmax_temp > maxValue2)
            {
                maxValue2 = cmax_temp;
                index = j;
            }
        }
        if (maxValue2 < maxValue1)
        {
            return best_solution;
        }
        else
        {
            return best;
        }
    }

    private List<Integer>[] reverseOperation(int[][] p, List<Integer>[] best_solution)
    {
        //对Cmax较大的工厂进行操作
        int[][] newp;
        int cmax_temp;
        int maxValue1;
        int maxValue2;
        int index = 0;
        int randomValue1;
        int randomValue2;
        int temp;
        List<Integer>[] best = new List[best_solution.length];
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i] = new ArrayList<>();
        }
//        System.arraycopy(best_solution, 0, best, 0, best_solution.length);
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i].addAll(best_solution[i]);
        }
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        maxValue1 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp = ccp.forwardMethod(newp);
            if (cmax_temp > maxValue1)
            {
                maxValue1 = cmax_temp;
                index = j;
            }
        }
        randomValue1 = random.nextInt(best_solution[index].size());
        randomValue2 = random.nextInt(best_solution[index].size());
        while (randomValue1 == randomValue2)
        {
            randomValue2 = random.nextInt(best_solution[index].size());
            if (best_solution[index].size() == 1)
            {
                break;
            }
        }
        List<Integer> best_solution_temp;
        if (randomValue1 < randomValue2)
        {
            best_solution_temp = best_solution[index].subList(randomValue1, randomValue2 + 1);
            Collections.reverse(best_solution_temp);
        }
        else
        {
            best_solution_temp = best_solution[index].subList(randomValue2, randomValue1 + 1);
            Collections.reverse(best_solution_temp);
        }

        //重新计算Cmax,若有改进,则接受此解
        maxValue2 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp = ccp.forwardMethod(newp);
            if (cmax_temp > maxValue2)
            {
                maxValue2 = cmax_temp;
                index = j;
            }
        }
        if (maxValue2 < maxValue1)
        {
            return best_solution;
        }
        else
        {
            return best;
        }
    }

    private List<Integer>[] insertOperation(int[][] p, List<Integer>[] best_solution)
    {
        //对Cmax较大的工厂进行操作
        int[][] newp;
        int cmax_temp;
        int maxValue1;
        int maxValue2;
        int index = 0;
        int randomValue1;
        int randomValue2;
        int temp;
        List<Integer>[] best = new List[best_solution.length];
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i] = new ArrayList<>();
        }
//        System.arraycopy(best_solution, 0, best, 0, best_solution.length);
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i].addAll(best_solution[i]);
        }
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        maxValue1 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp = ccp.forwardMethod(newp);
            if (cmax_temp > maxValue1)
            {
                maxValue1 = cmax_temp;
                index = j;
            }
        }
        randomValue1 = random.nextInt(best_solution[index].size());
        randomValue2 = random.nextInt(best_solution[index].size());
        while (randomValue1 == randomValue2)
        {
            randomValue2 = random.nextInt(best_solution[index].size());
            if (best_solution[index].size() == 1)
            {
                break;
            }
        }
        temp = best_solution[index].remove(randomValue1);
        best_solution[index].add(randomValue2, temp);
        //重新计算Cmax,若有改进,则接受此解
        maxValue2 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp = ccp.forwardMethod(newp);
            if (cmax_temp > maxValue2)
            {
                maxValue2 = cmax_temp;
                index = j;
            }
        }
        if (maxValue2 < maxValue1)
        {
            return best_solution;
        }
        else
        {
            return best;
        }
    }

    private List<Integer>[] swapOperation(int[][] p, List<Integer>[] best_solution)
    {
        //对Cmax较大的工厂进行操作
        int[][] newp;
        int[] cmax_temp = new int[best_solution.length];
//        int[] cmax_temp2 = new int[best_solution.length];
        int maxValue1;
        int maxValue2;
        int index = 0;
//        int index2 = 0;
        int randomValue1;
        int randomValue2;
        int temp;
        List<Integer>[] best = new List[best_solution.length];
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i] = new ArrayList<>();
        }
//        System.arraycopy(best_solution, 0, best, 0, best_solution.length);
        for (int i = 0; i < best_solution.length; i++)
        {
            best[i].addAll(best_solution[i]);
        }
        CalculateCmaxPFSP ccp = new CalculateCmaxPFSP();
        maxValue1 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp[j] = ccp.forwardMethod(newp);
            if (cmax_temp[j] > maxValue1)
            {
                maxValue1 = cmax_temp[j];
                index = j;
            }
        }
        randomValue1 = random.nextInt(best_solution[index].size());
        randomValue2 = random.nextInt(best_solution[index].size());
        while (randomValue1 == randomValue2)
        {
            randomValue2 = random.nextInt(best_solution[index].size());
            if (best_solution[index].size() == 1)
            {
                break;
            }
        }
//        temp = best_solution[index].remove(randomValue1);
//        best_solution[index].add(randomValue2, temp);
//        temp = best_solution[index].remove(randomValue2 + 1);
//        best_solution[index].add(randomValue1, temp);
        Collections.swap(best_solution[index], randomValue1, randomValue2);
        //重新计算Cmax,若有改进,则接受此解
        maxValue2 = Integer.MIN_VALUE;
        for (int j = 0; j < best_solution.length; j++)
        {
            newp = getNewp(p, best_solution[j]);
            cmax_temp[j] = ccp.forwardMethod(newp);
            if (cmax_temp[j] > maxValue2)
            {
                maxValue2 = cmax_temp[j];
                index = j;
            }
        }
        if (maxValue2 < maxValue1)
        {
            return best_solution;
        }
        else
        {
            return best;
        }
    }

    //    @Test
    public List<Integer>[] ecfRule(int[][] p, List<Integer> pi, int F)
    {
//        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++)
        {
            Lambda[k].add(pi.get(k));
        }

        for (int k = F; k < p.length; k++)
        {
            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;
    }

    public 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 double[][] getCopyOfTwoDimensionArray(double[][] p)
    {
        double[][] copyP = new double[p.length][p[0].length];
        for (int i = 0; i < p.length; i++)
        {
            System.arraycopy(p[i], 0, copyP[i], 0, p[0].length);
        }
        return copyP;
    }
}

计算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];
    }

    @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.demo19;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class ReadTxt
{
    public int[][] readDPFSPBenchmarkTxt(String filePath, int m, int n)
    {
        File f = new File(filePath);
        if (!f.exists())
        {
            System.out.println("找不到文件");
            return null;
        }
        int[][] p = new int[n][m];
        int[] a = null;
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(f));
            Scanner sc = new Scanner(br);
            a = new int[2 * m * n + 3];
            int i = 0;
            while (sc.hasNext())
            {
                a[i] = sc.nextInt();
                i++;
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        int k = 4;
        for (int j = 0; j < n; j++)
        {
            for (int i = 0; i < m; i++)
            {
                p[j][i] = a[k];
                k += 2;
            }
        }
        return p;
    }
}

结果

简单例子

在这里插入图片描述

测试集

在这里插入图片描述
需要打开注释:
在这里插入图片描述
在这里插入图片描述

参考文献

S. Y. Wang, L. Wang, M. Liu, and Y. Xu, “An effective estimation of distribution algorithm for solving the distributed permutation flow-shop scheduling problem,” in International Journal of Production Economics, 2013, vol. 145, no. 1. doi: 10.1016/j.ijpe.2013.05.004.

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值