1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
#include<iostream> #include<vector> #include<cmath> #include<algorithm> #include<ctime> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; #define PB push_back #define MP make_pair
int next_int() { return rand()*(RAND_MAX+1)+rand(); } double next_double() { return (double(rand()*(RAND_MAX+1)+rand()))/((RAND_MAX+1)*RAND_MAX+RAND_MAX); } void pmx(VI& a,VI& b,int pointcnt) { int sa=next_int()%pointcnt,sb=next_int()%pointcnt; int temp; if (sa>sb) { temp=sa; sa=sb; sb=temp; } VI aa(pointcnt),bb(pointcnt); int i; for(i=0;i<pointcnt;i++) { aa[i]=a[i],bb[i]=b[i]; } VI m1(pointcnt,-1); VI m2(pointcnt,-1); VI v1tov2(pointcnt,-1); for(i=sa;i<=sb;i++) { m1[aa[i]]=-2; m2[bb[i]]=-2; } for(i=0;i<pointcnt;i++) {
if(m2[i]==m1[i]) { m2[i]=-1; m1[i]=-1; } } int aaa=0; for(i=sa;i<=sb;i++) { if ((m1[aa[i]]==-2)&&(m2[bb[i]]==-2)) { v1tov2[aa[i]]=bb[i]; m1[aa[i]]=-1; m2[bb[i]]=-1; aaa++; } } if(aaa!=(sb-sa+1)) { for(i=0;i<pointcnt;i++) if (m1[i]==-2) { int aflag=0; for(int j=0;j<pointcnt;j++) if( (m2[j]==-2) && (aflag==0)) { v1tov2[i]=j; aflag=1; aaa++; m2[j]=-1; m1[i]=-1; } }
} for(i=sa;i<=sb;i++) {
swap(aa[i],bb[i]); }
for(i=0;i<pointcnt;i++) { if ((i<sa)||(i>sb)) for (int j=sa;j<=sb;j++) { if(aa[i]==aa[j]) { for(int k=0;k<pointcnt;k++) if(aa[i]==v1tov2[k]) aa[i]=k;
} }
}
for(i=0;i<pointcnt;i++) { if ((i<sa)||(i>sb)) for (int j=sa;j<=sb;j++) { if(bb[i]==bb[j]) bb[i]=v1tov2[bb[i]]; }
} a=aa; b=bb;
}
vector<double> x,y; double fitness(const VI& v,int pointcnt) { double r=0; for(int i=0;i<pointcnt;i++) { double dx=x[v[i]]-x[v[(i+1)%pointcnt]]; double dy=y[v[i]]-y[v[(i+1)%pointcnt]]; r+=sqrt(dx*dx+dy*dy); } return 1.0/r; } void change0(vector<int>& K,int N) { int i=next_int()%N; int d=next_int()%(N-1); int j=(i+1+d)%N; swap(K[i],K[j]); }
void mutate(VI& route,int mutate_type,int pointcnt) { if(mutate_type==0) change0(route,pointcnt); } bool pair_dec(const pair<double,VI*>& a,const pair<double,VI*>& b) { return a>b; }
class other_population { public: int popsize,pointcnt; double pc,pm; vector<pair<double,VI*> >pop; pair<double,VI*> bestofpop; int cross_type; int mutate_type; int make_p; int select_type; int toursize; double bestp; other_population(int a,int b,int c,int f,int g,double d,double e,int h,double j,int m) { popsize=a,pointcnt=b,cross_type=c,mutate_type=f,make_p=g,pc=d,pm=e,toursize=h,bestp=j,select_type=m; for(int i=0;i<popsize;i++) { VI* v=new VI(pointcnt); for(int j=0;j<pointcnt;j++) (*v)[j]=j; random_shuffle(v->begin(),v->end()); pop.PB(MP(fitness(*v,pointcnt),v)); } sort(pop.begin(),pop.end(),pair_dec); bestofpop.first=pop[0].first; bestofpop.second=new VI(*pop[0].second); } ~other_population() { for(int i=0;i<pop.size();i++) delete pop[i].second; delete bestofpop.second; } void next() { vector<double> ps(popsize); if(make_p==0) { double sum=0; for(int i=0;i<popsize;i++) sum+=pop[i].first; for(int i=0;i<popsize;i++) ps[i]=pop[i].first/sum; }
if(select_type==0) { vector<pair<double,VI*> > select_res; vector<double> addsum(popsize); for(int i=0;i<popsize-1;i++) { if(i==0) addsum[i]=ps[0]; else addsum[i]=addsum[i-1]+ps[i]; } addsum[popsize-1]=1.5; for(int i=0;i<popsize;i++) { double rd=next_double(); int r=lower_bound(addsum.begin(),addsum.end(),rd)-addsum.begin(); VI* v=new VI(*pop[r].second); select_res.PB(MP(fitness(*v,pointcnt),v)); } for(int i=0;i<popsize;i++) delete pop[i].second; pop=select_res; } for(int cc=0;cc<popsize/2;cc++) { int a=next_int()%popsize; int b=(a+1+(next_int()%(popsize-1)))%popsize; if(next_double()<pc) { if(cross_type==0) pmx(*pop[a].second,*pop[b].second,pointcnt);
pop[a].first=fitness(*pop[a].second,pointcnt); if(bestofpop.first<pop[a].first) { bestofpop.first=pop[a].first; delete bestofpop.second; bestofpop.second=new VI(*pop[a].second); } pop[b].first=fitness(*pop[b].second,pointcnt); if(bestofpop.first<pop[b].first) { bestofpop.first=pop[b].first; delete bestofpop.second; bestofpop.second=new VI(*pop[b].second); } } } for(int i=pop.size()-1;i>=0;i--) if(next_double()<pm) { mutate(*pop[i].second,mutate_type,pointcnt); pop[i].first=fitness(*pop[i].second,pointcnt); } sort(pop.begin(),pop.end(),pair_dec); if(bestofpop.first<pop[0].first) { delete bestofpop.second; bestofpop.first=pop[0].first; bestofpop.second=new VI(*pop[0].second); } } };
int main() { srand((unsigned)time(NULL)); int CASNUM,POINTCNT,POPSIZE,GENERATIONS; CASNUM=10; POINTCNT=10, POPSIZE=100,GENERATIONS=100; x.resize(POINTCNT); y.resize(POINTCNT); x[0]=0, x[1]=1.1,x[2]=3.5,x[3]=3,x[4]=7,x[5]=8,x[6]=4,x[7]=4.5,x[8]=9,x[9]=2; y[0]=1.1,y[1]=3,y[2]=2,y[3]=4,y[4]=5.1,y[5]=8,y[6]=4,y[7]=4.5,y[8]=9,y[9]=2; cout<<"城市数="<<POINTCNT<<endl; cout<<"各城市坐标:"<<endl; for(int i=0;i<POINTCNT;i++) { cout<<"["<<x[i]<<", "<<y[i]<<"]"<<endl; } int select_type,make_p_type,k,cross_type,mutate_type; double q,pc,pm; select_type=0,make_p_type=0,k=5; q=0.5,pc=0.85,pm=0.15; cross_type=0,mutate_type=0;
double best=1e9,worst=0,sum=0; VI res; for(int cas=0;cas<CASNUM;cas++) { other_population gen(POPSIZE,POINTCNT,cross_type,mutate_type,make_p_type,pc,pm,k,q,select_type);
for(int g=0;g<GENERATIONS;g++) gen.next(); if(best>1.0/gen.bestofpop.first) { best=1.0/gen.bestofpop.first; res=*gen.bestofpop.second; } if(worst<1.0/gen.bestofpop.first) worst=1.0/gen.bestofpop.first; sum+=1.0/gen.bestofpop.first; } sum/=CASNUM; cout<<endl; cout<<"历次最好适应度:"<<best<<"\n"<<"历次最差适应度:"<<worst<<"\n"<<"平均适应度:"<<sum<<"\n"; cout<<"输出最好解:"; for(int i=0;i<POINTCNT;i++) { cout<<res[i]; if (i<POINTCNT-1) cout<<"-"; } cout<<endl;
return 0; }
|