multiobj.m
希望覆盖三个不同的集合,但要服从允许使用的%元素的公共预算。狗万app足彩然而,这些集合有不同的优先级要覆盖%;我们用多目标优化来解决这个问题。%定义原始数据groundSetSize = 20;nSubSets = 4;预算= 12;Set = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0;0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0;0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 0 SetObjPriority = [3; 2; 2; 1]; SetObjWeight = [1.0; 0.25; 1.25; 1.0]; % Initialize model model.modelsense = 'max'; model.modelname = 'multiobj'; % Set variables and constraints model.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'}; for j = 1:groundSetSize model.varnames{j} = sprintf('El%d', j); model.A(1, j) = 1; end % Set multi-objectives for m = 1:nSubSets model.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 model gurobi_write(model,'multiobj_m.lp') % Set parameters params.PoolSolutions = 100; % Optimize result = gurobi(model, params); % Capture solution information if ~strcmp(result.status, 'OPTIMAL') fprintf('Optimization finished with status %d, quit now\n', result.status); return; end % Print best solution fprintf('Selected elements in best solution:\n'); for j = 1:groundSetSize if result.x(j) >= 0.9 fprintf('%s ', model.varnames{j}); end end fprintf('\n'); % Print all solution objectives and best furth solution if isfield(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); for m = 1:nSubSets fprintf(' %s:', model.multiobj(m).name); for k = 1:solcount fprintf(' %3g', result.pool(k).objval(m)); end fprintf('\n'); end fprintf('\n'); else fprintf('Number of solutions found: 1\n'); fprintf('Solution 1 has objective values:'); for k = 1:nSubSets fprintf(' %g', result.objval(k)); end fprintf('\n'); end