Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/predator.cc
[mareframe] / trunk / gadget / predator.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/predator.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "keeper.h"
2 :     #include "prey.h"
3 :     #include "suits.h"
4 :     #include "predator.h"
5 :     #include "errorhandler.h"
6 :     #include "readfunc.h"
7 :     #include "gadget.h"
8 :     #include "global.h"
9 :    
10 :     Predator::Predator(const char* givenname, const IntVector& Areas)
11 :     : HasName(givenname), LivesOnAreas(Areas), suitable(0) {
12 :     }
13 :    
14 :     Predator::~Predator() {
15 :     if (suitable != 0)
16 :     delete suitable;
17 :     }
18 :    
19 :     void Predator::setPrey(PreyPtrVector& preyvec, Keeper* const keeper) {
20 :     int i, j;
21 :     int found = 0;
22 :    
23 :     if (suitable == 0)
24 :     handle.logMessage(LOGFAIL, "Error in predator - found no suitability values for predator", this->getName());
25 :    
26 :     preys.resizeBlank(suitable->numPreys());
27 :     IntVector check(suitable->numPreys(), 0);
28 :     for (i = 0; i < preyvec.Size(); i++) {
29 :     found = 0;
30 :     for (j = 0; j < suitable->numPreys(); j++) {
31 :     if (strcasecmp(suitable->getPreyName(j), preyvec[i]->getName()) == 0) {
32 :     if (found == 0) {
33 :     preys[j] = preyvec[i];
34 :     check[j]++;
35 :     found++;
36 :     } else
37 :     handle.logMessage(LOGFAIL, "Error in predator - repeated suitability values for prey", preyvec[i]->getName());
38 :    
39 :     }
40 :     }
41 :     }
42 :    
43 :     found = 0;
44 :     for (i = 0; i < check.Size(); i++) {
45 :     //If we find a prey that we have read the suitability for, but not
46 :     //received a pointer to, we issue a warning and delete it
47 :     if (check[i] == 0) {
48 :     //JMB - need to make sure that we keep track of how many entries from
49 :     //the vector we have deleted so that we ensure that we delete the right ones
50 :     j = i - found;
51 :     handle.logMessage(LOGWARN, "Warning in predator - failed to match prey", this->getPreyName(j));
52 :     preys.Delete(j);
53 :     preference.Delete(j, keeper);
54 :     suitable->deletePrey(j, keeper);
55 :     found++;
56 :     }
57 :     }
58 :    
59 :     if (this->numPreys() == 0)
60 :     handle.logMessage(LOGFAIL, "Error in predator - found no preys for predator", this->getName());
61 :     }
62 :    
63 :     int Predator::doesEat(const char* preyname) const {
64 :     int i;
65 :     for (i = 0; i < suitable->numPreys(); i++)
66 :     if (strcasecmp(suitable->getPreyName(i), preyname) == 0)
67 :     return 1;
68 :     return 0;
69 :     }
70 :    
71 :     void Predator::Print(ofstream& outfile) const {
72 :     int i;
73 :     outfile << "\tName" << sep << this->getName() << "\n\tNames of preys:";
74 :     for (i = 0; i < suitable->numPreys(); i++)
75 :     outfile << sep << suitable->getPreyName(i);
76 :     outfile << endl;
77 :     for (i = 0; i < suitable->numPreys(); i++) {
78 :     if (this->getType() == STOCKPREDATOR)
79 :     outfile << "\tPreference for prey " << suitable->getPreyName(i) << sep << preference[i] << endl;
80 :     else if (this->getType() == EFFORTPREDATOR)
81 :     outfile << "\tCatchability for stock " << suitable->getPreyName(i) << sep << preference[i] << endl;
82 :    
83 :     outfile << "\tSuitability for prey " << suitable->getPreyName(i) << endl;
84 :     suitable->getSuitability(i).Print(outfile);
85 :     }
86 :     }
87 :    
88 :     void Predator::readSuitability(CommentStream& infile,
89 :     const TimeClass* const TimeInfo, Keeper* const keeper) {
90 :    
91 :     int i = 0;
92 :     char preyname[MaxStrLength];
93 :     char text[MaxStrLength];
94 :     strncpy(preyname, "", MaxStrLength);
95 :     strncpy(text, "", MaxStrLength);
96 :     suitable = new Suits();
97 :     SuitFuncPtrVector suitf;
98 :     keeper->addString("suitabilityfor");
99 :    
100 :     infile >> text >> ws;
101 :     if (strcasecmp(text, "suitability") != 0)
102 :     handle.logFileUnexpected(LOGFAIL, "suitability", text);
103 :    
104 :     //JMB - this is the next word to look for after the list of suitability values
105 :     char strcheck[15];
106 :     strncpy(strcheck, "", 15);
107 :     if (this->getType() == STOCKPREDATOR)
108 :     strcpy(strcheck, "preference");
109 :     else if (this->getType() == EFFORTPREDATOR)
110 :     strcpy(strcheck, "catchability");
111 :     else if (this->getType() == QUOTAPREDATOR)
112 :     strcpy(strcheck, "quotafunction");
113 :     else
114 :     strcpy(strcheck, "amount");
115 :    
116 :     infile >> preyname >> ws;
117 :     while ((strcasecmp(preyname, strcheck) != 0) && (!infile.eof())) {
118 :     keeper->addString(preyname);
119 :    
120 :     infile >> text >> ws;
121 :     if (strcasecmp(text, "function") == 0) {
122 :     infile >> text >> ws;
123 :     suitf.readSuitFunction(infile, text, TimeInfo, keeper);
124 :     suitable->addPrey(preyname, suitf[i++]);
125 :    
126 :     } else if (strcasecmp(text, "suitfile") == 0) {
127 :     handle.logFileMessage(LOGFAIL, "\nReading suitability values directly from file is no longer supported\nGadget version 2.0.07 was the last version to allow this functionality");
128 :    
129 :     } else
130 :     handle.logFileMessage(LOGFAIL, "unrecognised predation format", text);
131 :    
132 :     infile >> preyname >> ws;
133 :     keeper->clearLast();
134 :     }
135 :    
136 :     //JMB check the suitability functions for the various predator types
137 :     switch (this->getType()) {
138 :     case STOCKPREDATOR:
139 :     for (i = 0; i < suitf.Size(); i++) {
140 :     if (strcasecmp(suitf[i]->getName(), "gammasuitfunc") == 0)
141 :     handle.logFileMessage(LOGWARN, "Warning in predator - fleet based gamma suitability function used for stock predator", this->getName());
142 :     if (strcasecmp(suitf[i]->getName(), "andersenfleetsuitfunc") == 0)
143 :     handle.logFileMessage(LOGWARN, "Warning in predator - fleet based andersen suitability function used for stock predator", this->getName());
144 :     }
145 :     break;
146 :     case TOTALPREDATOR:
147 :     case LINEARPREDATOR:
148 :     case NUMBERPREDATOR:
149 :     case EFFORTPREDATOR:
150 :     case QUOTAPREDATOR:
151 :     for (i = 0; i < suitf.Size(); i++) {
152 :     if (strcasecmp(suitf[i]->getName(), "andersensuitfunc") == 0)
153 :     handle.logFileMessage(LOGWARN, "Warning in predator - stock based andersen suitability function used for fleet predator", this->getName());
154 :     }
155 :     break;
156 :     default:
157 :     handle.logFileMessage(LOGFAIL, "Error in predator - unrecognised predator type", this->getType());
158 :     break;
159 :     }
160 :    
161 :     //resize the prey preference vector - used for stockpredators and effortpredators
162 :     preference.resize(suitable->numPreys(), keeper);
163 :    
164 :     keeper->clearLast();
165 :     handle.logMessage(LOGMESSAGE, "Read predation data - number of preys", suitable->numPreys());
166 :     }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge