1 /*
2  *  cWorld.h
3  *  Avida
4  *
5  *  Created by David on 10/18/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *
8  *
9  *  This file is part of Avida.
10  *
11  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
12  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
13  *
14  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
18  *  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef cWorld_h
23 #define cWorld_h
24 
25 #include "apto/core/SmartPtr.h"
26 #include "avida/data/Types.h"
27 
28 #include "cAvidaConfig.h"
29 #include "cAvidaContext.h"
30 #include "cDataFileManager.h"
31 #include "cRandom.h"
32 
33 #include <cassert>
34 
35 namespace Avida {
36   class WorldDriver;
37 };
38 
39 class cAnalyze;
40 class cAnalyzeGenotype;
41 class cClassificationManager;
42 class cEnvironment;
43 class cEventList;
44 class cHardwareManager;
45 class cMigrationMatrix; // MIGRATION_MATRIX
46 class cOrganism;
47 class cPopulation;
48 class cMerit;
49 class cPopulationCell;
50 class cStats;
51 class cTestCPU;
52 class cUserFeedback;
53 template<class T> class tDataEntry;
54 template<class T> class tDictionary;
55 
56 using namespace Avida;
57 
58 
59 class cWorld
60 {
61 protected:
62   cString m_working_dir;
63 
64   cAnalyze* m_analyze;
65   cAvidaConfig* m_conf;
66   cAvidaContext m_ctx;
67   cClassificationManager* m_class_mgr;
68   cDataFileManager* m_datafile_mgr;
69   cEnvironment* m_env;
70   cEventList* m_event_list;
71   cHardwareManager* m_hw_mgr;
72   cMigrationMatrix* m_mig_mat;  // MIGRATION_MATRIX
73   cPopulation* m_pop;
74   Apto::SmartPtr<cStats, Apto::ThreadSafeRefCount> m_stats;
75   WorldDriver* m_driver;
76 
77   Avida::Data::Manager* m_data_mgr;
78 
79   cRandom m_rng;
80   cRandom m_srng;         // second random number seq to be used for random sampling etc without changing run processes
81 
82   bool m_test_on_div;     // flag derived from a collection of configuration settings
83   bool m_test_sterilize;  // flag derived from a collection of configuration settings
84 
85   bool m_own_driver;      // specifies whether this world object should manage its driver object
86 
87   cWorld(cAvidaConfig* cfg, const cString& wd);
88 
89 private:
90   cWorld(); // @not_implemented
91   cWorld(const cWorld&); // @not_implemented
92   cWorld& operator=(const cWorld&); // @not_implemented
93 
94 
95 public:
96   static cWorld* Initialize(cAvidaConfig* cfg, const cString& working_dir, cUserFeedback* feedback = NULL);
97   virtual ~cWorld();
98 
99   void SetDriver(WorldDriver* driver, bool take_ownership = false);
100 
GetWorkingDir()101   const cString& GetWorkingDir() const { return m_working_dir; }
102 
103   // General Object Accessors
104   cAnalyze& GetAnalyze();
GetConfig()105   cAvidaConfig& GetConfig() { return *m_conf; }
GetDefaultContext()106   cAvidaContext& GetDefaultContext() { return m_ctx; }
GetClassificationManager()107   cClassificationManager& GetClassificationManager() { return *m_class_mgr; }
GetDataFileManager()108   cDataFileManager& GetDataFileManager() { return *m_datafile_mgr; }
GetEnvironment()109   cEnvironment& GetEnvironment() { return *m_env; }
GetHardwareManager()110   cHardwareManager& GetHardwareManager() { return *m_hw_mgr; }
GetMigrationMatrix()111   cMigrationMatrix& GetMigrationMatrix(){ return *m_mig_mat; }; // MIGRATION_MATRIX
GetPopulation()112   cPopulation& GetPopulation() { return *m_pop; }
GetRandom()113   cRandom& GetRandom() { return m_rng; }
GetRandomSample()114   cRandom& GetRandomSample() { return m_srng; }
GetStats()115   cStats& GetStats() { return *m_stats; }
GetDriver()116   WorldDriver& GetDriver() { return *m_driver; }
117 
GetDataManager()118   Data::Manager& GetDataManager() { return *m_data_mgr; }
119 
120   Data::ProviderPtr GetStatsProvider(cWorld*);
121 
122   // Access to Data File Manager
GetDataFileOFStream(const cString & fname)123   std::ofstream& GetDataFileOFStream(const cString& fname) { return m_datafile_mgr->GetOFStream(fname); }
GetDataFile(const cString & fname)124   cDataFile& GetDataFile(const cString& fname) { return m_datafile_mgr->Get(fname); }
125 
126   // Config Dependent Modes
GetTestOnDivide()127   bool GetTestOnDivide() const { return m_test_on_div; }
GetTestSterilize()128   bool GetTestSterilize() const { return m_test_sterilize; }
129 
130   // Convenience Accessors
131   int GetNumResources();
GetVerbosity()132   inline int GetVerbosity() { return m_conf->VERBOSITY.Get(); }
SetVerbosity(int v)133   inline void SetVerbosity(int v) { m_conf->VERBOSITY.Set(v); }
134 
135   void GetEvents(cAvidaContext& ctx);
136 
GetEventsList()137 	cEventList* GetEventsList() { return m_event_list; }
138 
139 	//! Migrate this organism to a different world (does nothing here; see cMultiProcessWorld).
MigrateOrganism(cOrganism * org,const cPopulationCell & cell,const cMerit & merit,int lineage)140 	virtual void MigrateOrganism(cOrganism* org, const cPopulationCell& cell, const cMerit& merit, int lineage) { }
141 
142 	//! Returns true if an organism should be migrated to a different world.
TestForMigration()143 	virtual bool TestForMigration() { return false; }
144 
145 	//! Returns true if the given cell is on the boundary of the world, false otherwise.
IsWorldBoundary(const cPopulationCell & cell)146 	virtual bool IsWorldBoundary(const cPopulationCell& cell) { return false; }
147 
148 	//! Process post-update events.
ProcessPostUpdate(cAvidaContext & ctx)149 	virtual void ProcessPostUpdate(cAvidaContext& ctx) { }
150 
151 	//! Returns true if this world allows early exits, e.g., when the population reaches 0.
AllowsEarlyExit()152 	virtual bool AllowsEarlyExit() const { return true; }
153 
154 	//! Calculate the size (in virtual CPU cycles) of the current update.
155 	virtual int CalculateUpdateSize();
156 
157 protected:
158   // Internal Methods
159   bool setup(cUserFeedback* errors);
160 
161 };
162 
163 #endif
164