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

  • 摘要
  • 说明
  • 代码
    • 测试类(TDIWO)
    • 算法主体(TDIWO)
    • 测试类(HDIWO)
    • 算法主体(HDIWO)
    • 测试类(HDIWOp)
    • 算法主体(HDIWOp)
  • 结果
  • 参考文献

摘要

Distributed assembly permutation flowshop scheduling problem (DAPFSP) has important applications in modern assembly systems. In this paper, we present three variants of the discrete invasive weed optimization (DIWO) for the DAPFSP with total flowtime criterion. For solving such a problem, we present a two-level representation that consists of a product permutation and a number of job sequences. We introduce neighbourhood operators for both the product permutation and job sequences. We design effective local search procedures respectively for product-permutation-based neighbourhood and job-sequence-based neighbourhood. By combining the problem-specific knowledge and the idea of invasive weed optimization, we present three DIWO-based algorithms: a two-level discrete invasive weed optimization (TDIWO), a discrete invasive weed optimization with hybrid search operators (HDIWO), and a HDIWO with selection probability. The algorithms explore the two neighbourhoods in quite a different way. We calibrate the presented DIWO algorithms by means of the design of experimental method, and carry out a comprehensive computational campaign based on the 810 benchmark instances in the literature. The numerical experiments show that the presented DIWO algorithms perform significantly better than the other competing algorithms in the literature. Among the proposed algorithms, HDIWO is the best one.

说明

本篇博客在同窗@CXY_XZSong的大力帮助下得以实现,在此献上诚挚的感谢!本篇文章使用离散化的入侵杂草优化算法解决分布式装配置换流水车间调度问题,优化目标为总流经时间。本人在实现过程中使用的优化目标为最大完成时间(Cmax),请自行实现其他优化目标的代码。再次严正申明,本人不精通Java,只是工作需要,编程过程存在很多不规范的地方,代码并不能保证完全正确,请自行批判使用,此代码并不完整,有需要的请私信,诚望各位大佬指出不足,共同交流学习,这将有助于我改进自己的工作!

代码

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

测试类(TDIWO)

package practice.comparison.IWO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2022/4/19 16:25
 * @Author:
 * @File: TestTDIWO.class
 * @Software: IDEA
 * @Email: 1532116227@qq
 */
public class TestTDIWO
{
    public static void main(String[] args) throws IOException
    {
//        final int[][] p = {{4, 6}, {3, 4}, {5, 2}, {5, 4}, {4, 2}, {3, 3},
//                {4, 3}, {3, 5}, {6, 2}, {3, 5}, {4, 4}, {4, 3}};//加工时间矩阵
//        final int[] assignSet = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        final int[] assignSet = {2, 2, 4, 3, 3, 1, 2, 1, 1, 3, 4, 4};
//        final int[] apt = {4, 8, 6, 6};
//        final int F = 2;//工厂数
//        final int t = 4;//产品数
        final int delta = 5;
        final int ps0 = 10;
        final int psMax = 50;
        final int taoMax = 20;
        final int taoMin = 1;
        final int c = 40;
        final double rho = 0.9;
//
//        int Cmax;
//
//        HDIWO hdiwo = new HDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
//        Cmax = hdiwo.algorithm();
//        System.out.println("Cmax = " + Cmax);
//======================================================================================================================

        int jobs[] = {100, 200, 500};
        int machines[] = {5, 10, 20};
        int factories[] = {4, 6, 8};
        int products[] = {30, 40, 50};
        int instnces[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//        int jobs[] = {100};
//        int machines[] = {5};
//        int factories[] = {4};
//        int products[] = {30};
//        int instnces[] = {1};

        int F;//工厂数
        int t;//产品数

        int[][] p = new int[0][];
        ReadTxt readTxt = new ReadTxt();
        List<Integer> pi = new ArrayList<>();//工件序列
        int[] apt;
        int[] assignSet;
        int Cmax;
        int count = 1;

        int[][] result = new int[machines.length * factories.length * products.length * instnces.length][1];
        int index;
        int index2 = 0;
        String filePath;
        String sheetName;
//        PoiOutputXlsx poiOutputXlsx = new PoiOutputXlsx();
        filePath = "C:\\Users\\Zhu Bo\\Desktop\\001.xlsx";
        int[] flag_NI_machine;
//        RIG2 rig2 = new RIG2(flag_NI_machine);
//        RIG2 rig2 = new RIG2();

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\XXX\\Desktop\\Results\\DAPFSP_IWO_LARGE.txt"));//使用缓冲流加快速度
        int[] D = new int[jobs.length * machines.length * factories.length * products.length * instnces.length];

        for (int n = 0; n < jobs.length; n++)//job.length - 1
        {
            index = 0;
//            index2 = 0;
            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]);
//                            flag_NI_machine = readTxt.getNoIdleConstraint("D:\\Instances\\DAPFSPSmall\\NoIdleConstraint\\I" + "_"
//                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", machines[m]);
                            //大规模
                            p = readTxt.getProcessingTimes("D:\\Instances\\DAPFSPLarge\\ProcessingTime\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], machines[m]);
                            apt = readTxt.getAssemblyTimes("D:\\Instances\\DAPFSPLarge\\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\\DAPFSPLarge\\AssignSet\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], products[i]);
//                            flag_NI_machine = readTxt.getNoIdleConstraint("D:\\Instances\\DAPFSPLarge\\NoIdleConstraint\\I" + "_"
//                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", machines[m]);
                            System.out.println(count++);
                            F = factories[k];
                            t = products[i];
//                            HDIWO hdiwo = new HDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
                            TDIWO tdiwo = new TDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
                            Cmax = tdiwo.algorithm();
                            System.out.println("Cmax = " + Cmax);
                            System.out.println("============================================");
//                            result[index++][0] = Cmax;
                            D[index2++] = Cmax;
                        }
                    }
                }
            }
//            sheetName = "Job" + jobs[n];
//            poiOutputXlsx.outPutXlsx(filePath, sheetName, result);
        }
        for (int i = 0; i < D.length; i++)
        {
            bw.write(D[i] + "\n");
            if ((i + 1) % 180 == 0)
            {
                bw.write("\n\n");
            }
        }
        bw.close();
    }
}

算法主体(TDIWO)

package practice.comparison.IWO;

import practice.calculateCMAX.CalculateCmaxDAPFSP;

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

/**
 * @Date: 2022/4/19 16:25
 * @Author: 
 * @File: TDIWO.class
 * @Software: IDEA
 * @Email: 1532116227@qq
 */
public class TDIWO
{
    final int[][] p;
    final int[] assignSet;
    final int[] apt;
    final int F;
    final int t;
    final int delta;
    final int ps0;
    final int psMax;
    final int taoMax;
    final int taoMin;
    final double rho;
    final int c;

    public TDIWO(int[][] p, int[] assignSet, int[] apt, int f, int t, int delta, int ps0, int psMax, int taoMax, int taoMin, double rho, int c)
    {
        this.p = p;
        this.assignSet = assignSet;
        this.apt = apt;
        F = f;
        this.t = t;
        this.delta = delta;
        this.ps0 = ps0;
        this.psMax = psMax;
        this.taoMax = taoMax;
        this.taoMin = taoMin;
        this.rho = rho;
        this.c = c;
    }
    public int algorithm()
    {
        int n = p.length;
        int m = p[0].length;


        Individual individual;
        List<Individual> population = new ArrayList<>();
        List<Integer>[] products = new List[t];
        for (int i = 0; i < t; i++)
        {
            products[i] = new ArrayList<>();
        }
        for (int i = 0; i < assignSet.length; i++)
        {
            products[assignSet[i] - 1].add(i + 1);//产品与工件对应
        }
        //初始化种群
        List<Integer>[] schedule = new List[F];
        CalculateCmaxDAPFSP ccd = new CalculateCmaxDAPFSP();
        Heuristic11 h11 = new Heuristic11();
        Heuristic21 h21 = new Heuristic21();

        individual = new Individual();
        individual.products = Others.copyList(products, t);
        schedule = h11.algorithm(p, t, apt, F, individual.products, individual.pi);
        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
        population.add(individual);

        individual = new Individual();
        individual.products = Others.copyList(products, t);
        schedule = h21.algorithm(p, t, apt, F, individual.products, individual.pi);
        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
        population.add(individual);

        for (int i = 2; i < ps0; i++)
        {
            individual = new Individual();
            switch (i % 2)
            {
                case 0:
                    individual.pi = Others.copyList(population.get(0).pi);
                    individual.products = Others.copyList(population.get(0).products, t);
                    break;
                case 1:
                    individual.pi = Others.copyList(population.get(1).pi);
                    individual.products = Others.copyList(population.get(1).products, t);
                    break;
            }
            individual.cmax = Methods.randomInit(p, apt, individual.pi, individual.products, F);
            population.add(individual);
        }

        long startTime = System.currentTimeMillis();
        long end = c * m * n;
        long endStage1 = (long) (end * rho);
        int seedsNumber;
        Individual worstCmax, bestCmax;
        int populationSize, seedMinIndex;
//        int Gen = 100;
//        int gen = 0;
//        while (System.currentTimeMillis() - startTime < end)
        while (System.currentTimeMillis() - startTime < endStage1)
//        while (gen < Gen)
        {
            bestCmax = Collections.min(population);
            worstCmax = Collections.max(population);
            populationSize = population.size();
            for (int i = 0; i < populationSize; i++)
            {
                seedsNumber = Methods.getSeedsNumber(bestCmax.cmax, worstCmax.cmax, population.get(i).cmax, taoMax, taoMin);
                for (int j = 0; j < seedsNumber; j++)
                {
                    individual = new Individual();
                    List<Integer> piT = new ArrayList<>();
                    individual.pi = Others.copyList(population.get(i).pi);
                    individual.products = Others.copyList(population.get(i).products, t);
                    Methods.shiftProductDeltaTimes(individual.pi, delta);
                    Methods.shiftJobDeltaTimes(individual.products, delta);
                    for (int k = 0; k < apt.length; k++)
                    {
                        piT.addAll(products[individual.pi.get(k) - 1]);
                    }
                    schedule = Heuristic11.assignByNR1(p, piT, F);
                    individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
                    population.add(individual);
                }
            }
            if (population.size() > psMax)
            {
                Collections.sort(population);
                for (int i = population.size() - 1, psMax = this.psMax - 1; i > psMax; i--)
                {
                    population.remove(i);
                }
            }
//            gen++;
        }
        while (System.currentTimeMillis() - startTime < end)
//        while (gen < Gen)
        {
            bestCmax = Collections.min(population);
            worstCmax = Collections.max(population);
            populationSize = population.size();
            for (int i = 0; i < populationSize; i++)
            {
                seedsNumber = Methods.getSeedsNumber(bestCmax.cmax, worstCmax.cmax, population.get(i).cmax, taoMax, taoMin);
                for (int j = 0; j < seedsNumber; j++)
                {
                    individual = new Individual();
                    List<Integer> piT = new ArrayList<>();
                    individual.pi = Others.copyList(population.get(i).pi);
                    individual.products = Others.copyList(population.get(i).products, t);
                    Methods.shiftProductDeltaTimes(individual.pi, delta);
                    Methods.shiftJobDeltaTimes(individual.products, delta);
                    for (int k = 0; k < apt.length; k++)
                    {
                        piT.addAll(products[individual.pi.get(k) - 1]);
                    }
                    schedule = Heuristic11.assignByNR1(p, piT, F);
                    individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
                    population.add(individual);
                }
            }
            if (population.size() > psMax)
            {
                Collections.sort(population);
                for (int i = population.size() - 1, psMax = this.psMax - 1; i > psMax; i--)
                {
                    population.remove(i);
                }
            }
//            gen++;
        }
        return Collections.min(population).cmax;
    }
}

测试类(HDIWO)

package practice.comparison.IWO;

//import practiceparison.Others.PoiOutputXlsx;
import practice.comparison.IWO.ReadTxt;
//import practiceparison.RIG.RIG2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2022/4/18 15:56
 * @Author: 
 * @File: TestHDIWO.class
 * @Software: IDEA
 * @Email: 1532116227@qq
 */
public class TestHDIWO
{
    public static void main(String[] args) throws IOException
    {
//        final int[][] p = {{4, 6}, {3, 4}, {5, 2}, {5, 4}, {4, 2}, {3, 3},
//                {4, 3}, {3, 5}, {6, 2}, {3, 5}, {4, 4}, {4, 3}};//加工时间矩阵
//        final int[] assignSet = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        final int[] assignSet = {2, 2, 4, 3, 3, 1, 2, 1, 1, 3, 4, 4};
//        final int[] apt = {4, 8, 6, 6};
//        final int F = 2;//工厂数
//        final int t = 4;//产品数
        final int delta = 5;
        final int ps0 = 10;
        final int psMax = 50;
        final int taoMax = 20;
        final int taoMin = 1;
        final int c = 40;
        final double rho = 0.9;
//
//        int Cmax;
//
//        HDIWO hdiwo = new HDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
//        Cmax = hdiwo.algorithm();
//        System.out.println("Cmax = " + Cmax);
//======================================================================================================================

        int jobs[] = {100, 200, 500};
        int machines[] = {5, 10, 20};
        int factories[] = {4, 6, 8};
        int products[] = {30, 40, 50};
        int instnces[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//        int jobs[] = {100};
//        int machines[] = {5};
//        int factories[] = {4};
//        int products[] = {30};
//        int instnces[] = {1};

        int F;//工厂数
        int t;//产品数

        int[][] p = new int[0][];
        ReadTxt readTxt = new ReadTxt();
        List<Integer> pi = new ArrayList<>();//工件序列
        int[] apt;
        int[] assignSet;
        int Cmax;
        int count = 1;

        int[][] result = new int[machines.length * factories.length * products.length * instnces.length][1];
        int index;
        int index2 = 0;
        String filePath;
        String sheetName;
//        PoiOutputXlsx poiOutputXlsx = new PoiOutputXlsx();
        filePath = "C:\\Users\\Zhu Bo\\Desktop\\001.xlsx";
        int[] flag_NI_machine;
//        RIG2 rig2 = new RIG2(flag_NI_machine);
//        RIG2 rig2 = new RIG2();

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\XXX\\Desktop\\Results\\DAPFSP_IWO_LARGE.txt"));//使用缓冲流加快速度
        int[] D = new int[jobs.length * machines.length * factories.length * products.length * instnces.length];

        for (int n = 0; n < jobs.length; n++)//job.length - 1
        {
            index = 0;
//            index2 = 0;
            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]);
//                            flag_NI_machine = readTxt.getNoIdleConstraint("D:\\Instances\\DAPFSPSmall\\NoIdleConstraint\\I" + "_"
//                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", machines[m]);
                            //大规模
                            p = readTxt.getProcessingTimes("D:\\Instances\\DAPFSPLarge\\ProcessingTime\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], machines[m]);
                            apt = readTxt.getAssemblyTimes("D:\\Instances\\DAPFSPLarge\\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\\DAPFSPLarge\\AssignSet\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], products[i]);
//                            flag_NI_machine = readTxt.getNoIdleConstraint("D:\\Instances\\DAPFSPLarge\\NoIdleConstraint\\I" + "_"
//                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", machines[m]);
                            System.out.println(count++);
                            F = factories[k];
                            t = products[i];
                            HDIWO hdiwo = new HDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
                            Cmax = hdiwo.algorithm();
                            System.out.println("Cmax = " + Cmax);
                            System.out.println("============================================");
//                            result[index++][0] = Cmax;
                            D[index2++] = Cmax;
                        }
                    }
                }
            }
//            sheetName = "Job" + jobs[n];
//            poiOutputXlsx.outPutXlsx(filePath, sheetName, result);
        }
        for (int i = 0; i < D.length; i++)
        {
            bw.write(D[i] + "\n");
            if ((i + 1) % 180 == 0)
            {
                bw.write("\n\n");
            }
        }
        bw.close();

    }
}

算法主体(HDIWO)

package practice.comparison.IWO;

import Common.Common;
import Others.run.Utils;
import practice.calculateCMAX.CalculateCmaxDAPFSP;

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

/**
 * @Date: 2022/4/18 15:56
 * @Author: 
 * @File: HDIWO.class
 * @Software: IDEA
 * @Email: 1532116227@qq
 */
public class HDIWO
{
    final int[][] p;
    final int[] assignSet;
    final int[] apt;
    final int F;
    final int t;
    final int delta;
    final int ps0;
    final int psMax;
    final int taoMax;
    final int taoMin;
    final double rho;
    final int c;

    public HDIWO(int[][] p, int[] assignSet, int[] apt, int f, int t, int delta, int ps0, int psMax, int taoMax, int taoMin, double rho, int c)
    {
        this.p = p;
        this.assignSet = assignSet;
        this.apt = apt;
        F = f;
        this.t = t;
        this.delta = delta;
        this.ps0 = ps0;
        this.psMax = psMax;
        this.taoMax = taoMax;
        this.taoMin = taoMin;
        this.rho = rho;
        this.c = c;
    }


    public int algorithm()
    {
        int n = p.length;
        int m = p[0].length;


        Individual individual;
        List<Individual> population = new ArrayList<>();
        List<Integer>[] products = new List[t];
        for (int i = 0; i < t; i++)
        {
            products[i] = new ArrayList<>();
        }
        for (int i = 0; i < assignSet.length; i++)
        {
            products[assignSet[i] - 1].add(i + 1);//产品与工件对应
        }

        //初始化种群
        List<Integer>[] schedule = new List[F];
        CalculateCmaxDAPFSP ccd = new CalculateCmaxDAPFSP();
        Heuristic11 h11 = new Heuristic11();
        Heuristic21 h21 = new Heuristic21();

        individual = new Individual();
        individual.products = Others.copyList(products, t);
        schedule = h11.algorithm(p, t, apt, F, individual.products, individual.pi);
        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
        population.add(individual);

        individual = new Individual();
        individual.products = Others.copyList(products, t);
        schedule = h21.algorithm(p, t, apt, F, individual.products, individual.pi);
        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
        population.add(individual);

        for (int i = 2; i < ps0; i++)
        {
            individual = new Individual();
            switch (i % 2)
            {
                case 0:
                    individual.pi = Others.copyList(population.get(0).pi);
                    individual.products = Others.copyList(population.get(0).products, t);
                    break;
                case 1:
                    individual.pi = Others.copyList(population.get(1).pi);
                    individual.products = Others.copyList(population.get(1).products, t);
                    break;
            }
            individual.cmax = Methods.randomInit(p, apt, individual.pi, individual.products, F);
            population.add(individual);
        }

        long startTime = System.currentTimeMillis();
        long end = c * m * n;
        int seedsNumber;
        Individual worstCmax, bestCmax;
        int populationSize, seedMinIndex;

//        int Gen = 100;
//        int gen = 0;
        while (System.currentTimeMillis() - startTime < end)
//        while (gen < Gen)
        {
            bestCmax = Collections.min(population);
            worstCmax = Collections.max(population);
            populationSize = population.size();
            for (int i = 0; i < populationSize; i++)
            {
                seedsNumber = Methods.getSeedsNumber(bestCmax.cmax, worstCmax.cmax, population.get(i).cmax, taoMax, taoMin);
                for (int j = 0; j < seedsNumber; j++)
                {
                    individual = new Individual();
                    List<Integer> piT = new ArrayList<>();
                    individual.pi = Others.copyList(population.get(i).pi);
                    individual.products = Others.copyList(population.get(i).products, t);
                    Methods.shiftProductDeltaTimes(individual.pi, delta);
                    Methods.shiftJobDeltaTimes(individual.products, delta);
                    for (int k = 0; k < apt.length; k++)
                    {
                        piT.addAll(products[individual.pi.get(k) - 1]);
                    }
                    schedule = Heuristic11.assignByNR1(p, piT, F);
                    individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
                    population.add(individual);
                }
            }
            if (population.size() > psMax)
            {
                Collections.sort(population);
                for (int i = population.size() - 1, psMax = this.psMax - 1; i > psMax; i--)
                {
                    population.remove(i);
                }
            }
//            gen++;
        }
        return Collections.min(population).cmax;
    }
}

测试类(HDIWOp)

package practice.comparison.IWO;

//import practiceparison.Others.PoiOutputXlsx;
//import practiceparison.RIG.RIG2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2022/4/19 16:30
 * @Author: 
 * @File: TestHDIWOp.class
 * @Software: IDEA
 * @Email: 1532116227@qq
 */
public class TestHDIWOp
{
    public static void main(String[] args) throws IOException
    {
//        final int[][] p = {{4, 6}, {3, 4}, {5, 2}, {5, 4}, {4, 2}, {3, 3},
//                {4, 3}, {3, 5}, {6, 2}, {3, 5}, {4, 4}, {4, 3}};//加工时间矩阵
//        final int[] assignSet = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        final int[] assignSet = {2, 2, 4, 3, 3, 1, 2, 1, 1, 3, 4, 4};
//        final int[] apt = {4, 8, 6, 6};
//        final int F = 2;//工厂数
//        final int t = 4;//产品数
        final int delta = 5;
        final int ps0 = 10;
        final int psMax = 50;
        final int taoMax = 20;
        final int taoMin = 1;
        final int c = 20;
        final double rho = 0.9;
//
//        int Cmax;
//
//        HDIWO hdiwo = new HDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
//        Cmax = hdiwo.algorithm();
//        System.out.println("Cmax = " + Cmax);
//======================================================================================================================

        int jobs[] = {100, 200, 500};
        int machines[] = {5, 10, 20};
        int factories[] = {4, 6, 8};
        int products[] = {30, 40, 50};
        int instnces[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//        int jobs[] = {100};
//        int machines[] = {5};
//        int factories[] = {4};
//        int products[] = {30};
//        int instnces[] = {1};

        int F;//工厂数
        int t;//产品数
        double alpha = 0.9;

        int[][] p = new int[0][];
        ReadTxt readTxt = new ReadTxt();
        List<Integer> pi = new ArrayList<>();//工件序列
        int[] apt;
        int[] assignSet;
        int Cmax;
        int count = 1;

        int[][] result = new int[machines.length * factories.length * products.length * instnces.length][1];
        int index;
        int index2 = 0;
        String filePath;
        String sheetName;
//        PoiOutputXlsx poiOutputXlsx = new PoiOutputXlsx();
        filePath = "C:\\Users\\XXX\\Desktop\\001.xlsx";
//        RIG2 rig2 = new RIG2(flag_NI_machine);
//        RIG2 rig2 = new RIG2();

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\XXX\\Desktop\\Results\\DAPFSP_IWO_LARGE.txt"));//使用缓冲流加快速度
        int[] D = new int[jobs.length * machines.length * factories.length * products.length * instnces.length];

        for (int n = 0; n < jobs.length; n++)//job.length - 1
        {
            index = 0;
//            index2 = 0;
            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]);
//                            flag_NI_machine = readTxt.getNoIdleConstraint("D:\\Instances\\DAPFSPSmall\\NoIdleConstraint\\I" + "_"
//                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", machines[m]);
                            //大规模
                            p = readTxt.getProcessingTimes("D:\\Instances\\DAPFSPLarge\\ProcessingTime\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], machines[m]);
                            apt = readTxt.getAssemblyTimes("D:\\Instances\\DAPFSPLarge\\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\\DAPFSPLarge\\AssignSet\\I" + "_"
                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", jobs[n], products[i]);
//                            flag_NI_machine = readTxt.getNoIdleConstraint("D:\\Instances\\DAPFSPLarge\\NoIdleConstraint\\I" + "_"
//                                    + jobs[n] + "_" + machines[m] + " _" + factories[k] + "_" + products[i] + "_" + instnces[j] + ".txt", machines[m]);
                            System.out.println(count++);
                            F = factories[k];
                            t = products[i];
//                            HDIWO hdiwo = new HDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
//                            TDIWO tdiwo = new TDIWO(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c);
                            HDIWOp hdiwop = new HDIWOp(p, assignSet, apt, F, t, delta, ps0, psMax, taoMax, taoMin, rho, c, alpha);
                            Cmax = hdiwop.algorithm();
                            System.out.println("Cmax = " + Cmax);
                            System.out.println("============================================");
//                            result[index++][0] = Cmax;
                            D[index2++] = Cmax;
                        }
                    }
                }
            }
//            sheetName = "Job" + jobs[n];
//            poiOutputXlsx.outPutXlsx(filePath, sheetName, result);
        }
        for (int i = 0; i < D.length; i++)
        {
            bw.write(D[i] + "\n");
            if ((i + 1) % 180 == 0)
            {
                bw.write("\n\n");
            }
        }
        bw.close();

    }
}

算法主体(HDIWOp)

package practice.comparison.IWO;

import Others.run.Utils;
import practice.calculateCMAX.CalculateCmaxDAPFSP;

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

/**
 * @Date: 2022/4/19 16:30
 * @Author: 
 * @File: HDIWOp.class
 * @Software: IDEA
 * @Email: 1532116227@qq
 */
public class HDIWOp
{
    final int[][] p;
    final int[] assignSet;
    final int[] apt;
    final int F;
    final int t;
    final int delta;
    final int ps0;
    final int psMax;
    final int taoMax;
    final int taoMin;
    final double rho;
    final int c;
    final double alpha;

    public HDIWOp(int[][] p, int[] assignSet, int[] apt, int f, int t, int delta, int ps0, int psMax, int taoMax, int taoMin, double rho, int c, double alpha)
    {
        this.p = p;
        this.assignSet = assignSet;
        this.apt = apt;
        F = f;
        this.t = t;
        this.delta = delta;
        this.ps0 = ps0;
        this.psMax = psMax;
        this.taoMax = taoMax;
        this.taoMin = taoMin;
        this.rho = rho;
        this.c = c;
        this.alpha = alpha;
    }


    public int algorithm()
    {
        int n = p.length;
        int m = p[0].length;


        Individual individual;
        List<Individual> population = new ArrayList<>();
        List<Integer>[] products = new List[t];
        for (int i = 0; i < t; i++)
        {
            products[i] = new ArrayList<>();
        }
        for (int i = 0; i < assignSet.length; i++)
        {
            products[assignSet[i] - 1].add(i + 1);//产品与工件对应
        }

        //初始化种群
        List<Integer>[] schedule = new List[F];
        CalculateCmaxDAPFSP ccd = new CalculateCmaxDAPFSP();
        Heuristic11 h11 = new Heuristic11();
        Heuristic21 h21 = new Heuristic21();

        individual = new Individual();
        individual.products = Others.copyList(products, t);
        schedule = h11.algorithm(p, t, apt, F, individual.products, individual.pi);
        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
        population.add(individual);

        individual = new Individual();
        individual.products = Others.copyList(products, t);
        schedule = h21.algorithm(p, t, apt, F, individual.products, individual.pi);
        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
        population.add(individual);

        for (int i = 2; i < ps0; i++)
        {
            individual = new Individual();
            switch (i % 2)
            {
                case 0:
                    individual.pi = Others.copyList(population.get(0).pi);
                    individual.products = Others.copyList(population.get(0).products, t);
                    break;
                case 1:
                    individual.pi = Others.copyList(population.get(1).pi);
                    individual.products = Others.copyList(population.get(1).products, t);
                    break;
            }
            individual.cmax = Methods.randomInit(p, apt, individual.pi, individual.products, F);
            population.add(individual);
        }

        long startTime = System.currentTimeMillis();
        long end = c * m * n;
//        long endStage1 = (long) (end * rho);
        int seedsNumber;
        Individual worstCmax, bestCmax;
        int populationSize, seedMinIndex;

//        int Gen = 100;
//        int gen = 0;
//        while (System.currentTimeMillis() - startTime < end)
//        while (gen < Gen)
        while (System.currentTimeMillis() - startTime < end)
        {
            bestCmax = Collections.min(population);
            worstCmax = Collections.max(population);
            populationSize = population.size();
            if (Math.random() < alpha)
            {
                for (int i = 0; i < populationSize; i++)
                {
                    seedsNumber = Methods.getSeedsNumber(bestCmax.cmax, worstCmax.cmax, population.get(i).cmax, taoMax, taoMin);
                    for (int j = 0; j < seedsNumber; j++)
                    {
                        individual = new Individual();
                        List<Integer> piT = new ArrayList<>();
                        individual.pi = Others.copyList(population.get(i).pi);
                        individual.products = Others.copyList(population.get(i).products, t);
                        Methods.shiftProductDeltaTimes(individual.pi, delta);
//                        Methods.shiftJobDeltaTimes(individual.products, delta);
                        for (int k = 0; k < apt.length; k++)
                        {
                            piT.addAll(products[individual.pi.get(k) - 1]);
                        }
                        schedule = Heuristic11.assignByNR1(p, piT, F);
                        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
                        population.add(individual);
                    }
                }
                seedMinIndex = Methods.getSeedMinIndex(population, populationSize, population.size());
                individual = population.get(seedMinIndex);
                individual.cmax = Methods.localSearchProduct(p, apt, individual.pi, individual.products, individual.cmax, F, t);
//                individual.tf = localSearch.localSearchProduct(p, pp, individual.pi, individual.products, individual.tf, f, t);
            }
            else
            {
                for (int i = 0; i < populationSize; i++)
                {
                    seedsNumber = Methods.getSeedsNumber(bestCmax.cmax, worstCmax.cmax, population.get(i).cmax, taoMax, taoMin);
                    for (int j = 0; j < seedsNumber; j++)
                    {
                        individual = new Individual();
                        List<Integer> piT = new ArrayList<>();
                        individual.pi = Others.copyList(population.get(i).pi);
                        individual.products = Others.copyList(population.get(i).products, t);
//                        Methods.shiftProductDeltaTimes(individual.pi, delta);
                        Methods.shiftJobDeltaTimes(individual.products, delta);
                        for (int k = 0; k < apt.length; k++)
                        {
                            piT.addAll(products[individual.pi.get(k) - 1]);
                        }
                        schedule = Heuristic11.assignByNR1(p, piT, F);
                        individual.cmax = ccd.calAssembleCmax(schedule, p, F, individual.pi, products, apt);
                        population.add(individual);
                    }
                }
                seedMinIndex = Methods.getSeedMinIndex(population, populationSize, population.size());
                individual = population.get(seedMinIndex);
                individual.cmax = Methods.localSearchJobs(p, apt, individual.pi, individual.products, individual.cmax, F, t);
//                individual.tf = localSearch.localSearchProduct(p, pp, individual.pi, individual.products, individual.tf, f, t);
            }
            if (population.size() > psMax)
            {
                Collections.sort(population);
                for (int i = population.size() - 1, psMax = this.psMax - 1; i > psMax; i--)
                {
                    population.remove(i);
                }
            }
//            gen++;
        }
        return Collections.min(population).cmax;
    }
}

结果

参考文献

Sang, H. Y., Pan, Q. K., Li, J. Q., Wang, P., Han, Y. Y., Gao, K. Z., & Duan, P. (2019). Effective invasive weed optimization algorithms for distributed assembly permutation flowshop problem with total flowtime criterion. Swarm and Evolutionary Computation, 44. https://doi/10.1016/j.swevo.2018.12.001

更多推荐

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