def waitforfinalstatus(batchID): # Wait no longer than one hour maxwaittime = 3600 # Setup and start environment, create local Batch handle object with setupbatchenv().start() as env, gp.Batch(batchID, env) as batch: starttime = time.time() while batch.BatchStatus == GRB.BATCH_SUBMITTED: # Abort this batch if it is taking too long curtime = time.time() if curtime - starttime > maxwaittime: batch.abort() break # Wait for two seconds time.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 it if batch.BatchStatus == GRB.BATCH_FAILED: batch.retry() # Print information about error status of the job that processed the batch printbatcherrorinfo(batch)
def printfinalreport(batchID): # Setup and start environment, create local Batch handle object with setupbatchenv().start() as env, gp.Batch(batchID, env) as batch: if batch.BatchStatus == GRB.BATCH_CREATED: print("Batch status is 'CREATED'") elif batch.BatchStatus == GRB.BATCH_SUBMITTED: print("Batch is 'SUBMITTED") elif batch.BatchStatus == GRB.BATCH_ABORTED: print("Batch is 'ABORTED'") elif batch.BatchStatus == GRB.BATCH_FAILED: print("Batch is 'FAILED'") elif batch.BatchStatus == GRB.BATCH_COMPLETED: print("Batch is 'COMPLETED'") print("JSON solution:") # Get JSON solution as string, create dict from it sol = json.loads(batch.getJSONSolution()) # Pretty printing the general solution information print(json.dumps(sol["SolutionInfo"], indent=4)) # Write the full JSON solution string to a file batch.writeJSONSolution('batch-sol.json.gz') else: # Should not happen print("Batch has unknown BatchStatus") printbatcherrorinfo(batch)
def batchdiscard(batchID): # Setup and start environment, create local Batch handle object with setupbatchenv().start() as env, gp.Batch(batchID, env) as batch: # Remove batch request from manager batch.discard()
workers = ", ".join(schedule[k]) print(" - {:10} {:>5}: {}".format(day, time, workers)) if __name__ == '__main__': # Create Cluster Manager environment in batch mode. env = gp.Env(empty=True) env.setParam('CSBatchMode', 1) # env is a context manager; upon leaving, Env.dispose() is called with env.start(): # Submit the assignment problem to the cluster manager, get batch ID batchID = submit_assigment_problem(env) # Create a batch object, wait for batch to complete, query solution JSON with gp.Batch(batchID, env) as batch: waitforfinalbatchstatus(batch) if batch.BatchStatus != GRB.BATCH_COMPLETED: print("Batch request couldn't be completed") sys.exit(0) jsonsol = batch.getJSONSolution() # Dump JSON solution string into a dict soldict = json.loads(jsonsol) # Has the assignment problem been solved as expected? if soldict['SolutionInfo']['Status'] != GRB.OPTIMAL: # Shouldn't happen... print("Assignment problem could not be solved to optimality")