Algorithm na Neman Juyin Halitta (Evolutionary Search) a C++- Bayani, Misali, da Lamba

Algorithm din Binciken Juyin Halitta hanya ce ta ingantawa bisa tsarin juyin halitta. Wannan algorithm yana kwatanta tsarin juyin halitta na daidaikun mutane a cikin jama'a a cikin tsararraki don nemo mafi kyawun mafita ga matsala.

Yadda Ake Aiki

  1. Ƙaddamar da yawan jama'a: Ƙirƙirar yawan jama'a na farko na daidaikun mutane.
  2. Ƙididdiga: Ƙimar ingancin kowane mutum a cikin yawan jama'a bisa ga aikin haƙiƙa ko ma'auni na kimantawa.
  3. Zaɓi: Zaɓi wani yanki na mafi kyawun mutane daga yawan jama'a na yanzu dangane da yuwuwar ko ma'aunin zaɓi.
  4. Juyin Halitta: Ƙirƙiri sabon tsara ta hanyar amfani da ayyukan giciye da maye gurbi ga waɗanda aka zaɓa.
  5. Juyawa: Maimaita matakai 2 zuwa 4 sama da tsararraki da yawa har sai an sami gamsasshen bayani ko an kai ga ƙididdige adadin maimaitawa.

Misali: Inganta Fibonacci Aiki ta amfani da Binciken Juyin Halitta

Yi la'akari da matsalar inganta aikin Fibonacci F(x) = F(x-1) + F(x-2) tare da F(0) = 0, F(1) = 1. Muna son nemo darajar x don wanda F(x) yana da girma. Hanyar Neman Juyin Halittu na iya haifar da yawan adadin ƙima bazuwar x, ƙirƙirar su cikin tsararraki don nemo mafi kyawun ƙimar x.

Misalin Code a C++

#include <iostream>  
#include <vector>  
#include <cstdlib>  
#include <ctime>  
  
int fibonacci(int n) {  
    if(n <= 0) return 0;  
    if(n == 1) return 1;  
    return fibonacci(n- 1) + fibonacci(n- 2);  
}  
  
int evolutionSearchFibonacci(int populationSize, int numGenerations) {  
    srand(time(0));  
  
    std::vector<int> population(populationSize);  
    for(int i = 0; i < populationSize; ++i) {  
        population[i] = rand() % populationSize;  
    }  
  
    for(int gen = 0; gen < numGenerations; ++gen) {  
        int bestIndex = 0;  
        for(int i = 1; i < populationSize; ++i) {  
            if(fibonacci(population[i]) > fibonacci(population[bestIndex])) {  
                bestIndex = i;  
            }  
        }  
  
        // Crossover and mutation operations can be applied here  
  
        // Example: Replace the worst individual with the best individual  
        population[0] = population[bestIndex];  
    }  
  
    return population[0];  
}  
  
int main() {  
    int populationSize = 50;  
    int numGenerations = 100;  
    int result = evolutionSearchFibonacci(populationSize, numGenerations);  
  
    std::cout << "Optimal x for maximum Fibonacci value: " << result << std::endl;  
  
    return 0;  
}  

A cikin wannan misalin, muna amfani da hanyar Neman Juyin Halitta don inganta Fibonacci aikin. Muna samar da yawan adadin ƙimar x bazuwar, muna haɓaka su a cikin tsararraki ta hanyar zabar mafi kyawun mutane da aiwatar da ayyukan giciye da maye gurbi.