选修课作业,将C语言源代码改为C#,要求不高,代码在350行左右,请各位大神帮改~这两天就要交,在线等
代码附上一部分。关于遗传算法,大神请先看看~
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#define FILE_PATH "G:\\cnc144.txt","r" //数据文件名
#define N_COLONY 300 // N_COLONY>=xColony
#define CITY 144 // CITY>=xCity
//--------------------------------------------//
int xColony=300; //##// 个体数
int xCity=198;
double edgeSpeed=30; //##// 临界速度
double probab1=0.02; //##// 变异概率
double probab2=0.05; //##// 映射概率 //0.04(80) 0.03(50) 0.015(80) 0.05(50);
long NOCHANGE=200000; //##// 最大停止改变代数
long maxGen=300000; //##// 停机代数
int colony[N_COLONY][CITY];
double cityXY[CITY][2];
double city_dis[CITY][CITY];
double dis_p[N_COLONY];
double sumbest,sumTemp;
double speed;
int temp[CITY],ibest;
clock_t timeStart,timeNow,timeTemp;
long GenNum,Ni;
void init();
int position(int *tmp,int C);
void invert(int pos_start,int pos_end);
void printBest(long GenNum,long Ni);
void tempTest(int i);
void mapped();
void LastCP();
double path(int tmp[],int k1,int k2);
FILE *fpme;/*by me*/
//###################################################################//
void main()
{
register int C1,j,k,pos_C,pos_C1; int k1,k2,l1,l2,pos_flag,icount;
register double disChange;
static int i=0;
timeStart=timeNow=timeTemp=clock(); init();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
for(;;)
{ for(j=0;j<xCity;j++)temp[j]=colony[i][j];
disChange=0;pos_flag=0;
pos_C=rand()%xCity;
for(;;)
{
if((rand()/32768.0)<probab1) //内变异算子
{ do pos_C1=rand()%xCity;while (pos_C1==pos_C);
C1=colony[i][pos_C1];
}
else
{ do j=rand()%xColony;while(j==i);
k=position(colony[j],temp[pos_C]);
C1=colony[j][(k+1)%xCity];
pos_C1=position(temp,C1);
}
if( speed>edgeSpeed && pos_C1<pos_C+2)break; ///////////////////////
if((pos_C+1)%xCity==pos_C1 || (pos_C-1+xCity)%xCity==pos_C1 )break;
k1=temp[pos_C]; k2=temp[(pos_C+1)%xCity]; l1=temp[pos_C1]; l2=temp[(pos_C1+1)%xCity];
disChange+=city_dis[k1][l1]+city_dis[k2][l2]-city_dis[k1][k2]-city_dis[l1][l2];
invert(pos_C,pos_C1); pos_flag++;if(pos_flag>xCity-1)break; ////////////
pos_C++; if(pos_C>=xCity)pos_C=0; /**********************/
if( speed<edgeSpeed && disChange<0) { dis_p[i]+=disChange; disChange=0; tempTest(i);} //每有改变就计算
}
if(speed>=edgeSpeed && disChange<0 ) {dis_p[i]+=disChange;disChange=0; tempTest(i);} /////speed>=1500 &&
i++;
if(i>=xColony)
{ Ni++; GenNum++;i=0;
probab1=0.02*(1-GenNum*0.01/maxGen); //内逆转概率逐渐减小
if( speed<edgeSpeed && (rand()/32767.0<probab2) ) //
{ mapped();
probab2=0.05*(GenNum*2.0/maxGen+1); //部分交换概率逐渐增大
}
if(NOCHANGE-Ni<1) LastCP(); //5可改
if(GenNum>=maxGen || Ni>=NOCHANGE ) { printf("\n-------------------");printBest(GenNum,Ni); exit(1); }
}
}
}
|