Nurse scheduling problem based on genetic algorithm (with Matlab source code)

Catalog

Preface

1. Scheduling issues

2. Genetic Algorithms

1. Background introduction

2. Encoding

summary



Preface

        For the learning of genetic algorithm, I believe that many beginners like me start with the simplest binary function to find the extreme value. Understand population number, gene length, number of iterations, probability of cross-variation, generation of initial population, and conversion of binary encoding to decimal encoding step by step. After my own learning and debugging, I designed an algorithm for the nursing staff scheduling problem.

         The problem of nurses'shifting can affect whether a department can meet the operational needs and the need to ensure the rest and work efficiency of nurses when there are people working for 24 hours. A good shift must take into account the job requirements and meet the staff needs. This paper uses genetic algorithm to set up a nurses'schedule and input the actual constraints to provide a schedule that meets the needs.



1. Scheduling issues

         In the nursing staff scheduling, the most ideal scheme is to meet all the constraints, and the working hours of each nurses on duty every day and every week, so the basis of the scheduling scheme depends on the satisfaction of the constraints. Restrictions in the nursing staff schedule can be divided into two categories: hard constraints or invalid schedules if they are not met, such as a nurse who has completed the night shift must rest the next day, or she will be overtired; The other is soft constraints, which can be satisfied as much as possible, such as taking two days off at least one day after a 16-hour night shift. When considering the constraints of scheduling, we should also take into account the satisfaction of each nurse as much as possible, such as 40 hours of work a week, minimizing rest and ensuring a balanced weekly rest.

         Although the ideal solution is to satisfy all the hard and soft constraints, in the actual constraints fitness function settings, it may be because of my ignorance and inappropriate function design, leading to the failure to achieve the ideal value, only to pursue the optimization as far as possible. In the actual scheduling process, two constraint fitness functions y1 and y2 are set in this scheme.


2. Genetic Algorithms

1. Background introduction

         There are 16 nurses in the emergency department of a hospital, the first 12 are old staff, the last 4 are new staff, there are six types of shift every day: morning shift (8:00 - 16:30) 4 types: A1 (guide desk), A2 (rescue room), A3 (observation room), A4 (peripheral); Night shift (16:30 - 24:00) 1 type: P Gang; Night shift (16:30 - 8:00) 1 type: P+N.

         Early A1-A4 requires at least four nurses, of which A2 and A4 are best staffed by two nurses. Night nurses only assist the night shift and night shift requires two nurses. The following restrictions were determined: (1) One nurse must rest the next day after completing the night shift on a certain day; (2) Only one new person comes to the evening shift every day to become familiar with the night shift; (3) Only two old employees are required to work overnight every day; (4) A1-A4 are all at least one person on the job, of which A2 and A4 are more important positions, it is best to have two employees on the job; (5) Take at least one day off after night shift, preferably two days off; _Minimum part-time breaks, standard shift is 5 shifts per person per week, one night shift is 2 shifts

2. Encoding

         In the genetic algorithm, the chromosome coding is determined first, where a chromosome represents a schedule, where every three binary codes represent a shift: 1000 and 111 for rest days, 001, 010, 011, 100 for A1-A4, 101 for night shift, 110 for night shift. (Here, two genes are used to express rest days in order to satisfy double rest as much as possible)

         Therefore, for 16 nurses a day, 7 days a week, a chromosome is made up of 7*16 = 112 nurses'genomes, each of which is made up of a compound gene, followed by three shift genomes. Therefore, Monday's gene composition in a schedule is shown in Figure 1, which consists of 16*3=48-bit binary encoding. By analogy, a weekly schedule consists of a 7*16*3 = 336-bit code stain.

       

          Say nothing but code directly:

clear all
close all

pop = 50; %Initial population number
length = 336; %Population gene coding length,Sixteen shifters per day, seven days a week, totaling seven shifts (represented by a three-digit binary code)
gen = 1000; %Number of iterations
crossover_probablity = 0.9; %Crossover probability
variation_probablity = 0.8; %Probability of Variation
initial_pop = round(rand(pop,length)); %Generate Initial Population

%Algorithmic Iteration m second
for m=1:gen
    %Converting a binary population of 336 chromosome lengths per generation to 16*7 Matrix (16 nurses, 7 days a week)
    x = zeros(16,7,pop)
    for i = 1:size(initial_pop,1) %Traverse 50 schedules
        for j = 1:7  %Traverse seven days a week
            for k = 1:16 %Traverse 16 shifters
                for l = 1:3 %Binary shift to decimal
                    x(k,j,i) = initial_pop ( i,48*(j-1)+3*(k-1)+l ) * 2^(3-l) + x(k,j,i);
                end
            end      
            % Genes 111 and 000 exhibit both 0 phenotypes (increasing the likelihood of rest)
            x(x==7) = 0;
        end
        
        %Join Rest Constraint After Night Shift
        for j = 1:6  %Traverse seven days a week
            for k = 1:16 %Traverse 16 shifters
                if x(k,j,i)==6
                    x(k,j+1,i)=0;
                end
            end
        end
        
    end
 
    %Daily shift requirement, fit y1_day Calculation:
      %Hard Constraint y11: Only one 5 in the new person, two 6 in the old person
      %Soft Constraints y12: 1,2,3,4 At least one,2,4 Best case is 2
    y1_day = zeros(pop,7);%Daily fitness
    y11_day = zeros(pop,7);
    y12_day = zeros(pop,7);
    for i=1:size(initial_pop,1)
        for j=1:7
            %Hard Constraint y11,Set a higher weight
            y11_day(i,j) = 1/( (sum(x(13:16,j,i)==5) - 1)^2 + ( sum(x(1:12,j,i)==6) - 2 )^2 + ( sum(x(13:16,j,i)==6) )^2 + ( sum(x(1:12,j,i)==5) )^2 + 1);  
            %Soft Constraint 12, set lower weight
            y12_day(i,j) = 1/( ( sum(x(:,j,i)==1) - 1 )^2 + ( sum (x(:,j,i)==2) - 2 )^2 + ( sum (x(:,j,i)==3) -1 )^2 + ( sum (x(:,j,i)==4) - 2 )^2 + 1 );
            y1_day(i,j) = y11_day(i,j)*0.8 + y12_day(i,j)*0.2;
        end
    end
    
    %y1_day For daily fitness (max. 1), y1 For weekly fitness (theoretical maximum 7)
    for i=1:size(initial_pop,1)
        y1(i,1) = sum(y1_day(i,:));
    end
    
    % One day off after night shift, fit y2 Calculation:
    % Soft Constraint: Take at least one day off after night shift, preferably two days off
    y2_person = ones(pop,k);
    for i = 1:size(initial_pop,1) 
        for k = 1:16
            for j = 1:5  %Night shift from Monday to Friday
                if x(k,j,i) == 6 
                    % Penalty function if night shift is to continue working after Monday to Friday
                    y2_person(i,k) = y2_person(i,k) - 1/5 *( x(k,j+2,i)~=0 );
                end
            end
        end
    end
    
    %y2_person For each person's fitness (maximum 1), y2 Fit for the entire schedule (theoretical maximum 16)
    for i=1:size(initial_pop,1)
        y2(i,1) = sum( y2_person(pop,:) );
    end
    
    % y1 It's easy to get to the maximum, y2 It's difficult, so give it a bigger weight
    y = 0.1*y1 + 0.9*y2;
    
    %Finding the best genes in the population
    [a,b] = max(y);
    
    fit1=y/sum(y); %?Calculate the proportion of fitness to total fitness for each population
    fit2=cumsum(fit1); %?Each location is a cumulative proportion of all previous locations (it can be understood that each population occupies a position within a segment with a total length of 1, and a more adaptable population occupies a long position).
    
    %Gene Selection
    choose=sort(rand(pop,1)); %Ordered Random Number Sequence
    k=1;
    i=1;
    while k<=pop
        if choose(k)<fit2(i) %Random number less than population
            choosen_population(k,:)=initial_pop(i,:);
            k=k+1;
        else
            i=i+1;
        end
    end
    
    %Gene crossover
    for i=1:2:pop-1
        if rand<crossover_probablity
            % Selecting gene crossover length
            crossover_length=round(rand*(length-1))+1;
            % Example: Random Generation length The length is 19, that is, 19 first lines cross the second line to produce a new offspring, with the result on the first line
            crossover_population(i,:)=[choosen_population(i,1:crossover_length),choosen_population(i+1,crossover_length+1:end)];
            % The first 19 of the second line cross with the last one to produce a new offspring, with the result on the second line
            crossover_population(i+1,:)=[choosen_population(i+1,1:crossover_length),choosen_population(i,crossover_length+1:end)];
        end
    end
    
    %Gene Variation
    variation_population=crossover_population; 
    for i=1:pop
        if rand<variation_probablity
            variation_location=round(rand*(length-1))+1;
            variation_population(i,variation_location)=1-variation_population(i,variation_location);
        end
    end
    
    variation_population(end,:)=initial_pop(b,:); %?Keep the optimal population in this iteration
    initial_pop=variation_population; %Complete the iteration by selecting, crossing, and mutating the population as the initial population for the next generation
    best(m,1)=y(b); % Record No. m Optimal Function Value of Generation
    best(m,2) = y1(b);
    best(m,3) = y2(b);
    
end



plot(1:size(best,1),best(:,1)) 



summary

         The code notes are detailed, and the results of the algorithm show that most of the constraints reach the ideal value. Some of the constraints may not be optimal due to improper fit function settings or conflicts with other constraints. Only one result can be obtained as large as possible.

Tags: MATLAB Algorithm

Posted on Fri, 12 Nov 2021 11:46:27 -0500 by ShadowBlade72