Exemple #1
0
int Application::getTaxonID(const String &taxon)
{
  if(!taxonToID.isDefined(taxon)) {
    taxonToID[taxon]=taxonName.size();
    taxonName.push_back(taxon);
    nodes.push_back(tree->findNode(taxon));
    if(tree->findNode(taxon)==NULL) {
      cout<<"can't find node "<<taxon<<endl;
      INTERNAL_ERROR;
    }
  }
  return taxonToID[taxon];
}
Exemple #2
0
ProfileBackward::ProfileBackward(const PairHMM &hmm,
				 const AlignmentView &insideClade,
				 const AlignmentView &outsideClade,
				 Phylogeny &phylogeny,
				 const Array1D<int> &trackMap,
				 const AlphabetMap &alphabetMap)
  : hmm(hmm), inside(insideClade), outside(outsideClade),
    phylogeny(phylogeny), alphabet(PureDnaAlphabet::global()),
    fullAlignment(insideClade.getAlignment()), gapSymbol(hmm.getGapSymbol()),
    masterCol(hmm.getAlphabet(),hmm.getGapSymbol()), trackMap(trackMap),
    root(*phylogeny.getRoot()), alphabetMap(alphabetMap)
{
  // ctor

  initMasterCol();
  fillMatrix();
}
SolutionSet *PhyloMOCHC::execute() {
	
  int populationSize;
  int iterations;
  int maxEvaluations;
  int convergenceValue;
  int minimumDistance;
  int evaluations;
  int IntervalOptSubsModel;
   

  double preservedPopulation;
  double initialConvergenceCount;
  bool condition = false;
  SolutionSet *solutionSet, *offSpringPopulation, *newPopulation; 

  Comparator * crowdingComparator = new CrowdingComparator();

  SolutionSet * population;
  SolutionSet * offspringPopulation;
  SolutionSet * unionSolution;

  Operator * cataclysmicMutation;
  Operator * crossover;
  Operator * parentSelection;


  //Read the parameters
  populationSize = *(int *) getInputParameter("populationSize");
  maxEvaluations = *(int *) getInputParameter("maxEvaluations");
  IntervalOptSubsModel = *(int *) getInputParameter("intervalupdateparameters");
   
  convergenceValue = *(int *) getInputParameter("convergenceValue");
  initialConvergenceCount = *(double *)getInputParameter("initialConvergenceCount");
  preservedPopulation = *(double *)getInputParameter("preservedPopulation");
  
  //Read the operators
  cataclysmicMutation = operators_["mutation"];
  crossover	      = operators_["crossover"];
  parentSelection     = operators_["selection"];
  
  iterations  = 0;
  evaluations = 0;

   // calculating the maximum problem sizes .... 
   int size = 0;

    Solution * sol = new Solution(problem_);
    PhyloTree *Pt1 = (PhyloTree *)sol->getDecisionVariables()[0];
    TreeTemplate<Node> * tree1 = Pt1->getTree();
    BipartitionList* bipL1 = new BipartitionList(*tree1, true);
    bipL1->removeTrivialBipartitions();
    
    size = bipL1->getNumberOfBipartitions() * 2;
    

    delete bipL1;
    delete sol;
  
  minimumDistance = (int) std::floor(initialConvergenceCount*size);

  cout << "Minimun Distance " << minimumDistance << endl;
  
  // Create the initial solutionSet
  Solution * newSolution;
  
  ApplicationTools::displayTask("Initial Population", true);
   
  population = new SolutionSet(populationSize);
  Phylogeny * p = (Phylogeny *) problem_;
  
  for (int i = 0; i < populationSize; i++) {
    
      newSolution = new Solution(problem_);
     
      if(p->StartingOptRamas){
        p->BranchLengthOptimization(newSolution,p->StartingMetodoOptRamas,p->StartingNumIterOptRamas,p->StartingTolerenciaOptRamas);
      }
    
      if(p->OptimizacionSubstModel){
          p->OptimizarParamModeloSust(newSolution);
      }

    problem_->evaluate(newSolution);
    problem_->evaluateConstraints(newSolution);
    
    evaluations++;
    population->add(newSolution);
  } //for

  ApplicationTools::displayTaskDone();

  
  while (!condition) {
      
        cout << "Evaluating  " <<  evaluations << endl;
       
	offSpringPopulation = new SolutionSet(populationSize);
 	Solution **parents = new Solution*[2];
	
	for (int i = 0; i < population->size()/2; i++) {
               
  		parents[0] = (Solution *) (parentSelection->execute(population));
		parents[1] = (Solution *) (parentSelection->execute(population));

		if (RFDistance(parents[0],parents[1])>= minimumDistance) {
                    
		   Solution ** offSpring = (Solution **) (crossover->execute(parents));
                   
                   ((Phylogeny *)problem_)->Optimization(offSpring[0]); //Optimize and update the scores (Evaluate OffSpring)
                   ((Phylogeny *)problem_)->Optimization(offSpring[1]);
        
		   /*problem_->evaluate(offSpring[0]);
		   problem_->evaluateConstraints(offSpring[0]);
	           problem_->evaluate(offSpring[1]);
		   problem_->evaluateConstraints(offSpring[1]);*/
                   
		   evaluations+=2;
                   
		   offSpringPopulation->add(offSpring[0]);
		   offSpringPopulation->add(offSpring[1]);
                   delete[] offSpring;
		}		
	}  
        
	SolutionSet *join = population->join(offSpringPopulation);
 	delete offSpringPopulation;

	newPopulation = rankingAndCrowdingSelection(join,populationSize);
	delete join;
        if (equals(*population,*newPopulation)) {
		minimumDistance--;
	}   

	if (minimumDistance <= -convergenceValue) {
		minimumDistance = (int) (1.0/size * (1-1.0/size) * size);
		int preserve = (int) std::floor(preservedPopulation*populationSize);
		newPopulation->clear(); 
		population->sort(crowdingComparator);
		for (int i = 0; i < preserve; i++) {
			newPopulation->add(new Solution(population->get(i)));
		}
		for (int i = preserve; i < populationSize; i++) {
			Solution * solution = new Solution(population->get(i));
			cataclysmicMutation->execute(solution);
			problem_->evaluate(solution);
			problem_->evaluateConstraints(solution);	
			newPopulation->add(solution);
		}
		
	}

        //Update Interval
     if(evaluations%IntervalOptSubsModel==0 and IntervalOptSubsModel > 0){ 
        Solution * sol;  double Lk;
        Phylogeny * p = (Phylogeny*) problem_;
        cout << "Updating and Optimizing Parameters.." << endl;
        for(int i=0; i<newPopulation->size(); i++){
            sol =  newPopulation->get(i);
            Lk=  p->BranchLengthOptimization(sol,p->OptimizationMetodoOptRamas,p->OptimizationNumIterOptRamas,p->OptimizationTolerenciaOptRamas);
            sol->setObjective(1,Lk*-1);
        }
        cout << "Update Interval Done!!" << endl;
      }
        
	iterations++;
	delete population;
	population = newPopulation;
	if (evaluations >= maxEvaluations) {
		condition = true;		
	}
  }

  return population;

}
/*
 * Runs the ssNSGA-II algorithm.
 * @return a <code>SolutionSet</code> that is a set of non dominated solutions
 * as a result of the algorithm execution
 */
SolutionSet * ssNSGAII::execute() {

  int populationSize;
  int maxEvaluations;
  int evaluations;
  
  int IntervalOptSubsModel;

  // TODO: QualityIndicator indicators; // QualityIndicator object
  int requiredEvaluations; // Use in the example of use of the
                           // indicators object (see below)

  SolutionSet * population;
  SolutionSet * offspringPopulation;
  SolutionSet * unionSolution;

  Operator * mutationOperator;
  Operator * crossoverOperator;
  Operator * selectionOperator;

  Distance * distance = new Distance();

  //Read the parameters
  populationSize = *(int *) getInputParameter("populationSize");
  maxEvaluations = *(int *) getInputParameter("maxEvaluations");
  IntervalOptSubsModel = *(int *) getInputParameter("intervalupdateparameters");
  // TODO: indicators = (QualityIndicator) getInputParameter("indicators");

  //Initialize the variables
  population = new SolutionSet(populationSize);
  evaluations = 0;

  requiredEvaluations = 0;

  //Read the operators
  mutationOperator = operators_["mutation"];
  crossoverOperator = operators_["crossover"];
  selectionOperator = operators_["selection"];
  
  ApplicationTools::displayTask("Initial Population", true);
  
  // Create the initial solutionSet
  Solution * newSolution;
  Phylogeny * p = (Phylogeny *) problem_;
  
  
  for (int i = 0; i < populationSize; i++) {
    newSolution = new Solution(problem_);
    
    if(p->StartingOptRamas){
        p->BranchLengthOptimization(newSolution,p->StartingMetodoOptRamas,p->StartingNumIterOptRamas,p->StartingTolerenciaOptRamas);
    }
    
    if(p->OptimizacionSubstModel)
        p->OptimizarParamModeloSust(newSolution);
       
    
    problem_->evaluate(newSolution);
    problem_->evaluateConstraints(newSolution);
    evaluations++;
    population->add(newSolution);
  } //for
   ApplicationTools::displayTaskDone();
   
   
  // Generations
  while (evaluations < maxEvaluations) {
    
    // Create the offSpring solutionSet
    offspringPopulation = new SolutionSet(populationSize);
    Solution ** parents = new Solution*[2];
    
     if(evaluations%100==0){ 
         cout << "Evaluating  " <<  evaluations << endl;
     }
     
     
    //obtain parents
    parents[0] = (Solution *) (selectionOperator->execute(population));
    parents[1] = (Solution *) (selectionOperator->execute(population));
    
    // crossover
    Solution ** offSpring = (Solution **) (crossoverOperator->execute(parents));
    
    // mutation
    mutationOperator->execute(offSpring[0]);
    
    ((Phylogeny *)problem_)->Optimization(offSpring[0]); //Optimize and update the scores (Evaluate OffSpring)
    
    // evaluation
    //problem_->evaluate(offSpring[0]);
    //problem_->evaluateConstraints(offSpring[0]);
    
    // insert child into the offspring population
    offspringPopulation->add(offSpring[0]);
    
    evaluations ++;
    delete[] offSpring;
    delete[] parents;
    
    // Create the solutionSet union of solutionSet and offSpring
    unionSolution = population->join(offspringPopulation);
    delete offspringPopulation;

    // Ranking the union
    Ranking * ranking = new Ranking(unionSolution);

    int remain = populationSize;
    int index = 0;
    SolutionSet * front = NULL;
    for (int i=0;i<population->size();i++) {
      delete population->get(i);
    }
    population->clear();

    // Obtain the next front
    front = ranking->getSubfront(index);

    while ((remain > 0) && (remain >= front->size())) {
      //Assign crowding distance to individuals
      distance->crowdingDistanceAssignment(front, problem_->getNumberOfObjectives());
      //Add the individuals of this front
      for (int k = 0; k < front->size(); k++) {
        population->add(new Solution(front->get(k)));
      } // for
      
      //Decrement remain
      remain = remain - front->size();
      
      //Obtain the next front
      index++;
      if (remain > 0) {
        front = ranking->getSubfront(index);
      } // if
    } // while

    // Remain is less than front(index).size, insert only the best one
    if (remain > 0) {  // front contains individuals to insert
      distance->crowdingDistanceAssignment(front, problem_->getNumberOfObjectives());
      Comparator * c = new CrowdingComparator();
      front->sort(c);
      delete c;
      for (int k = 0; k < remain; k++) {
        population->add(new Solution(front->get(k)));
      } // for

      remain = 0;
    } // if

    delete ranking;
    delete unionSolution;

       //Update Interval
    if(evaluations%IntervalOptSubsModel==0 and IntervalOptSubsModel > 0){ 
        Solution * sol;  double Lk;
        Phylogeny * p = (Phylogeny*) problem_;
        //cout << "Updating and Optimizing Parameters.." << endl;
        for(int i=0; i<population->size(); i++){
            sol =  population->get(i);
            Lk=  p->BranchLengthOptimization(sol,p->OptimizationMetodoOptRamas,p->OptimizationNumIterOptRamas,p->OptimizationTolerenciaOptRamas);
            sol->setObjective(1,Lk*-1);
        }
        //cout << "Update Interval Done!!" << endl;
    }
    
    // This piece of code shows how to use the indicator object into the code
    // of NSGA-II. In particular, it finds the number of evaluations required
    // by the algorithm to obtain a Pareto front with a hypervolume higher
    // than the hypervolume of the true Pareto front.
// TODO:
//    if ((indicators != NULL) &&
//      (requiredEvaluations == 0)) {
//      double HV = indicators.getHypervolume(population);
//      if (HV >= (0.98 * indicators.getTrueParetoFrontHypervolume())) {
//        requiredEvaluations = evaluations;
//      } // if
//    } // if

  } // while

  delete distance;

  // Return as output parameter the required evaluations
  // TODO:
  //setOutputParameter("evaluations", requiredEvaluations);

  // Return the first non-dominated front
  Ranking * ranking = new Ranking(population);
  SolutionSet * result = new SolutionSet(ranking->getSubfront(0)->size());
  for (int i=0;i<ranking->getSubfront(0)->size();i++) {
    result->add(new Solution(ranking->getSubfront(0)->get(i)));
  }
  delete ranking;
  delete population;

  return result;

} // execute
Exemple #5
0
SparseMatrix3D *Application::transform(int xID,int yID)
{
  bool useDelta=false;//true;
  cout<<"transforming "<<taxonName[xID]<<":"<<taxonName[yID]<<endl;
  Time timer;
  timer.startCounting();
  PhylogenyNode *x=nodes[xID], *y=nodes[yID];
  if(x->getNodeType()!=LEAF_NODE || y->getNodeType()!=LEAF_NODE) return NULL;
  int Lx=lengths[xID], Ly=lengths[yID];
  Array3D<float> M(Lx+1,Ly+1,3);
  M.setAllTo(LOG_0);

  // Match states:
  for(int i=1 ; i<=Lx ; ++i) {
    Array1D< Vector<float> > sums(Ly+1);
    for(int zID=0 ; zID<numLeaves ; ++zID) {
      PhylogenyNode *z=nodes[zID];
      int Lz=lengths[zID];
      float delta=useDelta ? 
	tree->getSpannedDistance(x->getID(),y->getID(),z->getID()) : 0;
      SparseMatrix3D &Mxz=*matrices[xID][zID], &Mzy=*matrices[zID][yID];
      EntryList &row=Mxz(i,PHMM_MATCH);
      EntryList::iterator cur=row.begin(), end=row.end();
      for(; cur!=end ; ++cur) {
	Entry &e1=*cur;
	int k=e1.y;
	float v1=e1.value;
	EntryList &col=Mzy(k,PHMM_MATCH);
	EntryList::iterator cur=col.begin(), end=col.end();
	for(; cur!=end ; ++cur) {
	  Entry &e2=*cur;
	  int j=e2.y;
	  float v2=e2.value;
	  float product=v1+v2-delta;
	  sums[j].push_back(product);
	}
      }
    }
    for(int j=0 ; j<=Ly ; ++j)
      M[i][j][PHMM_MATCH]=sumLogProbs(sums[j])-numLeaves;
  }
  //cout<<M<<endl;

  // Insertion states:
  for(int i=0 ; i<=Lx ; ++i) {
    Array1D< Vector<float> > sums(Ly+1);
    for(int zID=0 ; zID<numLeaves ; ++zID) {
      PhylogenyNode *z=nodes[zID];
      int Lz=lengths[zID];
      float delta=useDelta ? 
	tree->getSpannedDistance(x->getID(),y->getID(),z->getID()) : 0;
      SparseMatrix3D &Mxz=*matrices[xID][zID], &Mzy=*matrices[zID][yID];
      EntryList &row=Mxz(i,PHMM_INSERT);
      EntryList::iterator cur=row.begin(), end=row.end();
      for(; cur!=end ; ++cur) {
	Entry &e1=*cur;
	int k=e1.y;
	float v1=e1.value;
	EntryList &col=Mzy(k,PHMM_MATCH);
	EntryList::iterator cur=col.begin(), end=col.end();
	for(; cur!=end ; ++cur) {
	  Entry &e2=*cur;
	  int j=e2.y;
	  float v2=e2.value;
	  float product=v1+v2-delta;
	  sums[j].push_back(product);
	}
      }
    }
    for(int j=0 ; j<=Ly ; ++j) 
      M[i][j][PHMM_INSERT]=sumLogProbs(sums[j])-numLeaves;
  }

  // Deletion states:
  for(int i=0 ; i<=Lx ; ++i) {
    Array1D< Vector<float> > sums(Ly+1);
    for(int zID=0 ; zID<numLeaves ; ++zID) {
      PhylogenyNode *z=nodes[zID];
      int Lz=lengths[zID];
      float delta=useDelta ? 
	tree->getSpannedDistance(x->getID(),y->getID(),z->getID()) : 0;
      SparseMatrix3D &Mxz=*matrices[xID][zID], &Mzy=*matrices[zID][yID];
      EntryList &row=Mxz(i,PHMM_MATCH);
      EntryList::iterator cur=row.begin(), end=row.end();
      for(; cur!=end ; ++cur) {
	Entry &e1=*cur;
	int k=e1.y;
	float v1=e1.value;
	EntryList &col=Mzy(k,PHMM_DELETE);
	EntryList::iterator cur=col.begin(), end=col.end();
	for(; cur!=end ; ++cur) {
	  Entry &e2=*cur;
	  int j=e2.y;
	  float v2=e2.value;
	  float product=v1+v2-delta;
	  sums[j].push_back(product);
	}
      }
    }
    for(int j=0 ; j<=Ly ; ++j) 
      M[i][j][PHMM_DELETE]=sumLogProbs(sums[j])-numLeaves;
  }
  //cout<<M<<endl;
  timer.stopCounting();
  cout<<timer.elapsedTime()<<endl;
  if(useDelta) {
    SparseMatrix3D *newM=new SparseMatrix3D(M,0.0);
    normalize(*newM);
    SparseMatrix3D *newerM=new SparseMatrix3D(*newM,threshold);
    delete newM;
    return newerM;
  }	
  SparseMatrix3D *newM=new SparseMatrix3D(M,threshold);

  {//###
    normalize(*newM);
    SparseMatrix3D *newerM=new SparseMatrix3D(*newM,threshold);
    delete newM;
    return newerM;
  }//###

  return newM;
}