CHATGPT AI
evolution
ary-algorithm/

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;

// random generator function:
int myrandom (int i) { return std::rand()%i;}


int main () {

// seed the random number generator with a constant value:
srand( unsigned (time(0)) );

const string target = "Hello World!"; // target string we are trying to match

const int populationSize = 500; // population size of our genetic algorithm

vector<string> population; // vector to store our population of strings

int generation = 0; // current generation number

// create a random starting population of strings:

for (int i=0; i<populationSize; i++) {

string str;

for (int j=0; j<target.length(); j++) {

char c = 97 + rand() % 26; // generate a random lowercase character from 'a' to 'z'

str += c;

}

population.push_back(str); // add the generated string to the population vector

}

while (true) { // loop until we find the target string

cout << "Generation: " << generation++ << endl;

for (int i=0; i<population.size(); i++) { // loop through each member of the population

cout << "String: " << population[i] << "\t\tFitness: " << fitness(population[i]) << endl;

if (population[i] == target) { // check if this member is equal to the target string

cout << "Target Reached!" << endl;
return 0; // exit program if target is reached

}
}

vector<string> newPopulation;// create a new empty vector for our new generation of strings

for (int i=0 ; i<population.size(); i++) {// loop through each member of the current population

string parent1 = selection(population);// select two parents using tournament selection
string parent2 = selection(population);

vector<string> children = crossover(parent1, parent2);// perform crossover between parents and generate two children
mutation(children[0]);// perform mutation on both children mutation(children[1]); newPopulation.push_back(children[0]);// add both children to the new generation's population newPopulation.push_back(children[1]); } population = newPopulation;// replace old generation's population with the new one } return 0;}
ary-algorithm/ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <random> #include <chrono> using namespace std; // random generator function: int myrandom (int i) { return std::rand()%i;} int main () { // seed the random number generator with a constant value: srand( unsigned (time(0)) ); const string target = "Hello World!"; // target string we are trying to match const int populationSize = 500; // population size of our genetic algorithm vector<string> population; // vector to store our population of strings int generation = 0; // current generation number // create a random starting population of strings: for (int i=0; i<populationSize; i++) { string str; for (int j=0; j<target.length(); j++) { char c = 97 + rand() % 26; // generate a random lowercase character from 'a' to 'z' str += c; } population.push_back(str); // add the generated string to the population vector } while (true) { // loop until we find the target string cout << "Generation: " << generation++ << endl; for (int i=0; i<population.size(); i++) { // loop through each member of the population cout << "String: " << population[i] << "\t\tFitness: " << fitness(population[i]) << endl; if (population[i] == target) { // check if this member is equal to the target string cout << "Target Reached!" << endl; return 0; // exit program if target is reached } } vector<string> newPopulation;// create a new empty vector for our new generation of strings for (int i=0 ; i<population.size(); i++) {// loop through each member of the current population string parent1 = selection(population);// select two parents using tournament selection string parent2 = selection(population); vector<string> children = crossover(parent1, parent2);// perform crossover between parents and generate two children mutation(children[0]);// perform mutation on both children mutation(children[1]); newPopulation.push_back(children[0]);// add both children to the new generation's population newPopulation.push_back(children[1]); } population = newPopulation;// replace old generation's population with the new one } return 0;}
0 Comments & Tags 0 Acciones 1 Views

Password Copied!

Please Wait....