【车间调度】基于matlab遗传算法求解柔性车间调度问题【含Matlab源码 660期】

一、简介

1 遗传算法概述
遗传算法(Genetic Algorithm,GA)是进化计算的一部分,是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。该算法简单、通用,鲁棒性强,适于并行处理。

2 遗传算法的特点和应用
遗传算法是一类可用于复杂系统优化的具有鲁棒性的搜索算法,与传统的优化算法相比,具有以下特点:
(1)以决策变量的编码作为运算对象。传统的优化算法往往直接利用决策变量的实际值本身来进行优化计算,但遗传算法是使用决策变量的某种形式的编码作为运算对象。这种对决策变量的编码处理方式,使得我们在优化计算中可借鉴生物学中染色体和基因等概念,可以模仿自然界中生物的遗传和进化激励,也可以很方便地应用遗传操作算子。
(2)直接以适应度作为搜索信息。传统的优化算法不仅需要利用目标函数值,而且搜索过程往往受目标函数的连续性约束,有可能还需要满足“目标函数的导数必须存在”的要求以确定搜索方向。遗传算法仅使用由目标函数值变换来的适应度函数值就可确定进一步的搜索范围,无需目标函数的导数值等其他辅助信息。直接利用目标函数值或个体适应度值也可以将搜索范围集中到适应度较高部分的搜索空间中,从而提高搜索效率。
(3)使用多个点的搜索信息,具有隐含并行性。传统的优化算法往往是从解空间的一个初始点开始最优解的迭代搜索过程。单个点所提供的搜索信息不多,所以搜索效率不高,还有可能陷入局部最优解而停滞;遗传算法从由很多个体组成的初始种群开始最优解的搜索过程,而不是从单个个体开始搜索。对初始群体进行的、选择、交叉、变异等运算,产生出新一代群体,其中包括了许多群体信息。这些信息可以避免搜索一些不必要的点,从而避免陷入局部最优,逐步逼近全局最优解。
(4) 使用概率搜索而非确定性规则。传统的优化算法往往使用确定性的搜索方法,一个搜索点到另一个搜索点的转移有确定的转移方向和转移关系,这种确定性可能使得搜索达不到最优店,限制了算法的应用范围。遗传算法是一种自适应搜索技术,其选择、交叉、变异等运算都是以一种概率方式进行的,增加了搜索过程的灵活性,而且能以较大概率收敛于最优解,具有较好的全局优化求解能力。但,交叉概率、变异概率等参数也会影响算法的搜索结果和搜索效率,所以如何选择遗传算法的参数在其应用中是一个比较重要的问题。
综上,由于遗传算法的整体搜索策略和优化搜索方式在计算时不依赖于梯度信息或其他辅助知识,只需要求解影响搜索方向的目标函数和相应的适应度函数,所以遗传算法提供了一种求解复杂系统问题的通用框架。它不依赖于问题的具体领域,对问题的种类有很强的鲁棒性,所以广泛应用于各种领域,包括:函数优化、组合优化生产调度问题、自动控制
、机器人学、图像处理(图像恢复、图像边缘特征提取…)、人工生命、遗传编程、机器学习。

3 遗传算法的基本流程及实现技术
基本遗传算法(Simple Genetic Algorithms,SGA)只使用选择算子、交叉算子和变异算子这三种遗传算子,进化过程简单,是其他遗传算法的基础。

3.1 遗传算法的基本流程
通过随机方式产生若干由确定长度(长度与待求解问题的精度有关)编码的初始群体;
通过适应度函数对每个个体进行评价,选择适应度值高的个体参与遗传操作,适应度低的个体被淘汰;
经遗传操作(复制、交叉、变异)的个体集合形成新一代种群,直到满足停止准则(进化代数GEN>=?);
将后代中变现最好的个体作为遗传算法的执行结果。
在这里插入图片描述
其中,GEN是当前代数;M是种群规模,i代表种群数量。

3.2 遗传算法的实现技术
基本遗传算法(SGA)由编码、适应度函数、遗传算子(选择、交叉、变异)及运行参数组成。
3.2.1 编码
(1)二进制编码
二进制编码的字符串长度与问题所求解的精度有关。需要保证所求解空间内的每一个个体都可以被编码。
优点:编、解码操作简单,遗传、交叉便于实现
缺点:长度大
(2)其他编码方法
格雷码、浮点数编码、符号编码、多参数编码等
3.2.2 适应度函数
适应度函数要有效反映每一个染色体与问题的最优解染色体之间的差距。
3.2.3选择算子
在这里插入图片描述
3.2.4 交叉算子
交叉运算是指对两个相互配对的染色体按某种方式相互交换其部分基因,从而形成两个新的个体;交叉运算是遗传算法区别于其他进化算法的重要特征,是产生新个体的主要方法。在交叉之前需要将群体中的个体进行配对,一般采取随机配对原则。
常用的交叉方式:
单点交叉
双点交叉(多点交叉,交叉点数越多,个体的结构被破坏的可能性越大,一般不采用多点交叉的方式)
均匀交叉
算术交叉
3.2.5 变异算子
遗传算法中的变异运算是指将个体染色体编码串中的某些基因座上的基因值用该基因座的其他等位基因来替换,从而形成一个新的个体。

就遗传算法运算过程中产生新个体的能力方面来说,交叉运算是产生新个体的主要方法,它决定了遗传算法的全局搜索能力;而变异运算只是产生新个体的辅助方法,但也是必不可少的一个运算步骤,它决定了遗传算法的局部搜索能力。交叉算子与变异算子的共同配合完成了其对搜索空间的全局搜索和局部搜索,从而使遗传算法能以良好的搜索性能完成最优化问题的寻优过程。

3.2.6 运行参数
在这里插入图片描述
4 遗传算法的基本原理
4.1 模式定理
在这里插入图片描述
4.2 积木块假设
具有低阶、定义长度短,且适应度值高于群体平均适应度值的模式称为基因块或积木块。
积木块假设:个体的基因块通过选择、交叉、变异等遗传算子的作用,能够相互拼接在一起,形成适应度更高的个体编码串。
积木块假设说明了用遗传算法求解各类问题的基本思想,即通过积木块直接相互拼接在一起能够产生更好的解。

二、源代码

function [Chromosome] = POX_GA(T,Iterations,PopSize,Pc,Pm)
%% INPUT:
%T--input matrix:
%  For example: 
%  A partial flexible scheduling problem in which 8 jobs are processed 
%  on 8 machines, in which the number of available machining machines per
%  step of job is less than or equal to the total number of machines
% J1 ={
    
    [5 3 5 3 3 0 10 9];[10 0 5 8 3 9 9 6];[0 10 0 5 6 2 4 5]};
% J2 ={
    
    [5 7 3 9 8 0 9 0];[0 8 5 2 6 7 10 9];[0 10 0 5 6 4 1 7];[10 8 9 6 4 7 0 0]};
% J3 ={
    
    [10 0 0 7 6 5 2 4];[0 10 6 4 8 9 10 0];[1 4 5 6 0 10 0 7]};
% J4 ={
    
    [3 1 6 5 9 7 8 4];[12 11 7 8 10 5 6 9];[4 6 2 10 3 9 5 7]}; 
% J5 ={
    
    [3 6 7 8 9 0 10 0];[10 0 7 4 9 8 6 0];[0 9 8 7 4 2 7 0];[11 9 0 6 7 5 3 6]};
% J6 ={
    
    [6 7 1 4 6 9 0 10];[11 0 9 9 9 7 6 4];[10 5 9 10 11 0 10 0]};
% J7 ={
    
    [5 4 2 6 7 0 10 0];[0 9 0 9 11 9 10 5];[0 8 9 3 8 6 0 10]};
% J8 ={
    
    [2 8 5 9 0 4 0 10];[7 4 7 8 9 0 10 0];[9 9 0 8 5 6 7 1];[9 0 3 7 1 5 8 0]};
%T={
    
    J1;J2;J3;J4;J5;J6;J7;J8}; 8*1 cell
%Iterations--The number of iterations of the genetic algorithm;
%PopSize--Population size in genetic algorithms,2*PopSize+1
%Pc--probability of crossover
%Pm--probability of mutation
%% OUTPUT
% Chromosome--The best Chromosome of the genetic algorithm
%% variable declaration
num_of_jobs = length(T);                                                   %number of jobs
num_of_machines = length(T{
    
    1}{
    
    1});                                         %number of machines
steps_of_job =[];
for i = 1:num_of_jobs
    steps_of_job=[steps_of_job;length(T{
    
    i})];
end
len_of_chromosome = sum(steps_of_job);
PopSize = PopSize+1;

Performance1 =[];
machine_of_job=cell(num_of_jobs,1);
% calculate the machine set for each steps of each job
for i=1:num_of_jobs
    steps=cell(steps_of_job(i),1);
    for j = 1:steps_of_job(i)
        machineset=[];
        for k=1:length(T{
    
    i}{
    
    j})
            if T{
    
    i}{
    
    j}(k)~=0
               machineset=[machineset k]; 
            end
        end
        steps{
    
    j}=machineset;
    end
    machine_of_job{
    
    i}=steps;
end


disp('begin interating ...')
%% Coding
[Population] = Coding(T,PopSize);

%% Population iteration
FITNESS = zeros(PopSize,1);    % fitness values for population
for iterator = 1:Iterations
    %% fitness calculation
    for index=1:PopSize                         
        
        chromosome=Population{
    
    index};                                     % choose one chromosome from population
        [FitnessValue] = FitnessCalculator(T,chromosome);                  % fitness calculation                            
        FITNESS(index)=FitnessValue;                                       % the larger the Pfit_value
    end
    BestFitness=min(FITNESS);                                              % find the best chromosome
    position=find(FITNESS==BestFitness);                                   % and the position,may be more than one
    BestChromosome=Population{
    
    position(1)};                                % choose one chromosome from population
  
    
    %% Selection :Elitism and 2-tournament selection are used
    Parent = cell(PopSize,1);
    Pr =0.8;
    for index =1:PopSize-1
        pos = randperm(PopSize,2);
        chromosome1 = Population{
    
    pos(1)};
        chromosome2 = Population{
    
    pos(2)};
        if (rand(1)<Pr) && FITNESS(pos(1))>FITNESS(pos(2))
                chromosome = chromosome1;
        else
                chromosome = chromosome2;
        end
        Parent{
    
    index} = chromosome;
    end
        Parent{
    
    PopSize}=BestChromosome;                                         
        
   %% Crossover: IPOX for steps, MPX for machine 
   
    Children_group1=cell(PopSize,1);
    for i=1:(PopSize-1)
        %Parent individuals are selected for crossover operation:
        %Parent1 is selected sequentially and Parent2 is selected randomly.
        index_parent2 = randi([1,(PopSize-1)]);
        Parent1=Parent{
    
    i};
        Parent2=Parent{
    
    index_parent2};
       
        Children1=zeros(2,len_of_chromosome);
        Children2=zeros(2,len_of_chromosome);
        if rand(1)<=Pc %Use the probability to determine if crossover is required
           %% Part1: IPX for step
            %Randomly divide the set of jobs {
    
    1,2,3...,n} into two non-empty sub-sets J1 and J2.
            num_J1 = randi([1,num_of_jobs]);   
            if num_J1==num_of_jobs
                num_J1 = fix(num_of_jobs/2);  
            end
            J = randperm(num_of_jobs);          
            J1 =J(1:num_J1);
            J2 =J(num_J1+1:num_of_jobs); 
            % Copy the jobs that Parent1 contains in J1 to Children1, 
            % and Parent2 contains in J2 to Children2, and keep them in place.
            for index = 1:num_J1                                            % look for jobs that Parent1 are included in J1
                job = J1(index);
                for j = 1:len_of_chromosome
                    if job == Parent1(1,j)
                        Children1(1,j)=Parent1(1,j);
                    end 
                end   
            end
            for index = 1:num_of_jobs-num_J1                                % look for jobs that Parent2 are included in J2
                job = J2(index);
                for j = 1:len_of_chromosome
                    if job == Parent2(1,j)
                        Children2(1,j)=Parent2(1,j);
                    end 
                end   
            end           
            %Copy the jobs that Parent1 contains in J1 to Children2, 
            %and Parent2 contains in J2 to Children1 in their order.
           for index = 1:len_of_chromosome                                            % look for jobs that Parent1 are included in J1
                job = Parent1(1,index);
                if ~isempty(find(J1==job, 1))
                     for gene = 1:len_of_chromosome
                         if Children2(1,gene)==0
                            Children2(1,gene)=job;
                            break;
                         end
                     end
                end
                
            end
           for index = 1:len_of_chromosome                                            % look for jobs that Parent1 are included in J1
                job = Parent2(1,index);
                if ~isempty(find(J2==job, 1))
                     for gene = 1:len_of_chromosome
                         if Children1(1,gene)==0
                            Children1(1,gene)=job;
                            break;
                         end
                     end
                end
                
            end
            %%IPOX cross operation completed
           %% Part 2 MPX for machine 
            %The process of crossover operation is as follows: firstly, a set rand0_1 
            %composed of 0 and 1 that is equal to the length of chromosome is randomly 
            %generated, and then the genes at the same position of 1 in the set of rand0_1
            %in the two parental generations are interchanged, and two offspring are
            %obtained after the crossover
            rand0_1 = zeros(1,len_of_chromosome);
            for gene = 1:len_of_chromosome
                if rand(1)>0.5
                    rand0_1(gene)=1;
                end
            end

            for gene = 1:len_of_chromosome
                if rand0_1(gene)==1
                   Children1(2,gene) = Parent2(2,gene);
                   Children2(2,gene) = Parent1(2,gene);
                else
                   Children1(2,gene) = Parent1(2,gene);
                   Children2(2,gene) = Parent2(2,gene);                   
                end
            end    
            %MOX cross operation completed
        else  
        Children1 = Parent1;
        Children2 = Parent2;  
        end
        %% Select the Fitness value best retained to the next generation
        [Parent1_FitnessValue] = FitnessCalculator(T,Parent1);
        [Parent2_FitnessValue] = FitnessCalculator(T,Parent2);
        [Children1_FitnessValue] = FitnessCalculator(T,Children1);
        [Children2_FitnessValue] = FitnessCalculator(T,Children2);
        [~, pos] = max([Parent1_FitnessValue Parent2_FitnessValue Children1_FitnessValue Children2_FitnessValue]);
        temp_group ={
    
    Parent1 Parent2 Children1 Children2};
%         if rand(1)>0.5
%             Children_group1{
    
    i}=Children1; 
%         else
%             Children_group1{
    
    i}=Children2;
%         end
        Children_group1{
    
    i}=temp_group{
    
    pos};
        
        
    end

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、备注

完整代码或者代写添加QQ 1564658423

猜你喜欢

转载自blog.csdn.net/TIQCmatlab/article/details/115361774