1 /*
2  *  cTaskLib.h
3  *  Avida
4  *
5  *  Called "task_lib.hh" prior to 12/5/05.
6  *  Copyright 1999-2011 Michigan State University. All rights reserved.
7  *  Copyright 1993-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 cTaskLib_h
24 #define cTaskLib_h
25 
26 #ifndef cTaskContext_h
27 #include "cTaskContext.h"
28 #endif
29 #ifndef cTaskEntry_h
30 #include "cTaskEntry.h"
31 #endif
32 #ifndef tArray_h
33 #include "tArray.h"
34 #endif
35 #ifndef cWorld_h
36 #include "cWorld.h"
37 #endif
38 #ifndef cStats_h
39 #include "cStats.h"
40 #endif
41 
42 
43 
44 class cEnvReqs;
45 class cString;
46 class cWorld;
47 
48 
49 class cTaskLib
50 {
51 private:
52   cWorld* m_world;
53   tArray<cTaskEntry*> task_array;
54 
55   // What extra information should be sent along when we are evaluating
56   // which tasks have been performed?
57   bool use_neighbor_input;
58   bool use_neighbor_output;
59 
60   enum req_list
61   {
62     REQ_NEIGHBOR_INPUT=1,
63     REQ_NEIGHBOR_OUTPUT=2,
64     UNUSED_REQ_C=4,
65     UNUSED_REQ_D=8
66   };
67 
68 
69   cTaskLib(const cTaskLib&); // @not_implemented
70   cTaskLib& operator=(const cTaskLib&); // @not_implemented
71 
72 public:
cTaskLib(cWorld * world)73   cTaskLib(cWorld* world) : m_world(world), use_neighbor_input(false), use_neighbor_output(false) { ; }
74   ~cTaskLib();
75 
GetSize()76   int GetSize() const { return task_array.GetSize(); }
77 
78   cTaskEntry* AddTask(const cString& name, const cString& info, cEnvReqs& envreqs, Feedback& feedback);
GetTask(int id)79   const cTaskEntry& GetTask(int id) const { return *(task_array[id]); }
GetTaskReference(int id)80   cTaskEntry * GetTaskReference(int id) { return task_array[id]; }
81 
82   void SetupTests(cTaskContext& ctx) const;
TestOutput(cTaskContext & ctx)83   inline double TestOutput(cTaskContext& ctx) const { return (this->*(ctx.GetTaskEntry()->GetTestFun()))(ctx); }
84 
UseNeighborInput()85   bool UseNeighborInput() const { return use_neighbor_input; }
UseNeighborOutput()86   bool UseNeighborOutput() const { return use_neighbor_output; }
87 
88 	// Get the strings that parameterize the MatchString tasks
89 	vector<cString> GetMatchStrings();
90 	cString GetMatchString(int x);
GetNumberOfMatchStrings()91 	int GetNumberOfMatchStrings() { return m_strings.size(); }
92 
93 private:
94 	// Store the strings used by the MatchString tasks
95 	vector<cString> m_strings;
96 
97 
98 private:
99 
100   void NewTask(const cString& name, const cString& desc, tTaskTest task_fun, int reqs = 0, cArgContainer* args = NULL);
101 
102   inline double FractionalReward(unsigned int supplied, unsigned int correct);
103 
104   // All tasks must be declared here, taking a cTaskContext reference as the sole input and
105   // returning a double between 0.0 and 1.0 indicating the quality of how well the task was
106   // performed.
107 
108   // Basic Tasks
109   double Task_Echo(cTaskContext& ctx) const;
110   double Task_Add(cTaskContext& ctx) const;
111   double Task_Add3(cTaskContext& ctx) const;
112   double Task_Sub(cTaskContext& ctx) const;
113   double Task_DontCare(cTaskContext& ctx) const;
114 
115   // All 1- and 2-Input Logic Functions
116   double Task_Not(cTaskContext& ctx) const;
117   double Task_Nand(cTaskContext& ctx) const;
118   double Task_And(cTaskContext& ctx) const;
119   double Task_OrNot(cTaskContext& ctx) const;
120   double Task_Or(cTaskContext& ctx) const;
121   double Task_AndNot(cTaskContext& ctx) const;
122   double Task_Nor(cTaskContext& ctx) const;
123   double Task_Xor(cTaskContext& ctx) const;
124   double Task_Equ(cTaskContext& ctx) const;
125 
126 	// resource dependent
127 	double Task_Nand_ResourceDependent(cTaskContext& ctx) const;
128 	double Task_Nor_ResourceDependent(cTaskContext& ctx) const;
129 
130 
131   // All 3-Input Logic Functions
132   double Task_Logic3in_AA(cTaskContext& ctx) const;
133   double Task_Logic3in_AB(cTaskContext& ctx) const;
134   double Task_Logic3in_AC(cTaskContext& ctx) const;
135   double Task_Logic3in_AD(cTaskContext& ctx) const;
136   double Task_Logic3in_AE(cTaskContext& ctx) const;
137   double Task_Logic3in_AF(cTaskContext& ctx) const;
138   double Task_Logic3in_AG(cTaskContext& ctx) const;
139   double Task_Logic3in_AH(cTaskContext& ctx) const;
140   double Task_Logic3in_AI(cTaskContext& ctx) const;
141   double Task_Logic3in_AJ(cTaskContext& ctx) const;
142   double Task_Logic3in_AK(cTaskContext& ctx) const;
143   double Task_Logic3in_AL(cTaskContext& ctx) const;
144   double Task_Logic3in_AM(cTaskContext& ctx) const;
145   double Task_Logic3in_AN(cTaskContext& ctx) const;
146   double Task_Logic3in_AO(cTaskContext& ctx) const;
147   double Task_Logic3in_AP(cTaskContext& ctx) const;
148   double Task_Logic3in_AQ(cTaskContext& ctx) const;
149   double Task_Logic3in_AR(cTaskContext& ctx) const;
150   double Task_Logic3in_AS(cTaskContext& ctx) const;
151   double Task_Logic3in_AT(cTaskContext& ctx) const;
152   double Task_Logic3in_AU(cTaskContext& ctx) const;
153   double Task_Logic3in_AV(cTaskContext& ctx) const;
154   double Task_Logic3in_AW(cTaskContext& ctx) const;
155   double Task_Logic3in_AX(cTaskContext& ctx) const;
156   double Task_Logic3in_AY(cTaskContext& ctx) const;
157   double Task_Logic3in_AZ(cTaskContext& ctx) const;
158   double Task_Logic3in_BA(cTaskContext& ctx) const;
159   double Task_Logic3in_BB(cTaskContext& ctx) const;
160   double Task_Logic3in_BC(cTaskContext& ctx) const;
161   double Task_Logic3in_BD(cTaskContext& ctx) const;
162   double Task_Logic3in_BE(cTaskContext& ctx) const;
163   double Task_Logic3in_BF(cTaskContext& ctx) const;
164   double Task_Logic3in_BG(cTaskContext& ctx) const;
165   double Task_Logic3in_BH(cTaskContext& ctx) const;
166   double Task_Logic3in_BI(cTaskContext& ctx) const;
167   double Task_Logic3in_BJ(cTaskContext& ctx) const;
168   double Task_Logic3in_BK(cTaskContext& ctx) const;
169   double Task_Logic3in_BL(cTaskContext& ctx) const;
170   double Task_Logic3in_BM(cTaskContext& ctx) const;
171   double Task_Logic3in_BN(cTaskContext& ctx) const;
172   double Task_Logic3in_BO(cTaskContext& ctx) const;
173   double Task_Logic3in_BP(cTaskContext& ctx) const;
174   double Task_Logic3in_BQ(cTaskContext& ctx) const;
175   double Task_Logic3in_BR(cTaskContext& ctx) const;
176   double Task_Logic3in_BS(cTaskContext& ctx) const;
177   double Task_Logic3in_BT(cTaskContext& ctx) const;
178   double Task_Logic3in_BU(cTaskContext& ctx) const;
179   double Task_Logic3in_BV(cTaskContext& ctx) const;
180   double Task_Logic3in_BW(cTaskContext& ctx) const;
181   double Task_Logic3in_BX(cTaskContext& ctx) const;
182   double Task_Logic3in_BY(cTaskContext& ctx) const;
183   double Task_Logic3in_BZ(cTaskContext& ctx) const;
184   double Task_Logic3in_CA(cTaskContext& ctx) const;
185   double Task_Logic3in_CB(cTaskContext& ctx) const;
186   double Task_Logic3in_CC(cTaskContext& ctx) const;
187   double Task_Logic3in_CD(cTaskContext& ctx) const;
188   double Task_Logic3in_CE(cTaskContext& ctx) const;
189   double Task_Logic3in_CF(cTaskContext& ctx) const;
190   double Task_Logic3in_CG(cTaskContext& ctx) const;
191   double Task_Logic3in_CH(cTaskContext& ctx) const;
192   double Task_Logic3in_CI(cTaskContext& ctx) const;
193   double Task_Logic3in_CJ(cTaskContext& ctx) const;
194   double Task_Logic3in_CK(cTaskContext& ctx) const;
195   double Task_Logic3in_CL(cTaskContext& ctx) const;
196   double Task_Logic3in_CM(cTaskContext& ctx) const;
197   double Task_Logic3in_CN(cTaskContext& ctx) const;
198   double Task_Logic3in_CO(cTaskContext& ctx) const;
199   double Task_Logic3in_CP(cTaskContext& ctx) const;
200 
201   // Arbitrary 1-Input Math Tasks
202   double Task_Math1in_AA(cTaskContext& ctx) const;
203   double Task_Math1in_AB(cTaskContext& ctx) const;
204   double Task_Math1in_AC(cTaskContext& ctx) const;
205   double Task_Math1in_AD(cTaskContext& ctx) const;
206   double Task_Math1in_AE(cTaskContext& ctx) const;
207   double Task_Math1in_AF(cTaskContext& ctx) const;
208   double Task_Math1in_AG(cTaskContext& ctx) const;
209   double Task_Math1in_AH(cTaskContext& ctx) const;
210   double Task_Math1in_AI(cTaskContext& ctx) const;
211   double Task_Math1in_AJ(cTaskContext& ctx) const;
212   double Task_Math1in_AK(cTaskContext& ctx) const;
213   double Task_Math1in_AL(cTaskContext& ctx) const;
214   double Task_Math1in_AM(cTaskContext& ctx) const;
215   double Task_Math1in_AN(cTaskContext& ctx) const;
216   double Task_Math1in_AO(cTaskContext& ctx) const;
217   double Task_Math1in_AP(cTaskContext& ctx) const;
218 
219   // Arbitrary 2-Input Math Tasks
220   double Task_Math2in_AA(cTaskContext& ctx) const;
221   double Task_Math2in_AB(cTaskContext& ctx) const;
222   double Task_Math2in_AC(cTaskContext& ctx) const;
223   double Task_Math2in_AD(cTaskContext& ctx) const;
224   double Task_Math2in_AE(cTaskContext& ctx) const;
225   double Task_Math2in_AF(cTaskContext& ctx) const;
226   double Task_Math2in_AG(cTaskContext& ctx) const;
227   double Task_Math2in_AH(cTaskContext& ctx) const;
228   double Task_Math2in_AI(cTaskContext& ctx) const;
229   double Task_Math2in_AJ(cTaskContext& ctx) const;
230   double Task_Math2in_AK(cTaskContext& ctx) const;
231   double Task_Math2in_AL(cTaskContext& ctx) const;
232   double Task_Math2in_AM(cTaskContext& ctx) const;
233   double Task_Math2in_AN(cTaskContext& ctx) const;
234   double Task_Math2in_AO(cTaskContext& ctx) const;
235   double Task_Math2in_AP(cTaskContext& ctx) const;
236   double Task_Math2in_AQ(cTaskContext& ctx) const;
237   double Task_Math2in_AR(cTaskContext& ctx) const;
238   double Task_Math2in_AS(cTaskContext& ctx) const;
239   double Task_Math2in_AT(cTaskContext& ctx) const;
240   double Task_Math2in_AU(cTaskContext& ctx) const;
241   double Task_Math2in_AV(cTaskContext& ctx) const;
242 
243   // Arbitrary 3-Input Math Tasks
244   double Task_Math3in_AA(cTaskContext& ctx) const;
245   double Task_Math3in_AB(cTaskContext& ctx) const;
246   double Task_Math3in_AC(cTaskContext& ctx) const;
247   double Task_Math3in_AD(cTaskContext& ctx) const;
248   double Task_Math3in_AE(cTaskContext& ctx) const;
249   double Task_Math3in_AF(cTaskContext& ctx) const;
250   double Task_Math3in_AG(cTaskContext& ctx) const;
251   double Task_Math3in_AH(cTaskContext& ctx) const;
252   double Task_Math3in_AI(cTaskContext& ctx) const;
253   double Task_Math3in_AJ(cTaskContext& ctx) const;
254   double Task_Math3in_AK(cTaskContext& ctx) const;
255   double Task_Math3in_AL(cTaskContext& ctx) const;
256   double Task_Math3in_AM(cTaskContext& ctx) const;
257 
258   // Matching Tasks
259   void Load_MatchStr(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
260   double Task_MatchStr(cTaskContext& ctx) const;
261 	void Load_MatchProdStr(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
262   double Task_MatchProdStr(cTaskContext& ctx) const;
263   void Load_MatchNumber(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
264   double Task_MatchNumber(cTaskContext& ctx) const;
265 
266   // Sequence Tasks
267   void Load_SortInputs(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
268   double Task_SortInputs(cTaskContext& ctx) const;
269   void Load_FibonacciSequence(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
270   double Task_FibonacciSequence(cTaskContext& ctx) const;
271 
272   // Math Tasks
273   void Load_Mult(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
274   double Task_Mult(cTaskContext& ctx) const;
275   void Load_Div(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
276   double Task_Div(cTaskContext& ctx) const;
277   void Load_Log(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
278   double Task_Log(cTaskContext& ctx) const;
279   void Load_Log2(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
280   double Task_Log2(cTaskContext& ctx) const;
281   void Load_Log10(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
282   double Task_Log10(cTaskContext& ctx) const;
283   void Load_Sqrt(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
284   double Task_Sqrt(cTaskContext& ctx) const;
285   void Load_Sine(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
286   double Task_Sine(cTaskContext& ctx) const;
287   void Load_Cosine(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
288   double Task_Cosine(cTaskContext& ctx) const;
289 
290   // Optimization Tasks
291   void Load_Optimize(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
292   double Task_Optimize(cTaskContext& ctx) const;
293 
294 
295 
296   // Communication Tasks
297   double Task_CommEcho(cTaskContext& ctx) const;
298   double Task_CommNot(cTaskContext& ctx) const;
299 
300   // Network Tasks
301   double Task_NetSend(cTaskContext& ctx) const;
302   double Task_NetReceive(cTaskContext& ctx) const;
303 
304   // Movement tasks (temp, rely on hack)
305   double Task_MoveUpGradient(cTaskContext& ctx) const;
306   double Task_MoveNeutralGradient(cTaskContext& ctx) const;
307   double Task_MoveDownGradient(cTaskContext& ctx) const;
308   double Task_MoveNotUpGradient(cTaskContext& ctx) const;
309   double Task_MoveToRightSide(cTaskContext& ctx) const;
310   double Task_MoveToLeftSide(cTaskContext& ctx) const;
311 
312   // BDC Movement tasks
313   double Task_Move(cTaskContext& ctx) const;
314   double Task_MoveToTarget(cTaskContext& ctx) const;
315   double Task_MoveToMovementEvent(cTaskContext& ctx) const;
316   double Task_MoveBetweenMovementEvent(cTaskContext& ctx) const;
317 
318   // movement
319   double Task_MoveToEvent(cTaskContext& ctx) const;
320   double Task_EventKilled(cTaskContext& ctx) const;
321 
322   // State Grid Tasks
323   void Load_SGPathTraversal(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
324   double Task_SGPathTraversal(cTaskContext& ctx) const;
325 
326 	// reputation
327 	double Task_CreatePerfectStrings(cTaskContext& ctx) const;
328 
329 	// group formation
330 	void Load_FormSpatialGroup(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
331 	double Task_FormSpatialGroup(cTaskContext& ctx) const;
332 	void Load_FormSpatialGroupWithID(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
333 	double Task_FormSpatialGroupWithID(cTaskContext& ctx) const;
334 	void Load_LiveOnPatchRes(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
335 	double Task_LiveOnPatchRes(cTaskContext& ctx) const;
336 
337   // Feed Specific Tasks
338   void Load_ConsumeTarget(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
339   double Task_ConsumeTarget(cTaskContext& ctx) const;
340 
341 
342   void Load_CollectOdds(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
343   double Task_CollectOdds(cTaskContext& ctx) const;
344 
345   // String Matching Tasks
346 	void Load_AllOnes(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
347 	double Task_AllOnes(cTaskContext& ctx) const;
348 	void Load_RoyalRoad(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
349 	double Task_RoyalRoad(cTaskContext& ctx) const;
350 	void Load_RoyalRoadWithDitches(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
351 	double Task_RoyalRoadWithDitches(cTaskContext& ctx) const;
352 
353 	// division of labor
354 	void Load_OpinionIs(const cString& name, const cString& argstr, cEnvReqs& envreqs, Feedback& feedback);
355 	double Task_OpinionIs(cTaskContext& ctx) const;
356 
357 
358 
359 };
360 
361 #endif
362