Skip to content

Commit

Permalink
Increase number of threads and set MIPFocus in Gurobi.
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritkalra committed Nov 1, 2023
1 parent 47d36ae commit 6cdee4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion schedulers/tetrisched/include/tetrisched/GurobiSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class GurobiSolver : public Solver {
/// variables.
std::unordered_map<uint32_t, GRBVar> gurobiVariables;

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

/// Translate the variable to a Gurobi variable.
GRBVar translateVariable(GRBModel& gurobiModel,
const VariablePtr& variable) const;
Expand All @@ -25,7 +28,7 @@ class GurobiSolver : public Solver {
GRBConstr translateConstraint(GRBModel& gurobiModel,
const ConstraintPtr& constraint) const;

/// Translate teh ObjectiveFunction into a Gurobi expression.
/// Translate the ObjectiveFunction into a Gurobi expression.
GRBLinExpr translateObjectiveFunction(
GRBModel& gurobiModel,
const ObjectiveFunctionPtr& objectiveFunction) const;
Expand Down
18 changes: 17 additions & 1 deletion schedulers/tetrisched/src/GurobiSolver.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "tetrisched/GurobiSolver.hpp"

#include <chrono>
#include <thread>

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

SolverModelPtr GurobiSolver::getModel() {
if (!solverModel) {
Expand All @@ -17,6 +20,18 @@ void GurobiSolver::setModel(SolverModelPtr solverModelPtr) {
solverModel = solverModelPtr;
}

void GurobiSolver::setDefaultParameters(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 find new incumbent solutions rather than prove bounds.
gurobiModel.set(GRB_IntParam_MIPFocus, 1);
}

GRBVar GurobiSolver::translateVariable(GRBModel& gurobiModel,
const VariablePtr& variable) const {
// Note (Sukrit): Do not use value_or here since the type coercion renders
Expand Down Expand Up @@ -129,6 +144,7 @@ void GurobiSolver::translateModel() {
}

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

// Generate all the variables and keep a cache of the variable indices
// to the Gurobi variables.
Expand Down

0 comments on commit 6cdee4e

Please sign in to comment.