Exemplo n.º 1
0
bool TermStagnationOp::operate(StateP state)
{
	uint currentGen = state->getGenerationNo();
	if(currentGen - state->getPopulation()->getHof()->getLastChange() > termStagnation_) {
		state->setTerminateCond();
		ECF_LOG(state, 1, "Termination: maximum number of generations without improvement ("
			+ uint2str(termStagnation_) + ") reached");
	}

	return true;
}
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;
}