Movatterモバイル変換


[0]ホーム

URL:


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

Mip2 Examples#

This section includes source code for all of the Gurobi mip2 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 and   prints the objective values from all feasible solutions   generated while solving the MIP. Then it creates the fixed   model and solves that model. */#include<assert.h>#include<stdlib.h>#include<stdio.h>#include<math.h>#include"gurobi_c.h"intmain(intargc,char*argv[]){GRBenv*env=NULL;GRBmodel*model=NULL;GRBmodel*fixed=NULL;interror=0;intismip;intj,k,solcount,numvars;doubleobjn;intoptimstatus,foptimstatus;doubleobjval,fobjval;char*varname;doublex;/* To change settings for a loaded model, we need to get     the model environment, which will be freed when the model     is freed. */GRBenv*menv,*fenv;if(argc<2){fprintf(stderr,"Usage: mip2_c filename\n");exit(1);}/* Create environment */error=GRBloadenv(&env,"mip2.log");if(error)gotoQUIT;/* Read model from file */error=GRBreadmodel(env,argv[1],&model);if(error)gotoQUIT;error=GRBgetintattr(model,"IsMIP",&ismip);if(error)gotoQUIT;if(ismip==0){printf("Model is not a MIP\n");gotoQUIT;}/* Get model environment */menv=GRBgetenv(model);assert(menv!=NULL);/* Solve model */error=GRBoptimize(model);if(error)gotoQUIT;/* Capture solution information */error=GRBgetintattr(model,GRB_INT_ATTR_STATUS,&optimstatus);if(error)gotoQUIT;printf("\nOptimization complete\n");if(optimstatus==GRB_OPTIMAL){error=GRBgetdblattr(model,GRB_DBL_ATTR_OBJVAL,&objval);if(error)gotoQUIT;printf("Optimal objective: %.4e\n\n",objval);}elseif(optimstatus==GRB_INF_OR_UNBD){printf("Model is infeasible or unbounded\n\n");gotoQUIT;}elseif(optimstatus==GRB_INFEASIBLE){printf("Model is infeasible\n\n");gotoQUIT;}elseif(optimstatus==GRB_UNBOUNDED){printf("Model is unbounded\n\n");gotoQUIT;}else{printf("Optimization was stopped with status = %d\n\n",optimstatus);gotoQUIT;}/* Iterate over the solutions and compute the objectives */error=GRBgetintattr(model,"SolCount",&solcount);if(error)gotoQUIT;printf("\n");for(k=0;k<solcount;++k){error=GRBsetintparam(menv,"SolutionNumber",k);if(error)gotoQUIT;error=GRBgetdblattr(model,GRB_DBL_ATTR_POOLNOBJVAL,&objn);if(error)gotoQUIT;printf("Solution %i has objective: %f\n",k,objn);}printf("\n");/* Create a fixed model, turn off presolve and solve */error=GRBfixmodel(model,&fixed);if(error||!fixed){fprintf(stderr,"Error: could not create fixed model\n");gotoQUIT;}fenv=GRBgetenv(fixed);assert(fenv!=NULL);error=GRBsetintparam(fenv,"PRESOLVE",0);if(error)gotoQUIT;error=GRBoptimize(fixed);if(error)gotoQUIT;error=GRBgetintattr(fixed,GRB_INT_ATTR_STATUS,&foptimstatus);if(error)gotoQUIT;if(foptimstatus!=GRB_OPTIMAL){fprintf(stderr,"Error: fixed model isn't optimal\n");gotoQUIT;}error=GRBgetdblattr(fixed,GRB_DBL_ATTR_OBJVAL,&fobjval);if(error)gotoQUIT;if(fabs(fobjval-objval)>1.0e-6*(1.0+fabs(objval))){fprintf(stderr,"Error: objective values are different\n");}error=GRBgetintattr(model,"NumVars",&numvars);if(error)gotoQUIT;/* Print values of nonzero variables */for(j=0;j<numvars;++j){error=GRBgetstrattrelement(fixed,"VarName",j,&varname);if(error)gotoQUIT;error=GRBgetdblattrelement(fixed,"X",j,&x);if(error)gotoQUIT;if(x!=0.0){printf("%s %f\n",varname,x);}}QUIT:/* Error reporting */if(error){printf("ERROR: %s\n",GRBgeterrormsg(env));exit(1);}/* Free models */GRBfreemodel(model);GRBfreemodel(fixed);/* Free environment */GRBfreeenv(env);return0;}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp