public static void MakeHouse(List <IIntervalVar> allTasks, List <IIntervalVar>[] workerTasks, int id, int deadline) { /* CREATE THE INTERVAL VARIABLES. */ String name; IIntervalVar[] tasks = new IIntervalVar[nbTasks]; IIntervalVar[,] taskMatrix = new IIntervalVar[nbTasks, nbWorkers]; for (int i = 0; i < nbTasks; i++) { name = "H" + id + "-" + taskNames[i]; tasks[i] = cp.IntervalVar(taskDurations[i], name); /* ALLOCATING TASKS TO WORKERS. */ List <IIntervalVar> alttasks = new List <IIntervalVar>(); for (int w = 0; w < nbWorkers; w++) { if (HasSkill(w, i)) { name = "H" + id + "-" + taskNames[i] + "-" + workerNames[w]; IIntervalVar wtask = cp.IntervalVar(taskDurations[i], name); wtask.SetOptional(); alttasks.Add(wtask); taskMatrix[i, w] = wtask; workerTasks[w].Add(wtask); allTasks.Add(wtask); /* DEFINING MAXIMIZATION OBJECTIVE. */ skill = cp.Sum(skill, cp.Prod(SkillLevel(w, i), cp.PresenceOf(wtask))); } } cp.Add(cp.Alternative(tasks[i], alttasks.ToArray())); } /* ADDING PRECEDENCE CONSTRAINTS. */ tasks[moving].EndMax = deadline; cp.Add(cp.EndBeforeStart(tasks[masonry], tasks[carpentry])); cp.Add(cp.EndBeforeStart(tasks[masonry], tasks[plumbing])); cp.Add(cp.EndBeforeStart(tasks[masonry], tasks[ceiling])); cp.Add(cp.EndBeforeStart(tasks[carpentry], tasks[roofing])); cp.Add(cp.EndBeforeStart(tasks[ceiling], tasks[painting])); cp.Add(cp.EndBeforeStart(tasks[roofing], tasks[windows])); cp.Add(cp.EndBeforeStart(tasks[roofing], tasks[facade])); cp.Add(cp.EndBeforeStart(tasks[plumbing], tasks[facade])); cp.Add(cp.EndBeforeStart(tasks[roofing], tasks[garden])); cp.Add(cp.EndBeforeStart(tasks[plumbing], tasks[garden])); cp.Add(cp.EndBeforeStart(tasks[windows], tasks[moving])); cp.Add(cp.EndBeforeStart(tasks[facade], tasks[moving])); cp.Add(cp.EndBeforeStart(tasks[garden], tasks[moving])); cp.Add(cp.EndBeforeStart(tasks[painting], tasks[moving])); /* ADDING SAME-WORKER CONSTRAINTS. */ cp.Add(cp.Add(cp.Equiv(cp.PresenceOf(taskMatrix[masonry, joe]), cp.PresenceOf(taskMatrix[carpentry, joe])))); cp.Add(cp.Add(cp.Equiv(cp.PresenceOf(taskMatrix[roofing, jack]), cp.PresenceOf(taskMatrix[facade, jack])))); cp.Add(cp.Add(cp.Equiv(cp.PresenceOf(taskMatrix[carpentry, joe]), cp.PresenceOf(taskMatrix[roofing, joe])))); cp.Add(cp.Add(cp.Equiv(cp.PresenceOf(taskMatrix[garden, jim]), cp.PresenceOf(taskMatrix[moving, jim])))); }
public static void Main(String[] args) { String filename = "../../../../examples/data/rcpspmm_default.data"; int failLimit = 30000; int nbTasks, nbRenewable, nbNonRenewable; if (args.Length > 0) { filename = args[0]; } if (args.Length > 1) { failLimit = Convert.ToInt32(args[1]); } CP cp = new CP(); DataReader data = new DataReader(filename); nbTasks = data.Next(); nbRenewable = data.Next(); nbNonRenewable = data.Next(); ICumulFunctionExpr[] renewables = new ICumulFunctionExpr[nbRenewable]; IIntExpr[] nonRenewables = new IIntExpr[nbNonRenewable]; int[] capRenewables = new int[nbRenewable]; int[] capNonRenewables = new int[nbNonRenewable]; for (int j = 0; j < nbRenewable; j++) { renewables[j] = cp.CumulFunctionExpr(); capRenewables[j] = data.Next(); } for (int j = 0; j < nbNonRenewable; j++) { nonRenewables[j] = cp.IntExpr(); capNonRenewables[j] = data.Next(); } IIntervalVar[] tasks = new IIntervalVar[nbTasks]; List <IIntervalVar>[] modes = new List <IIntervalVar> [nbTasks]; for (int i = 0; i < nbTasks; i++) { tasks[i] = cp.IntervalVar(); modes[i] = new List <IIntervalVar>(); } List <IIntExpr> ends = new List <IIntExpr>(); for (int i = 0; i < nbTasks; i++) { IIntervalVar task = tasks[i]; int d = data.Next(); int nbModes = data.Next(); int nbSucc = data.Next(); for (int k = 0; k < nbModes; k++) { IIntervalVar alt = cp.IntervalVar(); alt.SetOptional(); modes[i].Add(alt); } cp.Add(cp.Alternative(task, modes[i].ToArray())); ends.Add(cp.EndOf(task)); for (int s = 0; s < nbSucc; s++) { int succ = data.Next(); cp.Add(cp.EndBeforeStart(task, tasks[succ])); } } for (int i = 0; i < nbTasks; i++) { IIntervalVar task = tasks[i]; List <IIntervalVar> imodes = modes[i]; for (int k = 0; k < imodes.Count; k++) { int taskId = data.Next(); int modeId = data.Next(); int d = data.Next(); imodes[k].SizeMin = d; imodes[k].SizeMax = d; int q; for (int j = 0; j < nbNonRenewable; j++) { q = data.Next(); if (0 < q) { renewables[j].Add(cp.Pulse(imodes[k], q)); } } for (int j = 0; j < nbNonRenewable; j++) { q = data.Next(); if (0 < q) { nonRenewables[j] = cp.Sum(nonRenewables[j], cp.Prod(q, cp.PresenceOf(imodes[k]))); } } } } for (int j = 0; j < nbRenewable; j++) { cp.Add(cp.Le(renewables[j], capRenewables[j])); } for (int j = 0; j < nbRenewable; j++) { cp.Add(cp.Le(nonRenewables[j], capNonRenewables[j])); } IObjective objective = cp.Minimize(cp.Max(ends.ToArray())); cp.Add(objective); cp.SetParameter(CP.IntParam.FailLimit, failLimit); Console.WriteLine("Instance \t: " + filename); if (cp.Solve()) { Console.WriteLine("Makespan \t: " + cp.ObjValue); } else { Console.WriteLine("No solution found."); } }
public static void Main(String[] args) { String filename = "../../../../examples/data/jobshopflex_default.data"; int failLimit = 10000; if (args.Length > 0) { filename = args[0]; } if (args.Length > 1) { failLimit = Convert.ToInt32(args[1]); } CP cp = new CP(); DataReader data = new DataReader(filename); int nbJobs = data.Next(); int nbMachines = data.Next(); List <IIntervalVar>[] machines = new List <IIntervalVar> [nbMachines]; for (int j = 0; j < nbMachines; j++) { machines[j] = new List <IIntervalVar>(); } List <IIntExpr> ends = new List <IIntExpr>(); for (int i = 0; i < nbJobs; i++) { int nbOperations = data.Next(); IIntervalVar prec = cp.IntervalVar(); for (int j = 0; j < nbOperations; j++) { int nbOpMachines = data.Next(); IIntervalVar master = cp.IntervalVar(); List <IIntervalVar> members = new List <IIntervalVar>(); for (int k = 0; k < nbOpMachines; k++) { int m = data.Next(); int d = data.Next(); IIntervalVar member = cp.IntervalVar(d); member.SetOptional(); members.Add(member); machines[m - 1].Add(member); } cp.Add(cp.Alternative(master, members.ToArray())); if (j > 0) { cp.Add(cp.EndBeforeStart(prec, master)); } prec = master; } ends.Add(cp.EndOf(prec)); } for (int j = 0; j < nbMachines; j++) { cp.Add(cp.NoOverlap(machines[j].ToArray())); } IObjective objective = cp.Minimize(cp.Max(ends.ToArray())); cp.Add(objective); cp.SetParameter(CP.IntParam.FailLimit, failLimit); Console.WriteLine("Instance \t: " + filename); if (cp.Solve()) { Console.WriteLine("Makespan \t: " + cp.ObjValue); } else { Console.WriteLine("No solution found."); } }