Goose Management Model ODdox  1.02
GooseMemoryMap Class Reference

The class describing both local and seasonal goose memories and cognition. More...

#include <GooseMemoryMap.h>

Public Member Functions

double CalcScore (int a_dist, double a_foodresource, double a_threat)
 Inline function to calulate overall score for a distance, resource and threat. More...
 
bool ChangeAddFoodRes (int a_polyid, double a_foodres)
 Add to food at a memory location. More...
 
bool ChangeAddThreat (int a_polyid, double a_threat)
 Add to threat at a memory location. More...
 
bool ChangeMultFoodRes (int a_polyid, double a_foodres)
 Multiply food at a memory location. More...
 
bool ChangeMultThreat (int a_polyid, double a_threat)
 Multiply threat at a memory location. More...
 
bool ChangeSetFoodRes (int a_polyid, double a_grain, double a_maize, double a_grazing)
 Set food at a memory location. More...
 
void ClearAllMemory (void)
 Remove all memory. More...
 
void DecayAllMemory ()
 Decay all memory. More...
 
GooseMemoryLocation GetBestFeedingScore ()
 Find the current best feeding location. More...
 
double GetFoodRes (int a_polyid)
 Get the food resource at location. More...
 
double GetThreat (int a_polyid)
 Get the threat level at location. More...
 
double GetTotalThreats (void)
 Get total of threat currently remembered. More...
 
 GooseMemoryMap (Goose_Base *a_owner)
 Constructor. More...
 
bool IsKnownArea (int a_polyid)
 Check if this location is known. More...
 
void MemAdd (GooseMemoryLocation a_gml)
 Add a new memory location. More...
 
bool MemDel (int a_polyid)
 Delete a memory location. More...
 
 ~GooseMemoryMap ()
 Destructor. More...
 

Protected Member Functions

void GooseMemoryError (std::string a_str, int a_value)
 Error message functionality. More...
 

Protected Attributes

int m_expectedforagingtime
 the time in minutes a goose expects to be able to forage when evaluating cost benefit of flying to a location. More...
 
std::vector< GooseMemoryLocationm_memorylocations
 The list of memory locations. More...
 
Goose_Basem_myOwner
 pointer to the owner More...
 
double m_threatdecayrate
 The decay rate per day for threats. More...
 

Detailed Description

The class describing both local and seasonal goose memories and cognition.

Definition at line 93 of file GooseMemoryMap.h.

Constructor & Destructor Documentation

◆ GooseMemoryMap()

GooseMemoryMap::GooseMemoryMap ( Goose_Base a_owner)

Constructor.

Definition at line 39 of file GooseMemoryMap.cpp.

40 {
41  m_myOwner = a_owner;
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 }

References cfg_goose_mem_expectedforagingtime, cfg_goose_mem_minmemoryvalue, cfg_goose_mem_threatdecayrate, GooseMemoryLocation::m_infinitememorystop, CfgInt::value(), and CfgFloat::value().

◆ ~GooseMemoryMap()

GooseMemoryMap::~GooseMemoryMap ( )
inline

Destructor.

Definition at line 110 of file GooseMemoryMap.h.

110  {
112  }

References m_memorylocations.

Member Function Documentation

◆ CalcScore()

double GooseMemoryMap::CalcScore ( int  a_dist,
double  a_foodresource,
double  a_threat 
)

Inline function to calulate overall score for a distance, resource and threat.

Parameters
[in]a_distThe distance to the location
[in]a_foodresourceThe foodresource at the location
[in]a_threatA measure of the strength of threat memory.
Returns
the memory score calculation result

Definition at line 273 of file GooseMemoryMap.cpp.

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 }

Referenced by Goose_Base::EvaluateForageToHopLoc(), Goose_Base::On_Bang(), and Goose_Base::st_ChooseForageLocation().

◆ ChangeAddFoodRes()

bool GooseMemoryMap::ChangeAddFoodRes ( int  a_polyid,
double  a_foodres 
)

Add to food at a memory location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
a_foodres[in] A measure of the strength of food resource memory.
Returns
success

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find one bigger and add the food resource to be added

Definition at line 105 of file GooseMemoryMap.cpp.

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 }

◆ ChangeAddThreat()

bool GooseMemoryMap::ChangeAddThreat ( int  a_polyid,
double  a_threat 
)

Add to threat at a memory location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
a_threat[in] A measure of the strength of threat memory.
Returns
success

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find one bigger and add the threat to be added

Definition at line 123 of file GooseMemoryMap.cpp.

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 }

Referenced by Goose_Base::On_Bang().

◆ ChangeMultFoodRes()

bool GooseMemoryMap::ChangeMultFoodRes ( int  a_polyid,
double  a_foodres 
)

Multiply food at a memory location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
a_foodres[in] A measure of the strength of food resource memory.
Returns
success

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find one bigger and multiply by the value passed

Definition at line 141 of file GooseMemoryMap.cpp.

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 }

◆ ChangeMultThreat()

bool GooseMemoryMap::ChangeMultThreat ( int  a_polyid,
double  a_threat 
)

Multiply threat at a memory location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
a_threat[in] The memory of a threat at this location
Returns
success

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find one bigger and multiply by the value passed

Definition at line 187 of file GooseMemoryMap.cpp.

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 }

◆ ChangeSetFoodRes()

bool GooseMemoryMap::ChangeSetFoodRes ( int  a_polyid,
double  a_grain,
double  a_maize,
double  a_grazing 
)

Set food at a memory location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
a_grain[in] The grain food resource.
a_maize[in] The maize food resource.
a_grazing[in] The grazing food resource.
Returns
success

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find our polygon.

Todo:
Speed-up Identified as the program bottleneck. The problem is that in most cases this is called we run all the way to the end of the vector and don't find our polygon.

Definition at line 159 of file GooseMemoryMap.cpp.

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 }

References ALMaSS_MathFuncs::CalcDistPythagorasApprox(), and g_AlmassMathFuncs.

Referenced by Goose_Base::st_ChooseForageLocation(), and Goose_Base::st_Forage().

◆ ClearAllMemory()

void GooseMemoryMap::ClearAllMemory ( void  )

Remove all memory.

Returns
None

Definition at line 335 of file GooseMemoryMap.cpp.

336 {
338 }

◆ DecayAllMemory()

void GooseMemoryMap::DecayAllMemory ( void  )

Decay all memory.

1 - Loop through all locations 2 - Decay the memory 3 - Update the memory score 4 - Remove any memories that are old

Definition at line 295 of file GooseMemoryMap.cpp.

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
319 }

References ALMaSS_MathFuncs::CalcDistPythagorasApprox(), g_AlmassMathFuncs, and IsMarkedToDelete().

Referenced by Goose_Base::StartDay().

◆ GetBestFeedingScore()

GooseMemoryLocation GooseMemoryMap::GetBestFeedingScore ( )

Find the current best feeding location.

Returns
Best food resource memory in memory after taking threats and distance from roost into account

0 - If there are no memories return an impossible score to trigger explore 1 - Loop through all locations. 2 - Keep track of the best score. 3 - We need a way out if nothing is at all suitable, so if score is too small return a dummy location with a big negative score. 4 - Return the best scoring location if we have one.

Definition at line 236 of file GooseMemoryMap.cpp.

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 }

References GooseMemoryLocation::m_score.

Referenced by Goose_Base::st_ChooseForageLocation().

◆ GetFoodRes()

double GooseMemoryMap::GetFoodRes ( int  a_polyid)

Get the food resource at location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
Returns
Food resource memory at a_polyid

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list.

Definition at line 206 of file GooseMemoryMap.cpp.

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 }

◆ GetThreat()

double GooseMemoryMap::GetThreat ( int  a_polyid)

Get the threat level at location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
Returns
Threat memory at a_polyid

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list.

Definition at line 221 of file GooseMemoryMap.cpp.

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 }

◆ GetTotalThreats()

double GooseMemoryMap::GetTotalThreats ( void  )

Get total of threat currently remembered.

Returns
The sum of total threats remembered

1 - Loop through all locations.

2 - Sum the threats and return this.

Definition at line 323 of file GooseMemoryMap.cpp.

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 }

◆ GooseMemoryError()

void GooseMemoryMap::GooseMemoryError ( std::string  a_str,
int  a_value 
)
protected

Error message functionality.

Parameters
[in]a_strA string to send as output
[in]a_valueA value to send as output

Opens a file called GooseMemoryMapError.txt and prints an error message before dumping the contents of the current memory. The program then closes.

Definition at line 341 of file GooseMemoryMap.cpp.

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 }

◆ IsKnownArea()

bool GooseMemoryMap::IsKnownArea ( int  a_polyid)

Check if this location is known.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
Returns
true if we have this polygon otherwise false.

Loop through all locations and see if we have this polygon

If we have it, return true, otherwise return false.

Definition at line 282 of file GooseMemoryMap.cpp.

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 }

Referenced by Goose_Base::st_ChooseForageLocation().

◆ MemAdd()

void GooseMemoryMap::MemAdd ( GooseMemoryLocation  a_gml)

Add a new memory location.

Parameters
a_gml[in] A goose memory location to add to the memory list.

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find one bigger and insert before it.
However, perhaps we have been here before, so just in case make a test and quitely update the memory if we find this polygon id in the list.
If no polygon ids are bigger or equal to ours then we need to add ours to the end of the list.

Definition at line 50 of file GooseMemoryMap.cpp.

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 }

References GooseMemoryLocation::m_foodresource, GooseMemoryLocation::m_grain, GooseMemoryLocation::m_grazing, GooseMemoryLocation::m_maize, and GooseMemoryLocation::m_polygonid.

Referenced by Goose_Base::EvaluateForageToHopLoc(), Goose_Base::On_Bang(), and Goose_Base::st_ChooseForageLocation().

◆ MemDel()

bool GooseMemoryMap::MemDel ( int  a_polyid)

Delete a memory location.

Parameters
a_polyid[in] A unique polygon reference to a goose memory location.
Returns
success

Here we make use of the fact that there should only be one of each entry, so no need to read the whole list, just stop when we find the right one and remove it

Definition at line 87 of file GooseMemoryMap.cpp.

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 }

Referenced by Goose_Base::st_Forage().

Member Data Documentation

◆ m_expectedforagingtime

int GooseMemoryMap::m_expectedforagingtime
protected

the time in minutes a goose expects to be able to forage when evaluating cost benefit of flying to a location.

Definition at line 101 of file GooseMemoryMap.h.

◆ m_memorylocations

std::vector<GooseMemoryLocation> GooseMemoryMap::m_memorylocations
protected

The list of memory locations.

Definition at line 103 of file GooseMemoryMap.h.

Referenced by ~GooseMemoryMap().

◆ m_myOwner

Goose_Base* GooseMemoryMap::m_myOwner
protected

pointer to the owner

Definition at line 97 of file GooseMemoryMap.h.

◆ m_threatdecayrate

double GooseMemoryMap::m_threatdecayrate
protected

The decay rate per day for threats.

Definition at line 99 of file GooseMemoryMap.h.


The documentation for this class was generated from the following files:
GooseMemoryMap::m_myOwner
Goose_Base * m_myOwner
pointer to the owner
Definition: GooseMemoryMap.h:97
Goose_Base::GetMaxIntakeRate
double GetMaxIntakeRate(double a_grain, double a_maize, double a_grass)
Returns the max intake rate of the three different types of intake.
Definition: Goose_Base.h:536
Goose_Base::GetFlightCost
double GetFlightCost()
Get the flight costs per m per g.
Definition: Goose_Base.h:568
cfg_goose_mem_minmemoryvalue
static CfgInt cfg_goose_mem_minmemoryvalue("GOOSE_MEM_MINMEMVALUE", CFG_CUSTOM, 30)
cfg_goose_mem_threatdecayrate
static CfgFloat cfg_goose_mem_threatdecayrate("GOOSE_MEM_THREATDECAYRATE", CFG_CUSTOM, 0.1)
GooseMemoryMap::m_expectedforagingtime
int m_expectedforagingtime
the time in minutes a goose expects to be able to forage when evaluating cost benefit of flying to a ...
Definition: GooseMemoryMap.h:101
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::GooseMemoryError
void GooseMemoryError(std::string a_str, int a_value)
Error message functionality.
Definition: GooseMemoryMap.cpp:341
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
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::m_threatdecayrate
double m_threatdecayrate
The decay rate per day for threats.
Definition: GooseMemoryMap.h:99
IsMarkedToDelete
bool IsMarkedToDelete(GooseMemoryLocation &o)
Definition: GooseMemoryMap.cpp:32
GooseMemoryLocation::m_maize
double m_maize
the maize resource at the location as intake rate kJ/min
Definition: GooseMemoryMap.h:55
CfgFloat::value
double value(void)
Definition: configurator.h:118
GooseMemoryMap::m_memorylocations
std::vector< GooseMemoryLocation > m_memorylocations
The list of memory locations.
Definition: GooseMemoryMap.h:103
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
GooseMemoryLocation::m_polygonid
int m_polygonid
the unique polygon identification
Definition: GooseMemoryMap.h:51
CfgInt::value
int value(void)
Definition: configurator.h:98
Goose_Base::GetRoost
APoint GetRoost()
Supply roost coords.
Definition: Goose_Base.h:345
GooseMemoryLocation::m_grain
double m_grain
the grain resource at the location as intake rate kJ/min
Definition: GooseMemoryMap.h:53