//***************************************************************** //////////////////////////////////////////////////////////////// // comment style2 //////////////////////////////////////////////////////////////// //---------------------------------------------comment style 3 /* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ #include "pch.h" #include "chrome.h" #include "primitiv.h" extern char scratch[100]; #ifdef FASTEVAL extern evalnode* IpGlobal; extern Chrome* ChromeGlobal; #endif #define FEA_NUM 39 #define VEC_NUM 1000 #define COM_NUM 100 #define MAX_CLASS_NUM 20 #define SEQ_NUM 5 int gVecNum = 0; int gClassNum = 0; int gFeaIndex = 0; int gVecIndex = 0; int gSeqIndex = 0; int gChromeNum = COM_NUM; float gMaxValue = 0; float gGate = 0; Chrome* gCommittee[COM_NUM]; int gClass[VEC_NUM][MAX_CLASS_NUM]; //************************************************************************** // Define your Problem class class Eval1Prob : public Problem { public: Eval1Prob(ChromeParams* p=NULL); // Initialize primitives, parameters, and data float fitness(Chrome* chrome); void label(Chrome* chrome); void setParams(); void readData(string& aFileName); float evaluation(); // Driving ingredient - GP fitness function //virtual FitnessValue* GetFitnessObj(); // Optional new behavior public: //params for the problem int mCommittees; int mGenerations; int mMaxExpr; float mError; string mDataFile; string mOutputDir; }; //************************************************************************** // static variables that make up the evaluation environment // Build your environment here as statics // where the evaluation functions can get to the data // the first position put the class label retval gFea[VEC_NUM][SEQ_NUM][FEA_NUM+1]; //************************************************************************** /////////////////// Problem specific functions and terminals // Use the OPDEF,EVAL,IP and CURCHROME defines // and the code will recompile for different eval // A terminal named "FeaEval" that returns a data value //OPDEF(FeaEval) {gFeaIndex++;if(gFeaIndex > FEA_NUM) gFeaIndex = 1;\ // return gFea[gVecIndex][gFeaIndex];} //OPDEF(SetId) {gFeaIndex++;if(gFeaIndex > FEA_NUM) gFeaIndex = 1;\ // return gFea[gVecIndex][gFeaIndex];} OPDEF(FeaEval) { gFeaIndex= EVAL; gSeqIndex = EVAL; gFeaIndex = ((gFeaIndex > FEA_NUM)||(gFeaIndex < 1)) ? 1:gFeaIndex; gSeqIndex = ((gSeqIndex >= SEQ_NUM)||(gSeqIndex < 0)) ? 0:gSeqIndex; return gFea[gVecIndex][gSeqIndex][gFeaIndex]; } //************************************************************************** ///////////////////////// PROBLEM definition Eval1Prob::Eval1Prob(ChromeParams* p) : Problem() { setParams(); AddF(new ConstFunc(300)); // REQUIRED as function 0 // Add some standard primitives AddF(new AddFunc(100)); AddF(new SubFunc(100)); AddF(new MulFunc(100)); AddF(new DivFunc(100)); // These are not used for SSPROB, but are available //AddF(new Add4Func(100)); //AddF(new Prog4Func(100)); //AddF(new SineFunc(100)); AddF(new AbsFunc(100)); AddF(new BoolFunc(100)); AddF(new NotFunc(100)); AddF(new AndFunc(100)); AddF(new OrFunc(100)); AddF(new IfFunc(100)); AddF(new IflteFunc(100)); // Add the problem specific primitives AddF(new Function(2,0,100,FeaEval,"FeaEval")); // Set some parameters p->params[pMaxExpr]=mMaxExpr; p->params[pRepeatEval]=1; // repeat evaluation with new case on copy p->params[pMuteShrinkWt]=30; // population tends to collapse toward 0 average value. Reduce // shrinkage p->params[pMuteWt]=20; // standard settings p->params[pCrossWt]=70; p->params[pMuteRate]=60; // compensate for small population (typically 1000) p->params[pAnnealCrossWt]=50; // add some annealing p->params[pAnnealMuteWt]=100; p->params[pTournSize]=6; //reduce greediness from 7 // If you need to load problem specific data for evaluation Or load // an environment (a map, a scenario) Do it here } float Eval1Prob::fitness(Chrome* chrome) // Regression type fitness function. // Calculate a sum squared error over some number of cases { int i, nAnswer,nClass; float err = 0; retval answer,diff; // Check some number of cases (20 here) for (gVecIndex=0; gVecIndex< gVecNum; gVecIndex++){ // Set up the evaluation environment // for a specific case // Evaluate the expression answer=chrome->evalAll(); answer = (answer >= 0) ? (answer): (-answer); // while (answer > gClassNum) // answer -= gClassNum; nAnswer = answer/gGate; //nAnswer /= gGate; nAnswer = nAnswer > gClassNum ? gClassNum: nAnswer; nAnswer = nAnswer <1 ? 1: nAnswer; nClass = gFea[gVecIndex][0][0]; //nClass = nClass == gClassNum ? 0: nClass; if (nAnswer != nClass) err++; // if ((answer < (gFea[gVecIndex][0]-1)/gClassNum*BIGFLOAT) // ||(answer >= gFea[gVecIndex][0]/gClassNum*BIGFLOAT)) // if (answer < (gFea[gVecIndex][0]-1) // ||(answer >= gFea[gVecIndex][0])) // err++; } //cout<<"{ " <nfitness->fvalue=(-err/gVecNum); return (-err/gVecNum); } void Eval1Prob::label(Chrome* chrome) // Regression type fitness function. // Calculate a sum squared error over some number of cases { int i, nAnswer,nClass; retval answer; // Check some number of cases (20 here) for (gVecIndex=0; gVecIndex< gVecNum; gVecIndex++){ // Set up the evaluation environment // for a specific case // Evaluate the expression answer=chrome->evalAll(); answer = (answer > 0) ? (answer): (-answer); // while (answer > gClassNum) // answer -= gClassNum; nAnswer = answer/gGate; //nAnswer /= gGate; nAnswer = nAnswer > gClassNum ? gClassNum: nAnswer; nAnswer = nAnswer <1 ? 1: nAnswer; //cout< gClass[gVecIndex][gClassNum+1]){ gClass[gVecIndex][gClassNum+1] = gClass[gVecIndex][nAnswer]; gClass[gVecIndex][0] = nAnswer; } } } float Eval1Prob::evaluation() // Regression type fitness function. // Calculate a sum squared error over some number of cases { int i, nAnswer,nClass; retval err = 0; // Check some number of cases (20 here) for (gVecIndex=0; gVecIndex< gVecNum; gVecIndex++){ if(gClass[gVecIndex][0] != gFea[gVecIndex][0][0]) err++; } return err/gVecNum; } void Eval1Prob::setParams() // set params for the experiment // { char buf[256]; ifstream inFile; inFile.open("params.text"); if(!inFile){ cout << "can't open params file "; return; } inFile.getline(buf,256); cout << endl<< buf; inFile>>mCommittees; cout << endl<< mCommittees; inFile.getline(buf,256); inFile.getline(buf,256); cout << endl<< buf; inFile>>mGenerations; cout << endl<< mGenerations; inFile.getline(buf,256); inFile.getline(buf,256); cout << endl<< buf; inFile>>mMaxExpr; cout << endl<< mMaxExpr; inFile.getline(buf,256); inFile.getline(buf,256); cout << endl<< buf; inFile>>mError; cout << endl<< mError; inFile.getline(buf,256); inFile.getline(buf,256); cout << endl<< buf; inFile>>mDataFile; cout << endl<< mDataFile; inFile.getline(buf,256); inFile.getline(buf,256); cout << endl<< buf; inFile>>mOutputDir; cout << endl<< mOutputDir; inFile.getline(buf,256); inFile.getline(buf,256); cout << endl<< buf; inFile>>gGate; cout << endl<< gGate; inFile.close(); gChromeNum = mCommittees; } void Eval1Prob::readData(string& aFileName) // set params for the experiment // { char label[8]; int j = 0,i=1; ifstream inFile; inFile.open(aFileName.c_str()); if(!inFile){ cout << "can't open file:"<0) inFile.getline(label,8,':'); gFea[j][k][0] = atof(label); gClassNum = gFea[j][k][0] > gClassNum ? gFea[j][k][0]:gClassNum; //cout <> gFea[j][k][i]; //cout << " "< gMaxValue ? gFea[j][k][i]:gMaxValue ; } //inFile.getline(label,8); //gFea[j][k][FEA_NUM] = atof(label); //cout << " "< gMaxValue? gFea[j][k][FEA_NUM]:gMaxValue; } //cout <<" "<< gFea[j][FEA_NUM]; j++; if (j > VEC_NUM - 1) break; } gClassNum = 5; cout <<"reading data completed"<readData(prob->mDataFile); cout << "\n*****Pattern Recognition Final Project evals:*****\n"; //set the class array to zero // for (int i =0 ; i < gVecNum ; i ++){ for (int k = 0 ; k <= gClassNum+1; k++) gClass[i][k] = 0; } CSelector* fbag = prob->getfuncbag(); Chrome* bestChrome =new Chrome(params,fbag, prob,prob->getfuncs(), prob->getconsts()); // for (int i = 0; i < gChromeNum; i++){ CSelector* fbag = prob->getfuncbag(); gCommittee[i] = bestChrome; //node* expr = new node[gCommittee[i]->MaxExpr]; string outFile = dec(i); outFile = prob->mOutputDir+"/"+outFile + ".chrome"; ifstream in; in.open(outFile.c_str()); if(!in){ cout << "can't open file "; return 0; } cout << endl<< "begin ********************************************"; for (int j=0; j < gCommittee[i]->MaxExpr; j++){ in.get(gCommittee[i]->expr[j].op); in.get(gCommittee[i]->expr[j].idx); cout << (int) gCommittee[i]->expr[j].op <<(int)gCommittee[i]->expr[j].idx; } cout << endl<< "end ********************************************"; in.close(); //memcpy(expr,gCommittee[i]->expr,gCommittee[i]->ExprBytes); gCommittee[i]->SetupEval(); prob->label(gCommittee[i]); //delete gCommittee[i]; //delete[] expr; } delete bestChrome; cout << endl<< "********************************************"; //generate reports // ofstream out; out.open("output.class"); if(!out){ cout << "can't open file "; return 0; } for (int i =0 ; i < gVecNum ; i ++){ cout << endl<<"label: " <evaluation(); cout.flush(); delete prob; delete params; return done? 1 : 0; // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ // return 1; }