Movatterモバイル変換


[0]ホーム

URL:


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

Multiobj Examples#

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

/* Copyright 2025, Gurobi Optimization, LLC *//* Want to cover four different sets but subject to a common budget of * elements allowed to be used. However, the sets have different priorities to * be covered; and we tackle this by using multi-objective optimization. */#include<assert.h>#include<stdlib.h>#include<stdio.h>#include<math.h>#include<string.h>#include"gurobi_c.h"#define MAXSTR 128intmain(void){GRBenv*env=NULL;GRBenv*menv=NULL;GRBmodel*model=NULL;interror=0;int*cind=NULL;double*cval=NULL;charbuffer[MAXSTR];inte,i,status,nSolutions;doubleobjn;/* Sample data */constintgroundSetSize=20;constintnSubsets=4;constintBudget=12;doubleSet[][20]={{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1},{0,0,0,1,1,0,1,1,0,0,0,0,0,1,1,0,1,1,0,0},{0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0}};intSetObjPriority[]={3,2,2,1};doubleSetObjWeight[]={1.0,0.25,1.25,1.0};/* Create environment */error=GRBloadenv(&env,"multiobj_c.log");if(error)gotoQUIT;/* Create initial model */error=GRBnewmodel(env,&model,"multiobj_c",groundSetSize,NULL,NULL,NULL,NULL,NULL);if(error)gotoQUIT;/* get model environment */menv=GRBgetenv(model);assert(menv!=NULL);/* Initialize decision variables for ground set:   * x[e] == 1 if element e is chosen for the covering. */for(e=0;e<groundSetSize;e++){sprintf(buffer,"El%d",e);error=GRBsetcharattrelement(model,"VType",e,GRB_BINARY);if(error)gotoQUIT;error=GRBsetstrattrelement(model,"VarName",e,buffer);if(error)gotoQUIT;}/* Make space for constraint data */cind=malloc(sizeof(int)*groundSetSize);if(!cind)gotoQUIT;cval=malloc(sizeof(double)*groundSetSize);if(!cval)gotoQUIT;/* Constraint: limit total number of elements to be picked to be at most   * Budget */for(e=0;e<groundSetSize;e++){cind[e]=e;cval[e]=1.0;}sprintf(buffer,"Budget");error=GRBaddconstr(model,groundSetSize,cind,cval,GRB_LESS_EQUAL,(double)Budget,buffer);if(error)gotoQUIT;/* Set global sense for ALL objectives */error=GRBsetintattr(model,GRB_INT_ATTR_MODELSENSE,GRB_MAXIMIZE);if(error)gotoQUIT;/* Limit how many solutions to collect */error=GRBsetintparam(menv,GRB_INT_PAR_POOLSOLUTIONS,100);if(error)gotoQUIT;/* Set and configure i-th objective */for(i=0;i<nSubsets;i++){sprintf(buffer,"Set%d",i+1);error=GRBsetobjectiven(model,i,SetObjPriority[i],SetObjWeight[i],1.0+i,0.01,buffer,0.0,groundSetSize,cind,Set[i]);if(error)gotoQUIT;}/* Save problem */error=GRBwrite(model,"multiobj_c.lp");if(error)gotoQUIT;error=GRBwrite(model,"multiobj_c.mps");if(error)gotoQUIT;/* Optimize */error=GRBoptimize(model);if(error)gotoQUIT;/* Status checking */error=GRBgetintattr(model,"Status",&status);if(error)gotoQUIT;if(status==GRB_INF_OR_UNBD||status==GRB_INFEASIBLE||status==GRB_UNBOUNDED){printf("The model cannot be solved ""because it is infeasible or unbounded\n");gotoQUIT;}if(status!=GRB_OPTIMAL){printf("Optimization was stopped with status %i\n",status);gotoQUIT;}/* Print best selected set */error=GRBgetdblattrarray(model,GRB_DBL_ATTR_X,0,groundSetSize,cval);if(error)gotoQUIT;printf("Selected elements in best solution:\n\t");for(e=0;e<groundSetSize;e++){if(cval[e]<.9)continue;printf("El%d ",e);}/* Print number of solutions stored */error=GRBgetintattr(model,GRB_INT_ATTR_SOLCOUNT,&nSolutions);if(error)gotoQUIT;printf("\nNumber of solutions found: %d\n",nSolutions);/* Print objective values of solutions */if(nSolutions>10)nSolutions=10;printf("Objective values for first %d solutions:\n",nSolutions);for(i=0;i<nSubsets;i++){error=GRBsetintparam(menv,GRB_INT_PAR_OBJNUMBER,i);if(error)gotoQUIT;printf("\tSet %d:",i);for(e=0;e<nSolutions;e++){error=GRBsetintparam(menv,GRB_INT_PAR_SOLUTIONNUMBER,e);if(error)gotoQUIT;error=GRBgetdblattr(model,GRB_DBL_ATTR_OBJNVAL,&objn);if(error)gotoQUIT;printf(" %6g",objn);}printf("\n");}QUIT:if(cind!=NULL)free(cind);if(cval!=NULL)free(cval);if(model!=NULL)GRBfreemodel(model);if(env!=NULL)GRBfreeenv(env);returnerror;}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp