clc
clear
numItems = 10;
weights = [2, 3, 4, 5, 9, 7, 8, 6, 4, 5];
values = [3, 4, 8, 8, 10, 7, 6, 5, 3, 7];
capacity = 20;
popSize = 50;
N_Generations = 500;
cross_Rate = 0.8;
mutation_Rate = 0.1;
population = randi([0, 1], popSize, numItems);
best_Fitness = zeros(N_Generations, 1);
for gen = 1:N_Generations
fitness = evaluate_Fitness(population, weights, values, capacity);
best_Fitness(gen) = max(fitness);
selected_Population = selection(population, fitness);
new_Population = zeros(popSize, numItems);
for i = 1:2:popSize
if rand < cross_Rate
parent1 = selected_Population(i, :);
parent2 = selected_Population(i+1, :);
[offspring1, offspring2] = Crossover(parent1, parent2);
new_Population(i, :) = offspring1;
new_Population(i+1, :) = offspring2;
else
new_Population(i, :) = selected_Population(i, :);
new_Population(i+1, :) = selected_Population(i+1, :);
end
end
for i = 1:popSize
new_Population(i, :) = mutate(new_Population(i, :), mutation_Rate);
end
population = new_Population;
end
finalFitness = evaluate_Fitness(population, weights, values, capacity);
[bestValue, bestIndex] = max(finalFitness);
bestSolution = population(bestIndex, :);
基于matlab的粒子群算法 源码.7z
142 浏览量
2024-06-20
10:48:28
上传
评论
收藏 1KB 7Z 举报
![avatar](https://profile-avatar.csdnimg.cn/de97968ba7b34daeab370a074a61d36f_mohen_777.jpg!1)
墨痕_777
- 粉丝: 871
- 资源: 127
最新资源
- 利用DS1302的可调时钟
- 基于 python实现对一幅 BMP 格式的灰度图像先进行二元霍夫曼编码和游程编码【信息论与编码课程设计】(课程设计报告+源码)
- python头歌换披萨模拟场景示例
- 基于 yolo 的行人目标检测 + 源代码 + 详细文档
- 基于springboot+vue+MySQL实现的在线考试系统+源代码+文档
- 基于 yolo 的行人目标检测 + 源代码 + 详细文档
- 基于 C++的OpenGL、Qt 实现图形绘制系统,画图板课程实践(课程实践报告+源码)
- 机器学习课程设计报告基本大纲
- 基于LoRa的主从机农田监测系统原理图
- PTC Creo View 是由 PTC 公司开发的一款专业的三维可视化软件,专为工程设计和制造领域而设计
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)