GRALE
gridlensinversiongenomenew.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_GRIDLENSINVERSIONGENOMENEW_H
27 
28 #define GRALE_GRIDLENSINVERSIONGENOMENEW_H
29 
30 #include "graleconfig.h"
31 #include "lensinversiongenomebase.h"
32 #include "realtype.h"
33 #include <string.h>
34 #include <string>
35 #include <vector>
36 
37 #define GRIDLENSINVERSIONGENOMENEW_MAXFITNESSCOMP 16
38 
39 namespace grale
40 {
41 
42 class GridLensInversionGAFactoryNew;
43 class GravitationalLens;
44 
45 class GRALE_IMPORTEXPORT GridLensInversionGenomeNew : public LensInversionGenomeBase
46 {
47 public:
48  GridLensInversionGenomeNew(GridLensInversionGAFactoryNew *pFactory, int numMasses, bool useSheet);
49  GridLensInversionGenomeNew(GridLensInversionGAFactoryNew *pFactory, const std::vector<float> &masses, float sheetValue);
50  ~GridLensInversionGenomeNew();
51 
52  void setActiveFitnessComponent(int i) { m_activeFitnessComponent = i; }
53  bool calculateFitness();
54  bool isFitterThan(const mogal::Genome *pGenome) const;
55  mogal::Genome *reproduce(const mogal::Genome *pGenome) const;
56  Genome *clone() const;
57  void mutate();
58  std::string getFitnessDescription() const;
59 
60  const float *getFitnessValues() const { return m_fitnessValues; }
61  float getScaleFactor() const { return m_scaleFactor; }
62  void setFitnessValues(const float *pValues);
63  void setScaleFactor(float s) { m_scaleFactor = s; }
64 
65  const std::vector<float> &getMasses() const { return m_masses; }
66  float getSheetValue() const { return m_sheetValue; }
67 
68  GravitationalLens *createLens(double *totalmass, std::string &errstr) const;
69 
70  void copyFitnessValuesTo(float *pDestination, int amount) const { memcpy(pDestination, m_fitnessValues, sizeof(float)*amount); }
71 protected:
72  GridLensInversionGAFactoryNew *getFactory() const { return m_pFactory; }
73 private:
74  void initGenome();
75 
76  void initializeNewCalculation();
77  float calculateMassScaleFitness(float scalefactor, float sheetScale);
78  void calculateTotalFitness(float scalefactor, float sheetScale, float *fitnessvalues);
79 
80  int m_activeFitnessComponent;
81  float m_fitnessValues[GRIDLENSINVERSIONGENOMENEW_MAXFITNESSCOMP];
82  float m_scaleFactor;
83  std::vector<float> m_masses;
84  float m_sheetValue;
85 
86  GridLensInversionGAFactoryNew *m_pFactory;
87 };
88 
89 inline bool GridLensInversionGenomeNew::isFitterThan(const mogal::Genome *pGenome) const
90 {
91  const GridLensInversionGenomeNew *pGenome2 = (const GridLensInversionGenomeNew *)pGenome;
92 
93  if (m_fitnessValues[m_activeFitnessComponent] < pGenome2->m_fitnessValues[m_activeFitnessComponent])
94  return true;
95  return false;
96 }
97 
98 inline void GridLensInversionGenomeNew::setFitnessValues(const float *pValues)
99 {
100  memcpy(m_fitnessValues, pValues, sizeof(float)*GRIDLENSINVERSIONGENOMENEW_MAXFITNESSCOMP);
101 }
102 
103 } // end namespace
104 
105 #endif // GRALE_GRIDLENSINVERSIONGENOMENEW_H
106