openModeller  Version 1.5.0
garp_run.cpp
Go to the documentation of this file.
1 
30 #include <openmodeller/om.hh>
31 
32 #include "garp_run.hh"
33 #include "garp_run_thread.hh"
34 #include "bs_algorithm_factory.hh"
36 
37 /****************************************************************/
38 /************************ GARP Run Thread ***********************/
39 
40 // TODO: modify strategy to reuse threads instead of creating a
41 // new one for every garp run
42 
44 {
45  AlgorithmRun * algRun = (AlgorithmRun *) threadData;
46 
47  //Log::instance()->debug("Starting new thread (%d).\n", algRun->getId());
48 
49  while (!algRun->done())
50  algRun->iterate();
51 
52  algRun->calculateCommission();
53  algRun->calculateOmission();
54  algRun->finalize();
55 
56  //Log::instance()->debug("Finishing thread (%d).\n", algRun->getId());
57 
59 }
60 
61 
62 /****************************************************************/
63 /************************* GARP Run *****************************/
64 
66  _id(-1),
67  _running( false ),
68  _omission( -1.0 ),
69  _commission( -1.0 ),
70  _commission_samples( 0 ),
71  _alg( NULL ),
72  _train_sampler(),
73  _test_sampler()
74 {
75  //Log::instance()->info("Creating an AlgorithmRun at: %x\n",this);
76 }
77 
79  _id(-1),
80  _running( false ),
81  _omission( -1.0 ),
82  _commission( -1.0 ),
83  _commission_samples( 0 ),
84  _alg( alg ),
85  _train_sampler(),
86  _test_sampler()
87 {
88  //Log::instance()->info("Creating an AlgorithmRun at: %x\n",this);
89 }
90 
91 /****************************************************************/
93 {
94  //Log::instance()->info("Deleting an AlgorithmRun at: %x\n",this);
95 }
96 
97 /****************************************************************/
98 int AlgorithmRun::initialize(int id, int comm_samples,
99  const SamplerPtr& train_sampler,
100  const SamplerPtr& test_sampler,
101  int nparam, AlgParameter * param,
102  BSAlgorithmFactory * factory)
103 {
104  Log::instance()->debug( "Initializing garp run (%d)\n", id );
105 
106  _id = id;
107  _commission_samples = comm_samples;
108 
109  _train_sampler = train_sampler;
110  _test_sampler = test_sampler;
111 
112  _alg = factory->getBSAlgorithm();
113  _alg->setSampler(train_sampler);
114  _alg->setParameters(nparam, param);
115  _alg->initialize();
116 
117  return 1;
118 }
119 
120 /****************************************************************/
121 int AlgorithmRun::run()
122 {
123  //Log::instance()->debug("Starting new garp run (%d).\n", _id);
124  _running = true;
126  return 1;
127 }
128 
129 /****************************************************************/
130 bool AlgorithmRun::running() const
131 { return _running; }
132 
133 /****************************************************************/
135 {
136  //Log::instance()->debug("Iteration %6d on run %d.\n", _alg->getGeneration(), _id);
137  return _alg->iterate();
138 }
139 
140 /****************************************************************/
142 { return _alg->done(); }
143 
144 /****************************************************************/
145 float AlgorithmRun::getProgress() const
146 { return _alg->getProgress(); }
147 
148 /****************************************************************/
150 {
151  //Log::instance()->debug("Finishing up garp run.(%d)\n", _id);
152  _running = false;
153  //_alg->deleteTempDataMembers(); // this is not in Algorithm interface
154  THREAD_END();
155  return 1;
156 }
157 
158 /****************************************************************/
160 {
161  int i;
162  double sum = 0.0;
163 
164  // TODO: check how to use absences in computing commission
165 
166  //Log::instance()->debug("Calculating commission error (%d).\n", _id);
167 
168  // get random points from the background to estimate
169  // area predicted present
170  bool hasAbsences = (_train_sampler->numAbsence() != 0);
171  for (i = 0; i < _commission_samples; i++) {
172 
173  Scalar value;
174  if (hasAbsences) {
175 
176  ConstOccurrencePtr occ = _train_sampler->getAbsence();
177  value = _alg->getValue(occ->environment());
178  }
179  else {
180 
181  OccurrencePtr occ = _train_sampler->getPseudoAbsence();
182  value = _alg->getValue(occ->environment());
183  }
184 
185  // discard novalue (-1); zero is irrelevant to the sum
186  if (value > 0)
187  sum += value;
188  }
189 
190  _commission = sum / (double) _commission_samples;
191 
192  return 1;
193 }
194 
195 /****************************************************************/
197 {
198  // TODO: check how to use absences in computing omission
199 
200  //Log::instance()->debug("Calculating omission error (%d).\n", _id);
201 
202  // test which kind of test (intrinsic or extrinsic) should be performed
203  SamplerPtr sampler;
204 
205  if (!_test_sampler)
206  { sampler = _train_sampler; }
207  else
208  { sampler = _test_sampler; }
209 
210  int nomitted = 0;
211  OccurrencesPtr presences = sampler->getPresences();
212  OccurrencesImpl::const_iterator it = presences->begin();
213  OccurrencesImpl::const_iterator end = presences->end();
214 
215  while (it != end)
216  {
217  nomitted = !_alg->getValue((*it)->environment());
218  ++it;
219  }
220 
221  _omission = (double) nomitted / (double) presences->numOccurrences();
222 
223  return 1;
224 }
225 
226 /****************************************************************/
227 double AlgorithmRun::getOmission() const
228 { return _omission; }
229 
230 /****************************************************************/
231 double AlgorithmRun::getCommission() const
232 { return _commission; }
233 
234 /****************************************************************/
235 double AlgorithmRun::getError(int type) const
236 {
237  if (!type)
238  { return _omission; }
239  else
240  { return _commission; }
241 }
242 
243 /****************************************************************/
244 double AlgorithmRun::getValue(const Sample& x) const
245 { return _alg->getValue(x); }
246 
int iterate()
Definition: garp_run.cpp:134
virtual AlgorithmImpl * getBSAlgorithm()=0
int _commission_samples
Commission error, approximated by area predicted present.
Definition: AlgorithmRun.hh:83
SamplerPtr _train_sampler
Number of points used to calculate commission.
Definition: AlgorithmRun.hh:84
#define THREAD_PROC_RETURN_TYPE
Definition: threads.hh:56
double Scalar
Type of map values.
Definition: om_defs.hh:39
double _omission
Indicates whether the thread is running.
Definition: AlgorithmRun.hh:81
float getProgress() const
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
AlgorithmPtr _alg
Number of points used to calculate commission.
Definition: AlgorithmRun.hh:77
int calculateCommission()
bool _running
Identified for this particular garp run.
Definition: AlgorithmRun.hh:80
THREAD_PROC_RETURN_TYPE AlgorithmRunThreadProc(void *threadData)
Definition: garp_run.cpp:43
double getOmission() const
double getError(int type) const
#define THREAD_PROC_RETURN_STATEMENT
Definition: threads.hh:57
double _commission
Omission error for this run.
Definition: AlgorithmRun.hh:82
#define THREAD_START(threadProc, threadData)
Definition: threads.hh:62
SamplerPtr _test_sampler
Definition: AlgorithmRun.hh:85
double getCommission() const
int done() const
Definition: garp_run.cpp:141
#define THREAD_END()
Definition: threads.hh:66
Scalar getValue(const Sample &x) const
int calculateOmission()
int finalize()
Definition: garp_run.cpp:149
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
bool running() const
int _id
Algorithm used in this run.
Definition: AlgorithmRun.hh:79
Definition: Sample.hh:25
int initialize(int id, int comm_samples, const SamplerPtr &train_sampler, const SamplerPtr &test_sampler)