gpt4 book ai didi

c# - 将员工分成每周轮类(4 周),每周唯一

转载 作者:行者123 更新时间:2023-11-30 17:54:44 27 4
gpt4 key购买 nike

几天来,我一直在为一项要求而苦苦挣扎,我想我正在让它变得比现在更难。我想制作某种流体轮换/轮类系统。这应该是一个向导/随机发生器功能,只是为了帮助用户创建一个“一次性”时间表(设施管理)。我想让它尽可能通用。

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 Controller 代码

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>

我不知道该怎么做。希望有人能指导我。

最佳答案

我认为这对你有用:

        int maxLength = Math.Max(shiftList.Count, dailySchedules.Count);

var shiftSegmentSize = staffList.Count / maxLength;

var schedulerEmployees = staffList.OrderBy(e => Guid.NewGuid())
.Take(shiftSegmentSize * maxLength)
.ToArray(); //

for (int k = 0; k < shiftList.Count; k++)
{
for (int j = 0; j < dailySchedules.Count; j++)
{
int staffindex = (maxLength + j - k) % maxLength;

for (int i = 0; i < shiftSegmentSize; i++)
{
schedulerEmployees[staffindex + (i * maxLength)].Schedulers.Add(dailySchedules[j]);
shiftList[k].Staff.Add(new StaffMap(schedulerEmployees[staffindex + i * maxLength], dailySchedules[j]));
}
}
}

关于c# - 将员工分成每周轮类(4 周),每周唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15768265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com