使用c++语言提供简单遗传算法示例

以下是用C++语言实现遗传算法的示例代码,注释中详细介绍了每个函数的作用及原理。

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

// 染色体长度
const int GEN_LEN = 10;

// 种群大小
const int POP_SIZE = 100;

// 变异概率
const double MUTATION_RATE = 0.05;

// 遗传代数
const int MAX_GENERATION = 1000;

// 样本函数
double fitness_func(std::vector<int>& gene)
{
    double x = 0.0;
    for (int i = 0; i < GEN_LEN; ++i) {
        x = x * 2 + gene[i];
    }
    return x * x;
}

// 初始化种群
void init_population(std::vector<std::vector<int>>& population)
{
    std::srand(std::time(nullptr));
    for (int i = 0; i < POP_SIZE; ++i) {
        std::vector<int> gene(GEN_LEN, 0);
        for (int j = 0; j < GEN_LEN; ++j) {
            gene[j] = std::rand() % 2;
        }
        population.push_back(gene);
    }
}

// 选择操作
void selection(std::vector<std::vector<int>>& population, std::vector<std::vector<int>>& offspring)
{
    // 使用轮盘赌选择算法进行选择
    std::vector<double> select_prob(POP_SIZE, 0.0);
    double fitness_sum = 0.0;
    for (int i = 0; i < POP_SIZE; ++i) {
        select_prob[i] = fitness_func(population[i]);
        fitness_sum += select_prob[i];
    }
    for (int i = 0; i < POP_SIZE; ++i) {
        select_prob[i] /= fitness_sum;
    }

    // 使用轮盘赌进行选择
    for (int i = 0; i < POP_SIZE; ++i) {
        double prob = std::rand() % 101 / 100.0;
        double psum = 0.0;
        for (int j = 0; j < POP_SIZE; ++j) {
            psum += select_prob[j];
            if (psum > prob) {
                offspring.push_back(population[j]);
                break;
            }
        }
    }
}

// 交叉操作
void crossover(std::vector<std::vector<int>>& offspring)
{
    for (int i = 0; i < POP_SIZE; i += 2) {
        int pos = std::rand() % GEN_LEN;
        std::vector<int> gene1 = offspring[i], gene2 = offspring[i + 1];
        for (int j = pos; j < GEN_LEN; ++j) {
            int temp = gene1[j];
            gene1[j] = gene2[j];
            gene2[j] = temp;
        }
        offspring[i] = gene1;
        offspring[i + 1] = gene2;
    }
}

// 变异操作
void mutation(std::vector<std::vector<int>>& offspring)
{
    for (int i = 0; i < POP_SIZE; ++i) {
        for (int j = 0; j < GEN_LEN; ++j) {
            double prob = std::rand() % 101 / 100.0;
            if (prob < MUTATION_RATE) {
                offspring[i][j] = 1 - offspring[i][j];
            }
        }
    }
}

// 寻找最优解
std::vector<int> find_best_gene(std::vector<std::vector<int>>& population)
{
    std::vector<int> best_gene(GEN_LEN, 0);
    double best_fitness = -1.0;
    for (int i = 0; i < POP_SIZE; ++i) {
        double fitness = fitness_func(population[i]);
        if (fitness > best_fitness) {
            best_fitness = fitness;
            best_gene = population[i];
        }
    }
    return best_gene;
}

int main()
{
    // 种群
    std::vector<std::vector<int>> population;

    // 初始化种群
    init_population(population);

    // 遗传算法的主循环
    for (int i = 0; i < MAX_GENERATION; ++i) {
        // 子代种群
        std::vector<std::vector<int>> offspring; 

        // 选择操作
        selection(population, offspring);

        // 交叉操作
        crossover(offspring);

        // 变异操作
        mutation(offspring);

        // 种群更新
        population = offspring;

        // 输出种群最优解和对应的适应度值
        std::vector<int> best_gene = find_best_gene(population);
        double best_fitness = fitness_func(best_gene);
        std::cout << "Generation: " << i << "  Best gene: ";
        for (int j = 0; j < GEN_LEN; ++j) {
            std::cout << best_gene[j];
        }
        std::cout << "  Best fitness: " << best_fitness << std::endl;
    }

    return 0;
}

遗传算法的基本框架包括选择、交叉和变异操作,其中选择使用的是轮盘赌选择算法,交叉使用的是单点交叉法,变异是对某个位置的基因进行取反操作。在主循环中不断迭代进行这些操作,最终得到一个近似最优解。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值