ALMaSS Rabbit ODdox  1.1
The rabbit model description following ODdox protocol
PopulationManager.cpp
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 */
39 //---------------------------------------------------------------------------
40 //
41 #include <vector>
42 #include <algorithm>
43 #include <iostream>
44 #include <fstream>
45 #include <string.h>
46 #include <list>
47 
48 
49 #include "../Landscape/ls.h"
50 #include "../BatchALMaSS/PopulationManager.h"
51 #include "../GooseManagement/GooseMemoryMap.h"
52 #include "../GooseManagement/Goose_Base.h"
53 #include "../BatchALMaSS/CurveClasses.h"
54 #include "../Hunters/Hunters_all.h"
55 #include "../GooseManagement/Goose_Population_Manager.h"
56 #ifdef __ALMASS_VISUAL
57 #include "wx/wx.h"
58 #include "wx/spinctrl.h"
59 #include "../GUI/ALMaSS_GUI.h"
60 #endif
61 #include "../BatchALMaSS/BoostRandomGenerators.h"
62 #include "AOR_Probe.h"
63 
64 extern boost::variate_generator<base_generator_type&, boost::uniform_real<> > g_rand_uni;
65 
66 using namespace std;
67 
68 //------------------------------------------------------------------------------
69 
70 
71 /* Species code
72 #ifdef StartAnimal
73  #include "../Skylark/Skylarks_All.h"
74 #endif
75 #ifdef PartridgeModel
76  #include "../Partridge/Partridge_All.h"
77 #endif
78 #ifdef SkylarkModel
79  #include "../Skylark/Skylarks_All.h"
80 #endif
81 #ifdef YellowhammerModel
82  #include "../Yellowhammer/Yellowhammer_All.h"
83 #endif
84 #ifdef SpiderModel
85  #include "../Spider/Spider_all.h"
86 #endif
87 #ifdef BembidionModel
88  #include "../Bembidion/Bembidion_all.h"
89 #endif
90 #ifdef RoeDeerModel
91  #include "../RoeDeer/Roe_all.h"
92 #endif
93 #ifdef FieldVoleModel
94  #include "../Vole/GeneticMaterial.h"
95  #include "../Vole/Predators.h"
96  #include "../Vole/Vole_all.h"
97 #endif
98 */
99 #define _CRTDBG_MAP_ALLOC
100 
101 static CfgStr cfg_RipleysOutput_filename( "G_RIPLEYSOUTPUT_FILENAME", CFG_CUSTOM, "RipleysOutput.txt" );
102 static CfgStr cfg_ReallyBigOutput_filename( "G_REALLYBIGOUTPUT_FILENAME", CFG_CUSTOM, "ReallyBigOutput.txt" );
103 CfgBool cfg_RipleysOutputMonthly_used( "G_RIPLEYSOUTPUTMONTHLY_USED", CFG_CUSTOM, false );
104 CfgBool cfg_RipleysOutput_used("G_RIPLEYSOUTPUT_USED", CFG_CUSTOM, false);
105 CfgBool cfg_AOROutput_used("G_AOROUTPUT_USED", CFG_CUSTOM, false);
106 CfgBool cfg_ReallyBigOutput_used( "G_REALLYBIGOUTPUT_USED", CFG_CUSTOM, false );
107 CfgBool cfg_fixed_random_sequence("G_FIXEDRANDOMSEQUENCE",CFG_CUSTOM,false);
108 static CfgInt cfg_AOROutput_interval("G_AORSOUTPUT_INTERVAL", CFG_CUSTOM, 1);
109 static CfgInt cfg_AOROutput_day("G_AOROUTPUT_DAY", CFG_CUSTOM, 60);
110 static CfgInt cfg_AOROutputFirstYear("G_AOROUTPUT_FIRSTYEAR", CFG_CUSTOM, 1);
111 static CfgInt cfg_RipleysOutput_interval("G_RIPLEYSOUTPUT_INTERVAL", CFG_CUSTOM, 1);
112 static CfgInt cfg_RipleysOutput_day("G_RIPLEYSOUTPUT_DAY", CFG_CUSTOM, 60);
113 static CfgInt cfg_RipleysOutputFirstYear("G_RIPLEYSOUTPUT_FIRSTYEAR", CFG_CUSTOM, 1);
114 static CfgInt cfg_ReallyBigOutput_interval( "G_REALLYBIGOUTPUT_INTERVAL", CFG_CUSTOM, 1 );
115 static CfgInt cfg_ReallyBigOutput_day1( "G_REALLYBIGOUTPUT_DAY_ONE", CFG_CUSTOM, 1 );
116 static CfgInt cfg_ReallyBigOutput_day2( "G_REALLYBIGOUTPUT_DAY_TWO", CFG_CUSTOM, 91 );
117 static CfgInt cfg_ReallyBigOutput_day3( "G_REALLYBIGOUTPUT_DAY_THREE", CFG_CUSTOM, 182 );
118 static CfgInt cfg_ReallyBigOutput_day4( "G_REALLYBIGOUTPUT_DAY_FOUR", CFG_CUSTOM, 274 );
119 static CfgInt cfg_ReallyBigOutputFirstYear("G_REALLYBIGOUTPUT_FIRSTYEAR",CFG_CUSTOM,1);
120 // Catastrophe config variables
121 CfgInt cfg_CatastropheEventStartYear("PM_CATASTROPHEEVENTSTARTYEAR",CFG_CUSTOM,99999); // this means unless this is altered it events will not happen
122 CfgInt cfg_pm_eventfrequency( "PM_EVENTFREQUENCY", CFG_CUSTOM, 0 ); // every X years
123 CfgInt cfg_pm_eventsize( "PM_EVENTSIZE", CFG_CUSTOM, 100 ); // The percentage change in population size, 100=no change
124 static CfgInt cfg_DayInMonth( "PRB_DAYINMONTH", CFG_CUSTOM, 1 );
125 static CfgBool cfg_VoleCatastrophe_on( "VOLE_CATASTROPHE_ON", CFG_CUSTOM, false );
126 static CfgInt cfg_VoleCatastrophe_interval( "VOLE_CATASTROPHE_I", CFG_CUSTOM, 365 * 5 );
127 static CfgInt cfg_VoleCatastrophe_mortality( "VOLE_CATASTROPHE_M", CFG_CUSTOM, 90 );
128 
129 char g_str[255];
130 
131 //---------------------------------------------------------------------------
132 // Population Manager
133 //---------------------------------------------------------------------------
134 //---------------------------------------------------------------------------
136  if ( m_MyFile != NULL ) {
137  if (m_MyFile->is_open()) m_MyFile->close();
138  delete m_MyFile;
139  }
140 };
141 
146 class CompareX {
147 public:
148  bool operator() ( TAnimal * A1, TAnimal * A2 ) const {
149  return (A1->Supply_m_Location_x() < A2->Supply_m_Location_x());
150  }
151 };
152 
157 class CompareY {
158 public:
159  bool operator() ( TAnimal * A1, TAnimal * A2 ) const {
160  return (A1->Supply_m_Location_y() < A2->Supply_m_Location_y());
161  }
162 };
163 
172 public:
173  bool operator() ( TAnimal * A1, TAnimal * A2 ) const {
174  return (A1->WhatState() < A2->WhatState());
175  }
176 };
177 
178 
184 public:
185  bool operator() (TAnimal * A1, TAnimal * A2) const {
186  return (A1->GetCurrentStateNo() > A2->GetCurrentStateNo());
187  }
188 };
189 
195 public:
196  bool operator() (TAnimal * A1) const {
197  return (A1->GetCurrentStateNo() == -1);
198  }
199 };
200 
206 public:
207  bool operator() (TAnimal * A1) const {
208  return (A1->GetCurrentStateNo() != -1);
209  }
210 };
211 
212 
213 
214 //---------------------------------------------------------------------------
215 //---------------------------------------------------------------------------
216 
217 
222  // Keep a pointer to the landscape this population is in
223  m_TheLandscape = L;
224  // create 10 empty arrays as default, excess can be removed in descendent classes
225  TListOfAnimals alist;
226  TheArray.insert( TheArray.end(), 12, alist );
227  // Set the simulation bounds
228  SimH = m_TheLandscape->SupplySimAreaHeight();
229  SimW = m_TheLandscape->SupplySimAreaWidth();
230  SimHH = m_TheLandscape->SupplySimAreaHeight()/2;
231  SimWH = m_TheLandscape->SupplySimAreaWidth()/2;
232 
233  // dww this is hardcoded limit of 12.
234  // Needs fixing
235  for (int i = 0; i < 12; i++) {
236  // Set default BeforeStepActions
237  BeforeStepActions[ i ] = 0;
238  m_ListNames[ i ] = "Unknown";
239  m_LiveArraySize.push_back( 0 );
240  }
241  // modify this if they need to in descendent classes
242  StateNamesLength = 0; // initialise this variable.
243  m_catastrophestartyear=-1; // Default is don't do this
244 #ifdef __LAMBDA_RECORD
245  ofstream fout("LambdaGridOuput.txt", ios_base::out); // open for writing
246  fout.close();
247  LamdaClear();
248 #endif
250  OpenTheMonthlyRipleysOutputProbe();
251  }
252  if (cfg_AOROutput_used.value()) {
253  OpenTheAOROutputProbe("AOR_Probe.txt");
254  }
255  // Ensure the GUI pointer is NULL in case we are not in GUI mode
256 #ifdef __ALMASS_VISUAL
257  m_MainForm = NULL;
258 #endif
259  m_SeasonNumber = 0; // Ensure that we start in season number 0. Season number is incremented at day in year 183
260 }
261 //---------------------------------------------------------------------------
262 
267  // clean-up
268  for ( unsigned i = 0; i < TheArray.size(); i++ ) {
269  // if objects in the list need to be deleted:
270  for ( unsigned j = 0; j < (unsigned) GetLiveArraySize(i); j++ ) {
271  delete TheArray[ i ] [ j ];
272  }
273  // empty the array
274  TheArray[ i ].clear();
275  // --
276  }
277  if ( cfg_RipleysOutput_used.value() ) {
278  CloseTheRipleysOutputProbe();
279  }
281  CloseTheMonthlyRipleysOutputProbe();
282  }
284  CloseTheReallyBigOutputProbe();
285  }
286  if (cfg_AOROutput_used.value()) delete m_AOR_Probe;
287 }
288 
289 //-----------------------------------------------------------------------------
290 
294 void Population_Manager::LOG( const char* fname ) {
295  FILE * PFile = fopen(fname, "w" );
296  if (PFile) {
297  m_TheLandscape->Warn("PopulationManager::LOG - Could not open file ",fname);
298  exit(0);
299  }
300  AnimalPosition AP;
301  for ( unsigned listindex = 0; listindex < TheArray.size(); listindex++ ) {
302 #ifndef __BCB__
303  fprintf( PFile, "%s :\n", m_ListNames[ listindex ] );
304 #else
305  fprintf( PFile, "%s :\n", m_ListNames[ listindex ].c_str() );
306 #endif
307  for ( unsigned j = 0; j < (unsigned) m_LiveArraySize[listindex]; j++ ) {
308  AP = TheArray[ listindex ] [ j ]->SupplyPosition();
309  fprintf( PFile, "%i %i %i\n", j, AP.m_x, AP.m_y );
310  }
311  }
312  fclose( PFile );
313 }
314 
315 //-----------------------------------------------------------------------------
316 
321 }
322 
323 //---------------------------------------------------------------------------
324 
329 }
330 
331 //---------------------------------------------------------------------------
332 
337 void Population_Manager::Run( int NoTSteps ) {
341  for ( int TSteps = 0; TSteps < NoTSteps; TSteps++ )
342  {
343  unsigned size2;
344  unsigned size1 = (unsigned) TheArray.size();
349  for ( unsigned listindex = 0; listindex < size1; listindex++ )
350  {
351  // Must check each object in the list for m_CurrentStateNo==-1
352  m_LiveArraySize[listindex] = PartitionLiveDead(listindex);
353  }
354 #ifdef __ALMASS_VISUAL
355  if (m_MainForm!=NULL)
356  {
357  int n = 0;
358  for ( unsigned listindex = 0; listindex < size1; listindex++ ) n += (int) m_LiveArraySize[ listindex ];
360  //if (n>0) DisplayLocations();
361  }
362 #endif
363  // begin step actions ...
364  // set all stepdone to false.... is this really necessary??
365  for ( unsigned listindex = 0; listindex < size1; listindex++ )
366  {
367  size2 = (unsigned) GetLiveArraySize(listindex);
368  for ( unsigned j = 0; j < size2; j++ )
369  {
370  TheArray[ listindex ] [ j ]->SetStepDone( false );
371  }
372  }
373 
374  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
375  // Call the Shuffle/Sort procedures
376  Shuffle_or_Sort( listindex );
377  }
378  // Need to check if Ripleys Statistic needs to be saved
380  int Year = m_TheLandscape->SupplyYearNumber();
381  if (Year >= cfg_RipleysOutputFirstYear.value()) {
382  if (Year % cfg_RipleysOutput_interval.value() == 0) {
383  int day = m_TheLandscape->SupplyDayInYear();
384  if (cfg_RipleysOutput_day.value() == day) {
385  // Do the Ripley Probe
386  TheRipleysOutputProbe(RipleysOutputPrb);
387  }
388  }
389  }
390  }
391  // Need to check if AOR output needs to be saved
392  if (cfg_AOROutput_used.value()) {
393  int Year = m_TheLandscape->SupplyYearNumber();
394  if (Year >= cfg_AOROutputFirstYear.value()) {
395  if (Year % cfg_AOROutput_interval.value() == 0) {
396  int day = m_TheLandscape->SupplyDayInYear();
397  if (cfg_AOROutput_day.value() == day) {
398  // Do the AOR Probe
399  TheAOROutputProbe();
400  }
401  }
402  }
403  }
404  // Need to check if Monthly Ripleys Statistic needs to be saved
406  if (m_TheLandscape->SupplyDayInMonth()==1) {
407  int Year = m_TheLandscape->SupplyYearNumber();
408  if (Year>=cfg_RipleysOutputFirstYear.value()) {
409  if ( Year % cfg_RipleysOutput_interval.value() == 0 ) {
410  int month = m_TheLandscape->SupplyMonth();
411  // Do the Ripley Probe
412  switch (month) {
413  case 1: TheRipleysOutputProbe( RipleysOutputPrb1 );
414  break;
415  case 2: TheRipleysOutputProbe( RipleysOutputPrb2 );
416  break;
417  case 3: TheRipleysOutputProbe( RipleysOutputPrb3 );
418  break;
419  case 4: TheRipleysOutputProbe( RipleysOutputPrb4 );
420  break;
421  case 5: TheRipleysOutputProbe( RipleysOutputPrb5 );
422  break;
423  case 6: TheRipleysOutputProbe( RipleysOutputPrb6 );
424  break;
425  case 7: TheRipleysOutputProbe( RipleysOutputPrb7 );
426  break;
427  case 8: TheRipleysOutputProbe( RipleysOutputPrb8 );
428  break;
429  case 9: TheRipleysOutputProbe( RipleysOutputPrb9 );
430  break;
431  case 10: TheRipleysOutputProbe( RipleysOutputPrb10 );
432  break;
433  case 11: TheRipleysOutputProbe( RipleysOutputPrb11 );
434  break;
435  case 12: TheRipleysOutputProbe( RipleysOutputPrb12 );
436  break;
437  }
438  }
439  }
440  }
441  }
442  // Need to check if Really Big Probe needs to be saved
444  int Year = m_TheLandscape->SupplyYearNumber();
445  if (Year>=cfg_ReallyBigOutputFirstYear.value()) {
446  if ( Year % cfg_ReallyBigOutput_interval.value() == 0 ) {
447  int day = m_TheLandscape->SupplyDayInYear();
449  {
450  // Do the Ripley Probe
451  TheReallyBigOutputProbe();
452  }
453  }
454  }
455  }
456  int yr=m_TheLandscape->SupplyYearNumber();
457  if ( yr > cfg_CatastropheEventStartYear.value() ) {
458  if (m_catastrophestartyear==-1) m_catastrophestartyear=yr;
459  Catastrophe(); // This method must be overidden in descendent classes
460  }
461  DoFirst();
462  // call the begin-step-method of all objects
463  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
464  size2 = (unsigned)GetLiveArraySize( listindex );
465  for (unsigned j = 0; j < size2; j++)
466  TheArray[ listindex ] [ j ]->BeginStep();
467  }
468  DoBefore();
469  // call the step-method of all objects
470  do {
471  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
472  size2 = (unsigned)GetLiveArraySize( listindex );
473  for (unsigned j = 0; j < size2; j++) {
474  TheArray[ listindex ] [ j ]->Step();
475  }
476  } // for listindex
477  } while ( !StepFinished() );
478  DoAfter();
479  // call the end-step-method of all objects
480  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
481  size2 = (unsigned)GetLiveArraySize( listindex );
482  for (unsigned j = 0; j < size2; j++) {
483  TheArray[ listindex ] [ j ]->EndStep();
484  }
485  }
486  // ----------------
487  // end of this step actions
488  // For each animal list
489  DoAlmostLast();
490 
491 DoLast();
492  } // End of time step loop
493 }
494 
495 //---------------------------------------------------------------------------
496 
499  for ( unsigned listindex = 0; listindex < TheArray.size(); listindex++ ) {
500  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( listindex ); j++) {
501  if ( TheArray[ listindex ] [ j ]->GetStepDone() == false ) {
502  return false;
503  }
504  }
505  }
506  return true;
507 }
508 //---------------------------------------------------------------------------
509 
514  //TODO: Add your source code here
515 }
516 
517 //---------------------------------------------------------------------------
518 
523  //TODO: Add your source code here
524 }
525 
526 //---------------------------------------------------------------------------
531 
532 /*
533 #ifdef __UNIX__
534  for ( unsigned i = 0; i < 100; i++ ) {
535  StateList.n[ i ] = 0;
536  }
537 #else
538  // Zero results
539  for ( unsigned listindex = 0; listindex < m_ListNameLength; listindex++ ) {
540  for ( unsigned i = 0; i < 100; i++ ) {
541  Counts[ listindex ] [ i ] = 0;
542  }
543  }
544 #endif
545  // How many animals in each state?
546  for ( unsigned listindex = 0; listindex < TheArray.size(); listindex++ ) {
547  unsigned size = (unsigned) TheArray[ listindex ].size();
548  for ( unsigned j = 0; j < size; j++ ) {
549 #ifdef __UNIX__
550  StateList.n[ TheArray[ listindex ] [ j ]->WhatState() ] ++;
551 #else
552  Counts[ listindex ] [ TheArray[ listindex ] [ j ]->WhatState() ] ++;
553 #endif
554  }
555  }
556 */
557 }
558 //---------------------------------------------------------------------------
559 
561 {
565 #ifdef __ALMASS_VISUAL
566  m_MainForm->UpdateGraphics();
567 #endif
568 }
569 //---------------------------------------------------------------------------
570 
574 int Population_Manager::ProbeFileInput( char * p_Filename, int p_ProbeNo ) {
575  FILE * PFile;
576  int data = 0;
577  int data2 = 0;
578  char S[ 255 ];
579  PFile = fopen(p_Filename, "r" );
580  if ( !PFile ) {
581  m_TheLandscape->Warn( "Population Manager - cannot open Probe File ", p_Filename );
582  exit( 0 );
583  }
584  fgets( S, 255, PFile ); // dummy line
585  fgets( S, 255, PFile ); // dummy line
586  fscanf( PFile, "%d\n", & data ); // Reporting interval
587  TheProbe[ p_ProbeNo ]->m_ReportInterval = data;
588  fgets( S, 255, PFile ); // dummy line
589  fscanf( PFile, "%d\n", & data ); // Write to file
590  if ( data == 0 ) TheProbe[ p_ProbeNo ]->m_FileRecord = false; else
591  TheProbe[ p_ProbeNo ]->m_FileRecord = true;
592  fgets( S, 255, PFile ); // dummy line
593  for ( int i = 0; i < 10; i++ ) {
594  fscanf( PFile, "%d", & data );
595  if ( data > 0 ) TheProbe[ p_ProbeNo ]->m_TargetTypes[ i ] = true; else
596  TheProbe[ p_ProbeNo ]->m_TargetTypes[ i ] = false;
597  }
598 
599  fgets( S, 255, PFile ); // dummy line
600  fgets( S, 255, PFile ); // dummy line
601  fscanf( PFile, "%d", & data );
602  TheProbe[ p_ProbeNo ]->m_NoAreas = data;
603  fgets( S, 255, PFile ); // dummy line
604  fgets( S, 255, PFile ); // dummy line
605  fscanf( PFile, "%d", & data2 ); // No References areas
606  fgets( S, 255, PFile ); // dummy line
607  fgets( S, 255, PFile ); // dummy line
608  fscanf( PFile, "%d", & data ); // Type reference for probe
609  if ( data == 1 ) TheProbe[ p_ProbeNo ]->m_NoEleTypes = data2; else
610  TheProbe[ p_ProbeNo ]->m_NoEleTypes = 0;
611  if ( data == 2 ) TheProbe[ p_ProbeNo ]->m_NoVegTypes = data2; else
612  TheProbe[ p_ProbeNo ]->m_NoVegTypes = 0;
613  if ( data == 3 ) TheProbe[ p_ProbeNo ]->m_NoFarms = data2; else
614  TheProbe[ p_ProbeNo ]->m_NoFarms = 0;
615  fgets( S, 255, PFile ); // dummy line
616  fgets( S, 255, PFile ); // dummy line
617  // Now read in the areas data
618  for ( int i = 0; i < 10; i++ ) {
619  fscanf( PFile, "%d", & data );
620  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_x1 = data;
621  fscanf( PFile, "%d", & data );
622  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_y1 = data;
623  fscanf( PFile, "%d", & data );
624  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_x2 = data;
625  fscanf( PFile, "%d", & data );
626  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_y2 = data;
627  }
628  fgets( S, 255, PFile ); // dummy line
629  fgets( S, 255, PFile ); // dummy line
630  if ( TheProbe[ p_ProbeNo ]->m_NoVegTypes > 0 ) {
631  for ( int i = 0; i < 25; i++ ) {
632  fscanf( PFile, "%d", & data );
633  if ( data != 999 )
634  TheProbe[ p_ProbeNo ]->m_RefVeg[ i ] = m_TheLandscape->TranslateVegTypes( data );
635  }
636  } else if ( TheProbe[ p_ProbeNo ]->m_NoFarms > 0 ) {
637  for ( int i = 0; i < 25; i++ ) {
638  fscanf( PFile, "%d", & data );
639  if ( data != 999 )
640  TheProbe[ p_ProbeNo ]->m_RefFarms[ i ] = data;
641  }
642  } else {
643  for ( int i = 0; i < 25; i++ ) {
644  fscanf( PFile, "%d", & data );
645  if ( data != 999 ) TheProbe[ p_ProbeNo ]->m_RefEle[ i ] = m_TheLandscape->TranslateEleTypes( data );
646  }
647  }
648  fclose( PFile );
649  return data2; // number of data references
650 }
651 
652 //-----------------------------------------------------------------------------
653 
658  ofstream fout("LambdaGridOuput.txt", ios_base::app); // open for writing
659  for (int i=0; i<257; i++ ) {
660  for (int j=0; j<257; j++) {
661  fout << lamdagrid[0][i][j] << "\t" << lamdagrid[1][i][j] << endl;
662  }
663  }
664  //fout << "NEXT" << endl;
665  fout.close();
666 }
667 //-----------------------------------------------------------------------------
668 
673 
674 }
675 //-----------------------------------------------------------------------------
676 
680 float Population_Manager::Probe( int ListIndex, probe_data * p_TheProbe ) {
681  // Counts through the list and goes through each area to see if the animal
682  // is standing there and if the farm, veg or element conditions are met
683  AnimalPosition Sp;
684  float NumberSk = 0;
685  // Four possibilites
686  // either NoVegTypes or NoElementTypes or NoFarmTypes is >0 or all==0
687  if ( p_TheProbe->m_NoFarms != 0 ) {
688  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( ListIndex ); j++) {
689  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
690  unsigned Farm = TheArray[ ListIndex ] [ j ]->SupplyFarmOwnerRef();
691  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
692  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
693  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) )
694  for ( unsigned k = 0; k < p_TheProbe->m_NoFarms; k++ ) {
695  if ( p_TheProbe->m_RefFarms[ k ] == Farm )
696  NumberSk++; // it is in the square so increment number
697  }
698 
699  }
700  }
701  } else if ( p_TheProbe->m_NoEleTypes != 0 ) {
702  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( ListIndex ); j++) {
703  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
704  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
705  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
706  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) )
707  for ( unsigned k = 0; k < p_TheProbe->m_NoEleTypes; k++ ) {
708  if ( p_TheProbe->m_RefEle[ k ] == Sp.m_EleType )
709  NumberSk++; // it is in the square so increment number
710  }
711  }
712  }
713  } else {
714  if ( p_TheProbe->m_NoVegTypes != 0 ) {
715  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( ListIndex ); j++) {
716  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
717 
718  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
719  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
720  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) ) {
721  for ( unsigned k = 0; k < p_TheProbe->m_NoVegTypes; k++ ) {
722  if ( p_TheProbe->m_RefVeg[ k ] == Sp.m_VegType )
723  NumberSk++; // it is in the square so increment number
724  }
725  }
726  }
727  }
728  } else // both must be zero
729  {
730  unsigned sz = (unsigned)GetLiveArraySize( ListIndex );
731  // It is worth checking whether we have a total dump of all individuals
732  // if so don't bother with asking each where it is
733  if (p_TheProbe->m_NoAreas==1) {
734  if ((p_TheProbe->m_Rect[0].m_x1==0) && (p_TheProbe->m_Rect[0].m_y1==0) && (p_TheProbe->m_Rect[0].m_x2==(unsigned)SimW) &&
735  (p_TheProbe->m_Rect[0].m_y2==(unsigned)SimH)) {
736  return (float) sz;
737  }
738  }
739  // Asking for a subset - need to test them all
740  for ( unsigned j = 0; j < sz; j++ ) {
741  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
742  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
743  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
744  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) )
745  NumberSk++; // it is in the square so increment number
746  }
747  }
748  }
749  }
750  return NumberSk;
751 }
752 //-----------------------------------------------------------------------------
753 
757 bool Population_Manager::OpenTheRipleysOutputProbe(string a_NWordFilename) {
758  RipleysOutputPrb = fopen(cfg_RipleysOutput_filename.value(), "w");
759  if (!RipleysOutputPrb) {
760  g_msg->Warn(WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
762  exit(1);
763  }
764  return true;
765 }
766 
767 //-----------------------------------------------------------------------------
771 void Population_Manager::OpenTheAOROutputProbe(string a_AORFilename) {
772  m_AOR_Probe = new AOR_Probe(this, m_TheLandscape, a_AORFilename);
773 }
774 //-----------------------------------------------------------------------------
779  RipleysOutputPrb1 = fopen("RipleyOutput_Jan.txt", "w" );
780  if ( !RipleysOutputPrb1 ) {
781  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
783  exit( 1 );
784  }
785  RipleysOutputPrb2 = fopen("RipleyOutput_Feb.txt", "w" );
786  if ( !RipleysOutputPrb2 ) {
787  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
789  exit( 1 );
790  }
791  RipleysOutputPrb3 = fopen("RipleyOutput_Mar.txt", "w" );
792  if ( !RipleysOutputPrb3 ) {
793  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
795  exit( 1 );
796  }
797  RipleysOutputPrb4 = fopen("RipleyOutput_Apr.txt", "w" );
798  if ( !RipleysOutputPrb4 ) {
799  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
801  exit( 1 );
802  }
803  RipleysOutputPrb5 = fopen("RipleyOutput_May.txt", "w" );
804  if ( !RipleysOutputPrb5 ) {
805  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
807  exit( 1 );
808  }
809  RipleysOutputPrb6 = fopen("RipleyOutput_Jun.txt", "w" );
810  if ( !RipleysOutputPrb6 ) {
811  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
813  exit( 1 );
814  }
815  RipleysOutputPrb7 = fopen("RipleyOutput_Jul.txt", "w" );
816  if ( !RipleysOutputPrb7 ) {
817  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
819  exit( 1 );
820  }
821  RipleysOutputPrb8 = fopen("RipleyOutput_Aug.txt", "w" );
822  if ( !RipleysOutputPrb8 ) {
823  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
825  exit( 1 );
826  }
827  RipleysOutputPrb9 = fopen("RipleyOutput_Sep.txt", "w" );
828  if ( !RipleysOutputPrb9 ) {
829  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
831  exit( 1 );
832  }
833  RipleysOutputPrb10 = fopen("RipleyOutput_Oct.txt", "w" );
834  if ( !RipleysOutputPrb10 ) {
835  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
837  exit( 1 );
838  }
839  RipleysOutputPrb11 = fopen("RipleyOutput_Nov.txt", "w" );
840  if ( !RipleysOutputPrb11 ) {
841  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
843  exit( 1 );
844  }
845  RipleysOutputPrb12 = fopen("RipleyOutput_Dec.txt", "w" );
846  if ( !RipleysOutputPrb12 ) {
847  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
849  exit( 1 );
850  }
851  return true;
852 }
853 
854 //-----------------------------------------------------------------------------
855 
860  ReallyBigOutputPrb = fopen(cfg_ReallyBigOutput_filename.value(), "w" );
861  if ( !ReallyBigOutputPrb ) {
862  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
864  exit( 1 );
865  }
866  return true;
867 }
868 
869 //-----------------------------------------------------------------------------
870 
876  fclose( RipleysOutputPrb );
877  RipleysOutputPrb=NULL;
878 }
879 
880 //-----------------------------------------------------------------------------
881 
886  fclose( RipleysOutputPrb1 );
887  fclose( RipleysOutputPrb2 );
888  fclose( RipleysOutputPrb3 );
889  fclose( RipleysOutputPrb4 );
890  fclose( RipleysOutputPrb5 );
891  fclose( RipleysOutputPrb6 );
892  fclose( RipleysOutputPrb7 );
893  fclose( RipleysOutputPrb8 );
894  fclose( RipleysOutputPrb9 );
895  fclose( RipleysOutputPrb10 );
896  fclose( RipleysOutputPrb11 );
897  fclose( RipleysOutputPrb12 );
898 }
899 
900 //-----------------------------------------------------------------------------
905  if ( ReallyBigOutputPrb != 0 )
906  fclose( ReallyBigOutputPrb );
907  ReallyBigOutputPrb=0;
908 }
909 
910 //-----------------------------------------------------------------------------
911 
914 }
915 //-----------------------------------------------------------------------------
916 
919 }
920 
921 //---------------------------------------------------------------------------
922 
925 }
926 
927 //---------------------------------------------------------------------------
928 
929 
930 //-----------------------------------------------------------------------------
931 // TheArray handling functions
932 //----------------------
933 
937 TAnimal * Population_Manager::FindClosest( int x, int y, unsigned Type ) {
938  int distance = 100000000;
939  TAnimal * TA = NULL;
940  int dx, dy, d;
941  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( Type ); j++) {
942  dx = TheArray[ Type ] [ j ]->Supply_m_Location_x();
943  dy = TheArray[ Type ] [ j ]->Supply_m_Location_y();
944  dx = ( dx - x );
945  dx *= dx;
946  dy = ( dy - y );
947 
948  dy *= dy;
949  d = dx + dy;
950  if ( d < distance ) {
951  distance = d;
952  TA = TheArray[ Type ] [ j ];
953  }
954  }
955  return TA;
956 }
957 
958 //------------------------------------------------------------------------------
963 {
964  for ( unsigned i = 0; i < TheArray.size(); i++ )
965  {
966  // if objects in the list need to be deleted:
967  for (unsigned j = 0; j < (unsigned) TheArray[i].size(); j++)
968  {
969  delete TheArray[ i ] [ j ];
970  }
971  // empty the array
972  TheArray[ i ].clear();
973  }
974 }
975 
979 void Population_Manager::SortX( unsigned Type ) {
980  vector<TAnimal*>::iterator nth = TheArray[Type].begin() + m_LiveArraySize[Type];
981  sort( TheArray[ Type ].begin(), nth, CompareX() );
982 }
983 
984 //-----------------------------------------------------------------------------
985 
989 void Population_Manager::SortY( unsigned Type ) {
990  vector<TAnimal*>::iterator nth = TheArray[Type].begin() + m_LiveArraySize[Type];
991  sort(TheArray[Type].begin(), nth, CompareY());
992 }
993 
994 //-----------------------------------------------------------------------------
995 
999 void Population_Manager::SortState( unsigned Type ) {
1000  sort( TheArray[ Type ].begin(), TheArray[ Type ].end(), CompareState() );
1001 }
1002 
1003 //-----------------------------------------------------------------------------
1004 
1008 void Population_Manager::SortStateR(unsigned Type) {
1009  sort(TheArray[Type].begin(), TheArray[Type].begin()+m_LiveArraySize[Type], CompareStateR());
1010 }
1011 
1015 unsigned Population_Manager::PartitionLiveDead(unsigned Type) {
1016  auto it = partition(TheArray[Type].begin(), TheArray[Type].begin() + m_LiveArraySize[Type], CompareStateAlive());
1017  // it now points at the first dead element
1018  // the number of live ones before it is it-TheArray[Type].begin(). This is the new vector size for live animals.
1019  return unsigned(it - TheArray[Type].begin());
1020 }
1021 
1022 //-----------------------------------------------------------------------------
1023 
1027 void Population_Manager::SortXIndex( unsigned Type ) {
1028  vector<TAnimal*>::iterator nth = TheArray[Type].begin() + m_LiveArraySize[Type];
1029  sort(TheArray[Type].begin(), nth, CompareX());
1030  unsigned s = (unsigned)GetLiveArraySize( Type );
1031  // Now make the index array to X;
1032  int counter = 0;
1033  int x;
1034  // for each individual
1035  for ( unsigned i = 0; i < s; i++ ) {
1036  // Get Next co-ordiated
1037  x = TheArray[ Type ] [ i ]->Supply_m_Location_x();
1038  // fill gaps up to x
1039  while ( counter < x ) IndexArrayX[ Type ] [ counter++ ] = -1;
1040  if ( x == counter ) {
1041  IndexArrayX[ Type ] [ counter++ ] = i;
1042  }
1043  }
1044  // Fill up the rest of the array with -1;
1045  for ( int c = counter; c < 10000; c++ ) {
1046  IndexArrayX[ Type ] [ c ] = -1;
1047  }
1048 }
1049 
1050 //-----------------------------------------------------------------------------
1051 
1055 void Population_Manager::Shuffle( unsigned Type ) {
1056  unsigned s = (unsigned)GetLiveArraySize( Type );
1057  for ( unsigned i = 0; i < s; i++ ) {
1058  TAnimal * temp;
1059  unsigned a = random( s );
1060  unsigned b = random( s );
1061  temp = TheArray[ Type ] [ a ];
1062  TheArray[ Type ] [ a ] = TheArray[ Type ] [ b ];
1063  TheArray[ Type ] [ b ] = temp;
1064  }
1065 }
1066 
1067 //-----------------------------------------------------------------------------
1068 
1074  switch ( BeforeStepActions[ Type ] ) {
1075  case 0:
1076  Shuffle( Type );
1077  break;
1078  case 1:
1079  SortX( Type );
1080  break;
1081  case 2:
1082  SortY( Type );
1083  break;
1084  case 3:
1085  SortXIndex( Type );
1086  break;
1087  case 4: // Do nothing
1088  break;
1089  case 5:
1090  if (g_rand_uni() < double (1/500)) Shuffle( Type );
1091  break;
1092  default:
1093  m_TheLandscape->Warn( "Population_Manager::Shuffle_or_Sort- BeforeStepAction Unknown", NULL );
1094  exit( 1 );
1095  }
1096 }
1097 
1098 //-----------------------------------------------------------------------------
1103  if ( m_TheLandscape->SupplyDayInMonth() == cfg_DayInMonth.value() ) return true;
1104  return false;
1105 }
1106 
1107 //------------------------------------------------------------------------------
1108 
1114  return;
1115 }
1116 //---------------------------------------------------------------------------
1117 
1121 char* Population_Manager::SpeciesSpecificReporting(int a_species, int a_time) {
1122  strcpy(g_str,"");
1123 // Vole Model
1124  if ( a_species == 1 ) {
1125 /*
1126 // The next lines are obselete code from genetic simulations
1127 float MeanNoAlleles;
1128  float MeanHO;
1129  float MeanHE;
1130  unsigned size;
1131 
1132  if ( m_time % ( 365 * 24 * 60 ) == 0 ) // End of year
1133  {
1134 
1135  // empty the GenStruc data
1136  for ( int ProbeNo = 0; ProbeNo < ( int )m_NoProbes; ProbeNo++ ) {
1137  fprintf( m_GeneticsFile, "%d ", m_time );
1138  int TotalSize = 0;
1139  m_AManager->AlFreq->Flush();
1140  for ( unsigned listindex = 0; listindex <= 1; listindex++ ) {
1141  size = 0;
1142  m_AManager->TheGeneticProbe( listindex, ProbeNo, size );
1143  TotalSize += size;
1144  }
1145  if ( TotalSize > 0 ) {
1146  m_AManager->AlFreq->CalcAF();
1147  m_AManager->AlFreq->CalcNoAlleles();
1148  m_AManager->AlFreq->CalcHE();
1149  m_AManager->AlFreq->CalcHO( TotalSize );
1150  }
1151  // Get the means
1152  // For N alleles
1153  int NA = 0;
1154  float NHO = 0;
1155  float NHE = 0;
1156  for ( int l = 0; l < 16; l++ ) {
1157  NA += m_AManager->AlFreq->SupplyNoAlleles( l );
1158  }
1159  MeanNoAlleles = ( float )NA / 16.0;
1160  for ( int l = 0; l < 16; l++ ) {
1161  NHO += m_AManager->AlFreq->SupplyHO( l );
1162  }
1163  MeanHO = NHO / 16.0;
1164  for ( int l = 0; l < 16; l++ ) {
1165  NHE += m_AManager->AlFreq->SupplyHE( l );
1166  }
1167  MeanHE = NHE / 16.0;
1168 
1169  // Do some output
1170  // Output is to two files, the genetics file and the AllelerequencyFile
1171  fprintf( m_GeneticsFile, "%2.2f ", MeanNoAlleles );
1172  fprintf( m_GeneticsFile, "%1.3f ", MeanHO );
1173  fprintf( m_GeneticsFile, "%1.3f ", MeanHE );
1174  for ( int l = 0; l < 16; l++ ) {
1175  fprintf( m_GeneticsFile, "%04i ", m_AManager->AlFreq->SupplyNoAlleles( l ) );
1176  }
1177  for ( int l = 0; l < 16; l++ ) {
1178  fprintf( m_GeneticsFile, "%1.3f ", m_AManager->AlFreq->SupplyHO( l ) );
1179  }
1180  for ( int l = 0; l < 16; l++ ) {
1181  fprintf( m_GeneticsFile, "%1.3f ", m_AManager->AlFreq->SupplyHE( l ) );
1182  }
1183  fprintf( m_GeneticsFile, "\n" );
1184  fprintf( m_AlleleFreqsFile, "%d ", m_time );
1185  for ( int l = 0; l < 16; l++ ) {
1186  for ( int al = 0; al < 4; al++ ) {
1187  fprintf( m_AlleleFreqsFile, "%1.3f ", m_AManager->AlFreq->SupplyAF( l, al ) );
1188  }
1189  }
1190  fprintf( m_AlleleFreqsFile, "\n" );
1191  }
1192  // make sure progress reaches the disk
1193  fflush( m_GeneticsFile );
1194  fflush( m_AlleleFreqsFile );
1195  }
1196 
1197  if ( cfg_UseEasyPop.value()) {
1198  int targettime=a_time % ( 365 * 100 );
1199  if ( ( targettime == 59 ) ||(targettime == 181)||(targettime ==304)||(targettime ==59+365)||(targettime ==181+365)||(targettime ==304+365)) {
1200  for ( unsigned listindex = 0; listindex <= 1; listindex++ ) {
1201  GeneticsResultsOutput( m_EasyPopRes, listindex);
1202  }
1203  fflush( m_EasyPopRes );
1204  }
1205  }
1206 */
1207  /* Three times a year reporting
1208  if ( m_time % 365 == 60 ) ProbeReportNow( m_time );
1209  if ( m_time % 365 == 152 ) ProbeReportNow( m_time );
1210  if ( m_time % 365 == 273 ) ProbeReportNow( m_time ); */
1211  ProbeReport( a_time );
1212 #ifdef __SpecificPesticideEffectsVinclozolinLike__
1213  ImpactProbeReport( a_time );
1214 #endif
1215 #ifdef __WithinOrchardPesticideSim__
1216  ImpactProbeReport( a_time );
1217 #endif
1218 
1219  }
1220  // Skylark Model
1221  else if ( a_species == 0 ) {
1222  int No;
1223  int a_day = a_time % 365;
1224  if ( a_time % 365 == 364 )
1225  {
1226  //Write the Breeding Attempts Probe
1227  int BreedingFemales, YoungOfTheYear, TotalPop, TotalFemales, TotalMales, BreedingAttempts;
1228  No = TheBreedingSuccessProbe( BreedingFemales, YoungOfTheYear, TotalPop, TotalFemales, TotalMales, BreedingAttempts );
1229  float bs = 0;
1230  if ( BreedingFemales > 0 ) {
1231  bs = No / ( float )BreedingFemales; //bs = successful breeding attempt per attempting to breed female
1232  }
1233  BreedingSuccessProbeOutput( bs, BreedingFemales, YoungOfTheYear, TotalPop,
1234  TotalFemales, TotalMales, a_time, BreedingAttempts );
1235  }
1236  if ( a_day == 152 ) {
1237  // Need to fill in the landscape from 1st June, it will change before
1238  // the count of fledgelings is needed
1239  m_TheLandscape->FillVegAreaData();
1240  }
1241  if ( a_day == 197 ) {
1242  for ( int ProbeNo = 0; ProbeNo < m_NoProbes; ProbeNo++ ) {
1243  No = TheFledgelingProbe();
1244  // Do some output
1245  FledgelingProbeOutput( No, a_time );
1246  }
1247  }
1248 #ifdef __SKPOM
1249  // This is clunky but for purposes of validation we want probes throughout the year
1250  // First get the breeding pairs data.
1251  BreedingPairsOutput(a_day);
1252 #else
1253  ProbeReport( a_time );
1254 #endif
1255  }
1256  else return ProbeReport( a_time );
1257  return g_str;
1258 }
1259 //-----------------------------------------------------------------------------
1260 
1261 unsigned int Population_Manager::FarmAnimalCensus(unsigned int a_farm, unsigned int a_typeofanimal)
1262 {
1263  unsigned int No = 0;
1264  unsigned sz = (unsigned)GetLiveArraySize( a_typeofanimal );
1265  for ( unsigned j = 0; j < sz; j++ )
1266  {
1267  if (a_farm == TheArray[ a_typeofanimal ] [ j ]->SupplyFarmOwnerRef()) No++;
1268  }
1269  return No;
1270 }
1271 //-----------------------------------------------------------------------------
1272 
1274 
1275  int No;
1276  char str[100]; // 100 out to be enough!!
1277  strcpy(g_str,"");
1278  for ( int ProbeNo = 0; ProbeNo < m_NoProbes; ProbeNo++ ) {
1279  No = 0;
1280  // See if we need to record/update this one
1281  // if time/months/years ==0 or every time
1282  if ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 3 )
1283  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 2 ) && ( BeginningOfMonth() ) )
1284  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 1 ) && ( Time % 365 == 0 ) ) ) {
1285  // Goes through each area and sends a value to OutputForm
1286  unsigned Index = SupplyListIndexSize();
1287  for ( unsigned listindex = 0; listindex < Index; listindex++ ) {
1288  if ( TheProbe[ ProbeNo ]->m_TargetTypes[ listindex ] )
1289  No += (int) Probe( listindex, TheProbe[ ProbeNo ] );
1290  }
1291  TheProbe[ ProbeNo ]->FileOutput( No, Time, ProbeNo );
1292  sprintf(str," %d ", No );
1293  strcat(g_str,str);
1294  }
1295  }
1296  return g_str;
1297 }
1298 //-----------------------------------------------------------------------------
1299 
1301  int No;
1302  char str[100]; // 100 ought to be enough!!
1303  strcpy(g_str,"");
1304  for ( int ProbeNo = 0; ProbeNo < m_NoProbes; ProbeNo++ ) {
1305  No = 0;
1306  unsigned Index = SupplyListIndexSize();
1307  for ( unsigned listindex = 0; listindex < Index; listindex++ ) {
1308  if ( TheProbe[ ProbeNo ]->m_TargetTypes[ listindex ] ) No += (int) Probe( listindex, TheProbe[ ProbeNo ] );
1309  }
1310  TheProbe[ ProbeNo ]->FileOutput( No, Time, ProbeNo );
1311  sprintf(str," %d ", No );
1312  strcat(g_str,str);
1313  }
1314  return g_str;
1315 }
1316 //-----------------------------------------------------------------------------
1317 
1322 
1323  for ( int ProbeNo = 0; ProbeNo < 1; ProbeNo++ ) {
1324  // See if we need to record/update this one
1325  // if time/months/years ==0 or every time
1326  if ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 3 )
1327  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 2 ) && ( BeginningOfMonth() ) )
1328  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 1 ) && ( a_Time % 365 == 0 ) ) ) {
1329  ImpactedProbe();
1330  }
1331  }
1332 }
1333 
1334 //-----------------------------------------------------------------------------
1335 
1336 //-----------------End of TheArray handling functions
1337 
1338 //-----------------------------------------------------------------------------
1339 //------------------------------------------------------------------------------
1340 
1341 
1346  AnimalPosition SkP;
1347  SkP.m_x = m_Location_x;
1348  SkP.m_y = m_Location_y;
1349  SkP.m_EleType = m_OurLandscape->SupplyElementType( m_Location_x, m_Location_y );
1350  SkP.m_VegType = m_OurLandscape->SupplyVegType( m_Location_x, m_Location_y );
1351  return SkP;
1352 }
1353 
1354 //-----------------------------------------------------------------------------
1355 
1360  return m_OurLandscape->SupplyFarmOwner( m_Location_x, m_Location_y );
1361 }
1362 
1363 //-----------------------------------------------------------------------------
1367 TAnimal::TAnimal( int x, int y, Landscape * L ) {
1368  m_OurLandscape = L;
1369  m_Location_x = x;
1370  m_Location_y = y;
1371 }
1372 
1373 //-----------------------------------------------------------------------------
1374 //-----------------------------------------------------------------------------
1375 
1380 #ifdef __CJTDebug_5
1381  AmAlive = 0xDEADC0DE;
1382 #endif
1383  m_StepDone = false;
1384  m_CurrentStateNo = 0;
1385 }
1386 
1387 //-----------------------------------------------------------------------------
1388 
1389 
1394 #ifdef __CJTDebug_5
1395  AmAlive = 0;
1396 #endif
1397 }
1398 
1399 //-----------------------------------------------------------------------------
1400 
1405  FarmToDo event;
1406  int i = 0;
1407  while ( ( event = ( FarmToDo )m_OurLandscape->SupplyLastTreatment( m_Location_x, m_Location_y, & i ) ) != sleep_all_day ) {
1408  if ( OnFarmEvent( event ) ) break;
1409  }
1410 }
1411 
1412 //-----------------------------------------------------------------------------
1413 
1417 void TAnimal::CheckManagementXY( int x, int y ) {
1418  FarmToDo event;
1419  int i = 0;
1420  while ( ( event = ( FarmToDo )m_OurLandscape->SupplyLastTreatment( x, y, & i ) ) != sleep_all_day ) {
1421  OnFarmEvent( event );
1422  }
1423 }
1424 
1425 //-----------------------------------------------------------------------------
1426 
1428  exit( 1 );
1429 }
1430 
1431 //-----------------------------------------------------------------------------
1432 
1433 #ifdef __CJTDebug_5
1434 void TALMaSSObject::DEADCODEError() {
1435  exit( 0 );
1436 }
1437 
1438 #endif
1439 //-----------------------------------------------------------------------------
1440 // probe_data
1441 //-----------------------------------------------------------------------------
1442 
1447 void probe_data::FileOutput( int No, int time, int ProbeNo ) {
1448  if ( m_FileRecord ) {
1449 
1450  if ( ProbeNo == 0 ) {
1451  // First probe so write the time and a new line
1452  (*m_MyFile) << endl;
1453  (*m_MyFile) << time << '\t' << No;
1454  } else
1455  (*m_MyFile) << '\t' << No ;
1456  }
1457  (*m_MyFile).flush();
1458 }
1459 
1460 //-----------------------------------------------------------------------------
1461 
1462 void probe_data::FileAppendOutput( int No, int time ) {
1463  m_MyFile->open(m_MyFileName,ios::app);
1464  if ( !m_MyFile->is_open() ) {
1465  g_msg->Warn( (MapErrorState)0,"Cannot open file for append: ", m_MyFileName );
1466  exit( 0 );
1467  }
1468  if ( m_FileRecord ) {
1469  (*m_MyFile)<< time << '\t' << No << endl;
1470  }
1471  m_MyFile->close();
1472 }
1473 
1474 //-----------------------------------------------------------------------------
1475 
1480  m_Time = 0;
1481  m_FileRecord = false;
1482  m_MyFile = NULL;
1483 }
1484 
1485 //-----------------------------------------------------------------------------
1486 
1490 ofstream * probe_data::OpenFile( char * Nme ) {
1491  m_MyFile = new ofstream( Nme );
1492  if ( !m_MyFile->is_open() ) {
1493  g_msg->Warn( (MapErrorState)0,"probe_data::OpenFile - Cannot open file: ", Nme );
1494  exit( 0 );
1495  }
1496  strcpy( m_MyFileName, Nme );
1497  return m_MyFile;
1498 }
1499 //-----------------------------------------------------------------------------
1500 
1504 void probe_data::SetFile( ofstream * F ) {
1505  m_MyFile = F;
1506 }
1507 
1508 //-----------------------------------------------------------------------------
1509 
1514 }
1515 //-----------------------------------------------------------------------------
1516 
1517 
1519 {
1520  if (TheArray[l][i]->Supply_m_Location_x() > 10000) return true;
1521  if (TheArray[l][i]->Supply_m_Location_y() > 10000) return true;
1522  if (TheArray[l][i]->Supply_m_Location_x() < 0) return true;
1523  if (TheArray[l][i]->Supply_m_Location_y() < 0) return true;
1524  return false;
1525 }
1526 
cfg_VoleCatastrophe_mortality
static CfgInt cfg_VoleCatastrophe_mortality("VOLE_CATASTROPHE_M", CFG_CUSTOM, 90)
cfg_RipleysOutput_interval
static CfgInt cfg_RipleysOutput_interval("G_RIPLEYSOUTPUT_INTERVAL", CFG_CUSTOM, 1)
Population_Manager::EmptyTheArray
void EmptyTheArray()
Removes all objects from the TheArray by deleting them and clearing TheArray.
Definition: PopulationManager.cpp:962
Population_Manager::SpeciesSpecificReporting
char * SpeciesSpecificReporting(int a_species, int a_time)
Definition: PopulationManager.cpp:1121
CfgStr::value
const char * value(void)
Definition: configurator.h:152
cfg_RipleysOutput_used
CfgBool cfg_RipleysOutput_used("G_RIPLEYSOUTPUT_USED", CFG_CUSTOM, false)
Population_Manager::ProbeFileInput
int ProbeFileInput(char *p_Filename, int p_ProbeNo)
Definition: PopulationManager.cpp:574
Population_Manager::DoLast
virtual void DoLast()
Definition: PopulationManager.cpp:530
cfg_VoleCatastrophe_on
static CfgBool cfg_VoleCatastrophe_on("VOLE_CATASTROPHE_ON", CFG_CUSTOM, false)
g_str
char g_str[255]
Definition: PopulationManager.cpp:129
Population_Manager::CloseTheMonthlyRipleysOutputProbe
void CloseTheMonthlyRipleysOutputProbe()
Definition: PopulationManager.cpp:885
probe_data::m_NoAreas
unsigned m_NoAreas
Definition: populationmanager.h:292
Population_Manager::Shuffle_or_Sort
void Shuffle_or_Sort(unsigned Type)
Definition: PopulationManager.cpp:1073
probe_data::probe_data
probe_data()
Definition: PopulationManager.cpp:1479
probe_data::m_RefFarms
unsigned m_RefFarms[25]
Definition: populationmanager.h:299
TALMaSSObject::~TALMaSSObject
virtual ~TALMaSSObject()
The destructor for TALMaSSObject.
Definition: PopulationManager.cpp:1393
Population_Manager::Population_Manager
Population_Manager(Landscape *L)
Definition: PopulationManager.cpp:221
cfg_pm_eventsize
CfgInt cfg_pm_eventsize("PM_EVENTSIZE", CFG_CUSTOM, 100)
Population_Manager::Probe
virtual float Probe(int ListIndex, probe_data *p_TheProbe)
Definition: PopulationManager.cpp:680
Population_Manager::Run
virtual void Run(int NoTSteps)
Definition: PopulationManager.cpp:337
cfg_pm_eventfrequency
CfgInt cfg_pm_eventfrequency("PM_EVENTFREQUENCY", CFG_CUSTOM, 0)
cfg_ReallyBigOutput_day1
static CfgInt cfg_ReallyBigOutput_day1("G_REALLYBIGOUTPUT_DAY_ONE", CFG_CUSTOM, 1)
probe_data::OpenFile
ofstream * OpenFile(char *Nme)
Definition: PopulationManager.cpp:1490
probe_data::m_Rect
rectangle m_Rect[10]
Definition: populationmanager.h:293
CfgStr
String configurator entry class.
Definition: configurator.h:144
Population_Manager::TheRipleysOutputProbe
virtual void TheRipleysOutputProbe(FILE *a_prb)
Definition: PopulationManager.cpp:918
CompareStateAlive
Function to compare to TAnimal's m_CurrentStateNo to anything but -1.
Definition: PopulationManager.cpp:205
probe_data::SetFile
void SetFile(ofstream *F)
Definition: PopulationManager.cpp:1504
Population_Manager::FindClosest
TAnimal * FindClosest(int x, int y, unsigned Type)
Definition: PopulationManager.cpp:937
probe_data::CloseFile
void CloseFile()
Definition: PopulationManager.cpp:135
cfg_fixed_random_sequence
CfgBool cfg_fixed_random_sequence("G_FIXEDRANDOMSEQUENCE", CFG_CUSTOM, false)
cfg_RipleysOutputFirstYear
static CfgInt cfg_RipleysOutputFirstYear("G_RIPLEYSOUTPUT_FIRSTYEAR", CFG_CUSTOM, 1)
cfg_AOROutput_interval
static CfgInt cfg_AOROutput_interval("G_AORSOUTPUT_INTERVAL", CFG_CUSTOM, 1)
Population_Manager::OpenTheRipleysOutputProbe
bool OpenTheRipleysOutputProbe(string a_NWordFilename)
Definition: PopulationManager.cpp:757
Population_Manager::CloseTheRipleysOutputProbe
virtual void CloseTheRipleysOutputProbe()
Definition: PopulationManager.cpp:874
AnimalPosition::m_x
unsigned m_x
Definition: populationmanager.h:172
rectangle::m_y1
unsigned m_y1
Definition: populationmanager.h:101
Population_Manager::CheckXY
bool CheckXY(int l, int i)
Debug method to test for out of bounds coordinates.
Definition: PopulationManager.cpp:1518
CompareY
Function to compare to TAnimal's m_Location_y.
Definition: PopulationManager.cpp:157
TAnimal::Supply_m_Location_x
int Supply_m_Location_x()
Definition: populationmanager.h:213
probe_data::m_NoVegTypes
unsigned m_NoVegTypes
Definition: populationmanager.h:295
probe_data::~probe_data
~probe_data()
Definition: PopulationManager.cpp:1513
Population_Manager::SortXIndex
void SortXIndex(unsigned Type)
Definition: PopulationManager.cpp:1027
Population_Manager::ImpactProbeReport
void ImpactProbeReport(int a_Time)
Definition: PopulationManager.cpp:1321
rectangle::m_x2
unsigned m_x2
Definition: populationmanager.h:102
Population_Manager::SortState
void SortState(unsigned Type)
Definition: PopulationManager.cpp:999
g_msg
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:41
TAnimal::TAnimal
TAnimal(int x, int y, Landscape *L)
Definition: PopulationManager.cpp:1367
cfg_DayInMonth
static CfgInt cfg_DayInMonth("PRB_DAYINMONTH", CFG_CUSTOM, 1)
probe_data::FileOutput
void FileOutput(int No, int time, int ProbeNo)
Definition: PopulationManager.cpp:1447
probe_data::m_RefEle
TTypesOfLandscapeElement m_RefEle[25]
Definition: populationmanager.h:298
Landscape
The landscape class containing all environmental and topographical data.
Definition: landscape.h:112
cfg_AOROutput_day
static CfgInt cfg_AOROutput_day("G_AOROUTPUT_DAY", CFG_CUSTOM, 60)
cfg_ReallyBigOutput_interval
static CfgInt cfg_ReallyBigOutput_interval("G_REALLYBIGOUTPUT_INTERVAL", CFG_CUSTOM, 1)
probe_data::FileAppendOutput
void FileAppendOutput(int No, int time)
Definition: PopulationManager.cpp:1462
cfg_ReallyBigOutput_day2
static CfgInt cfg_ReallyBigOutput_day2("G_REALLYBIGOUTPUT_DAY_TWO", CFG_CUSTOM, 91)
probe_data::m_RefVeg
TTypesOfVegetation m_RefVeg[25]
Definition: populationmanager.h:297
Landscape::SupplySimAreaHeight
int SupplySimAreaHeight(void)
Definition: landscape.h:1637
Population_Manager::TheReallyBigOutputProbe
virtual void TheReallyBigOutputProbe()
Definition: PopulationManager.cpp:913
Population_Manager::DoBefore
virtual void DoBefore()
Definition: PopulationManager.cpp:328
CfgBool
Bool configurator entry class.
Definition: configurator.h:127
cfg_ReallyBigOutput_day3
static CfgInt cfg_ReallyBigOutput_day3("G_REALLYBIGOUTPUT_DAY_THREE", CFG_CUSTOM, 182)
Population_Manager::LamdaDumpOutput
void LamdaDumpOutput()
Definition: PopulationManager.cpp:657
TAnimal
The base class for all ALMaSS animal classes.
Definition: populationmanager.h:205
CompareX
Function to compare to TAnimal's m_Location_x.
Definition: PopulationManager.cpp:146
AnimalPosition
A class defining an animals position.
Definition: populationmanager.h:168
cfg_VoleCatastrophe_interval
static CfgInt cfg_VoleCatastrophe_interval("VOLE_CATASTROPHE_I", CFG_CUSTOM, 365 *5)
Population_Manager::ImpactedProbe
virtual void ImpactedProbe()
Definition: PopulationManager.cpp:672
MapErrorMsg::Warn
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:59
probe_data
Data structure to hold & output probe data probe data is designed to be used to return the number of ...
Definition: populationmanager.h:283
cfg_RipleysOutputMonthly_used
CfgBool cfg_RipleysOutputMonthly_used("G_RIPLEYSOUTPUTMONTHLY_USED", CFG_CUSTOM, false)
cfg_ReallyBigOutput_used
CfgBool cfg_ReallyBigOutput_used("G_REALLYBIGOUTPUT_USED", CFG_CUSTOM, false)
TAnimal::CheckManagementXY
void CheckManagementXY(int x, int y)
Definition: PopulationManager.cpp:1417
rectangle::m_y2
unsigned m_y2
Definition: populationmanager.h:103
TAnimal::SupplyPosition
AnimalPosition SupplyPosition()
Definition: PopulationManager.cpp:1345
cfg_ReallyBigOutput_filename
static CfgStr cfg_ReallyBigOutput_filename("G_REALLYBIGOUTPUT_FILENAME", CFG_CUSTOM, "ReallyBigOutput.txt")
MapErrorState
MapErrorState
Definition: maperrormsg.h:33
cfg_ReallyBigOutput_day4
static CfgInt cfg_ReallyBigOutput_day4("G_REALLYBIGOUTPUT_DAY_FOUR", CFG_CUSTOM, 274)
AnimalPosition::m_EleType
TTypesOfLandscapeElement m_EleType
Definition: populationmanager.h:174
Population_Manager::ProbeReportTimed
char * ProbeReportTimed(int a_time)
Definition: PopulationManager.cpp:1300
AnimalPosition::m_y
unsigned m_y
Definition: populationmanager.h:173
Population_Manager::DoFirst
virtual void DoFirst()
Definition: PopulationManager.cpp:320
Population_Manager::FarmAnimalCensus
unsigned int FarmAnimalCensus(unsigned int a_farm, unsigned int a_typeofanimal)
Definition: PopulationManager.cpp:1261
CFG_CUSTOM
Definition: configurator.h:60
cfg_RipleysOutput_filename
static CfgStr cfg_RipleysOutput_filename("G_RIPLEYSOUTPUT_FILENAME", CFG_CUSTOM, "RipleysOutput.txt")
Population_Manager::ProbeReport
char * ProbeReport(int a_time)
Definition: PopulationManager.cpp:1273
Population_Manager::OpenTheReallyBigProbe
bool OpenTheReallyBigProbe()
Definition: PopulationManager.cpp:859
cfg_RipleysOutput_day
static CfgInt cfg_RipleysOutput_day("G_RIPLEYSOUTPUT_DAY", CFG_CUSTOM, 60)
CompareStateDead
Function to compare to TAnimal's m_CurrentStateNo to -1.
Definition: PopulationManager.cpp:194
Population_Manager::DisplayLocations
virtual void DisplayLocations()
Definition: PopulationManager.cpp:560
Population_Manager::OpenTheMonthlyRipleysOutputProbe
bool OpenTheMonthlyRipleysOutputProbe()
Definition: PopulationManager.cpp:778
TAnimal::SupplyFarmOwnerRef
unsigned SupplyFarmOwnerRef()
Definition: PopulationManager.cpp:1359
cfg_ReallyBigOutputFirstYear
static CfgInt cfg_ReallyBigOutputFirstYear("G_REALLYBIGOUTPUT_FIRSTYEAR", CFG_CUSTOM, 1)
TALMaSSObject::OnArrayBoundsError
void OnArrayBoundsError()
Used for debugging only, tests basic object properties.
Definition: PopulationManager.cpp:1427
WARN_FILE
Definition: maperrormsg.h:37
TListOfAnimals
vector< TAnimal * > TListOfAnimals
Definition: populationmanager.h:50
rectangle::m_x1
unsigned m_x1
Definition: populationmanager.h:100
CfgInt
Integer configurator entry class.
Definition: configurator.h:87
TALMaSSObject::GetCurrentStateNo
int GetCurrentStateNo()
Returns the current state number.
Definition: populationmanager.h:121
Population_Manager::SortStateR
void SortStateR(unsigned Type)
Definition: PopulationManager.cpp:1008
Population_Manager::BeginningOfMonth
bool BeginningOfMonth()
Definition: PopulationManager.cpp:1102
Farm
The base class for all farm types.
Definition: farm.h:767
Population_Manager::SortX
void SortX(unsigned Type)
Definition: PopulationManager.cpp:979
TAnimal::CheckManagement
void CheckManagement(void)
Definition: PopulationManager.cpp:1404
probe_data::m_NoEleTypes
unsigned m_NoEleTypes
Definition: populationmanager.h:294
CompareState
Function to compare to TAnimal's Current behavioural state.
Definition: PopulationManager.cpp:171
TAnimal::Supply_m_Location_y
int Supply_m_Location_y()
Definition: populationmanager.h:216
probe_data::m_NoFarms
unsigned m_NoFarms
Definition: populationmanager.h:296
Population_Manager::PartitionLiveDead
unsigned PartitionLiveDead(unsigned Type)
Definition: PopulationManager.cpp:1015
CfgInt::value
int value(void)
Definition: configurator.h:98
Population_Manager::~Population_Manager
virtual ~Population_Manager(void)
Definition: PopulationManager.cpp:266
Population_Manager::StepFinished
virtual bool StepFinished()
Overrides the population manager StepFinished - there is no chance that hunters do not finish a step ...
Definition: PopulationManager.cpp:498
cfg_CatastropheEventStartYear
CfgInt cfg_CatastropheEventStartYear("PM_CATASTROPHEEVENTSTARTYEAR", CFG_CUSTOM, 99999)
Population_Manager::DoAlmostLast
virtual void DoAlmostLast()
Definition: PopulationManager.cpp:522
Population_Manager::CloseTheReallyBigOutputProbe
virtual void CloseTheReallyBigOutputProbe()
Definition: PopulationManager.cpp:904
Population_Manager::Shuffle
void Shuffle(unsigned Type)
Definition: PopulationManager.cpp:1055
FarmToDo
FarmToDo
Definition: treatment.h:31
CompareStateR
Function to compare to TAnimal's m_CurrentStateNo.
Definition: PopulationManager.cpp:183
Population_Manager::OpenTheAOROutputProbe
void OpenTheAOROutputProbe(string a_AORFilename)
Definition: PopulationManager.cpp:771
sleep_all_day
Definition: treatment.h:33
CfgBool::value
bool value(void)
Definition: configurator.h:135
Population_Manager::LOG
void LOG(const char *fname)
Definition: PopulationManager.cpp:294
Population_Manager::TheAOROutputProbe
virtual void TheAOROutputProbe()
Definition: PopulationManager.cpp:924
g_rand_uni
boost::variate_generator< base_generator_type &, boost::uniform_real<> > g_rand_uni
TAnimal::WhatState
virtual int WhatState()
Definition: populationmanager.h:263
Population_Manager::SortY
void SortY(unsigned Type)
Definition: PopulationManager.cpp:989
TALMaSSObject::TALMaSSObject
TALMaSSObject()
The constructor for TALMaSSObject.
Definition: PopulationManager.cpp:1379
cfg_AOROutput_used
CfgBool cfg_AOROutput_used("G_AOROUTPUT_USED", CFG_CUSTOM, false)
Population_Manager::Catastrophe
virtual void Catastrophe()
Definition: PopulationManager.cpp:1109
cfg_AOROutputFirstYear
static CfgInt cfg_AOROutputFirstYear("G_AOROUTPUT_FIRSTYEAR", CFG_CUSTOM, 1)
Population_Manager::DoAfter
virtual void DoAfter()
Definition: PopulationManager.cpp:513
AnimalPosition::m_VegType
TTypesOfVegetation m_VegType
Definition: populationmanager.h:175