mip2.m.


mip2.m.


函数MIP2(文件名)%版权所有2021,Gurobi优化,LLC %%此示例从文件狗万app足彩中读取MIP模型,解决了它并从求解MIP的%生成的所有可行解决方案中打印%。然后它创建了相关的固定模型和%解决该模型。%读取模型fprintf('读取模型%s \ n',文件名);model = gurobi_read(文件名);cols = size(model.a,2);ivars = find(model.vtype〜='c');ints =长度(ivars);如果INTS <= 0 FPRINTF('模型的所有变量是连续的,没什么可以做的\ n');返回;终端%优化参数.Poolsolutions = 20; result = gurobi(model, params); % Capture solution information if ~strcmp(result.status, 'OPTIMAL') fprintf('This model cannot be solved because its optimization status is %s\n', ... result.status); return; end % Iterate over the solutions if isfield(result, 'pool') && ~isempty(result.pool) solcount = length(result.pool); for k = 1:solcount fprintf('Solution %d has objective %g\n', k, result.pool(k).objval); end else fprintf('Solution 1 has objective %g\n', result.objval); end % Convert to fixed model for j = 1:cols if model.vtype(j) ~= 'C' t = floor(result.x(j) + 0.5); model.lb(j) = t; model.ub(j) = t; end end % Solve the fixed model result2 = gurobi(model, params); if ~strcmp(result.status, 'OPTIMAL') fprintf('Error: fixed model is not optimal\n'); return; end if abs(result.objval - result2.objval) > 1e-6 * (1 + abs(result.objval)) fprintf('Error: Objective values differ\n'); end % Print values of non-zero variables for j = 1:cols if abs(result2.x(j)) > 1e-6 fprintf('%s %g\n', model.varnames{j}, result2.x(j)); end end