[jgap-users] please help with cusotm constraintchecker asap
Brought to you by:
klausikm
From: Samik G. <sam...@gm...> - 2006-05-23 20:26:04
|
hi all, I owuld like to get your help in building a custom constraintchecker. I hav= e built my class and added its instance to the conf objet. However, when I run, its gives the following error org.jgap.InvalidConfigurationException: The gene type org.jgap.impl.IntegerGene is not allowed to be used in the chromosome due t= o the constraint checker used. please find attahced my code for custom constraint checker as well as the code for building the sample chromosome. I would like to mention here that I am checking over the entire chromosome in the verify method rather than on the genes alone. --------------------------------------------------- //custom checker code public class ZOConstraintChecker implements IGeneConstraintChecker{ private OptimizationParameters op; private Class m_forbidden; public ZOConstraintChecker(OptimizationParameters op) { this.op =3D op; m_forbidden =3D Integer.class; } public boolean verify(Gene a, Object value, IChromosome chrome, int geneIndex ) { if (m_forbidden =3D=3D null) { return true; } boolean status =3D true; int [][] X =3D new int[op.getnBSC()][op.getnBSC()]; int [][] Z =3D new int[op.getnCell()][op.getnCell()]; int [][] U =3D new int[op.getnBSC()][op.getnLPP()]; int [][] V =3D new int[op.getnCell()][op.getnBSC()]; int [][] Y =3D new int[op.getnCell()][op.getnCell()]; int nCell =3D op.getnCell(); int nBSC =3D op.getnBSC(); int nLPP =3D op.getnLPP(); int nZone =3D nCell; for(int l=3D0; l < nCell; l++) { for(int n=3D0; n < nZone; n++) { Y[l][n] =3D0; } } Gene[] geneList =3D chrome.getGenes(); //populate Z and Y int zone,z; for(int cellGene =3D 0; cellGene < nCell; cellGene++) { zone =3D ((Integer)geneList[cellGene].getAllele()).intValue(); Y[cellGene][zone-1] =3D 1; for(int j=3D0; j < nCell; j++) { z =3D ((Integer)geneList[j].getAllele()).intValue(); if(zone=3D=3Dz) Z[cellGene][j] =3D 0; else Z[cellGene][j] =3D 1; } } int counter =3D nCell; //populate X int bscGene, zoneBSC, zBSC; for(bscGene =3D 0; bscGene < nBSC; bscGene++) { zoneBSC =3D ((Integer)geneList[counter + bscGene].getAllele()).intValue(); for(int j=3D0; j < nBSC; j++) { zBSC =3D ((Integer)geneList[counter + j].getAllele()).intValue(); if(zoneBSC=3D=3DzBSC) X[bscGene][j] =3D 0; else X[bscGene][j] =3D 1; } } //populate V int val; counter =3D nCell + nBSC; for(int i =3D 0; i < nCell; i++) { for(int j=3D0; j < nBSC; j++) { val =3D ((Integer)geneList[counter].getAllele()).intValue(= ); counter++; V[i][j] =3D val; } } //populate U counter =3D+ nCell*nBSC; for(int i =3D 0; i < nBSC; i++) { for(int j=3D0; j < nLPP; j++) { val =3D ((Integer)geneList[counter].getAllele()).intValue= (); counter++; U[i][j] =3D val; } } // now check all the constraints as defined in eqn. 11 to 20 in the report // note that consts 13 and 14 are captured in the encoding itself //const 11 int sumL=3D0; for(int l=3D0; l < nCell; l++) { sumL=3D0; for(int j=3D0; j < nBSC; j++) { sumL+=3DV[l][j]; } if(sumL!=3D1) { status =3D false; return status; } else status =3D true; } //const 12 int sumI=3D0; for(int i=3D0; i < nBSC; i++) { sumI=3D0; for(int k=3D0; k < nLPP; k++) { sumI+=3DU[i][k]; } if(sumI!=3D1) { status =3D false; return status; } else status =3D true; } //const 13 for(int l=3D0; l < nCell; l++) { sumI=3D0; for(int n=3D0; n < nZone; n++) { sumI+=3DY[l][n]; } if(sumI!=3D1) { status =3D false; return status; } else status =3D true; } //const 14 int RHS, LHS=3D0; for(int l=3D0; l < nCell; l++) { for(int m=3D0; m < nCell; m++) { LHS=3D1 - Z[l][m]; RHS=3D0; for(int n=3D0; n < nZone; n++) { RHS+=3DY[l][n]*Y[m][n]; } if(LHS !=3D RHS) { status =3D false; return status; } else status =3D true; } } //const 15 RHS=3D0; LHS=3D0; for(int i=3D0; i < nBSC; i++) { for(int j=3D0; j < nBSC; j++) { LHS=3D1 - X[i][j]; RHS=3D0; for(int k=3D0; k < nLPP; k++) { RHS+=3DU[i][k]*U[j][k]; } if(LHS > RHS) { status =3D false; return status; } else status =3D true; } } //const 16 for(int l=3D0; l < nCell; l++) { for(int m=3D0; m < nCell; m++) { LHS=3DZ[l][m]; RHS=3D0; for(int i=3D0; i < nBSC; i++) { for(int j=3D0; j < nBSC; j++) { RHS+=3D V[l][i]*V[m][j]*X[i][j]; } } if(LHS !=3D RHS) { status =3D false; return status; } else status =3D true; } } //const 17 double suml=3D0.0; double[] alphaCell =3D new double[nCell]; for(int l=3D0; l < nCell; l++) { suml =3D0.0; for(int m=3D0; m < nCell; m++) { suml +=3D op.getLambdaCell()[m]*(1-Z[l][m]); } alphaCell[l]=3D suml; if(alphaCell[l] > op.getPageLoadCell()[l]) { status =3D false; return status; } else status =3D true; } //const 18 for(int i=3D0; i < nBSC; i++) { suml =3D0.0; for(int j=3D0; j < nBSC; j++) { for(int l =3D0; l < nCell; l++) { suml+=3DalphaCell[l]*V[l][j]; } suml*=3D(1 - X[i][j]); } if(suml > op.getPageLoadBSC()[i]) { status =3D false; return status; } else status =3D true; } //const 19 double sumCap=3D0.0; for(int i =3D0; i < nBSC; i++) { sumCap=3D0.0; for(int l=3D0; l < nCell; l++) { sumCap+=3D op.getBhcaCell()[l]*V[l][i]; } if(sumCap > op.getBhcaBSC()[i]) { status =3D false; return status; } else status =3D true; } //const 20 double sumLHS=3D0.0; for(int k =3D0; k < nLPP; k++) { sumLHS=3D0.0; for(int i=3D0;i < nBSC; i++) { for(int l=3D0; l < nCell; l++) sumLHS +=3D op.getBhcaCell()[l]*V[l][i]; sumLHS*=3DU[i][k]; } if(sumLHS > op.getBhcaLPP()[k]) { status =3D false; return status; } else status =3D true; } return status; } } ------------------------------------------------------- //sample chromosome code and conf init code public Chromosome generateSampleChromosome(Gene[] sampleGenes) { RandomGenerator rng =3D new GaussianRandomGenerator(); int cell=3D0; for(cell=3D0; cell < op.getnCell(); cell++) { sampleGenes[cell] =3D new IntegerGene(1,op.getnCell()); sampleGenes[cell].setToRandomValue(rng); } int bscIndex =3D cell; for(int bsc=3D0; bsc < op.getnBSC(); bsc++) { sampleGenes[bscIndex + bsc] =3D new IntegerGene(1,op.getnBSC())= ; sampleGenes[bscIndex + bsc].setToRandomValue(rng); } int geneCounter =3D op.getnCell()+ op.getnBSC(); int counter =3D0; for(counter=3D0; counter < op.getnCell()*op.getnBSC(); counter++) { sampleGenes[geneCounter + counter] =3D (new IntegerGene(0,1)); sampleGenes[geneCounter + counter].setToRandomValue(rng); } geneCounter =3D geneCounter + op.getnCell()*op.getnBSC(); for(counter=3D0; counter < op.getnBSC()*op.getnLPP(); counter++) { sampleGenes[geneCounter + counter] =3D new IntegerGene(0,1); sampleGenes[geneCounter + counter].setToRandomValue(rng); } Chromosome sampleChrome =3D new Chromosome(sampleGenes); return sampleChrome; } public void generateSolution() throws Exception { Configuration conf =3D new DefaultConfiguration(); conf.setPreservFittestIndividual(true); // Set the fitness function we want to use, which is our // MinimizingMakeChangeFitnessFunction. We construct it with // the target amount of change passed in to this method. // --------------------------------------------------------- ZOFitnessFunction zoFunc =3D new ZOFitnessFunction(op); conf.setFitnessFunction(zoFunc); // Now we need to tell the Configuration object how we want our // Chromosomes to be setup. We do that by actually creating a // sample Chromosome and then setting it on the Configuration // object. Gene[] sampleGenes =3D new Gene[op.getnCell() + op.getnBSC()+ op.getnCell()* op.getnBSC() + op.getnBSC()*op.getnLPP()]; Chromosome sampleChromosome =3DgenerateSampleChromosome(sampleGenes= ); ZOConstraintChecker zoConst =3D new ZOConstraintChecker(op); sampleChromosome.setConstraintChecker(zoConst); conf.setPopulationSize(GASolver.MAX_POPULATION_SIZE); conf.setSampleChromosome(sampleChromosome); ...... ------------------------------------------------------------------- Eagerly waiting for your help, SAmik --=20 Samik Ghosh Center For Research In Wireless Mobility And Networking (CreWMaN) http://crewman.uta.edu/~sghosh Department of Computer Science & Engineering University of Texas at Arlington |