B
Baby Lion
hi,everyone ,
one Runtime error occur when running , maybe it's about assigning
memory .
I need your help , thanks.
It's about the travelling sellman problem .
//#include "stdafx.h"
//TSPÎÊÌâµÄ½â·¨
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include "assert.h"
#include "iostream"
using namespace std;
struct city{
double x,y;};
city * cities;
double * dis;
double random1()//·µ»Ø[0,1)Ö®¼äµÄdouble
{
return ((double)rand())/RAND_MAX;
}
/*
template <class T>
void swap( T& a, T& b)
{ T c = a; a = b; b=c;}*/
int random()
{ return rand();}
inline double distance(int CityCnt, char a, char b)
{
double disX = cities[a].x -cities.x;
double disY = cities[a].y -cities.y;
return sqrt(disX*disX+disY*disY);
}
class Generation;
class Member{
public:
char * bytes;//ȾɫÌå
double dist;
static int Length ; // to denote using how many bytes
static double p; //±äÒì¸ÅÂÊ
Member(const Member &a) ;
Member();
~Member();
void bianyi();
double Adaptablity (double miniS); //¸æËßÕâÒ»´úµÄ×î¶Ì·¾¶£¬ÇóAdaptability
void diyidai();
void birth(Generation& g);
double Dis();
Member& operator=(Member& x);
};
class Generation {
public:
static int msize; // mn means MemberSize£¨ÖÖȺµÄ´óС£©
int GCount; //¼Ç¼ÏÖÔÚ´¦ÓÚµÚ¼¸´ú
double * board; //ÂÖÅÌ
static double p; // ±äÒì»ú»á
class Member * members; //¸öÌåÊý×é
double miniS;//µ±Ç°´úµÄ×î¶Ì·³Ì
double CalcMiniDis();
void PrintAllDis();
void CalcDis();
void setBoard(); //ÉèÖÃÂÛÅÌ
void PrintBoard();
void jiaocha(Member & a, Member & b); //½»²æ
void bianyi();//¸öÌå±äÒì
void FG();
Generation& birth();
~Generation(){ delete [] board; delete[] members;}
Generation(){
board = new double[msize];
members = new Member[msize];
}
};
void Generation::CalcDis()
{
for(int i=0;i<msize;i++)
members.dist = members.Dis();
}
double Generation::CalcMiniDis()
{
double s =members[0].dist;
for(int i=0;i<msize;i++)
{
if(s>members.dist) s=members.dist;
}
this->miniS= s;
return s;
}
void Generation:rintBoard()
{
cout<<endl;
for(int i=0;i<msize;i++)
cout<<board<< " ";
cout<<endl;
}
void Generation::bianyi()
{
for(int i=1;i<Generation::msize;i++)
{
this->members.bianyi();
}
}
Member::~Member()
{
delete []bytes;
}
Member::Member(const Member &a)
{
this->bytes = new char[Length];
for(int i =0 ;i<Member::Length;i++)
{ this->bytes = a.bytes;}
}
void Member::bianyi() //±äÒì
{
double p1=random1();
while (p1<p)
{
int x1 = rand()%Member::Length; int x2 = rand()%Member::Length;
swap<char>(bytes[x1],bytes[x2]);
p1 = random1();
}
//
cout<<endl;
for(int i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
void Member::diyidai() //µÚÒ»´úµÄmembers
{
int i,j;
for( i=0;i<Member::Length;i++)
{bytes = i;}
for( i=0;i<Member::Length;i++)
{ j = random()%Member::Length; swap<char>(bytes,bytes[j]);}
//
cout<<endl;
for(i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
Member::Member()
{
bytes = new char[Member::Length];
}
void Generation:rintAllDis()
{
cout<<endl;
for(int i=0;i<Generation::msize;i++)
cout<<members.dist<<" ";
cout<<endl;
}
inline double Member:is()
{
double s = 0 ;
for(int i=0;i<Length-1;i++)
{ double temp = dis[bytes*Member::Length+bytes[i+1]];
s+=temp;
}
s+= dis[bytes[0]*Member::Length+bytes[Member::Length]];
this->dist = s;
return s;
}
inline double Member::Adaptablity(double miniS)//¼ÆËãÊÊÓ¦¶È //Ϊ×Ü·³ÌµÄƽ·½µÄµ¹Êý
{ double s =this->dist;
s -= 0.9 * miniS; s*=s;
return 1000000/s;
}
void Generation::FG() //FirstGeneration
{
for(int i = 0; i<Generation::msize;i++)
members.diyidai();
}
Generation& Generation::birth()
{
Generation * child = new Generation();
for(int i=0;i<msize;i++)
{
double p1 = random1();
int j;
for(j=0;j<Generation::msize && this->board[j]<p1;j++);
//delete[] child->members.bytes;
child->members = this->members[j];
cout<<i<<endl;
}
return *child;
}
Member& Member:perator=(Member& x) //
{
if(this->bytes != NULL) delete[] this->bytes;
if(NULL==(this->bytes = new char[Member::Length])) cout<<"memory
error";
for(int i=0;i<Member::Length;i++)
this->bytes=x.bytes;
return *this;
}
void Generation::jiaocha(Member &a,Member &b) //½»²æ
{
//
cout<<"positionxxx"<<endl;
cout<<endl;
for(int p=0;p<Member::Length;p++) cout<<(int)b.bytes[p]<<" ";
cout<<endl;
bool *flagA=new bool[Member::Length];
bool *flagB=new bool[Member::Length];
memset(flagA,0,sizeof(flagA));
memset(flagB,0,sizeof(flagB));
char * Abytes = new char[Member::Length];
char * Bbytes = new char[Member::Length];
for(int i=0;i<Member::Length;i++)
{Abytes=a.bytes;Bbytes=b.bytes;}
int x1 = random()%Member::Length;
int x2 = random()%Member::Length;
if(x1>x2) swap<int>(x1,x2);
i=x1;int j=0;
for(;i<=x2;i++,j++)
{ Abytes[j]=a.bytes;Bbytes[j]=a.bytes;
flagA[Abytes[j]]=true; flagB[Bbytes[j]]=true;}
int j1=j,j2=j;
for(int k=0;k<Member::Length;k++)
{
char temp = b.bytes[k];
if(flagA[temp]==false) Abytes[j1++] =temp;
} assert(j1==Member::Length);
for(k=0;k<Member::Length;k++)
{
char temp = a.bytes[k];
if(flagB[temp]==false) Bbytes[j2++] = temp;
} assert(j2==Member::Length);
delete[] a.bytes;
delete[] b.bytes;
a.bytes = Abytes; b.bytes = Bbytes;
delete [] flagA; delete [] flagB;
}
void Generation::setBoard()
{ double s = 0;
for(int i=0;i<msize;i++)
{ s+=members.Adaptablity(this->miniS); board=s;}
for(i=0;i<msize;i++)//normalize
{ board/=board[msize-1];}
}
//DEL void Generation::Init()
//DEL {
//DEL
//DEL }
int Member::Length=0;
double Member: = 0.1;
int Generation::msize = 10;
void main()
{
freopen("f:\\oliver30.txt","r",stdin);
srand((int)time(0));
int CntCity,i,j;
cin>>CntCity;
Member::Length = CntCity;
cities = new city[CntCity];
double temp;
for( i=0;i<CntCity;i++)
{ cin>>temp>>cities.x>>cities.y>>temp;}
for(i=0;i<CntCity;i++)
{cout<<cities.x<<" "<<cities.y<<endl;}
dis = new double[CntCity*CntCity]; //done
for(i=0;i<CntCity;i++)
{
for(j=0;j<CntCity;j++)
{ dis[i*CntCity+j]= distance(CntCity,(char)i,(char)j);
cout<<dis[i*CntCity+j]<<" ";}cout<<endl;
}
Generation * g1 = new Generation(),gt;
g1->FG();g1->CalcDis();g1->CalcMiniDis();
g1->setBoard();g1->PrintAllDis();g1->PrintBoard();
for(i=0;i<=100;i++)
{
Generation * g2 = &(g1->birth());
for(j=0;j<Generation::msize/2;j++)
{g2->jiaocha(g2->members,g2->members[i+Generation::msize]);}
g2->CalcDis();
g2->CalcMiniDis();
g2->setBoard();
g2->PrintAllDis();
g2->PrintBoard();
cout<<g1->miniS<<endl;
delete g1;
cout<<" xxxxxxxxxxxx"<<endl;
g1 = g2;
}
}
//#include "stdafx.h"
//TSPÎÊÌâµÄ½â·¨
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include "assert.h"
#include "iostream"
using namespace std;
struct city{
double x,y;};
city * cities;
double * dis;
double random1()//·µ»Ø[0,1)Ö®¼äµÄdouble
{
return ((double)rand())/RAND_MAX;
}
/*
template <class T>
void swap( T& a, T& b)
{ T c = a; a = b; b=c;}*/
int random()
{ return rand();}
inline double distance(int CityCnt, char a, char b)
{
double disX = cities[a].x -cities.x;
double disY = cities[a].y -cities.y;
return sqrt(disX*disX+disY*disY);
}
class Generation;
class Member{
public:
char * bytes;//ȾɫÌå
double dist;
static int Length ; // to denote using how many bytes
static double p; //±äÒì¸ÅÂÊ
Member(const Member &a) ;
Member();
~Member();
void bianyi();
double Adaptablity (double miniS); //¸æËßÕâÒ»´úµÄ×î¶Ì·¾¶£¬ÇóAdaptability
void diyidai();
void birth(Generation& g);
double Dis();
Member& operator=(Member& x);
};
class Generation {
public:
static int msize; // mn means MemberSize£¨ÖÖȺµÄ´óС£©
int GCount; //¼Ç¼ÏÖÔÚ´¦ÓÚµÚ¼¸´ú
double * board; //ÂÖÅÌ
static double p; // ±äÒì»ú»á
class Member * members; //¸öÌåÊý×é
double miniS;//µ±Ç°´úµÄ×î¶Ì·³Ì
double CalcMiniDis();
void PrintAllDis();
void CalcDis();
void setBoard(); //ÉèÖÃÂÛÅÌ
void PrintBoard();
void jiaocha(Member & a, Member & b); //½»²æ
void bianyi();//¸öÌå±äÒì
void FG();
Generation& birth();
~Generation(){ delete [] board; delete[] members;}
Generation(){
board = new double[msize];
members = new Member[msize];
}
};
void Generation::CalcDis()
{
for(int i=0;i<msize;i++)
members.dist = members.Dis();
}
double Generation::CalcMiniDis()
{
double s =members[0].dist;
for(int i=0;i<msize;i++)
{
if(s>members.dist) s=members.dist;
}
this->miniS= s;
return s;
}
void Generation:rintBoard()
{
cout<<endl;
for(int i=0;i<msize;i++)
cout<<board<< " ";
cout<<endl;
}
void Generation::bianyi()
{
for(int i=1;i<Generation::msize;i++)
{
this->members.bianyi();
}
}
Member::~Member()
{
delete []bytes;
}
Member::Member(const Member &a)
{
this->bytes = new char[Length];
for(int i =0 ;i<Member::Length;i++)
{ this->bytes = a.bytes;}
}
void Member::bianyi() //±äÒì
{
double p1=random1();
while (p1<p)
{
int x1 = rand()%Member::Length; int x2 = rand()%Member::Length;
swap<char>(bytes[x1],bytes[x2]);
p1 = random1();
}
//
cout<<endl;
for(int i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
void Member::diyidai() //µÚÒ»´úµÄmembers
{
int i,j;
for( i=0;i<Member::Length;i++)
{bytes = i;}
for( i=0;i<Member::Length;i++)
{ j = random()%Member::Length; swap<char>(bytes,bytes[j]);}
//
cout<<endl;
for(i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
Member::Member()
{
bytes = new char[Member::Length];
}
void Generation:rintAllDis()
{
cout<<endl;
for(int i=0;i<Generation::msize;i++)
cout<<members.dist<<" ";
cout<<endl;
}
inline double Member:is()
{
double s = 0 ;
for(int i=0;i<Length-1;i++)
{ double temp = dis[bytes*Member::Length+bytes[i+1]];
s+=temp;
}
s+= dis[bytes[0]*Member::Length+bytes[Member::Length]];
this->dist = s;
return s;
}
inline double Member::Adaptablity(double miniS)//¼ÆËãÊÊÓ¦¶È //Ϊ×Ü·³ÌµÄƽ·½µÄµ¹Êý
{ double s =this->dist;
s -= 0.9 * miniS; s*=s;
return 1000000/s;
}
void Generation::FG() //FirstGeneration
{
for(int i = 0; i<Generation::msize;i++)
members.diyidai();
}
Generation& Generation::birth()
{
Generation * child = new Generation();
for(int i=0;i<msize;i++)
{
double p1 = random1();
int j;
for(j=0;j<Generation::msize && this->board[j]<p1;j++);
//delete[] child->members.bytes;
child->members = this->members[j];
cout<<i<<endl;
}
return *child;
}
Member& Member:perator=(Member& x) //
{
if(this->bytes != NULL) delete[] this->bytes;
if(NULL==(this->bytes = new char[Member::Length])) cout<<"memory
error";
for(int i=0;i<Member::Length;i++)
this->bytes=x.bytes;
return *this;
}
void Generation::jiaocha(Member &a,Member &b) //½»²æ
{
//
cout<<"positionxxx"<<endl;
cout<<endl;
for(int p=0;p<Member::Length;p++) cout<<(int)b.bytes[p]<<" ";
cout<<endl;
bool *flagA=new bool[Member::Length];
bool *flagB=new bool[Member::Length];
memset(flagA,0,sizeof(flagA));
memset(flagB,0,sizeof(flagB));
char * Abytes = new char[Member::Length];
char * Bbytes = new char[Member::Length];
for(int i=0;i<Member::Length;i++)
{Abytes=a.bytes;Bbytes=b.bytes;}
int x1 = random()%Member::Length;
int x2 = random()%Member::Length;
if(x1>x2) swap<int>(x1,x2);
i=x1;int j=0;
for(;i<=x2;i++,j++)
{ Abytes[j]=a.bytes;Bbytes[j]=a.bytes;
flagA[Abytes[j]]=true; flagB[Bbytes[j]]=true;}
int j1=j,j2=j;
for(int k=0;k<Member::Length;k++)
{
char temp = b.bytes[k];
if(flagA[temp]==false) Abytes[j1++] =temp;
} assert(j1==Member::Length);
for(k=0;k<Member::Length;k++)
{
char temp = a.bytes[k];
if(flagB[temp]==false) Bbytes[j2++] = temp;
} assert(j2==Member::Length);
delete[] a.bytes;
delete[] b.bytes;
a.bytes = Abytes; b.bytes = Bbytes;
delete [] flagA; delete [] flagB;
}
void Generation::setBoard()
{ double s = 0;
for(int i=0;i<msize;i++)
{ s+=members.Adaptablity(this->miniS); board=s;}
for(i=0;i<msize;i++)//normalize
{ board/=board[msize-1];}
}
//DEL void Generation::Init()
//DEL {
//DEL
//DEL }
int Member::Length=0;
double Member: = 0.1;
int Generation::msize = 10;
void main()
{
freopen("f:\\oliver30.txt","r",stdin);
srand((int)time(0));
int CntCity,i,j;
cin>>CntCity;
Member::Length = CntCity;
cities = new city[CntCity];
double temp;
for( i=0;i<CntCity;i++)
{ cin>>temp>>cities.x>>cities.y>>temp;}
for(i=0;i<CntCity;i++)
{cout<<cities.x<<" "<<cities.y<<endl;}
dis = new double[CntCity*CntCity]; //done
for(i=0;i<CntCity;i++)
{
for(j=0;j<CntCity;j++)
{ dis[i*CntCity+j]= distance(CntCity,(char)i,(char)j);
cout<<dis[i*CntCity+j]<<" ";}cout<<endl;
}
Generation * g1 = new Generation(),gt;
g1->FG();g1->CalcDis();g1->CalcMiniDis();
g1->setBoard();g1->PrintAllDis();g1->PrintBoard();
for(i=0;i<=100;i++)
{
Generation * g2 = &(g1->birth());
for(j=0;j<Generation::msize/2;j++)
{g2->jiaocha(g2->members,g2->members[i+Generation::msize]);}
g2->CalcDis();
g2->CalcMiniDis();
g2->setBoard();
g2->PrintAllDis();
g2->PrintBoard();
cout<<g1->miniS<<endl;
delete g1;
cout<<" xxxxxxxxxxxx"<<endl;
g1 = g2;
}
}
one Runtime error occur when running , maybe it's about assigning
memory .
I need your help , thanks.
It's about the travelling sellman problem .
//#include "stdafx.h"
//TSPÎÊÌâµÄ½â·¨
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include "assert.h"
#include "iostream"
using namespace std;
struct city{
double x,y;};
city * cities;
double * dis;
double random1()//·µ»Ø[0,1)Ö®¼äµÄdouble
{
return ((double)rand())/RAND_MAX;
}
/*
template <class T>
void swap( T& a, T& b)
{ T c = a; a = b; b=c;}*/
int random()
{ return rand();}
inline double distance(int CityCnt, char a, char b)
{
double disX = cities[a].x -cities.x;
double disY = cities[a].y -cities.y;
return sqrt(disX*disX+disY*disY);
}
class Generation;
class Member{
public:
char * bytes;//ȾɫÌå
double dist;
static int Length ; // to denote using how many bytes
static double p; //±äÒì¸ÅÂÊ
Member(const Member &a) ;
Member();
~Member();
void bianyi();
double Adaptablity (double miniS); //¸æËßÕâÒ»´úµÄ×î¶Ì·¾¶£¬ÇóAdaptability
void diyidai();
void birth(Generation& g);
double Dis();
Member& operator=(Member& x);
};
class Generation {
public:
static int msize; // mn means MemberSize£¨ÖÖȺµÄ´óС£©
int GCount; //¼Ç¼ÏÖÔÚ´¦ÓÚµÚ¼¸´ú
double * board; //ÂÖÅÌ
static double p; // ±äÒì»ú»á
class Member * members; //¸öÌåÊý×é
double miniS;//µ±Ç°´úµÄ×î¶Ì·³Ì
double CalcMiniDis();
void PrintAllDis();
void CalcDis();
void setBoard(); //ÉèÖÃÂÛÅÌ
void PrintBoard();
void jiaocha(Member & a, Member & b); //½»²æ
void bianyi();//¸öÌå±äÒì
void FG();
Generation& birth();
~Generation(){ delete [] board; delete[] members;}
Generation(){
board = new double[msize];
members = new Member[msize];
}
};
void Generation::CalcDis()
{
for(int i=0;i<msize;i++)
members.dist = members.Dis();
}
double Generation::CalcMiniDis()
{
double s =members[0].dist;
for(int i=0;i<msize;i++)
{
if(s>members.dist) s=members.dist;
}
this->miniS= s;
return s;
}
void Generation:rintBoard()
{
cout<<endl;
for(int i=0;i<msize;i++)
cout<<board<< " ";
cout<<endl;
}
void Generation::bianyi()
{
for(int i=1;i<Generation::msize;i++)
{
this->members.bianyi();
}
}
Member::~Member()
{
delete []bytes;
}
Member::Member(const Member &a)
{
this->bytes = new char[Length];
for(int i =0 ;i<Member::Length;i++)
{ this->bytes = a.bytes;}
}
void Member::bianyi() //±äÒì
{
double p1=random1();
while (p1<p)
{
int x1 = rand()%Member::Length; int x2 = rand()%Member::Length;
swap<char>(bytes[x1],bytes[x2]);
p1 = random1();
}
//
cout<<endl;
for(int i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
void Member::diyidai() //µÚÒ»´úµÄmembers
{
int i,j;
for( i=0;i<Member::Length;i++)
{bytes = i;}
for( i=0;i<Member::Length;i++)
{ j = random()%Member::Length; swap<char>(bytes,bytes[j]);}
//
cout<<endl;
for(i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
Member::Member()
{
bytes = new char[Member::Length];
}
void Generation:rintAllDis()
{
cout<<endl;
for(int i=0;i<Generation::msize;i++)
cout<<members.dist<<" ";
cout<<endl;
}
inline double Member:is()
{
double s = 0 ;
for(int i=0;i<Length-1;i++)
{ double temp = dis[bytes*Member::Length+bytes[i+1]];
s+=temp;
}
s+= dis[bytes[0]*Member::Length+bytes[Member::Length]];
this->dist = s;
return s;
}
inline double Member::Adaptablity(double miniS)//¼ÆËãÊÊÓ¦¶È //Ϊ×Ü·³ÌµÄƽ·½µÄµ¹Êý
{ double s =this->dist;
s -= 0.9 * miniS; s*=s;
return 1000000/s;
}
void Generation::FG() //FirstGeneration
{
for(int i = 0; i<Generation::msize;i++)
members.diyidai();
}
Generation& Generation::birth()
{
Generation * child = new Generation();
for(int i=0;i<msize;i++)
{
double p1 = random1();
int j;
for(j=0;j<Generation::msize && this->board[j]<p1;j++);
//delete[] child->members.bytes;
child->members = this->members[j];
cout<<i<<endl;
}
return *child;
}
Member& Member:perator=(Member& x) //
{
if(this->bytes != NULL) delete[] this->bytes;
if(NULL==(this->bytes = new char[Member::Length])) cout<<"memory
error";
for(int i=0;i<Member::Length;i++)
this->bytes=x.bytes;
return *this;
}
void Generation::jiaocha(Member &a,Member &b) //½»²æ
{
//
cout<<"positionxxx"<<endl;
cout<<endl;
for(int p=0;p<Member::Length;p++) cout<<(int)b.bytes[p]<<" ";
cout<<endl;
bool *flagA=new bool[Member::Length];
bool *flagB=new bool[Member::Length];
memset(flagA,0,sizeof(flagA));
memset(flagB,0,sizeof(flagB));
char * Abytes = new char[Member::Length];
char * Bbytes = new char[Member::Length];
for(int i=0;i<Member::Length;i++)
{Abytes=a.bytes;Bbytes=b.bytes;}
int x1 = random()%Member::Length;
int x2 = random()%Member::Length;
if(x1>x2) swap<int>(x1,x2);
i=x1;int j=0;
for(;i<=x2;i++,j++)
{ Abytes[j]=a.bytes;Bbytes[j]=a.bytes;
flagA[Abytes[j]]=true; flagB[Bbytes[j]]=true;}
int j1=j,j2=j;
for(int k=0;k<Member::Length;k++)
{
char temp = b.bytes[k];
if(flagA[temp]==false) Abytes[j1++] =temp;
} assert(j1==Member::Length);
for(k=0;k<Member::Length;k++)
{
char temp = a.bytes[k];
if(flagB[temp]==false) Bbytes[j2++] = temp;
} assert(j2==Member::Length);
delete[] a.bytes;
delete[] b.bytes;
a.bytes = Abytes; b.bytes = Bbytes;
delete [] flagA; delete [] flagB;
}
void Generation::setBoard()
{ double s = 0;
for(int i=0;i<msize;i++)
{ s+=members.Adaptablity(this->miniS); board=s;}
for(i=0;i<msize;i++)//normalize
{ board/=board[msize-1];}
}
//DEL void Generation::Init()
//DEL {
//DEL
//DEL }
int Member::Length=0;
double Member: = 0.1;
int Generation::msize = 10;
void main()
{
freopen("f:\\oliver30.txt","r",stdin);
srand((int)time(0));
int CntCity,i,j;
cin>>CntCity;
Member::Length = CntCity;
cities = new city[CntCity];
double temp;
for( i=0;i<CntCity;i++)
{ cin>>temp>>cities.x>>cities.y>>temp;}
for(i=0;i<CntCity;i++)
{cout<<cities.x<<" "<<cities.y<<endl;}
dis = new double[CntCity*CntCity]; //done
for(i=0;i<CntCity;i++)
{
for(j=0;j<CntCity;j++)
{ dis[i*CntCity+j]= distance(CntCity,(char)i,(char)j);
cout<<dis[i*CntCity+j]<<" ";}cout<<endl;
}
Generation * g1 = new Generation(),gt;
g1->FG();g1->CalcDis();g1->CalcMiniDis();
g1->setBoard();g1->PrintAllDis();g1->PrintBoard();
for(i=0;i<=100;i++)
{
Generation * g2 = &(g1->birth());
for(j=0;j<Generation::msize/2;j++)
{g2->jiaocha(g2->members,g2->members[i+Generation::msize]);}
g2->CalcDis();
g2->CalcMiniDis();
g2->setBoard();
g2->PrintAllDis();
g2->PrintBoard();
cout<<g1->miniS<<endl;
delete g1;
cout<<" xxxxxxxxxxxx"<<endl;
g1 = g2;
}
}
//#include "stdafx.h"
//TSPÎÊÌâµÄ½â·¨
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include "assert.h"
#include "iostream"
using namespace std;
struct city{
double x,y;};
city * cities;
double * dis;
double random1()//·µ»Ø[0,1)Ö®¼äµÄdouble
{
return ((double)rand())/RAND_MAX;
}
/*
template <class T>
void swap( T& a, T& b)
{ T c = a; a = b; b=c;}*/
int random()
{ return rand();}
inline double distance(int CityCnt, char a, char b)
{
double disX = cities[a].x -cities.x;
double disY = cities[a].y -cities.y;
return sqrt(disX*disX+disY*disY);
}
class Generation;
class Member{
public:
char * bytes;//ȾɫÌå
double dist;
static int Length ; // to denote using how many bytes
static double p; //±äÒì¸ÅÂÊ
Member(const Member &a) ;
Member();
~Member();
void bianyi();
double Adaptablity (double miniS); //¸æËßÕâÒ»´úµÄ×î¶Ì·¾¶£¬ÇóAdaptability
void diyidai();
void birth(Generation& g);
double Dis();
Member& operator=(Member& x);
};
class Generation {
public:
static int msize; // mn means MemberSize£¨ÖÖȺµÄ´óС£©
int GCount; //¼Ç¼ÏÖÔÚ´¦ÓÚµÚ¼¸´ú
double * board; //ÂÖÅÌ
static double p; // ±äÒì»ú»á
class Member * members; //¸öÌåÊý×é
double miniS;//µ±Ç°´úµÄ×î¶Ì·³Ì
double CalcMiniDis();
void PrintAllDis();
void CalcDis();
void setBoard(); //ÉèÖÃÂÛÅÌ
void PrintBoard();
void jiaocha(Member & a, Member & b); //½»²æ
void bianyi();//¸öÌå±äÒì
void FG();
Generation& birth();
~Generation(){ delete [] board; delete[] members;}
Generation(){
board = new double[msize];
members = new Member[msize];
}
};
void Generation::CalcDis()
{
for(int i=0;i<msize;i++)
members.dist = members.Dis();
}
double Generation::CalcMiniDis()
{
double s =members[0].dist;
for(int i=0;i<msize;i++)
{
if(s>members.dist) s=members.dist;
}
this->miniS= s;
return s;
}
void Generation:rintBoard()
{
cout<<endl;
for(int i=0;i<msize;i++)
cout<<board<< " ";
cout<<endl;
}
void Generation::bianyi()
{
for(int i=1;i<Generation::msize;i++)
{
this->members.bianyi();
}
}
Member::~Member()
{
delete []bytes;
}
Member::Member(const Member &a)
{
this->bytes = new char[Length];
for(int i =0 ;i<Member::Length;i++)
{ this->bytes = a.bytes;}
}
void Member::bianyi() //±äÒì
{
double p1=random1();
while (p1<p)
{
int x1 = rand()%Member::Length; int x2 = rand()%Member::Length;
swap<char>(bytes[x1],bytes[x2]);
p1 = random1();
}
//
cout<<endl;
for(int i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
void Member::diyidai() //µÚÒ»´úµÄmembers
{
int i,j;
for( i=0;i<Member::Length;i++)
{bytes = i;}
for( i=0;i<Member::Length;i++)
{ j = random()%Member::Length; swap<char>(bytes,bytes[j]);}
//
cout<<endl;
for(i=0;i<Member::Length;i++)
{ cout<<(int)bytes<<" ";}
cout<<endl;
}
Member::Member()
{
bytes = new char[Member::Length];
}
void Generation:rintAllDis()
{
cout<<endl;
for(int i=0;i<Generation::msize;i++)
cout<<members.dist<<" ";
cout<<endl;
}
inline double Member:is()
{
double s = 0 ;
for(int i=0;i<Length-1;i++)
{ double temp = dis[bytes*Member::Length+bytes[i+1]];
s+=temp;
}
s+= dis[bytes[0]*Member::Length+bytes[Member::Length]];
this->dist = s;
return s;
}
inline double Member::Adaptablity(double miniS)//¼ÆËãÊÊÓ¦¶È //Ϊ×Ü·³ÌµÄƽ·½µÄµ¹Êý
{ double s =this->dist;
s -= 0.9 * miniS; s*=s;
return 1000000/s;
}
void Generation::FG() //FirstGeneration
{
for(int i = 0; i<Generation::msize;i++)
members.diyidai();
}
Generation& Generation::birth()
{
Generation * child = new Generation();
for(int i=0;i<msize;i++)
{
double p1 = random1();
int j;
for(j=0;j<Generation::msize && this->board[j]<p1;j++);
//delete[] child->members.bytes;
child->members = this->members[j];
cout<<i<<endl;
}
return *child;
}
Member& Member:perator=(Member& x) //
{
if(this->bytes != NULL) delete[] this->bytes;
if(NULL==(this->bytes = new char[Member::Length])) cout<<"memory
error";
for(int i=0;i<Member::Length;i++)
this->bytes=x.bytes;
return *this;
}
void Generation::jiaocha(Member &a,Member &b) //½»²æ
{
//
cout<<"positionxxx"<<endl;
cout<<endl;
for(int p=0;p<Member::Length;p++) cout<<(int)b.bytes[p]<<" ";
cout<<endl;
bool *flagA=new bool[Member::Length];
bool *flagB=new bool[Member::Length];
memset(flagA,0,sizeof(flagA));
memset(flagB,0,sizeof(flagB));
char * Abytes = new char[Member::Length];
char * Bbytes = new char[Member::Length];
for(int i=0;i<Member::Length;i++)
{Abytes=a.bytes;Bbytes=b.bytes;}
int x1 = random()%Member::Length;
int x2 = random()%Member::Length;
if(x1>x2) swap<int>(x1,x2);
i=x1;int j=0;
for(;i<=x2;i++,j++)
{ Abytes[j]=a.bytes;Bbytes[j]=a.bytes;
flagA[Abytes[j]]=true; flagB[Bbytes[j]]=true;}
int j1=j,j2=j;
for(int k=0;k<Member::Length;k++)
{
char temp = b.bytes[k];
if(flagA[temp]==false) Abytes[j1++] =temp;
} assert(j1==Member::Length);
for(k=0;k<Member::Length;k++)
{
char temp = a.bytes[k];
if(flagB[temp]==false) Bbytes[j2++] = temp;
} assert(j2==Member::Length);
delete[] a.bytes;
delete[] b.bytes;
a.bytes = Abytes; b.bytes = Bbytes;
delete [] flagA; delete [] flagB;
}
void Generation::setBoard()
{ double s = 0;
for(int i=0;i<msize;i++)
{ s+=members.Adaptablity(this->miniS); board=s;}
for(i=0;i<msize;i++)//normalize
{ board/=board[msize-1];}
}
//DEL void Generation::Init()
//DEL {
//DEL
//DEL }
int Member::Length=0;
double Member: = 0.1;
int Generation::msize = 10;
void main()
{
freopen("f:\\oliver30.txt","r",stdin);
srand((int)time(0));
int CntCity,i,j;
cin>>CntCity;
Member::Length = CntCity;
cities = new city[CntCity];
double temp;
for( i=0;i<CntCity;i++)
{ cin>>temp>>cities.x>>cities.y>>temp;}
for(i=0;i<CntCity;i++)
{cout<<cities.x<<" "<<cities.y<<endl;}
dis = new double[CntCity*CntCity]; //done
for(i=0;i<CntCity;i++)
{
for(j=0;j<CntCity;j++)
{ dis[i*CntCity+j]= distance(CntCity,(char)i,(char)j);
cout<<dis[i*CntCity+j]<<" ";}cout<<endl;
}
Generation * g1 = new Generation(),gt;
g1->FG();g1->CalcDis();g1->CalcMiniDis();
g1->setBoard();g1->PrintAllDis();g1->PrintBoard();
for(i=0;i<=100;i++)
{
Generation * g2 = &(g1->birth());
for(j=0;j<Generation::msize/2;j++)
{g2->jiaocha(g2->members,g2->members[i+Generation::msize]);}
g2->CalcDis();
g2->CalcMiniDis();
g2->setBoard();
g2->PrintAllDis();
g2->PrintBoard();
cout<<g1->miniS<<endl;
delete g1;
cout<<" xxxxxxxxxxxx"<<endl;
g1 = g2;
}
}