Concepts
Features
Reference
Gurobi batch object. Batch optimization is a feature available with theGurobi Cluster Manager. It allows a client program to build anoptimization model, submit it to a Compute Server cluster (through aCluster Manager), and later check on the status of the model andretrieve its solution. For more information, please refer to theBatch Optimization section.
Commonly used methods on batch objects includeupdate(refresh attributes from the Cluster Manager),abort (abortexecution of a batch request),retry (retry optimization foran interrupted or failed batch),discard (remove the batchrequest and all related information from the Cluster Manager), andgetJSONSolution (query solution information for the batchrequest).
These methods are built on top of calls to the Cluster Manager REST API.They are meant to simplify such calls, but note that you always have theoption of calling the REST API directly.
Batch objects have four attributes:
BatchID: Unique ID for the batch request.
BatchStatus: Last batch status. Status values aredescribed in theBatch Status Code section.
BatchErrorCode: Last error code.
BatchErrorMessage: Last error message.
You can access their values as you would for other attributes:batch.BatchStatus,batch.BatchID, etc. Note that all Batchattributes are locally cached, and are only updated when you create aclient-side batch object or when you explicitly update this cache, whichcan done by callingupdate.
Given aBatchID, as returned byoptimizeBatch, and a Gurobi environment thatcan connect to the appropriate Cluster Manager (i.e., one whereparametersCSManager,UserName, andServerPassword have been setappropriately), this function returns aBatch object. With it, you can querythe current status of the associated batch request and, once the batchrequest has been processed, you can query its solution. Please refer totheBatch Optimization section for detailsand examples.
batchID – ID of the batch request for which you want to accessstatus and other information.
env – The environment in which the new batch object should becreated.
New batch object.
batch=gp.Batch(batchID,env)# Automatically disposed with context managerwithgp.Batch(batchID,env)asbatch:pass
This method instructs the Cluster Manager to abort the processing ofthis batch request, changing its status to ABORTED. Please refer to theBatch Status Codes section for furtherdetails.
starttime=time.time()whilebatch.BatchStatus==GRB.BATCH_SUBMITTED:# Abort this batch if it is taking too longcurtime=time.time()ifcurtime-starttime>maxwaittime:batch.abort()break
This method instructs the Cluster Manager to remove all informationrelated to the batch request in question, including the stored solutionif available. Further queries for the associated batch request will failwith error codeDATA_NOT_AVAILABLE. Use thisfunction with care, as the removed information can not be recoveredlater on.
# Remove batch request from managerbatch.discard()
Free all resources associated with this Batch object. After this methodis called, this Batch object must no longer be used.
batch.dispose()
This method retrieves the solution of a completed batch request from aCluster Manager. The solution is returned as aJSON solutionstring. For this call to succeed, the status of the batchrequest must be COMPLETED. Note further that the result file storedCluster Manager side must be gzip-compressed and exactly one result fileshould be associated with this batch; for batches submittedprogrammatically through the API both will be the case. Please refer totheBatch Status Codes section for furtherdetails.
print("JSON solution:")# Get JSON solution as string, create dict from itsol=json.loads(batch.getJSONSolution())
This method instructs the Cluster Manager to retry optimization of afailed or aborted batch request, changing its status to SUBMITTED.Please refer to theBatch Status Codessection for further details.
starttime=time.time()whilebatch.BatchStatus==GRB.BATCH_SUBMITTED:# Abort this batch if it is taking too longcurtime=time.time()ifcurtime-starttime>maxwaittime:batch.abort()break# Wait for two secondstime.sleep(2)# Update the resident attribute cache of the Batch object with the# latest values from the cluster manager.batch.update()# If the batch failed, we retry itifbatch.BatchStatus==GRB.BATCH_FAILED:batch.retry()
All Batch attribute values are cached locally, so queries return thevalue received during the last communication with the Cluster Manager.This method refreshes the values of all attributes with the valuescurrently available in the Cluster Manager (which involves networkcommunication).
# Update the resident attribute cache of the Batch object with the# latest values from the cluster manager.batch.update()
This method returns the stored solution of a completed batch requestfrom a Cluster Manager. The solution is returned in a gzip-compressedJSON file. The file name you provide must end with a .json.gz extension.The JSON format is described in theJSON solution format section. Note that for this call to succeed, the status ofthe batch request must be COMPLETED. Note further that the result filestored Cluster Manager side must be gzip-compressed and exactly oneresult file should be associated with this batch; for batches submittedprogrammatically through the API both will be the case. Please refer totheBatch Status Codes section for furtherdetails.
filename – Name of file where the solution should be stored (inJSON format).
# Write the full JSON solution string to a filebatch.writeJSONSolution("batch-sol.json.gz")
Help and Feedback