基于粒子群算法的容器艇和操作手购买方案优化
clc; clear; clear global global para_c_fix para_c_bao para_c_train para_r para_n_0 %% 数据输入 %容器艇与操作手价格 para_c_fix=[200 100]; %保养价格 para_c_bao=[10 5]; %训练价格 para_c_train=10; %需求数量 para_r=[11 5 4 7 16 6 5 7 13 6 5 7 12 5 4 6 9 5 5 11 29 21 17 20 27 13 9 10 16 6 5 7 11 5 5 6 12 7 7 10 15 10 9 11 15 10 10 16 26 21 23 36 50 45 45 49 57 43 40 44 52 43 42 45 52 41 39 41 48 35 34 35 42 34 36 43 55 48 54 65 80 70 74 75 101 89 88 90 100 87 88 89 104 89 89 90 106 96 94 99 109 99 96 102]; para_r=reshape(para_r',[],1); %初始数量 para_n_0=[13 50]; %% 开始优化 sizepop=500; dim=208; ger=3000; xlimit_max=ones(1,dim)-0.000001; xlimit_min=zeros(1,dim)+0.00001; vlimit_max=0.2*(xlimit_max-xlimit_min); vlimit_min =-1vlimit_max; c_1=0.8; c_2=0.5; c_3=0.5; pop_x=zeros(dim,sizepop); pop_v=zeros(dim,sizepop); for i=1:dim for j=1:sizepop pop_x(i,j)=(xlimit_min(i)+(xlimit_max(i)-xlimit_min(i))rand); pop_v(i,j)=(vlimit_min(i)+(vlimit_max(i)-vlimit_min(i))rand); end end gbest=pop_x; for j=1:sizepop %每周购买的容器艇数量 X_1=reshape(pop_x(1:104,j),[],1); %每周购买的操作手数量 X_2=reshape(pop_x(105:208,j),[],1); X_1(X_1<0.5)=0; X_1(X_1>0)=X_1(X_1>0)2-1; X_1=round(X_1100); X_2(X_2<0.5)=0; X_2(X_2>0)=X_2(X_2>0)2-1; X_2=round(X_2100); %当前总数 now_n=para_n_0; %当前操作挺数量-剩余时间 test_n1=[]; %当前操作手数量-剩余时间 test_n2=[]; test_n3=[]; %当前训练 train_num=0; %当前使用 use_num=[0 0]; %当前成本 fitness_gbest(j)=0; record_n=[]; for i=1:104 %训练新增 if length(test_n1)>0 temp_index=find(test_n1(:,2)==1); fitness_gbest(j)=fitness_gbest(j)+length(temp_index)para_c_bao(1); now_n(1)=now_n(1)+length(temp_index); test_n1(temp_index,:)=[]; end if length(test_n2)>0 temp_index=find(test_n2(:,2)==1); fitness_gbest(j)=fitness_gbest(j)+length(temp_index)para_c_train; now_n(2)=now_n(2)+length(temp_index); test_n2(temp_index,:)=[]; end if length(test_n3)>0 temp_index=find(test_n3(:,2)==1); fitness_gbest(j)=fitness_gbest(j)+length(temp_index)para_c_bao(2); now_n(2)=now_n(2)+length(temp_index); test_n3(temp_index,:)=[]; end now_n=now_n+train_num; train_num=0; %使用新增 now_n(1)=now_n(1)+use_num(1); for k=1:use_num(2) test_n3(end+1,:)=[1 1]; end %消耗 temp_con=para_r(i)[1 4]; record_n(end+1,:)=[i now_n]; if now_n(1)<temp_con(1) fitness_gbest(j)=fitness_gbest(j)+10^9abs(now_n(1)-temp_con(1)); end if now_n(2)<temp_con(2)/4 fitness_gbest(j)=fitness_gbest(j)+10^9abs(now_n(2)-temp_con(2)); end temp_con=[min(temp_con(1),now_n(1)) min(temp_con(2),now_n(2))]; now_n=now_n-temp_con; use_num=temp_con; %购买 fitness_gbest(j)=fitness_gbest(j)+X_1(i)para_c_fix(1); fitness_gbest(j)=fitness_gbest(j)+X_2(i)para_c_fix(2); for k=1:X_1(i) test_n1(end+1,:)=[1 1]; end for k=1:X_2(i) test_n2(end+1,:)=[1 10^9]; end %训练 left_n1=now_n(1); if length(test_n2)>0 temp_con=min([ceil(sum(test_n2(:,2)==10^9)/3),left_n1,now_n(2)]); temp_num=min([temp_con3,sum(test_n2(:,2)==10^9)]); test_n2=sortrows(test_n2,2); test_n2(1:temp_num,2)=1; now_n=now_n-temp_con; train_num=temp_con; end %其余保养 fitness_gbest(j)=fitness_gbest(j)+sum(now_n.para_c_bao); end end zbest=pop_x(:,1); fitness_zbest=fitness_gbest(1); for j=1:sizepop if fitness_gbest(j)<fitness_zbest zbest=pop_x(:,j); fitness_zbest=fitness_gbest(j); end end iter=1; record_2=zeros(ger,1); while iter<=ger for j=1:sizepop pop_v(:,j)=(c_1pop_v(:,j)+c_2rand(gbest(:,j)-pop_x(:,j))+c_3rand(zbest-pop_x(:,j))); for i=1:dim if pop_v(i,j)>vlimit_max(i) pop_v(i,j)=vlimit_max(i); end if pop_v(i,j)<vlimit_min(i) pop_v(i,j)=vlimit_min(i); end end pop_x(:,j)=pop_x(:,j)+pop_v(:,j); for i=1:dim if pop_x(i,j)>xlimit_max(i) pop_x(i,j)=xlimit_max(i); end if pop_x(i,j)<xlimit_min(i) pop_x(i,j)=xlimit_min(i); end end if rand>0.75 i=ceil(dim*rand); pop_x(i,j)=(xlimit_min(i)+(xlimit_max(i)-xlimit_min(i))*rand); end %每周购买的容器艇数量 X_1=reshape(pop_x(1:104,j),[],1); %每周购买的操作手数量 X_2=reshape(pop_x(105:208,j),[],1); X_1(X_1<0.5)=0; X_1(X_1>0)=X_1(X_1>0)2-1; X_1=round(X_1100); X_2(X_2<0.5)=0; X_2(X_2>0)=X_2(X_2>0)2-1; X_2=round(X_2100); %当前总数 now_n=para_n_0; %当前操作挺数量-剩余时间 test_n1=[]; %当前操作手数量-剩余时间 test_n2=[]; test_n3=[]; %当前训练 train_num=0; %当前使用 use_num=[0 0]; %当前成本 fitness_pop(j)=0; record_n=[]; for i=1:104 %训练新增 if length(test_n1)>0 temp_index=find(test_n1(:,2)==1); fitness_pop(j)=fitness_pop(j)+length(temp_index)para_c_bao(1); now_n(1)=now_n(1)+length(temp_index); test_n1(temp_index,:)=[]; end if length(test_n2)>0 temp_index=find(test_n2(:,2)==1); fitness_pop(j)=fitness_pop(j)+length(temp_index)para_c_train; now_n(2)=now_n(2)+length(temp_index); test_n2(temp_index,:)=[]; end if length(test_n3)>0 temp_index=find(test_n3(:,2)==1); fitness_pop(j)=fitness_pop(j)+length(temp_index)para_c_bao(2); now_n(2)=now_n(2)+length(temp_index); test_n3(temp_index,:)=[]; end now_n=now_n+train_num; train_num=0; %使用新增 now_n(1)=now_n(1)+use_num(1); for k=1:use_num(2) test_n3(end+1,:)=[1 1]; end %消耗 temp_con=para_r(i)[1 4]; record_n(end+1,:)=[i now_n]; if now_n(1)<temp_con(1) fitness_pop(j)=fitness_pop(j)+10^9abs(now_n(1)-temp_con(1)); end if now_n(2)<temp_con(2)/4 fitness_pop(j)=fitness_pop(j)+10^9abs(now_n(2)-temp_con(2)); end temp_con=[min(temp_con(1),now_n(1)) min(temp_con(2),now_n(2))]; now_n=now_n-temp_con; use_num=temp_con; %购买 fitness_pop(j)=fitness_pop(j)+X_1(i)*para_c_fix(1); fitness_pop(j)=fitness_pop(j)+X_2(i)para_c_fix(2); for k=1:X_1(i) test_n1(end+1,:)=[1 1]; end for k=1:X_2(i) test_n2(end+1,:)=[1 10^9]; end %训练 left_n1=now_n(1); if length(test_n2)>0 temp_con=min([ceil(sum(test_n2(:,2)==10^9)/3),left_n1,now_n(2)]); temp_num=min([temp_con3,sum(test_n2(:,2)==10^9)]); test_n2=sortrows(test_n2,2); test_n2(1:temp_num,2)=1; now_n=now_n-temp_con; train_num=temp_con; end %其余保养 fitness_pop(j)=fitness_pop(j)+sum(now_n.*para_c_bao); end if fitness_pop(j)<fitness_gbest(j) gbest(:,j)=pop_x(:,j); fitness_gbest(j)=fitness_pop(j); end if fitness_gbest(j)<fitness_zbest zbest=gbest(:,j); fitness_zbest=fitness_gbest(j); end end record_2(iter)=fitness_zbest; iter=iter+1; disp(['当前迭代次数:' num2str(iter)]), end x=zbest; %每周购买的容器艇数量 X_1=reshape(x(1:104),[],1); %每周购买的操作手数量 X_2=reshape(x(105:208),[],1); X_1(X_1<0.5)=0; X_1(X_1>0)=X_1(X_1>0)2-1; X_1=round(X_1100); X_2(X_2<0.5)=0; X_2(X_2>0)=X_2(X_2>0)2-1; X_2=round(X_2100); %当前总数 now_n=para_n_0; %当前操作挺数量-剩余时间 test_n1=[]; %当前操作手数量-剩余时间 test_n2=[]; test_n3=[]; %当前训练 train_num=0; %当前使用 use_num=[0 0]; %当前成本 y=0; record_n=[]; totle_record=[]; for i=1:104 totle_record(1:8,i)=0; %训练新增 if length(test_n1)>0 temp_index=find(test_n1(:,2)==1); y=y+length(temp_index)para_c_bao(1); now_n(1)=now_n(1)+length(temp_index); test_n1(temp_index,:)=[]; end if length(test_n2)>0 temp_index=find(test_n2(:,2)==1); totle_record(7,i-1)=ceil(length(temp_index)/3); totle_record(8,i-1)=ceil(length(temp_index)/3); y=y+length(temp_index)para_c_train; now_n(2)=now_n(2)+length(temp_index); test_n2(temp_index,:)=[]; end if length(test_n3)>0 temp_index=find(test_n3(:,2)==1); y=y+length(temp_index)para_c_bao(2); now_n(2)=now_n(2)+length(temp_index); test_n3(temp_index,:)=[]; end now_n=now_n+train_num; train_num=0; %使用新增 now_n(1)=now_n(1)+use_num(1); totle_record(6,i)=totle_record(6,i)+use_num(2); for j=1:use_num(2) test_n3(end+1,:)=[1 1]; end %消耗 temp_con=para_r(i)[1 4]; record_n(end+1,:)=[i now_n]; if now_n(1)<temp_con(1) y=y+10^9abs(now_n(1)-temp_con(1)); end if now_n(2)<temp_con(2)/4 y=y+10^9abs(now_n(2)-temp_con(2)); end temp_con=[min(temp_con(1),now_n(1)) min(temp_con(2),now_n(2))]; totle_record(1,i)=temp_con(1); totle_record(2,i)=temp_con(2); now_n=now_n-temp_con; use_num=temp_con; %购买 y=y+X_1(i)*para_c_fix(1); y=y+X_2(i)para_c_fix(2); totle_record(3,i)=X_1(i); totle_record(4,i)=X_2(i); for j=1:X_1(i) test_n1(end+1,:)=[1 1]; end for j=1:X_2(i) test_n2(end+1,:)=[1 10^9]; end %训练 left_n1=now_n(1); if length(test_n2)>0 temp_con=min([ceil(sum(test_n2(:,2)==10^9)/3),left_n1,now_n(2)]); temp_num=min([temp_con3,sum(test_n2(:,2)==10^9)]); test_n2=sortrows(test_n2,2); test_n2(1:temp_num,2)=1; now_n=now_n-temp_con; train_num=temp_con; end %其余保养 totle_record(5,i)=totle_record(5,i)+now_n(1); totle_record(6,i)=totle_record(6,i)+now_n(2); y=y+sum(now_n.*para_c_bao); end %% 数据记录-totle_record 以及final_totle final_totle=[sum(X_1) sum(X_2) sum(totle_record(5,:)) sum(totle_record(6,:)) sum(totle_record(7,:)) sum(totle_record(8,:))]; jie_giyy1=final_totle(1)*210+final_totle(2)*110+final_totle(4)*5+(final_totle(3)+final_totle(5)+final_totle(6))*10 解释该代码并给出运行结果内容:该代码是一个基于粒子群算法的优化问题,旨在寻找最优的容器艇和操作手的购买方案,以满足一定的需求量,同时最小化成本。
代码首先输入了相关的参数和数据,包括容器艇和操作手的价格、保养价格、训练价格、需求量、初始数量等。然后设置了优化的相关参数,包括粒子个数、迭代次数、变量上下限、惯性权重、加速因子等。接着初始化了
原文地址: https://www.cveoy.top/t/topic/f1wn 著作权归作者所有。请勿转载和采集!