p; if (randShk < 0.33) {
tmpS = shk.Swap(Pop[p], randPosition); //交换操作
}
else if (randShk >= 0.67) {
tmpS = shk.Flip(Pop[p], randPosition); //翻转操作
}
else {
tmpS = shk.Insert(Pop[p], randPosition); //插入操作
}
HeuristicOperator evl;
if (evl.eva l(tmpS, GA_DM, n) > evl.eva l(Pop[p], GA_DM, n)) {
newPop[p] = Pop[p];
}
else {
newPop[p] = tmpS;
}
}
Pop = newPop;
//选择操作(轮盘赌)
vector<double> Cusum(POP_SIZE + 1, 0); //适用于轮盘赌的累加器Cusum(补充了cus[0]=0;
for (int i = 0; i < POP_SIZE; i++) {
Cusum[i + 1] = Cusum[i] + popFit[i];
}
double Sum = accumulate(popFit.begin(), popFit.end(), 0.0); //计算各个体被选择的概率(归一化)
vector<double> cusFit(POP_SIZE + 1); //放置种群中个个体被选择的概率值
for (int i = 0; i < POP_SIZE + 1; i++) {
cusFit[i] = Cusum[i] / Sum;
}
for (int p = 0; p < POP_SIZE; p++) { //轮盘赌产生新种群
double r = rand() / double(RAND_MAX);
for (int q = 0; q < POP_SIZE; q++) {
if (r > cusFit[q] && r <= cusFit[q + 1]) {
newPop[p] = Pop[q];
}
}
}
Pop = newPop;
}
//计时结束
auto end = system_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << "花费了"
<< double(duration.count()) * microseconds::period::num / microseconds::period::den
<< "秒" << endl;
//输出结果
double gs0 = 15377.711;
cout << "最优解为" << gs[MAX_GEN] << endl;
double e = (gs[MAX_GEN] - gs0) / gs0;
cout << "误差为" << e * 100.0 << '%' << endl;
cout << "最优路径为" << endl;
for (int i = 0; i < n; i++) {
cout << bestIndival[i] + 1 << '\t';
}