forked from Marxan-source-code/marxan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heuristics.cpp
448 lines (382 loc) · 17.8 KB
/
heuristics.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "utils.hpp"
#include "computation.hpp"
#include "heuristics.hpp"
#include "output.hpp"
#include "clumping.hpp"
#include "score_change.hpp"
#include "defines.hpp"
// functions that read from input files
namespace marxan {
double GreedyPen(int ipu, int puno, int spno, const vector<sspecies>& spec, const vector<int>& R, const vector<spustuff>& pu,
const vector<spu>& SM, const vector<spu_out>& SM_out, int clumptype)
{
double famount = 0.0, fold, newamount;
for (int i = 0; i < spno; i++)
{
fold = (spec[i].target - spec[i].amount);
if (fold > 0)
{
if (spec[i].target2)
newamount = NewPenalty4(ipu, i, puno, spec, pu, SM, SM_out, R, connections, 1, clumptype);
else
newamount = computeSpeciesPlanningUnitPenalty(ipu, i, spec, pu, SM, 1);
famount += (newamount - fold) * spec[i].spf;
} // Add new penalty if species isn't already in the system
}
return (famount); // Negative means decrease in amount missing
} // Greedy Species Penalty
double GreedyScore(int ipu, int puno, int spno, const vector<sspecies>& spec, const vector<spu>& SM, const vector<spu_out>& SM_out, const vector<sconnections>& connections,
const vector<int>& R, const vector<spustuff>& pu, double cm, int clumptype)
{
double currpen, currcost, currscore;
currpen = GreedyPen(ipu, puno, spno, spec, R, pu, SM, SM_out, clumptype);
currcost = pu[ipu].cost + ConnectionCost2(connections[ipu], R, 1, 1, cm, asymmetricconnectivity, fOptimiseConnectivityIn);
if (currcost <= 0)
{
currscore = -1.0 / delta;
} // otherwise this 'free pu' will have +score
else
{
currscore = currpen / currcost;
// multiply by rand (1.000,1.001)
}
return (currscore);
} // Score for a planning unit based upon greedy algorithm
/*********** Rarity Settup. Sets up rare score for each species ******/
/**** score is total abundance / smallest species abundance *********/
void SetRareness(int puno, int spno, vector<double>& Rare, const vector<int>& R, const vector<spustuff>& pu, const vector<spu>& SM, stringstream& logBuffer)
{
double smallest = 0;
vector<double> fcount(spno, 0);
int i, ism, isp, ipu;
#ifdef DEBUG_HEURISTICS
logBuffer << "SetRareness start\n";
#endif
for (ipu = 0; ipu < puno; ipu++)
if (pu[ipu].richness)
for (i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
if (R[ipu] < 2)
fcount[isp] += SM[ism].amount;
}
for (isp = 0; isp < spno; isp++)
{
if (smallest == 0 || (fcount[isp] < smallest && fcount[isp] > 0))
smallest = fcount[isp];
Rare[isp] = fcount[isp];
#ifdef DEBUG_HEURISTICS
logBuffer << "SetRareness isp " << isp << " Rare " << Rare[isp] << endl;
#endif
}
if (smallest == 0)
displayErrorMessage("Serious Error in calculating Rarenesses. No species detected.\n");
for (isp = 0; isp < spno; isp++)
Rare[isp] /= smallest;
#ifdef DEBUG_HEURISTICS
logBuffer << "SetRareness end\n";
#endif
} // SetRareness
// RareScore The score for a particular conservation value on a particular PU
double RareScore(int isp, int ipu, int puno, const vector<sspecies>& spec, const vector<spu>& SM, const vector<spu_out>& SM_out, const vector<int>& R,
const vector<sconnections>& connections, const vector<spustuff>& pu, double cm, int clumptype)
{
double currpen, currcost, currscore;
double fold, newamount;
fold = (spec[isp].target - spec[isp].amount);
if (fold > 0)
{
if (spec[isp].target2)
newamount = NewPenalty4(ipu, isp, puno, spec, pu, SM, SM_out, R, connections, 1, clumptype);
else
newamount = computeSpeciesPlanningUnitPenalty(ipu, isp, spec, pu, SM, 1);
currpen = newamount - fold;
} // Add new penalty if species isn't already in the system
currcost = pu[ipu].cost + ConnectionCost2(connections[ipu], R, 1, 1, cm, asymmetricconnectivity, fOptimiseConnectivityIn);
if (currcost <= 0)
{
currscore = -1.0 / delta;
} // otherwise this 'free pu' will have +score
else
{
currscore = currpen / currcost;
// multiply by rand (1.000,1.001)
}
return (currscore);
} // RareScore
// Max Rare Score Heuristic. PU scores based on rarest beast on PU
double MaxRareScore(int ipu, int puno, const vector<sspecies>& spec, const vector<spu>& SM, const vector<spu_out>& SM_out,
const vector<int>& R, const vector<sconnections>& connections, const vector<spustuff>& pu, double cm, const vector<double>& Rare, int clumptype)
{
int ism, isp, rareno = -1;
double rarest = 0.0, rarescore;
if (pu[ipu].richness)
for (int i = 0; i < pu[ipu].richness; i++)
{
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
if (SM[ism].amount && (spec[isp].target > spec[isp].amount || (spec[isp].sepdistance && spec[isp].separation < 3)))
if (1.0 / Rare[isp] < rarest || rareno < 0)
{
rareno = isp;
rarest = Rare[isp];
} // Determine which is the rarest species
}
if (rareno > -1)
rarescore = RareScore(rareno, ipu, puno, spec, SM, SM_out, R, connections, pu, cm, clumptype) / rarest;
else
rarescore = 1.0 / delta;
return (rarescore);
} // Max Rare Score
// Best Rarity Score. Determines each species rare score
double BestRareScore(int ipu, int puno, const vector<sspecies>& spec, const vector<spu>& SM, const vector<spu_out>& SM_out,
const vector<int>& R, const vector<sconnections>& connections, const vector<spustuff>& pu, double cm, const vector<double>& Rare, int clumptype)
{
int i, ism, isp, rareno = -1;
double rarest = 0, rarescore;
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 && (spec[isp].target > spec[isp].amount || (spec[isp].sepdistance && spec[isp].separation < 3)))
{
rarescore = RareScore(isp, ipu, puno, spec, SM, SM_out, R, connections, pu, cm, clumptype) / Rare[isp];
if (rarescore > rarest || rareno < 0)
{
rarest = rarescore;
rareno = isp;
}
}
}
return (rarescore);
} // Best Rare Score
// Average Rare Score. Rare Score for each scoring species/number scoring species
double AveRareScore(int ipu, int puno, const vector<sspecies>& spec, const vector<spu>& SM, const vector<spu_out>& SM_out,
const vector<int>& R, const vector<sconnections>& connections, const vector<spustuff>& pu, double cm, const vector<double>& Rare, int clumptype)
{
int i, ism, isp, rareno = 0;
double rarescore = 0;
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 &&
(spec[isp].target > spec[isp].amount ||
(spec[isp].sepdistance && spec[isp].separation < 3)))
{
rarescore += RareScore(isp, ipu, puno, spec, SM, SM_out, R, connections, pu, cm, clumptype) / Rare[isp];
rareno++;
}
}
return (rarescore / rareno);
} // Ave Rare Score
double SumRareScore(int ipu, int puno, const vector<sspecies>& spec, const vector<spu>& SM, const vector<spu_out>& SM_out,
const vector<int>& R, const vector<sconnections>& connections, const vector<spustuff>& pu, double cm,
const vector<double>& Rare, int clumptype, stringstream& logBuffer)
{
int i, ism, isp;
double rarescore = 0;
#ifdef DEBUG_HEURISTICS
logBuffer << "SumRareScore start\n";
#endif
if (pu[ipu].richness)
for (i = 0; i < pu[ipu].richness; i++)
{
#ifdef DEBUG_HEURISTICS
logBuffer << "SumRareScore feature " << i << "of " << pu[ipu].richness << "\n";
#endif
ism = pu[ipu].offset + i;
isp = SM[ism].spindex;
#ifdef DEBUG_HEURISTICS
logBuffer << "SumRareScore SMamount " << SM[ism].amount << " target " << spec[isp].target << " specamount " << spec[isp].amount << " rare " << Rare[isp] << "\n";
#endif
if (SM[ism].amount &&
(spec[isp].target > spec[isp].amount ||
(spec[isp].sepdistance && spec[isp].separation < 3)))
rarescore += RareScore(isp, ipu, puno, spec, SM, SM_out, R, connections, pu, cm, clumptype) / Rare[isp];
}
#ifdef DEBUG_HEURISTICS
logBuffer << "SumRareScore end\n";
#endif
return (rarescore);
} // Sum Rare Score
// Set Abundances
void SetAbundance(int puno, vector<double>& Rare, const vector<spustuff>& pu, const vector<spu>& SM)
{
int i, j, ism, isp;
for (i = 0; i < puno; i++)
if (pu[i].richness)
for (j = 0; j < pu[i].richness; j++)
{
ism = pu[i].offset + j;
isp = SM[ism].spindex;
Rare[isp] += SM[ism].amount;
}
} // Set Abundance
// Irreplaceability For site for species
double Irreplaceability(int ipu, int isp, const vector<double>& Rare, const vector<spustuff>& pu, const vector<spu>& SM, const vector<sspecies>& spec)
{
double buffer, effamount;
buffer = Rare[isp] < spec[isp].target ? 0 : Rare[isp] - spec[isp].target;
if (spec[isp].amount > spec[isp].target)
return (0);
effamount = returnAmountSpecAtPu(pu[ipu], SM, isp).second;
return (buffer < effamount ? 1 : effamount / buffer);
}
// Product Irreplaceability for a single site
double ProdIrr(int ipu, const vector<double>& Rare, const vector<spustuff>& pu, const vector<spu>& SM, const vector<sspecies>& spec)
{
int i, ism, isp;
double product = 1;
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 && (spec[isp].target - spec[isp].amount) > 0)
product *= (1 - Irreplaceability(ipu, isp, Rare, pu, SM, spec));
}
return (1 - product);
} // Product Irreplaceability
double SumIrr(int ipu, const vector<double>& Rare, const vector<spustuff>& pu, const vector<spu>& SM, const vector<sspecies>& spec)
{
int i, ism, isp;
double sum = 0;
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 && (spec[isp].target - spec[isp].amount) > 0)
sum += (Irreplaceability(ipu, isp, Rare, pu, SM, spec));
}
return (sum);
} // Sum Irreplaceability
// Main Heuristic Engine
void Heuristics(int spno, int puno, const vector<spustuff>& pu, const vector<sconnections>& connections,
vector<int>& R, double cm, vector<sspecies>& spec, const vector<spu>& SM, vector<spu_out>& SM_out, scost& reserve,
double costthresh, double tpf1, double tpf2, int imode, int clumptype, stringstream& logBuffer, rng_engine& rngEngine)
// imode = 1: 2: 3: 4:
// imode = 5: 6: Prod Irreplaceability, 7: Sum Irreplaceability
{
int i, bestpu;
double bestscore, currscore;
scost change;
vector<double> Rare;
uniform_real_distribution<double> float_range(0.0, 1.0);
#ifdef DEBUG_HEURISTICS
logBuffer << "Heuristics start mode " << imode << "\n";
#endif
// Irreplacability
if (imode >= 6 && imode <= 7)
{
Rare.resize(spno);
SetAbundance(puno, Rare, pu, SM);
#ifdef DEBUG_HEURISTICS
logBuffer << "Heuristics 6 or 7\n";
#endif
}
if (imode >= 2 && imode <= 5) // Rareness Setups
{
Rare.resize(spno);
SetRareness(puno, spno, Rare, R, pu, SM, logBuffer);
#ifdef DEBUG_HEURISTICS
logBuffer << "Heuristics 2 to 5 after SetRareness\n";
#endif
}
do
{
bestpu = 0;
bestscore = 0;
for (i = 0; i < puno; i++)
{
if (!R[i]) // Only look for new PUS
{
// Set the score for the given Planning Unit
currscore = 1; // null if no other mode set
if (imode == 0)
currscore = GreedyScore(i, puno, spno, spec, SM, SM_out, connections, R, pu, cm, clumptype);
if (imode == 1)
{
computeChangeScore(-1, i, spno, puno, pu, connections, spec, SM, SM_out, R, cm, 1, change, reserve,
costthresh, tpf1, tpf2, 1, clumptype);
currscore = change.total;
}
if (imode == 2)
{
currscore = MaxRareScore(i, puno, spec, SM, SM_out, R, connections, pu, cm, Rare, clumptype);
}
if (imode == 3)
{
currscore = BestRareScore(i, puno, spec, SM, SM_out, R, connections, pu, cm, Rare, clumptype);
}
if (imode == 4)
{
currscore = AveRareScore(i, puno, spec, SM, SM_out, R, connections, pu, cm, Rare, clumptype);
}
if (imode == 5)
{
#ifdef DEBUG_HEURISTICS
logBuffer << "Heuristics pu " << i << " of " << puno << "\n";
#endif
currscore = SumRareScore(i, puno, spec, SM, SM_out, R, connections, pu, cm, Rare, clumptype, logBuffer);
#ifdef DEBUG_HEURISTICS
logBuffer << "Heuristics after SumRareScore\n";
#endif
}
if (imode == 6)
{
currscore = -ProdIrr(i, Rare, pu, SM, spec);
}
if (imode == 7)
{
currscore = -SumIrr(i, Rare, pu, SM, spec);
}
currscore *= float_range(rngEngine) * 0.001 + 1.0;
if (!costthresh || pu[i].cost + reserve.cost <= costthresh)
if (currscore < bestscore)
{
bestpu = i;
bestscore = currscore;
} // is this better (ie negative) than bestscore?
} // I've looked through each pu to find best
if (bestscore)
{
computeChangeScore(-1, bestpu, spno, puno, pu, connections, spec, SM, SM_out, R, cm, 1, change, reserve,
costthresh, tpf1, tpf2, 1, clumptype);
doChange(bestpu, puno, R, reserve, change, pu, SM, SM_out, spec, connections, 1, clumptype, logBuffer);
// Different Heuristics might have different penalty effects
// Old fashioned penalty and missing counting
reserve.missing = 0;
for (i = 0; i < spno; i++)
{
if (spec[i].amount < spec[i].target)
{
reserve.missing++;
}
else if (spec[i].sepdistance && spec[i].separation < 3)
reserve.missing++;
// Species missing
} // checking to see who I am missing
} // Add Pu as long as I've found one
if (bestscore)
{
displayProgress2("P.U. %i PUs %i score %.6f Cost %.1f Connection %.1f Missing %i",
bestpu, reserve.pus, bestscore, reserve.cost, reserve.connection, reserve.missing);
if (fProb1D == 1)
displayProgress2(" Probability1D %.1f\n", reserve.probability1D);
if (fProb2D == 1)
displayProgress2(" Probability2D %.1f\n", reserve.probability2D);
displayProgress2(" Penalty %.1f\n", reserve.penalty);
}
}
} while (bestscore); // Repeat until all good PUs have been added
reserve.total = reserve.cost + reserve.connection + reserve.penalty + reserve.probability1D + reserve.probability2D;
#ifdef DEBUG_HEURISTICS
logBuffer << "Heuristics end\n";
#endif
} // Heuristics
} // marxan