Skip to content

Commit

Permalink
Merge pull request #6 from Marxan-source-code/marzone4
Browse files Browse the repository at this point in the history
Marzone4
  • Loading branch information
adelebai committed Mar 24, 2021
2 parents b941fbe + d94b797 commit 9165378
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
3 changes: 2 additions & 1 deletion analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Analysis {
myfile << "\n";
}

for (int i = 0; i < pu.puno; i++)
// Pu must be printed in reverse order for compatibility with zonae cogito
for (int i = pu.puno-1; i >= 0; i--)
{
if (imode > 1)
{
Expand Down
2 changes: 1 addition & 1 deletion marzone.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
marzone.exe
MarZone_x64.exe
pause
39 changes: 27 additions & 12 deletions marzone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
if (fnames.savetotalareas)
{
tempname = fnames.savename + "_totalareas" + getFileSuffix(fnames.savetotalareas);
CalcTotalAreas(pu, spec, tempname, true);
CalcTotalAreas(pu, spec, tempname);
}

// finalise zone and non-zone targets now that matrix has been loaded
Expand Down Expand Up @@ -382,6 +382,10 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
omp_lock_t bestR_write_lock;
omp_init_lock(&bestR_write_lock);

// lock for solutions matrix
omp_lock_t matrix_write_lock;
omp_init_lock(&matrix_write_lock);

//create seeds for local rng engines
vector<unsigned int> seeds(runoptions.repeats);
for (int run_id = 1; run_id <= runoptions.repeats; run_id++)
Expand Down Expand Up @@ -459,6 +463,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
if (runoptions.verbose > 1)
{
progbuffer << " ThermalAnnealing:";
reserveThread.EvaluateObjectiveValue(pu, spec, zones, runoptions.blm);
PrintResVal(reserveThread, spec, zones, runoptions.misslevel, progbuffer);
}

Expand All @@ -475,6 +480,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
if (runoptions.verbose > 1)
{
progbuffer << " PopAnnealing:";
reserveThread.EvaluateObjectiveValue(pu, spec, zones, runoptions.blm);
PrintResVal(reserveThread, spec, zones, runoptions.misslevel, progbuffer);
}
}
Expand All @@ -488,6 +494,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
if (runoptions.verbose > 1 && (runoptions.runopts == 2 || runoptions.runopts == 5))
{
progbuffer << "\n Heuristic:";
reserveThread.EvaluateObjectiveValue(pu, spec, zones, runoptions.blm);
PrintResVal(reserveThread, spec, zones, runoptions.misslevel, progbuffer);
}

Expand All @@ -504,6 +511,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
if (runoptions.verbose > 1)
{
progbuffer << " Iterative Improvement:";
reserveThread.EvaluateObjectiveValue(pu, spec, zones, runoptions.blm);
PrintResVal(reserveThread, spec, zones, runoptions.misslevel, progbuffer);
}
} // Activate Iterative Improvement
Expand All @@ -521,6 +529,9 @@ int MarZone(string sInputFileName, int marxanIsSecondary)

debugbuffer << "WriteSolution ran " << irun << "\n";

// re-evaluate entire system in case of any floating point/evaluation errors.
reserveThread.EvaluateObjectiveValue(pu, spec, zones, runoptions.blm);

if (fnames.savespecies && fnames.saverun)
{
// output species distribution for a run (specific reserve)
Expand All @@ -540,8 +551,6 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
// Saving the best from all the runs
if (fnames.savebest)
{
// re-evaluate entire system in case of any floating point/evaluation errors.
reserveThread.EvaluateObjectiveValue(pu, spec, zones, runoptions.blm);
if (reserveThread.objective.total < bestR.objective.total)
{
omp_set_lock(&bestR_write_lock);
Expand All @@ -564,6 +573,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)

if (fnames.savesolutionsmatrix)
{
omp_set_lock(&matrix_write_lock);
string solutionsMatrixName = fnames.savename + "_solutionsmatrix" + getFileSuffix(fnames.savesolutionsmatrix);
reserveThread.AppendSolutionsMatrix(solutionsMatrixName, zones.zoneCount, fnames.savesolutionsmatrix, fnames.solutionsmatrixheaders);

Expand All @@ -573,6 +583,7 @@ int MarZone(string sInputFileName, int marxanIsSecondary)
// append solutions matrix for each zone separately
reserveThread.AppendSolutionsMatrixZone(solutionsMatrixZoneName, i, fnames.savesolutionsmatrix, fnames.solutionsmatrixheaders);
}
omp_unset_lock(&matrix_write_lock);
}

if (fnames.savezoneconnectivitysum)
Expand Down Expand Up @@ -976,14 +987,17 @@ void ShowPauseExit(void)

void WriteSecondarySyncFileRun(int iSyncRun)
{
FILE* fsync;
char sSyncFileName[80];
FILE *fsync;
char sSyncFileName[80];

sprintf(sSyncFileName,"sync%i",iSyncRun);
if (iSyncRun)
sprintf(sSyncFileName, "sync%i", iSyncRun);
else
sprintf(sSyncFileName, "sync");

fsync = fopen(sSyncFileName,"w");
fprintf(fsync,"%s",sSyncFileName);
fclose(fsync);
fsync = fopen(sSyncFileName, "w");
fprintf(fsync, "%s", sSyncFileName);
fclose(fsync);
}

/* SecondaryExit does not deliver a message prior to exiting, but creates a file so C-Plan knows marxan has exited */
Expand Down Expand Up @@ -1160,7 +1174,7 @@ void ApplySpecProp(Species& spec, Pu& pu)
spec.SetSpeciesProportionTarget(speciesSums);
}

void CalcTotalAreas(Pu& pu, Species& spec, string filename /*= "MarZoneTotalAreas.csv"*/, bool save /*= false*/)
void CalcTotalAreas(Pu& pu, Species& spec, string filename /*= "MarZoneTotalAreas.csv"*/, bool save /*= true*/)
{
int ipu, i, ism, isp;
vector<int> TotalOccurrences, TO_2, TO_3;
Expand Down Expand Up @@ -1347,15 +1361,16 @@ int main(int argc,char *argv[])
marzone::SecondaryExit();
return 1;
} // Abnormal Exit
if (marxanIsSecondary == 1)
marzone::SecondaryExit();
}
catch (const std::exception& e)
{
marzone::logger.ShowErrorMessage("Error occurred in execution: " + std::string(e.what()));
exit(EXIT_FAILURE);
}

if (marxanIsSecondary == 1)
marzone::SecondaryExit();

return 0;
}

2 changes: 1 addition & 1 deletion marzone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace marzone
/* Protytpe function Headers */
void SetRunOptions(srunoptions &runoptions);
int CalcPenalties(Pu &pu, Species &spec, Zones &zones, Reserve &r, int clumptype);
void CalcTotalAreas(Pu& pu, Species& spec, string filename = "MarZoneTotalAreas.csv", bool save = false);
void CalcTotalAreas(Pu& pu, Species& spec, string filename = "MarZoneTotalAreas.csv", bool save = true);
double ReturnPuZoneCost(int ipu,int iZone, Pu& pu, Zones& zones);

void ApplySpecProp(Species& spec, Pu& pu);
Expand Down
14 changes: 13 additions & 1 deletion reserve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ namespace marzone
// Clear map clumps for this species
if (spec.specList[isp].target2)
ClearClumps(isp); // for target2 species, clear existing pu.

// Reset zone species if specified
int zoneBase = isp * zones.zoneCount;
for (int i = 0; i < zones.zoneCount; i++) {
iZoneSpecIndex = zoneBase + i;
zoneSpec[iZoneSpecIndex].amount = 0;
zoneSpec[iZoneSpecIndex].occurrence = 0;
}
}

for (int ipu = 0; ipu < solution.size(); ipu++)
Expand Down Expand Up @@ -927,7 +935,7 @@ namespace marzone
rOccurrenceTarget = zones.zoneTarget[isp][i].occurrence;
rOccurrencesHeld = zoneSpec[iZoneArrayIndex].occurrence;

myfile << d << rTarget << d << rAmountHeld << d << zones.zoneContribValues[iZoneArrayIndex] << d << rOccurrenceTarget << d << rOccurrencesHeld;
myfile << d << rTarget << d << rAmountHeld << d << zones.zoneContribValues[iZoneArrayIndex]*rAmountHeld << d << rOccurrenceTarget << d << rOccurrencesHeld;

temp = ReturnStringIfTargetMet(rTarget, rAmountHeld, rOccurrenceTarget, rOccurrencesHeld, rMPM, misslevel);
myfile << d << temp;
Expand Down Expand Up @@ -1157,6 +1165,10 @@ namespace marzone
if (rTestMPM < rMPM)
rMPM = rTestMPM;
}

if (!targetArea && !targetOcc)
temp = "";

return temp;
}

Expand Down
4 changes: 3 additions & 1 deletion zones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,9 @@ class Zones {
{
if (i == 0) // the "available" zone.
{
zoneContribValues[(j*zoneCount)+i] = 0;
// zoneContribValues[(j*zoneCount)+i] = 0;
// It is unclear what the original code was doing with the "availableZoneInput", so we will keep consistent w/ manual and default all to 1.
zoneContribValues[(j*zoneCount)+i] = 1;
} else {
zoneContribValues[(j*zoneCount)+i] = 1;
}
Expand Down

0 comments on commit 9165378

Please sign in to comment.