openModeller  Version 1.5.0
distance_to_average.cpp
Go to the documentation of this file.
1 
28 #include "distance_to_average.hh"
29 
33 
34 #include <stdio.h>
35 #include <math.h>
36 
37 /****************************************************************/
38 /********************** Algorithm's Metadata ********************/
39 
40 #define NUM_PARAM 1
41 #define PARAM_MAXDIST "MaxDist"
42 
43 
44 /*************************************/
45 /*** Algorithm parameters metadata ***/
46 
48 
49  // Metadata of the first parameter.
50  {
51  PARAM_MAXDIST, // Id
52  "Maximum distance", // Name
53  Real, // Type.
54  "Maximum cartesian distance to be considered", // Overview
55  "Maximum cartesian distance to be considered", // Description
56 
57  1, // Not zero if the parameter has lower limit.
58  0.0, // Parameter's lower limit.
59  1, // Not zero if the parameter has upper limit.
60  1.0, // Parameter's upper limit.
61  "0.1" // Parameter's typical (default) value.
62  },
63 };
64 
65 
66 /************************************/
67 /*** Algorithm's general metadata ***/
68 
70 
71  "DistanceToAverage", // Id.
72  "Distance to average", // Name.
73  "0.1", // Version.
74 
75  // Overview
76  "Probability is inversely proportional to the cartesian distance\
77  in environmental space to the mean point (calculated with all\
78  presence points).",
79 
80  // Description.
81  "Normalizes the environmental variables values and the\
82  parameter (according to the number of environmental variables).\
83  Calculates the mean point in environmental space\
84  considering all given presence points. When projecting the\
85  result, it calculates the Euclidean distance between the\
86  average point and each point in the environmental space.\
87  If the distance 'dist' is in [0, MAXDIST] then the probability\
88  of occurrence will be in [1,0] (linear decay).\
89  If 'dist' > MAXDIST then the probability will be zero.",
90 
91  "Mauro E. S. Munoz", // Algorithm author
92  "", // Bibliography.
93 
94  "Mauro E. S. Munoz", // Code author.
95  "mauro [at] cria.org.br", // Code author contact.
96 
97  0, // Does not accept categorical data.
98  0, // Does not needs absence points to run.
99 
100  NUM_PARAM, // Algorithm's parameters.
101  parameters
102 };
103 
104 
105 
106 /****************************************************************/
107 /****************** Algorithm's factory function ****************/
108 
109 OM_ALG_DLL_EXPORT
112 {
113  return new DistanceToAverage();
114 }
115 
116 OM_ALG_DLL_EXPORT
117 AlgMetadata const *
119 {
120  return &metadata;
121 }
122 
123 /****************************************************************/
124 /************************ Distance To Average *******************/
125 
126 /*******************/
127 /*** constructor ***/
128 
130  AlgorithmImpl( &metadata ),
131  _done( false ),
132  _dist( 0.0 ),
133  _min( 0.0 ),
134  _max( 0.0 ),
135  _avg()
136 {
137  _normalizerPtr = new ScaleNormalizer( 0.0, 1.0, true );
138 }
139 
140 
141 /******************/
142 /*** destructor ***/
143 
145 {
146  if ( _done ) {
147 
148  Log::instance()->info( "\nMinimum distance found: %f\n", _min );
149  Log::instance()->info( "\nMaximum distance found: %f\n\n", _max );
150  }
151 }
152 
153 
154 /******************/
155 /*** initialize ***/
156 int
158 {
159  if ( ! getParameter( PARAM_MAXDIST, &_dist ) )
160  return 0;
161 
162  Log::instance()->info( "Parameter %s: %f\n", PARAM_MAXDIST, _dist );
163 
164  // Distance should range from 0 to 1
165  if ( _dist > 1.0 ) _dist = 1.0;
166  else if ( _dist < 0.0 ) _dist = 0.0;
167 
168  int dim = _samp->numIndependent();
169 
170  // Normalize the distance parameter according to the number
171  // of layers.
172  _dist *= sqrt( (double) dim );
173 
174  Log::instance()->info( "\nEnvironmental layers: %d\n", dim );
175  Log::instance()->info( "Parameter normalized: %f\n\n", _dist );
176 
177  //
178  // Generate model from the average of given points.
179  //
180 
181  Log::instance()->info( "Reading %d-dimensional occurrence points.\n", dim );
182 
183  int npnt = _samp->numPresence();
184  if ( npnt == 0 ) {
185  Log::instance()->info( "All occurrences are outside the mask!\n" );
186  return 0;
187  }
188 
189  // Read all presence occurence points.
190  OccurrencesPtr presences = _samp->getPresences();
191  OccurrencesImpl::const_iterator pres = presences->begin();
192  OccurrencesImpl::const_iterator fin = presences->end();
193 
194  Log::instance()->info( "Finding average from %d occurrences.\n", npnt );
195 
196  // Redimension _avg.
197  _avg.resize( dim );
198 
199  while ( pres != fin ) {
200  _avg += (*pres)->environment();
201  ++pres;
202  }
203 
204  _avg /= npnt;
205 
206  Log::instance()->info( "Average related to occurrences: " );
207  for ( int d = 0; d < dim; d++ )
208  Log::instance()->info( "%f ", _avg[d] );
209  Log::instance()->info( "\n\n" );
210 
211  _done = true;
212 
213  return 1;
214 }
215 
216 
217 /***************/
218 /*** iterate ***/
219 int
221 {
222  return 1;
223 }
224 
225 
226 /************/
227 /*** done ***/
228 int
230 {
231  return _done;
232 }
233 
234 
235 /*****************/
236 /*** get Value ***/
237 Scalar
239 {
240  static int first_time = 1;
241 
242  // Calculate distance between *x and _avg.
243 
244  Sample dif = x;
245  dif -= _avg;
246  Scalar dist = dif.norm();
247 
248  // Minimum and maximum distances found. Only for log!
249  if ( first_time )
250  {
251  _min = _max = dist;
252  first_time = 0;
253  }
254  else
255  {
256  if ( dist < _min ) _min = dist;
257  if ( dist > _max ) _max = dist;
258  }
259 
260  return (dist <= _dist) ? 1.0 - (dist / _dist) : 0.0;
261 }
262 
263 
264 /***********************/
265 /*** get Convergence ***/
266 int
268 {
269  *val = 1.0;
270  return 1;
271 }
272 
273 /****************************************************************/
274 /****************** configuration *******************************/
275 void
277 {
278  if (!_done )
279  return;
280 
281  ConfigurationPtr model_config( new ConfigurationImpl("DistanceToAverage") );
282  config->addSubsection( model_config );
283 
284  model_config->addNameValue( "Distance", _dist );
285  model_config->addNameValue( "Average", _avg );
286 }
287 
288 void
290 {
291  ConstConfigurationPtr model_config = config->getSubsection( "DistanceToAverage",false );
292 
293  if (!model_config)
294  return;
295 
296  _done = true;
297  _dist = model_config->getAttributeAsDouble( "Distance", 0.0 );
298  _avg = model_config->getAttributeAsSample( "Average" );
299 
300 }
double Scalar
Type of map values.
Definition: om_defs.hh:39
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
Sample _avg
Average related to occurrence points.
Scalar _max
Store the maximum distance (for debug).
int getParameter(std::string const &name, std::string *value)
void resize(std::size_t size)
Definition: Sample.cpp:153
virtual void _getConfiguration(ConfigurationPtr &) const
#define NUM_PARAM
Scalar getValue(const Sample &x) const
int getConvergence(Scalar *val)
Scalar norm() const
Definition: Sample.cpp:457
static AlgMetadata metadata
SamplerPtr _samp
Definition: Algorithm.hh:245
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
Scalar _min
Store the minimum distance (for debug).
OM_ALG_DLL_EXPORT AlgorithmImpl * algorithmFactory()
#define PARAM_MAXDIST
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
Normalizer * _normalizerPtr
Definition: Algorithm.hh:247
static AlgParamMetadata parameters[NUM_PARAM]
virtual void _setConfiguration(const ConstConfigurationPtr &)
OM_ALG_DLL_EXPORT AlgMetadata const * algorithmMetadata()
Definition: Sample.hh:25