/* Copyright 2025, Gurobi Optimization, LLC *//* This example reads a model from a file, solves it in batch mode * and prints the JSON solution string. */importcom.gurobi.gurobi.*;publicclassBatchmode{// Set-up a batch-mode environmentprivatestaticGRBEnvsetupbatchconnection()throwsGRBException{GRBEnvenv=newGRBEnv(true);env.set(GRB.IntParam.CSBatchMode,1);env.set(GRB.StringParam.LogFile,"batchmode.log");env.set(GRB.StringParam.CSManager,"http://localhost:61080");env.set(GRB.StringParam.UserName,"gurobi");env.set(GRB.StringParam.ServerPassword,"pass");env.start();returnenv;}// Display batch-error if anyprivatestaticvoidbatcherrorinfo(GRBBatchbatch)throwsGRBException{// Get last error codeinterror=batch.get(GRB.IntAttr.BatchErrorCode);if(error==0)return;// Query last error messageStringerrMsg=batch.get(GRB.StringAttr.BatchErrorMessage);// Query batchIDStringbatchID=batch.get(GRB.StringAttr.BatchID);System.out.println("Batch ID "+batchID+"Error Code "+error+"("+errMsg+")");}// Create a batch request from the given problem fileprivatestaticStringnewbatchrequest(Stringfilename)throwsGRBException{// Setup a batch connectionGRBEnvenv=setupbatchconnection();// Read a modelGRBModelmodel=newGRBModel(env,filename);// Set some parametersmodel.set(GRB.DoubleParam.MIPGap,0.01);model.set(GRB.IntParam.JSONSolDetail,1);// Set-up some tags, we need tags to be able to query resultsintcount=0;for(GRBVarv:model.getVars()){v.set(GRB.StringAttr.VTag,"UniqueVariableIdentifier"+count);count+=1;if(count>=10)break;}// Batch-mode optimizationStringbatchid=model.optimizeBatch();// no need to keep the model aroundmodel.dispose();// no need to keep environmentenv.dispose();returnbatchid;}// Wait for final statusprivatestaticvoidwaitforfinalstatus(Stringbatchid)throwsException{// Setup a batch connectionGRBEnvenv=setupbatchconnection();// Create Batch-objectGRBBatchbatch=newGRBBatch(env,batchid);try{// Query status, and wait for completedintstatus=batch.get(GRB.IntAttr.BatchStatus);longtimestart=System.currentTimeMillis();while(status==GRB.BatchStatus.SUBMITTED){// Abort if taking too longlongcurtime=System.currentTimeMillis();if(curtime-timestart>3600*1000){// Request to abort the batchbatch.abort();break;}// Do not bombard the serverThread.sleep(2000);// Update local attributesbatch.update();// Query current statusstatus=batch.get(GRB.IntAttr.BatchStatus);// Deal with failed statusif(status==GRB.BatchStatus.FAILED||status==GRB.BatchStatus.ABORTED){// Retry the batch jobbatch.retry();}}}catch(Exceptione){// Display batch-error if anybatcherrorinfo(batch);throwe;}finally{// Dispose resourcesbatch.dispose();env.dispose();}}// Final report on batch requestprivatestaticvoidfinalreport(Stringbatchid)throwsGRBException{// Setup a batch connectionGRBEnvenv=setupbatchconnection();// Create batch objectGRBBatchbatch=newGRBBatch(env,batchid);try{intstatus=batch.get(GRB.IntAttr.BatchStatus);// Display depending on batch statusswitch(status){caseGRB.BatchStatus.CREATED:System.out.println("Batch is 'CREATED'");System.out.println("maybe batch-creation process was killed?");break;caseGRB.BatchStatus.SUBMITTED:System.out.println("Batch is 'SUBMITTED'");System.out.println("Some other user re-submitted this Batch object?");break;caseGRB.BatchStatus.ABORTED:System.out.println("Batch is 'ABORTED'");break;caseGRB.BatchStatus.FAILED:System.out.println("Batch is 'FAILED'");break;caseGRB.BatchStatus.COMPLETED:// print JSON solution into stringSystem.out.println("JSON solution:"+batch.getJSONSolution());// save solution into a filebatch.writeJSONSolution("batch-sol.json.gz");break;default:System.out.println("This should not happen, probably points to a user-memory corruption problem");System.exit(1);break;}}catch(GRBExceptione){// Display batch-error if anybatcherrorinfo(batch);throwe;}finally{// Dispose resourcesbatch.dispose();env.dispose();}}// Discard batch data from the Cluster Managerprivatestaticvoiddiscardbatch(Stringbatchid)throwsGRBException{// Setup a batch connectionGRBEnvenv=setupbatchconnection();// Create batch objectGRBBatchbatch=newGRBBatch(env,batchid);try{// Request to erase input and output data related to this batchbatch.discard();}catch(GRBExceptione){// Display batch-error if anybatcherrorinfo(batch);throwe;}finally{// Dispose resourcesbatch.dispose();env.dispose();}}// Main public functionpublicstaticvoidmain(String[]args){// Ensure enough parametersif(args.length<1){System.out.println("Usage: java Batch filename");System.exit(1);}try{// Create a new batch requestStringbatchid=newbatchrequest(args[0]);// Wait for final statuswaitforfinalstatus(batchid);// Query final status, and if completed, print JSON solutionfinalreport(batchid);// once the user is done, discard all remote informationdiscardbatch(batchid);// Signal successSystem.out.println("OK");}catch(GRBExceptione){System.out.println("Error code: "+e.getErrorCode()+". "+e.getMessage());}catch(Exceptione){System.out.println("Error");}}}
Help and Feedback