fixanddive.m


函数fixanddive(文件名)% % 2023年版权,Gurobi优化,LLC % %实现一个简狗万app足彩单的MIP启发式。放松模型,基于fractionality %变量排序,并修复的25% %的部分变量接近整数变量。%重复直到放松是%线性整数可行或不可行。%读取流模型(“% s \ n阅读模式”,文件名);模型= gurobi_read(文件名);关口=大小(模型。,2);实例变量=找到(模型。vtype ~ = ' C ');如果长度(实例变量)< = 0流(”模型的所有变量是连续的,无关\ n”);返回;%保存vtype和所有变量设置为连续vtype = model.vtype; model.vtype = repmat('C', cols, 1); params.OutputFlag = 0; result = gurobi(model, params); % Perform multiple iterations. In each iteration, identify the first % quartile of integer variables that are closest to an integer value % in the relaxation, fix them to the nearest integer, and repeat. frac = zeros(cols, 1); for iter = 1:1000 % See if status is optimal if ~strcmp(result.status, 'OPTIMAL') fprintf('Model status is %s\n', result.status); fprintf('Can not keep fixing variables\n'); break; end % collect fractionality of integer variables fracs = 0; for j = 1:cols if vtype(j) == 'C' frac(j) = 1; % indicating not integer variable else t = result.x(j); t = t - floor(t); if t > 0.5 t = t - 0.5; end if t > 1e-5 frac(j) = t; fracs = fracs + 1; else frac(j) = 1; % indicating not fractional end end end fprintf('Iteration %d, obj %g, fractional %d\n', iter, result.objval, fracs); if fracs == 0 fprintf('Found feasible solution - objective %g\n', result.objval); break; end % sort variables based on fractionality [~, I] = sort(frac); % fix the first quartile to the nearest integer value nfix = max(fracs/4, 1); for i = 1:nfix j = I(i); t = floor(result.x(j) + 0.5); model.lb(j) = t; model.ub(j) = t; end % use warm start basis and reoptimize model.vbasis = result.vbasis; model.cbasis = result.cbasis; result = gurobi(model, params); end