Workforce3.java


/ * 2023年版权,Gurobi优狗万app足彩化,LLC * / / *分配工人的变化;每个工人可能是也可能不是在某一天。如果问题不能解决,放松模型来确定哪些约束条件不能满足,以及他们需要放松。* /进口gurobi。*;公开课Workforce3{公共静态空main (String [] args){尝试{/ / / /示例数据集的日子和工人字符串转变新String [] [] = {“Mon1”、“Tue2”,“Wed3”、“Thu4”、“Fri5”、“Sat6”、“Sun7”、“Mon8”、“Tue9”、“Wed10”、“Thu11”、“Fri12”、“Sat13”、“Sun14”};字符串工人新String[][] ={“艾米”、“Bob”、“凯西”、“丹”,“Ed”,“弗雷德”,“顾”};int nShifts = Shifts.length;int nWorkers = Workers.length;/ /每个转变所需的工人数量双shiftRequirements新双[][]= {3、2、4、4、5、6、5、2、2、3、4、6、7,5};/ /每个工人数量支付双倍工资工作一个转变新双[][]= {10、12、10、8、8,9,11}; // Worker availability: 0 if the worker is unavailable for a shift double availability[][] = new double[][] { { 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1 }, { 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 }, { 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1 }, { 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; // Model GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); model.set(GRB.StringAttr.ModelName, "assignment"); // Assignment variables: x[w][s] == 1 if worker w is assigned // to shift s. Since an assignment model always produces integer // solutions, we use continuous variables and solve as an LP. GRBVar[][] x = new GRBVar[nWorkers][nShifts]; for (int w = 0; w < nWorkers; ++w) { for (int s = 0; s < nShifts; ++s) { x[w][s] = model.addVar(0, availability[w][s], pay[w], GRB.CONTINUOUS, Workers[w] + "." + Shifts[s]); } } // The objective is to minimize the total pay costs model.set(GRB.IntAttr.ModelSense, GRB.MINIMIZE); // Constraint: assign exactly shiftRequirements[s] workers // to each shift s for (int s = 0; s < nShifts; ++s) { GRBLinExpr lhs = new GRBLinExpr(); for (int w = 0; w < nWorkers; ++w) { lhs.addTerm(1.0, x[w][s]); } model.addConstr(lhs, GRB.EQUAL, shiftRequirements[s], Shifts[s]); } // Optimize model.optimize(); int status = model.get(GRB.IntAttr.Status); if (status == GRB.UNBOUNDED) { System.out.println("The model cannot be solved " + "because it is unbounded"); return; } if (status == GRB.OPTIMAL) { System.out.println("The optimal objective is " + model.get(GRB.DoubleAttr.ObjVal)); return; } if (status != GRB.INF_OR_UNBD && status != GRB.INFEASIBLE ) { System.out.println("Optimization was stopped with status " + status); return; } // Relax the constraints to make the model feasible System.out.println("The model is infeasible; relaxing the constraints"); int orignumvars = model.get(GRB.IntAttr.NumVars); model.feasRelax(0, false, false, true); model.optimize(); status = model.get(GRB.IntAttr.Status); if (status == GRB.INF_OR_UNBD || status == GRB.INFEASIBLE || status == GRB.UNBOUNDED ) { System.out.println("The relaxed model cannot be solved " + "because it is infeasible or unbounded"); return; } if (status != GRB.OPTIMAL) { System.out.println("Optimization was stopped with status " + status); return; } System.out.println("\nSlack values:"); GRBVar[] vars = model.getVars(); for (int i = orignumvars; i < model.get(GRB.IntAttr.NumVars); ++i) { GRBVar sv = vars[i]; if (sv.get(GRB.DoubleAttr.X) > 1e-6) { System.out.println(sv.get(GRB.StringAttr.VarName) + " = " + sv.get(GRB.DoubleAttr.X)); } } // Dispose of model and environment model.dispose(); env.dispose(); } catch (GRBException e) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } } }

尝试Gurobi免费

选择最适合你的评估许可,开始使用我们的专家团队提供技术指导和支持。

评估许可
得到一个免费的,功能齐全的许可证的Gurobi性能优化经验,支持,基准测试和调优服务我们提供我们产品的一部分。
学术许可
Gurobi支持教学和学术机构内使用的优化。我们提供免费的,功能齐全的Gurobi用于类的副本,并进行研究。
云试验

申请免费试用时间,所以你可以看到一个模型可以解决如何快速而方便地在云上。

<\/i>","library":"fa-solid"}}" data-widget_type="nav-menu.default">

Gurobi通讯