Hi everyond. I'm trying to write a library usign policy based design
so I can implement
different behaviors. One of the behaviors was to define a
StoragePolicy. The following code gives the whole idea for a
population object:
#ifndef _POPULATION_H
#define _POPULATION_H
#include "gaParameters.h"
#include "gaIndividual.h"
using namespace std;
template <class T>
class StoragePolicy
{
void create(size_t,size_t);
};
template <class T>
struct StdVectorStorage
{
typename std::vector<T>::iterator it_;
std::vector<Tstorage_;
void create(size_t p,size_t l)
{
storage_ = std::vector<T>(p);
for(it_ = storage_.begin(); it_ != storage_.end(); ++it_)
it_->init(l);
}
protected:
~StdVectorStorage()
{
}
};
template
<
class Individual,
template <classclass StoragePolicy = StdVectorStorage
class Population : public StoragePolicy<Individual>
{
public:
gaParameters* gaParams_;
// initialize population
void initialize(gaParameters* p);
};
template
<
typename Individual,
template <classclass StoragePolicy
void Population<Individual,StoragePolicy>::initialize(g aParameters* p)
{
// assign pointer to parameter list
gaParams_ = p;
unsigned int xsite_; //!< Crossover point
// create population according to the desired policy
this->create(
(size_t)gaParams_->get(popSize),
(size_t)gaParams_->get(lChrom));
}
#endif
Now, since I'm using a storage policy, I thought that it may be nice to
try different data structures to hold the population. One of them of
course is the std::vector so that is the first one I'm using here.
Then, the user will only need to specify the storage used in this
context:
int main(int argc, char *argv[]){
typedef Chromosome<BoostBitsetStoragechrom;
typedef Individual<chrom, HeapStorageind;
typedef Population<ind,StdVectorStoragepop;
....
There are many things that bother me from this design:
1. I didn't know if it was fine to declare the population size having a
Pointer to the actual storage as this:
template
<
class Pointer,
class Individual,
template <classclass StoragePolicy = StdVectorStorage
class Population : public StoragePolicy<Individual>
{
Pointer *pointee_;
....
because then the user needs to know what the Pointer points to (and
this information is supposed to be within the StoragePolicy).
Therefore, I created within the policy class, a pointee_ variable that
points to the actual data structures where the individuals are created
(and this variable is inherited when the Population is instantiated).
Is there a better way to accomplish this?
2. Now, If I create another policy parallel to the StoragePolicy, then
I need to know what kind of data structure the StoragePolicy is using
and this is really what is bothering me right now because it is telling
me that this policy cannot be orthogonalized with other policies
(unless I can access the structure using a random access iterator,
thing that I cannot do with my knowledge of C++ at this moment).
Any ideas???