给下面代码加上中文注释：clear
clc
close all
%% 车间参数
L = 70;
W = 100;
gap = 1;
LW0 = xlsread('数据.xlsx',1,'A2:B11');    % 设施的长宽
T = xlsread('数据.xlsx',2,'B2:K11');       % 非物流关系密切等级
F = xlsread('数据.xlsx',3,'B2:K11');       % 搬运量
C = xlsread('数据.xlsx',4,'B2:K11');       % 物流成本
Gnum = size(LW0,1);              % 设施数量
dim = 2 * Gnum;
p1 = [58        84
    32  63
    14  87
    43  94
    8.5 53
    35  38
    61  26
    53  7
    28  12
    7   12
    ];
p2 = [50        62
    61  81
    34  77
    34  62
    8.5 65
    36  42
    11  25
    11  7
    34  14
    57  8
    ];
y1 = [0 0 0 1 0 0 0 0 0 0];
y2 = [0 0 0 1 0 0 0 0 1 1];
%% 遗传算法参数
NP = 100;             % 种群大小
maxgen = 800;        % 最大进化代数
Pc = 0.8;             % 交叉概率
Pm = 0.2;             % 变异概率
Pe = 0.9;             % 代沟(Generation gap)
minbound = zeros(1,dim);   % 变量下边界
maxbound = L * ones(1,dim);    % 变量上边界
big = 1000000;
%% 初始化种群:在满足最小间距的条件下，依次放置，保证初始种群满足约束
X = zeros(NP,dim);    % 矩形中心坐标编码
Y = zeros(NP,Gnum);   % 矩形长宽方向编码
trysteps = 10000;
% 1
LW = LW0;
for j = 1 : Gnum
    if y1(j) == 1
        LW(j,1:2) = LW0(j,2:-1:1);
    end
end
Pg = p1;
cla
for j = 1 : Gnum
    rectangle('Position',[Pg(j,1)-LW(j,1)/2,Pg(j,2)-LW(j,2)/2,LW(j,1),LW(j,2)],'EdgeColor','r')
    text(Pg(j,1),Pg(j,2),num2str(j))
    hold on
end
axis equal
axis([0 L 0 W])
box on
title('设施初始分布图')
figure
% 2
LW = LW0;
for j = 1 : Gnum
    if y2(j) == 1
        LW(j,1:2) = LW0(j,2:-1:1);
    end
end

Pg = p2;
cla
for j = 1 : Gnum
    rectangle('Position',[Pg(j,1)-LW(j,1)/2,Pg(j,2)-LW(j,2)/2,LW(j,1),LW(j,2)],'EdgeColor','r')
    text(Pg(j,1),Pg(j,2),num2str(j))
    hold on
end
axis equal
axis([0 L 0 W])
box on
title('设施初始分布图')
X(1,:) = p1(:)';
Y(1,:) = y1;
X(2,:) = p2(:)';
Y(2,:) = y2;
tic
for i = 3 : NP
    disp(['初始化进程' num2str(i) '/' num2str(NP)])
    trying = 1;
    while trying       % 如果多次尝试摆放仍不能满足约束，则重新开始摆放
        trying = 0;
        y = round(rand(1,Gnum));
        LW = LW0;
        for j = 1 : Gnum
            if y(j) == 1
                LW(j,1:2) = LW0(j,2:-1:1);
            end
        end
        % 放入第一个矩形，直至满足约束
        flag = 0;
        while ~flag
            p = [L * rand,W * rand];
            flag = isInRange(p,LW(1,:),L,W,gap);
        end
        P = p;
        % 依次放入其他矩形
        for k = 2 : Gnum
            dtn = 1;
            step = 0;     % 试探次数，如果经过很多次试探无法成功，说明第一个摆放有问题
            while dtn > 0
                flag = 0;
                while ~flag        % 保证放置的位置满足边界限制
                    p = [L * rand,W * rand];
                    flag = isInRange(p,LW(k,:),L,W,gap);
                end
                Pt = [P ;p];
                LWt = LW(1:k,:);
                d = CalDistance(Pt,LWt,big,gap);  % 计算已放置的设施之间的距离及是否满足间隔
                dtag = (d == big);
                dtn = sum(sum(dtag));     % 不满足间隔要求的数量
                step = step + 1;
                if step > trysteps
                    trying = 1;
                    break
                end
            end
            P = [P; p];
        end
    end
    X(i,:) = P(:)';
    Y(i,:) = y;
end
toc
figure
Pg = P;
cla
for j = 1 : Gnum
    rectangle('Position',[Pg(j,1)-LW(j,1)/2,Pg(j,2)-LW(j,2)/2,LW(j,1),LW(j,2)],'EdgeColor','r')
    text(Pg(j,1),Pg(j,2),num2str(j))
    hold on
end
axis equal
axis([0 L 0 W])
box on
title('设施初始随机分布图')
%% 遗传进化
Fx = zeros(NP,1);
for i = 1 : NP
    p = round(100 * [X(i,1:Gnum); X(i,Gnum+1:end)]') / 100;
    y = Y(i,:);
    LW = LW0;
    for j = 1 : Gnum
        if y(j) == 1
            LW(j,1:2) = LW0(j,2:-1:1);
        end
    end
    D = CalDistance(p,LW,big,gap);
    Fx(i,1) = Fitness(p,LW,D,F,C,T,L,W,big,gap);      % 计算目标函数值
end
gen = 1;
while gen <= maxgen
    gen
    % 计算适应度
    fit = 1 ./ (Fx+1);   % 将求最小转为最大值fit
    % 选择
    XSel = Select(X,fit,Pe);
    YSel = Select(Y,fit,Pe);
    % 交叉操作
    XSel = XCross(XSel,Pc);
    YSel = YCross(YSel,Pc);
    % 变异
    XSel = XMutate(XSel,Pm,minbound,maxbound);
    YSel = YMutate(YSel,Pm);
    % 重插入子代的新种群
    X = Reins(X,XSel,fit);
    Y = Reins(Y,YSel,fit);
    for i = 1 : NP
        p = round(100 * [X(i,1:Gnum); X(i,Gnum+1:end)]') / 100;
        y = Y(i,:);
        LW = LW0;
        for j = 1 : Gnum
            if y(j) == 1
                LW(j,1:2) = LW0(j,2:-1:1);
            end
        end
        D = CalDistance(p,LW,big,gap);
        Fx(i,1) = Fitness(p,LW,D,F,C,T,L,W,big,gap);      % 计算目标函数值
    end
    % 记录各代最优值
    fgbest = min(Fx);
    FG(gen) = fgbest;
    % 更新迭代次数
    gen=gen+1 ;
end
%% 画出最优解的路线图
clc
[fgbest,minInd] = min(Fx);
xgbest = X(minInd(1),:);
ygbest = Y(minInd(1),:);
figure
plot(FG)
xlabel('迭代次数')
ylabel('最优目标函数值')
title('遗传算法迭代曲线')
figure
Pg = round(100 * [xgbest(1:Gnum); xgbest(Gnum+1:end)]') / 100;
LW = LW0;
for j = 1 : Gnum
    if ygbest(j) == 1
        LW(j,1:2) = LW0(j,2:-1:1);
    end
end
D = CalDistance(Pg,LW,big,gap);
[ff,T1,T2] = Fitness(Pg,LW,D,F,C,T,L,W,big,gap);   %
fprintf('目标函数1的最优值为 %f \n', T1)
fprintf('目标函数2的最优值为 %f \n', T2)
disp('各区域中心对应坐标值为')
disp(Pg)
for i = 1 : Gnum
    rectangle('Position',[Pg(i,1)-LW(i,1)/2,Pg(i,2)-LW(i,2)/2,LW(i,1),LW(i,2)],'EdgeColor','r')
    text(Pg(i,1),Pg(i,2),num2str(i))
    hold on
end
axis equal
axis([0 L 0 W])
box on
title('优化后设施布局位置')