Goose Management Model ODdox  1.02
GooseMemoryMap.cpp
Go to the documentation of this file.
1 //
6 #include <cmath>
7 #include <vector>
8 #include <list>
9 #include <iostream>
10 #include <fstream>
11 #include <string>
12 #include "../Landscape/ls.h"
13 #include "GooseMemoryMap.h"
14 #include "../BatchALMaSS/PopulationManager.h"
15 #include "Goose_Base.h"
16 
17 using namespace std;
18 
20 static CfgFloat cfg_goose_mem_threatdecayrate("GOOSE_MEM_THREATDECAYRATE", CFG_CUSTOM, 0.1);
22 static CfgInt cfg_goose_mem_minmemoryvalue("GOOSE_MEM_MINMEMVALUE", CFG_CUSTOM, 30);
24 static CfgInt cfg_goose_mem_expectedforagingtime("GOOSE_MEM_EXPECTEDFORAGINGTIME", CFG_CUSTOM, 120);
25 int GooseMemoryLocation::m_infinitememorystop = 0; // Initialize static variable
26 
29 
30 //*******************************************************************************************************
33 {
34  return o.ShouldDelete();
35 }
36 
37 //*******************************************************************************************************
38 
40 {
41  m_myOwner = a_owner;
42  m_threatdecayrate = cfg_goose_mem_threatdecayrate.value();
43  m_expectedforagingtime = cfg_goose_mem_expectedforagingtime.value();
45  aloc.m_infinitememorystop = cfg_goose_mem_minmemoryvalue.value(); // Set the static variable
46  //m_memorylocations.reserve(200); // there are usually 200-300 memories, reserving this in advance does not hurt, and speeds the process when new elements are added (a bit).
47 }
48 
49 
51 {
58  if (m_memorylocations.size() == 0)
59  {
60  m_memorylocations.push_back(a_gml);
61  return;
62  }
63  int polyid = a_gml.m_polygonid;
64  std::vector<GooseMemoryLocation>::iterator ci;
65  for (ci = m_memorylocations.begin(); ci != m_memorylocations.end(); ++ci)
66  {
67  if (ci->m_polygonid>polyid)
68  {
69  m_memorylocations.insert(ci, a_gml);
70  return;
71  }
72  if (ci->m_polygonid == polyid)
73  {
74  // We have been here before so update memory
75  ci->m_grain = a_gml.m_grain;
76  ci->m_maize = a_gml.m_maize;
77  ci->m_grazing = a_gml.m_grazing;
78  ci->m_foodresource = a_gml.m_foodresource;
79  return;
80  }
81  // the only possibility left is that polyid is > than any on our list, so push it to the end
82  }
83  m_memorylocations.push_back(a_gml);
84 }
85 //-------------------------------------------------------------------------------------------------------
86 
87 bool GooseMemoryMap::MemDel(int a_polyid)
88 {
93  for (std::vector<GooseMemoryLocation>::iterator ci = m_memorylocations.begin(); ci != m_memorylocations.end(); ++ci)
94  {
95  if (ci->m_polygonid == a_polyid)
96  {
97  m_memorylocations.erase(ci);
98  return true;
99  }
100  }
101  return false;
102 }
103 //-------------------------------------------------------------------------------------------------------
104 
105 bool GooseMemoryMap::ChangeAddFoodRes(int a_polyid, double a_foodres)
106 {
110  std::vector<GooseMemoryLocation>::iterator it;
111  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
112  {
113  if (it->m_polygonid == a_polyid)
114  {
115  it->m_foodresource += a_foodres;
116  return true; // all good
117  }
118  }
119  return false; // entry not found
120 }
121 //-------------------------------------------------------------------------------------------------------
122 
123 bool GooseMemoryMap::ChangeAddThreat(int a_polyid, double a_threat)
124 {
128  std::vector<GooseMemoryLocation>::iterator it;
129  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
130  {
131  if (it->m_polygonid == a_polyid)
132  {
133  it->m_threat += a_threat;
134  return true; // all good
135  }
136  }
137  return false; // entry not found
138 }
139 //-------------------------------------------------------------------------------------------------------
140 
141 bool GooseMemoryMap::ChangeMultFoodRes(int a_polyid, double a_foodres)
142 {
146  std::vector<GooseMemoryLocation>::iterator it;
147  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
148  {
149  if (it->m_polygonid == a_polyid)
150  {
151  it->m_foodresource *= a_foodres;
152  return true; // all good
153  }
154  }
155  return false; // entry not found
156 }
157 //-------------------------------------------------------------------------------------------------------
158 
159 bool GooseMemoryMap::ChangeSetFoodRes(int a_polyid, double a_grain, double a_maize, double a_grazing)
160 {
167  std::vector<GooseMemoryLocation>::iterator it;
168  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
169  {
170  if (it->m_polygonid == a_polyid)
171  {
172  it->m_grain = a_grain;
173  it->m_maize = a_maize;
174  it->m_grazing = a_grazing;
175  it->m_foodresource = m_myOwner->GetMaxIntakeRate(a_grain, a_maize, a_grazing);
176  APoint roost = m_myOwner->GetRoost();
177  int dist = g_AlmassMathFuncs.CalcDistPythagorasApprox( roost.m_x, roost.m_y, it->m_x, it->m_y );
178  it->m_score = CalcScore(dist, it->m_foodresource, it->m_threat);
179  return true; // all good
180  }
181  }
182  return false; // entry not found
183 }
184 //-------------------------------------------------------------------------------------------------------
185 
186 
187 bool GooseMemoryMap::ChangeMultThreat(int a_polyid, double a_threat)
188 {
192  std::vector<GooseMemoryLocation>::iterator it;
193  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
194  {
195  if (it->m_polygonid == a_polyid)
196  {
197  it->m_threat *= a_threat;
198  return true; // all good
199  }
200  }
201  return false; // entry not found
202 }
203 //-------------------------------------------------------------------------------------------------------
204 
205 
206 double GooseMemoryMap::GetFoodRes(int a_polyid)
207 {
211  std::vector<GooseMemoryLocation>::const_iterator ci;
212  for (ci = m_memorylocations.begin(); ci != m_memorylocations.end(); ++ci)
213  {
214  if (ci->m_polygonid == a_polyid) return ci->m_foodresource;
215  }
216  GooseMemoryError("Polygonid not found", a_polyid);
217  return 0; // compiler happiness
218 }
219 //-------------------------------------------------------------------------------------------------------
220 
221 double GooseMemoryMap::GetThreat(int a_polyid)
222 {
226  std::vector<GooseMemoryLocation>::const_iterator ci;
227  for (ci = m_memorylocations.begin(); ci != m_memorylocations.end(); ++ci)
228  {
229  if (ci->m_polygonid == a_polyid) return ci->m_threat;
230  }
231  GooseMemoryError("Polygonid not found", a_polyid);
232  return 0; // compiler happiness
233 }
234 //-------------------------------------------------------------------------------------------------------
235 
237 {
246  double score = -9999.0;
247  if (m_memorylocations.size()<1)
248  {
250  gml.m_score = score;
251  return gml;
252  }
253 
254  std::vector<GooseMemoryLocation>::iterator ci, ci_best;
255  for (ci = m_memorylocations.begin(); ci != m_memorylocations.end(); ++ci)
256  {
257  if (ci->m_score>score)
258  {
259  ci_best = ci;
260  score = ci->m_score;
261  }
262  }
263  if (score < 1)
264  {
266  gml.m_score = -9999;
267  return gml;
268  }
269  return *(ci_best);
270 }
271 //-------------------------------------------------------------------------------------------------------
272 
273 double GooseMemoryMap::CalcScore(int a_dist, double a_foodresource, double a_threat)
274 {
275  double cost = a_dist * m_myOwner->GetFlightCost();
276  double score = (a_foodresource * m_expectedforagingtime) - cost;
277  score = score * (1 - a_threat);
278  return score;
279 }
280 //-------------------------------------------------------------------------------------------------------
281 
282 bool GooseMemoryMap::IsKnownArea(int a_polyid)
283 {
285  std::vector<GooseMemoryLocation>::iterator it;
286  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
287  {
289  if (it->m_polygonid == a_polyid) return true;
290  }
291  return false;
292 }
293 //-------------------------------------------------------------------------------------------------------
294 
296 {
304  vector<GooseMemoryLocation>::iterator it;
305  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); it++)
306  {
307  // 2 - Decay and age the memory
308  it->m_threat *= m_threatdecayrate;
309  it->m_age++;
310  // 3. Update the memory score
311  int rx = m_myOwner->GetRoost().m_x;
312  int ry = m_myOwner->GetRoost().m_y;
313  int dist = g_AlmassMathFuncs.CalcDistPythagorasApprox( rx, ry, it->m_x, it->m_y );
314  double flightcost = m_myOwner->GetFlightCost();
315  it->CalcScore(dist, flightcost, m_expectedforagingtime);
316  }
317  // 4. Remove memories too old
318  m_memorylocations.erase(remove_if(m_memorylocations.begin(), m_memorylocations.end(), IsMarkedToDelete), m_memorylocations.end());
319 }
320 
321 //-------------------------------------------------------------------------------------------------------
322 
324 {
325  double theThreat = 0.0;
327  std::vector<GooseMemoryLocation>::iterator it;
328  for (it = m_memorylocations.begin(); it != m_memorylocations.end(); ++it)
329  {
331  theThreat += it->m_threat;
332  }
333  return theThreat;
334 }
336 {
337  m_memorylocations.erase(m_memorylocations.begin(), m_memorylocations.end());
338 }
339 //-------------------------------------------------------------------------------------------------------
340 
341 void GooseMemoryMap::GooseMemoryError(std::string a_str, int a_value)
342 {
344  std::ofstream ofile("GooseMemoryMapError.txt", std::ios::app | std::ios::out);
345  ofile << a_str << ':' << a_value << std::endl;
346  // Dumping goose memory
347  std::vector<GooseMemoryLocation>::iterator ci;
348  ofile << "polygonid" << '\t' << "m_x" << '\t' << "m_y" << '\t' << "foodresource" << '\t' << "threat" << std::endl;
349  for (ci = m_memorylocations.begin(); ci != m_memorylocations.end(); ++ci)
350  {
351  ofile << ci->m_polygonid << '\t' << ci->m_x << '\t' << ci->m_y << '\t' << ci->m_foodresource << '\t' << ci->m_threat << std::endl;
352  }
353  ofile.close();
354  exit(9);
355 }
356 //-------------------------------------------------------------------------------------------------------
cfg_goose_mem_minmemoryvalue
static CfgInt cfg_goose_mem_minmemoryvalue("GOOSE_MEM_MINMEMVALUE", CFG_CUSTOM, 30)
GooseMemoryMap::ChangeAddFoodRes
bool ChangeAddFoodRes(int a_polyid, double a_foodres)
Add to food at a memory location.
Definition: GooseMemoryMap.cpp:105
cfg_goose_mem_threatdecayrate
static CfgFloat cfg_goose_mem_threatdecayrate("GOOSE_MEM_THREATDECAYRATE", CFG_CUSTOM, 0.1)
GooseMemoryMap::MemDel
bool MemDel(int a_polyid)
Delete a memory location.
Definition: GooseMemoryMap.cpp:87
GooseMemoryLocation::ShouldDelete
bool ShouldDelete()
Test to see if a memory is too old to remember any longer.
Definition: GooseMemoryMap.h:85
GooseMemoryLocation
a data structure to hold goose memory location attributes
Definition: GooseMemoryMap.h:41
GooseMemoryLocation::m_score
double m_score
A score used to assess the location.
Definition: GooseMemoryMap.h:63
g_AlmassMathFuncs
ALMaSS_MathFuncs g_AlmassMathFuncs
This variable provides access the to the internal ALMaSS math functions.
GooseMemoryLocation::m_grazing
double m_grazing
the grazing resource at the location as intake rate kJ/min
Definition: GooseMemoryMap.h:57
GooseMemoryMap::GetFoodRes
double GetFoodRes(int a_polyid)
Get the food resource at location.
Definition: GooseMemoryMap.cpp:206
GooseMemoryMap::ChangeSetFoodRes
bool ChangeSetFoodRes(int a_polyid, double a_grain, double a_maize, double a_grazing)
Set food at a memory location.
Definition: GooseMemoryMap.cpp:159
GooseMemoryMap::GooseMemoryError
void GooseMemoryError(std::string a_str, int a_value)
Error message functionality.
Definition: GooseMemoryMap.cpp:341
GooseMemoryMap::ChangeAddThreat
bool ChangeAddThreat(int a_polyid, double a_threat)
Add to threat at a memory location.
Definition: GooseMemoryMap.cpp:123
ALMaSS_MathFuncs
ALMaSS_MathFuncs constructor.
Definition: Misc.h:28
GooseMemoryMap::CalcScore
double CalcScore(int a_dist, double a_foodresource, double a_threat)
Inline function to calulate overall score for a distance, resource and threat.
Definition: GooseMemoryMap.cpp:273
GooseMemoryMap::GooseMemoryMap
GooseMemoryMap(Goose_Base *a_owner)
Constructor.
Definition: GooseMemoryMap.cpp:39
GooseMemoryLocation::m_foodresource
double m_foodresource
The max food intake rate (kJ/min) at the location.
Definition: GooseMemoryMap.h:59
cfg_goose_mem_expectedforagingtime
static CfgInt cfg_goose_mem_expectedforagingtime("GOOSE_MEM_EXPECTEDFORAGINGTIME", CFG_CUSTOM, 120)
GooseMemoryLocation::m_infinitememorystop
static int m_infinitememorystop
the age at which a memory is removed.
Definition: GooseMemoryMap.h:65
GooseMemoryMap::ChangeMultFoodRes
bool ChangeMultFoodRes(int a_polyid, double a_foodres)
Multiply food at a memory location.
Definition: GooseMemoryMap.cpp:141
GooseMemoryMap::GetBestFeedingScore
GooseMemoryLocation GetBestFeedingScore()
Find the current best feeding location.
Definition: GooseMemoryMap.cpp:236
IsMarkedToDelete
bool IsMarkedToDelete(GooseMemoryLocation &o)
Definition: GooseMemoryMap.cpp:32
GooseMemoryMap::GetTotalThreats
double GetTotalThreats(void)
Get total of threat currently remembered.
Definition: GooseMemoryMap.cpp:323
GooseMemoryLocation::m_maize
double m_maize
the maize resource at the location as intake rate kJ/min
Definition: GooseMemoryMap.h:55
GooseMemoryMap.h
Header file for the Goose Memory Map and associated classes.
CfgFloat::value
double value(void)
Definition: configurator.h:118
CFG_CUSTOM
Definition: configurator.h:60
GooseMemoryMap::GetThreat
double GetThreat(int a_polyid)
Get the threat level at location.
Definition: GooseMemoryMap.cpp:221
GooseMemoryMap::DecayAllMemory
void DecayAllMemory()
Decay all memory.
Definition: GooseMemoryMap.cpp:295
CfgInt
Integer configurator entry class.
Definition: configurator.h:87
Goose_Base.h
Goose_Base.h This is the header file for the goose base class
Goose_Base
A class to describe the goose base.
Definition: Goose_Base.h:131
ALMaSS_MathFuncs::CalcDistPythagorasApprox
int CalcDistPythagorasApprox(int a_x, int a_y, int a_x1, int a_y1)
Calculate distance using the Pythagoras approximation.
Definition: misc.cpp:49
GooseMemoryMap::ClearAllMemory
void ClearAllMemory(void)
Remove all memory.
Definition: GooseMemoryMap.cpp:335
GooseMemoryMap::IsKnownArea
bool IsKnownArea(int a_polyid)
Check if this location is known.
Definition: GooseMemoryMap.cpp:282
CfgFloat
Double configurator entry class.
Definition: configurator.h:106
GooseMemoryLocation::m_polygonid
int m_polygonid
the unique polygon identification
Definition: GooseMemoryMap.h:51
CfgInt::value
int value(void)
Definition: configurator.h:98
GooseMemoryMap::MemAdd
void MemAdd(GooseMemoryLocation a_gml)
Add a new memory location.
Definition: GooseMemoryMap.cpp:50
GooseMemoryMap::ChangeMultThreat
bool ChangeMultThreat(int a_polyid, double a_threat)
Multiply threat at a memory location.
Definition: GooseMemoryMap.cpp:187
GooseMemoryLocation::m_grain
double m_grain
the grain resource at the location as intake rate kJ/min
Definition: GooseMemoryMap.h:53