Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Example Tour
Light LogoDark Logo
Gurobi
Back to top

Batchmode Examples#

This section includes source code for all of the Gurobi batch mode examples.The same source code can be found in theexamples directory of theGurobi distribution.

/* Copyright 2025, Gurobi Optimization, LLC *//* This example reads a MIP model from a file, solves it in batch mode, * and prints the JSON solution string. */#include<assert.h>#include<stdlib.h>#include<stdio.h>#include<time.h>#include<string.h>#if defined (WIN32) || defined (WIN64)#include<Windows.h>#define sleep(n) Sleep(1000*n)#else#include<unistd.h>#endif#include"gurobi_c.h"/* setup gurobi environment */intsetupbatchconnection(GRBenv**envP){interror=0;GRBenv*env=NULL;/* setup a batch environment */error=GRBemptyenv(envP);if(error)gotoQUIT;env=*envP;error=GRBsetintparam(env,"CSBatchMode",1);if(error)gotoQUIT;error=GRBsetstrparam(env,"LogFile","batchmode.log");if(error)gotoQUIT;error=GRBsetstrparam(env,"CSManager","http://localhost:61080");if(error)gotoQUIT;error=GRBsetstrparam(env,"UserName","gurobi");if(error)gotoQUIT;error=GRBsetstrparam(env,"ServerPassword","pass");if(error)gotoQUIT;error=GRBstartenv(env);if(error)gotoQUIT;QUIT:if(error){printf("Failed to setup environment, error code %d\n",error);}else{printf("Successfully created environment\n");}returnerror;}/* display batch-error code if any */voidbatcherrorinfo(GRBbatch*batch){interror=0;interrorCode;char*errorMsg;char*BatchID;if(!batch)gotoQUIT;/* query the last error code */error=GRBgetbatchintattr(batch,"BatchErrorCode",&errorCode);if(error||!errorCode)gotoQUIT;/* query the last error message */error=GRBgetbatchstrattr(batch,"BatchErrorMessage",&errorMsg);if(error)gotoQUIT;error=GRBgetbatchstrattr(batch,"BatchID",&BatchID);if(error)gotoQUIT;printf("Batch ID %s Error Code %d (%s)\n",BatchID,errorCode,errorMsg);QUIT:return;}/* create a batch request for given problem file */intnewbatchrequest(constchar*filename,char*BatchID){interror=0;GRBenv*env=NULL;GRBenv*menv=NULL;GRBmodel*model=NULL;chartag[128];intcols,j;/* setup a batch connection */error=setupbatchconnection(&env);if(error)gotoQUIT;/* read a model */error=GRBreadmodel(env,filename,&model);if(error)gotoQUIT;/* set some params */menv=GRBgetenv(model);assert(menv!=NULL);error=GRBsetdblparam(menv,"MIPGap",0.01);if(error)gotoQUIT;/* for extra detailed information on JSON solution string */error=GRBsetintparam(menv,"JSONSolDetail",1);if(error)gotoQUIT;/* setup some tags, we need tags to be able to query results later on */error=GRBgetintattr(model,"NumVars",&cols);if(error)gotoQUIT;if(cols>10)cols=10;for(j=0;j<cols;j++){sprintf(tag,"MyUniqueVariableID%d",j);error=GRBsetstrattrelement(model,"VTag",j,tag);}/* submit batch request to the Manager */error=GRBoptimizebatch(model,BatchID);if(error)gotoQUIT;QUIT:if(error){printf("Failed to submit a new batch request, error code %d\n",error);}else{printf("Successfully submitted new batch request %s\n",BatchID);}GRBfreemodel(model);GRBfreeenv(env);returnerror;}/* wait for final bstatus */intwaitforfinalstatus(constchar*BatchID){interror=0;GRBenv*env=NULL;GRBbatch*batch=NULL;time_tstart,current;intbstatus;/* setup a batch connection */error=setupbatchconnection(&env);if(error)gotoQUIT;/* create batch-object */error=GRBgetbatch(env,BatchID,&batch);if(error)gotoQUIT;/* query bstatus, and wait for completed */error=GRBgetbatchintattr(batch,"BatchStatus",&bstatus);if(error)gotoQUIT;start=time(NULL);while(bstatus==GRB_BATCH_SUBMITTED){/* abort if taking too long */current=time(NULL);if(current-start>=3600){/* request to abort the batch */error=GRBabortbatch(batch);if(error)gotoQUIT;}/* do not bombard the server */sleep(1u);/* update local attributes */error=GRBupdatebatch(batch);if(error)gotoQUIT;/* query bstatus */error=GRBgetbatchintattr(batch,"BatchStatus",&bstatus);if(error)gotoQUIT;/* deal with failed bstatus */if(bstatus==GRB_BATCH_FAILED){/* retry the batch request */error=GRBretrybatch(batch);if(error)gotoQUIT;bstatus=GRB_BATCH_SUBMITTED;}}QUIT:if(error){printf("Failed to wait for final bstatus, error code %d\n",error);}else{printf("Final Batch Status %d\n",bstatus);}batcherrorinfo(batch);/* release local resources */GRBfreebatch(batch);GRBfreeenv(env);returnerror;}/* final report on batch request */intfinalreport(constchar*BatchID){interror=0;GRBenv*env=NULL;GRBbatch*batch=NULL;char*jsonsol=NULL;intbstatus;/* setup a batch connection */error=setupbatchconnection(&env);if(error)gotoQUIT;/* create batch object */error=GRBgetbatch(env,BatchID,&batch);if(error)gotoQUIT;/* query bstatus, and wait for completed */error=GRBgetbatchintattr(batch,"BatchStatus",&bstatus);if(error)gotoQUIT;/* display depending on batch bstatus */switch(bstatus){caseGRB_BATCH_CREATED:printf("Batch is 'CREATED'\n");printf("maybe batch-creation process was killed?\n");break;caseGRB_BATCH_SUBMITTED:printf("Batch is 'SUBMITTED'\n");printf("Some other user re-submitted this Batch object?\n");break;caseGRB_BATCH_ABORTED:printf("Batch is 'ABORTED'\n");break;caseGRB_BATCH_FAILED:printf("Batch is 'FAILED'\n");break;caseGRB_BATCH_COMPLETED:/* print JSON solution into string */error=GRBgetbatchjsonsolution(batch,&jsonsol);if(error)gotoQUIT;printf("JSON solution: %s\n",jsonsol);/* save solution into a file */error=GRBwritebatchjsonsolution(batch,"batch-sol.json.gz");if(error)gotoQUIT;break;default:printf("This should not happen, probably points to a"" user-memory corruption problem\n");exit(EXIT_FAILURE);break;}QUIT:if(error){printf("Failed to perform final report, error code %d\n",error);}else{printf("Reporting done\n");}batcherrorinfo(batch);if(jsonsol)GRBfree(jsonsol);GRBfreebatch(batch);GRBfreeenv(env);returnerror;}/* remove batch ID from manager */intdiscardbatch(constchar*BatchID){interror=0;GRBenv*env=NULL;GRBbatch*batch=NULL;/* setup a batch connection */error=setupbatchconnection(&env);if(error)gotoQUIT;/* create batch object */error=GRBgetbatch(env,BatchID,&batch);if(error)gotoQUIT;/* discard the batch object in the manager */error=GRBdiscardbatch(batch);if(error)gotoQUIT;QUIT:batcherrorinfo(batch);GRBfreebatch(batch);GRBfreeenv(env);returnerror;}intmain(intargc,char**argv){interror=0;charBatchID[GRB_MAX_STRLEN+1];/* ensure enough parameters */if(argc<2){fprintf(stderr,"Usage: %s filename\n",argv[0]);gotoQUIT;}/* create a new batch request */error=newbatchrequest(argv[1],BatchID);if(error)gotoQUIT;/* wait for final bstatus */error=waitforfinalstatus(BatchID);if(error)gotoQUIT;/* query final bstatus, and if completed, print JSON solution */error=finalreport(BatchID);if(error)gotoQUIT;/* eliminate batch from the manager */error=discardbatch(BatchID);if(error)gotoQUIT;QUIT:returnerror;}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp