Skip to content

Commit

Permalink
Add option to set the number of threads for the CPLEX_PY API (#666)
Browse files Browse the repository at this point in the history
* set number of threads for CPLEX_PY

* rename CPLEX_PY threads option for consistency

* changed CPLEX_PY threads default to "None" for consistency
  • Loading branch information
f-kretschmer authored Sep 24, 2023
1 parent 63d1616 commit ea0e35a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pulp/apis/cplex_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ def __init__(
logPath=None,
epgap=None,
logfilename=None,
threads=None
):
"""
:param bool mip: if False, assume LP even if integer variables
Expand All @@ -313,6 +314,7 @@ def __init__(
:param str logPath: path to the log file
:param float epgap: deprecated for gapRel
:param str logfilename: deprecated for logPath
:param int threads: number of threads to be used by CPLEX to solve a problem (default None uses all available)
"""
if epgap is not None:
warnings.warn("Parameter epgap is being depreciated for gapRel")
Expand All @@ -337,6 +339,7 @@ def __init__(
timeLimit=timeLimit,
warmStart=warmStart,
logPath=logPath,
threads=threads,
)

def available(self):
Expand Down Expand Up @@ -455,6 +458,7 @@ def cplex_var_types(var):
self.changeEpgap(gapRel)
if self.timeLimit is not None:
self.setTimeLimit(self.timeLimit)
self.setThreads(self.optionsDict.get("threads", None))
if self.optionsDict.get("warmStart", False):
# We assume "auto" for the effort_level
effort = self.solverModel.MIP_starts.effort_level.auto
Expand All @@ -478,6 +482,12 @@ def setlogfile(self, fileobj):
self.solverModel.set_warning_stream(fileobj)
self.solverModel.set_results_stream(fileobj)

def setThreads(self, threads=None):
"""
Change cplex thread count used (None is default which uses all available resources)
"""
self.solverModel.parameters.threads.set(threads or 0)

def changeEpgap(self, epgap=10**-4):
"""
Change cplex solver integer bound gap tolerence
Expand Down

0 comments on commit ea0e35a

Please sign in to comment.