forked from Marxan-source-code/marxan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computation.cpp
348 lines (308 loc) · 12.2 KB
/
computation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// All functions here should be unit and should not have dependencies on the core marxan files.
#include <algorithm>
#include "computation.hpp"
#include "marxan.hpp"
#include "utils.hpp"
namespace marxan {
// Sums all connectivity edges for a pu.
double connectionCost1(const sconnections& connections, double cm, int asymmetricconnectivity)
{
double fcost;
fcost = connections.fixedcost;
for (const auto& p : connections.first)
{
if (asymmetricconnectivity)
{
if (p.connectionorigon)
fcost += p.cost;
}
else
{
fcost += p.cost;
}
}
return (fcost * cm);
}
/*** Counts the number of species missing from the reserve ****/
// compute the number of species whose representation fraction is less that the MISSLEVEL parameter
int computeRepresentationMISSLEVEL(int spno, const vector<sspecies>& spec, double misslevel, double& shortfall, double& rMinimumProportionMet)
{
int i, isp = 0;
double rProportionMet;
shortfall = 0;
rMinimumProportionMet = 1;
for (i = 0; i < spno; i++)
{
checkAndUpdateTargetProportion(spec[i].target, spec[i].amount, shortfall, rMinimumProportionMet); // check regular target
checkAndUpdateTargetProportion(spec[i].targetocc, spec[i].occurrence, shortfall, rMinimumProportionMet); // check occurence target
if (spec[i].target)
{
if (spec[i].amount / spec[i].target < misslevel)
{
isp++;
continue;
}
}
if (spec[i].targetocc)
{
if ((double)spec[i].occurrence / (double)spec[i].targetocc < misslevel)
{
isp++;
continue;
}
}
if (spec[i].sepdistance && spec[i].separation < 3)
{
isp++; /* count species if not met separation and not already counted */
}
}
return(isp);
}
// compute connectivity total, in, edge, out for summary report
void computeConnectivityIndices(double& rConnectivityTotal, double& rConnectivityIn,
double& rConnectivityEdge, double& rConnectivityOut,
int puno, const vector<int>& R, const vector<sconnections>& connections)
// We record 4 categories for connectivity;
// - total, all connections in the region
// - in, all connections entirely within the reserve network (ie. both pu's in)
// - edge, all connections on the edge of the reserve network (ie. one pu in & one pu out)
// - out, all connections not captured in the reserve network (ie. both pu's out)
//
// Of these, we previously only recorded "edge", referring to it as boundary length.
// The proportion of connections captured is given by;
// in / total
//
// total = in + edge + out
{
int i;
double rFixed;
for (i = 0; i < puno; i++)
{
rFixed = connections[i].fixedcost;
rConnectivityTotal += rFixed;
if (R[i] == 1 || R[i] == 2)
{ // add to 'in' or 'edge'
rConnectivityEdge += rFixed;
for (sneighbour p : connections[i].first)
{
if (p.nbr > i)
{
if (R[p.nbr] == 1 || R[p.nbr] == 2) // add to 'in'
rConnectivityIn += p.cost;
else // add to 'edge'
rConnectivityEdge += p.cost;
// add to 'total'
rConnectivityTotal += p.cost;
}
}
}
else
{ // add to 'out' or 'edge'
rConnectivityOut += rFixed;
for (sneighbour p : connections[i].first)
{
if (p.nbr > i)
{
if (R[p.nbr] == 1 || R[p.nbr] == 2) // add to 'edge'
rConnectivityEdge += p.cost;
else // add to 'out'
rConnectivityOut += p.cost;
// add to 'total'
rConnectivityTotal += p.cost;
}
}
}
}
}
// Merging repeated area computation code. Recomputes TO, TA etc. from scratch.
void computeOccurrencesAndAreas(int& puno, const vector<spustuff>& pu, const vector<spu>& SM,
vector<int>& TotalOccurrences, vector<int>& TO_2, vector<int>& TO_3,
vector<double>& TotalAreas, vector<double>& TA_2, vector<double>& TA_3) {
int ism, isp;
for (int ipu = 0; ipu < puno; ipu++)
{
if (pu[ipu].richness)
{
for (int i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
TotalOccurrences[isp]++;
TotalAreas[isp] += SM[ism].amount;
if (pu[ipu].status == 2)
{
TO_2[isp]++;
TA_2[isp] += SM[ism].amount;
}
if (pu[ipu].status == 3)
{
TO_3[isp]++;
TA_3[isp] += SM[ism].amount;
}
}
}
}
}
// Used in ReturnProbabilityAmounts1D and ReturnProbabilityAmounts2D
void computeExpectedAndVariance(int ipu, const vector<spustuff>& pu, const vector<spu>& SM, vector<double>& variance, vector<double>& expected) {
int i, ism, isp;
if (pu[ipu].richness)
{
for (i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
if (SM[ism].amount)
{
expected[isp] += SM[ism].amount * (1 - pu[ipu].prob);
variance[isp] += SM[ism].amount * SM[ism].amount * pu[ipu].prob * (1 - pu[ipu].prob);
}
}
}
}
// Returns both index and species amount for a planning unit
pair<int, double> returnAmountSpecAtPu(const spustuff& pu, const vector<spu>& SM, int iSpecIndex)
{
if (pu.richness > 0)
{
auto start_it = SM.begin() + pu.offset;
auto end_it = start_it + pu.richness;
auto spindex_cmp = [](const spu& lhs, int rhs) -> bool { return lhs.spindex < rhs; };
auto elem_it = std::lower_bound(start_it, end_it, iSpecIndex, spindex_cmp);
if (elem_it != end_it && elem_it->spindex == iSpecIndex)
{
size_t index = elem_it - SM.begin();
return pair<int, double>(index, elem_it->amount);
}
}
return pair<int, double>(-1, 0);
}
// Sums the total spec amount across all pu
double computeTotalSpecAmtAllPu(const vector<spustuff>& PU, const vector<spu>& SM, int speciesInd)
{
double totalAmount = 0.0;
for (int ipu = 0; ipu < PU.size(); ipu++)
totalAmount += returnAmountSpecAtPu(PU[ipu], SM, speciesInd).second;
return totalAmount;
}
// compute proportional target for species when prop target is specified
// use the prop value from the conservation feature file to set a proportion target for species
void computeSpecProp(int spno, vector<sspecies>& spec, int puno, const vector<spustuff>& pu, const vector<spu>& SM)
{
// compute and set target for species with a prop value
for (int isp = 0; isp < spno; isp++)
{
if (spec[isp].prop > 0)
{
spec[isp].target = computeTotalSpecAmtAllPu(pu, SM, isp) * spec[isp].prop;
}
}
}
// Computes penalty for a species given a reserve, based on only fixed pu
void computeFixedPenaltyForSpec(const vector<int>& R, const vector<spustuff>& pu, const vector<spu>& SM, const vector<sconnections>& connections, int spIndex,
double& ftarget, int& itargetocc, double& penalty, double cm, int asymmetricconnectivity)
{
ftarget = 0, itargetocc = 0, penalty = 0;
for (int j = 0; j < pu.size(); j++)
{
if (R[j] == 2)
{
ftarget += returnAmountSpecAtPu(pu[j], SM, spIndex).second;
itargetocc++;
penalty += computePlanningUnitValue(pu[j], connections[j], cm, asymmetricconnectivity);
}
}
}
// ********* Connection Cost Type 2 **************
// ** Requires R[]. imode2 = 0 there is no negative cost for removing connection, we are calling from ReserveCost
// or 1 there is a negative cost for removing connection, we are calling from Annealing
// imode = -1 we are removing the planning unit from a reserve, calling from Annealing
// or 1 we are adding the planning unit to a reserve, or it is already in reserve
// It seems that the behaviour of this function is undefined/unsupported if imode2=0 and imode=-1
double ConnectionCost2(const sconnections& connection, const vector<int>& R, int imode, int imode2, double cm,
int asymmetricconnectivity, int fOptimiseConnectivityIn)
{
double fcost = connection.fixedcost * imode;
int R_pu1;
if (asymmetricconnectivity)
{
for (const sneighbour& p : connection.first)
{
if (imode2) // calling from Annealing
{
// determines if ipu is currently switched on or not
// if imode==1 then we assume currently switched off, and will switch on.
if (imode == 1)
R_pu1 = 0;
else
R_pu1 = 1;
if (p.connectionorigon)
{
if (R[p.nbr] == 0)
{
if (R_pu1 == 1)
{
fcost -= p.cost;
}
else
{
fcost += p.cost;
}
}
}
else
{
if (R[p.nbr] == 1 || R[p.nbr] == 2)
{
if (R_pu1 == 1)
{
fcost += p.cost;
}
else
{
fcost -= p.cost;
}
}
}
}
else // calling from ReserveCost
{
if (R[p.nbr] == 0)
if (p.connectionorigon)
{
fcost += p.cost;
}
}
}
}
else
{
for (const sneighbour& p : connection.first) // treatment for symmetric connectivity
{
if (fOptimiseConnectivityIn == 1)
{ // optimise for "Connectivity In"
if (R[p.nbr] == 1 || R[p.nbr] == 2)
{
fcost += imode * p.cost;
}
else
{
fcost += imode * imode2 * p.cost * -1;
}
}
else
{ // optimise for "Connectivity Edge"
if (R[p.nbr] == 1 || R[p.nbr] == 2)
{
fcost += imode * imode2 * p.cost * -1;
}
else
{
fcost += imode * p.cost;
}
}
}
}
return (fcost * cm);
}
} // namespace marxan