1 /*
2  *  cTestCPU.h
3  *  Avida
4  *
5  *  Called "test_cpu.hh" prior to 11/30/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *  Copyright 1999-2003 California Institute of Technology.
8  *
9  *
10  *  This file is part of Avida.
11  *
12  *  Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13  *  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14  *
15  *  Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public License along with Avida.
19  *  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef cTestCPU_h
24 #define cTestCPU_h
25 
26 #include <fstream>
27 
28 #ifndef tArray_h
29 #include "tArray.h"
30 #endif
31 #ifndef cString_h
32 #include "cString.h"
33 #endif
34 #ifndef cResourceCount_h
35 #include "cResourceCount.h"
36 #endif
37 #ifndef cCPUTestInfo_h
38 #include "cCPUTestInfo.h"
39 #endif
40 #ifndef cWorld_h
41 #include "cWorld.h"
42 #endif
43 
44 
45 namespace Avida {
46   class Genome;
47 };
48 
49 class cAvidaContext;
50 class cBioGroup;
51 class cInstSet;
52 class cResourceCount;
53 class cResourceHistory;
54 
55 using namespace Avida;
56 
57 
58 class cTestCPU
59 {
60 public:
61 
62 private:
63   cWorld* m_world;
64   tArray<int> input_array;
65   tArray<int> receive_array;
66   int cur_input;
67   int cur_receive;
68   bool m_use_random_inputs;
69   bool m_use_manual_inputs;
70 
71   // Resource settings. Reinitialized from cCPUTestInfo on each test.
72   eTestCPUResourceMethod m_res_method;
73   const cResourceHistory* m_res;
74   int m_res_update;
75   int m_res_cpu_cycle_offset;
76 
77   // Actual CPU resources.
78   cResourceCount m_resource_count;
79   cResourceCount m_faced_cell_resource_count;
80   cResourceCount m_deme_resource_count;
81   cResourceCount m_cell_resource_count;
82 
83 
84   bool ProcessGestation(cAvidaContext& ctx, cCPUTestInfo& test_info, int cur_depth);
85   bool TestGenome_Body(cAvidaContext& ctx, cCPUTestInfo& test_info, const Genome& genome, int cur_depth);
86 
87 
88   cTestCPU(); // @not_implemented
89   cTestCPU(const cTestCPU&); // @not_implemented
90   cTestCPU& operator=(const cTestCPU&); // @not_implemented
91 
92   // Internal methods for setting up and updating resources
93   void InitResources(cAvidaContext& ctx, int res_method = RES_INITIAL, cResourceHistory* res = NULL, int update = 0, int cpu_cycle_offset = 0);
94   void UpdateResources(cAvidaContext& ctx, int cpu_cycles_used);
95   inline void SetResourceUpdate(cAvidaContext& ctx, int update, bool exact = true);
96   inline void SetResource(cAvidaContext& ctx, int id, double new_level);
97 
98 public:
99   cTestCPU(cAvidaContext& ctx, cWorld* world);
~cTestCPU()100   ~cTestCPU() { }
101 
102   bool TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const Genome& genome);
103   bool TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const Genome& genome, std::ofstream& out_fp);
104 
105   void PrintGenome(cAvidaContext& ctx, const Genome& genome, cString filename = "", int update = -1, bool for_groups = false, int last_birth_cell = 0, int last_group_id = -1, int last_forager_type = -1);
106   void PrintBioGroup(cAvidaContext& ctx, cBioGroup* bg, cString filename = "", int update = -1);
107 
108   inline int GetInput();
109   inline int GetInputAt(int & input_pointer);
GetInputs()110   inline const tArray<int>& GetInputs() const { return input_array; }
111   void ResetInputs(cAvidaContext& ctx);
112 
113   inline int GetReceiveValue();
114   inline const tArray<double>& GetResources(cAvidaContext& ctx);
115   inline const tArray<double>& GetAvatarResources(cAvidaContext& ctx);
116   inline const tArray<double>& GetFacedCellResources(cAvidaContext& ctx);
117   inline const tArray<double>& GetFacedAvatarResources(cAvidaContext& ctx);
118   inline const tArray<double>& GetDemeResources(int deme_id, cAvidaContext& ctx);
119   inline const tArray<double>& GetCellResources(int cell_id, cAvidaContext& ctx);
120   inline const tArray<double>& GetFrozenResources(cAvidaContext& ctx, int cell_id);
121   inline const tArray< tArray<int> >& GetCellIdLists();
122 
123   // Used by cTestCPUInterface to get/update resources
124   void ModifyResources(cAvidaContext& ctx, const tArray<double>& res_change);
GetResourceCount()125   cResourceCount& GetResourceCount() { return m_resource_count; }
126 };
127 
128 
129 // Inline Methods
130 
GetInput()131 inline int cTestCPU::GetInput()
132 {
133   if (cur_input >= input_array.GetSize()) cur_input = 0;
134   return input_array[cur_input++];
135 }
136 
GetInputAt(int & input_pointer)137 inline int cTestCPU::GetInputAt(int & input_pointer)
138 {
139   if (input_pointer >= input_array.GetSize()) input_pointer = 0;
140   return input_array[input_pointer++];
141 }
142 
GetReceiveValue()143 inline int cTestCPU::GetReceiveValue()
144 {
145   if (cur_receive >= receive_array.GetSize()) cur_receive = 0;
146   return receive_array[cur_receive++];
147 }
148 
GetResources(cAvidaContext & ctx)149 inline const tArray<double>& cTestCPU::GetResources(cAvidaContext& ctx)
150 {
151   return m_resource_count.GetResources(ctx);
152 }
153 
GetFacedCellResources(cAvidaContext & ctx)154 inline const tArray<double>& cTestCPU::GetFacedCellResources(cAvidaContext& ctx)
155 {
156   return m_faced_cell_resource_count.GetResources(ctx);
157 }
158 
GetFacedAvatarResources(cAvidaContext & ctx)159 inline const tArray<double>& cTestCPU::GetFacedAvatarResources(cAvidaContext& ctx)
160 {
161   return m_faced_cell_resource_count.GetResources(ctx);
162 }
163 
GetDemeResources(int deme_id,cAvidaContext & ctx)164 inline const tArray<double>& cTestCPU::GetDemeResources(int deme_id, cAvidaContext& ctx)
165 {
166     return m_deme_resource_count.GetResources(ctx);
167 }
168 
GetCellResources(int cell_id,cAvidaContext & ctx)169 inline const tArray<double>& cTestCPU::GetCellResources(int cell_id, cAvidaContext& ctx)
170 {
171   return m_cell_resource_count.GetResources(ctx);
172 }
173 
GetAvatarResources(cAvidaContext & ctx)174 inline const tArray<double>& cTestCPU::GetAvatarResources(cAvidaContext& ctx)
175 {
176   return m_cell_resource_count.GetResources(ctx);
177 }
178 
GetFrozenResources(cAvidaContext & ctx,int cell_id)179 inline const tArray<double>& cTestCPU::GetFrozenResources(cAvidaContext& ctx, int cell_id)
180 {
181   return m_cell_resource_count.GetResources(ctx);
182 }
183 
GetCellIdLists()184 inline const tArray< tArray<int> >& cTestCPU::GetCellIdLists()
185 {
186 	return m_resource_count.GetCellIdLists();
187 }
188 
SetResource(cAvidaContext & ctx,int id,double new_level)189 inline void cTestCPU::SetResource(cAvidaContext& ctx, int id, double new_level)
190 {
191   m_resource_count.Set(ctx, id, new_level);
192 }
193 
194 #endif
195