if (r < m_pCrossover) {  int cp = Math.abs(m_random.nextInt());  cp %= (m_numAttribs-2); cp ++;  for (i=0;i<cp;i++) { if (m_population[parent1].get(i)) { newPop[j+1].set(i); } else { newPop[j+1].clear(i); } if (m_population[parent2].get(i)) { newPop[j].set(i); } else { newPop[j].clear(i);
if (m_numAttribs >= 3) { if (r < m_pCrossover) {  int cp = Math.abs(m_random.nextInt());  cp %= (m_numAttribs-2); cp ++;  for (i=0;i<cp;i++) { if (m_population[parent1].get(i)) { newPop[j+1].set(i); } else { newPop[j+1].clear(i); } if (m_population[parent2].get(i)) { newPop[j].set(i); } else { newPop[j].clear(i); }
  private void generation () throws Exception {    int i,j=0;    double best_fit = -Double.MAX_VALUE;    int old_count = 0;    int count;    GABitSet [] newPop = new GABitSet [m_popSize];    int parent1,parent2;    /** first ensure that the population best is propogated into the new	generation */    for (i=0;i<m_popSize;i++) {      if (m_population[i].getFitness() > best_fit) {	j = i;	best_fit = m_population[i].getFitness();	old_count = countFeatures(m_population[i].getChromosome());      } else if (Utils.eq(m_population[i].getFitness(), best_fit)) {	count = countFeatures(m_population[i].getChromosome());	if (count < old_count) {	  j = i;	  best_fit = m_population[i].getFitness();	  old_count = count;	}      }    }    newPop[0] = (GABitSet)(m_population[j].clone());    newPop[1] = newPop[0];    for (j=2;j<m_popSize;j+=2) {      parent1 = select();      parent2 = select();      newPop[j] = (GABitSet)(m_population[parent1].clone());      newPop[j+1] = (GABitSet)(m_population[parent2].clone());      // if parents are equal mutate one bit      if (parent1 == parent2) {	int r;	if (m_hasClass) {	  while ((r = (Math.abs(m_random.nextInt()) % m_numAttribs)) == m_classIndex);	}	else {	  r = m_random.nextInt() % m_numAttribs;	}		if (newPop[j].get(r)) {	  newPop[j].clear(r);	}	else {	  newPop[j].set(r);	}      }      else {	// crossover	double r = m_random.nextDouble();	if (r < m_pCrossover) {	  // cross point	  int cp = Math.abs(m_random.nextInt());	  	  cp %= (m_numAttribs-2);	  cp ++;	  	  for (i=0;i<cp;i++) {	    if (m_population[parent1].get(i)) {	      newPop[j+1].set(i);	    }	    else {	      newPop[j+1].clear(i);	    }	    if (m_population[parent2].get(i)) {	      newPop[j].set(i);	    }	    else {	      newPop[j].clear(i);	    }	  }	}	// mutate	for (int k=0;k<2;k++) {	  for (i=0;i<m_numAttribs;i++) {	    r = m_random.nextDouble();	    if (r < m_pMutation) {	      if (m_hasClass && (i == m_classIndex)) {		// ignore class attribute	      }	      else {		if (newPop[j+k].get(i)) {		  newPop[j+k].clear(i);		}		else {		  newPop[j+k].set(i);		}	      }	    }	  }	}		        }    }    m_population = newPop;  }