一个完整的例子

一个完整的例子


#!/ usr / bin / env python3.7#版权所有2021,guro狗万app足彩bi优化,llc#此示例从文件中读取MIP模型,以批处理模式,#并打印JSON解决方案字符串。##您将需要此示例的群集管理器许可证。导入系统导入时间导入JSON导入GUROBIP作为GUROBIPY导入GUROM导入GUR#设置批处理模式优化的环境。##函数创建空环境,设置所有必要的参数,#并将现成的envegy返回给呼叫者。当它没有禁止时,它是#来电者的责任处理这个环境。def setupbatchenv():env = gp.env(空= true)env.setParam('logfile','batchmode.log')env.setParam('CsManager','http:// localhost:61080')env.setParam('用户名','gurobi')env.setParam('serverpassword','pass')env.setParam('csbatchMode',1)#没有网络通信达到这一点。一旦调用者调用返回的env对象的start()方法,就会发生#。返回env#打印批处理作业错误信息,如果有任何def printbatcherrorinfo(批量):如果批次为none或batch.batcherrorcode == 0:返回打印(“批处理ID {}:错误代码{}({})”。格式(batch.batchid,batch.batcherrcode,batch.batcherrormessage))#创建给定问题文件def newbatchrequest(文件名):#start环境,通过使用env和model的上下文处理程序创建模型对象,从文件##创建模型对象确保使用setupbatchenv()自动调用#model.dispose()和envispose()。start()作为env,gp.read(filename,env = env)作为模型:#设置一些参数model.params。MIPGap = 0.01 model.Params.JSONSolDetail = 1 # Define tags for some variables in order to access their values later for count, v in enumerate(model.getVars()): v.VTag = "Variable{}".format(count) if count >= 10: break # Submit batch request batchID = model.optimizeBatch() return batchID # Wait for the final status of the batch. # Initially the status of a batch is "submitted"; the status will change # once the batch has been processed (by a compute server). 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) # Instruct the cluster manager to discard all data relating to this BatchID 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() # Solve a given model using batch optimization if __name__ == '__main__': # Ensure we have an input file if len(sys.argv) < 2: print("Usage: {} filename".format(sys.argv[0])) sys.exit(0) # Submit new batch request batchID = newbatchrequest(sys.argv[1]) # Wait for final status waitforfinalstatus(batchID) # Report final status info printfinalreport(batchID) # Remove batch request from manager batchdiscard(batchID) print('Batch optimization OK')