mip2.m


函数mip2(文件名)% 2023年版权,Gurobi优化,LLC % %这个例子从狗万app足彩文件读取MIP模型,解决了它从所有可行的解决方案并打印%客观值%解决MIP时生成的。然后它创建相关的固定模型和%解决模型。%读取流模型(“% s \ n阅读模式”,文件名);模型= gurobi_read(文件名);关口=大小(模型。,2);实例变量=找到(模型。vtype ~ = ' C ');整数长度=(实例变量);如果整数< = 0流(“模型的所有变量是连续的,无关\ 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