GRALE
treelensinversiongenomebase.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_TREELENSINVERSIONGENOMEBASE_H
27 
28 #define GRALE_TREELENSINVERSIONGENOMEBASE_H
29 
30 #include "graleconfig.h"
31 #include "lensinversiongenomebase.h"
32 #include "vector2d.h"
33 #include <vector>
34 #include <string>
35 
36 #define TREELENSINVERSIONGENOMEBASE_MAXFITNESSCOMP 16
37 
38 namespace grale
39 {
40 
41 class BackProjectTreeBase;
42 class TreeLensInversionGAFactoryBase;
43 class GravitationalLens;
44 class GridNodeBase;
45 class GridTreeNode;
46 class MultiplePlummerLensParams;
47 
48 class GRALE_IMPORTEXPORT TreeLensInversionGenomeBase : public LensInversionGenomeBase
49 {
50 protected:
51  TreeLensInversionGenomeBase(TreeLensInversionGAFactoryBase *pFactory, BackProjectTreeBase *m, int firstLevelSubdiv);
52  TreeLensInversionGenomeBase(TreeLensInversionGAFactoryBase *pFactory, BackProjectTreeBase *m, GridNodeBase *pRootNode);
53 public:
54  ~TreeLensInversionGenomeBase();
55 
56  void setActiveFitnessComponent(int i) { m_activeFitnessComponent = i; }
57  bool calculateFitness();
58  bool isFitterThan(const mogal::Genome *g) const { const TreeLensInversionGenomeBase *g2 = (const TreeLensInversionGenomeBase *)g; if (m_fitnessValues[m_activeFitnessComponent] < g2->getFitnessValues()[m_activeFitnessComponent]) return true; return false; }
59  mogal::Genome *reproduce(const mogal::Genome *g) const;
60  mogal::Genome *clone() const;
61  void mutate();
62  std::string getFitnessDescription() const;
63 
64  const float *getFitnessValues() const { return m_fitnessValues; }
65  float getScaleFactor() const { return m_scaleFactor; }
66  void setFitnessValues(const float *pValues) { for (int i = 0 ; i < TREELENSINVERSIONGENOMEBASE_MAXFITNESSCOMP ; i++) m_fitnessValues[i] = pValues[i]; }
67  void setScaleFactor(float s) { m_scaleFactor = s; }
68 
69  const GridNodeBase *getRootNode() const { return m_pRootNode; }
70 
71  GravitationalLens *createLens(double *totalmass, std::string &errstr) const;
72 protected:
73  virtual TreeLensInversionGenomeBase *allocateNewInstance(TreeLensInversionGAFactoryBase *f, BackProjectTreeBase *m, GridNodeBase *pRootNode) const = 0;
74  virtual void initializeNewCalculation() = 0;
75  virtual float calculateMassScaleFitness(float scalefactor) = 0;
76  virtual void calculateTotalFitness(float scalefactor, float *fitnessvalues) = 0;
77  virtual void getCalculationParameters(float &startfactor, float &stopfactor, int &numiterationsteps, int &numiterations) const
78  {
79  startfactor = 0.2;
80  stopfactor = 5.0;
81  numiterationsteps = 20;
82  numiterations = 5;
83  }
84 
85  TreeLensInversionGAFactoryBase *getFactory() const { return m_pFactory; }
86  BackProjectTreeBase *getBackProjectTree() { return m_pBackProjectTree; }
87 private:
88  void rescale();
89  void addBasisFunction(const GridNodeBase *pNode, int level, MultiplePlummerLensParams *pLensParams, Vector2D<double> center, double areaSize, double widthScaleFactor, double massScale) const;
90  static float findMaxWeight(const GridNodeBase *pNode);
91  static float sumWeights(const GridNodeBase *pNode, int level, bool useWeights);
92  static void scaleWeights(GridNodeBase *pNode, float scaleFactor);
93  GridNodeBase *reproduce(const GridNodeBase *pNode1, const GridNodeBase *pNode2, float scaleFactor1, float scaleFactor2) const;
94  void findAllWeightNodes(GridNodeBase *pNode, std::vector<GridNodeBase *> &allNodes);
95  static int countAllWeightNodes(GridNodeBase *pNode, std::vector<GridTreeNode *> &parentNodes);
96  GridNodeBase *prune(GridNodeBase *pNode, int maxBasisFunctions) const;
97  static void scaleAllWeightNodes(GridNodeBase *pNode, float factor);
98 
99  int m_activeFitnessComponent;
100  float m_fitnessValues[TREELENSINVERSIONGENOMEBASE_MAXFITNESSCOMP];
101  float m_scaleFactor;
102  GridNodeBase *m_pRootNode;
103  bool m_useWeights;
104 
105  TreeLensInversionGAFactoryBase *m_pFactory;
106  BackProjectTreeBase *m_pBackProjectTree;
107 };
108 
109 } // end namespace
110 
111 #endif // GRALE_TREELENSINVERSIONGENOMEBASE_H
112