GRALE
gridlensinversiongafactorybase.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_GRIDLENSINVERSIONGAFACTORYBASE_H
27 
28 #define GRALE_GRIDLENSINVERSIONGAFACTORYBASE_H
29 
30 #include "graleconfig.h"
31 #include "randomnumbergenerator.h"
32 #include "backprojectmatrixnew.h"
33 #include "vector2d.h"
34 #include <mogal/gafactorydefaults.h>
35 #include <vector>
36 
37 namespace grale
38 {
39 
40 class GridLensInversionGAFactoryParams;
41 class GridLensInversionGenomeBase;
42 class ImagesDataExtended;
43 class GravitationalLens;
44 class LensFitnessObject;
45 
46 // NOTE: the virtual inheritance is again very important!
47 class GRALE_IMPORTEXPORT GridLensInversionGAFactoryBase : public virtual mogal::GAFactory
48 {
49 public:
50  GridLensInversionGAFactoryBase();
51  ~GridLensInversionGAFactoryBase();
52 
53  mogal::GAFactoryParams *createParamsInstance() const;
54  const mogal::GAFactoryParams *getCurrentParameters() const;
55 
56  bool init(const mogal::GAFactoryParams *p);
57 
58  mogal::Genome *createNewGenome() const;
59 
60  size_t getMaximalFitnessSize() const { return sizeof(float)*(2+getNumberOfFitnessComponents()); } // one for scale factor, one for sheet scale factor some for fitness
61 #ifdef AUTODISTFRAC
62  size_t getMaximalGenomeSize() const { return (m_numMasses+1+m_numAutoDistFrac)*sizeof(float); } // one extra for the mass sheet value
63 #else
64  size_t getMaximalGenomeSize() const { return (m_numMasses+1)*sizeof(float); } // one extra for the mass sheet value
65 #endif // AUTODISTFRAC
66 
67  bool writeGenome(serut::SerializationInterface &si, const mogal::Genome *g) const;
68  bool readGenome(serut::SerializationInterface &si, mogal::Genome **g) const;
69  bool writeGenomeFitness(serut::SerializationInterface &si, const mogal::Genome *g) const;
70  bool readGenomeFitness(serut::SerializationInterface &si, mogal::Genome *g) const;
71  bool writeCommonGenerationInfo(serut::SerializationInterface &si) const;
72  bool readCommonGenerationInfo(serut::SerializationInterface &si);
73 
74  bool hasFloatingPointFitnessValues() const { return true; }
75 
76  const mogal::RandomNumberGenerator *getRandomNumberGenerator() const { return &m_rndGen; }
77  const std::vector<Vector2D<float> > &getPlummerPositions() const { return m_plummerPositions; }
78  const std::vector<float> &getSquareSizes() const { return m_squareSizes; }
79  float getGridSize() const { return m_gridSize; }
80  Vector2D<float> getGridCenter() const { return m_gridCenter; }
81  bool allowNegativeValues() const { return m_allowNegativeValues; }
82  const float *getMassWeights() const { return &(m_massWeights[0]); }
83  float getSheetScale() const { return m_sheetScale; }
84  bool useLoopSheet() const { return m_useLoopSheet; }
85 
86  virtual float getChanceMultiplier() = 0;
87  virtual bool useAbsoluteMutation() = 0;
88  virtual float getMutationAmplitude() = 0;
89 
90  GravitationalLens *createLens(const std::vector<float> &masses, float sheetValue, float scaleFactor, double *pTotalMass, std::string &errStr) const;
91 
92  DeflectionMatrix *getDeflectionMatrix() { return m_pDeflectionMatrix; }
93  BackProjectMatrixNew *getShortBackProjectMatrix() { return m_pShortBPMatrix; }
94  BackProjectMatrixNew *getTotalBackProjectMatrix() { return m_pTotalBPMatrix; }
95  LensFitnessObject *getFitnessObject() { return m_pFitnessObject; }
96 #ifdef AUTODISTFRAC
97  const std::vector<float> &getDistanceFractions() const { return m_distanceFractions; }
98  int getImagesGroupSize() const { return m_imagesGroupSize; }
99  int getNumberOfAutoDistanceFractions() const { return m_numAutoDistFrac; }
100 #endif // AUTODISTFRAC
101 protected:
102  int getMaximumNumberOfGenerations() const { return m_maxGenerations; }
103 
104  virtual LensFitnessObject *createFitnessObject() = 0;
105 
106  // This should at least set the number of fitness components
107  virtual bool subInit(LensFitnessObject *pFitnessObject) = 0;
108 #ifdef SHOWEVOLUTION
109  void onSortedPopulation(const std::vector<mogal::GenomeWrapper> &population);
110 #endif // SHOWEVOLUTION
111 private:
112  bool localSubInit(double z_d, const std::list<ImagesDataExtended *> &images,
113  const std::vector<std::pair<GravitationalLens *, Vector2D<double> > > &basisLenses,
114  const GravitationalLens *pBaseLens, bool useSheet);
115  double getAngularScale() const { return m_pShortBPMatrix->getAngularScale(); }
116 
117  RandomNumberGenerator m_rndGen;
118  GridLensInversionGAFactoryParams *m_pCurrentParams;
119  int m_numMasses, m_maxGenerations;
120  bool m_allowNegativeValues;
121  bool m_useGenomeSheet;
122  bool m_useLoopSheet;
123  float m_sheetScale;
124 
125  std::vector<Vector2D<float> > m_plummerPositions;
126  std::vector<float> m_squareSizes;
127  std::vector<float> m_massWeights;
128  float m_gridSize;
129  Vector2D<float> m_gridCenter;
130 
131  std::vector<std::pair<GravitationalLens *, Vector2D<double> > > m_basisLenses;
132 
133  LensFitnessObject *m_pFitnessObject;
134  DeflectionMatrix *m_pDeflectionMatrix;
135  BackProjectMatrixNew *m_pShortBPMatrix, *m_pTotalBPMatrix;
136 
137 #ifdef AUTODISTFRAC
138  std::vector<float> m_distanceFractions;
139  int m_imagesGroupSize;
140  int m_numAutoDistFrac;
141 #endif // AUTODISTFRAC
142 };
143 
144 } // end namespace
145 
146 #endif // GRALE_GRIDLENSINVERSIONGAFACTORYBASE_H
147