续2
ahe2 2003-01-27 10:33:56 //population.h
#include"chrom.h"
//#include<fstream>
//const int
const int POPSIZE=7;
const int SAMPLESIZE=10;
const char* SAMPLE_FILE="..\\..\\sample.txt";
const char* SAMPLE_RESULT_FILE="..\\..\\sample_result.txt";
//const char* OLDGEN_FILE="..\\..\\oldgen.txt";
//const char* NEWGEN_FILE="..\\..\\newgen.txt";
ifstream sample_file(SAMPLE_FILE);
ifstream sample_result_file(SAMPLE_RESULT_FILE);
//fstream oldgen_file(OLDGEN_FILE);
//fstream newgen_file(NEWGEN_FILE);
double sample[SAMPLESIZE];
double sample_result[SAMPLESIZE];
//string oldgen[POPSIZE];
//string newgen[POPSIZE];
void ReadSample() {//to arrays
string s;
if(!sample_file || !sample_result_file) {
cout<<"NO sample FILE(s)!";
exit(0);
}
for(int i=0; i<SAMPLESIZE; ++i) {
sample_file>>sample[i];
cout<<sample[i]<<'\t';
sample_result_file>>sample_result[i];
}
}//ReadSample()
const int SUMRATE=7;
class Pop{
public:
string the_strings[POPSIZE];
double result[POPSIZE];
static int RATE1R;
static int RATE2R;
static int RATEGR;
Chrom chroms[POPSIZE];
Pop();
Chrom* GetBody() {return chroms;}
void Evaluate3(double v);
static void CreateRRates(void);
void Recombine1(void);
void Recombine2(void);
void RecombineGene(void);
void Mutate(void);
void CountFit (double error);
void Scaling(double error);
void printChroms(void); //test onli
void ExchangeGene(int no1, int no2);
};//class Pop
int Pop::RATE1R = 1;
int Pop::RATE2R = 1;
int Pop::RATEGR = 1;
void Pop::Mutate() {
for(int i=0; i<POPSIZE; ++i) {
this->chroms [i].Mutate ();
}
}
void Pop::Evaluate3(double v) {
for(int i=0; i<POPSIZE; ++i) {
chroms[i].Evaluate2(v);
cout<<chroms[i].Evaluate2(v)<<'\t';
//(*(p.GetBody ()+i))->Evaluate2(1);
}
}
void Pop::CreateRRates(void) {
srand(unsigned(time(0)));
while(Pop::RATE1R ==0 ||
Pop::RATE2R ==0 ||
Pop::RATEGR ==0) {
Pop::RATE1R = rand() % SUMRATE;
Pop::RATE2R = rand() % SUMRATE;
Pop::RATEGR =
SUMRATE - Pop::RATE2R - Pop::RATEGR;
}
}
void Pop::RecombineGene() {
int R = Pop::RATEGR;
if(0 == R) {
cout<<"Recombination Rate not inited!";
exit(0);
}
else { //T times Recombine1
int no1=0;
int no2=0;
int T = R * POPSIZE / SUMRATE;
// cout<<endl<<T<<endl;
srand(unsigned(time(0)));
for(int i=0; i<T; ++i) {
no1 = rand() % POPSIZE;
no2 = rand() % POPSIZE;
if(no1==no2) {
--i;
}
else {
cout<<endl<<no1<<'&'<<no2
<<" are to ExchangeGene ";
this->ExchangeGene(no1,no2);
}
}
}
}//RecombineGene()
void Pop::ExchangeGene(int n1, int n2) {
srand(unsigned(time(0)));
int pos = rand() % GENE_NUMBER;
cout<<"at gene "<<pos<<endl;
string s1=this->chroms [n1].genes [pos].GetString();
string s2=this->chroms [n2].genes [pos].GetString();
cout<<endl<<s1<<endl<<s2<<endl;
s1+=this->chroms [n1].the_string.substr
(pos*GENELEN, GENELEN);
s2+=this->chroms [n2].the_string.substr
(pos*GENELEN, GENELEN);
cout<<endl<<s1<<endl<<s2<<endl;
// Chrom *p2=new Chrom(s2);
this->chroms[n1] =Chrom(s1) ;
// this->chroms[n2] =c1 ;
}
Pop::Pop() {
string cs="";
string s="";
srand((unsigned)time(0));
for(int i=0; i<CHROMLEN * POPSIZE; ++i) {
if(i % GENELEN > HEADLEN-1)
s += 'a';
else
s += CHARSET [rand() % CHARSETLEN];
}
for(i=0; i<POPSIZE; ++i) {
cs = s.substr (i*CHROMLEN, CHROMLEN);
cout<<i<<':'<<cs<<endl<<endl;
Chrom c(cs);
this->chroms[i] = c;
this->result[i]=0;
this->the_strings[i]=cs;
}
}
void Pop::Scaling(double error) {
this->CountFit (error);
int max=0;
for(int i=0; i<POPSIZE; i++) {
if(max < result[i])
max=result[i];
}
for(i=0; i<POPSIZE; i++) {
result[i] = result[i] * 1000 / max;
result[i] = 1000 - result[i];
cout<<endl;
cout<<"Scaled fitnesses:"<<endl;
cout<<result[i]<<endl;
}
}
void Pop::CountFit (double error) {//to array result[]
for(int i=0; i<POPSIZE; ++i) {
cout<<this->chroms [i].CountFit
(sample[i], sample_result[i], error)
<<'\t';
}
}
void Pop::printChroms(void) {
for(int i=0; i<POPSIZE; ++i)
cout<<i<<':'
<<this->chroms [i].GetString ()
<<endl;
}
//main.cpp
//#include"chrom.h"
#include"population.h"
const int G = 50;
void main() {
ReadSample();
Pop p;
p.printChroms ();
p.Evaluate3 (1);
p.RecombineGene();
cout<<endl;
p.printChroms ();
p.Evaluate3 (1);
}
/*
BTree b[2];
b[1]= BTree("****aaaaa");
cout<<b[1].GetString ()<<endl;
b[1].Traverse(cout,b[1].GetRoot ());
}*/
/* Chrom c;
cout<<c.GetString()<<endl;
c=Chrom("----aaaaa----aaaaa----aaaaa");
cout<<c.GetString()<<endl;
cout<<c.Evaluate2(1);
}*/
/* BTree b;
// BTree ba("****aaaaa");
b= BTree("****aaaaa");
cout<<b.GetString ()<<endl;
b.Traverse(cout,b.GetRoot ());
}
}*/
// p.CountFit (RERROR);
/*
int r=RERROR;
for(int i=0; i<G; ++i) {
p.CreateRRates ();
p.CountFit (r);
r/=MULTIPLE;
p.RecombineGene();
}
for(i=0; i<goodno; ++i)
cout<<good[i];
*/