#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <ctime>
#define POPULATION 1000
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
const string target = "I've got it!";
const string GeneBase= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP"\
"QRSTUVWXYZ 1234567890, .-;:_!\"#%&/()=?@${[]}'"; // 基因库
int random(int l, int r) { // 随机一个位于 [l, r] 之间的整数
return rand() % (r - l + 1) + l;
}
char mutate() {
int len = GeneBase.size();
return GeneBase[random(0, len - 1)];
}
string create() { // 产生新的染色体
string chromosome = "";
int len = target.size();
for (int i = 0; i < len; i ++ )
chromosome += mutate();
return chromosome;
}
struct Individual { // 个体
string chromosome; // 染色体
int fitness; // 适应度
Individual(string chromosome);
Individual mate(Individual parent); // 交配
int calc_fitness(); // 计算适应度
void mutation(); // 随机变异
bool operator < (const Individual &tmp)const {
return fitness < tmp.fitness;
}
};
Individual::Individual(string chromosome) {
this -> chromosome = chromosome;
this -> fitness = calc_fitness();
}
Individual Individual::mate(Individual parent) { // 父母双方交配产生后代
string child = ""; // 后代
int len = chromosome.size();
for (int i = 0; i < len; i ++ ) {
double p = random(0, 100) / 100; // 计算交配概率
if (p <= 0.5) // 如果概率小于等于 0.5,则从母亲(chromosome)遗传该基因
child += chromosome[i];
else // 从父亲遗传(parent)
child += parent.chromosome[i];
}
return Individual(child);
}
int Individual::calc_fitness() { // 计算适应度
int len = target.size();
int now_fitness = 0;
for (int i = 0; i < len; i ++ )
now_fitness += (chromosome[i] != target[i]);
return now_fitness;
}
void Individual::mutation() {
int P = random(1, 2), LEN = target.size();
for (int i = 1; i <= P; i ++ ) {
int pos = random(0, LEN - 1);
chromosome[pos] = mutate();
}
this -> fitness = calc_fitness();
}
int main() {
srand(time(0));
int cnt = 0; // 计算当前迭代到第几代
vector<Individual> population;
bool found = false;
for (int i = 0; i < POPULATION; i ++ ) { // 随机个体
population.emplace_back(Individual(create()));
}
while (!found) {
sort(population.begin(), population.end()); // 按照适应度排序
if (population[0].fitness == 0) {
found = true; break;
}
vector<Individual> new_population;
int s = (10 * POPULATION) / 100; // 保留精英种子
for (int i = 0; i < s; i ++ )
new_population.emplace_back(population[i]);
s = POPULATION - s; // 剩下的随机交配
for (int i = 0; i < s; i ++ ) {
int len = population.size();
Individual p1 = population[random(0, 50)];
Individual p2 = population[random(0, 50)];
Individual ch = p1.mate(p2);
int P = random(0, 100);
if (P <= 20) ch.mutation();
new_population.emplace_back(ch);
}
population = new_population;
printf("Generation: %d ", cnt);
cout << "String : " << population[0].chromosome << " ";
cout << "Fitness : " << population[0].fitness << "\n"; cnt ++ ;
}
printf("Generation : %d ", cnt);
cout << "String : " << population[0].chromosome << " ";
cout << "Fitness : " << population[0].fitness << "\n"; cnt ++ ;
return 0;
}剪贴板
剪贴板 5mx3g2os
剪贴板信息
剪贴板信息
- 收录
- 更新