Esempio n. 1
0
int main()
{
    std::cout << "[moeoNSGAII]" << std::endl;

    TestEval eval;
    eoPopLoopEval <Solution> popEval(eval);
    eoQuadCloneOp < Solution > xover;
    eoUniformMutation < Solution > mutation(0.05);

    eoRealVectorBounds bounds(1, 1.0, 2.0);
    eoRealInitBounded < Solution > init(bounds);
    eoPop < Solution > pop(20, init);
    eoQuadGenOp <Solution> genOp(xover);
    eoSGATransform < Solution > transform(xover, 0.1, mutation, 0.1);
    eoGenContinue <Solution > continuator(10);

    // build NSGA-II
    moeoNSGAII < Solution > algo(20, eval, xover, 1.0, mutation, 1.0);
    moeoNSGAII < Solution > algo2(continuator, eval, genOp);
    moeoNSGAII < Solution > algo3(continuator, popEval, genOp);
    moeoNSGAII < Solution > algo4(continuator, eval, transform);
    moeoNSGAII < Solution > algo5(continuator, popEval, transform);

    // run the algo
    algo(pop);

    // final pop
    std::cout << "Final population" << std::endl;
    std::cout << pop << std::endl;

    std::cout << "[moeoNSGAII] OK" << std::endl;
    return EXIT_SUCCESS;
}
Esempio n. 2
0
int main()
{
    std::cout << "[moeoSEEA]" << std::endl;

    TestEval eval;
    eoQuadCloneOp < Solution > xover;
    eoUniformMutation < Solution > mutation(0.05);

    eoRealVectorBounds bounds(2, 1.0, 2.0);
    eoRealInitBounded < Solution > init(bounds);
    eoPop < Solution > pop(20, init);
    eoQuadGenOp <Solution> genOp(xover);
    eoSGATransform < Solution > transform(xover, 0.1, mutation, 0.1);
    eoGenContinue <Solution > continuator(20);
    moeoUnboundedArchive < Solution > archive;

    eoPopLoopEval <Solution> loopEval(eval);
    eoPopEvalFunc <Solution>& popEval(loopEval);

    // build SEEA
    moeoSEEA < Solution > algo1(20, eval, xover, 1.0, mutation, 1.0, archive);
    moeoSEEA < Solution > algo2(continuator, eval, genOp, archive);
    moeoSEEA < Solution > algo3(continuator, popEval, genOp, archive);
    moeoSEEA < Solution > algo4(continuator, eval, transform, archive);
    moeoSEEA < Solution > algo5(continuator, popEval, transform, archive);

    // run the algo
    algo5(pop);

    // final archive
    std::cout << "Final archive" << std::endl;
    std::cout << archive << std::endl;

    std::cout << "[moeoSEEA] OK" << std::endl;
    return EXIT_SUCCESS;
}
Esempio n. 3
0
void main_function()
{
  const unsigned SIZE = 8;
  unsigned i, j;
  eoBooleanGenerator gen;

  Chrom chrom(SIZE), chrom2;
  chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));

  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = false; chrom.fitness(binary_value(chrom));
  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));

  std::cout << "chrom.className() = " << chrom.className() << std::endl;

  std::cout << "chrom:  " << chrom << std::endl
       << "chrom2: " << chrom2 << std::endl;

  std::ostringstream os;
  os << chrom;
  std::istringstream is(os.str());
  is >> chrom2; chrom.fitness(binary_value(chrom2));

  std::cout << "\nTesting reading, writing\n";
  std::cout << "chrom:  " << chrom << "\nchrom2: " << chrom2 << '\n';

  std::fill(chrom.begin(), chrom.end(), false);
  std::cout << "--------------------------------------------------"
       << std::endl << "eoMonOp's aplied to .......... " << chrom << std::endl;

  eoInitFixedLength<Chrom>
      random(chrom.size(), gen);

  random(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinRandom ............ " << chrom << std::endl;

  eoOneBitFlip<Chrom> bitflip;
  bitflip(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBitFlip .............. " << chrom << std::endl;

  eoBitMutation<Chrom> mutation(0.5);
  mutation(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinMutation(0.5) ..... " << chrom << std::endl;

  eoBitInversion<Chrom> inversion;
  inversion(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinInversion ......... " << chrom << std::endl;

  eoBitNext<Chrom> next;
  next(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinNext .............. " << chrom << std::endl;

  eoBitPrev<Chrom> prev;
  prev(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinPrev .............. " << chrom << std::endl;

  std::fill(chrom.begin(), chrom.end(), false); chrom.fitness(binary_value(chrom));
  std::fill(chrom2.begin(), chrom2.end(), true); chrom2.fitness(binary_value(chrom2));
  std::cout << "--------------------------------------------------"
       << std::endl << "eoBinOp's aplied to ... "
       << chrom << " " << chrom2 << std::endl;

  eo1PtBitXover<Chrom> xover;
  std::fill(chrom.begin(), chrom.end(), false);
  std::fill(chrom2.begin(), chrom2.end(), true);
  xover(chrom, chrom2);
  chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
  std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl;

  for (i = 1; i < SIZE; i++)
    {
      eoNPtsBitXover<Chrom> nxover(i);
      std::fill(chrom.begin(), chrom.end(), false);
      std::fill(chrom2.begin(), chrom2.end(), true);
      nxover(chrom, chrom2);
      chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
      std::cout << "eoBinNxOver(" << i << ") ........ "
	   << chrom << " " << chrom2 << std::endl;
    }

  for (i = 1; i < SIZE / 2; i++)
    for (j = 1; j < SIZE / 2; j++)
      {
	eoBitGxOver<Chrom> gxover(i, j);
	std::fill(chrom.begin(), chrom.end(), false);
	std::fill(chrom2.begin(), chrom2.end(), true);
	gxover(chrom, chrom2);
	chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
	std::cout  << "eoBinGxOver(" << i << ", " << j << ") ..... "
	      << chrom << " " << chrom2 << std::endl;
      }

    // test SGA algorithm
    eoGenContinue<Chrom> continuator1(50);
    eoFitContinue<Chrom> continuator2(65535.f);

    eoCombinedContinue<Chrom> continuator(continuator1, continuator2);

    eoCheckPoint<Chrom> checkpoint(continuator);

    eoStdoutMonitor monitor;

    checkpoint.add(monitor);

    eoSecondMomentStats<Chrom> stats;

    monitor.add(stats);
    checkpoint.add(stats);

    eoProportionalSelect<Chrom> select;
    eoEvalFuncPtr<Chrom>  eval(binary_value);

    eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint);

    eoInitFixedLength<Chrom> init(16, gen);
    eoPop<Chrom> pop(100, init);

    apply<Chrom>(eval, pop);

    sga(pop);

    pop.sort();

    std::cout << "Population " << pop << std::endl;

    std::cout << "\nBest: " << pop[0].fitness() << '\n';

  /*

    Commented this out, waiting for a definite decision what to do with the mOp's

    // Check multiOps
    eoMultiMonOp<Chrom> mOp( &next );
    mOp.adOp( &bitflip );
    std::cout << "before multiMonOp............  " << chrom << std::endl;
    mOp( chrom );
    std::cout << "after multiMonOp .............. " << chrom << std::endl;

    eoBinGxOver<Chrom> gxover(2, 4);
    eoMultiBinOp<Chrom> mbOp( &gxover );
    mOp.adOp( &bitflip );
    std::cout << "before multiBinOp............  " << chrom << " " << chrom2 << std::endl;
    mbOp( chrom, chrom2 );
    std::cout << "after multiBinOp .............. " << chrom << " " << chrom2 <<std::endl;
  */
}
Esempio n. 4
0
int
main(int argc, char** argv)
{
  const unsigned int T_SIZE = 3; // size for tournament selection
  const unsigned int VEC_SIZE = 3; // Number of object variables in genotypes
  const unsigned int POP_SIZE = 10; // Size of population
  const unsigned int MAX_GEN = 1000; // Maximum number of generation before STOP
  const unsigned int MIN_GEN = 10;  // Minimum number of generation before ...
  const unsigned int STEADY_GEN = 10; // stop after STEADY_GEN gen. without improvement
  const float P_CROSS = 0.8;	// Crossover probability
  const float P_MUT = 0.5;	// mutation probability
  const double EPSILON = 0.01;	// range for real uniform mutation
  double SIGMA = 0.3;       	// std dev. for normal mutation
  // some parameters for chosing among different operators
  const double hypercubeRate = 0.5;     // relative weight for hypercube Xover
  const double segmentRate = 0.5;  // relative weight for segment Xover
  const double uniformMutRate = 0.5;  // relative weight for uniform mutation
  const double detMutRate = 0.5;      // relative weight for det-uniform mutation
  const double normalMutRate = 0.5;   // relative weight for normal mutation
  const unsigned int SEED = 42;	// seed for random number generator

  po::options_description desc;
  std::string matrixdir, model, nullmodel, trainingset, testingset, lastrun;

  desc.add_options()
    ("matrixdir", po::value<std::string>(&matrixdir),
     "The directory set up by SOFT2Matrix")
    ("model", po::value<std::string>(&model),
     "The gene regulatory network model")
    ("nullmodel", po::value<std::string>(&nullmodel),
     "The gene regulatory network null (scrambled) model")
    ("trainingset", po::value<std::string>(&trainingset),
     "The list of arrays which have been selected for inclusion in the training set")
    ("testingset", po::value<std::string>(&testingset),
     "The list of arrays which have been selected for inclusion in the testing set")
    ("lastrun", po::value<std::string>(&lastrun),
     "The output file from the last run, to re-use scores from (optional)")
    ;

  po::variables_map vm;

  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);

  std::string wrong;
  if (!vm.count("help"))
  {
    if (!vm.count("matrixdir"))
      wrong = "matrixdir";
    else if (!vm.count("model"))
      wrong = "model";
    else if (!vm.count("trainingset"))
      wrong = "trainingset";
    else if (!vm.count("testingset"))
      wrong = "testingset";
    else if (!vm.count("nullmodel"))
      wrong = "nullmodel";
  }

  if (wrong != "")
    std::cerr << "Missing option: " << wrong << std::endl;
  if (vm.count("help") || wrong != "")
  {
    std::cout << desc << std::endl;
    return 1;
  }
  
  if (!fs::is_directory(matrixdir))
  {
    std::cout << "Matrix directory doesn't exist."
              << std::endl;
    return 1;
  }

  if (!fs::is_regular(model))
  {
    std::cout << "Model file doesn't exist or not regular file."
              << std::endl;
    return 1;
  }
  if (!fs::is_regular(nullmodel))
  {
    std::cout << "Null model file doesn't exist or not regular file."
              << std::endl;
    return 1;
  }

  if (!fs::is_regular(trainingset))
  {
    std::cout << "Training set file doesn't exist or not regular file."
              << std::endl;
    return 1;
  }

  if (!fs::is_regular(testingset))
  {
    std::cout << "Testing set file doesn't exist or not regular file."
              << std::endl;
    return 1;
  }

  std::list<double> lastRun;
  if (fs::is_regular(lastrun))
  {
    std::ifstream flastrun(lastrun.c_str());

    static const boost::regex prev(".*SVM Result: .* Result \\(([^\\)]+)\\).*");
    while (flastrun.good())
    {
      std::string l;
      std::getline(flastrun, l);
      boost::smatch m;
      if (!boost::regex_match(l, m, prev))
      {
        continue;
      }

      lastRun.push_back(strtod(m[1].str().c_str(), NULL));
    }
  }

  ExpressionMatrixProcessor emp(matrixdir);
  GRNModel m(model, emp, 30);
  GRNModel m2(nullmodel, emp, 30);

  std::list<std::string> trainingArrays, testingArrays;
  m.loadArraySet(trainingset, trainingArrays);
  m.loadArraySet(testingset, testingArrays);
  m.loadSVMTrainingData(trainingArrays);
  m2.loadArraySet(trainingset, trainingArrays);
  m2.loadArraySet(testingset, testingArrays);
  m2.loadSVMTrainingData(trainingArrays);

  // We seed it just so we can restart if need be.
  rng.reseed(SEED);

  EvaluateSVMFit eval(m, m2, trainingArrays, lastRun, 30, testingArrays.size());

  std::vector<double> minVals, maxVals;
  // log(gamma)
  minVals.push_back(-15);
  maxVals.push_back(15);
  // log(C)
  minVals.push_back(-15);
  maxVals.push_back(2);
  // nu
  minVals.push_back(0);
  maxVals.push_back(1);

  eoRealVectorBounds rvb(minVals, maxVals);
  eoRealInitBounded<Indi> random(rvb);

  eoPop<Indi> pop(POP_SIZE, random);
  apply<Indi>(eval, pop);
  pop.sort();
  std::cout << "Initial Population" << std::endl;
  std::cout << pop;
  eoDetTournamentSelect<Indi> selectOne(T_SIZE);
  eoSelectPerc<Indi> select(selectOne);// by default rate==1
  eoGenerationalReplacement<Indi> replace;
  eoSegmentCrossover<Indi> xoverS;
  eoHypercubeCrossover<Indi> xoverA;
  eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
  xover.add(xoverA, hypercubeRate, true);
  
  eoUniformMutation<Indi>  mutationU(EPSILON);
  eoDetUniformMutation<Indi>  mutationD(EPSILON);
  eoNormalMutation<Indi>  mutationN(SIGMA);
  eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
  mutation.add(mutationD, detMutRate);
  mutation.add(mutationN, normalMutRate, true);

  eoGenContinue<Indi> genCont(MAX_GEN);
  eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
  eoCombinedContinue<Indi> continuator(genCont);
  continuator.add(steadyCont);

  eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
  eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
  gga(pop);

  pop.sort();
  std::cout << "Final Population:"
            << std::endl << pop << std::endl;

  return 0;
}
Esempio n. 5
0
int main()
{
  const unsigned POP_SIZE = 10, CHROM_SIZE = 12;
  unsigned i;
  eoBooleanGenerator gen;

  // the populations: 
  eoPop<Chrom> pop; 

  // Evaluation
  RoyalRoad<Chrom> rr( 8 ); 
  eoEvalFuncCounter<Chrom> eval( rr );

  eoInitVirus<float> random(CHROM_SIZE, gen); 
  for (i = 0; i < POP_SIZE; ++i) {
      Chrom chrom;
      random(chrom);
      eval(chrom);
      pop.push_back(chrom);
  }
  
  std::cout << "population:" << std::endl;
  for (i = 0; i < pop.size(); ++i)
    std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
  
  // selection
  eoStochTournamentSelect<Chrom> lottery(0.9 );

  // breeder
  eoOneBitFlip<Chrom> vm;
  eoUBitXover<Chrom> xover;
  eoProportionalOp<Chrom> propSel;
  eoGeneralBreeder<Chrom> breeder( lottery, propSel );
  propSel.add(vm, 0.2);
  propSel.add(xover, 0.8);
  
  // Replace a single one
  eoCommaReplacement<Chrom> replace;

  // Terminators
  eoGenContinue<Chrom> continuator1(10);
  eoFitContinue<Chrom> continuator2(CHROM_SIZE);
  eoCombinedContinue<Chrom> continuator(continuator1, continuator2);  
  eoCheckPoint<Chrom> checkpoint(continuator);
  eoStdoutMonitor monitor;
  checkpoint.add(monitor);
  eoSecondMomentStats<Chrom> stats;
  eoPopStat<Chrom> dumper( 10 );
  monitor.add(stats);
  checkpoint.add(dumper);
  checkpoint.add(stats);

  // GA generation
  eoEasyEA<Chrom> ea(checkpoint, eval,  breeder, replace );

  // evolution
  try
    {
      ea(pop);
    }
  catch (std::exception& e)
    {
	std::cout << "exception: " << e.what() << std::endl;;
	exit(EXIT_FAILURE);
    }
  
  std::cout << "pop" << std::endl;
  for (i = 0; i < pop.size(); ++i)
    std::cout << "\t" <<  pop[i] << " " << pop[i].fitness() << std::endl;

  std::cout << "\n --> Number of Evaluations = " << eval.getValue() << std::endl;
  return 0;
}