Skip to content

Commit 6293d74

Browse files
Added hill climbing as option to run.
1 parent 8e722b9 commit 6293d74

6 files changed

+84
-101
lines changed

hill_climbing.cpp

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// C++ code for Marxan
2-
// version 2.3 introduced multiple connectivity files and their associated weighting file
3-
// version 2.4.3 introduced 1D and 2D probability
4-
// version 3.0.0 is refactoring of code in 2019
5-
61

72
#include <algorithm>
83
#include <chrono>
@@ -105,24 +100,41 @@ namespace marxan {
105100
int iRowCounter, iRowLimit;
106101
};
107102

103+
void initialiseHillClimbing(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections, vector<sspecies>& spec,
104+
const vector<spu>& SM, vector<spu_out>& SM_out, double cm, int aggexist,
105+
vector<int>& R, double prop, int clumptype, int irun, stringstream& logBuffer, rng_engine& rngEngine)
106+
{
107+
scost reserve = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
108+
109+
initialiseReserve(prop, pu, R, rngEngine);
110+
111+
if (aggexist)
112+
ClearClumps(spno, spec, pu, SM, SM_out);
113+
114+
computeReserveValue(puno, spno, R, pu, connections, SM, SM_out, cm, spec, aggexist, reserve, clumptype, logBuffer);
115+
116+
}
117+
118+
108119
// iteratively improves a planning unit solutions
109120
// a descent algorithm un-reserves planning units that don't have a negative value when removed
110121
void hill_climbing(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections,
111122
vector<sspecies>& spec, const vector<spu>& SM, vector<spu_out>& SM_out, vector<int>& R, double cm,
112-
scost& reserve, scost& change, double costthresh, double tpf1, double tpf2,
123+
scost& reserve, double costthresh, double tpf1, double tpf2,
113124
int clumptype, int irun, int iterations, string savename, stringstream& logBuffer, rng_engine& rngEngine)
114125
{
126+
scost change = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
115127
int puvalid = 0, ipu = 0, imode, ichoice;
116128
vector<int> iimparray;
117129

118-
logBuffer << "iterativeImprovement start\n";
130+
logBuffer << "Hillclimbing start iterations "<< iterations << "\n";
119131
// counting pu's we need to test
120132
for (int i = 0; i < puno; i++)
121133
{
122134
if ((R[i] < 2) && (pu[i].status < 2))
123135
puvalid++;
124136
}
125-
logBuffer << "iterativeImprovement puvalid " << puvalid << "\n";
137+
logBuffer << "Hillclimbing puvalid " << puvalid << "\n";
126138

127139
ImportTraceSaver import_trace_saver;
128140
if (fnames.saveitimptrace)
@@ -141,9 +153,9 @@ namespace marxan {
141153
}
142154
}
143155

144-
logBuffer << "iterativeImprovement after array init\n";
156+
logBuffer << "Hill climbing after array init\n";
157+
displayProgress2(" Main Hillclimbing Section.\n");
145158

146-
147159
for(int itime = 1; itime <= iterations; )
148160
{
149161
// shuffle iimp array
@@ -155,18 +167,14 @@ namespace marxan {
155167
{
156168
ichoice = iimparray[i];
157169

158-
if ((R[ichoice] < 2) && (pu[ichoice].status < 2))
159-
{
160-
imode = R[ichoice] == 1 ? -1 : 1;
161-
computeChangeScore(-1, ichoice, spno, puno, pu, connections, spec, SM, SM_out, R, cm, imode, change, reserve,
170+
imode = R[ichoice] == 1 ? -1 : 1;
171+
computeChangeScore(-1, ichoice, spno, puno, pu, connections, spec, SM, SM_out, R, cm, imode, change, reserve,
162172
costthresh, tpf1, tpf2, 1, clumptype);
163-
if (change.total < 0)
164-
{
165-
displayProgress2("It Imp has changed %i with change value %lf \n", ichoice, change.total);
173+
if (change.total < 0 || (imode == -1 && change.total == 0 ))
174+
{
166175
doChange(ichoice, puno, R, reserve, change, pu, SM, SM_out, spec, connections, imode, clumptype, logBuffer);
167176
was_change = true;
168-
} // I've just made a good change
169-
}
177+
} // I've just made a good change
170178

171179
if (fnames.saveitimptrace)
172180
import_trace_saver.append( itime, puno, reserve, change, R);
@@ -180,7 +188,7 @@ namespace marxan {
180188

181189
}
182190
}
183-
appendTraceFile("iterativeImprovement end\n");
191+
displayProgress2("Hill climbing end\n");
184192
} // hill_climbing
185193

186194
// iteratively improves a planning unit solutions
@@ -193,14 +201,14 @@ namespace marxan {
193201
int puvalid = 0, ipu = 0;
194202
vector<int> iimparray;
195203

196-
logBuffer << "iterativeImprovement start\n";
204+
logBuffer << "Two step hillclimbing start iterations "<< iterations << "\n";
197205
// counting pu's we need to test
198206
for (int i = 0; i < puno; i++)
199207
{
200208
if ((R[i] < 2) && (pu[i].status < 2))
201209
puvalid++;
202210
}
203-
logBuffer << "iterativeImprovement puvalid " << puvalid << "\n";
211+
logBuffer << "Two step hillclimbing puvalid " << puvalid << "\n";
204212

205213
ImportTraceSaver import_trace_saver;
206214
if (fnames.saveitimptrace)
@@ -219,7 +227,8 @@ namespace marxan {
219227
}
220228
}
221229

222-
logBuffer << "iterativeImprovement after array init\n";
230+
logBuffer << "Two step hillclimbing after array init\n";
231+
displayProgress2(" Main two step hillclimbing section.\n");
223232

224233

225234
for(int itime = 1; itime <= iterations; )
@@ -252,10 +261,9 @@ namespace marxan {
252261

253262
if (change0.total + change1.total < 0)
254263
{
255-
displayProgress2("It Imp has changed %i with change value %lf \n", ichoice1, change.total);
256264
doChange(ichoice1, puno, R, reserve, change1, pu, SM, SM_out, spec, connections, imode1, clumptype, logBuffer);
257265
was_change = true;
258-
} // I've just made a good change
266+
}
259267

260268
if (fnames.saveitimptrace)
261269
import_trace_saver.append( itime, puno, reserve, change, R);
@@ -275,7 +283,7 @@ namespace marxan {
275283
}
276284
}
277285

278-
appendTraceFile("iterativeImprovement end\n");
286+
displayProgress2("Two step hillclimbing end\n");
279287
} // hill_climbing_two_step
280288
} // namespace marxan
281289

hill_climbing.hpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919

2020
namespace marxan {
2121

22+
void initialiseHillClimbing(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections,
23+
vector<sspecies>& spec, const vector<spu>& SM, vector<spu_out>& SM_out, double cm, int aggexist,
24+
vector<int>& R, double prop, int clumptype, int irun, stringstream& logBuffer, rng_engine& rngEngine);
25+
2226
void hill_climbing(int puno, int spno, const vector<spustuff>& pu, const vector<sconnections>& connections,
2327
vector<sspecies>& spec, const vector<spu>& SM, vector<spu_out>& SM_out, vector<int>& R, double cm,
24-
scost& reserve, scost& change, double costthresh, double tpf1, double tpf2,
25-
int clumptype, int irun, int iterations, string savename, stringstream& logBuffer, rng_engine& rngEngine);
28+
scost& reserve, double costthresh, double tpf1, double tpf2,
29+
int clumptype, int irun, int iterations, string savename, stringstream& logBuffer, rng_engine& rngEngine);
2630

2731
} // namespace marxan

marxan.cpp

+27-25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "iterative_improvement.hpp"
2727
#include "quantum_annealing.hpp"
2828
#include "thermal_annealing.hpp"
29+
#include "hill_climbing.hpp"
2930

3031
#include "marxan.hpp"
3132

@@ -176,11 +177,12 @@ namespace marxan {
176177
if (aggexist)
177178
ClearClumps(spno, spec, pu, SMGlobal, SM_out);
178179

180+
computeReserveValue(puno, spno, R, pu, connections, SMGlobal, SM_out, cm, spec, aggexist, reserve, clumptype, appendLogBuffer);
181+
179182
appendLogBuffer << "after computeReserveValue run " << run_id << endl;
180183

181184
if (verbosity > 1)
182185
{
183-
computeReserveValue(puno, spno, R, pu, connections, SMGlobal, SM_out, cm, spec, aggexist, reserve, clumptype, appendLogBuffer);
184186
runConsoleOutput << "Run " << run_id << " Init: " << displayValueForPUs(puno, spno, R, reserve, spec, misslevel).str();
185187
}
186188
if (verbosity > 5)
@@ -196,6 +198,7 @@ namespace marxan {
196198
repeats, run_id, savename, misslevel,
197199
aggexist, costthresh, tpf1, tpf2, clumptype, anneal, appendLogBuffer, rngEngine);
198200

201+
199202
if (verbosity > 1)
200203
{
201204
computeReserveValue(puno, spno, R, pu, connections, SMGlobal, SM_out, cm, spec, aggexist, reserve, clumptype, appendLogBuffer);
@@ -205,22 +208,21 @@ namespace marxan {
205208
appendLogBuffer << "after thermalAnnealing run " << run_id << endl;
206209
}
207210

208-
if (runoptions.QuantumAnnealingOn)
211+
if (runoptions.HillClimbingOn)
209212
{
210-
appendLogBuffer << "before quantumAnnealing run " << run_id << endl;
213+
appendLogBuffer << "before hill climbing run " << run_id << endl;
211214

212-
quantumAnnealing(spno, puno, connections, R, cm, spec, pu, SMGlobal, SM_out, change, reserve,
213-
repeats, run_id, savename, misslevel,
214-
aggexist, costthresh, tpf1, tpf2, clumptype, anneal, rngEngine);
215+
hill_climbing( puno, spno, pu, connections, spec, SMGlobal, SM_out, R, cm, reserve, costthresh, tpf1, tpf2,
216+
clumptype, run_id, anneal.iterations, savename, appendLogBuffer, rngEngine);
215217

216218
if (verbosity > 1)
217219
{
218220
computeReserveValue(puno, spno, R, pu, connections, SMGlobal, SM_out, cm, spec, aggexist, reserve, clumptype, appendLogBuffer);
219-
runConsoleOutput << "Run " << run_id << " QuantumAnnealing: " << displayValueForPUs(puno, spno, R, reserve, spec, misslevel).str();
221+
runConsoleOutput << "Run " << run_id << " Hill climbing: " << displayValueForPUs(puno, spno, R, reserve, spec, misslevel).str();
220222

221223
}
222224

223-
appendLogBuffer << "after quantumAnnealing run " << run_id << endl;
225+
appendLogBuffer << "after hill climbing run " << run_id << endl;
224226
}
225227

226228
if (runoptions.HeuristicOn)
@@ -700,7 +702,7 @@ namespace marxan {
700702
}
701703

702704
// If we are in a runmode with only CalcPenalties, we stop/exit here gracefully because we are finished.
703-
if (runoptions.HeuristicOn == 0 && runoptions.ThermalAnnealingOn == 0 && runoptions.QuantumAnnealingOn == 0 && runoptions.ItImpOn == 0)
705+
if (runoptions.HeuristicOn == 0 && runoptions.ThermalAnnealingOn == 0 && runoptions.HillClimbingOn == 0 && runoptions.ItImpOn == 0)
704706
{
705707
appendTraceFile("end final file output\n");
706708
appendTraceFile("\nMarxan end execution\n");
@@ -805,112 +807,112 @@ namespace marxan {
805807
case 0:
806808
runoptions.CalcPenaltiesOn = 1;
807809
runoptions.ThermalAnnealingOn = 1;
808-
runoptions.QuantumAnnealingOn = 0;
810+
runoptions.HillClimbingOn = 0;
809811
runoptions.HeuristicOn = 1;
810812
runoptions.ItImpOn = 0;
811813
break;
812814
case 1:
813815
runoptions.CalcPenaltiesOn = 1;
814816
runoptions.ThermalAnnealingOn = 1;
815-
runoptions.QuantumAnnealingOn = 0;
817+
runoptions.HillClimbingOn = 0;
816818
runoptions.HeuristicOn = 0;
817819
runoptions.ItImpOn = 1;
818820
break;
819821
case 2:
820822
runoptions.CalcPenaltiesOn = 1;
821823
runoptions.ThermalAnnealingOn = 1;
822-
runoptions.QuantumAnnealingOn = 0;
824+
runoptions.HillClimbingOn = 0;
823825
runoptions.HeuristicOn = 1;
824826
runoptions.ItImpOn = 1;
825827
break;
826828
case 3:
827829
runoptions.CalcPenaltiesOn = 0;
828830
runoptions.ThermalAnnealingOn = 0;
829-
runoptions.QuantumAnnealingOn = 0;
831+
runoptions.HillClimbingOn = 0;
830832
runoptions.HeuristicOn = 1;
831833
runoptions.ItImpOn = 0;
832834
break;
833835
case 4:
834836
runoptions.CalcPenaltiesOn = 0;
835837
runoptions.ThermalAnnealingOn = 0;
836-
runoptions.QuantumAnnealingOn = 0;
838+
runoptions.HillClimbingOn = 0;
837839
runoptions.HeuristicOn = 0;
838840
runoptions.ItImpOn = 1;
839841
break;
840842
case 5:
841843
runoptions.CalcPenaltiesOn = 0;
842844
runoptions.ThermalAnnealingOn = 0;
843-
runoptions.QuantumAnnealingOn = 0;
845+
runoptions.HillClimbingOn = 0;
844846
runoptions.HeuristicOn = 1;
845847
runoptions.ItImpOn = 1;
846848
break;
847849
case 6:
848850
runoptions.CalcPenaltiesOn = 1;
849851
runoptions.ThermalAnnealingOn = 1;
850-
runoptions.QuantumAnnealingOn = 0;
852+
runoptions.HillClimbingOn = 0;
851853
runoptions.HeuristicOn = 0;
852854
runoptions.ItImpOn = 0;
853855
break;
854856
case 7:
855857
runoptions.CalcPenaltiesOn = 1;
856858
runoptions.ThermalAnnealingOn = 0;
857-
runoptions.QuantumAnnealingOn = 0;
859+
runoptions.HillClimbingOn = 0;
858860
runoptions.HeuristicOn = 0;
859861
runoptions.ItImpOn = 0;
860862
break;
861863
case 8:
862864
runoptions.CalcPenaltiesOn = 0;
863865
runoptions.ThermalAnnealingOn = 1;
864-
runoptions.QuantumAnnealingOn = 0;
866+
runoptions.HillClimbingOn = 0;
865867
runoptions.HeuristicOn = 0;
866868
runoptions.ItImpOn = 0;
867869
break;
868870
case 9:
869871
runoptions.CalcPenaltiesOn = 0;
870872
runoptions.ThermalAnnealingOn = 1;
871-
runoptions.QuantumAnnealingOn = 0;
873+
runoptions.HillClimbingOn = 0;
872874
runoptions.HeuristicOn = 0;
873875
runoptions.ItImpOn = 1;
874876
break;
875877
case 10:
876878
runoptions.CalcPenaltiesOn = 0;
877879
runoptions.ThermalAnnealingOn = 0;
878-
runoptions.QuantumAnnealingOn = 0;
880+
runoptions.HillClimbingOn = 0;
879881
runoptions.HeuristicOn = 0;
880882
runoptions.ItImpOn = 1;
881883
break;
882884
case 11:
883885
runoptions.CalcPenaltiesOn = 1;
884886
runoptions.ThermalAnnealingOn = 0;
885-
runoptions.QuantumAnnealingOn = 1;
887+
runoptions.HillClimbingOn = 1;
886888
runoptions.HeuristicOn = 0;
887889
runoptions.ItImpOn = 0;
888890
break;
889891
case 12:
890892
runoptions.CalcPenaltiesOn = 1;
891893
runoptions.ThermalAnnealingOn = 0;
892-
runoptions.QuantumAnnealingOn = 1;
894+
runoptions.HillClimbingOn = 1;
893895
runoptions.HeuristicOn = 0;
894896
runoptions.ItImpOn = 1;
895897
break;
896898
case 13:
897899
runoptions.CalcPenaltiesOn = 0;
898900
runoptions.ThermalAnnealingOn = 0;
899-
runoptions.QuantumAnnealingOn = 1;
901+
runoptions.HillClimbingOn = 1;
900902
runoptions.HeuristicOn = 0;
901903
runoptions.ItImpOn = 0;
902904
break;
903905
case 14:
904906
runoptions.CalcPenaltiesOn = 0;
905907
runoptions.ThermalAnnealingOn = 0;
906-
runoptions.QuantumAnnealingOn = 1;
908+
runoptions.HillClimbingOn = 1;
907909
runoptions.HeuristicOn = 0;
908910
runoptions.ItImpOn = 1;
909911
break;
910912
default:
911913
runoptions.CalcPenaltiesOn = 0;
912914
runoptions.ThermalAnnealingOn = 0;
913-
runoptions.QuantumAnnealingOn = 0;
915+
runoptions.HillClimbingOn = 0;
914916
runoptions.HeuristicOn = 0;
915917
runoptions.ItImpOn = 0;
916918
break;

options.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace marxan {
7373
int CalcPenaltiesOn;
7474
int HeuristicOn;
7575
int ThermalAnnealingOn;
76-
int QuantumAnnealingOn;
76+
int HillClimbingOn;
7777
int ItImpOn;
7878
} srunoptions;
7979

0 commit comments

Comments
 (0)