added best position updating + printOn

This commit is contained in:
tlegrand 2007-10-08 15:22:25 +00:00
commit c864f2deb5
4 changed files with 24 additions and 29 deletions

View file

@ -135,32 +135,14 @@ public:
bool operator< (const PO & _po2) const { return fitness () < _po2.fitness ();} bool operator< (const PO & _po2) const { return fitness () < _po2.fitness ();}
bool operator> (const PO & _po2) const { return !(fitness () <= _po2.fitness ());} bool operator> (const PO & _po2) const { return !(fitness () <= _po2.fitness ());}
/**
/**
* Write object. Called printOn since it prints the object _on_ a stream. * Write object. Called printOn since it prints the object _on_ a stream.
* @param _os A std::ostream. * @param _os A std::ostream.
*/ */
virtual void printOn(std::ostream& _os) const { virtual void printOn(std::ostream& _os) const { _os << bestFitness <<' ' ;}
// the latest version of the code. Very similar to the old code
if (invalid()) {
_os << "INVALID ";
}
else
{
_os << repFitness << ' ' ;
}
if (invalidBest()) {
_os << "INVALID BEST";
}
else
{
_os << "best: " << bestFitness ;
}
}
/** /**
* Read object.\\ * Read object.\\
* Calls base class, just in case that one had something to do. * Calls base class, just in case that one had something to do.

View file

@ -128,17 +128,18 @@ public:
return i; return i;
} }
/**
/**
* Update the neighborhood: update the particle's best fitness and the best particle * Update the neighborhood: update the particle's best fitness and the best particle
* of the corresponding neighborhood. * of the corresponding neighborhood.
*/ */
void updateNeighborhood(POT & _po,unsigned _indice) void updateNeighborhood(POT & _po,unsigned _indice)
{ {
// update the best fitness of the particle // update the best fitness of the particle
if (_po.fitness() > _po.best()) if (_po.fitness() > _po.best())
{ {
_po.best(_po.fitness()); _po.best(_po.fitness());
for(unsigned i=0;i<_po.size();i++)
_po.bestPositions[i]=_po[i];
} }
// update the best in its neighborhood // update the best in its neighborhood
@ -149,7 +150,6 @@ public:
} }
} }
/** /**
* Return the best informative of a particle. Could be itself. * Return the best informative of a particle. Could be itself.
* @param _indice - The indice of a particle in the population * @param _indice - The indice of a particle in the population

View file

@ -77,7 +77,7 @@ public:
} }
} }
/* /*
* Update the best fitness of the given particle if it's better. * Update the best fitness of the given particle if it's better.
* Also replace the global best by the given particle if it's better. * Also replace the global best by the given particle if it's better.
* @param _po - The particle to update * @param _po - The particle to update
@ -88,7 +88,9 @@ public:
// update the best fitness of the particle // update the best fitness of the particle
if (_po.fitness() > _po.best()) if (_po.fitness() > _po.best())
{ {
_po.best(_po.fitness()); _po.best(_po.fitness());
for(unsigned i=0;i<_po.size();i++)
_po.bestPositions[i]=_po[i];
} }
// update the global best if the given particle is "better" // update the global best if the given particle is "better"
if (_po.fitness() > neighborhood.best().fitness()) if (_po.fitness() > neighborhood.best().fitness())
@ -106,7 +108,7 @@ public:
POT & best (unsigned _indice) {return (neighborhood.best());} POT & best (unsigned _indice) {return (neighborhood.best());}
/** /**
* Print the structure of the topology on the standard output. * Print the structure of the topology on the standard output.
* @param * @param
* @return * @return
@ -120,6 +122,7 @@ public:
} }
protected: protected:
eoSocialNeighborhood<POT> neighborhood; // the only neighborhood eoSocialNeighborhood<POT> neighborhood; // the only neighborhood
bool isSetup; bool isSetup;

View file

@ -120,6 +120,16 @@ public:
velocities.resize (_size); velocities.resize (_size);
} }
/**
* Print-on a vector-particle
*/
virtual void printOn(std::ostream& os) const
{
PO<FitT>::printOn(os);
os << ' ';
os << size() << ' ';
std::copy(bestPositions.begin(), bestPositions.end(), std::ostream_iterator<AtomType>(os, " "));
}
/* public attributes */ /* public attributes */
std::vector < PositionType > bestPositions; std::vector < PositionType > bestPositions;