|
楼主 |
发表于 2024-3-6 16:11:30
|
显示全部楼层
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <random>
- // 定义材料的结构体
- struct Material {
- int length;
- int demand;
- };
- // 定义染色体的结构体
- struct Chromosome {
- std::vector<int> genes;
- int fitness;
- };
- // 初始化种群
- std::vector<Chromosome> initializePopulation(int populationSize, int geneSize) {
- std::vector<Chromosome> population;
- for (int i = 0; i < populationSize; i++) {
- Chromosome chromosome;
- chromosome.genes.resize(geneSize);
- for (int j = 0; j < geneSize; j++) {
- chromosome.genes[j] = rand() % 2; // 随机生成0或1
- }
- chromosome.fitness = 0;
- population.push采用back(chromosome);
- }
- return population;
- }
- // 计算染色体的适应度
- void calculateFitness(Chromosome& chromosome, const std::vector<Material>& materials, int targetLength) {
- int totalLength = 0;
- int totalDemand = 0;
- for (int i = 0; i < chromosome.genes.size(); i++) {
- if (chromosome.genes[i] == 1) {
- totalLength += materials[i].length;
- totalDemand += materials[i].demand;
- }
- }
- chromosome.fitness = (totalLength >= targetLength) ? totalDemand : 0;
- }
- // 选择操作
- std::vector<Chromosome> selection(const std::vector<Chromosome>& population, int tournamentSize) {
- std::vector<Chromosome> selectedPopulation;
- for (int i = 0; i < population.size(); i++) {
- std::vector<int> tournamentIndices;
- for (int j = 0; j < tournamentSize; j++) {
- int index = rand() % population.size();
- tournamentIndices.push采用back(index);
- }
- int maxFitness = -1;
- int maxIndex = -1;
- for (int j = 0; j < tournamentIndices.size(); j++) {
- int index = tournamentIndices[j];
- if (population[index].fitness > maxFitness) {
- maxFitness = population[index].fitness;
- maxIndex = index;
- }
- }
- selectedPopulation.push采用back(population[maxIndex]);
- }
- return selectedPopulation;
- }
- // 交叉操作
- std::vector<Chromosome> crossover(const std::vector<Chromosome>& population, double crossoverRate) {
- std::vector<Chromosome> offspringPopulation;
- for (int i = 0; i < population.size(); i += 2) {
- Chromosome parent1 = population[i];
- Chromosome parent2 = population[i + 1];
- Chromosome offspring1 = parent1;
- Chromosome offspring2 = parent2;
- if (rand() / double(RAND采用MAX) < crossoverRate) {
- int crossoverPoint = rand() % parent1.genes.size();
- for (int j = crossoverPoint; j < parent1.genes.size(); j++) {
- offspring1.genes[j] = parent2.genes[j];
- offspring2.genes[j] = parent1.genes[j];
- }
- }
- offspringPopulation.push采用back(offspring1);
- offspringPopulation.push采用back(offspring2);
- }
- return offspringPopulation;
- }
- // 变异操作
- void mutation(std::vector<Chromosome>& population, double mutationRate) {
- for (int i = 0; i < population.size(); i++) {
- for (int j = 0; j < population[i].genes.size(); j++) {
- if (rand() / double(RAND采用MAX) < mutationRate) {
- population[i].genes[j] = 1 - population[i].genes[j]; // 变异
- }
- }
- }
- }
- // 打印最优解
- void printBestSolution(const std::vector<Chromosome>& population, const std::vector<Material>& materials, int targetLength) {
- int maxFitness = -1;
- int maxIndex = -1;
- for (int i = 0; i < population.size(); i++) {
- if (population[i].fitness > maxFitness) {
- maxFitness = population[i].fitness;
- maxIndex = i;
- }
- }
- std::cout << "Best solution: ";
- for (int i = 0; i < population[maxIndex].genes.size(); i++) {
- if (population[maxIndex].genes[i] == 1) {
- std::cout << "Material " << i + 1 << " (Length: " << materials[i].length << ", Demand: " << materials[i].demand << ") ";
- }
- }
- std::cout << std::endl;
- std::cout << "Total Length: " << maxFitness << std::endl;
- std::cout << "Target Length: " << targetLength << std::endl;
- }
- int main() {
- // 定义材料和目标长度
- std::vector<Material> materials = { {10, 5}, {15, 8}, {20, 10}, {25, 12} };
- int targetLength = 50;
- // 定义遗传算法的参数
- int populationSize = 100;
- int geneSize = materials.size();
- int tournamentSize = 5;
- double crossoverRate = 0.8;
- double mutationRate = 0.01;
- int maxGenerations = 100;
- // 初始化种群
- std::vector<Chromosome> population = initializePopulation(populationSize, geneSize);
- // 迭代进化
- for (int generation = 0; generation < maxGenerations; generation++) {
- // 计算适应度
- for (int i = 0; i < population.size(); i++) {
- calculateFitness(population[i], materials, targetLength);
- }
- // 打印当前最优解
- printBestSolution(population, materials, targetLength);
- // 选择操作
- std::vector<Chromosome> selectedPopulation = selection(population, tournamentSize);
- // 交叉操作
- std::vector<Chromosome> offspringPopulation = crossover(selectedPopulation, crossoverRate);
- // 变异操作
- mutation(offspringPopulation, mutationRate);
- // 更新种群
- population = offspringPopulation;
- }
- return 0;
- }
复制代码 |
|