/* Copyright 2025, Gurobi Optimization, LLC *//* Facility location: a company currently ships its product from 5 plants to 4 warehouses. It is considering closing some plants to reduce costs. What plant(s) should the company close, in order to minimize transportation and fixed costs? Since the plant fixed costs and the warehouse demands are uncertain, a scenario approach is chosen. Note that this example is similar to the facility_c.c example. Here we added scenarios in order to illustrate the multi-scenario feature. Based on an example from Frontline Systems: http://www.solver.com/disfacility.htm Used with permission. */#include<stdlib.h>#include<stdio.h>#include<math.h>#include"gurobi_c.h"#define opencol(p) p#define transportcol(w,p) nPlants*(w+1)+p#define demandconstr(w) nPlants+w#define MAXSTR 128intmain(intargc,char*argv[]){GRBenv*env=NULL;GRBenv*modelenv=NULL;GRBmodel*model=NULL;double*cval=NULL;double*rhs=NULL;int*cbeg=NULL;int*cind=NULL;char**cname=NULL;char*sense=NULL;doublemaxFixed=-GRB_INFINITY;doubleminFixed=GRB_INFINITY;intcnamect=0;interror=0;intp,s,w,col;intidx,rowct;intnScenarios;charvname[MAXSTR];/* Number of plants, warehouses and scenarios */constintnPlants=5;constintnWarehouses=4;/* Warehouse demand in thousands of units */doubleDemand[]={15,18,14,20};/* Plant capacity in thousands of units */doubleCapacity[]={20,22,17,19,18};/* Fixed costs for each plant */doubleFixedCosts[]={12000,15000,17000,13000,16000};/* Transportation costs per thousand units */doubleTransCosts[4][5]={{4000,2000,3000,2500,4500},{2500,2600,3400,3000,4000},{1200,1800,2600,4100,3000},{2200,2600,3100,3700,3200}};/* Compute minimal and maximal fixed cost */for(p=0;p<nPlants;p++){if(FixedCosts[p]>maxFixed)maxFixed=FixedCosts[p];if(FixedCosts[p]<minFixed)minFixed=FixedCosts[p];}/* Create environment */error=GRBloadenv(&env,"multiscenario.log");if(error)gotoQUIT;/* Create initial model */error=GRBnewmodel(env,&model,"multiscenario",nPlants*(nWarehouses+1),NULL,NULL,NULL,NULL,NULL);if(error)gotoQUIT;modelenv=GRBgetenv(model);/* Initialize decision variables for plant open variables */for(p=0;p<nPlants;p++){col=opencol(p);error=GRBsetcharattrelement(model,GRB_CHAR_ATTR_VTYPE,col,GRB_BINARY);if(error)gotoQUIT;error=GRBsetdblattrelement(model,GRB_DBL_ATTR_OBJ,col,FixedCosts[p]);if(error)gotoQUIT;sprintf(vname,"Open%i",p);error=GRBsetstrattrelement(model,GRB_STR_ATTR_VARNAME,col,vname);if(error)gotoQUIT;}/* Initialize decision variables for transportation decision variables: how much to transport from a plant p to a warehouse w */for(w=0;w<nWarehouses;w++){for(p=0;p<nPlants;p++){col=transportcol(w,p);error=GRBsetdblattrelement(model,GRB_DBL_ATTR_OBJ,col,TransCosts[w][p]);if(error)gotoQUIT;sprintf(vname,"Trans%i.%i",p,w);error=GRBsetstrattrelement(model,GRB_STR_ATTR_VARNAME,col,vname);if(error)gotoQUIT;}}/* The objective is to minimize the total fixed and variable costs */error=GRBsetintattr(model,GRB_INT_ATTR_MODELSENSE,GRB_MINIMIZE);if(error)gotoQUIT;/* Make space for constraint data */rowct=(nPlants>nWarehouses)?nPlants:nWarehouses;cbeg=malloc(sizeof(int)*rowct);if(!cbeg)gotoQUIT;cind=malloc(sizeof(int)*(nPlants*(nWarehouses+1)));if(!cind)gotoQUIT;cval=malloc(sizeof(double)*(nPlants*(nWarehouses+1)));if(!cval)gotoQUIT;rhs=malloc(sizeof(double)*rowct);if(!rhs)gotoQUIT;sense=malloc(sizeof(char)*rowct);if(!sense)gotoQUIT;cname=calloc(rowct,sizeof(char*));if(!cname)gotoQUIT;/* Production constraints Note that the limit sets the production to zero if the plant is closed */idx=0;for(p=0;p<nPlants;p++){cbeg[p]=idx;rhs[p]=0.0;sense[p]=GRB_LESS_EQUAL;cname[p]=malloc(sizeof(char)*MAXSTR);if(!cname[p])gotoQUIT;cnamect++;sprintf(cname[p],"Capacity%i",p);for(w=0;w<nWarehouses;w++){cind[idx]=transportcol(w,p);cval[idx++]=1.0;}cind[idx]=opencol(p);cval[idx++]=-Capacity[p];}error=GRBaddconstrs(model,nPlants,idx,cbeg,cind,cval,sense,rhs,cname);if(error)gotoQUIT;/* Demand constraints */idx=0;for(w=0;w<nWarehouses;w++){cbeg[w]=idx;sense[w]=GRB_EQUAL;sprintf(cname[w],"Demand%i",w);for(p=0;p<nPlants;p++){cind[idx]=transportcol(w,p);cval[idx++]=1.0;}}error=GRBaddconstrs(model,nWarehouses,idx,cbeg,cind,cval,sense,Demand,cname);if(error)gotoQUIT;/* We constructed the base model, now we add 7 scenarios Scenario 0: Represents the base model, hence, no manipulations. Scenario 1: Manipulate the warehouses demands slightly (constraint right hand sides). Scenario 2: Double the warehouses demands (constraint right hand sides). Scenario 3: Manipulate the plant fixed costs (objective coefficients). Scenario 4: Manipulate the warehouses demands and fixed costs. Scenario 5: Force the plant with the largest fixed cost to stay open (variable bounds). Scenario 6: Force the plant with the smallest fixed cost to be closed (variable bounds). */error=GRBsetintattr(model,GRB_INT_ATTR_NUMSCENARIOS,7);if(error)gotoQUIT;/* Scenario 0: Base model, hence, nothing to do except giving the scenario a name */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,0);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Base model");if(error)gotoQUIT;/* Scenario 1: Increase the warehouse demands by 10% */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,1);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Increased warehouse demands");if(error)gotoQUIT;for(w=0;w<nWarehouses;w++){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNRHS,demandconstr(w),Demand[w]*1.1);if(error)gotoQUIT;}/* Scenario 2: Double the warehouse demands */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,2);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Double the warehouse demands");if(error)gotoQUIT;for(w=0;w<nWarehouses;w++){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNRHS,demandconstr(w),Demand[w]*2.0);if(error)gotoQUIT;}/* Scenario 3: Decrease the plant fixed costs by 5% */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,3);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Decreased plant fixed costs");if(error)gotoQUIT;for(p=0;p<nPlants;p++){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNOBJ,opencol(p),FixedCosts[p]*0.95);if(error)gotoQUIT;}/* Scenario 4: Combine scenario 1 and scenario 3 */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,4);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Increased warehouse demands and decreased plant fixed costs");for(w=0;w<nWarehouses;w++){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNRHS,demandconstr(w),Demand[w]*1.1);if(error)gotoQUIT;}for(p=0;p<nPlants;p++){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNOBJ,opencol(p),FixedCosts[p]*0.95);if(error)gotoQUIT;}/* Scenario 5: Force the plant with the largest fixed cost to stay open */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,5);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Force plant with largest fixed cost to stay open");if(error)gotoQUIT;for(p=0;p<nPlants;p++){if(FixedCosts[p]==maxFixed){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNLB,opencol(p),1.0);if(error)gotoQUIT;break;}}/* Scenario 6: Force the plant with the smallest fixed cost to be closed */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,6);if(error)gotoQUIT;error=GRBsetstrattr(model,GRB_STR_ATTR_SCENNNAME,"Force plant with smallest fixed cost to be closed");if(error)gotoQUIT;for(p=0;p<nPlants;p++){if(FixedCosts[p]==minFixed){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_SCENNUB,opencol(p),0.0);if(error)gotoQUIT;break;}}/* Guess at the starting point: close the plant with the highest fixed costs; open all others *//* First, open all plants */for(p=0;p<nPlants;p++){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_START,opencol(p),1.0);if(error)gotoQUIT;}/* Now close the plant with the highest fixed cost */printf("Initial guess:\n");for(p=0;p<nPlants;p++){if(FixedCosts[p]==maxFixed){error=GRBsetdblattrelement(model,GRB_DBL_ATTR_START,opencol(p),0.0);if(error)gotoQUIT;printf("Closing plant %i\n\n",p);break;}}/* Use barrier to solve root relaxation */error=GRBsetintparam(modelenv,GRB_INT_PAR_METHOD,GRB_METHOD_BARRIER);if(error)gotoQUIT;/* Solve multi-scenario model */error=GRBoptimize(model);if(error)gotoQUIT;error=GRBgetintattr(model,GRB_INT_ATTR_NUMSCENARIOS,&nScenarios);if(error)gotoQUIT;/* Print solution for each */for(s=0;s<nScenarios;s++){char*scenarioName;doublescenNObjBound;doublescenNObjVal;intmodelSense=GRB_MINIMIZE;/* Set the scenario number to query the information for this scenario */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,s);if(error)gotoQUIT;/* Collect result for the scenario */error=GRBgetstrattr(model,GRB_STR_ATTR_SCENNNAME,&scenarioName);if(error)gotoQUIT;error=GRBgetdblattr(model,GRB_DBL_ATTR_SCENNOBJBOUND,&scenNObjBound);if(error)gotoQUIT;error=GRBgetdblattr(model,GRB_DBL_ATTR_SCENNOBJVAL,&scenNObjVal);if(error)gotoQUIT;printf("\n\n------ Scenario %d (%s)\n",s,scenarioName);/* Check if we found a feasible solution for this scenario */if(modelSense*scenNObjVal>=GRB_INFINITY)if(modelSense*scenNObjBound>=GRB_INFINITY)/* Scenario was proven to be infeasible */printf("\nINFEASIBLE\n");else/* We did not find any feasible solution - should not happen in this case, because we did not set any limit (like a time limit) on the optimization process */printf("\nNO SOLUTION\n");else{printf("\nTOTAL COSTS: %g\n",scenNObjVal);printf("SOLUTION:\n");for(p=0;p<nPlants;p++){doublescenNX;error=GRBgetdblattrelement(model,GRB_DBL_ATTR_SCENNX,opencol(p),&scenNX);if(error)gotoQUIT;if(scenNX>0.5){printf("Plant %i open\n",p);for(w=0;w<nWarehouses;w++){error=GRBgetdblattrelement(model,GRB_DBL_ATTR_SCENNX,transportcol(w,p),&scenNX);if(error)gotoQUIT;if(scenNX>0.0001)printf(" Transport %g units to warehouse %i\n",scenNX,w);}}elseprintf("Plant %i closed!\n",p);}}}/* Print a summary table: for each scenario we add a single summary line */printf("\n\nSummary: Closed plants depending on scenario\n\n");printf("%8s | %17s %13s\n","","Plant","|");printf("%8s |","Scenario");for(p=0;p<nPlants;p++)printf(" %5d",p);printf(" | %6s %s\n","Costs","Name");for(s=0;s<nScenarios;s++){char*scenarioName;doublescenNObjBound;doublescenNObjVal;intmodelSense=GRB_MINIMIZE;/* Set the scenario number to query the information for this scenario */error=GRBsetintparam(modelenv,GRB_INT_PAR_SCENARIONUMBER,s);if(error)gotoQUIT;/* collect result for the scenario */error=GRBgetstrattr(model,GRB_STR_ATTR_SCENNNAME,&scenarioName);if(error)gotoQUIT;error=GRBgetdblattr(model,GRB_DBL_ATTR_SCENNOBJBOUND,&scenNObjBound);if(error)gotoQUIT;error=GRBgetdblattr(model,GRB_DBL_ATTR_SCENNOBJVAL,&scenNObjVal);if(error)gotoQUIT;printf("%-8d |",s);/* Check if we found a feasible solution for this scenario */if(modelSense*scenNObjVal>=GRB_INFINITY)if(modelSense*scenNObjBound>=GRB_INFINITY)/* Scenario was proven to be infeasible */printf(" %-30s| %6s %s\n","infeasible","-",scenarioName);else/* We did not find any feasible solution - should not happen in this case, because we did not set any limit (like a time limit) on the optimization process */printf(" %-30s| %6s %s\n","no solution found","-",scenarioName);else{for(p=0;p<nPlants;p++){doublescenNX;error=GRBgetdblattrelement(model,GRB_DBL_ATTR_SCENNX,opencol(p),&scenNX);if(scenNX>0.5)printf(" %5s"," ");elseprintf(" %5s","x");}printf(" | %6g %s\n",scenNObjVal,scenarioName);}}QUIT:/* Error reporting */if(error){printf("ERROR: %s\n",GRBgeterrormsg(env));exit(1);}/* Free data */free(cbeg);free(cind);free(cval);free(rhs);free(sense);for(p=0;p<cnamect;p++)free(cname[p]);free(cname);/* Free model */GRBfreemodel(model);/* Free environment */GRBfreeenv(env);return0;}