温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++算法库中的遗传算法实现

发布时间:2024-08-13 12:11:33 来源:亿速云 阅读:102 作者:小樊 栏目:编程语言

在C++中实现遗传算法,通常会使用STL库提供的数据结构和算法来实现遗传算法的各个步骤,比如选择、交叉、变异等。以下是一个简单的C++遗传算法实现示例:

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <ctime>

using namespace std;

const int POPULATION_SIZE = 10;
const int GENOME_SIZE = 5;
const int MAX_GENERATION = 100;
const double CROSSOVER_RATE = 0.7;
const double MUTATION_RATE = 0.1;

// 个体结构体
struct Individual {
    vector<int> genome;
    int fitness;
};

// 随机生成一个个体
Individual generateIndividual() {
    Individual ind;
    for (int i = 0; i < GENOME_SIZE; ++i) {
        ind.genome.push_back(rand() % 2);
    }
    return ind;
}

// 计算个体的适应度
int calcFitness(const Individual& ind) {
    int fitness = 0;
    for (int gene : ind.genome) {
        fitness += gene;
    }
    return fitness;
}

// 选择操作
vector<Individual> selection(const vector<Individual>& population) {
    vector<Individual> newPopulation;
    newPopulation.reserve(POPULATION_SIZE);
    for (int i = 0; i < POPULATION_SIZE; ++i) {
        int idx1 = rand() % POPULATION_SIZE;
        int idx2 = rand() % POPULATION_SIZE;
        if (population[idx1].fitness > population[idx2].fitness) {
            newPopulation.push_back(population[idx1]);
        } else {
            newPopulation.push_back(population[idx2]);
        }
    }
    return newPopulation;
}

// 交叉操作
void crossover(vector<Individual>& population) {
    for (int i = 0; i < POPULATION_SIZE; i += 2) {
        if ((double)rand() / RAND_MAX < CROSSOVER_RATE) {
            int crossoverPoint = rand() % GENOME_SIZE;
            for (int j = crossoverPoint; j < GENOME_SIZE; ++j) {
                swap(population[i].genome[j], population[i + 1].genome[j]);
            }
        }
    }
}

// 变异操作
void mutation(vector<Individual>& population) {
    for (Individual& ind : population) {
        for (int i = 0; i < GENOME_SIZE; ++i) {
            if ((double)rand() / RAND_MAX < MUTATION_RATE) {
                ind.genome[i] = 1 - ind.genome[i];
            }
        }
    }
}

int main() {
    srand(time(NULL));
    
    vector<Individual> population;
    for (int i = 0; i < POPULATION_SIZE; ++i) {
        population.push_back(generateIndividual());
    }

    for (int generation = 0; generation < MAX_GENERATION; ++generation) {
        for (Individual& ind : population) {
            ind.fitness = calcFitness(ind);
        }
        sort(population.begin(), population.end(), [](const Individual& a, const Individual& b) {
            return a.fitness > b.fitness;
        });

        cout << "Generation " << generation << " Best fitness: " << population[0].fitness << endl;

        population = selection(population);
        crossover(population);
        mutation(population);
    }

    cout << "Best individual: ";
    for (int gene : population[0].genome) {
        cout << gene << " ";
    }
    cout << endl;

    return 0;
}

上面的代码实现了一个简单的遗传算法,并且使用随机生成的二进制基因组来演示遗传算法的选择、交叉和变异操作。在实际项目中,可以根据具体的问题领域和需求对遗传算法进行进一步的优化和扩展。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI