目录
💥1 概述
输入文件:
- 100.txt(100件物品的背包问题)
- 500.txt(500件物品的背包问题)
运行:
- 通过运行QGA.m函数,这是算法的主要函数,QEA开始。
- Knapsack_quantum.m 是算法的下一个重要功能,它运行 QEA baed 的第一个配置是在主文件中完成的。
- Knapsack_observe.m 是从概率计算二元个体的函数。
- Knapsack_repair.m 修复当前解决方案。
- Knapsack_fitness.m 根据所选项目生成健身度。
- isconvergence.m 检查个体的概率是否对流。
输出文件:
- Fig.Fig:显示了在定义的迭代期间运行 N 次后的性能平均值。
- res.txt :包括算法的最终适应度,即 n 次运行的平均值。
- time.txt:包括 QEA 的运行时间(以秒为单位)。
- 所有输出文件都写入新文件夹中。
📚2 运行结果
🎉3 参考文献
[1]贺毅朝,王熙照,李文斌,张新禄,陈嶷瑛.基于遗传算法求解折扣{0-1}背包问题的研究[J].计算机学报,2016,39(12):2614-2630.
👨💻4 Matlab代码
主函数部分代码:function QGA
tic ;
run = 5; %run
colors = 'krmbgcyw' ;
iteration=200; %generation
mean_fitness=zeros(1,iteration);
temp=dlmread('100.txt'); %read from text file
capacity=temp(1,1); %assign capacity from text file
temp(1,:)=[];
knapsack_input=temp'; %rotate array
profits=knapsack_input(1,:);
weights=knapsack_input(2,:);
repair_type=2; %type=1 random . type=2 greedy . type=3 none
penalty_type=3; %type=1 linear . type=2 logarithm . type=3 none
x=1:iteration ;
teta=0.01*pi; %Q gate
H_gate=0.01; %epsilon
agent_number=25;
for i=1:run
mean_fitness(i,:)=Knapsack_quantum(agent_number,profits,weights,capacity,H_gate,repair_type,penalty_type,iteration,teta);
disp(['End of run ',num2str(i),' th.']) ;
% figure ;
% j = randsample(1:7,1) ;
% plot(x,iteration_fitness,[colors(j),'-']) ;
% axis([min(x)-100 max(x)+100 min(iteration_fitness)-100 max(iteration_fitness)+100]) ;
end
y = mean(mean_fitness,1) ;
h=figure ;
hold on ;
plot(x,y,'b-') ;
% axis([min(x)-100 max(x)+100 min(y(:))-100 max(y(:))+100]) ;
axis auto ;
xlabel('All Iterations') ;
ylabel('Fitness Mean') ;
title('All Quantum Genetic Algorithm') ;
disp('Allruntime=') ;
qgatime = toc ; % Run Time % Set QGA Time
disp(mat2str(qgatime)) ;
dlmwrite('new\time.txt',qgatime);
dlmwrite('new\res.txt',y(iteration));
hgsave(h,'new\fig');
end
%--------------------------------------------------------------------------