Skip to content

Commit

Permalink
Enable logging of Solver logs to files.
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritkalra committed Nov 2, 2023
1 parent 6cdee4e commit 98ba0ad
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 5 deletions.
3 changes: 3 additions & 0 deletions schedulers/tetrisched/include/tetrisched/CPLEXSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class CPLEXSolver : public Solver {
/// Export the constructed model to the given file.
void exportModel(const std::string& fileName) override;

/// Set the log file for the solver to output its log to.
void setLogFile(const std::string& fileName) override;

/// Solve the constructed model.
SolverSolutionPtr solveModel() override;

Expand Down
3 changes: 3 additions & 0 deletions schedulers/tetrisched/include/tetrisched/GoogleCPSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class GoogleCPSolver : public Solver {
/// Export the constructed model to the given file.
void exportModel(const std::string& fileName) override;

/// Set the log file for the solver to output its log to.
void setLogFile(const std::string& fileName) override;

/// Solve the constructed model.
SolverSolutionPtr solveModel() override;

Expand Down
7 changes: 6 additions & 1 deletion schedulers/tetrisched/include/tetrisched/GurobiSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class GurobiSolver : public Solver {
/// A map from the ID of the SolverModel variables to internal Gurobi
/// variables.
std::unordered_map<uint32_t, GRBVar> gurobiVariables;
/// The name of the log file for the Gurobi solving.
std::string logFileName;

/// Set the defaults for parameters on the model.
void setDefaultParameters(GRBModel& gurobiModel);
void setParameters(GRBModel& gurobiModel);

/// Translate the variable to a Gurobi variable.
GRBVar translateVariable(GRBModel& gurobiModel,
Expand Down Expand Up @@ -51,6 +53,9 @@ class GurobiSolver : public Solver {
/// Export the constructed model to the given file.
void exportModel(const std::string& fileName) override;

/// Set the log file for the solver to output its log to.
void setLogFile(const std::string& fileName) override;

/// Solve the constructed model.
SolverSolutionPtr solveModel() override;

Expand Down
3 changes: 3 additions & 0 deletions schedulers/tetrisched/include/tetrisched/Solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Solver {
/// Export the constructed model to the given file.
virtual void exportModel(const std::string& fileName) = 0;

/// Set the log file for the solver to output its log to.
virtual void setLogFile(const std::string& fileName) = 0;

/// Solve the constructed model.
virtual SolverSolutionPtr solveModel() = 0;

Expand Down
5 changes: 5 additions & 0 deletions schedulers/tetrisched/src/CPLEXSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ void CPLEXSolver::exportModel(const std::string& fname) {
cplexInstance.exportModel(fname.c_str());
}

void CPLEXSolver::setLogFile(const std::string& fname) {
throw tetrisched::exceptions::SolverException(
"setLogFile() not implemented for CPLEXSolver.");
}

SolverSolutionPtr CPLEXSolver::solveModel() {
// Create the result object.
SolverSolutionPtr solverSolution = std::make_shared<SolverSolution>();
Expand Down
5 changes: 5 additions & 0 deletions schedulers/tetrisched/src/GoogleCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ void GoogleCPSolver::exportModel(const std::string& fileName) {
cpModel->ExportToFile(fileName);
}

void GoogleCPSolver::setLogFile(const std::string& fname) {
throw tetrisched::exceptions::SolverException(
"setLogFile() not implemented for GoogleCPSolver.");
}

SolverSolutionPtr GoogleCPSolver::solveModel() {
throw exceptions::SolverException("Not implemented yet!");
}
Expand Down
22 changes: 18 additions & 4 deletions schedulers/tetrisched/src/GurobiSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

namespace tetrisched {
GurobiSolver::GurobiSolver()
: gurobiEnv(new GRBEnv()), gurobiModel(new GRBModel(*gurobiEnv)) {
setDefaultParameters(*gurobiModel);
: gurobiEnv(new GRBEnv()),
gurobiModel(new GRBModel(*gurobiEnv)),
logFileName("") {
setParameters(*gurobiModel);
}

SolverModelPtr GurobiSolver::getModel() {
Expand All @@ -20,16 +22,24 @@ void GurobiSolver::setModel(SolverModelPtr solverModelPtr) {
solverModel = solverModelPtr;
}

void GurobiSolver::setDefaultParameters(GRBModel& gurobiModel) {
void GurobiSolver::setParameters(GRBModel& gurobiModel) {
// Set the maximum numer of threads.
const auto thread_count = std::thread::hardware_concurrency();
gurobiModel.set(GRB_IntParam_Threads, thread_count);

// Ask Gurobi to aggressively cut the search space.
gurobiModel.set(GRB_IntParam_Cuts, 3);

// Ask Gurobi to aggressively presolve the model.
gurobiModel.set(GRB_IntParam_Presolve, 2);

// Ask Gurobi to find new incumbent solutions rather than prove bounds.
gurobiModel.set(GRB_IntParam_MIPFocus, 1);

// Ask Gurobi to not output to the console, and instead direct it
// to the specified file.
// gurobiModel.set(GRB_IntParam_LogToConsole, 0);
gurobiModel.set(GRB_StringParam_LogFile, logFileName);
}

GRBVar GurobiSolver::translateVariable(GRBModel& gurobiModel,
Expand Down Expand Up @@ -144,7 +154,7 @@ void GurobiSolver::translateModel() {
}

gurobiModel = std::make_unique<GRBModel>(*gurobiEnv);
setDefaultParameters(*gurobiModel);
setParameters(*gurobiModel);

// Generate all the variables and keep a cache of the variable indices
// to the Gurobi variables.
Expand Down Expand Up @@ -183,6 +193,10 @@ void GurobiSolver::exportModel(const std::string& fileName) {
gurobiModel->write(fileName);
}

void GurobiSolver::setLogFile(const std::string& fileName) {
logFileName = fileName;
}

SolverSolutionPtr GurobiSolver::solveModel() {
// Create the result object.
SolverSolutionPtr solverSolution = std::make_shared<SolverSolution>();
Expand Down
4 changes: 4 additions & 0 deletions schedulers/tetrisched/src/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ void Scheduler::schedule(Time currentTime) {
"Please invoke registerSTRL() first.");
}

// Set the log file based on the current time.
std::string logFileName = "tetrisched_" + std::to_string(currentTime) + ".log";
this->solver->setLogFile(logFileName);

// Translate the model to the solver backend.
this->solver->translateModel();

Expand Down

0 comments on commit 98ba0ad

Please sign in to comment.