From 75c2c6ab767354e886cf246fd10bbd5086781006 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 5 Jul 2020 17:59:30 +0200 Subject: [PATCH] fix eoDetSingdeBitFlip: better algorithm The old version was inefficient, using trial and errors. This new version is a single pass algorithm. --- eo/src/ga/eoBitOp.h | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/eo/src/ga/eoBitOp.h b/eo/src/ga/eoBitOp.h index 0e88b492f..8645a86e1 100644 --- a/eo/src/ga/eoBitOp.h +++ b/eo/src/ga/eoBitOp.h @@ -99,18 +99,19 @@ template class eoDetBitFlip: public eoMonOp }; -/** eoDetSingleBitFlip --> changes exactly k bits with checking for duplicate -\class eoDetSingleBitFlip eoBitOp.h ga/eoBitOp.h +/** Changes exactly k bits with checking for duplicate \ingroup bitstring */ - -template class eoDetSingleBitFlip: public eoMonOp +template +class eoDetSingleBitFlip: public eoMonOp { public: /** * (Default) Constructor. * @param _num_bit The number of bits to change * default is one - equivalent to eoOneBitFlip then + * + * @note: use a reference for num_bit, thus you may change and recall without having to re-instantiate. */ eoDetSingleBitFlip(const unsigned& _num_bit = 1): num_bit(_num_bit) {} @@ -122,32 +123,34 @@ template class eoDetSingleBitFlip: public eoMonOp * @param chrom The cromosome which one bit is going to be changed. */ bool operator()(Chrom& chrom) - { - std::vector< unsigned > selected; + { + // All possible indices + std::vector< unsigned > indices; + indices.reserve(chrom.size()); + for(unsigned i=0; i 0) { + return true; + } else { + return false; + } + } - for ( size_t i = 0; i < selected.size(); ++i ) - { - chrom[i] = !chrom[i]; - } - - return true; - } - private: - unsigned num_bit; + protected: + const unsigned& num_bit; };