溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++算法庫(kù)中的遺傳算法實(shí)現(xiàn)

發(fā)布時(shí)間:2024-08-13 12:11:33 來(lái)源:億速云 閱讀:84 作者:小樊 欄目:編程語(yǔ)言

在C++中實(shí)現(xiàn)遺傳算法,通常會(huì)使用STL庫(kù)提供的數(shù)據(jù)結(jié)構(gòu)和算法來(lái)實(shí)現(xiàn)遺傳算法的各個(gè)步驟,比如選擇、交叉、變異等。以下是一個(gè)簡(jiǎn)單的C++遺傳算法實(shí)現(xiàn)示例:

#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;

// 個(gè)體結(jié)構(gòu)體
struct Individual {
    vector<int> genome;
    int fitness;
};

// 隨機(jī)生成一個(gè)個(gè)體
Individual generateIndividual() {
    Individual ind;
    for (int i = 0; i < GENOME_SIZE; ++i) {
        ind.genome.push_back(rand() % 2);
    }
    return ind;
}

// 計(jì)算個(gè)體的適應(yīng)度
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;
}

上面的代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的遺傳算法,并且使用隨機(jī)生成的二進(jìn)制基因組來(lái)演示遺傳算法的選擇、交叉和變異操作。在實(shí)際項(xiàng)目中,可以根據(jù)具體的問(wèn)題領(lǐng)域和需求對(duì)遺傳算法進(jìn)行進(jìn)一步的優(yōu)化和擴(kuò)展。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI