[galib] GA for GAP

Ramya Narayanaswamy narayanaswamy.2 at wright.edu
Wed May 12 21:24:28 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


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];
}
}
 
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