functionmultiobj()% 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.% define primitive datagroundSetSize=20;nSubSets=4;Budget=12;Set=[11111111110000000000;00000111110000011111;00011011000001101100;00011100011100011100];SetObjPriority=[3;2;2;1];SetObjWeight=[1.0;0.25;1.25;1.0];% Initialize modelmodel.modelsense='max';model.modelname='multiobj';% Set variables and constraintsmodel.vtype=repmat('B',groundSetSize,1);model.lb=zeros(groundSetSize,1);model.ub=ones(groundSetSize,1);model.A=sparse(1,groundSetSize);model.rhs=Budget;model.sense='<';model.constrnames={'Budget'};forj=1:groundSetSizemodel.varnames{j}=sprintf('El%d',j);model.A(1,j)=1;end% Set multi-objectivesform=1:nSubSetsmodel.multiobj(m).objn=Set(m,:);model.multiobj(m).priority=SetObjPriority(m);model.multiobj(m).weight=SetObjWeight(m);model.multiobj(m).abstol=m;model.multiobj(m).reltol=0.01;model.multiobj(m).name=sprintf('Set%d',m);model.multiobj(m).con=0.0;end% Save modelgurobi_write(model,'multiobj_m.lp')% Set parametersparams.PoolSolutions=100;% Optimizeresult=gurobi(model,params);% Capture solution informationif~strcmp(result.status,'OPTIMAL')fprintf('Optimization finished with status %d, quit now\n',result.status);return;end% Print best solutionfprintf('Selected elements in best solution:\n');forj=1:groundSetSizeifresult.x(j)>=0.9fprintf('%s ',model.varnames{j});endendfprintf('\n');% Print all solution objectives and best furth solutionifisfield(result,'pool')&&~isempty(result.pool)solcount=length(result.pool);fprintf('Number of solutions found: %d\n',solcount);fprintf('Objective values for first %d solutions:\n',solcount);form=1:nSubSetsfprintf(' %s:',model.multiobj(m).name);fork=1:solcountfprintf(' %3g',result.pool(k).objval(m));endfprintf('\n');endfprintf('\n');elsefprintf('Number of solutions found: 1\n');fprintf('Solution 1 has objective values:');fork=1:nSubSetsfprintf(' %g',result.objval(k));endfprintf('\n');end
Help and Feedback