A brief introduction to genetic algorithms

1 the introduction Genetic algorithm theory2.1 Biological basis of genetic algorithm 2.2 Theoretical basis of genetic algorithm 2.3 Basic concepts of genetic algorithm 2.4 Standard genetic algorithm 2.5 Characteristics of genetic algorithm 2.6 Improvement direction of genetic algorithm 3. Genetic algorithm flow 4 Key Parameters

Two, some source code

close all;
clear;
clc;
global MIN_START_TIME
global MAX_END_TIME
MIN_START_TIME = 8;
MAX_END_TIME = 18;

%===============================%
gnmax = 50; % Max algebra PC =0.8; % cross probability PM =0.1; % variation probability pop_size =10; % Initialize population eventStartTime =10; Moment of dynamic events in % % = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = % [job, mac_num] = read_data ('hospitaldata.xlsx'); [mac, mac_state] = creat_machine(mac_num); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- % strategy first in first out % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- pop = inipop(1, job, mac_num);
[f_mac_time, f_mac_serial, f_mac_start, f_mac_end] = decode(pop, job, mac_num);
draw_gantt(f_mac_time, f_mac_serial, f_mac_start, f_mac_end, job, mac, 0);
fprintf('FIFO completion time :%.2f\n',f_mac_time);
f_job_wait = job_waittime(f_mac_serial, f_mac_start, f_mac_end, job, mac);
fprintf('FIFO user waiting time :\n'); F_job_wait % input: decode % output: Two-dimensional matrix, the number of rows I corresponds to the ith element in the original code, the first column corresponds to the workpiece, Function [max_MAC_time, MAC_serial, MAC_start,mac_end]=decode(code, job, mac_num) mac = creat_machine(mac_num); n=length(code)/2; % total number of procedures % decode to generate work sequence, two-dimensional matrix, the I th procedure of the I th behavior coding, the first column of the workpiece code, the second column of the procedure code job_serial=[];for i=1:n
    job_serial(end+1,:)=[code(i),sum(code(1:i)==code(i))]; End % Decode to generate machine sequence, cell group, the i-th cell group is the machine code, the internal process vector according to the sequence, the process format is job_serial format % decode to generate the start and end time of each process of the machine, the start and end time of the workpiece MAC_serial =cell(1,sum(mac_num));
job_start=cell(1,length(job));
job_end=cell(1,length(job));
mac_start=cell(1,sum(mac_num));
mac_end=cell(1,sum(mac_num));
overtime = [];
for i=1The_mac the_MAC_type =job{job_serial(I,1)}{job_serial(i,2)} (2);
    job_in_mac_code=0;
    for j=1:job_serial(i,1)- 1
        job_in_mac_code=job_in_mac_code+length(job{j});
    end
    job_in_mac_code=job_in_mac_code+job_serial(i,2)+n; the_mac=mac{the_mac_type}(code(job_in_mac_code)); % find the work start time (not the real start time)if job_serial(i,2)= =1
        job_start{job_serial(i,1)} (1) = job{job_serial(i,1)} {1} (3); % Earliest start timeelse
        job_start{job_serial(i,1)}(end+1)=job_end{job_serial(i,1)}(job_serial(i,2)- 1); End % insert the i-th operation into the machine for processing. If the machine interval is suitable, insert the interval. If not, insert the last bit into the machine. [mac_start{the_MAC}, MAC_end {the_MAC},job_end_time,insert_pot] = insert_MAC (... mac_start{the_mac},... mac_end{the_mac},... job_start{job_serial(i,1)}(job_serial(i,2)),...
        job_end{job_serial(i,1)},...
        job{job_serial(i,1)}{job_serial(i,2)} (1),...
        job{job_serial(i,1)}{job_serial(i,2)} (3),...
        job{job_serial(i,1)}{job_serial(i,2)} (4),...
        true);
    if insert_pot > 0
        if insert_pot == 1
            mac_serial{the_mac} = [job_serial(i,:); mac_serial{the_mac}];
        else
            mac_serial{the_mac} = [mac_serial{the_mac}(1:insert_pot- 1, :); job_serial(i,:); mac_serial{the_mac}(insert_pot:end,:)]; end job_end{job_serial(i,1)}(end+1)=job_end_time;
    else
        overtime(end+1,:) = job_serial(i,:);
        job_end{job_serial(i,1)}(end+1)=job_end_time; End end % Global MAX_END_TIME max_MAC_time =0;
if ~isempty(overtime)
    max_mac_time = MAX_END_TIME;
else
    for i=1:sum(mac_num)
        if~isempty(mac_end{i}) max_mac_time=max(max_mac_time,max(mac_end{i})); end end end end function [flag, max_mac_time, up_mac_serial, up_mac_start, up_mac_end, update_job, update_mac, over_time_job] = dynamin(... mac_serial, mac_start, mac_end,... add_job, delete_job, add_mac, delete_mac,... EventStartTime, Job, MAC, MAC_state) % Dynamin % Based on the dynamic change information of the job and MAC, Update mac_serial, MAC_start, MAC_END, and job lists. % Jobs that need to be processed after dynamic events are sentlist, the first column is the job number, the second column is the corresponding operation number, and the third column is the earliest start time todo_job_list = []; % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - %1.To delete a patient follow-up stage % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --if ~isempty(delete_job)
    [num, ~] = size(delete_job);
    for i = 1:num
        [first, second] = find_job_coord(mac_serial, delete_job(i,:));
        if first>0
            temp_time = mac_start{first}(1,second);
            if temp_time > eventStartTime
                flag = true;
                [mac_serial, mac_start, mac_end] = delete_task(first, second, mac_serial, mac_start, mac_end);
                Tot = length(job{delete_job(i,1)});
                if delete_job(i,2)<Tot
                    for n = delete_job(i,2) +1:Tot
                        [first, second] = find_job_coord(mac_serial, [delete_job(i,1),n]);
                        [mac_serial, mac_start, mac_end] = delete_task(first, second, mac_serial, mac_start, mac_end);
                    end
                end
            end
        end
    end
end

%-----------------------------------------------
% 2.New patients % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --if ~isempty(add_job)
    flag = true;
    N = length(job);
    for i = 1:length(add_job)
        job_start_time = eventStartTime;
        for j = 1:length(add_job{i})
            if j > 1
                job_start_time = job_start_time + add_job{i}{j- 1} (1);
            end
            todo_job_list(end+1, :) = [N+i, j, job_start_time];
        end
    end
    update_job = [job, add_job];
end

%-----------------------------------------------
% 3.Delete a medical resources % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --if ~isempty(delete_mac)
    [num, ~] = size(delete_mac);
    nb_mac = mac_max_num(mac);
    for i = 1:num % Checks whether the MAC ID to be deleted exceeds the rangeif delete_mac(i,1) > nb_mac
            continue;
        end
        
        mac_pos = delete_mac(i,1);
        [~, mac_state] = UpdateMac(mac, mac_state, mac_pos, 'D');
        [row, ~] = size(mac_serial{mac_pos});
        for j = 1:row
            if mac_start{mac_pos}(j) >= eventStartTime
                flag = true; tmptime = eventStartTime; {first}(second), eventStartTime); Tot = length(job{mac_serial{mac_pos}(j,1)});
                for n = mac_serial{mac_pos}(j,2):Tot
                    job_time = 0;
                    if n > mac_serial{mac_pos}(j,2)
                        job_time = job{mac_serial{mac_pos}(j,1)}{n- 1} (1);
                    end
                    todo_job_list(end+1, :) = [mac_serial{mac_pos}(j,1), n, tmptime+job_time];  
                end
            end  
        end
    end
end

%-----------------------------------------------
% 4.Add some medical resources % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --if ~isempty(add_mac)
    [row, ~] = size(add_mac);
    for i = 1:row
        [mac, mac_state] = UpdateMac(mac, mac_state, add_mac(i,:), 'A');
        mac_serial = [mac_serial, cell(1, add_mac(i,2))];
        mac_start = [mac_start, cell(1, add_mac(i,2))];
        mac_end = [mac_end, cell(1, add_mac(i,2))];
    end
end

%-----------------------------------------------
% todo listTo heavy, and delete the corresponding task, and then dynamically insert % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --if ~isempty(todo_job_list)
    todo_job_list = unique(todo_job_list, 'rows');
  
    up_mac_start = mac_start;
    up_mac_end = mac_end;
    update_mac = mac;
end

end


Copy the code

3. Operation results

Matlab version and references

1 matlab version 2014A

Intelligent Optimization Algorithm and its MATLAB Examples (2nd edition), baoyang, Yu Jizhou, Yang Shan, Publishing House of Electronics Industry