基于量子进化算法(QEA)的经典版本解决“背包”问题(Matlab代码实现)

该文介绍了一种基于量子进化算法(QEA)解决0-1背包问题的Matlab实现。QGA.m是主函数,调用Knapsack_quantum等辅助函数进行计算。算法考虑了个体的修复、适应度评价和收敛性检查。输出包括性能平均值、适应度值和运行时间。实验在不同规模的背包问题上运行,如100件和500件物品的情况。
摘要由CSDN通过智能技术生成

       目录

💥1 概述

📚2 运行结果

🎉3 参考文献

👨‍💻4 Matlab代码


💥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
%--------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值