/* ---------------------------------------------------------------------------- A GENETIC ALGORITHM FOR GENERALIZED ASSIGNEMENT PROBLEM. ---------------------------------------------------------------------------- */ #include #include #include #include "GASState.h" //#include "GA1DBinS.h" #include "GA2DBinS.h" float Objective(GAGenome &); // This is the declaration of our obj function. // The definition comes later in the file. #define NO_VAR1 5 #define NO_VAR2 15 #define GAP_FILE "gap.txt"// Problem file float c[NO_VAR1][NO_VAR2],a[NO_VAR1][NO_VAR2]; float b1,ZIP,Rho,Slackness; int dropper,adder; // seed is optional but better use to have same results each time you run the code // Seeding is important in random search int seed = 1254869; int main() { // opening data file ifstream in(GAP_FILE); for (int jy=1; jy<9 ; jy++) { char zipi[32] = "Zaman15.txt"; char filenam[50]; sprintf(filenam, "%s%i%s", "RUNco_",jy,".txt"); ofstream outfile ; outfile.open(zipi,(ios::out | ios::app )); //dropper=adder = 0; double dump; int prob; if(!in) { cerr << "could not read data file " << GAP_FILE << "\n"; exit(1); } // first rows of each problem is read to have parameters and RHS of problem // parameters I don't need, dumped!But may use in the future to record //the parameters of the program. in >> prob; in >> dump; in >> dump;\ in >> ZIP; in >> b1; //in >> b2; //in >> Rho; in >> Slackness; in >> dump; in >> dump; in >> dump; cout << jy << "\n"; for(int i=0; i<5; i++) //reading the cost function values { for(int j=0; j<15; j++) { in>>c[i][j]; } } for(i=0; i<5; i++) //reading the resource allocation values { for(int j=0; j<15; j++) { in>> a[i][j]; } } /* int nvar = 0; do{ // reading obj. func. values in >> c[nvar]; nvar++; } while(nvar<100); nvar = 0; do{ //reading first constraint in >> a1[nvar]; nvar++; } while(nvar<100); nvar = 0; do{ //reading second constraint in >> a2[nvar]; nvar++; } while(nvar<100); */ for( i=0; i<5; i++) { for(int j=0; j<15; j++) { if (in.eof()) in.close(); if(i >= NO_VAR1 || j >= NO_VAR2) { cerr << "data file contains more VARIABLES than allowed for in the fixed\n"; cerr << "arrays. Recompile the program with larger arrays or try a\n"; cerr << "smaller problem.\n"; exit(1); } } // Declare variables for the GA parameters and set them to some default values //int length = 100; int width=5; int height=15; int flush = 5; int atama = 100; int popsize = 100; int ngen = 5000; float pmut = 0.01; double pcross = 0.85; int rplcmnt = 25; int best = 5; time_t time1; time1=0; time1 = clock(); // GA1DBinaryStringGenome genome(length, Objective); GA2DBinarystringGenome genome(width,height,Objective); GASteadyStateGA ga(genome); ga.initialize(seed); ga.populationSize(popsize); ga.nGenerations(ngen); ga.pMutation(pmut); ga.nReplacement(rplcmnt); ga.pCrossover(pcross); ga.flushFrequency(flush); ga.scoreFrequency(atama); ga.scoreFilename(filenam); ga.crossover(GA2DBinaryStringGenome :: UniformCrossover ); // ga.crossover(GA2DBinaryStringGenome :: OnePointCrossover); GARouletteWheelSelector wheel; //RankSelector ; RoulletteWheelSelector ; ga.selector(wheel) ; GASigmaTruncationScaling sigma ; ga.scaling(sigma) ; ga.selectScores(2); ga.evolve(); if (ga.done){ time_t time2; time2 = clock(); outfile << difftime(time2,time1)/ CLOCKS_PER_SEC << "\n"; //cout << genome; } } return 0; } // Objective function. The most imoratant part. Basically a child is evaluated by this function before //getting into the population float Objective(GAGenome& g) { GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)g; float score=0.0; float feasible1[i]=0.0;// left hand side of current first constraint float feasible2[j]=0.0;// left hand side of current second constraint //* First constraints are calculated to see if the reported solution is good! for(int i=0; ib1) || (feasible2>b2)){ dropper++ ; int mdpnt = GARandomInt(0,(genome.length()-1)); for (int q=mdpnt ; q