mip2.m


mip2.m


这个例子从一个文件中读取一个MIP模型,求解它,并在%求解MIP时从所有可行的解决方狗万app足彩案中打印%的客观值。然后,它创建相关的固定模型,%求解该模型。%读取模型fprintf('读取模型%s\n', filename);模型= gurobi_read(文件名);关口=大小(模型。, 2);实例变量=找到(模型。vtype ~ = ' C ');整数长度=(实例变量);if int <= 0 fprintf('模型中的所有变量都是连续的,什么都不做\n');返回;end %优化参数。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