GRALE
treelensinversiongafactorybase.h
1 /*
2 
3  This file is a part of GRALE, a library to facilitate the simulation
4  and inversion of gravitational lenses.
5 
6  Copyright (C) 2008-2012 Jori Liesenborgs
7 
8  Contact: jori.liesenborgs@gmail.com
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 
24 */
25 
26 #ifndef GRALE_TREELENSINVERSIONGAFACTORYBASE_H
27 
28 #define GRALE_TREELENSINVERSIONGAFACTORYBASE_H
29 
30 #include "graleconfig.h"
31 #include "randomnumbergenerator.h"
32 #include <mogal/gafactory.h>
33 #include <vector>
34 
35 namespace grale
36 {
37 
38 class TreeLensInversionGAFactoryParams;
39 class TreeLensInversionGenomeBase;
40 class GridNodeBase;
41 class BackProjectTreeBase;
42 class ImagesDataExtended;
43 
44 // NOTE: the virtual inheritance is again very important!
45 class GRALE_IMPORTEXPORT TreeLensInversionGAFactoryBase : public virtual mogal::GAFactory
46 {
47 public:
48  TreeLensInversionGAFactoryBase();
49  ~TreeLensInversionGAFactoryBase();
50 
51  mogal::GAFactoryParams *createParamsInstance() const;
52  const mogal::GAFactoryParams *getCurrentParameters() const;
53 
54  bool init(const mogal::GAFactoryParams *p);
55 
56  mogal::Genome *createNewGenome() const;
57 
58  size_t getMaximalFitnessSize() const { return sizeof(float)*(1+getNumberOfFitnessComponents()); } // one for scale factor, some for fitness
59  size_t getMaximalGenomeSize() const { return m_maxGenomeSize; }
60 
61  bool writeGenome(serut::SerializationInterface &si, const mogal::Genome *g) const;
62  bool readGenome(serut::SerializationInterface &si, mogal::Genome **g) const;
63  bool writeGenomeFitness(serut::SerializationInterface &si, const mogal::Genome *g) const;
64  bool readGenomeFitness(serut::SerializationInterface &si, mogal::Genome *g) const;
65  bool writeCommonGenerationInfo(serut::SerializationInterface &si) const;
66  bool readCommonGenerationInfo(serut::SerializationInterface &si);
67 
68  const RandomNumberGenerator *getRandomNumberGenerator() const { return &m_rndGen; }
69 
70  int getMaximumNumberOfBasisFunctions() const { return m_maxBasisFunctions; }
71  int getMaximumDepth() const { return m_maxDepth; }
72  bool useWeights() const { return m_useWeights; }
73 
74  virtual float getChanceMultiplier() = 0;
75  virtual bool useAbsoluteMutation() = 0;
76  virtual float getMutationAmplitude() = 0;
77 protected:
78  // if 'pRootNode' is null, random masses should be used
79  virtual TreeLensInversionGenomeBase *allocateNewGenome(TreeLensInversionGAFactoryBase *pFactory,
80  BackProjectTreeBase *pBackProjectTree,
81  GridNodeBase *pRootNode,
82  int firstLevelSubdiv) const = 0;
83 
84  int getMaximumNumberOfGenerations() const { return m_maxGenerations; }
85  virtual bool subInit(std::list<ImagesDataExtended *> &images) { return true; }
86 
87 #ifdef SHOWEVOLUTION
88  void onSortedPopulation(const std::vector<mogal::GenomeWrapper> &population);
89 #endif // SHOWEVOLUTION
90 private:
91  bool writeNode(serut::SerializationInterface &si, const GridNodeBase *pNode) const;
92  bool readNode(serut::SerializationInterface &si, GridNodeBase **pNode, int level) const;
93 
94  BackProjectTreeBase *m_pBackProjectTree;
95  RandomNumberGenerator m_rndGen;
96  TreeLensInversionGAFactoryParams *m_pCurrentParams;
97  int m_maxGenerations;
98  int m_maxDepth;
99  int m_maxBasisFunctions;
100  int m_maxGenomeSize;
101  int m_firstLevelSubdiv;
102  bool m_useWeights;
103 };
104 
105 } // end namespace
106 
107 #endif // GRALE_TREELENSINVERSIONGAFACTORYBASE_H
108