string
Creating a complete AI system is quite an extensive task, but I can show you how to write a simple AI program in C++. Below, you will find an example of a simple genetic algorithm, which is often considered a part of the AI field. This algorithm attempts to "evolve" the string "Hello, World!" from a population of random strings.
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
const std::string TARGET = "Hello, World!";
const int POP_SIZE = 100;
const double MUTATION_RATE = 0.01;
struct Individual {
std::string dna;
int fitness;
Individual() : dna(TARGET.size(), ' '), fitness(0) {}
// Initialize individual with random DNA
void randomize() {
for (char &c : dna) {
c = (rand() % 95) + 32; // Printable characters range from 32 to 126
}
}
// Calculate fitness as number of correct characters
void calcFitness() {
fitness = 0;
for (size_t i = 0; i < dna.size(); ++i) {
if (dna[i] == TARGET[i]) {
fitness++;
}
}
}
};
// Perform a crossover between two parents to produce an offspring
Individual crossover(const Individual &parent1, const Individual &parent2) {
Individual offspring;
for (size_t i = 0; i < parent1.dna.size(); ++i) {
offspring.dna[i] = (rand() % 2) ? parent1.dna[i] : parent2.dna[i];
}
return offspring;
}
// Mutate an individual by randomly changing its DNA
void mutate(Individual &individual) {
for (char &c : individual.dna) {
if ((rand() / (double)RAND_MAX) < MUTATION_RATE) {
c = (rand() % 95) + 32;
}
}
}
int main() {
srand(static_cast<unsigned>(time(nullptr)));
std::vector<Individual> population(POP_SIZE);
// Initialize population with random DNA
for (Individual &individual : population) {
individual.randomize();
individual.calcFitness();
}
int generation = 0;
while (true) {
// Sort population by fitness
std::sort(population.begin(), population.end(), [](const Individual &a, const Individual &b) {
return a.fitness > b.fitness;
});
// Check for solution
if (population[0].fitness == TARGET.size()) {
break;
}
// Create next generation
std::vector<Individual> newPopulation;
for (int i = 0; i < POP_SIZE; ++i) {
Individual parent1 = population[rand() % (POP_SIZE / 2)];
Individual parent2 = population[rand() % (POP_SIZE / 2)];
Individual offspring = crossover(parent1, parent2);
mutate(offspring);
offspring.calcFitness();
newPopulation.push_back(offspring);
}
population = newPopulation;
generation++;
// Optionally, print out the best fit individual every few generations
if (generation % 10 == 0) {
std::cout << "Generation " << generation
<< " | Best fit: " << population[0].dna
<< " | Fitness: " << population[0].fitness << std::endl;
}
}
std::cout << "Solution found! (" << population[0].dna << ") after " << generation << " generations." << std::endl;
return 0;
}
To compile this code, save it to a file (for example, genetic_algorithm.cpp) and use the following command:
g++ -o genetic_algorithm genetic_algorithm.cpp -std=c++11
Then run the compiled program:
./genetic_algorithm
This genetic algorithm starts with a population of random strings and evolves over generations to match the target string "Hello, World!". Each individual's "fitness" is determined by the number of characters that match the target string. The fittest individuals are more likely to reproduce, and their offspring may inherit their successful traits. Random mutations can introduce new traits.