Research on transplanting path optimization based on genetic algorithm/greedy algorithm (with all MATLAB codes attached)

The source code download address for the four algorithms in this article: [Free] Genetic Algorithm/Greedy Algorithm/Fixed Order Method Transplantation Path Planning MATLAB Code Resources-CSDN Library

Seedling transplanting and sparse transplanting are the two main methods of multi-leaf transplanting in greenhouses. Seedling transplanting is to replace holes or poor-quality seedlings in plug trays with healthy seedlings to ensure the consistency of seedlings leaving the factory, and can reduce missed seeds in field transplants due to lack of seedlings; sparse transplanting is to replace dense seedlings in plug trays with Transplant the seedlings into sparse plug trays so that the seedlings have more nutrients and a larger space for growth. When performing transplanting tasks, the end effector of the transplanter spends most of its time shuttling between the supply plug tray and the target plug tray. Without changing the structure and cost of the machine, the moving distance can be shortened through transplanting path planning. to improve transplanting efficiency. The code in this article implements a transplantation algorithm similar to the TSP traveling salesman problem.

Code:

rng default
rng (1)
%% order
sizes = 500/8; % grid Y-axis length
size2 = 250/4; % X-axis length
end_base =[30 30]; % left offset
end_width = 4; % X-axis grid number
end_long = 8; % Y-axis grid number
end_num = end_width*end_long;
end_emptynum = 12; % number of missing seedlings
end_v = ones(1,end_num); end_v(randi(end_num,1,end_emptynum))=0; end_Mat = reshape(end_v,end_long,end_width); % matrix
%end_Mat=[1 1 1 1;1 0 1 1;1 0 1 1;1 1 1 1] If specified, just overwrite the matrix directly.
end_pos = [];
noneed_pos=[];
for i = 1:end_long
    for j = 1:end_width
        if end_Mat(i,j)==0
            pos = end_base + [(j-1 + 0.5)*size2 (end_long-i + 0.5)*sizes ];
            end_pos=[end_pos;pos]; % all positions lacking seedlings
        end
        if end_Mat(i,j)==1
            pos = end_base + [(j-1 + 0.5)*size2 (end_long-i + 0.5)*sizes ];
            noneed_pos=[noneed_pos;pos]; % All positions lacking seedlings
        end
    end
end


start_base =[380 30];% right offset
start_width = 4; % Number of X-axis grids in the seedling patching area
start_long = 8; % The number of Y-axis grids in the seedling repair area
start_num = start_width*start_long;
start_emptynum = 12;% Number of spaces in the patching area
start_v = ones(1,start_num); start_v(randi(start_num,1,start_emptynum))=0; start_Mat = reshape(start_v,start_long,start_width);
start_pos=[];

for i = 1:start_long
    for j = 1:start_width
        if start_Mat(i,j)==1
            pos = start_base + [(j-1 + 0.5)*size2 (start_long-i + 0.5)*sizes ];
            start_pos=[start_pos;pos]; % The number of all available seedlings
        end
    end
end

% Wireframe position calculation
line1=[];line2=[];
for i = 1:end_long + 1
    line1=[line1;end_base + [0 (i-1)*sizes]];
    line2=[line2;end_base + [size2*end_width (i-1)*sizes]];
end
 for j = 1:end_width + 1
     line1=[line1;end_base + [(j-1)*size2 0]];
     line2=[line2;end_base + [(j-1)*size2 sizes*end_long]];
 end
data.start_pos=start_pos;
data.end_pos=end_pos;
data.end_num = size(end_pos,1);
data.select_num = size(start_pos,1);
% Wireframe position calculation
for i = 1:start_long + 1
    line1=[line1;start_base + [0 (i-1)*sizes]];
    line2=[line2;start_base + [size2*start_width (i-1)*sizes]];
end
 for j = 1:start_width + 1
     line1=[line1;start_base + [(j-1)*size2 0]];
     line2=[line2;start_base + [(j-1)*size2 sizes*start_long]];
 end
tic
start_pos_temp=start_pos;
POS_ORDER=[0 0]; % starting point
for i = 1:data.end_num
    POS_ORDER=[POS_ORDER;start_pos_temp(i,:);end_pos(i,:);]; % Add
end
POS_ORDER=[POS_ORDER;0 0]; % Return to starting point
toc
figure(3)
% draw results
hold on
plot(end_pos(:,1),end_pos(:,2),'blues','MarkerSize',15)
plot(start_pos(:,1),start_pos(:,2),'blueo','MarkerSize',15)
plot(POS_ORDER(:,1),POS_ORDER(:,2),'-.*','LineWidth',1.6);
plot(noneed_pos(:,1),noneed_pos(:,2),'blacko','MarkerSize',15)
for i = 1:size(line1,1)
plot([line1(i,1) line2(i,1)],[line1(i,2) line2(i,2)],'black-');
end
for i = 1:size(POS_ORDER,1)-1
    text(POS_ORDER(i,1),POS_ORDER(i,2),num2str(i));
end

dist_ORDER=sqrt(sum((POS_ORDER(2:end,:)-POS_ORDER(1:end-1,:)).^2,2));
disp('fixed order distance sum')
title(['Fixed order algorithm total displacement:' num2str(sum(dist_ORDER))])
disp(sum(dist_ORDER))
disp('fixed order distance')
disp(dist_ORDER)
disp('fixed order position')
disp(POS_ORDER)
figure(4)
hold on
plot(start_pos(:,1),start_pos(:,2),'redo','MarkerSize',15)
plot(noneed_pos(:,1),noneed_pos(:,2),'blacko','MarkerSize',15)
for i = 1:size(line1,1)
plot([line1(i,1) line2(i,1)],[line1(i,2) line2(i,2)],'black-');
end

Main function code:

rng default
rng (1)
%%
sizes = 500/8; % grid Y-axis length
size2 = 250/4; % X-axis length
end_base =[30 30]; % left offset
end_width = 4; % X-axis grid number
end_long = 8; % Y-axis grid number
end_num = end_width*end_long;
end_emptynum = 8; % number of missing seedlings
end_v = ones(1,end_num); end_v(randi(end_num,1,end_emptynum))=0; end_Mat = reshape(end_v,end_long,end_width); % matrix
%end_Mat=[1 1 1 1;1 0 1 1;1 0 1 1;1 1 1 1] If specified, just overwrite the matrix directly.
end_pos = [];
noneed_pos=[];
for i = 1:end_long
    for j = 1:end_width
        if end_Mat(i,j)==0
            pos = end_base + [(j-1 + 0.5)*size2 (end_long-i + 0.5)*sizes ];
            end_pos=[end_pos;pos]; % all positions lacking seedlings
        end
        if end_Mat(i,j)==1
            pos = end_base + [(j-1 + 0.5)*size2 (end_long-i + 0.5)*sizes ];
            noneed_pos=[noneed_pos;pos]; % All positions lacking seedlings
        end
    end
end


start_base =[380 30];% right offset
start_width = 4; % Number of X-axis grids in the seedling patching area
start_long = 8; % The number of Y-axis grids in the seedling repair area
start_num = start_width*start_long;
start_emptynum = 12;% Number of spaces in the patching area
start_v = ones(1,start_num); start_v(randi(start_num,1,start_emptynum))=0; start_Mat = reshape(start_v,start_long,start_width);
start_pos=[];

for i = 1:start_long
    for j = 1:start_width
        if start_Mat(i,j)==1
            pos = start_base + [(j-1 + 0.5)*size2 (start_long-i + 0.5)*sizes ];
            start_pos=[start_pos;pos]; % The number of all available seedlings
        end
    end
end

% Wireframe position calculation
line1=[];line2=[];
for i = 1:end_long + 1
    line1=[line1;end_base + [0 (i-1)*sizes]];
    line2=[line2;end_base + [size2*end_width (i-1)*sizes]];
end
 for j = 1:end_width + 1
     line1=[line1;end_base + [(j-1)*size2 0]];
     line2=[line2;end_base + [(j-1)*size2 sizes*end_long]];
 end
data.start_pos=start_pos;
data.end_pos=end_pos;
data.end_num = size(end_pos,1);
data.select_num = size(start_pos,1);
% Wireframe position calculation
for i = 1:start_long + 1
    line1=[line1;start_base + [0 (i-1)*sizes]];
    line2=[line2;start_base + [size2*start_width (i-1)*sizes]];
end
 for j = 1:start_width + 1
     line1=[line1;start_base + [(j-1)*size2 0]];
     line2=[line2;start_base + [(j-1)*size2 sizes*start_long]];
 end
% draw background
figure(1)
hold on
plot(end_pos(:,1),end_pos(:,2),'blacks','MarkerSize',15)
plot(start_pos(:,1),start_pos(:,2),'blueo','MarkerSize',15)
for i = 1:size(line1,1)
plot([line1(i,1) line2(i,1)],[line1(i,2) line2(i,2)],'black-');
end
% Genetic algorithm parameters
dim = data.end_num + data.select_num;
lb = zeros(1,dim); % lower bound
ub = ones(1,dim); % upper bound
NP = 200; % population number
maxiter = 200; % number of iterations
Pc = 0.8; % crossover probability
Pm = 0.1; % mutation probability
Pe = 0.1; % elite selection ratio
tic
res = GA(NP,dim,maxiter,@callfit,lb,ub,data,Pc,Pm,Pe); % call genetic optimization
toc
figure(2) % draw iteration diagram
plot(res.trace,'LineWidth',1.6);
xlabel('Iteration')
ylabel('Fitness')
[dist,RES]=callfit(res.X,data)

figure(1) % Plot genetic results
plot(RES.trace(:,1),RES.trace(:,2),'-.*','LineWidth',1.6);
plot(noneed_pos(:,1),noneed_pos(:,2),'blacko','MarkerSize',15)
disp('Genetic algorithm distance sum')
disp(dist)
disp('genetic algorithm distance')
disp(RES.distlist)
disp('Genetic Algorithm Position')
disp(RES.trace)
title(['Genetic algorithm total displacement:' num2str(dist)])
for i = 1:size(RES.trace,1)-1
    text(RES.trace(i,1),RES.trace(i,2),num2str(i));
end

For its complete code, please see the personal release resources:

[Free] Genetic Algorithm/Greedy Algorithm/Fixed Order Method Transplantation Path Planning MATLAB Code Resources-CSDN Library

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 56221 people are learning the system