Skip to content

Commit

Permalink
Improve solver API
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaslundell committed Feb 8, 2025
1 parent 7d5b1a2 commit d39e997
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
55 changes: 25 additions & 30 deletions src/SHOT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
*/

#include "Environment.h"
#include "Output.h"
#include "Solver.h"
#include "Report.h"
#include "Utilities.h"
#include "Output.h"
#include "Settings.h"
#include "Problem.h"

#include "argh.h"

Expand Down Expand Up @@ -67,7 +64,7 @@ int main(int argc, char* argv[])

if(cmdl["--help"])
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;

#ifdef SIMPLE_OUTPUT_CHARS
Expand Down Expand Up @@ -161,7 +158,7 @@ int main(int argc, char* argv[])
{
if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand All @@ -175,7 +172,7 @@ int main(int argc, char* argv[])

env->output->outputInfo("");

std::string markup = env->settings->getSettingsAsMarkup();
std::string markup = solver.getSettingsAsMarkup();

auto filepath = fs::filesystem::current_path() / fs::filesystem::path("options.md");
if(!Utilities::writeStringToFile(filepath.string(), markup))
Expand Down Expand Up @@ -203,7 +200,7 @@ int main(int argc, char* argv[])
{
if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand Down Expand Up @@ -238,7 +235,7 @@ int main(int argc, char* argv[])
{
if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand Down Expand Up @@ -275,7 +272,7 @@ int main(int argc, char* argv[])
{
if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand Down Expand Up @@ -310,7 +307,7 @@ int main(int argc, char* argv[])
{
if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand Down Expand Up @@ -341,7 +338,7 @@ int main(int argc, char* argv[])
{
if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand Down Expand Up @@ -596,12 +593,11 @@ int main(int argc, char* argv[])
}

// Need to set the log levels after we have read the options from file and console
env->output->setLogLevels(static_cast<E_LogLevel>(env->settings->getSetting<int>("Console.LogLevel", "Output")),
static_cast<E_LogLevel>(env->settings->getSetting<int>("File.LogLevel", "Output")));
solver.updateLogLevels();

if(!headerPrinted)
{
env->report->outputSolverHeader();
solver.outputSolverHeader();
headerPrinted = true;
}

Expand Down Expand Up @@ -649,7 +645,7 @@ int main(int argc, char* argv[])
}

// Check if we want to use the ASL calling format
if(useASL && !((ES_SourceFormat)env->settings->getSetting<int>("SourceFormat", "Input") == ES_SourceFormat::NL))
if(useASL && !((ES_SourceFormat)solver.getSetting<int>("SourceFormat", "Input") == ES_SourceFormat::NL))
{
env->output->outputCritical(" Error: Can only use parameter AMPL if the problem is a AMPL (.nl) file.");
return (0);
Expand All @@ -661,43 +657,42 @@ int main(int argc, char* argv[])
if(cmdl("--osrl")) // Have specified a OSrL-file location
{
osrlFilename = cmdl("--osrl").str();
resultFile = fs::filesystem::path(env->settings->getSetting<std::string>("ResultPath", "Output"))
resultFile = fs::filesystem::path(solver.getSetting<std::string>("ResultPath", "Output"))
/ fs::filesystem::path(osrlFilename);
}

std::string trcFilename;
if(cmdl("--trc")) // Have specified a trace-file location
{
trcFilename = cmdl("--trc").str();
traceFile = fs::filesystem::path(env->settings->getSetting<std::string>("ResultPath", "Output"))
traceFile = fs::filesystem::path(solver.getSetting<std::string>("ResultPath", "Output"))
/ fs::filesystem::path(trcFilename);
}

std::string solFilename;
if(cmdl("--sol")) // Have specified an sol-file location
{
solFilename = cmdl("--sol").str();
solFile = fs::filesystem::path(env->settings->getSetting<std::string>("ResultPath", "Output"))
solFile = fs::filesystem::path(solver.getSetting<std::string>("ResultPath", "Output"))
/ fs::filesystem::path(solFilename);
}

std::string gdxFilename;
if(gdxFilename = env->settings->getSetting<std::string>("GAMS.AlternateSolutionsFile", "Output");
if(gdxFilename = solver.getSetting<std::string>("GAMS.AlternateSolutionsFile", "Output");
gdxFilename != "") // Have specified an gdx-file location
{
gdxFile = fs::filesystem::absolute(gdxFilename).string();
}

env->report->outputProblemInstanceReport();
env->report->outputOptionsReport();
solver.outputProblemInstanceReport();
solver.outputOptionsReport();

if(!solver.solveProblem()) // Solve the problem
{
return (0);
}

solver.finalizeSolution();
env->report->outputSolutionReport();
solver.outputSolutionReport();

#ifdef SIMPLE_OUTPUT_CHARS
env->output->outputInfo("-----------------------------------------------------------------------------------"
Expand All @@ -713,8 +708,8 @@ int main(int argc, char* argv[])

if(resultFile.empty())
{
fs::filesystem::path resultPath(env->settings->getSetting<std::string>("ResultPath", "Output"));
resultPath /= env->settings->getSetting<std::string>("ProblemName", "Input");
fs::filesystem::path resultPath(solver.getSetting<std::string>("ResultPath", "Output"));
resultPath /= solver.getSetting<std::string>("ProblemName", "Input");
resultPath = resultPath.replace_extension(".osrl");

if(!Utilities::writeStringToFile(resultPath.string(), osrl))
Expand All @@ -739,8 +734,8 @@ int main(int argc, char* argv[])

if(traceFile.empty())
{
fs::filesystem::path tracePath(env->settings->getSetting<std::string>("ResultPath", "Output"));
tracePath /= env->settings->getSetting<std::string>("ProblemName", "Input");
fs::filesystem::path tracePath(solver.getSetting<std::string>("ResultPath", "Output"));
tracePath /= solver.getSetting<std::string>("ProblemName", "Input");
tracePath = tracePath.replace_extension(".trc");

if(!Utilities::writeStringToFile(tracePath.string(), trace))
Expand Down Expand Up @@ -782,9 +777,9 @@ int main(int argc, char* argv[])

env->output->outputInfo(" Log written to: " + logFile.string());

if(env->settings->getSetting<bool>("Debug.Enable", "Output"))
if(solver.getSetting<bool>("Debug.Enable", "Output"))
{
auto debugDirectory = fs::filesystem::path(env->settings->getSetting<std::string>("Debug.Path", "Output"));
auto debugDirectory = fs::filesystem::path(solver.getSetting<std::string>("Debug.Path", "Output"));
env->output->outputInfo(" Debug directory: " + debugDirectory.string());
}

Expand Down
15 changes: 15 additions & 0 deletions src/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,20 @@ bool Solver::setOptionsFromOSoL(std::string options)
return (status);
}

std::string Solver::getSettingsAsMarkup() { return (env->settings->getSettingsAsMarkup()); }

bool Solver::setLogFile(std::string filename)
{
env->output->setFileSink(filename);
return (true);
}

void Solver::updateLogLevels()
{
env->output->setLogLevels(static_cast<E_LogLevel>(env->settings->getSetting<int>("Console.LogLevel", "Output")),
static_cast<E_LogLevel>(env->settings->getSetting<int>("File.LogLevel", "Output")));
}

bool Solver::setProblem(std::string fileName)
{
if(!fs::filesystem::exists(fileName))
Expand Down Expand Up @@ -652,6 +660,8 @@ bool Solver::solveProblem()
assert(solutionStrategy != nullptr); /* would be NULL if setProblem failed */
isProblemSolved = solutionStrategy->solveProblem();

this->finalizeSolution();

return (isProblemSolved);
}

Expand All @@ -661,6 +671,11 @@ void Solver::finalizeSolution()
env->modelingSystem->finalizeSolution();
}

void Solver::outputSolverHeader() { env->report->outputSolverHeader(); }
void Solver::outputOptionsReport() { env->report->outputOptionsReport(); }
void Solver::outputProblemInstanceReport() { env->report->outputProblemInstanceReport(); }
void Solver::outputSolutionReport() { env->report->outputSolutionReport(); }

std::string Solver::getResultsOSrL() { return (env->results->getResultsOSrL()); }

std::string Solver::getOptionsOSoL()
Expand Down
18 changes: 17 additions & 1 deletion src/Solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Environment.h"
#include "Enums.h"
#include "EventHandler.h"
#include "Settings.h"
#include "Structs.h"

#include "ModelingSystem/IModelingSystem.h"
Expand All @@ -42,6 +43,8 @@ class DllExport Solver

bool selectStrategy();

void finalizeSolution();

bool isProblemInitialized = false;
bool isProblemSolved = false;

Expand All @@ -59,7 +62,10 @@ class DllExport Solver
bool setOptionsFromString(std::string options);
bool setOptionsFromOSoL(std::string options);

std::string getSettingsAsMarkup();

bool setLogFile(std::string filename);
void updateLogLevels();

bool setProblem(std::string fileName);
bool setProblem(ProblemPtr problem, ProblemPtr reformulatedProblem, ModelingSystemPtr modelingSystem = nullptr);
Expand All @@ -73,7 +79,10 @@ class DllExport Solver

bool solveProblem();

void finalizeSolution();
void outputSolverHeader();
void outputOptionsReport();
void outputProblemInstanceReport();
void outputSolutionReport();

template <typename Callback> inline void registerCallback(const E_EventType& event, Callback&& callback)
{
Expand All @@ -92,6 +101,11 @@ class DllExport Solver
void updateSetting(std::string name, std::string category, double value);
void updateSetting(std::string name, std::string category, bool value);

template <typename T> T getSetting(std::string name, std::string category)
{
return (env->settings->getSetting<T>(name, category));
}

VectorString getSettingIdentifiers(E_SettingType type);

double getCurrentDualBound();
Expand All @@ -103,6 +117,8 @@ class DllExport Solver
PrimalSolution getPrimalSolution();
std::vector<PrimalSolution> getPrimalSolutions();

SolutionStatistics getSetSolutionStatistics() { return env->solutionStatistics; };

E_TerminationReason getTerminationReason();
E_ModelReturnStatus getModelReturnStatus();
};
Expand Down

0 comments on commit d39e997

Please sign in to comment.