GRALE
gridlensinversiongafactorynew.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_GRIDLENSINVERSIONGAFACTORYNEW_H
27 
28 #define GRALE_GRIDLENSINVERSIONGAFACTORYNEW_H
29 
30 #include "graleconfig.h"
31 #include "randomnumbergenerator.h"
32 #include "backprojectmatrixnew.h"
33 #include "vector2d.h"
34 #include "grid.h"
35 #include <mogal/gafactory.h>
36 #include <vector>
37 
38 namespace grale
39 {
40 
41 class GridLensInversionGAFactoryParamsNew;
42 class GridLensInversionGenomeNew;
43 class ImagesDataExtended;
44 class GravitationalLens;
45 class LensFitnessObject;
46 
47 // NOTE: the virtual inheritance is again very important!
48 class GRALE_IMPORTEXPORT GridLensInversionGAFactoryNew : public virtual mogal::GAFactory
49 {
50 public:
51  GridLensInversionGAFactoryNew();
52  ~GridLensInversionGAFactoryNew();
53 
54  mogal::GAFactoryParams *createParamsInstance() const;
55  const mogal::GAFactoryParams *getCurrentParameters() const;
56 
57  bool init(const mogal::GAFactoryParams *pParams);
58 
59  mogal::Genome *createNewGenome() const;
60 
61  size_t getMaximalFitnessSize() const { return sizeof(float)*(1+getNumberOfFitnessComponents()); } // one for scale factor, some for fitness
62  size_t getMaximalGenomeSize() const { return (m_numMasses+1)*sizeof(float); } // one extra for the mass sheet value
63 
64  bool writeGenome(serut::SerializationInterface &si, const mogal::Genome *pGenome) const;
65  bool readGenome(serut::SerializationInterface &si, mogal::Genome **pGenome) const;
66  bool writeGenomeFitness(serut::SerializationInterface &si, const mogal::Genome *pGenome) const;
67  bool readGenomeFitness(serut::SerializationInterface &si, mogal::Genome *pGenome) const;
68  bool writeCommonGenerationInfo(serut::SerializationInterface &si) const;
69  bool readCommonGenerationInfo(serut::SerializationInterface &si);
70 
71  bool hasFloatingPointFitnessValues() const { return true; }
72 
73  const RandomNumberGenerator *getRandomNumberGenerator() const { return &m_rndGen; }
74 
75  GravitationalLens *createLens(const std::vector<float> &masses, float sheetValue, float scaleFactor, double *pTotalMass, std::string &errStr) const;
76 
77  DeflectionMatrix *getDeflectionMatrix() { return m_pDeflectionMatrix; }
78  BackProjectMatrixNew *getShortBackProjectMatrix() { return m_pShortBPMatrix; }
79  BackProjectMatrixNew *getTotalBackProjectMatrix() { return m_pTotalBPMatrix; }
80  LensFitnessObject *getFitnessObject() { return m_pFitnessObject; }
81 
82  Vector2D<float> getBottomLeft() const { return m_bottomLeftFloat; }
83  Vector2D<float> getTopRight() const { return m_topRightFloat; }
84  const std::vector<Vector2D<float> > &getBasisCenters() const { return m_basisCenters; }
85  float getMinimalMutationWidth() const { return m_minMutationWidth; }
86  float getMaximalMutationWidth() const { return m_maxMutationWidth; }
87 
88  float getSheetScale() const { return m_sheetScale; }
89 
90  virtual bool useAbsoluteMutation() = 0;
91  virtual float getMutationAmplitude() = 0;
92 protected:
93  int getMaximumNumberOfGenerations() const { return m_maxGenerations; }
94 
95  virtual LensFitnessObject *createFitnessObject() = 0;
96  virtual bool subInit(LensFitnessObject *pFitnessObject) = 0;
97  void onSortedPopulation(const std::vector<mogal::GenomeWrapper> &population);
98 private:
99  bool localSubInit(double z_d, const std::list<ImagesDataExtended *> &images,
100  const std::vector<std::pair<GravitationalLens *, Vector2D<double> > > &basisLenses,
101  const GravitationalLens *pBaseLens, bool useSheet);
102  double getAngularScale() const { return m_pShortBPMatrix->getAngularScale(); }
103  bool createBasisLenses(double D_d, const std::list<GridSquare> &squares,
104  const std::vector<double> &massWeights, double massScale,
105  std::vector<std::pair<GravitationalLens *, Vector2D<double> > > &basisLenses);
106  bool recalculateMassWeights(const std::vector<std::pair<GravitationalLens *, Vector2D<double> > > &basisLenses,
107  std::vector<double> &massWeights, const std::list<GridSquare> &squares,
108  double massScale);
109  void calculateMassRegion(const std::list<GridSquare> &squares, double &minSize);
110 
111  RandomNumberGenerator m_rndGen;
112  GridLensInversionGAFactoryParamsNew *m_pCurrentParams;
113  int m_numMasses, m_maxGenerations;
114  bool m_showEvolution, m_saveFirstGenome;
115  bool m_allowNegativeValues;
116  bool m_useGenomeSheet;
117  float m_sheetScale;
118 
119  Vector2D<float> m_bottomLeftFloat;
120  Vector2D<float> m_topRightFloat;
121  Vector2D<double> m_bottomLeft;
122  Vector2D<double> m_topRight;
123  std::vector<Vector2D<float> > m_basisCenters;
124  float m_minMutationWidth;
125  float m_maxMutationWidth;
126 
127  std::vector<std::pair<GravitationalLens *, Vector2D<double> > > m_basisLenses;
128 
129  LensFitnessObject *m_pFitnessObject;
130  DeflectionMatrix *m_pDeflectionMatrix;
131  BackProjectMatrixNew *m_pShortBPMatrix, *m_pTotalBPMatrix;
132 };
133 
134 } // end namespace
135 
136 #endif // GRALE_GRIDLENSINVERSIONGAFACTORYNEW_H
137