//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // // Function Optimization using two membered Evolution Strategies (I) // Copyright (c) by ChanJin Chung, 1997-2001 // This source code is free for MCS course students at LTU MCS. This // cannot be redistributed to anyone without author's written permission. Also, // it cannot be used for commercial purpose unless you have got the written // permission from the author. //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #include #include #include #include const int NumPar = 2; // Max. dimension const int MaxGen = 50000; const int MaxTrial = 5; // ************************************************************ Common Functions float random0_1() // based on System Rand(). Between 0 <= x < 1 { return (float)rand() / RAND_MAX; } float randLH(float L, float H) // uniform randomnumbers between [Low..High) { return L + random0_1()*(H-L); } float gauss(float mean, float var) // Gaussian (normal) random number { float a, b, r; do{ a = random0_1(); b = random0_1(); } while ( a<=0 || b<=0 ); r = sqrt(var/2.0); r*= log(a/b); r+= mean; return r; } // float objfunc(float* X) or float objfunc(float X[NumPar]) { // Rosenbrock function // float score = (X[0]*X[0] - X[1]); // score *= score; // score = 100.0*score + (1.0 - X[0])*(1.0 - X[0]); // return score; return X[0]*X[0] + X[1]*X[1]; } // *********************************************************** Class Definitions class Individual { protected: float x[NumPar]; public: float value; void init(void); void show(void); void show(ofstream&); Individual variation(float); }; void Individual::init(void) { for(int j=0; j < NumPar; j++) x[j] = randLH(-5.0, 5.0); value = objfunc(x); } Individual Individual::variation(float steps) // a new child is born { Individual temp; for(int j=0; j < NumPar; j++) temp.x[j] = x[j] + gauss(0.0, steps); temp.value = objfunc(temp.x); return temp; } void Individual::show(void) { cout << endl; for(int j=0; j < NumPar; j++) { cout << '\t' << x[j]; } cout << '\t' << value << endl; } void Individual::show(ofstream& fout) { fout << endl; for(int j=0; j < NumPar; j++) { fout << '\t' << x[j]; } fout << '\t' << value << endl; } class Population { public: Individual parent; Individual child; void select(void); void snapshot(unsigned, unsigned); }; void Population::select() // minimization problem { if(child.value < parent.value) { // child survives, if is is better parent = child; } } void Population::snapshot(unsigned trial, unsigned gen) { if((gen)%10 == 0) { cout << "T=" << trial << " " << "G=" << gen << " " << "B=" << parent.value << " \r"; } } // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void main() { unsigned long totgen = 0; unsigned foundCnt = 0; ofstream fout("proj21.out"); for (unsigned trial = 1; trial <= MaxTrial; trial++) { Population p; randomize(); // system function p.parent.init(); // Evaluation included for (unsigned gen = 1; gen <= MaxGen; gen++) { p.child = p.parent.variation(0.1); // Evaluation included p.select(); p.snapshot(trial, gen); if(p.parent.value < 0.000005) { cout << endl << "Solution found at generation # " << gen << endl; totgen += gen; foundCnt++; break; } } p.parent.show(); fout << "Trial = " << trial; p.parent.show(fout); cout << endl; } float ss; ss = float(foundCnt)/float(MaxTrial)*100.0; cout << "** System Success = " << ss << "%"; fout << "** System Success = " << ss << "%"; totgen += (MaxTrial-foundCnt)*MaxGen; cout << endl << "** Total Number of Generations used = " << totgen; fout << endl << "** Total Number of Generations used = " << totgen; cout << endl << "** Average Number of Generations used = " << totgen / MaxTrial; fout << endl << "** Average Number of Generations used = " << totgen / MaxTrial; }