GPLIB++
BinaryTranscribe.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include "BinaryTranscribe.h"
3 #include "FatalException.h"
4 #include <boost/numeric/conversion/cast.hpp>
5 
6 namespace gplib
7  {
9  const ttranscribed &step, const tsizev &gene) :
10  basevalues(base), stepsizes(step), genesizes(gene)
11  {
12  //check that all three vectors have the same size
13  if ((basevalues.size() != stepsizes.size()) || (basevalues.size()
14  != genesizes.size()))
15  throw FatalException(
16  "Basevalues, Stepsizes and Genesizes do not have equal length !");
17  //they all have the same size, so we only check whether one is empty
18  if (basevalues.empty())
19  throw FatalException("Specified an empty base value vector for binary transcribe !");
20 
21  }
22 
24  {
25  }
26 
28  {
29  unsigned int currentindex = 0;
30  double currentvalue;
31 
32  ttranscribed ReturnValues(genesizes.size());
33  //for each model parameter
34  for (unsigned int i = 0; i < genesizes.size(); ++i)
35  {
36  currentvalue = 0;
37  //scan the corresponding section of the genetic string
38  for (unsigned int j = currentindex; j < currentindex + genesizes(i); ++j)
39  {
40  //calculate the numeric value from the bits
41  //some compilers have trouble using pow with some combinations of numeric types
42  //so we explicitly use double and int which should work everywhere
43  currentvalue += member(j) * std::pow(2.0, boost::numeric_cast<int>(j - currentindex));
44  }
45  //currentvalue now holds the raw integer value that is encoded in the bit string
46  //so we multiply by the step size and add the basevalue to get the parameter value
47  currentvalue *= stepsizes(i);
48  currentvalue += basevalues(i);
49  currentindex += genesizes(i);
50  ReturnValues(i) = currentvalue; //store in the right component
51  }
52  return ReturnValues;
53  }
54 
56  basevalues(Old.basevalues), stepsizes(Old.stepsizes), genesizes(
57  Old.genesizes)
58  {
59  // The body is empty
60  }
61 
62  }
BinaryTranscibe implements transcription for standard binary populations.
ublas::vector< double > ttranscribed
Definition: gentypes.h:21
BinaryTranscribe(const ttranscribed &base, const ttranscribed &step, const tsizev &gene)
Without basevalues, stepsizes and genesizes BinaryTranscribe does not work, so we enforce their use b...
virtual ttranscribed GetValues(const tpopmember &member)
Implements the abstract function from GeneralTranscribe.
const ttranscribed stepsizes
The stepsize associated to a bit change for each parameter.
ublas::vector< bool > tpopmember
Definition: gentypes.h:22
const tsizev genesizes
The number of bits for each parameter.
ublas::vector< int > tsizev
Definition: gentypes.h:27
The basic exception class for all errors that arise in gplib.
const ttranscribed basevalues
The minimum value for each parameter.