facility.m


设施()函数% 2023年版权,Gurobi优化,LLC % %设施地点:公狗万app足彩司目前船舶产品从5植物% 4仓库。狗万滚球球它正在考虑关闭一些工厂降低%成本。什么植物(s)公司应关闭,为了减少%运输和固定成本狗万滚球球?% %注意到,这个示例使用列表而不是字典。自%不处理稀疏数据,列表是一个合理的选择。% %从前线系统基于一个例子:http://www.solver.com/disfacility.htm % %使用许可。手机万博登录%定义原始数据nPlants = 5;nWarehouses = 4;%的仓库需求在成千上万的单位需求= [15;18; 14; 20]; % Plant capacity in thousands of units Capacity = [20; 22; 17; 19; 18]; % Fixed costs for each plant FixedCosts = [12000; 15000; 17000; 13000; 16000]; % Transportation costs per thousand units TransCosts = [ 4000; 2000; 3000; 2500; 4500; 2500; 2600; 3400; 3000; 4000; 1200; 1800; 2600; 4100; 3000; 2200; 2600; 3100; 3700; 3200]; % Index helper function flowidx = @(w, p) nPlants * w + p; % Build model model.modelname = 'facility'; model.modelsense = 'min'; % Set data for variables ncol = nPlants + nPlants * nWarehouses; model.lb = zeros(ncol, 1); model.ub = [ones(nPlants, 1); inf(nPlants * nWarehouses, 1)]; model.obj = [FixedCosts; TransCosts]; model.vtype = [repmat('B', nPlants, 1); repmat('C', nPlants * nWarehouses, 1)]; for p = 1:nPlants model.varnames{p} = sprintf('Open%d', p); end for w = 1:nWarehouses for p = 1:nPlants v = flowidx(w, p); model.varnames{v} = sprintf('Trans%d,%d', w, p); end end % Set data for constraints and matrix nrow = nPlants + nWarehouses; model.A = sparse(nrow, ncol); model.rhs = [zeros(nPlants, 1); Demand]; model.sense = [repmat('<', nPlants, 1); repmat('=', nWarehouses, 1)]; % Production constraints for p = 1:nPlants for w = 1:nWarehouses model.A(p, p) = -Capacity(p); model.A(p, flowidx(w, p)) = 1.0; end model.constrnames{p} = sprintf('Capacity%d', p); end % Demand constraints for w = 1:nWarehouses for p = 1:nPlants model.A(nPlants+w, flowidx(w, p)) = 1.0; end model.constrnames{nPlants+w} = sprintf('Demand%d', w); end % Save model gurobi_write(model,'facility_m.lp'); % Guess at the starting point: close the plant with the highest fixed % costs; open all others first open all plants model.start = [ones(nPlants, 1); inf(nPlants * nWarehouses, 1)]; [~, idx] = max(FixedCosts); model.start(idx) = 0; % Set parameters params.method = 2; % Optimize res = gurobi(model, params); % Print solution if strcmp(res.status, 'OPTIMAL') fprintf('\nTotal Costs: %g\n', res.objval); fprintf('solution:\n'); for p = 1:nPlants if res.x(p) > 0.99 fprintf('Plant %d open:\n', p); end for w = 1:nWarehouses if res.x(flowidx(w, p)) > 0.0001 fprintf(' Transport %g units to warehouse %d\n', res.x(flowidx(w, p)), w); end end end else fprintf('\n No solution\n'); end end