ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
populationmanager.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************************************
3 Copyright (c) 2011, Christopher John Topping, Aarhus University
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided
7 that the following conditions are met:
8 
9 Redistributions of source code must retain the above copyright notice, this list of conditions and the
10 following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12 the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
17 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 ********************************************************************************************************
23 */
37 //---------------------------------------------------------------------------
38 
39 #ifndef PopulationManagerH
40  #define PopulationManagerH
41 
42 // Forwards
43 class TAnimal;
44 class MyDialog;
45 class AlleleFreq;
46 
47 // Start defines
48 typedef char * AnsiString;
49 typedef vector < TAnimal * > TListOfAnimals;
50 // END defines
51 
52 
53 //------------------------------------------------------------------------------
57 typedef enum
58 {
72 #ifdef __dormouse // LADA
73  TOP_Dormouse,
74 #endif //__dormosue
77 //------------------------------------------------------------------------------
78 
83 struct IntArray100 {
84 public:
85  int n[ 100 ];
86 };
87 
88 //------------------------------------------------------------------------------
89 
94 struct rectangle {
95 public:
96  unsigned m_x1;
97  unsigned m_y1;
98  unsigned m_x2;
99  unsigned m_y2;
100 };
101 //------------------------------------------------------------------------------
102 
107 //------------------------------------------------------------------------------
108 
110 protected:
115 public:
118  return m_CurrentStateNo;
119  }
121  void SetCurrentStateNo(int a_num) {
122  m_CurrentStateNo = a_num;
123  }
125  bool GetStepDone() {
126  return m_StepDone;
127  }
129  void SetStepDone(bool a_bool) {
130  m_StepDone = a_bool;
131  }
133  virtual void BeginStep(void) {}
135  virtual void Step(void) {}
137  virtual void EndStep(void) {}
139  virtual void ReinitialiseObject() {
140  m_StepDone = false;
141  m_CurrentStateNo = 0;
142  }
144  TALMaSSObject();
146  virtual ~TALMaSSObject();
148  void OnArrayBoundsError();
149 #ifdef __CJTDebug_5
150  int AmAlive;
151  int IsAlive() {
152  return AmAlive;
153  }
154  void DEADCODEError();
155 #endif
156 };
157 
158 //------------------------------------------------------------------------------
159 
165 // used to communicate the position of an animal to inquiring objects
166 {
167 public:
168  unsigned m_x;
169  unsigned m_y;
172 };
173 //------------------------------------------------------------------------------
174 
183 {
184 public:
185  int m_Size;
186  int m_Age;
187  unsigned m_Range_x;
188  unsigned m_Range_y;
189  unsigned m_OldRange_x;
190  unsigned m_OldRange_y;
191 };
192 //------------------------------------------------------------------------------
193 
201 class TAnimal : public TALMaSSObject {
202 public:
203  unsigned SupplyFarmOwnerRef();
204  AnimalPosition SupplyPosition();
205  APoint SupplyPoint() { APoint p( m_Location_x, m_Location_y); return p; }
207  return m_OurLandscape->SupplyPolyRef( m_Location_x, m_Location_y );
208  }
210  return m_Location_x;
211  }
213  return m_Location_y;
214  }
215  virtual void KillThis()
216  {
217  m_CurrentStateNo = -1;
218  m_StepDone = true;
219  };
220  virtual void CopyMyself() {
221  };
222 protected:
223  int m_Location_x;
232  m_Location_x = (m_Location_x + m_OurLandscape->SupplySimAreaWidth()) % m_OurLandscape->SupplySimAreaWidth();
233  m_Location_y = (m_Location_y + m_OurLandscape->SupplySimAreaHeight()) % m_OurLandscape->SupplySimAreaHeight();
234  }
235 public:
236  void SetX( int a_x ) {
237  m_Location_x = a_x;
238  }
239  void SetY( int a_y ) {
240 
241  m_Location_y = a_y;
242 
243  }
244  TAnimal( int x, int y, Landscape * L );
245  // Note that below must be replaced in a descendent class e.g. Skylark_Base
246  virtual void BeginStep( void ) {
247  }
248  virtual void Step( void ) {
249  }
250  virtual void EndStep( void ) {
251  }
253  virtual void ReinitialiseObject(int x, int y, Landscape * L) {
254  m_OurLandscape = L;
255  m_Location_x = x;
256  m_Location_y = y;
258  }
259  virtual int WhatState() {
260  return 0;
261  }
262  virtual void Dying() {
263  KillThis();
264  }
265  void CheckManagement( void );
266  void CheckManagementXY( int x, int y );
267  virtual bool OnFarmEvent( FarmToDo /* event */ ) {
268  return false;
269  }
270 };
271 
272 //------------------------------------------------------------------------------
273 //------------------------------------------------------------------------------
274 
280 {
281 protected:
282  ofstream * m_MyFile;
283  int m_Time;
284  char m_MyFileName[ 255 ];
285 public:
287  unsigned m_ReportInterval; // from 1-10
288  unsigned m_NoAreas; // from 1-10
289  rectangle m_Rect[ 10 ]; // can have up to ten areas
290  unsigned m_NoEleTypes;
291  unsigned m_NoVegTypes;
292  unsigned m_NoFarms;
293  TTypesOfVegetation m_RefVeg[ 25 ]; // up to 25 reference types
294  TTypesOfLandscapeElement m_RefEle[ 25 ]; // up to 25 reference types
295  unsigned m_RefFarms[ 25 ]; // up to 25 reference types
296  // Species Specific Code below:
297  bool m_TargetTypes[ 10 ]; // eggs,nestlings,fledgelings,males,females etc.
298  void FileOutput( int No, int time, int ProbeNo );
299  void FileAppendOutput( int No, int time );
300  probe_data();
301  void SetFile( ofstream * F );
302  ofstream * OpenFile( char * Nme );
303 
305  m_MyFile = new ofstream(m_MyFileName, ios::app);
306  if (!(*m_MyFile).is_open())
307  {
308  g_msg->Warn( WARN_FILE, "PopulationManager::AppendToFile() Unable to open file for append: ", m_MyFileName );
309  exit(1);
310  }
311  return true;
312  }
313  void CloseFile();
314  ~probe_data();
315 };
316 //------------------------------------------------------------------------------
317 //------------------------------------------------------------------------------
318 
320 {
325 protected:
326  double m_K;
327  double m_n;
328  double m_Sum;
329  double m_SumX;
330  double m_SumX2;
331 public:
334  {
335  ClearData();
336  }
338  void add_variable(double x){
339  // This uses the computed shifted data equation.
340  if (m_n == 0) m_K = x;
341  m_n++;
342  m_Sum += x;
343  m_SumX += x - m_K;
344  m_SumX2 += (x - m_K) * (x - m_K);
345  }
347  void remove_variable(double x){
348  m_n--;
349  m_Sum -= x;
350  m_SumX -= (x - m_K);
351  m_SumX2 -= (x - m_K) * (x - m_K);
352  }
354  double get_N(){
355  return m_n;
356  }
358  double get_Total(){
359  return m_Sum;
360  }
362  double get_meanvalue(){
363  return m_K + m_SumX / m_n;
364  }
366  double get_varianceP(){
367  if (m_n < 2)
368  {
369  return -1; // Ilegal n value, but don't want to exit
370  }
371  return (m_SumX2 - (m_SumX*m_SumX) / m_n) / (m_n);
372  }
374  double get_varianceS(){
375  if (m_n < 2)
376  {
377  return -1; // Ilegal n value, but don't want to exit
378  }
379  return (m_SumX2 - (m_SumX*m_SumX) / m_n) / (m_n - 1);
380  }
382  void ClearData()
383  {
384  m_K = 0;
385  m_n = 0;
386  m_Sum = 0;
387  m_SumX = 0;
388  m_SumX2 = 0;
389  }
390 };
391 
392 //------------------------------------------------------------------------------
393 //------------------------------------------------------------------------------
402 public:
403  // Methods
405  virtual ~Population_Manager( void );
406  void SetNoProbes(int a_pn) { m_NoProbes=a_pn; }
408  unsigned GetLiveArraySize(int a_listindex) {
409  return m_LiveArraySize[a_listindex];
410  }
412  void IncLiveArraySize(int a_listindex) {
413  m_LiveArraySize[a_listindex]++;
414  }
415  virtual void Catastrophe(int /* a_mort */) {
416  }
417  unsigned int FarmAnimalCensus(unsigned int a_farm, unsigned int a_typeofanimal);
418  char* SpeciesSpecificReporting(int a_species, int a_time);
419  char* ProbeReport(int a_time);
420  char* ProbeReportTimed(int a_time);
421  void ImpactProbeReport( int a_Time );
422  bool BeginningOfMonth();
423  void LOG( const char* fname);
425  return m_StepSize;
426  }
427  int SupplySimW() {
428  return SimW;
429  }
430  int SupplySimH() {
431  return SimH;
432  }
433  virtual void Run( int NoTSteps );
434  virtual float Probe( int ListIndex, probe_data * p_TheProbe );
435  virtual void ImpactedProbe( );
437  return m_ListNameLength;
438  }
439  unsigned SupplyListIndexSize() {
440  return (unsigned)TheArray.size();
441  }
442  unsigned SupplyListSize( unsigned listindex ) {
443  return (unsigned) TheArray[ listindex ].size();
444  }
446  bool CheckXY(int l, int i);
447 
448 //---------------------------------------------------------------------------
449  const char* SupplyListName( int i ) {
450  return m_ListNames[ i ];
451  }
452  bool IsLast( unsigned listindex ) {
453  if ( TheArray[ listindex ].size() > 1 ) return false; else
454  return true;
455  }
461  int SupplyState( unsigned listindex, unsigned j ) {
462  return TheArray[ listindex ] [ j ]->WhatState();
463  }
464  virtual void SupplyLocXY( unsigned listindex, unsigned j, int & x, int & y ) {
465  x = TheArray[ listindex ] [ j ]->Supply_m_Location_x();
466  y = TheArray[ listindex ] [ j ]->Supply_m_Location_y();
467  }
468  const char* SupplyStateNames( int i ) {
469  return StateNames[ i ];
470  }
472  return StateNamesLength;
473  }
474  virtual void DisplayLocations();
475  int ProbeFileInput( char * p_Filename, int p_ProbeNo );
476  // Attributes
477 #ifdef __ALMASS_VISUAL
478  MyDialog * m_MainForm; // Need to use this when drawing to main form
479 #endif
480  int IndexArrayX[ 5 ] [ 10000 ];
481  probe_data * TheProbe[ 100 ];
482  int SimH, SimW;
483  unsigned SimHH, SimWH;
484  char m_SimulationName[ 255 ];
485  bool ProbesSet; // used to show when probes are needed to be set
487  TAnimal * FindClosest( int x, int y, unsigned Type );
488  // Output arrays
489  #ifndef __UNIX__
490  // unsigned Counts[ 10 ] [ 100 ];
491  #endif
492 protected:
493  // Attributes
494  // Holds the number of live animals repsented in each element of vector of vectors TheArray
495  vector<unsigned> m_LiveArraySize;
500  //FILE* m_FledgelingFile;
501  const char* StateNames[ 100 ];
504  vector < TListOfAnimals > TheArray; // Creates an empty array of arrays
506  /* IntArray100 StateList; */
507  const char* m_ListNames[ 10 ];
509  FILE * TestFile;
510  FILE * TestFile2;
511  unsigned BeforeStepActions[ 10 ];
514  // Methods
516  virtual bool StepFinished();
517  virtual void DoFirst();
518  virtual void DoBefore();
519  virtual void DoAfter();
520  virtual void DoAlmostLast();
521  virtual void DoLast();
523  void EmptyTheArray();
524  void SortX( unsigned Type );
525  void SortXIndex( unsigned Type );
526  void SortY( unsigned Type );
527  void SortState( unsigned Type );
528  void SortStateR(unsigned Type);
529  unsigned PartitionLiveDead(unsigned Type);
530  void Shuffle_or_Sort(unsigned Type);
531  void Shuffle( unsigned Type );
532  virtual void Catastrophe();
534  void NWordOutput();
535 // Grid related functions
536 public:
537  bool OpenTheRipleysOutputProbe();
538  bool OpenTheMonthlyRipleysOutputProbe();
539  bool OpenTheReallyBigProbe();
540  virtual void TheRipleysOutputProbe(FILE* a_prb);
541  virtual void TheReallyBigOutputProbe();
542  virtual void TheNWordOutputProbe() {};
543  void CloseTheMonthlyRipleysOutputProbe();
544  void CloseTheRipleysOutputProbe();
545  void CloseTheReallyBigOutputProbe();
546  bool OpenTheCIPEGridOutputProbe();
547  virtual void TheCIPEGridOutputProbe();
548  void CloseTheCIPEGridOutputProbe();
549  TTypesOfPopulation GetPopulationType() { return m_population_type; }
551  int GetSeasonNumber() {return m_SeasonNumber;}
552 protected:
572  int* m_gridcount[4];
574  int m_gridcountwidth[4];
576  int m_gridcountsize[4];
577 
578  long int lamdagrid[2][257][257]; // THIS ONLY WORKS UP TO 10x10 KM !!!!
579 public:
580  void LamdaDeath(int x, int y) {
581  // inlined for speed
582  lamdagrid[1][x / __lgridsize][y / __lgridsize ]++;
583  }
584  void LamdaBirth(int x, int y) {
585  lamdagrid[0][x / __lgridsize][y / __lgridsize ]++;
586  }
587  void LamdaBirth(int x, int y, int z) {
588  lamdagrid[0][x / __lgridsize][y / __lgridsize ]+=z;
589  }
590  void LamdaClear() {
591  for (int i=0; i<257; i++ ) {
592  for (int j=0; j<257; j++) {
593  lamdagrid[0][i][j]=0;
594  lamdagrid[1][i][j]=0;
595  }
596  }
597  }
598  void LamdaDumpOutput();
599  // end grid stuff
600 public: // Special ones for compatability to descended managers
601  virtual int SupplyPegPosx( int ) {
602  return 0;
603  }
604  virtual int SupplyPegPosy( int ) {
605  return 0;
606  }
607  virtual int SupplyCovPosx( int ) {
608  return 0;
609  }
610  virtual int SupplyCovPosy( int ) {
611  return 0;
612  }
613  virtual bool OpenTheFledgelingProbe() {
614  return false;
615  }
616  virtual bool OpenTheBreedingPairsProbe() {
617  return false;
618  }
620  return false;
621  }
622  virtual void BreedingPairsOutput( int ) {
623  }
624  virtual int TheBreedingFemalesProbe( int ) {
625  return 0;
626  }
627  virtual int TheFledgelingProbe() {
628  return 0;
629  }
630  virtual void BreedingSuccessProbeOutput( double, int, int, int, int, int, int , int ) {
631  }
632  virtual int TheBreedingSuccessProbe( int & , int & , int & , int & , int &, int & ) {
633  return 0;
634  }
635  virtual void FledgelingProbeOutput( int, int ) {
636  }
637  virtual void TheGeneticProbe( unsigned , int , unsigned & ) {
638  }
639  virtual void GeneticsResultsOutput( FILE * , unsigned ) {
640  }
641  // Attributes
642 // AlleleFreq * AlFreq;
643 };
644 //------------------------------------------------------------------------------
645 //------------------------------------------------------------------------------
646 
652 {
653 public:
654  PopulationManagerList() { for (int i=0; i< TOP_foobar; i++) m_populationlist[i] = NULL; }
655  void SetPopulation(Population_Manager* p_pm, TTypesOfPopulation a_pt) { m_populationlist[a_pt] = p_pm; }
656  Population_Manager* GetPopulation(TTypesOfPopulation a_pt) { return m_populationlist[a_pt]; }
657 protected:
658  Population_Manager* m_populationlist[TOP_foobar];
659 };
660 //------------------------------------------------------------------------------
661 
662 
663 #endif
664 
665 
TTypesOfPopulation
An enum to hold all the possible types of population handled by a Population_Manager class...
virtual int SupplyCovPosy(int)
const char * SupplyListName(int i)
The landscape class containing all environmental and topographical data.
Definition: landscape.h:109
A class defining an animals position.
void LamdaDeath(int x, int y)
unsigned SupplyListIndexSize()
bool IsLast(unsigned listindex)
unsigned GetLiveArraySize(int a_listindex)
Gets the number of &#39;live&#39; objects for a list index in the TheArray.
virtual bool OpenTheFledgelingProbe()
TTypesOfLandscapeElement m_EleType
void SetY(int a_y)
virtual void TheGeneticProbe(unsigned, int, unsigned &)
unsigned m_NoAreas
virtual bool OnFarmEvent(FarmToDo)
int SupplyState(unsigned listindex, unsigned j)
char * AnsiString
bool OpenForAppendToFile()
virtual void ReinitialiseObject(int x, int y, Landscape *L)
Used to re-use an object - must be implemented in descendent classes.
unsigned m_Range_x
virtual void BeginStep(void)
BeingStep behaviour - must be implemented in descendent classes.
ofstream * m_MyFile
double get_varianceP()
Returns the population variance.
virtual int WhatState()
virtual int SupplyPegPosy(int)
TTypesOfVegetation
vector< TListOfAnimals > TheArray
int SupplySimAreaWidth(void)
Definition: landscape.h:1558
void CorrectWrapRound()
Corrects wrap around co-ordinate problems.
virtual int SupplyCovPosx(int)
Base class for all population managers.
unsigned m_ReportInterval
virtual int SupplyPegPosx(int)
Population_Manager * GetPopulation(TTypesOfPopulation a_pt)
void IncLiveArraySize(int a_listindex)
Increments the number of &#39;live&#39; objects for a list index in the TheArray.
unsigned m_NoVegTypes
virtual void TheNWordOutputProbe()
A struct of 100 ints.
unsigned m_x1
unsigned m_Range_y
virtual void BreedingSuccessProbeOutput(double, int, int, int, int, int, int, int)
void add_variable(double x)
Add a value.
void LamdaBirth(int x, int y)
virtual void Dying()
void SetPopulation(Population_Manager *p_pm, TTypesOfPopulation a_pt)
unsigned m_OldRange_y
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
TTypesOfVegetation m_VegType
SimpleStatistics()
SimpleStatistics constructor.
virtual void EndStep(void)
EndStep behaviour - must be implemented in descendent classes.
int Supply_m_Location_y()
int m_CurrentStateNo
The basic state number for all objects - &#39;-1&#39; indicates death.
void SetNoProbes(int a_pn)
int m_SeasonNumber
Holds the season number. Used when running goose and hunter sims.
void ClearData()
Clears the data.
double get_N()
Returns the number of values.
TTypesOfPopulation GetPopulationType()
void remove_variable(double x)
Remove a value.
double get_varianceS()
Returns the sample variance.
FarmToDo
Definition: treatment.h:31
virtual void Catastrophe(int)
virtual void BreedingPairsOutput(int)
int Supply_m_Location_x()
The base class for all ALMaSS animal classes.
virtual void CopyMyself()
unsigned SupplyStateNamesLength()
virtual void Step(void)
Step behaviour - must be implemented in descendent classes.
double get_meanvalue()
Returns the mean.
double get_Total()
Returns the mean.
int SupplyPolygonRef()
int SupplySimAreaHeight(void)
Definition: landscape.h:1563
unsigned m_NoEleTypes
vector< TAnimal * > TListOfAnimals
unsigned SupplyListSize(unsigned listindex)
virtual bool OpenTheBreedingSuccessProbe()
APoint SupplyPoint()
A struct defining two x,y coordinate sets of positive co-ords only.
Landscape * m_OurLandscape
virtual void GeneticsResultsOutput(FILE *, unsigned)
The base class of all ALMaSS objects requiring Step code.
unsigned m_y1
unsigned m_NoFarms
virtual void KillThis()
virtual void BeginStep(void)
BeingStep behaviour - must be implemented in descendent classes.
int GetSeasonNumber()
Get the season number.
bool GetStepDone()
Returns the step done indicator flag.
TTypesOfPopulation m_population_type
virtual bool OpenTheBreedingPairsProbe()
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
virtual void FledgelingProbeOutput(int, int)
virtual int TheBreedingFemalesProbe(int)
unsigned m_x2
void SetStepDone(bool a_bool)
Sets the step done indicator flag.
void SetCurrentStateNo(int a_num)
Sets the current state number.
virtual void Step(void)
Step behaviour - must be implemented in descendent classes.
const char * SupplyStateNames(int i)
TTypesOfLandscapeElement
void LamdaBirth(int x, int y, int z)
bool m_StepDone
Indicates whether the iterative step code is done for this timestep.
virtual void SupplyLocXY(unsigned listindex, unsigned j, int &x, int &y)
Data structure to hold & output probe data probe data is designed to be used to return the number of ...
virtual int TheFledgelingProbe()
vector< unsigned > m_LiveArraySize
virtual void ReinitialiseObject()
Used to re-use an object - must be implemented in descendent classes.
int GetCurrentStateNo()
Returns the current state number.
unsigned m_OldRange_x
A small class to hold the pointers to active population managers.
virtual int TheBreedingSuccessProbe(int &, int &, int &, int &, int &, int &)
Landscape * m_TheLandscape
unsigned m_y2
void SetX(int a_x)
Part of the basic ALMaSS system (obselete)
virtual void EndStep(void)
EndStep behaviour - must be implemented in descendent classes.