Workforce2.java


/*版权2023,Gurobi Opt狗万app足彩imization, LLC */ /*安排工人轮班;每个员工在特定的一天可能有工作,也可能没有。如果问题无法解决,则使用IIS迭代查找所有冲突约束。*/进口gurobi.*;进口java.util。*;public class Workforce2 {public static void main(String[] args) {try{//样本数据// days和workers的集合String Shifts[] = new String[] {"Mon1", "Tue2", "Wed3", "Thu4", "Fri5", "Sat6", "Sun7", "Mon8", "Tue9", "Wed10", "Thu11", "Fri12", "Sat13", "Sun14"};字符串工人新String[][] ={“艾米”、“Bob”、“凯西”、“丹”,“Ed”,“弗雷德”,“顾”};int nShifts = shift .length;int nWorkers = Workers.length;//每个班次所需的工人数量double shifrequirements [] = new double[] {3,2,4,4,5,6,5,2,2,3,4,4,6,7,5}; // Amount each worker is paid to work one shift double pay[] = new double[] { 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.Status.UNBOUNDED) { System.out.println("The model cannot be solved " + "because it is unbounded"); return; } if (status == GRB.Status.OPTIMAL) { System.out.println("The optimal objective is " + model.get(GRB.DoubleAttr.ObjVal)); return; } if (status != GRB.Status.INF_OR_UNBD && status != GRB.Status.INFEASIBLE ) { System.out.println("Optimization was stopped with status " + status); return; } // Do IIS System.out.println("The model is infeasible; computing IIS"); LinkedList removed = new LinkedList(); // Loop until we reduce to a model that can be solved while (true) { model.computeIIS(); System.out.println("\nThe following constraint cannot be satisfied:"); for (GRBConstr c : model.getConstrs()) { if (c.get(GRB.IntAttr.IISConstr) == 1) { System.out.println(c.get(GRB.StringAttr.ConstrName)); // Remove a single constraint from the model removed.add(c.get(GRB.StringAttr.ConstrName)); model.remove(c); break; } } System.out.println(); model.optimize(); status = model.get(GRB.IntAttr.Status); if (status == GRB.Status.UNBOUNDED) { System.out.println("The model cannot be solved " + "because it is unbounded"); return; } if (status == GRB.Status.OPTIMAL) { break; } if (status != GRB.Status.INF_OR_UNBD && status != GRB.Status.INFEASIBLE ) { System.out.println("Optimization was stopped with status " + status); return; } } System.out.println("\nThe following constraints were removed " + "to get a feasible LP:"); for (String s : removed) { System.out.print(s + " "); } System.out.println(); // Dispose of model and environment model.dispose(); env.dispose(); } catch (GRBException e) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } } }