bool DifferentialEvolution::advanceGeneration(StateP state, DemeP deme)
{
	// create donor vectors for each population member
	for(uint iIter = 0; iIter < deme->size(); iIter++){ 
		createDonorVectors(deme, state);
	}

	// perform DE crossover, generate trial vectors (stored in donor_vector)
	for(uint iIter = 0; iIter < deme->size(); iIter++) {
		crossover(deme, iIter, state);
	}
	
	// select the better one for each population member and trial vector
	for(uint iIter = 0; iIter < deme->size(); iIter++) {
		evaluate(donor_vector[iIter]);
		if(donor_vector[iIter]->fitness->isBetterThan(deme->at(iIter)->fitness))
			replaceWith(deme->at(iIter), donor_vector[iIter]);
	}

	//for(uint i = 0; i < deme->size(); i++){
	//	state->getLogger()->log(5, "deme[" + uint2str(i) + "]: " + dbl2str(deme->at(i)->fitness->getValue()) + "\t" + uint2str(deme->at(i)->index));
	//}

	donor_vector.clear();

	return true;
}
bool RandomSearch :: advanceGeneration(StateP state, DemeP deme)
{
	// initialize and evaluate all individuals
	for(uint i = 0; i < deme->size(); i++) {
		deme->at(i)->initialize(state);
		evaluate(deme->at(i));
	}

	return true;
}
		 bool scoutBeesPhase(StateP state, DemeP deme){
			IndividualP unimproved ;

			double maxTrial = 0;
			for( uint i = 0; i < deme->getSize(); i++ ) { // for each food source
				IndividualP food = deme->at(i);
				//get food source's trial variable
				FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(1));
				double &trial = flp->realValue[0];
				
				//remember the source if its trial exceeded limit 
				if (trial > limit && trial >maxTrial){
					unimproved = food;
					maxTrial = trial;
				}					
			}

			//if there is a  food source that exceeded the limit, replace it with a random one
			if (unimproved != NULL){
					FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (unimproved->getGenotype(1));
					double &trial = flp->realValue[0];
					trial = 0;
					flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (unimproved->getGenotype(0));
					flp->initialize(state);
					evaluate(unimproved);
			}

			return true;
		 }
bool ArtificialBeeColony::calculateProbabilities(StateP state, DemeP deme)
{
	IndividualP bestFood = selBestOp->select(*deme);
	double bestFitness = bestFood->fitness->getValue();
	double worstFitness = selWorstOp->select(*deme)->fitness->getValue();
	double offset = 0;

	// scale fitness values
	if(bestFitness < worstFitness && bestFitness < 0)
		offset = 0.1 - bestFitness;	// minimization
	else if(worstFitness < 0)
		offset = 0.1 - worstFitness;	// maximization

	// for each food source
	for( uint i = 0; i < deme->getSize(); i++ ) {
		IndividualP food = deme->at(i);
		double thisFitness = food->fitness->getValue();

		if (bestFitness == thisFitness)
			probability_[i] = 1.0;
		else if (thisFitness < bestFitness) // maximization problems
			probability_[i] = 0.1 + 0.9 * (thisFitness + offset) / (bestFitness + offset);
		else								// minimization problems
			probability_[i] = 0.1 + 0.9 * (bestFitness + offset) / (thisFitness + offset);

		// using selFitPropOp method
		//probability_[i] = 0.1 + 0.9 * (thisFitness - worstFitness)/(bestFitness - worstFitness);
	}
	return true;
}
bool ArtificialBeeColony::onlookerBeesPhase(StateP state, DemeP deme)
{
// jednostavni odabir, uz selFitPropOp
/*	for( uint i = 0; i < deme->getSize(); i++ ) { // for each food source
		// choose a food source depending on its fitness value (better individuals are more likely to be chosen)
		IndividualP food = selFitOp->select(*deme);
		createNewFoodSource(food, state, deme);
	}
*/


// uz vjerojatnosti, jedinka po jedinka
	calculateProbabilities(state, deme);
	int demeSize = deme->getSize();
	int i = state->getRandomizer()->getRandomInteger(demeSize);
	int n = 0;
	while( n < demeSize) {
		int fact = i++ % demeSize;
		IndividualP food = deme->at(fact);
		
		if (state->getRandomizer()->getRandomDouble() < probability_[fact]){
			n++;
			createNewFoodSource(food, state, deme);
		}			
	}

	return true;
}
Exemplo n.º 6
0
		bool birthPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
		{
			//number of new antibodies (randomly created)
			uint birthNumber = deme->getSize() - clones.size();

			//if no new antibodies are needed, return (this if part is optional, code works fine w/o it)
			if (birthNumber == 0) return true;

			IndividualP newAntibody = copy(deme->at(0));
			FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (newAntibody->getGenotype(0));
	
			for (uint i = 0; i<birthNumber; i++){
				//create a random antibody
				flp->initialize(state);
				evaluate(newAntibody);
			
				//reset its age
				flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (newAntibody->getGenotype(1));
				double &age = flp->realValue[0];
				age = 0;

				//add it to the clones vector
				clones.push_back(copy(newAntibody));
			}
			return true;
		}
		 bool employedBeesPhase(StateP state, DemeP deme)
        {	
			for( uint i = 0; i < deme->getSize(); i++ ) { // for each food source
				IndividualP food = deme->at(i);
				createNewFoodSource(food, state, deme);
			}
            return true;
        }
Exemplo n.º 8
0
		bool cloningPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
		{
			// storing all antibodies in a vector
			for( uint i = 0; i < deme->getSize(); i++ )  // for each antibody	
				clones.push_back(deme->at(i));

			for( uint i = 0; i < deme->getSize(); i++ ){ // for each antibody in clones vector
				IndividualP antibody = clones.at(i);
				
				// static cloning is fitness independent : : cloning each antibody dup times
				for (uint j = 0; j < dup; j++) 
					clones.push_back(copy(antibody));					
			}

			return true;
		}
//! cross donor vectors with population members to create trial vectors
void DifferentialEvolution::crossover(DemeP deme, uint index, StateP state)
{
	// get population member and corresponding donor vector
	FloatingPoint::FloatingPoint* flp1 = (FloatingPoint::FloatingPoint*) (deme->at(index)->getGenotype().get());
	int dim = (int) flp1->realValue.size();
	FloatingPoint::FloatingPoint* flp2 = (FloatingPoint::FloatingPoint*) donor_vector[index]->getGenotype().get();

	// crossover their elements (keep the result in donor_vector)
	for(uint i = 0; i < flp1->realValue.size(); i++) {
		if (state->getRandomizer()->getRandomDouble() <= CR_ || i == state->getRandomizer()->getRandomInteger(dim)) {
		}
		else {
			flp2->realValue[i] = flp1->realValue[i];
	    }
	}

}
Exemplo n.º 10
0
bool PSOInheritance::advanceGeneration(StateP state, DemeP deme)
{
//       a) For each particle:
//          1) If the fitness value is better than the best fitness value (pBest) in  history
//          2) Set current value as the new pBest
//          3) Put particle in the ranking array using the fitness value
//          End
//       b) For the p best particles in the ranking arrayºº
//          1) Find, in the particle neighborhood, the particle with the best fitness
//          2) Calculate particle velocity according to the velocity equation (1)
//          3) Apply the velocity constriction
//          4) Update particle position according to the position equation (2)
//          5) Apply the position constriction
//       c) Inherit // Evaluate
//          1) Get the fitness value via evaluation or inheritance.
//          End

	for( uint i = 0; i < deme->getSize(); i++ ) { // for each particle 
        IndividualP particle = deme->at(i);                                                                             //Read "i" particle

		// the whole point of this section is to compare fitness and pbest
        FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(3));
        double &particlePbestFitness = flp->realValue[0];
        double fitness = particle->fitness->getValue();

        //There is a problem with the particlePbestFitness initialization in this algorithm. The following lines take care of this.
        //TODO: Find a way to do this in the ¿initialize fuction?.
        if(state->getGenerationNo()==1){
            //std::cout<<"INCIALIZADOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!"<<std::endl;
            particlePbestFitness=fitness;
        }
        else{
            //std::cout<<"FITNESS"<<fitness<<std::endl;

            flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(0));
            std::vector< double > &positions = flp->realValue;

            flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(2));
            std::vector< double > &pbestx = flp->realValue;

            // set particle pbestx-es
            if( /*iter == 0 ||*/ fitness < particlePbestFitness ) { // minimize error
                particlePbestFitness = fitness; //Update particle personal fitness

                // set pbestx-es
                for( uint j = 0;j<pbestx.size();j++ ) {
                    pbestx[j] = positions[j];
                }
            }
        }

		// NOTE store best particle index?
        //std::cout<<"THE PBEST OF THIS PARTICLE IS!!!!!!!!!!!!:"<<particlePbestFitness<<std::endl;
	}

	// b)
	for( uint i = 0; i < deme->getSize(); i++ ) { // for each particle
		IndividualP particle = deme->at(i);

		IndividualP bestParticle = selBestOp->select( *deme );

		FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(0));
		std::vector< double > &positions = flp->realValue;

		flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(1));
		std::vector< double > &velocities = flp->realValue;

		flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(2));
        std::vector< double > &pbestx = flp->realValue;

        double R1=rand()/(float)RAND_MAX, R2=rand()/(float)RAND_MAX, vf;
        int C1=2, C2=2;



        double weight_up;

		switch( m_weightType )
		{
			//time variant weight, linear from weight to 0.4
			case TIME_VARIANT:
			weight_up = ( m_weight - 0.4 ) * ( m_maxIter - state->getGenerationNo() ) / m_maxIter + 0.4;
			break;

			// constant inertia weight
			case CONSTANT:
            default:
			weight_up = m_weight;
			break;
		}
		// calculate particle velocity according to the velocity equation (1)
		flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (bestParticle->getGenotype(2));
		std::vector< double > &bestParticlesPbestx = flp->realValue;
		for( uint j = 0; j < velocities.size(); j++ ) {
			double velocity;

			velocity = weight_up * velocities[j] +
               2 * R1 * (pbestx[j] - positions[j]) +
               2 * R2 * (bestParticlesPbestx[j] - positions[j]);

			if( velocity > m_maxV ) velocity = m_maxV;
            if( velocity < -m_maxV) velocity = -m_maxV;
			velocities[j] = velocity;

            positions[j] += velocities[j];      //Updated positions with velocitites X(t+1)=X(t)+velocities(t);
			// TODO apply position constriction

			// check for bounds
            if(bounded_) {
				if(positions[j] < lbound_)
					positions[j] = lbound_;
				if(positions[j] > ubound_)
					positions[j] = ubound_;
			}

            //std::cout<<"LA VELOCIDAD ES::::"<<velocity<<std::endl;
		}

        int proportion=55;
        if(rand()%100>=proportion){
            //Initial PSO inheritance algorithm -> 100%inheritance no evaluations.
            //determine new particle fitness

            //Particle best personal fitness
//            flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(3));
//            double &particlePbestFitness = flp->realValue[0];

            //Best particle fitness
//            flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (bestParticle->getGenotype(3));
//            double &bestparticlePbestFitness = flp->realValue[0];

//            vf=(C1*R1*(particlePbestFitness-particle->fitness->getValue())+C2*R2*(bestparticlePbestFitness-particle->fitness->getValue()))
//                    /(1+C1*R1+C2*R2);

//            vf=vf+particle->fitness->getValue();



            evaluate( particle );

            //std::cout<< " Inherited: " << vf<< " -> Evaluated "<< particle->fitness->getValue() <<std::endl ;

        }
        else{
            //Particle best personal fitness
            flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (particle->getGenotype(3));
            double &particlePbestFitness = flp->realValue[0];

            //Best particle fitness
            flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (bestParticle->getGenotype(3));
            double &bestparticlePbestFitness = flp->realValue[0];

            //std::cout<<std::endl<<"The EverPersonalBEST of this particle is:"<<particlePbestFitness<<std::endl;
            //std::cout<<std::endl<<"THE present LEADER(Allbes) of this particle fitness is:"<<bestparticlePbestFitness<<std::endl;

            //Inheritance based on flight formula
            std::cout<<"Particle Fitness"<<particle->fitness->getValue()<<" "<<std::endl;


            //Fitness inheritance
            vf=(C1*R1*(particlePbestFitness-particle->fitness->getValue())+C2*R2*(bestparticlePbestFitness-particle->fitness->getValue()))
                    /(1+C1*R1+C2*R2);
            particle->fitness->setValue(vf+particle->fitness->getValue());

            std::cout<< " Inherited: " << particle->fitness->getValue()<< " "<< std::endl ;
        }

	}

    //std::cout<<std::endl<<"THE NUMBER OF THIS GENERATION IS:"<<state->getGenerationNo() <<std::endl;
    std::cout<<std::endl<<"THE NUMBER OF EVALUATIONS ARE:"<<state->getEvaluations() <<std::endl;
    std::cout<<std::endl<<"THE TIME TAKEN TO DO THIS IS:"<<state->getElapsedTime() <<std::endl;

    //*******************FILE OUTPUT FOR DEBUGGING***********************************************//
    IndividualP bestParticle = selBestOp->select( *deme );

    FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (bestParticle->getGenotype(3));

    double &bestparticlePbestFitness = flp->realValue[0];
    std::ofstream myfile1;
    myfile1.open("FitnessvsEvaluations.txt", std::ios_base::app);
    if (myfile1.is_open()){
        myfile1<<bestparticlePbestFitness<<" ";
        myfile1<<state->getEvaluations()<<std::endl;
    }
    //*******************************************************************************************//

	return true;
}