[galib] GA for GAP

Ramya Narayanaswamy narayanaswamy.2 at wright.edu
Wed May 12 16:58:16 EDT 2004


Hello All,
I am trying to use Genetic Algorithm from the GALib for solving 
Generalized Assignment Problem. I have a running code for MKP which is 
a 1Dimensional array which uses GALib. Now is have a problem calling 
the obejctive function and defining in GA2DBinS.h. My code is some what 
like this
#include <stdio.h>
#include <iostream.h>
#include <ctime>
#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; i<genome.widht(); i++)
  {
	  for(int j=0; j<genome.height(); j++)
	  {
  feasible1[i] += genome.gene(i,j);
  }
  }
for(int j=0; j<genome.height(); j++)
  {
	  for(int i=0; i<genome.width(); i++)
	  {
  feasible2[j] += genome.gene(i,j)*a[i][j];
  }
  }
  
/*
  //* if the current solution is not good ( violated constraint(s) ), 
then DROP and ADD algorithms are
 // invoked to make child good for the feasible population. This is 
done by starting from a random point
 // in its chromosome '1' are excluded until they are feasible in terms 
of both constraints.
  if ((feasible1>b1) || (feasible2>b2)){
  dropper++ ;
int mdpnt = GARandomInt(0,(genome.length()-1));
for (int q=mdpnt ; q<genome.length() ; q++){
if(genome.gene(q)){
genome.gene(q,0);
feasible1 -= a1[q];
feasible2 -= a2[q];
		}
if ((feasible1<=b1) && (feasible2<=b2)) break;
if ( q == (genome.length()-1)){
for (int j=0 ; j<mdpnt ; j++){
if(genome.gene(j)){
genome.gene(j,0);
feasible1 -= a1[j];
feasible2 -= a2[j];
		}
if ((feasible1<=b1) && (feasible2<=b2)) break;}	
	}
	}
 }
  //* After DROP algortihm, ADD algorithm tries to improve the quality 
of  chromosome by 
 // adding feasible items ,starting from a random byte.
 int addition = GARandomInt(0,(genome.length()-1));
for (int t=addition; t<genome.length() ; t++){
if(((feasible1 +a1[t])<=b1) && ((feasible2 + a2[t])<=b2) && 
(!(genome.gene(t)))){
adder++;
genome.gene(t,1);
feasible1 += a1[t];
feasible2 += a2[t];}
if ((feasible1==b1) && (feasible2==b2)) break;
if ( t == (genome.length()-1)){
for (int j=0 ; j<addition ; j++){
if(((feasible1 +a1[j])<=b1) && ((feasible2 + a2[j])<=b2) && 
(!(genome.gene(j)))){
adder++;
genome.gene(j,1);
feasible1 += a1[j];
feasible2 += a2[j];}
if ((feasible1==b1) && (feasible2==b2)) break;}	
		}
	}
*/		
for(int i=0; i<genome.width(); i++)	// Score of the child is returned
{ 
	for(int j=0;j<genome.height(); j++)
		{

		score +=  genome.gene(i,j)* c[i,j];
	}
}
			return score;
		 }
  }

The errors which it gives while compiling are

error C2065: 'GA2DBinarystringGenome' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'genome'
error C2065: 'genome' : undeclared identifier
error C2601: 'Objective' : local function definitions are illegal.

If some body has some suggestions or any solutions for this kindly let 
me know as it would really help me as i am stuck. Thanks in advance.
Ramya Narayanaswamy




More information about the galib mailing list