Fix according to
[ 1663606 ] eoBitOp vector access
This commit is contained in:
parent
44fab2d8e4
commit
4c89acad72
2 changed files with 67 additions and 62 deletions
11
eo/src/ga/ChangeLog
Normal file
11
eo/src/ga/ChangeLog
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
2007-08-21 Jochen Küpper <jochen@fhi-berlin.mpg.de>
|
||||||
|
|
||||||
|
* eoBitOp.h (eoNPtsBitXover::operator()): Make sure bit is within
|
||||||
|
allocated length of vector points: [0, max_size).
|
||||||
|
|
||||||
|
|
||||||
|
* Local Variables:
|
||||||
|
* coding: iso-8859-1
|
||||||
|
* mode: flyspell
|
||||||
|
* fill-column: 80
|
||||||
|
* End:
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
Marc.Schoenauer@polytechnique.fr
|
Marc.Schoenauer@polytechnique.fr
|
||||||
mak@dhi.dk
|
mak@dhi.dk
|
||||||
CVS Info: $Date: 2003-06-06 10:29:13 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/ga/eoBitOp.h,v 1.17 2003-06-06 10:29:13 maartenkeijzer Exp $ $Author: maartenkeijzer $
|
CVS Info: $Date: 2007-08-21 14:52:50 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/ga/eoBitOp.h,v 1.18 2007-08-21 14:52:50 kuepper Exp $ $Author: kuepper $
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -315,64 +315,58 @@ template<class Chrom> class eoUBitXover: public eoQuadOp<Chrom>
|
||||||
\class eoNPtsBitXover eoBitOp.h ga/eoBitOp.h
|
\class eoNPtsBitXover eoBitOp.h ga/eoBitOp.h
|
||||||
\ingroup bitstring
|
\ingroup bitstring
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class Chrom> class eoNPtsBitXover : public eoQuadOp<Chrom>
|
template<class Chrom> class eoNPtsBitXover : public eoQuadOp<Chrom>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// (Default) Constructor.
|
|
||||||
|
/** (Default) Constructor. */
|
||||||
eoNPtsBitXover(const unsigned& _num_points = 2) : num_points(_num_points)
|
eoNPtsBitXover(const unsigned& _num_points = 2) : num_points(_num_points)
|
||||||
{
|
{
|
||||||
if (num_points < 1)
|
if (num_points < 1)
|
||||||
std::runtime_error("NxOver --> invalid number of points");
|
std::runtime_error("NxOver --> invalid number of points");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The class name.
|
/** The class name */
|
||||||
virtual std::string className() const { return "eoNPtsBitXover"; }
|
virtual std::string className() const { return "eoNPtsBitXover"; }
|
||||||
|
|
||||||
/**
|
/** n-point crossover for binary chromosomes.
|
||||||
* n-point crossover for binary chromosomes.
|
|
||||||
* @param chrom1 The first chromosome.
|
|
||||||
* @param chrom2 The first chromosome.
|
|
||||||
*/
|
|
||||||
bool operator()(Chrom& chrom1, Chrom& chrom2)
|
|
||||||
{
|
|
||||||
unsigned max_size = std::min(chrom1.size(), chrom2.size());
|
|
||||||
unsigned max_points = std::min(max_size - 1, num_points);
|
|
||||||
|
|
||||||
|
@param chrom1 The first chromosome.
|
||||||
|
@param chrom2 The first chromosome.
|
||||||
|
*/
|
||||||
|
bool operator()(Chrom& chrom1, Chrom& chrom2) {
|
||||||
|
unsigned max_size(std::min(chrom1.size(), chrom2.size()));
|
||||||
|
unsigned max_points(std::min(max_size - 1, num_points));
|
||||||
std::vector<bool> points(max_size, false);
|
std::vector<bool> points(max_size, false);
|
||||||
|
|
||||||
// select ranges of bits to swap
|
// select ranges of bits to swap
|
||||||
do {
|
do {
|
||||||
unsigned bit = eo::rng.random(max_size) + 1;
|
unsigned bit(eo::rng.random(max_size));
|
||||||
if(points[bit])
|
if(points[bit])
|
||||||
continue;
|
continue;
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
points[bit] = true;
|
points[bit] = true;
|
||||||
max_points--;
|
--max_points;
|
||||||
}
|
}
|
||||||
} while(max_points);
|
} while(max_points);
|
||||||
|
|
||||||
|
|
||||||
// swap bits between chromosomes
|
// swap bits between chromosomes
|
||||||
bool change = false;
|
bool change(false);
|
||||||
for (unsigned bit = 1; bit < points.size(); bit++)
|
for (unsigned bit = 1; bit < points.size(); bit++) {
|
||||||
{
|
|
||||||
if (points[bit])
|
if (points[bit])
|
||||||
change = !change;
|
change = !change;
|
||||||
|
if (change) {
|
||||||
if (change)
|
|
||||||
{
|
|
||||||
typename Chrom::AtomType tmp = chrom1[bit];
|
typename Chrom::AtomType tmp = chrom1[bit];
|
||||||
chrom1[bit] = chrom2[bit];
|
chrom1[bit] = chrom2[bit];
|
||||||
chrom2[bit] = tmp;
|
chrom2[bit] = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
/** @todo Document this data member */
|
||||||
unsigned num_points;
|
unsigned num_points;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue