基于遗传算法/贪心算法的移栽路径优化研究(附全部MATLAB代码)

本文四种算法源码下载地址:【免费】遗传算法/贪心算法/固定次序法移植路径规划MATLAB代码资源-CSDN文库        

补苗移栽和稀植移栽是温室多叶移栽的两种主要作业方式。补苗移栽是将穴盘中的空 穴或劣质幼苗替代为健康幼苗以保证幼苗出厂的一致性,能减少因缺苗导致的大田移栽的 漏种;稀植移栽是将密集穴盘内的幼苗移栽到稀疏穴盘内,使幼苗有更多的养分和更大的 生长空间。移栽机的末端执行器在执行移栽任务时,大部分时间穿梭于供苗穴盘和目标穴 盘之间,在不改变机器结构和成本的情况下,可通过移栽路径规划缩短移动距离来提高移栽效率。本文的代码实现了类似于TSP旅行商问题的移植算法。

代码:

rng default
rng (1)
%% order
sizes = 500/8; % 格子Y轴长度
size2 = 250/4; % X轴长度
end_base =[30 30]; % 左边偏移
end_width = 4; % X轴格子个数
end_long = 8; % Y轴格子个数
end_num = end_width*end_long;
end_emptynum = 12; % 缺苗个数
end_v = ones(1,end_num); end_v(randi(end_num,1,end_emptynum))=0; end_Mat = reshape(end_v,end_long,end_width); % 矩阵
%end_Mat=[1 1 1 1;1 0 1 1;1 0 1 1;1 1 1 1] 指定的话直接覆盖矩阵就行。
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]; % 所有缺苗位置
        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]; % 所有缺苗位置
        end
    end
end


start_base =[380 30];% 右边偏移
start_width = 4; % 补苗区X轴格子个数
start_long = 8; % 补苗区Y轴格子个数
start_num = start_width*start_long;
start_emptynum = 12;% 补苗区空格个数
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]; % 所有可使用苗的数目
        end
    end
end

% 线框位置计算
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);
% 线框位置计算
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]; % 起点
for i = 1:data.end_num
    POS_ORDER=[POS_ORDER;start_pos_temp(i,:);end_pos(i,:);]; % 添加
end
POS_ORDER=[POS_ORDER;0 0]; % 回到起点
toc
figure(3)
% 绘制结果
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('固定次序法距离和')
title(['固定次序算法 总位移:' num2str(sum(dist_ORDER))])
disp(sum(dist_ORDER))
disp('固定次序距离')
disp(dist_ORDER)
disp('固定次序位置')
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

主函数代码:

rng default
rng (1)
%%
sizes = 500/8; % 格子Y轴长度
size2 = 250/4; % X轴长度
end_base =[30 30]; % 左边偏移
end_width = 4; % X轴格子个数
end_long = 8; % Y轴格子个数
end_num = end_width*end_long;
end_emptynum = 8; % 缺苗个数
end_v = ones(1,end_num); end_v(randi(end_num,1,end_emptynum))=0; end_Mat = reshape(end_v,end_long,end_width); % 矩阵
%end_Mat=[1 1 1 1;1 0 1 1;1 0 1 1;1 1 1 1] 指定的话直接覆盖矩阵就行。
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]; % 所有缺苗位置
        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]; % 所有缺苗位置
        end
    end
end


start_base =[380 30];% 右边偏移
start_width = 4; % 补苗区X轴格子个数
start_long = 8; % 补苗区Y轴格子个数
start_num = start_width*start_long;
start_emptynum = 12;% 补苗区空格个数
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]; % 所有可使用苗的数目
        end
    end
end

% 线框位置计算
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);
% 线框位置计算
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
% 绘制背景
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
% 遗传算法参数
dim = data.end_num+data.select_num;
lb = zeros(1,dim); % 下界
ub = ones(1,dim); % 上界
NP = 200; % 种群数目
maxiter = 200; % 迭代次数
Pc = 0.8; % 交叉概率
Pm = 0.1; % 变异概率
Pe = 0.1; % 精英选择比例
tic
res = GA(NP,dim,maxiter,@callfit,lb,ub,data,Pc,Pm,Pe); % 调用遗传优化
toc
figure(2) % 绘制迭代图
plot(res.trace,'LineWidth',1.6);
xlabel('Iteration')
ylabel('Fitness')
[dist,RES]=callfit(res.X,data)

figure(1) % 绘制遗传结果
plot(RES.trace(:,1),RES.trace(:,2),'-.*','LineWidth',1.6);
plot(noneed_pos(:,1),noneed_pos(:,2),'blacko','MarkerSize',15)
disp('遗传算法距离和')
disp(dist)
disp('遗传算法距离')
disp(RES.distlist)
disp('遗传算法位置')
disp(RES.trace)
title(['遗传算法 总位移:' num2str(dist)])
for i = 1:size(RES.trace,1)-1
    text(RES.trace(i,1),RES.trace(i,2),num2str(i));
end

其完整的代码见个人发布资源:

【免费】遗传算法/贪心算法/固定次序法移植路径规划MATLAB代码资源-CSDN文库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值