Skip to content

Commit 62147ed

Browse files
author
Adele Bai
committed
Minor bugfixes and console printing
Minor bugfixes and console printing
1 parent c0f455c commit 62147ed

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ marxan
33
input
44
input.dat
55
output
6+
bin
7+
*.exe

heuristics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ double GreedyScore(int ipu,int puno,int spno, vector<sspecies>& spec,vector<spu>
3434
double currpen, currcost, currscore;
3535

3636
currpen = GreedyPen(ipu, puno, spno, spec, R, pu, SM, clumptype, thread);
37-
currcost = pu[ipu].cost + ConnectionCost2(ipu, connections, R, 1, 1, cm);
37+
currcost = pu[ipu].cost + ConnectionCost2(ipu, connections, R, 1, 1, cm, asymmetricconnectivity, fOptimiseConnectivityIn);
3838
if (currcost <= 0)
3939
{
4040
currscore = -1.0 / delta;
@@ -107,7 +107,7 @@ double RareScore(int isp,int ipu,int puno,vector<sspecies> &spec,vector<spu> &SM
107107
currpen = newamount - fold;
108108
} // Add new penalty if species isn't already in the system
109109

110-
currcost = pu[ipu].cost + ConnectionCost2(ipu, connections, R, 1, 1, cm);
110+
currcost = pu[ipu].cost + ConnectionCost2(ipu, connections, R, 1, 1, cm, asymmetricconnectivity, fOptimiseConnectivityIn);
111111
if (currcost <= 0)
112112
{
113113
currscore = -1.0 / delta;

input.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,11 @@ void readPenalties(vector<sspecies> &spec,int spno,sfname& fnames,map<int,int> &
613613
void readSparseMatrixSpOrder(int &iSMSize, vector<spusporder> &SM, int puno, int spno,
614614
map<int,int> &PULookup,map<int,int> &SPLookup, vector<sspecies> &spec,
615615
sfname &fnames) {
616+
vector<map<int,spusporder>> SMTemp;
616617
FILE *fp;
617618
string readname;
618-
char sLine[500],*sVarName,*sVarVal;
619-
int i, _spid,spid, _puid, iInternalSMSize = 0, iBigMatrixSize, iLastSPID;
619+
char sLine[500],*sVarVal;
620+
int _spid, _puid, iInternalSMSize = 0, iBigMatrixSize, iLastSPID;
620621
double amount, rDensity, rInternalSMSize, rBigMatrixSize;
621622

622623
readname = fnames.inputdir + fnames.matrixspordername;
@@ -627,54 +628,58 @@ void readSparseMatrixSpOrder(int &iSMSize, vector<spusporder> &SM, int puno, int
627628
if (fgets(sLine,500-1,fp) == NULL)
628629
displayErrorMessage("Error reading sparse matrix sporder.\n");
629630

630-
while (fgets(sLine,500-1,fp))
631-
iInternalSMSize++;
632-
633-
rewind(fp);
634-
635-
iSMSize = iInternalSMSize;
636-
637631
// create the sparse matrix
638-
SM.resize(iInternalSMSize);
632+
SMTemp.resize(spno);
639633

640-
iLastSPID = -1;
641634
// planning unit richness and offset are already set to zero
642635
// init with zero values
643-
for (i=0;i<iInternalSMSize;i++)
636+
while (fgets(sLine,500-1,fp))
644637
{
645-
fgets(sLine,500-1,fp);
646-
647638
sVarVal = strtok(sLine," ,;:^*\"/\t\'\\\n");
648639
sscanf(sVarVal,"%d",&_spid);
649640
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
650641
sscanf(sVarVal,"%d",&_puid);
651642
sVarVal = strtok(NULL," ,;:^*\"/\t\'\\\n");
652643
sscanf(sVarVal,"%lf",&amount);
653644

654-
if (_spid < iLastSPID)
655-
{
656-
// error condition exists, file is not in ascending order for SPID
657-
appendTraceFile("Error: PU v Species file %s is not in ascending order for SPID at record %i.\nAborting Program.",fnames.puvsprname,i+1);
658-
displayErrorMessage("Error: PU v Species file %s is not in ascending order for SPID at record %i.\nAborting Program.",fnames.puvsprname,i+1);
645+
int old_puid;
646+
try {
647+
old_puid = _puid;
648+
_puid = PULookup.at(_puid);
649+
}
650+
catch (out_of_range ex) {
651+
displayWarningMessage("Puid %d found in puvspr file but not found in pu file. Ignoring.\n", old_puid);
659652
}
660653

661-
iLastSPID = _spid;
662-
663-
_puid = PULookup[_puid];
664-
spid = SPLookup[_spid];
654+
int old_spid;
655+
try {
656+
old_spid = _spid;
657+
_spid = SPLookup.at(_spid);
658+
}
659+
catch (out_of_range ex) {
660+
displayWarningMessage("Spid %d found in puvspr file but not found in spec file. Ignoring.\n", old_spid);
661+
}
665662

666663
// increment richness for planning unit containing this feature
667-
spec[spid].richness += 1;
668-
// if planning units richness is one, set its offset
669-
if (spec[spid].richness == 1)
670-
spec[spid].offset = i;
671-
672-
SM[i].amount = amount;
673-
SM[i].puindex = _puid;
664+
spec[_spid].richness += 1;
665+
SMTemp[_spid][_puid].amount = amount;
674666
}
675667

676668
fclose(fp);
677669

670+
// Fill the SM vector
671+
int j = 0;
672+
for (int i = 0; i < spno; i++) {
673+
spec[i].offset = j;
674+
for (auto& [puindex, value]: SMTemp[i]) {
675+
spusporder temp;
676+
temp.amount = value.amount;
677+
temp.puindex = puindex;
678+
SM.push_back(temp);
679+
j++;
680+
}
681+
}
682+
678683
iBigMatrixSize = puno * spno;
679684
rInternalSMSize = iInternalSMSize;
680685
rBigMatrixSize = iBigMatrixSize;

marxan.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ void executeRunLoop(int iSparseMatrixFileLength, long int repeats,int puno,int s
121121
int maxThreads = omp_get_max_threads();
122122

123123
printf("Running multithreaded over number of threads: %d\n", maxThreads);
124+
displayProgress1("Running multithreaded over number of threads: " + to_string(maxThreads) + "\n");
124125
#pragma omp parallel for
125126
for (int i = 1; i <= repeats; i++)
126127
{
@@ -331,6 +332,7 @@ void executeRunLoop(int iSparseMatrixFileLength, long int repeats,int puno,int s
331332
// On exceptions, append exception to log file in addition to existing buffer.
332333
displayProgress1(runConsoleOutput.str());
333334
appendLogBuffer << "Exception occurred on run " << i << ": " << e.what() << endl;
335+
displayProgress1(appendLogBuffer.str());
334336
appendTraceFile(appendLogBuffer.str());
335337

336338
throw(e);
@@ -354,7 +356,11 @@ void executeRunLoop(int iSparseMatrixFileLength, long int repeats,int puno,int s
354356
string tempname2 = savename + "_sum" + getFileNameSuffix(fnames.savesum);
355357
writeSummary(tempname2, summaries, fnames.saverun);
356358
}
357-
cout << "\nBest run: " << bestRun << " Best score: " << bestScore << "\n" << bestRunString;
359+
360+
stringstream bestOut;
361+
bestOut << "\nBest run: " << bestRun << " Best score: " << bestScore << "\n" << bestRunString;
362+
cout << bestOut.str();
363+
displayProgress1(bestOut.str());
358364
} // executeRunLoop
359365

360366
int executeMarxan(string sInputFileName)
@@ -1628,7 +1634,7 @@ void computeQuantumChangeScore(int spno,int puno, vector<spustuff>& pu, vector<s
16281634
#endif
16291635

16301636
change.cost += pu[j].cost * imode; /* Cost of this PU on it's own */
1631-
change.connection += ConnectionCost2(j, connections, R, imode, 1, cm);
1637+
change.connection += ConnectionCost2(j, connections, R, imode, 1, cm, asymmetricconnectivity, fOptimiseConnectivityIn);
16321638
if (threshtype == 1)
16331639
{
16341640
tchangeconnection = change.connection;

utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void trim(std::string &s) {
4848
rtrim(s);
4949
}
5050

51-
// Adds '/' or '\' to end of dir string if not existent
51+
// Adds '/' to end of dir string if not existent
5252
inline
5353
std::string cleanDirectoryString(std::string dirName) {
5454
if (dirName.back() != '\\' && dirName.back() != '/') {

validation/validate_solution.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""
22
This script checks the given marxan solution's cost and shortfall.
33
Use this to independently validate whether a marxan solution
4+
5+
Example run
6+
python validate_solution.py "../bin/data/Base_30_costadd100/output/output_best.txt" "../bin/data/Base_30_costadd100/input/pu.dat" "../bin/data/Base_30_costadd100/input/spec.dat" "../bin/data/Base_30_costadd100/input/puvspr.dat"
47
"""
58
import argparse
69
import csv
@@ -91,7 +94,10 @@ def calculate_shortfall(contributions, targets):
9194
shortfall = 0.0
9295

9396
for spec in targets:
94-
shortfall += max(0, targets[spec] - contributions[spec])
97+
if spec in contributions:
98+
shortfall += max(0, targets[spec] - contributions[spec])
99+
else:
100+
shortfall += max(0, targets[spec])
95101

96102
return shortfall
97103

0 commit comments

Comments
 (0)