ALMaSS Rabbit ODdox  1.00
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 #include "../Landscape/ls.h"
49 #include "../BatchALMaSS/PopulationManager.h"
50 #include "../GooseManagement/GooseMemoryMap.h"
51 #include "../GooseManagement/Goose_Base.h"
52 #include "../BatchALMaSS/CurveClasses.h"
53 #include "../Hunters/Hunters_all.h"
54 #include "../GooseManagement/Goose_Population_Manager.h"
55 #ifdef __ALMASS_VISUAL
56 #include "wx/wx.h"
57 #include "wx/spinctrl.h"
58 #include "../GUI/ALMaSS_GUI.h"
59 #endif
60 #include "../BatchALMaSS/BoostRandomGenerators.h"
61 
62 extern boost::variate_generator<base_generator_type&, boost::uniform_real<> > g_rand_uni;
63 
64 using namespace std;
65 
66 //------------------------------------------------------------------------------
67 
68 
69 /* Species code
70 #ifdef StartAnimal
71  #include "../Skylark/Skylarks_All.h"
72 #endif
73 #ifdef PartridgeModel
74  #include "../Partridge/Partridge_All.h"
75 #endif
76 #ifdef SkylarkModel
77  #include "../Skylark/Skylarks_All.h"
78 #endif
79 #ifdef YellowhammerModel
80  #include "../Yellowhammer/Yellowhammer_All.h"
81 #endif
82 #ifdef SpiderModel
83  #include "../Spider/Spider_all.h"
84 #endif
85 #ifdef BembidionModel
86  #include "../Bembidion/Bembidion_all.h"
87 #endif
88 #ifdef RoeDeerModel
89  #include "../RoeDeer/Roe_all.h"
90 #endif
91 #ifdef FieldVoleModel
92  #include "../Vole/GeneticMaterial.h"
93  #include "../Vole/Predators.h"
94  #include "../Vole/Vole_all.h"
95 #endif
96 */
97 #define _CRTDBG_MAP_ALLOC
98 
99 static CfgStr cfg_RipleysOutput_filename( "G_RIPLEYSOUTPUT_FILENAME", CFG_CUSTOM, "RipleysOutput.txt" );
100 static CfgStr cfg_ReallyBigOutput_filename( "G_REALLYBIGOUTPUT_FILENAME", CFG_CUSTOM, "ReallyBigOutput.txt" );
101 CfgBool cfg_RipleysOutputMonthly_used( "G_RIPLEYSOUTPUTMONTHLY_USED", CFG_CUSTOM, false );
102 CfgBool cfg_RipleysOutput_used( "G_RIPLEYSOUTPUT_USED", CFG_CUSTOM, true );
103 CfgBool cfg_ReallyBigOutput_used( "G_REALLYBIGOUTPUT_USED", CFG_CUSTOM, false );
104 CfgBool cfg_fixed_random_sequence("G_FIXEDRANDOMSEQUENCE",CFG_CUSTOM,false);
105 static CfgInt cfg_RipleysOutput_interval( "G_RIPLEYSOUTPUT_INTERVAL", CFG_CUSTOM, 1 );
106 static CfgInt cfg_RipleysOutput_day( "G_RIPLEYSOUTPUT_DAY", CFG_CUSTOM, 60 );
107 static CfgInt cfg_RipleysOutputFirstYear("G_RIPLEYSOUTPUT_FIRSTYEAR",CFG_CUSTOM,1);
108 static CfgInt cfg_ReallyBigOutput_interval( "G_REALLYBIGOUTPUT_INTERVAL", CFG_CUSTOM, 1 );
109 static CfgInt cfg_ReallyBigOutput_day1( "G_REALLYBIGOUTPUT_DAY_ONE", CFG_CUSTOM, 1 );
110 static CfgInt cfg_ReallyBigOutput_day2( "G_REALLYBIGOUTPUT_DAY_TWO", CFG_CUSTOM, 91 );
111 static CfgInt cfg_ReallyBigOutput_day3( "G_REALLYBIGOUTPUT_DAY_THREE", CFG_CUSTOM, 182 );
112 static CfgInt cfg_ReallyBigOutput_day4( "G_REALLYBIGOUTPUT_DAY_FOUR", CFG_CUSTOM, 274 );
113 static CfgInt cfg_ReallyBigOutputFirstYear("G_REALLYBIGOUTPUT_FIRSTYEAR",CFG_CUSTOM,1);
114 static CfgInt cfg_CipeGridSize("G_CIPEGRIDSIZE",CFG_CUSTOM,500);
115 CfgStr cfg_CIPEGridOutput_filename( "G_CIPEGRIDOUTPUT_FILENAME", CFG_CUSTOM, "CIPEGridOutput.txt" );
116 CfgStr cfg_CIPEGridOutput_filenameB( "G_CIPEGRIDOUTPUT_FILENAME_B", CFG_CUSTOM, "CIPEGridOutputB.txt" );
117 CfgBool cfg_CIPEGridOutput_used( "G_CIPEGRIDOUTPUT_USED", CFG_CUSTOM, false );
118 CfgInt cfg_CIPEGridOutput_Interval( "G_CIPEGRIDOUTPUT_INTERVAL", CFG_CUSTOM, 1 );
119 CfgInt cfg_CIPEGridOutput_day( "G_CIPEGRIDOUTPUT_DAY", CFG_CUSTOM, 1 );
120 CfgInt cfg_CIPEGridOutput_day_b( "G_CIPEGRIDOUTPUT_DAY_B", CFG_CUSTOM, 270 );
121 // Catastrophe config variables
122 CfgInt cfg_CatastropheEventStartYear("PM_CATASTROPHEEVENTSTARTYEAR",CFG_CUSTOM,99999); // this means unless this is altered it events will not happen
123 CfgInt cfg_pm_eventfrequency( "PM_EVENTFREQUENCY", CFG_CUSTOM, 0 ); // every X years
124 CfgInt cfg_pm_eventsize( "PM_EVENTSIZE", CFG_CUSTOM, 100 ); // The percentage change in population size, 100=no change
125 static CfgInt cfg_DayInMonth( "PRB_DAYINMONTH", CFG_CUSTOM, 1 );
126 static CfgBool cfg_VoleCatastrophe_on( "VOLE_CATASTROPHE_ON", CFG_CUSTOM, false );
127 static CfgInt cfg_VoleCatastrophe_interval( "VOLE_CATASTROPHE_I", CFG_CUSTOM, 365 * 5 );
128 static CfgInt cfg_VoleCatastrophe_mortality( "VOLE_CATASTROPHE_M", CFG_CUSTOM, 90 );
129 
130 char g_str[255];
131 
132 //---------------------------------------------------------------------------
133 // Population Manager
134 //---------------------------------------------------------------------------
135 //---------------------------------------------------------------------------
137  if ( m_MyFile != NULL ) {
138  if (m_MyFile->is_open()) m_MyFile->close();
139  delete m_MyFile;
140  }
141 };
142 
147 class CompareX {
148 public:
149  bool operator() ( TAnimal * A1, TAnimal * A2 ) const {
150  return (A1->Supply_m_Location_x() < A2->Supply_m_Location_x());
151  }
152 };
153 
158 class CompareY {
159 public:
160  bool operator() ( TAnimal * A1, TAnimal * A2 ) const {
161  return (A1->Supply_m_Location_y() < A2->Supply_m_Location_y());
162  }
163 };
164 
173 public:
174  bool operator() ( TAnimal * A1, TAnimal * A2 ) const {
175  return (A1->WhatState() < A2->WhatState());
176  }
177 };
178 
179 
185 public:
186  bool operator() (TAnimal * A1, TAnimal * A2) const {
187  return (A1->GetCurrentStateNo() > A2->GetCurrentStateNo());
188  }
189 };
190 
196 public:
197  bool operator() (TAnimal * A1) const {
198  return (A1->GetCurrentStateNo() == -1);
199  }
200 };
201 
207 public:
208  bool operator() (TAnimal * A1) const {
209  return (A1->GetCurrentStateNo() != -1);
210  }
211 };
212 
213 
214 
215 //---------------------------------------------------------------------------
216 //---------------------------------------------------------------------------
217 
218 
223  // Keep a pointer to the landscape this population is in
224  m_TheLandscape = L;
225  // create 10 empty arrays as default, excess can be removed in descendent classes
226  TListOfAnimals alist;
227  TheArray.insert( TheArray.end(), 10, alist );
228  // Set the simulation bounds
229  SimH = m_TheLandscape->SupplySimAreaHeight();
230  SimW = m_TheLandscape->SupplySimAreaWidth();
231  SimHH = m_TheLandscape->SupplySimAreaHeight()/2;
232  SimWH = m_TheLandscape->SupplySimAreaWidth()/2;
233  for (int i = 0; i < 10; i++) {
234  // Set default BeforeStepActions
235  BeforeStepActions[ i ] = 0;
236  m_ListNames[ i ] = "Unknown";
237  m_LiveArraySize.push_back( 0 );
238  }
239  // modify this if they need to in descendent classes
240  StateNamesLength = 0; // initialise this variable.
241  m_catastrophestartyear=-1; // Default is don't do this
242 #ifdef __LAMBDA_RECORD
243  ofstream fout("LambdaGridOuput.txt", ios_base::out); // open for writing
244  fout.close();
245  LamdaClear();
246 #endif
248  OpenTheMonthlyRipleysOutputProbe();
249  }
250  m_cipegridsize = cfg_CipeGridSize.value();
251  m_cgridcountwidth = SimW/m_cipegridsize;
252  m_cgridcount = new int[ m_cgridcountwidth * m_cgridcountwidth ];
253  m_gridcountsize[0] = 50;
254  m_gridcountsize[1] = 100;
255  m_gridcountsize[2] = 200;
256  m_gridcountsize[3] = 500;
257  m_gridcountwidth[0] = SimW/50;
258  m_gridcountwidth[1] = SimW/100;
259  m_gridcountwidth[2] = SimW/200;
260  m_gridcountwidth[3] = SimW/500;
261  m_gridcount[0] = new int[ m_gridcountwidth[0] * m_gridcountwidth[0] ];
262  m_gridcount[1] = new int[ m_gridcountwidth[1] * m_gridcountwidth[1] ];
263  m_gridcount[2] = new int[ m_gridcountwidth[2] * m_gridcountwidth[2] ];
264  m_gridcount[3] = new int[ m_gridcountwidth[3] * m_gridcountwidth[3] ];
265  // Ensure the GUI pointer is NULL in case we are not in GUI mode
266 #ifdef __ALMASS_VISUAL
267  m_MainForm = NULL;
268 #endif
269  m_SeasonNumber = 0; // Ensure that we start in season number 0. Season number is incremented at day in year 183
270 }
271 //---------------------------------------------------------------------------
272 
277  // clean-up
278  for ( unsigned i = 0; i < TheArray.size(); i++ ) {
279  // if objects in the list need to be deleted:
280  for ( unsigned j = 0; j < (unsigned) GetLiveArraySize(i); j++ ) {
281  delete TheArray[ i ] [ j ];
282  }
283  // empty the array
284  TheArray[ i ].clear();
285  // --
286  }
287  if ( cfg_RipleysOutput_used.value() ) {
288  CloseTheRipleysOutputProbe();
289  fclose( NWordOutputPrb );
290  }
292  CloseTheMonthlyRipleysOutputProbe();
293  }
295  CloseTheReallyBigOutputProbe();
296  }
297  for (int d=0; d<4; d++) delete [] m_gridcount[d];
298  delete [] m_cgridcount;
299 }
300 
301 //-----------------------------------------------------------------------------
302 
306 void Population_Manager::LOG( const char* fname ) {
307  FILE * PFile = fopen(fname, "w" );
308  if (PFile) {
309  m_TheLandscape->Warn("PopulationManager::LOG - Could not open file ",fname);
310  exit(0);
311  }
312  AnimalPosition AP;
313  for ( unsigned listindex = 0; listindex < TheArray.size(); listindex++ ) {
314 #ifndef __BCB__
315  fprintf( PFile, "%s :\n", m_ListNames[ listindex ] );
316 #else
317  fprintf( PFile, "%s :\n", m_ListNames[ listindex ].c_str() );
318 #endif
319  for ( unsigned j = 0; j < (unsigned) m_LiveArraySize[listindex]; j++ ) {
320  AP = TheArray[ listindex ] [ j ]->SupplyPosition();
321  fprintf( PFile, "%i %i %i\n", j, AP.m_x, AP.m_y );
322  }
323  }
324  fclose( PFile );
325 }
326 
327 //-----------------------------------------------------------------------------
328 
333 }
334 
335 //---------------------------------------------------------------------------
336 
341 }
342 
343 //---------------------------------------------------------------------------
344 
349 void Population_Manager::Run( int NoTSteps ) {
353  for ( int TSteps = 0; TSteps < NoTSteps; TSteps++ )
354  {
355  unsigned size2;
356  unsigned size1 = (unsigned) TheArray.size();
361  for ( unsigned listindex = 0; listindex < size1; listindex++ )
362  {
363  // Must check each object in the list for m_CurrentStateNo==-1
364  m_LiveArraySize[listindex] = PartitionLiveDead(listindex);
365  }
366 #ifdef __ALMASS_VISUAL
367  if (m_MainForm!=NULL)
368  {
369  int n = 0;
370  for ( unsigned listindex = 0; listindex < size1; listindex++ ) n += (int) m_LiveArraySize[ listindex ];
372  //if (n>0) DisplayLocations();
373  }
374 #endif
375  // begin step actions ...
376  // set all stepdone to false.... is this really necessary??
377  for ( unsigned listindex = 0; listindex < size1; listindex++ )
378  {
379  size2 = (unsigned) GetLiveArraySize(listindex);
380  for ( unsigned j = 0; j < size2; j++ )
381  {
382  TheArray[ listindex ] [ j ]->SetStepDone( false );
383  }
384  }
385 
386  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
387  // Call the Shuffle/Sort procedures
388  Shuffle_or_Sort( listindex );
389  }
390  // Need to check if Ripleys Statistic needs to be saved
391  if ( cfg_RipleysOutput_used.value() ) {
392  int Year = m_TheLandscape->SupplyYearNumber();
393  if (Year>=cfg_RipleysOutputFirstYear.value()) {
394  if ( Year % cfg_RipleysOutput_interval.value() == 0 ) {
395  int day = m_TheLandscape->SupplyDayInYear();
396  if ( cfg_RipleysOutput_day.value() == day ) {
397  // Do the Ripley Probe
398  TheRipleysOutputProbe( RipleysOutputPrb );
399  TheNWordOutputProbe(); // These two go hand in hand.
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  // Need to check if CIPEGrid Statistic needs to be saved
457  if ( cfg_CIPEGridOutput_used.value() ) {
458  int Year = m_TheLandscape->SupplyYearNumber();
459  if ( Year % cfg_CIPEGridOutput_Interval.value() == 0 ) {
460  int day = m_TheLandscape->SupplyDayInYear();
461  if ( cfg_CIPEGridOutput_day.value() == day ) {
462  // Do the Ripley Probe
463  TheCIPEGridOutputProbe();
464  }
465  }
466  }
467  int yr=m_TheLandscape->SupplyYearNumber();
468  if ( yr > cfg_CatastropheEventStartYear.value() ) {
469  if (m_catastrophestartyear==-1) m_catastrophestartyear=yr;
470  Catastrophe(); // This method must be overidden in descendent classes
471  }
472  DoFirst();
473  // call the begin-step-method of all objects
474  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
475  size2 = (unsigned)GetLiveArraySize( listindex );
476  for (unsigned j = 0; j < size2; j++)
477  TheArray[ listindex ] [ j ]->BeginStep();
478  }
479  DoBefore();
480  // call the step-method of all objects
481  do {
482  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
483  size2 = (unsigned)GetLiveArraySize( listindex );
484  for (unsigned j = 0; j < size2; j++) {
485  TheArray[ listindex ] [ j ]->Step();
486  }
487  } // for listindex
488  } while ( !StepFinished() );
489  DoAfter();
490  // call the end-step-method of all objects
491  for ( unsigned listindex = 0; listindex < size1; listindex++ ) {
492  size2 = (unsigned)GetLiveArraySize( listindex );
493  for (unsigned j = 0; j < size2; j++) {
494  TheArray[ listindex ] [ j ]->EndStep();
495  }
496  }
497  // ----------------
498  // end of this step actions
499  // For each animal list
500  DoAlmostLast();
501 
502 DoLast();
503  } // End of time step loop
504 }
505 
506 //---------------------------------------------------------------------------
507 
510  for ( unsigned listindex = 0; listindex < TheArray.size(); listindex++ ) {
511  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( listindex ); j++) {
512  if ( TheArray[ listindex ] [ j ]->GetStepDone() == false ) {
513  return false;
514  }
515  }
516  }
517  return true;
518 }
519 //---------------------------------------------------------------------------
520 
525  //TODO: Add your source code here
526 }
527 
528 //---------------------------------------------------------------------------
529 
534  //TODO: Add your source code here
535 }
536 
537 //---------------------------------------------------------------------------
542 
543 /*
544 #ifdef __UNIX__
545  for ( unsigned i = 0; i < 100; i++ ) {
546  StateList.n[ i ] = 0;
547  }
548 #else
549  // Zero results
550  for ( unsigned listindex = 0; listindex < m_ListNameLength; listindex++ ) {
551  for ( unsigned i = 0; i < 100; i++ ) {
552  Counts[ listindex ] [ i ] = 0;
553  }
554  }
555 #endif
556  // How many animals in each state?
557  for ( unsigned listindex = 0; listindex < TheArray.size(); listindex++ ) {
558  unsigned size = (unsigned) TheArray[ listindex ].size();
559  for ( unsigned j = 0; j < size; j++ ) {
560 #ifdef __UNIX__
561  StateList.n[ TheArray[ listindex ] [ j ]->WhatState() ] ++;
562 #else
563  Counts[ listindex ] [ TheArray[ listindex ] [ j ]->WhatState() ] ++;
564 #endif
565  }
566  }
567 */
568 }
569 //---------------------------------------------------------------------------
570 
572 {
576 #ifdef __ALMASS_VISUAL
577  m_MainForm->UpdateGraphics();
578 #endif
579 }
580 //---------------------------------------------------------------------------
581 
585 int Population_Manager::ProbeFileInput( char * p_Filename, int p_ProbeNo ) {
586  FILE * PFile;
587  int data = 0;
588  int data2 = 0;
589  char S[ 255 ];
590  PFile = fopen(p_Filename, "r" );
591  if ( !PFile ) {
592  m_TheLandscape->Warn( "Population Manager - cannot open Probe File ", p_Filename );
593  exit( 0 );
594  }
595  fgets( S, 255, PFile ); // dummy line
596  fgets( S, 255, PFile ); // dummy line
597  fscanf( PFile, "%d\n", & data ); // Reporting interval
598  TheProbe[ p_ProbeNo ]->m_ReportInterval = data;
599  fgets( S, 255, PFile ); // dummy line
600  fscanf( PFile, "%d\n", & data ); // Write to file
601  if ( data == 0 ) TheProbe[ p_ProbeNo ]->m_FileRecord = false; else
602  TheProbe[ p_ProbeNo ]->m_FileRecord = true;
603  fgets( S, 255, PFile ); // dummy line
604  for ( int i = 0; i < 10; i++ ) {
605  fscanf( PFile, "%d", & data );
606  if ( data > 0 ) TheProbe[ p_ProbeNo ]->m_TargetTypes[ i ] = true; else
607  TheProbe[ p_ProbeNo ]->m_TargetTypes[ i ] = false;
608  }
609 
610  fgets( S, 255, PFile ); // dummy line
611  fgets( S, 255, PFile ); // dummy line
612  fscanf( PFile, "%d", & data );
613  TheProbe[ p_ProbeNo ]->m_NoAreas = data;
614  fgets( S, 255, PFile ); // dummy line
615  fgets( S, 255, PFile ); // dummy line
616  fscanf( PFile, "%d", & data2 ); // No References areas
617  fgets( S, 255, PFile ); // dummy line
618  fgets( S, 255, PFile ); // dummy line
619  fscanf( PFile, "%d", & data ); // Type reference for probe
620  if ( data == 1 ) TheProbe[ p_ProbeNo ]->m_NoEleTypes = data2; else
621  TheProbe[ p_ProbeNo ]->m_NoEleTypes = 0;
622  if ( data == 2 ) TheProbe[ p_ProbeNo ]->m_NoVegTypes = data2; else
623  TheProbe[ p_ProbeNo ]->m_NoVegTypes = 0;
624  if ( data == 3 ) TheProbe[ p_ProbeNo ]->m_NoFarms = data2; else
625  TheProbe[ p_ProbeNo ]->m_NoFarms = 0;
626  fgets( S, 255, PFile ); // dummy line
627  fgets( S, 255, PFile ); // dummy line
628  // Now read in the areas data
629  for ( int i = 0; i < 10; i++ ) {
630  fscanf( PFile, "%d", & data );
631  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_x1 = data;
632  fscanf( PFile, "%d", & data );
633  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_y1 = data;
634  fscanf( PFile, "%d", & data );
635  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_x2 = data;
636  fscanf( PFile, "%d", & data );
637  TheProbe[ p_ProbeNo ]->m_Rect[ i ].m_y2 = data;
638  }
639  fgets( S, 255, PFile ); // dummy line
640  fgets( S, 255, PFile ); // dummy line
641  if ( TheProbe[ p_ProbeNo ]->m_NoVegTypes > 0 ) {
642  for ( int i = 0; i < 25; i++ ) {
643  fscanf( PFile, "%d", & data );
644  if ( data != 999 )
645  TheProbe[ p_ProbeNo ]->m_RefVeg[ i ] = m_TheLandscape->TranslateVegTypes( data );
646  }
647  } else if ( TheProbe[ p_ProbeNo ]->m_NoFarms > 0 ) {
648  for ( int i = 0; i < 25; i++ ) {
649  fscanf( PFile, "%d", & data );
650  if ( data != 999 )
651  TheProbe[ p_ProbeNo ]->m_RefFarms[ i ] = data;
652  }
653  } else {
654  for ( int i = 0; i < 25; i++ ) {
655  fscanf( PFile, "%d", & data );
656  if ( data != 999 ) TheProbe[ p_ProbeNo ]->m_RefEle[ i ] = m_TheLandscape->TranslateEleTypes( data );
657  }
658  }
659  fclose( PFile );
660  return data2; // number of data references
661 }
662 
663 //-----------------------------------------------------------------------------
664 
669  ofstream fout("LambdaGridOuput.txt", ios_base::app); // open for writing
670  for (int i=0; i<257; i++ ) {
671  for (int j=0; j<257; j++) {
672  fout << lamdagrid[0][i][j] << "\t" << lamdagrid[1][i][j] << endl;
673  }
674  }
675  //fout << "NEXT" << endl;
676  fout.close();
677 }
678 //-----------------------------------------------------------------------------
679 
684 
685 }
686 //-----------------------------------------------------------------------------
687 
691 float Population_Manager::Probe( int ListIndex, probe_data * p_TheProbe ) {
692  // Counts through the list and goes through each area to see if the animal
693  // is standing there and if the farm, veg or element conditions are met
694  AnimalPosition Sp;
695  float NumberSk = 0;
696  // Four possibilites
697  // either NoVegTypes or NoElementTypes or NoFarmTypes is >0 or all==0
698  if ( p_TheProbe->m_NoFarms != 0 ) {
699  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( ListIndex ); j++) {
700  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
701  unsigned Farm = TheArray[ ListIndex ] [ j ]->SupplyFarmOwnerRef();
702  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
703  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
704  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) )
705  for ( unsigned k = 0; k < p_TheProbe->m_NoFarms; k++ ) {
706  if ( p_TheProbe->m_RefFarms[ k ] == Farm )
707  NumberSk++; // it is in the square so increment number
708  }
709 
710  }
711  }
712  } else if ( p_TheProbe->m_NoEleTypes != 0 ) {
713  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( ListIndex ); j++) {
714  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
715  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
716  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
717  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) )
718  for ( unsigned k = 0; k < p_TheProbe->m_NoEleTypes; k++ ) {
719  if ( p_TheProbe->m_RefEle[ k ] == Sp.m_EleType )
720  NumberSk++; // it is in the square so increment number
721  }
722  }
723  }
724  } else {
725  if ( p_TheProbe->m_NoVegTypes != 0 ) {
726  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( ListIndex ); j++) {
727  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
728 
729  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
730  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
731  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) ) {
732  for ( unsigned k = 0; k < p_TheProbe->m_NoVegTypes; k++ ) {
733  if ( p_TheProbe->m_RefVeg[ k ] == Sp.m_VegType )
734  NumberSk++; // it is in the square so increment number
735  }
736  }
737  }
738  }
739  } else // both must be zero
740  {
741  unsigned sz = (unsigned)GetLiveArraySize( ListIndex );
742  // It is worth checking whether we have a total dump of all individuals
743  // if so don't bother with asking each where it is
744  if (p_TheProbe->m_NoAreas==1) {
745  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) &&
746  (p_TheProbe->m_Rect[0].m_y2==(unsigned)SimH)) {
747  return (float) sz;
748  }
749  }
750  // Asking for a subset - need to test them all
751  for ( unsigned j = 0; j < sz; j++ ) {
752  Sp = TheArray[ ListIndex ] [ j ]->SupplyPosition();
753  for ( unsigned i = 0; i < p_TheProbe->m_NoAreas; i++ ) {
754  if ( ( Sp.m_x >= p_TheProbe->m_Rect[ i ].m_x1 ) && ( Sp.m_y >= p_TheProbe->m_Rect[ i ].m_y1 )
755  && ( Sp.m_x <= p_TheProbe->m_Rect[ i ].m_x2 ) && ( Sp.m_y <= p_TheProbe->m_Rect[ i ].m_y2 ) )
756  NumberSk++; // it is in the square so increment number
757  }
758  }
759  }
760  }
761  return NumberSk;
762 }
763 
764 //-----------------------------------------------------------------------------
765 
770  CIPEGridOutputPrb = fopen(cfg_CIPEGridOutput_filename.value(), "w" );
771  if ( !CIPEGridOutputPrb ) {
772  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheCIPEGridOutputProbe(): ""Unable to open probe file",
774  exit( 1 );
775  }
776  CIPEGridOutputPrbB = fopen(cfg_CIPEGridOutput_filenameB.value(), "w" );
777  if ( !CIPEGridOutputPrbB ) {
778  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheCIPEGridOutputProbe(): ""Unable to open probe file",
780  exit( 1 );
781  }
782  return true;
783 }
784 
785 //-----------------------------------------------------------------------------
786 
791  if ( CIPEGridOutputPrb != NULL )
792  fclose( CIPEGridOutputPrb );
793  fclose( CIPEGridOutputPrbB );
794 }
795 
796 //-----------------------------------------------------------------------------
797 
800 }
801 
802 //---------------------------------------------------------------------------
803 
807  int Counted[4];
808  int totalcells[4];
809  int OccupiedCells[4];
810  for (int cellwidth=0; cellwidth<4; cellwidth++) {
811  Counted[cellwidth] = 0;
812  totalcells[cellwidth] = m_gridcountwidth[cellwidth] * m_gridcountwidth[cellwidth];
813  OccupiedCells[cellwidth] = 0;
814  for (int i=0; i<m_gridcountwidth[cellwidth]; i++) {
815  for (int j=0; j<m_gridcountwidth[cellwidth]; j++) {
816  int res=m_gridcount[cellwidth][i+j*m_gridcountwidth[cellwidth]];
817  Counted[cellwidth] += res;
818  if (res>0) OccupiedCells[cellwidth]++;
819  }
820  }
821  }
822  fprintf(NWordOutputPrb,"%d\t%d\t%d\t", m_TheLandscape->SupplyYearNumber() ,(int)m_TheLandscape->SupplyDayInYear(), Counted[0] );
823  for (int c=0; c<4; c++) {
824  fprintf(NWordOutputPrb,"%d\t%d\t", totalcells[c], OccupiedCells[c]);
825  }
826  fprintf(NWordOutputPrb,"\n");
827  fflush(NWordOutputPrb);
828 }
829 //---------------------------------------------------------------------------
830 
835  RipleysOutputPrb = fopen(cfg_RipleysOutput_filename.value(), "w" );
836  if ( !RipleysOutputPrb ) {
837  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
839  exit( 1 );
840  }
841  NWordOutputPrb = fopen("NWordOutputPrb.txt", "w" );
842  if ( !NWordOutputPrb ) {
843  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheCIPEGridOutputProbe(): ""Unable to open NWord probe file", "" );
844  exit( 1 );
845  }
846  fprintf(NWordOutputPrb,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n","Year","Day","Total no.","Cells50","Occupied50","Cells100","Occupied100","Cells200","Occupied200","Cells400","Occupied400");
847  return true;
848 }
849 
850 //-----------------------------------------------------------------------------
855  RipleysOutputPrb1 = fopen("RipleyOutput_Jan.txt", "w" );
856  if ( !RipleysOutputPrb1 ) {
857  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
859  exit( 1 );
860  }
861  RipleysOutputPrb2 = fopen("RipleyOutput_Feb.txt", "w" );
862  if ( !RipleysOutputPrb2 ) {
863  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
865  exit( 1 );
866  }
867  RipleysOutputPrb3 = fopen("RipleyOutput_Mar.txt", "w" );
868  if ( !RipleysOutputPrb3 ) {
869  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
871  exit( 1 );
872  }
873  RipleysOutputPrb4 = fopen("RipleyOutput_Apr.txt", "w" );
874  if ( !RipleysOutputPrb4 ) {
875  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
877  exit( 1 );
878  }
879  RipleysOutputPrb5 = fopen("RipleyOutput_May.txt", "w" );
880  if ( !RipleysOutputPrb5 ) {
881  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
883  exit( 1 );
884  }
885  RipleysOutputPrb6 = fopen("RipleyOutput_Jun.txt", "w" );
886  if ( !RipleysOutputPrb6 ) {
887  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
889  exit( 1 );
890  }
891  RipleysOutputPrb7 = fopen("RipleyOutput_Jul.txt", "w" );
892  if ( !RipleysOutputPrb7 ) {
893  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
895  exit( 1 );
896  }
897  RipleysOutputPrb8 = fopen("RipleyOutput_Aug.txt", "w" );
898  if ( !RipleysOutputPrb8 ) {
899  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
901  exit( 1 );
902  }
903  RipleysOutputPrb9 = fopen("RipleyOutput_Sep.txt", "w" );
904  if ( !RipleysOutputPrb9 ) {
905  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
907  exit( 1 );
908  }
909  RipleysOutputPrb10 = fopen("RipleyOutput_Oct.txt", "w" );
910  if ( !RipleysOutputPrb10 ) {
911  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
913  exit( 1 );
914  }
915  RipleysOutputPrb11 = fopen("RipleyOutput_Nov.txt", "w" );
916  if ( !RipleysOutputPrb11 ) {
917  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
919  exit( 1 );
920  }
921  RipleysOutputPrb12 = fopen("RipleyOutput_Dec.txt", "w" );
922  if ( !RipleysOutputPrb12 ) {
923  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
925  exit( 1 );
926  }
927  return true;
928 }
929 
930 //-----------------------------------------------------------------------------
931 
936  ReallyBigOutputPrb = fopen(cfg_ReallyBigOutput_filename.value(), "w" );
937  if ( !ReallyBigOutputPrb ) {
938  g_msg->Warn( WARN_FILE, "Population_Manager::OpenTheRipleysOutputProbe(): ""Unable to open probe file",
940  exit( 1 );
941  }
942  return true;
943 }
944 
945 //-----------------------------------------------------------------------------
946 
952  fclose( RipleysOutputPrb );
953  RipleysOutputPrb=NULL;
954 }
955 
956 //-----------------------------------------------------------------------------
957 
962  fclose( RipleysOutputPrb1 );
963  fclose( RipleysOutputPrb2 );
964  fclose( RipleysOutputPrb3 );
965  fclose( RipleysOutputPrb4 );
966  fclose( RipleysOutputPrb5 );
967  fclose( RipleysOutputPrb6 );
968  fclose( RipleysOutputPrb7 );
969  fclose( RipleysOutputPrb8 );
970  fclose( RipleysOutputPrb9 );
971  fclose( RipleysOutputPrb10 );
972  fclose( RipleysOutputPrb11 );
973  fclose( RipleysOutputPrb12 );
974 }
975 
976 //-----------------------------------------------------------------------------
981  if ( ReallyBigOutputPrb != 0 )
982  fclose( ReallyBigOutputPrb );
983  ReallyBigOutputPrb=0;
984 }
985 
986 //-----------------------------------------------------------------------------
987 
990 }
991 //-----------------------------------------------------------------------------
992 
994 void Population_Manager::TheRipleysOutputProbe(FILE* /* a_Prob */ ) {
995 }
996 
997 //---------------------------------------------------------------------------
998 
999 
1000 //-----------------------------------------------------------------------------
1001 // TheArray handling functions
1002 //----------------------
1003 
1007 TAnimal * Population_Manager::FindClosest( int x, int y, unsigned Type ) {
1008  int distance = 100000000;
1009  TAnimal * TA = NULL;
1010  int dx, dy, d;
1011  for (unsigned j = 0; j < (unsigned)GetLiveArraySize( Type ); j++) {
1012  dx = TheArray[ Type ] [ j ]->Supply_m_Location_x();
1013  dy = TheArray[ Type ] [ j ]->Supply_m_Location_y();
1014  dx = ( dx - x );
1015  dx *= dx;
1016  dy = ( dy - y );
1017 
1018  dy *= dy;
1019  d = dx + dy;
1020  if ( d < distance ) {
1021  distance = d;
1022  TA = TheArray[ Type ] [ j ];
1023  }
1024  }
1025  return TA;
1026 }
1027 
1028 //------------------------------------------------------------------------------
1033 {
1034  for ( unsigned i = 0; i < TheArray.size(); i++ )
1035  {
1036  // if objects in the list need to be deleted:
1037  for (unsigned j = 0; j < (unsigned) TheArray[i].size(); j++)
1038  {
1039  delete TheArray[ i ] [ j ];
1040  }
1041  // empty the array
1042  TheArray[ i ].clear();
1043  }
1044 }
1045 
1049 void Population_Manager::SortX( unsigned Type ) {
1050  vector<TAnimal*>::iterator nth = TheArray[Type].begin() + m_LiveArraySize[Type];
1051  sort( TheArray[ Type ].begin(), nth, CompareX() );
1052 }
1053 
1054 //-----------------------------------------------------------------------------
1055 
1059 void Population_Manager::SortY( unsigned Type ) {
1060  vector<TAnimal*>::iterator nth = TheArray[Type].begin() + m_LiveArraySize[Type];
1061  sort(TheArray[Type].begin(), nth, CompareY());
1062 }
1063 
1064 //-----------------------------------------------------------------------------
1065 
1069 void Population_Manager::SortState( unsigned Type ) {
1070  sort( TheArray[ Type ].begin(), TheArray[ Type ].end(), CompareState() );
1071 }
1072 
1073 //-----------------------------------------------------------------------------
1074 
1078 void Population_Manager::SortStateR(unsigned Type) {
1079  sort(TheArray[Type].begin(), TheArray[Type].begin()+m_LiveArraySize[Type], CompareStateR());
1080 }
1081 
1085 unsigned Population_Manager::PartitionLiveDead(unsigned Type) {
1086  auto it = partition(TheArray[Type].begin(), TheArray[Type].begin() + m_LiveArraySize[Type], CompareStateAlive());
1087  // it now points at the first dead element
1088  // the number of live ones before it is it-TheArray[Type].begin(). This is the new vector size for live animals.
1089  return unsigned(it - TheArray[Type].begin());
1090 }
1091 
1092 //-----------------------------------------------------------------------------
1093 
1097 void Population_Manager::SortXIndex( unsigned Type ) {
1098  vector<TAnimal*>::iterator nth = TheArray[Type].begin() + m_LiveArraySize[Type];
1099  sort(TheArray[Type].begin(), nth, CompareX());
1100  unsigned s = (unsigned)GetLiveArraySize( Type );
1101  // Now make the index array to X;
1102  int counter = 0;
1103  int x;
1104  // for each individual
1105  for ( unsigned i = 0; i < s; i++ ) {
1106  // Get Next co-ordiated
1107  x = TheArray[ Type ] [ i ]->Supply_m_Location_x();
1108  // fill gaps up to x
1109  while ( counter < x ) IndexArrayX[ Type ] [ counter++ ] = -1;
1110  if ( x == counter ) {
1111  IndexArrayX[ Type ] [ counter++ ] = i;
1112  }
1113  }
1114  // Fill up the rest of the array with -1;
1115  for ( int c = counter; c < 10000; c++ ) {
1116  IndexArrayX[ Type ] [ c ] = -1;
1117  }
1118 }
1119 
1120 //-----------------------------------------------------------------------------
1121 
1125 void Population_Manager::Shuffle( unsigned Type ) {
1126  unsigned s = (unsigned)GetLiveArraySize( Type );
1127  for ( unsigned i = 0; i < s; i++ ) {
1128  TAnimal * temp;
1129  unsigned a = random( s );
1130  unsigned b = random( s );
1131  temp = TheArray[ Type ] [ a ];
1132  TheArray[ Type ] [ a ] = TheArray[ Type ] [ b ];
1133  TheArray[ Type ] [ b ] = temp;
1134  }
1135 }
1136 
1137 //-----------------------------------------------------------------------------
1138 
1144  switch ( BeforeStepActions[ Type ] ) {
1145  case 0:
1146  Shuffle( Type );
1147  break;
1148  case 1:
1149  SortX( Type );
1150  break;
1151  case 2:
1152  SortY( Type );
1153  break;
1154  case 3:
1155  SortXIndex( Type );
1156  break;
1157  case 4: // Do nothing
1158  break;
1159  case 5:
1160  if (g_rand_uni() < double (1/500)) Shuffle( Type );
1161  break;
1162  default:
1163  m_TheLandscape->Warn( "Population_Manager::Shuffle_or_Sort- BeforeStepAction Unknown", NULL );
1164  exit( 1 );
1165  }
1166 }
1167 
1168 //-----------------------------------------------------------------------------
1173  if ( m_TheLandscape->SupplyDayInMonth() == cfg_DayInMonth.value() ) return true;
1174  return false;
1175 }
1176 
1177 //------------------------------------------------------------------------------
1178 
1184  return;
1185 }
1186 //---------------------------------------------------------------------------
1187 
1191 char* Population_Manager::SpeciesSpecificReporting(int a_species, int a_time) {
1192  strcpy(g_str,"");
1193 // Vole Model
1194  if ( a_species == 1 ) {
1195 /*
1196 // The next lines are obselete code from genetic simulations
1197 float MeanNoAlleles;
1198  float MeanHO;
1199  float MeanHE;
1200  unsigned size;
1201 
1202  if ( m_time % ( 365 * 24 * 60 ) == 0 ) // End of year
1203  {
1204 
1205  // empty the GenStruc data
1206  for ( int ProbeNo = 0; ProbeNo < ( int )m_NoProbes; ProbeNo++ ) {
1207  fprintf( m_GeneticsFile, "%d ", m_time );
1208  int TotalSize = 0;
1209  m_AManager->AlFreq->Flush();
1210  for ( unsigned listindex = 0; listindex <= 1; listindex++ ) {
1211  size = 0;
1212  m_AManager->TheGeneticProbe( listindex, ProbeNo, size );
1213  TotalSize += size;
1214  }
1215  if ( TotalSize > 0 ) {
1216  m_AManager->AlFreq->CalcAF();
1217  m_AManager->AlFreq->CalcNoAlleles();
1218  m_AManager->AlFreq->CalcHE();
1219  m_AManager->AlFreq->CalcHO( TotalSize );
1220  }
1221  // Get the means
1222  // For N alleles
1223  int NA = 0;
1224  float NHO = 0;
1225  float NHE = 0;
1226  for ( int l = 0; l < 16; l++ ) {
1227  NA += m_AManager->AlFreq->SupplyNoAlleles( l );
1228  }
1229  MeanNoAlleles = ( float )NA / 16.0;
1230  for ( int l = 0; l < 16; l++ ) {
1231  NHO += m_AManager->AlFreq->SupplyHO( l );
1232  }
1233  MeanHO = NHO / 16.0;
1234  for ( int l = 0; l < 16; l++ ) {
1235  NHE += m_AManager->AlFreq->SupplyHE( l );
1236  }
1237  MeanHE = NHE / 16.0;
1238 
1239  // Do some output
1240  // Output is to two files, the genetics file and the AllelerequencyFile
1241  fprintf( m_GeneticsFile, "%2.2f ", MeanNoAlleles );
1242  fprintf( m_GeneticsFile, "%1.3f ", MeanHO );
1243  fprintf( m_GeneticsFile, "%1.3f ", MeanHE );
1244  for ( int l = 0; l < 16; l++ ) {
1245  fprintf( m_GeneticsFile, "%04i ", m_AManager->AlFreq->SupplyNoAlleles( l ) );
1246  }
1247  for ( int l = 0; l < 16; l++ ) {
1248  fprintf( m_GeneticsFile, "%1.3f ", m_AManager->AlFreq->SupplyHO( l ) );
1249  }
1250  for ( int l = 0; l < 16; l++ ) {
1251  fprintf( m_GeneticsFile, "%1.3f ", m_AManager->AlFreq->SupplyHE( l ) );
1252  }
1253  fprintf( m_GeneticsFile, "\n" );
1254  fprintf( m_AlleleFreqsFile, "%d ", m_time );
1255  for ( int l = 0; l < 16; l++ ) {
1256  for ( int al = 0; al < 4; al++ ) {
1257  fprintf( m_AlleleFreqsFile, "%1.3f ", m_AManager->AlFreq->SupplyAF( l, al ) );
1258  }
1259  }
1260  fprintf( m_AlleleFreqsFile, "\n" );
1261  }
1262  // make sure progress reaches the disk
1263  fflush( m_GeneticsFile );
1264  fflush( m_AlleleFreqsFile );
1265  }
1266 
1267  if ( cfg_UseEasyPop.value()) {
1268  int targettime=a_time % ( 365 * 100 );
1269  if ( ( targettime == 59 ) ||(targettime == 181)||(targettime ==304)||(targettime ==59+365)||(targettime ==181+365)||(targettime ==304+365)) {
1270  for ( unsigned listindex = 0; listindex <= 1; listindex++ ) {
1271  GeneticsResultsOutput( m_EasyPopRes, listindex);
1272  }
1273  fflush( m_EasyPopRes );
1274  }
1275  }
1276 */
1277  /* Three times a year reporting
1278  if ( m_time % 365 == 60 ) ProbeReportNow( m_time );
1279  if ( m_time % 365 == 152 ) ProbeReportNow( m_time );
1280  if ( m_time % 365 == 273 ) ProbeReportNow( m_time ); */
1281  ProbeReport( a_time );
1282 #ifdef __SpecificPesticideEffectsVinclozolinLike__
1283  ImpactProbeReport( a_time );
1284 #endif
1285 #ifdef __WithinOrchardPesticideSim__
1286  ImpactProbeReport( a_time );
1287 #endif
1288 
1289  }
1290  // Skylark Model
1291  else if ( a_species == 0 ) {
1292  int No;
1293  int a_day = a_time % 365;
1294  if ( a_time % 365 == 364 )
1295  {
1296  //Write the Breeding Attempts Probe
1297  int BreedingFemales, YoungOfTheYear, TotalPop, TotalFemales, TotalMales, BreedingAttempts;
1298  int No = TheBreedingSuccessProbe( BreedingFemales, YoungOfTheYear, TotalPop, TotalFemales, TotalMales, BreedingAttempts );
1299  float bs = 0;
1300  if ( BreedingFemales > 0 ) {
1301  bs = No / ( float )BreedingFemales; //bs = successful breeding attempt per attempting to breed female
1302  }
1303  BreedingSuccessProbeOutput( bs, BreedingFemales, YoungOfTheYear, TotalPop,
1304  TotalFemales, TotalMales, a_time, BreedingAttempts );
1305  }
1306  if ( a_day == 152 ) {
1307  // Need to fill in the landscape from 1st June, it will change before
1308  // the count of fledgelings is needed
1309  m_TheLandscape->FillVegAreaData();
1310  }
1311  if ( a_day == 197 ) {
1312  for ( int ProbeNo = 0; ProbeNo < m_NoProbes; ProbeNo++ ) {
1313  No = TheFledgelingProbe();
1314  // Do some output
1315  FledgelingProbeOutput( No, a_time );
1316  }
1317  }
1318 #ifdef __SKPOM
1319  // This is clunky but for purposes of validation we want probes throughout the year
1320  // First get the breeding pairs data.
1321  BreedingPairsOutput(a_day);
1322 #else
1323  ProbeReport( a_time );
1324 #endif
1325  }
1326  else return ProbeReport( a_time );
1327  return g_str;
1328 }
1329 //-----------------------------------------------------------------------------
1330 
1331 unsigned int Population_Manager::FarmAnimalCensus(unsigned int a_farm, unsigned int a_typeofanimal)
1332 {
1333  unsigned int No = 0;
1334  unsigned sz = (unsigned)GetLiveArraySize( a_typeofanimal );
1335  for ( unsigned j = 0; j < sz; j++ )
1336  {
1337  if (a_farm == TheArray[ a_typeofanimal ] [ j ]->SupplyFarmOwnerRef()) No++;
1338  }
1339  return No;
1340 }
1341 //-----------------------------------------------------------------------------
1342 
1344 
1345  int No;
1346  char str[100]; // 100 out to be enough!!
1347  strcpy(g_str,"");
1348  for ( int ProbeNo = 0; ProbeNo < m_NoProbes; ProbeNo++ ) {
1349  No = 0;
1350  // See if we need to record/update this one
1351  // if time/months/years ==0 or every time
1352  if ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 3 )
1353  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 2 ) && ( BeginningOfMonth() ) )
1354  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 1 ) && ( Time % 365 == 0 ) ) ) {
1355  // Goes through each area and sends a value to OutputForm
1356  unsigned Index = SupplyListIndexSize();
1357  for ( unsigned listindex = 0; listindex < Index; listindex++ ) {
1358  if ( TheProbe[ ProbeNo ]->m_TargetTypes[ listindex ] )
1359  No += (int) Probe( listindex, TheProbe[ ProbeNo ] );
1360  }
1361  TheProbe[ ProbeNo ]->FileOutput( No, Time, ProbeNo );
1362  sprintf(str," %d ", No );
1363  strcat(g_str,str);
1364  }
1365  }
1366  return g_str;
1367 }
1368 //-----------------------------------------------------------------------------
1369 
1371  int No;
1372  char str[100]; // 100 ought to be enough!!
1373  strcpy(g_str,"");
1374  for ( int ProbeNo = 0; ProbeNo < m_NoProbes; ProbeNo++ ) {
1375  No = 0;
1376  unsigned Index = SupplyListIndexSize();
1377  for ( unsigned listindex = 0; listindex < Index; listindex++ ) {
1378  if ( TheProbe[ ProbeNo ]->m_TargetTypes[ listindex ] ) No += (int) Probe( listindex, TheProbe[ ProbeNo ] );
1379  }
1380  TheProbe[ ProbeNo ]->FileOutput( No, Time, ProbeNo );
1381  sprintf(str," %d ", No );
1382  strcat(g_str,str);
1383  }
1384  return g_str;
1385 }
1386 //-----------------------------------------------------------------------------
1387 
1392 
1393  for ( int ProbeNo = 0; ProbeNo < 1; ProbeNo++ ) {
1394  // See if we need to record/update this one
1395  // if time/months/years ==0 or every time
1396  if ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 3 )
1397  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 2 ) && ( BeginningOfMonth() ) )
1398  || ( ( TheProbe[ ProbeNo ]->m_ReportInterval == 1 ) && ( a_Time % 365 == 0 ) ) ) {
1399  ImpactedProbe();
1400  }
1401  }
1402 }
1403 
1404 //-----------------------------------------------------------------------------
1405 
1406 //-----------------End of TheArray handling functions
1407 
1408 //-----------------------------------------------------------------------------
1409 //------------------------------------------------------------------------------
1410 
1411 
1416  AnimalPosition SkP;
1417  SkP.m_x = m_Location_x;
1418  SkP.m_y = m_Location_y;
1419  SkP.m_EleType = m_OurLandscape->SupplyElementType( m_Location_x, m_Location_y );
1420  SkP.m_VegType = m_OurLandscape->SupplyVegType( m_Location_x, m_Location_y );
1421  return SkP;
1422 }
1423 
1424 //-----------------------------------------------------------------------------
1425 
1430  return m_OurLandscape->SupplyFarmOwner( m_Location_x, m_Location_y );
1431 }
1432 
1433 //-----------------------------------------------------------------------------
1437 TAnimal::TAnimal( int x, int y, Landscape * L ) {
1438  m_OurLandscape = L;
1439  m_Location_x = x;
1440  m_Location_y = y;
1441 }
1442 
1443 //-----------------------------------------------------------------------------
1444 //-----------------------------------------------------------------------------
1445 
1450 #ifdef __CJTDebug_5
1451  AmAlive = 0xDEADC0DE;
1452 #endif
1453  m_StepDone = false;
1454  m_CurrentStateNo = 0;
1455 }
1456 
1457 //-----------------------------------------------------------------------------
1458 
1459 
1464 #ifdef __CJTDebug_5
1465  AmAlive = 0;
1466 #endif
1467 }
1468 
1469 //-----------------------------------------------------------------------------
1470 
1475  FarmToDo event;
1476  int i = 0;
1477  while ( ( event = ( FarmToDo )m_OurLandscape->SupplyLastTreatment( m_Location_x, m_Location_y, & i ) ) != sleep_all_day ) {
1478  if ( OnFarmEvent( event ) ) break;
1479  }
1480 }
1481 
1482 //-----------------------------------------------------------------------------
1483 
1487 void TAnimal::CheckManagementXY( int x, int y ) {
1488  FarmToDo event;
1489  int i = 0;
1490  while ( ( event = ( FarmToDo )m_OurLandscape->SupplyLastTreatment( x, y, & i ) ) != sleep_all_day ) {
1491  OnFarmEvent( event );
1492  }
1493 }
1494 
1495 //-----------------------------------------------------------------------------
1496 
1498  exit( 1 );
1499 }
1500 
1501 //-----------------------------------------------------------------------------
1502 
1503 #ifdef __CJTDebug_5
1504 void TALMaSSObject::DEADCODEError() {
1505  exit( 0 );
1506 }
1507 
1508 #endif
1509 //-----------------------------------------------------------------------------
1510 // probe_data
1511 //-----------------------------------------------------------------------------
1512 
1517 void probe_data::FileOutput( int No, int time, int ProbeNo ) {
1518  if ( m_FileRecord ) {
1519 
1520  if ( ProbeNo == 0 ) {
1521  // First probe so write the time and a new line
1522  (*m_MyFile) << endl;
1523  (*m_MyFile) << time << '\t' << No;
1524  } else
1525  (*m_MyFile) << '\t' << No ;
1526  }
1527  (*m_MyFile).flush();
1528 }
1529 
1530 //-----------------------------------------------------------------------------
1531 
1532 void probe_data::FileAppendOutput( int No, int time ) {
1533  m_MyFile->open(m_MyFileName,ios::app);
1534  if ( !m_MyFile->is_open() ) {
1535  g_msg->Warn( (MapErrorState)0,"Cannot open file for append: ", m_MyFileName );
1536  exit( 0 );
1537  }
1538  if ( m_FileRecord ) {
1539  (*m_MyFile)<< time << '\t' << No << endl;
1540  }
1541  m_MyFile->close();
1542 }
1543 
1544 //-----------------------------------------------------------------------------
1545 
1550  m_Time = 0;
1551  m_FileRecord = false;
1552  m_MyFile = NULL;
1553 }
1554 
1555 //-----------------------------------------------------------------------------
1556 
1560 ofstream * probe_data::OpenFile( char * Nme ) {
1561  m_MyFile = new ofstream( Nme );
1562  if ( !m_MyFile->is_open() ) {
1563  g_msg->Warn( (MapErrorState)0,"probe_data::OpenFile - Cannot open file: ", Nme );
1564  exit( 0 );
1565  }
1566  strcpy( m_MyFileName, Nme );
1567  return m_MyFile;
1568 }
1569 //-----------------------------------------------------------------------------
1570 
1574 void probe_data::SetFile( ofstream * F ) {
1575  m_MyFile = F;
1576 }
1577 
1578 //-----------------------------------------------------------------------------
1579 
1584 }
1585 //-----------------------------------------------------------------------------
1586 
1587 
1589 {
1590  if (TheArray[l][i]->Supply_m_Location_x() > 10000) return true;
1591  if (TheArray[l][i]->Supply_m_Location_y() > 10000) return true;
1592  if (TheArray[l][i]->Supply_m_Location_x() < 0) return true;
1593  if (TheArray[l][i]->Supply_m_Location_y() < 0) return true;
1594  return false;
1595 }
1596 
rectangle m_Rect[10]
void SortX(unsigned Type)
The landscape class containing all environmental and topographical data.
Definition: landscape.h:109
A class defining an animals position.
CfgBool cfg_RipleysOutputMonthly_used("G_RIPLEYSOUTPUTMONTHLY_USED", CFG_CUSTOM, false)
void Shuffle_or_Sort(unsigned Type)
boost::variate_generator< base_generator_type &, boost::uniform_real<> > g_rand_uni
virtual ~TALMaSSObject()
The destructor for TALMaSSObject.
virtual void Run(int NoTSteps)
TTypesOfLandscapeElement m_EleType
virtual void TheReallyBigOutputProbe()
TALMaSSObject()
The constructor for TALMaSSObject.
static CfgInt cfg_ReallyBigOutput_interval("G_REALLYBIGOUTPUT_INTERVAL", CFG_CUSTOM, 1)
void CheckManagementXY(int x, int y)
virtual float Probe(int ListIndex, probe_data *p_TheProbe)
unsigned m_NoAreas
static CfgInt cfg_ReallyBigOutput_day2("G_REALLYBIGOUTPUT_DAY_TWO", CFG_CUSTOM, 91)
Integer configurator entry class.
Definition: configurator.h:85
static CfgInt cfg_RipleysOutput_interval("G_RIPLEYSOUTPUT_INTERVAL", CFG_CUSTOM, 1)
static CfgInt cfg_DayInMonth("PRB_DAYINMONTH", CFG_CUSTOM, 1)
Function to compare to TAnimal&#39;s m_CurrentStateNo to -1.
char * SpeciesSpecificReporting(int a_species, int a_time)
CfgBool cfg_RipleysOutput_used("G_RIPLEYSOUTPUT_USED", CFG_CUSTOM, true)
CfgInt cfg_CatastropheEventStartYear("PM_CATASTROPHEEVENTSTARTYEAR", CFG_CUSTOM, 99999)
unsigned m_RefFarms[25]
void CloseTheMonthlyRipleysOutputProbe()
virtual int WhatState()
void NWordOutput()
The output section for the NWord probe. .
AnimalPosition SupplyPosition()
static CfgInt cfg_ReallyBigOutput_day1("G_REALLYBIGOUTPUT_DAY_ONE", CFG_CUSTOM, 1)
unsigned int FarmAnimalCensus(unsigned int a_farm, unsigned int a_typeofanimal)
CfgInt cfg_pm_eventsize("PM_EVENTSIZE", CFG_CUSTOM, 100)
ofstream * OpenFile(char *Nme)
Function to compare to TAnimal&#39;s Current behavioural state.
virtual void DisplayLocations()
static CfgInt cfg_RipleysOutputFirstYear("G_RIPLEYSOUTPUT_FIRSTYEAR", CFG_CUSTOM, 1)
void FileOutput(int No, int time, int ProbeNo)
unsigned m_NoVegTypes
void SetFile(ofstream *F)
virtual void TheCIPEGridOutputProbe()
static CfgInt cfg_VoleCatastrophe_interval("VOLE_CATASTROPHE_I", CFG_CUSTOM, 365 *5)
Function to compare to TAnimal&#39;s m_CurrentStateNo.
void SortState(unsigned Type)
CfgBool cfg_fixed_random_sequence("G_FIXEDRANDOMSEQUENCE", CFG_CUSTOM, false)
virtual bool StepFinished()
Overrides the population manager StepFinished - there is no chance that hunters do not finish a step ...
TTypesOfVegetation m_RefVeg[25]
virtual void DoAlmostLast()
const char * value(void)
Definition: configurator.h:139
static CfgInt cfg_ReallyBigOutput_day3("G_REALLYBIGOUTPUT_DAY_THREE", CFG_CUSTOM, 182)
unsigned m_x1
void EmptyTheArray()
Removes all objects from the TheArray by deleting them and clearing TheArray.
void Shuffle(unsigned Type)
CfgInt cfg_CIPEGridOutput_day_b("G_CIPEGRIDOUTPUT_DAY_B", CFG_CUSTOM, 270)
virtual void TheRipleysOutputProbe(FILE *a_prb)
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
TTypesOfVegetation m_VegType
CfgStr cfg_CIPEGridOutput_filename("G_CIPEGRIDOUTPUT_FILENAME", CFG_CUSTOM,"CIPEGridOutput.txt")
The base class for all farm types.
Definition: farm.h:683
CfgStr cfg_CIPEGridOutput_filenameB("G_CIPEGRIDOUTPUT_FILENAME_B", CFG_CUSTOM,"CIPEGridOutputB.txt")
bool value(void)
Definition: configurator.h:123
void CheckManagement(void)
int Supply_m_Location_y()
FarmToDo
Definition: treatment.h:31
int Supply_m_Location_x()
The base class for all ALMaSS animal classes.
TTypesOfLandscapeElement m_RefEle[25]
void SortXIndex(unsigned Type)
char * ProbeReportTimed(int a_time)
void FileAppendOutput(int No, int time)
void SortY(unsigned Type)
unsigned SupplyFarmOwnerRef()
String configurator entry class.
Definition: configurator.h:132
Bool configurator entry class.
Definition: configurator.h:116
int SupplySimAreaHeight(void)
Definition: landscape.h:1563
unsigned m_NoEleTypes
vector< TAnimal * > TListOfAnimals
CfgInt cfg_CIPEGridOutput_Interval("G_CIPEGRIDOUTPUT_INTERVAL", CFG_CUSTOM, 1)
static CfgInt cfg_CipeGridSize("G_CIPEGRIDSIZE", CFG_CUSTOM, 500)
bool CheckXY(int l, int i)
Debug method to test for out of bounds coordinates.
void ImpactProbeReport(int a_Time)
CfgInt cfg_CIPEGridOutput_day("G_CIPEGRIDOUTPUT_DAY", CFG_CUSTOM, 1)
MapErrorState
Definition: maperrormsg.h:33
CfgBool cfg_CIPEGridOutput_used("G_CIPEGRIDOUTPUT_USED", CFG_CUSTOM, false)
char * ProbeReport(int a_time)
static CfgInt cfg_VoleCatastrophe_mortality("VOLE_CATASTROPHE_M", CFG_CUSTOM, 90)
unsigned m_y1
unsigned m_NoFarms
CfgInt cfg_pm_eventfrequency("PM_EVENTFREQUENCY", CFG_CUSTOM, 0)
Function to compare to TAnimal&#39;s m_CurrentStateNo to anything but -1.
int value(void)
Definition: configurator.h:92
Function to compare to TAnimal&#39;s m_Location_y.
Population_Manager(Landscape *L)
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
char g_str[255]
unsigned PartitionLiveDead(unsigned Type)
void SortStateR(unsigned Type)
static CfgInt cfg_RipleysOutput_day("G_RIPLEYSOUTPUT_DAY", CFG_CUSTOM, 60)
static CfgBool cfg_VoleCatastrophe_on("VOLE_CATASTROPHE_ON", CFG_CUSTOM, false)
unsigned m_x2
virtual void ImpactedProbe()
int ProbeFileInput(char *p_Filename, int p_ProbeNo)
virtual void Catastrophe()
static CfgInt cfg_ReallyBigOutput_day4("G_REALLYBIGOUTPUT_DAY_FOUR", CFG_CUSTOM, 274)
static CfgStr cfg_RipleysOutput_filename("G_RIPLEYSOUTPUT_FILENAME", CFG_CUSTOM,"RipleysOutput.txt")
TAnimal * FindClosest(int x, int y, unsigned Type)
Data structure to hold & output probe data probe data is designed to be used to return the number of ...
void LOG(const char *fname)
virtual ~Population_Manager(void)
static CfgInt cfg_ReallyBigOutputFirstYear("G_REALLYBIGOUTPUT_FIRSTYEAR", CFG_CUSTOM, 1)
CfgBool cfg_ReallyBigOutput_used("G_REALLYBIGOUTPUT_USED", CFG_CUSTOM, false)
int GetCurrentStateNo()
Returns the current state number.
TAnimal(int x, int y, Landscape *L)
void OnArrayBoundsError()
Used for debugging only, tests basic object properties.
unsigned m_y2
static CfgStr cfg_ReallyBigOutput_filename("G_REALLYBIGOUTPUT_FILENAME", CFG_CUSTOM,"ReallyBigOutput.txt")
Function to compare to TAnimal&#39;s m_Location_x.