10 #include <boost/bind.hpp>
24 boost::shared_ptr<GeneralObjective>
operator()(boost::shared_ptr<
27 return boost::shared_ptr<GeneralObjective>(param->clone());
38 transform(IndObjective.begin(), IndObjective.end(), result.begin(),
46 const int iterationnumber,
const int Programnum)
48 ostringstream filename;
49 filename <<
"j" << j <<
"i" << i <<
"g" << iterationnumber <<
"p"
51 return filename.str();
58 CombMisFit(LocalPopulation->GetPopsize()), AvgFit(IndObjective.
size()),
59 MaxFit(IndObjective.
size()), MinFit(IndObjective.
size()), Weights(
60 IndObjective.
size(), 1), nobjective(IndObjective.
size()),
61 Transcribed(LocalPopulation->GetPopsize(),
62 LocalTranscribe->GetNparams()), MisFit(IndObjective.
size(),
63 LocalPopulation->GetPopsize()), Objective(IndObjective),
64 Propagation(LocalPropagation), Population(LocalPopulation),
65 Transcribe(LocalTranscribe)
69 Programnum = getpid();
81 for (
unsigned int j = 0; j < ParameterIndices.at(i).size(); ++j)
83 params.at(i)(j) = member(ParameterIndices.at(i).at(j));
89 if (LocalWeights.size() != Weights.size())
91 "Configured number of weights does not match number of objective functions !");
92 Weights = LocalWeights;
97 ParameterIndices = Indices;
104 output << MaxFit.at(i) <<
" " << MinFit.at(i) <<
" "
105 << AvgFit.at(i) <<
" ";
107 output << CombMaxFit <<
" " << CombMinFit <<
" " << CombAvgFit <<
" ";
108 output << endl << flush;
113 const unsigned int isize =
MisFit.size1();
114 const unsigned int jsize =
MisFit.size2();
116 for (
unsigned int i = 0; i < isize; ++i)
118 for (
unsigned int j = 0; j < jsize; ++j)
119 output <<
MisFit(i, j) <<
" ";
122 copy(CombMisFit.begin(), CombMisFit.end(), ostream_iterator<double> (
124 output << endl << endl;
132 for (i = 0; i < isize; ++i)
134 for (j = 0; j < jsize; ++j)
138 output << endl << flush;
144 const unsigned int isize = Indices.size();
145 const unsigned int jsize =
MisFit.size1();
147 for (
unsigned int i = 0; i < isize; ++i)
149 const int currindex = Indices.at(i);
150 for (
unsigned int j = 0; j < jsize; ++j)
151 output <<
MisFit(j, currindex) <<
" ";
154 output << endl << endl;
157 void GeneralGA::CalcMisfit(
const int iterationnumber)
159 int calculatecount = 0;
163 ublas::vector<bool> AlreadyCalculated(popsize);
166 #pragma omp parallel for default(shared)
167 for (
int i = 0; i < popsize; ++i)
172 LocalParameters.at(k).resize(ParameterIndices.at(k).size(),
179 if (!AlreadyCalculated(i))
186 iterationnumber, Programnum));
187 if (Weights.at(j) != 0)
191 LocalObjective.at(j)->PreParallel(
192 LocalParameters.at(j));
194 LocalObjective.at(j)->SafeParallel(
195 LocalParameters.at(j));
198 MisFit(j, i) = Weights.at(j)
199 * LocalObjective.at(j)->PostParallel(
200 LocalParameters.at(j));
201 CombMisFit.at(i) +=
MisFit(j, i);
213 UniquePopHist.
Insert(fitvector, ublas::matrix_row<
220 column(
MisFit, i) = fitvec;
221 CombMisFit.at(i) = ublas::sum(fitvec);
225 cout <<
"New models: " << newcount <<
" Re-used models: "
226 << calculatecount << endl;
231 cout << endl << endl <<
"Iteration: " << iterationnumber + 1 << endl;
232 cout <<
"Calculating Misfit ...";
233 CalcMisfit(iterationnumber);
234 cout <<
" done" << endl << flush;
238 if ((iterationnumber >= 1) && Elitist)
242 CalcMisfit(iterationnumber);
248 ublas::matrix_row<gplib::rmat> mr(
MisFit, i);
249 AvgFit.at(i) =
Mean(mr.begin(), mr.end());
250 MaxFit.at(i) = *max_element(mr.begin(), mr.end());
251 MinFit.at(i) = *min_element(mr.begin(), mr.end());
253 CombAvgFit =
Mean(CombMisFit.begin(), CombMisFit.end());
254 CombMaxFit = *max_element(CombMisFit.begin(), CombMisFit.end());
255 CombMinFit = *min_element(CombMisFit.begin(), CombMisFit.end());
The base class for the population of a genetic algorithm, implements storage and access functions...
std::string MakeParallelID(const int j, const int i, const int iterationnumber, const int Programnum)
Generate a unique ID for filenames etc. so we can call generated scripts in parallel.
ublas::vector< double > ttranscribed
GeneralPopulation *const Population
A pointer to an object holding the population.
bool Insert(const tfitvec &fitness, const ttranscribed &popmember)
std::iterator_traits< InputIterator >::value_type Mean(InputIterator begin, InputIterator end)
Calculate the mean for a given range.
void PrintTranscribed(std::ostream &output)
Print transcribed values for each member to output.
virtual void DoIteration(const int iterationnumber, const bool last)
Do one iteration of the GA.
virtual std::vector< int > GetBestModelIndices()=0
Return the indices of the best models.
virtual void Elitism(const int iterationnumber)
default implementation does nothing, this can be overriden
bool Find(const ttranscribed &popmember, tfitvec &fitness)
Copy the objective function within the shared pointer.
Generate a new copy of the Objective function vector.
const unsigned int nobjective
The number of objective functions we're using.
The base class for genetic algorithm propagation methods.
virtual ttranscribed GetValues(const tpopmember &member)=0
gplib::rmat MisFit
Misfit first index objective function second index population member.
std::vector< gplib::rvec > tparamvector
We provide some typedefs that are used in other parts as well.
void PrintFitStat(std::ostream &output)
Print Fitness statistics for each objective function to output.
void PrintBestMisfit(std::ostream &output)
Print misfit of the best population members.
std::vector< std::vector< int > > tparamindv
GeneralTranscribe *const Transcribe
A pointer to an object translating genes to model vectors.
tObjectiveVector Objective
The vector holding the objective functions.
General Transcribe base class for genetic algorithm parameter transcription.
void SetWeights(const std::vector< double > &LocalWeights)
Set the weights for each objective function.
The basic object for any objective function, mainly an interface class and some storage.
void StoreOldPopulation()
virtual void CalcProbabilities(const int iterationnumber, gplib::rmat &LocalMisFit, GeneralPopulation &LocalPopulation)=0
Calculate the Probabilities.
const tpopulation & GetPopulation() const
ublas::vector< double > tfitvec
GeneralPropagation *const Propagation
A pointer to an object dealing with propagation.
GeneralGA::tObjectiveVector operator()(const GeneralGA::tObjectiveVector &IndObjective)
boost::shared_ptr< GeneralObjective > operator()(boost::shared_ptr< GeneralObjective > param)
void SetParameterIndices(const tparamindv &Indices)
Configure which parts of the complete parameter vector are used in each objective function...
virtual void NextGeneration()=0
std::vector< boost::shared_ptr< GeneralObjective > > tObjectiveVector
gplib::rmat Transcribed
A matrix holding the numerical model vectors (transcribed from the genes) for each population member...
void SetupParams(const ttranscribed &member, tparamvector ¶ms)
Copy the appropriate parameter values from the transcribed vector for use with the objective function...
void PrintMisfit(std::ostream &output)
Print misfit for each member to output.
The basic exception class for all errors that arise in gplib.