|
发表于 2024-1-24 09:14:18
|
显示全部楼层
// 交叉操作
void crossover(vector<vector<int>>& population, vector<vector<int>>& newPopulation) {
int size = population.size();
for (int i = 0; i < size; i++) {
if (rand() % 2 == 0) { // 随机选择两个个体进行交叉操作
int j = rand() % size;
int k = rand() % size;
swap(population[i][j], population[i][k]); // 交叉操作
}
}
}
// 变异操作
void mutation(vector<vector<int>>& population) {
int size = population.size();
for (int i = 0; i < size; i++) {
if (rand() % 2 == 0) { // 随机选择一个个体进行变异操作
int index = rand() % population[i].size();
population[i][index] = 1 - population[i][index]; // 变异操作
}
}
}
|
|