几天来,我一直在为一个要求而苦苦挣扎,我认为我正在使它变得比现在更难。我想制作某种流体旋转/移位系统。这应该是一个向导/随机器功能,只是为了帮助用户创建一个“一次性”时间表(设施管理)。我想让它尽可能通用。
X 员工数量
X 每日轮班数量(例如早上、午餐、晚上和晚上)
X 周期数(例如 4 周 - 周期为一周)
这将导致以下设置:
第 1 周:早上 (*)、午餐、晚上和晚上
第 2 周:早上、午餐 (*)、晚上和晚上
第 3 周:早上、午餐、晚上 (*) 和晚上
第 4 周:早上、午餐、晚上和晚上 (*)
每个员工每周都必须轮班,但接下来的几周轮班不得相同。我可以要求应该有足够的员工来填补所有星期的每个班次。每次执行的结果应该是随机的。
例子:
如果我有 8 名员工,每周轮班 4 次。每班每周至少应有 2 名员工。
如果我每周有 4 名员工和 4 个班次。每周每班至少应有 1 名员工。
员工数量必须始终等于或大于轮班数量。
是否有任何“剩余”这没关系,当员工数量与轮班数量不匹配时,用户必须定义更多轮班或员工。
示例可以在这里看到:http: //v2.iclean.dk/shifts
我有以下代码:
课程
[DebuggerDisplay("ID: {ID}, Name: {Name}")]
public class Staff
{
public Staff(int id, string name)
{
ID = id;
Name = name;
Schedulers = new List<StaffScheduler>();
}
public int ID { get; set; }
public string Name { get; set; }
public List<StaffScheduler> Schedulers { get; set; }
}
public class StaffMap
{
public StaffMap(Staff staff, StaffScheduler scheduler)
{
Staff = staff;
Scheduler = scheduler;
}
public Staff Staff { get; set; }
public StaffScheduler Scheduler { get; set; }
}
[DebuggerDisplay("ID: {ID}, Name: {Name}")]
public class StaffScheduler
{
public StaffScheduler(int id, string name)
{
ID = id;
Name = name;
}
public int ID { get; set; }
public string Name { get; set; }
}
[DebuggerDisplay("Name: {Name}, Start: {DateStart}, End: {DateEnd}")]
public class DateRangeShift
{
public DateRangeShift()
{
Staff = new List<StaffMap>();
}
public DateRangeShift(string name, DateTime start, DateTime end)
: this()
{
Name = name;
DateStart = start;
DateEnd = new DateTime(end.Year, end.Month, end.Day, 23, 59, 59);
}
public string Name { get; set; }
public DateTime DateStart { get; set; }
public DateTime DateEnd { get; set; }
public List<StaffMap> Staff { get; set; }
}
MVC 控制器代码
private readonly int SHIFTS_MAX_SHUFFLE_RETRIES = 50;
public ActionResult Index()
{
var dailySchedules = new List<StaffScheduler>() {
new StaffScheduler(1, "Morning"),
new StaffScheduler(2, "Lunch"),
new StaffScheduler(3, "Evening"),
new StaffScheduler(4, "Night")
};
List<DateRangeShift> shiftList = new List<DateRangeShift>();
shiftList.Add(new DateRangeShift("Week 1", new DateTime(2013, 3, 4), new DateTime(2013, 3, 10)));
shiftList.Add(new DateRangeShift("Week 2", new DateTime(2013, 3, 11), new DateTime(2013, 3, 17)));
shiftList.Add(new DateRangeShift("Week 3", new DateTime(2013, 3, 18), new DateTime(2013, 3, 24)));
shiftList.Add(new DateRangeShift("Week 4", new DateTime(2013, 3, 25), new DateTime(2013, 3, 31)));
List<Staff> staffList = new List<Staff>();
staffList.Add(new Staff(1, "Fred Smith"));
staffList.Add(new Staff(2, "Charlie Brown"));
staffList.Add(new Staff(3, "Samantha Green"));
staffList.Add(new Staff(4, "Bash Malik"));
staffList.Add(new Staff(5, "Bryan Griffiths"));
staffList.Add(new Staff(6, "Akaash Patel"));
staffList.Add(new Staff(7, "Kang-Hyun Kim"));
staffList.Add(new Staff(8, "Pedro Morales"));
var shiftSegmentSize = (int)Math.Floor((double)staffList.Count / (double)dailySchedules.Count)
int shuffleCount = 0;
bool shuffleMatchedSize = true;
do
{
shiftList.ForEach(e => e.Staff.Clear());
shuffleMatchedSize = true;
foreach (var shift in shiftList)
{
foreach (var scheduler in dailySchedules)
{
var schedulerEmployees = staffList.OrderByDescending(e => e.Schedulers.Count()).ThenBy(e => Guid.NewGuid()).Where(e => !shiftList.Any(sl => sl.Staff.Any(s => s.Scheduler.ID == scheduler.ID && s.Staff.ID == e.ID)) && !shift.Staff.Any(s => s.Staff.ID == e.ID)).Take(shiftSegmentSize).ToList();
if (schedulerEmployees.Count < shiftSegmentSize)
{
shuffleMatchedSize = false;
shuffleCount++;
break;
}
while (schedulerEmployees.Count > 0)
{
var staffSelector = schedulerEmployees.FirstOrDefault();
var staff = staffList.FirstOrDefault(e => e.ID == staffSelector.ID);
staff.Schedulers.Add(scheduler);
shift.Staff.Add(new StaffMap(staff, scheduler));
schedulerEmployees.Remove(staffSelector);
}
}
if (!shuffleMatchedSize)
break;
}
} while (!shuffleMatchedSize && shuffleCount < SHIFTS_MAX_SHUFFLE_RETRIES);
ViewData["Iterations"] = shuffleCount;
return View(shiftList);
}
但是因为我按分配给员工的班次数量排序,然后随机排序..我有一个重试,直到我得到一分钟。员工/班次匹配。
<h2>Week 1 <span>(04-03-2013 - 10-03-2013)</span></h2>
<table>
<tr>
<td>Morning</td>
<td>Akaash Patel, Bash Malik</td>
</tr>
<tr>
<td>Lunch</td>
<td>Fred Smith, Kang-Hyun Kim</td>
</tr>
<tr>
<td>Evening</td>
<td>Charlie Brown, Samantha Green</td>
</tr>
<tr>
<td>Night</td>
<td>Bryan Griffiths, Pedro Morales</td>
</tr>
</table>
<h2>Week 2 <span>(11-03-2013 - 17-03-2013)</span></h2>
<table>
<tr>
<td>Morning</td>
<td>Fred Smith, Kang-Hyun Kim</td>
</tr>
<tr>
<td>Lunch</td>
<td>Akaash Patel, Bash Malik</td>
</tr>
<tr>
<td>Evening</td>
<td>Bryan Griffiths, Pedro Morales</td>
</tr>
<tr>
<td>Night</td>
<td>Charlie Brown, Samantha Green</td>
</tr>
</table>
<h2>Week 3 <span>(18-03-2013 - 24-03-2013)</span></h2>
<table>
<tr>
<td>Morning</td>
<td>Charlie Brown, Samantha Green</td>
</tr>
<tr>
<td>Lunch</td>
<td>Bryan Griffiths, Pedro Morales</td>
</tr>
<tr>
<td>Evening</td>
<td>Akaash Patel, Fred Smith</td>
</tr>
<tr>
<td>Night</td>
<td>Bash Malik, Kang-Hyun Kim</td>
</tr>
</table>
<h2>Week 4 <span>(25-03-2013 - 31-03-2013)</span></h2>
<table>
<tr>
<td>Morning</td>
<td>Bryan Griffiths, Pedro Morales</td>
</tr>
<tr>
<td>Lunch</td>
<td>Charlie Brown, Samantha Green</td>
</tr>
<tr>
<td>Evening</td>
<td>Bash Malik, Kang-Hyun Kim</td>
</tr>
<tr>
<td>Night</td>
<td>Akaash Patel, Fred Smith</td>
</tr>
</table>
我无法理解如何做到这一点。希望有人可以指导我。