manbet体育手机客户端


BatchMode_c ++。CPP


/ *版权所有2019,Gurobi优狗万app足彩化,LLC * / //此示例从文件中读取MIP模型,以批处理模式,//打印JSON解决方案字符串。// //您需要一个群集管理器许可证的此示例来工作。#include  #f定义(win32)||定义(win64)#include  #define睡眠(n)睡眠(1000 * n)#else #include  #endif #include“gurobi_c ++。h”使用namespace std;//设置批处理模式优化的环境。// //该函数创建一个空的环境,设置所有必需的//参数,并将现成的envegy返回给呼叫者。//当//时,呼叫者有责任在//不再需要时处理这种环境。grbenv * setupbatchenv(void){grbenv * env = new grbenv(true);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->set(GRB_IntParam_CSBatchMode, 1); // No network communication happened up to this point. This will happen // once the caller invokes the start() method of the returned Env // object. return env; } // Print batch job error information, if any void printbatcherrorinfo(GRBBatch& batch) { if (batch.get(GRB_IntAttr_BatchErrorCode) == 0) return; cerr << "Batch ID " << batch.get(GRB_StringAttr_BatchID) << ": Error code " << batch.get(GRB_IntAttr_BatchErrorCode) << " (" << batch.get(GRB_StringAttr_BatchErrorMessage) << ")" << endl; } // Create a batch request for given problem file string newbatchrequest(char* filename) { // Start environment, create Model object from file GRBEnv* env = setupbatchenv(); env->start(); GRBModel* model = new GRBModel(*env, filename); // Set some parameters; switch on detailed JSON information model->set(GRB_DoubleParam_MIPGap, 0.01); model->set(GRB_IntParam_JSONSolDetail, 1); // Define tags for some variables in order to access their values later int numvars = model->get(GRB_IntAttr_NumVars); GRBVar* v = model->getVars(); if (numvars > 10) numvars = 10; for (int j = 0; j < numvars; j++) { char vtag[64]; sprintf(vtag, "Variable %d", j); v[j].set(GRB_StringAttr_VTag, string(vtag)); } // submit batch request string batchID = model->optimizeBatch(); // Free local resources delete[] v; delete model; delete env; 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). void waitforfinalstatus(string batchID) { // Wait no longer than one hour time_t maxwaittime = 3600; // Setup and start environment, create local Batch handle object GRBEnv* env = setupbatchenv(); env->start(); GRBBatch* batch = new GRBBatch(*env, batchID); try { time_t starttime = time(NULL); int BatchStatus = batch->get(GRB_IntAttr_BatchStatus); while (BatchStatus == GRB_BATCH_SUBMITTED) { // Abort this batch if it is taking too long time_t curtime = time(NULL); if (curtime - starttime > maxwaittime) { batch->abort(); break; } // Wait for two seconds sleep(2); // Update the resident attribute cache of the Batch object with the // latest values from the cluster manager. batch->update(); BatchStatus = batch->get(GRB_IntAttr_BatchStatus); // If the batch failed, we retry it if (BatchStatus == GRB_BATCH_FAILED) batch->retry(); } } catch (...) { // Print information about error status of the job that // processed the batch printbatcherrorinfo(*batch); throw; } delete batch; delete env; } void printfinalreport(string batchID) { // Setup and start environment, create local Batch handle object GRBEnv* env = setupbatchenv(); env->start(); GRBBatch* batch = new GRBBatch(*env, batchID); int BatchStatus = batch->get(GRB_IntAttr_BatchStatus); if (BatchStatus == GRB_BATCH_CREATED) cout << "Batch status is 'CREATED'" << endl; else if (BatchStatus == GRB_BATCH_SUBMITTED) cout << "Batch is 'SUBMITTED" << endl; else if (BatchStatus == GRB_BATCH_ABORTED) cout << "Batch is 'ABORTED'" << endl; else if (BatchStatus == GRB_BATCH_FAILED) cout << "Batch is 'FAILED'" << endl; else if (BatchStatus == GRB_BATCH_COMPLETED) { cout << "Batch is 'COMPLETED'" << endl; // Pretty printing the general solution information cout << "JSON solution:" << batch->getJSONSolution() << endl; // Write the full JSON solution string to a file batch->writeJSONSolution("batch-sol.json.gz"); } else { // Should not happen cout << "Batch has unknown BatchStatus" << endl; } delete batch; delete env; } // Instruct cluster manager to discard all data relating to this BatchID void batchdiscard(string batchID) { // Setup and start environment, create local Batch handle object GRBEnv* env = setupbatchenv(); env->start(); GRBBatch* batch = new GRBBatch(*env, batchID); // Remove batch request from manager batch->discard(); delete batch; delete env; } // Solve a given model using batch optimization int main(int argc, char** argv) { // Ensure we have an input file if (argc != 2) { cout << "Usage: " << argv[0] << " filename" << endl; return 0; } // Submit new batch request string batchID = newbatchrequest(argv[1]); // Wait for final status waitforfinalstatus(batchID); // Report final status info printfinalreport(batchID); // Remove batch request from manager batchdiscard(batchID); cout << "Batch optimization OK" << endl; return 0; }