/* 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. */usingSystem;usingGurobi;classmultiobj_cs{staticvoidMain(){try{// Sample dataintgroundSetSize=20;intnSubsets=4;intBudget=12;double[,]Set=newdouble[,]{{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}};int[]SetObjPriority=newint[]{3,2,2,1};double[]SetObjWeight=newdouble[]{1.0,0.25,1.25,1.0};inte,i,status,nSolutions;// Create environmentGRBEnvenv=newGRBEnv("multiobj_cs.log");// Create initial modelGRBModelmodel=newGRBModel(env);model.ModelName="multiobj_cs";// Initialize decision variables for ground set:// x[e] == 1 if element e is chosen for the covering.GRBVar[]Elem=model.AddVars(groundSetSize,GRB.BINARY);for(e=0;e<groundSetSize;e++){stringvname=string.Format("El{0}",e);Elem[e].VarName=vname;}// Constraint: limit total number of elements to be picked to be at most// BudgetGRBLinExprlhs=newGRBLinExpr();for(e=0;e<groundSetSize;e++){lhs.AddTerm(1.0,Elem[e]);}model.AddConstr(lhs,GRB.LESS_EQUAL,Budget,"Budget");// Set global sense for ALL objectivesmodel.ModelSense=GRB.MAXIMIZE;// Limit how many solutions to collectmodel.Parameters.PoolSolutions=100;// Set and configure i-th objectivefor(i=0;i<nSubsets;i++){stringvname=string.Format("Set{0}",i);GRBLinExprobjn=newGRBLinExpr();for(e=0;e<groundSetSize;e++){objn.AddTerm(Set[i,e],Elem[e]);}model.SetObjectiveN(objn,i,SetObjPriority[i],SetObjWeight[i],1.0+i,0.01,vname);}// Save problemmodel.Write("multiobj_cs.lp");// Optimizemodel.Optimize();// Status checkingstatus=model.Status;if(status==GRB.Status.INF_OR_UNBD||status==GRB.Status.INFEASIBLE||status==GRB.Status.UNBOUNDED){Console.WriteLine("The model cannot be solved "+"because it is infeasible or unbounded");return;}if(status!=GRB.Status.OPTIMAL){Console.WriteLine("Optimization was stopped with status {0}",status);return;}// Print best selected setConsole.WriteLine("Selected elements in best solution:");Console.Write("\t");for(e=0;e<groundSetSize;e++){if(Elem[e].X<.9)continue;Console.Write("El{0} ",e);}Console.WriteLine();// Print number of solutions storednSolutions=model.SolCount;Console.WriteLine("Number of solutions found: {0}",nSolutions);// Print objective values of solutionsif(nSolutions>10)nSolutions=10;Console.WriteLine("Objective values for first {0} solutions:",nSolutions);for(i=0;i<nSubsets;i++){model.Parameters.ObjNumber=i;Console.Write("\tSet"+i);for(e=0;e<nSolutions;e++){model.Parameters.SolutionNumber=e;Console.Write("{0,8}",model.ObjNVal);}Console.WriteLine();}model.Dispose();env.Dispose();}catch(GRBExceptione){Console.WriteLine("Error code = {0}",e);Console.WriteLine(e.Message);}}}