コード例 #1
0
ファイル: main.c プロジェクト: beentaken/mmeng-personal-work
int main(void)
{
  check_primes(1000);
  conjecture(2, 2000);
  return 0;
}
コード例 #2
0
bool GAConjProblemForORGroupSolver::tournament( GACPforORGSolverGene &gene )
{
  double newFit = gene.fitness( );

  // number of last iterations with conjecture
  // I do next iteration through 1000 iterations
  static int it = theIter1;

  if( newFit==0 ) {
    bestFit = 0;
    lastImprovement = 0;
    if( file )
    *file << "Generation " << theIter1 << ", best fitness " << bestFit << endl << endl;
    return false;
  }

  if( gene.getHasShorterWords( ) ) {
    if( file )
      *file << "Given words can be shortened." << endl << endl;
    toStart( gene.getWord1( ) , gene.getWord2( ) );
    theIter2 = 0;
    return true;
  }

  //Conjecture
  if( gene.getHasConjecture( ) && theIter1 - it>1000 ) {
    Word conjWord = gene.getConjectureWord( );

    int nCheck = 1;
    if( checkedWords.bound(conjWord) )
      nCheck = checkedWords.valueOf( conjWord )+1;

    if( theIter1 - it > 1000*nCheck ) {
      checkedWords.bind( conjWord , nCheck );
      
      if( file ) {
	*file << "The algorithm will try to check whether ";
	theGroup.printWord( *file , conjWord );
	*file << " is trivial.";
	*file << endl << endl;
      }

      GAConjProblemForORGroupConjecture conjecture( theGroup , conjWord , file );
      bool conjResult = conjecture.isConj( 1000 , theIter1 );
      it = theIter1;

      if( conjResult ) {
	bestFit = 0;
	lastImprovement = 0;
	if( file ) {
	  *file << "The word is trivial." << endl << endl;
	  *file << "Generation " << theIter1 << ", best fitness " << bestFit << endl << endl;
	}
	return false;
      }

      if( file ) 
	*file << "The algorithm couldn't determine whether the word was trivial." << endl << endl;    
    }
  }
  
  // get random chromosome and compare it with the new one by roulette wheel
  int g = rnd1( numGenes );
  
  if( newFit<bestFit ) {
    delete genes[g];
    genes[g] = new GACPforORGSolverGene( gene );
    bestFit = newFit;
    lastImprovement = 0;
    if( file )
      *file << "Generation " << theIter1 << ", best fitness " << bestFit << endl << endl;
    return false;
  }
  
  if( !genes[g] )
    genes[g] = new GACPforORGSolverGene( gene );
  else {
    double prob1 = newFit, prob2 = genes[g]->fitness( );
    double min = ( prob1<prob2 ? prob1 : prob2 );
    if( min>5 ) { 
      prob1 -= min - 5;
      prob2 -= min - 5;
    }
    prob1 += fitnessRate;
    prob2 += fitnessRate;

    if( (roulette( prob1 , prob2 )==1 ) ) {
      delete genes[g];
      genes[g] = new GACPforORGSolverGene( gene );
    }
  }
  return false;
}