forked from Marxan-source-code/marxan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hill_climbing.cpp
320 lines (263 loc) · 12.1 KB
/
hill_climbing.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
#include <algorithm>
#include <chrono>
#include <ctime>
#include <cfloat>
#include <iostream>
#include <omp.h>
#include <chrono>
// load the required function definition modules
#include "defines.hpp"
#include "utils.hpp"
#include "algorithms.hpp"
#include "computation.hpp"
#include "clumping.hpp"
#include "probability.hpp"
#include "input.hpp"
#include "output.hpp"
#include "score_change.hpp"
#include "hill_climbing.hpp"
namespace marxan {
using namespace algorithms;
using namespace utils;
class ImportTraceSaver
{
public:
ImportTraceSaver()
{
ttfp = nullptr;
Rfp = nullptr;
iRowCounter = 1;
iRowLimit = 0;
}
void init(int irun, int puno, int puvalid, const string& savename, const vector<int>& R)
{
string tempname2, paddedRun = utils::intToPaddedString(irun, 5);
tempname2 = savename + "_itimp_objective" + paddedRun + ".csv";
writename = fnames.outputdir + tempname2;
if ((ttfp = fopen(writename.c_str(), "w")) == NULL)
displayErrorMessage("cannot create threshold trace file %s\n", writename.c_str());
fprintf(ttfp, "improvement,total,pus,cost,connection,penalty,change total\n");
tempname2 = savename + "_itimp_zones" + paddedRun + ".csv";
writename = fnames.outputdir + tempname2;
if ((Rfp = fopen(writename.c_str(), "w")) == NULL)
displayErrorMessage("cannot create threshold trace file %s\n", writename.c_str());
fprintf(Rfp, "configuration");
for (int i = 0; i < puno; i++)
fprintf(Rfp, ",%i", pu[i].id);
fprintf(Rfp, "\n0");
for (int i = 0; i < puno; i++)
fprintf(Rfp, ",%i", R[i]);
fprintf(Rfp, "\n");
iRowCounter = 0;
if (fnames.itimptracerows == 0)
iRowLimit = 0;
else
iRowLimit = floor(puvalid / fnames.itimptracerows);
}
void append(long long i, int puno, const scost& reserve, const scost& change, const vector<int>& R)
{
iRowCounter++;
if (iRowCounter > iRowLimit)
iRowCounter = 1;
if (iRowCounter == 1)
{
fprintf(Rfp, "%lli", i);
fprintf(ttfp, "%lli,%f,%i,%f,%f,%f,%f\n"
, i, reserve.total
, reserve.pus, reserve.cost, reserve.connection, reserve.penalty,
change.total); // i,costthresh,pus,cost,connection,penalty
for (int j = 0; j < puno; j++)
fprintf(Rfp, ",%i", R[j]);
}
}
~ImportTraceSaver()
{
if(ttfp != nullptr)
fclose(ttfp);
if(Rfp != nullptr)
fclose(Rfp);
}
private:
FILE* ttfp, * Rfp;
string writename;
int iRowCounter, iRowLimit;
};
void initialiseHillClimbing(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections, vector<sspecies>& spec,
const vector<spu>& SM, vector<spu_out>& SM_out, double cm, int aggexist,
vector<int>& R, double prop, int clumptype, int irun, stringstream& logBuffer, rng_engine& rngEngine)
{
scost reserve;
initialiseReserve(prop, pu, R, rngEngine);
if (aggexist)
ClearClumps(spno, spec, pu, SM, SM_out);
computeReserveValue(puno, spno, R, pu, connections, SM, SM_out, cm, spec, aggexist, reserve, clumptype, logBuffer);
}
// iteratively improves a planning unit solutions
// a descent algorithm un-reserves planning units that don't have a negative value when removed
void hill_climbing(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections,
vector<sspecies>& spec, const vector<spu>& SM, vector<spu_out>& SM_out, vector<int>& R, double cm,
scost& reserve, double costthresh, double tpf1, double tpf2,
int clumptype, int irun, long long iterations, string savename, stringstream& logBuffer, rng_engine& rngEngine)
{
scost change;
int puvalid = 0, ipu = 0, imode, ichoice;
vector<int> iimparray;
logBuffer << "Hillclimbing start iterations "<< iterations << "\n";
// counting pu's we need to test
for (int i = 0; i < puno; i++)
{
if ((R[i] < 2) && (pu[i].status < 2))
puvalid++;
}
logBuffer << "Hillclimbing puvalid " << puvalid << "\n";
ImportTraceSaver import_trace_saver;
if (fnames.saveitimptrace)
import_trace_saver.init(irun, puno, puvalid, savename, R);
if (puvalid > 0)
{
iimparray.resize(puvalid);
for (int i = 0; i < puno; i++)
{
if ((R[i] < 2) && (pu[i].status < 2))
{
iimparray[ipu] = i;
ipu++;
}
}
logBuffer << "Hill climbing after array init\n";
displayProgress2(" Main Hillclimbing Section.\n");
for(long long itime = 1; itime <= iterations; )
{
// shuffle iimp array
std::shuffle(iimparray.begin(), iimparray.end(), rngEngine);
bool was_change = false;
/***** Doing the improvements ****/
for (int i = 0; i < puvalid && itime <= iterations; i++, itime++)
{
ichoice = iimparray[i];
imode = R[ichoice] == 1 ? -1 : 1;
computeChangeScore(-1, ichoice, spno, puno, pu, connections, spec, SM, SM_out, R, cm, imode, change, reserve,
costthresh, tpf1, tpf2, 1, clumptype);
if (change.total < 0 || (imode == -1 && change.total == 0 ))
{
doChange(ichoice, puno, R, reserve, change, pu, SM, SM_out, spec, connections, imode, clumptype, logBuffer);
was_change = true;
} // I've just made a good change
if (fnames.saveitimptrace)
import_trace_saver.append( itime, puno, reserve, change, R);
} // no untested PUs left
if(!was_change)
{
logBuffer << "Converged after "<< itime << " iterations\n";
break;
}
}
}
displayProgress2("Hill climbing end\n");
} // hill_climbing
// iteratively improves a planning unit solutions
//
void hill_climbing_two_steps(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections,
vector<sspecies>& spec, const vector<spu>& SM, vector<spu_out>& SM_out, vector<int>& R, double cm,
scost& reserve, double costthresh, double tpf1, double tpf2,
int clumptype, int irun, long long iterations, string savename, stringstream& logBuffer, rng_engine& rngEngine)
{
int puvalid = 0, ipu = 0;
vector<int> iimparray;
logBuffer << "Two step hillclimbing start iterations "<< iterations << "\n";
// counting pu's we need to test
for (int i = 0; i < puno; i++)
{
if ((R[i] < 2) && (pu[i].status < 2))
puvalid++;
}
logBuffer << "Two step hillclimbing puvalid " << puvalid << "\n";
ImportTraceSaver import_trace_saver;
if (fnames.saveitimptrace)
import_trace_saver.init(irun, puno, puvalid, savename, R);
if (puvalid > 0)
{
iimparray.resize(puvalid);
for (int i = 0; i < puno; i++)
{
if ((R[i] < 2) && (pu[i].status < 2))
{
iimparray[ipu] = i;
ipu++;
}
}
logBuffer << "Two step hillclimbing after array init\n";
displayProgress2(" Main two step hillclimbing section.\n");
bool skip_add_two_units = true;
for(long long itime = 1; itime <= iterations; )
{
// shuffle iimp array
std::shuffle(iimparray.begin(), iimparray.end(), rngEngine);
// ***** Doing the improvements ****
bool was_change_per_total_loop = false;
for (int i0 = 0; i0 < puvalid && itime <= iterations; i0++)
{
scost change0;
bool was_change = false;
int ichoice0 = iimparray[i0];
//remember old score
int imode0 = R[ichoice0] == 1 ? -1 : 1;
computeChangeScore(-1, ichoice0, spno, puno, pu, connections, spec, SM, SM_out, R, cm, imode0, change0, reserve,
costthresh, tpf1, tpf2, 1, clumptype);
doChange(ichoice0, puno, R, reserve, change0, pu, SM, SM_out, spec, connections, imode0, clumptype, logBuffer);
if (change0.total < 0 || (imode0 == -1 && change0.total == 0))
{
was_change_per_total_loop = true;
continue; //no need to go further
}
for (int i1 = i0+1; i1 < puvalid && itime <= iterations; i1++, itime++)
{
scost change1;
int ichoice1 = iimparray[i1];
int imode1 = R[ichoice1] == 1 ? -1 : 1;
if(skip_add_two_units && (imode1 == imode0 && imode0 == 1))
{
//If there is not enough iterations for complete coverage,
//for cases where result amount of units is small part of total units,
//concentration of search on swap or remove pair
//may improve chance of success
itime--;
continue;
}
computeChangeScore(-1, ichoice1, spno, puno, pu, connections, spec, SM, SM_out, R, cm, imode1, change1, reserve,
costthresh, tpf1, tpf2, 1, clumptype);
if (change0.total + change1.total < 0)
{
doChange(ichoice1, puno, R, reserve, change1, pu, SM, SM_out, spec, connections, imode1, clumptype, logBuffer);
was_change = true;
}
if (fnames.saveitimptrace)
import_trace_saver.append( itime, puno, reserve, change1, R);
if (was_change)
break;//need to recompute change0
} // no untested PUs left
if (was_change)
{
was_change_per_total_loop = true;
}
else
{
//undo change
int imode0_reverted = imode0 == 1 ? -1 : 1;
computeChangeScore(-1, ichoice0, spno, puno, pu, connections, spec, SM, SM_out, R, cm, imode0_reverted, change0, reserve,
costthresh, tpf1, tpf2, 1, clumptype);
doChange(ichoice0, puno, R, reserve, change0, pu, SM, SM_out, spec, connections, imode0_reverted, clumptype, logBuffer);
}
}
if (!was_change_per_total_loop)
{
if(skip_add_two_units)
skip_add_two_units = false;
else
break;
}
}
}
displayProgress2("Two step hillclimbing end\n");
} // hill_climbing_two_step
} // namespace marxan