在C#

时间:2018-10-17 13:51:07

标签: c# .net optimization task-parallel-library parallel.foreach

我正在研究一种调度算法,该算法根据以下限制来生成/分配时隙到“收件人列表”:

  • 每分钟最多收件人
  • 每小时最多收件人

假设交货开始时间为2018-10-17 9:00 AM,我们有19位收件人,每分钟最多5个,每小时最多10个,因此输出应为:

  1. 5名收件人将于2018年10月17日上午9:00
  2. 5名收件人将于2018年10月17日上午9:01
  3. 5个收件人将于2018年10月17日上午10:00
  4. 4名收件人计划于2018-10-17 10:01 AM

该算法非常准确,但是其工作方式如下:

  • 首先,它会生成一个准确地符合编号的时隙或时间窗口的列表。基于我之前提到的限制。
  • 然后,我将移动每个列表/组或收件人的“时隙列表”中的所有可用内容。
  • 在“时隙”列表中,我添加了一个计数器,该计数器针对添加到其中的每个收件人递增,因此可以以此方式跟踪编号。每个时隙中添加的每个收件人的数量,以遵守“每分钟最大/小时”的限制。

此代码段中简化了先前的过程-我正在使用While循环进行迭代,以我为例,当有500K收件人时,这需要28分钟才能完成! 我尝试使用Parallel.ForEach,但在这种情况下我不知道如何实现。

DateTime DeliveryStart = DateTime.Now;
//This list has DateTime: Time-windows  values starting from DeliveryStart to the Max value of the time needed to schedule the Recipients
var listOfTimeSlots = new List<Tuple<DateTime, bool, int>>();
//List of Recipients with Two types of data: DateTime to tell when its scheduled and int value refers to the Recipient's ID
var ListOfRecipients = new List<Tuple<DateTime, int>>();
List<Tuple<int, DateTime>> RecipientsWithTimeSlots= new List<Tuple<int, DateTime>>();
int noOfRecipients = ListOfRecipients.Count;

int Prevhour = 0, _AddedPerHour = 0, Prevday = 0;
// Scheduling restrictions 
int _MaxPerHour = 5400, _MaxPerMinute = 90;
int i = 0;
int indexStart = 0;

// ...
//     ...
//           Code to fill listOfTimeSlots ListOfRecipients with Data

while (noOfRecipients > 0)
{
    var TimeStamp = listOfTimeSlots[i];

    int hour = TimeStamp.Item1.Hour;
    int day = TimeStamp.Item1.Day;

    if (Prevhour == 0)
    {
        Prevhour = hour;
        Prevday = day;
    }
    if (Prevhour != hour)
    {
        Prevhour = hour;
        _AddedPerHour = 0;
    }

    if (_AddedPerHour >= _MaxPerHour)
    {
        var tmpItem = listOfTimeSlots.Where(l => l.Item1.Hour == hour && l.Item1.Day == day).LastOrDefault();
        int indexOfNextItem = listOfTimeSlots.LastIndexOf(tmpItem) + 1;
        i = indexOfNextItem;
        _AddedPerHour = 0;
        continue;
    }
    else
    {
        int endIndex;


        endIndex = _MaxPerMinute > noOfRecipients ? noOfRecipients : _MaxPerMinute;

        if (endIndex > Math.Abs(_AddedPerHour - _MaxPerHour))
            endIndex = Math.Abs(_AddedPerHour - _MaxPerHour);

        var RecipientsToIteratePerMinute = ListOfRecipients.GetRange(indexStart, endIndex);

        foreach (var item in RecipientsToIteratePerMinute)
        {
            RecipientsWithTimeSlots.Add(new Tuple<int, DateTime>(item.Item2, TimeStamp.Item1));
            listOfTimeSlots[i] = new Tuple<DateTime, bool, int>(TimeStamp.Item1, true, listOfTimeSlots[i].Item3 + 1);
            _AddedPerHour++;
        }

        indexStart += endIndex;
        noOfRecipients -= endIndex;
        i++;

    }
}

我在这里简化了代码,因为不易理解,我只希望它加快while循环或将其替换为Parallel.ForEach。

从来没有简化过循环,这是如何工作的

任何帮助或建议都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

这是另一种方法。首先创建ID组,然后根据要求为其分配日期。

首先,一个代表组的类(避免使用元组):

public class RecipientGroup
{       
    public RecipientGroup(DateTime scheduledDateTime, IEnumerable<int> recipients)
    {
        ScheduledDateTime= scheduledDateTime;
        Recipients = recipients;
    }

    public DateTime ScheduledDateTime { get; private set; }
    public IEnumerable<int> Recipients { get; private set; }

    public override string ToString()
    {
        return string.Format($"Date: {ScheduledDateTime.ToShortDateString()} {ScheduledDateTime.ToLongTimeString()}, count: {Recipients.Count()}");
    }
}

然后是一个类,以遍历各个组。您稍后将看到为什么需要这样做:

public class GroupIterator
{        
    public GroupIterator(DateTime scheduledDateTime)
    {
        ScheduledDateTime = scheduledDateTime;
    }

    public DateTime ScheduledDateTime { get; set; }
    public int Count { get; set; }
}

现在,代码:

DateTime DeliveryStart = new DateTime(2018, 10, 17);

//List of Recipients (fake populate function)
IEnumerable<int> allRecipients = PopulateRecipients();            

// Scheduling restrictions 
int maxPerMinute = 90;
int maxPerHour = 270;

//Creates groups broken down by the max per minute.  
var groupsPerMinute = allRecipients
        .Select((s, i) => new { Value = s, Index = i })
        .GroupBy(x => x.Index / maxPerMinute)
        .Select(group => group.Select(x => x.Value).ToArray());

//This will be the resulting groups
var deliveryDateGroups = new List<RecipientGroup>();

//Perform an aggregate run on the groups using the iterator
groupsPerMinute.Aggregate(new GroupIterator(DeliveryStart), (iterator, ids) => 
{
    var nextBreak = iterator.Count + ids.Count();
    if (nextBreak >= maxPerHour)
    {
        //Will go over limit, split
        var difference = nextBreak-maxPerHour;
        var groupSize = ids.Count() - difference;
        //This group completes the batch
        var group = new RecipientGroup(iterator.ScheduledDateTime, ids.Take(groupSize));
        deliveryDateGroups.Add(group);
        var newDate = iterator.ScheduledDateTime.AddHours(1).AddMinutes(-iterator.ScheduledDateTime.Minute);
        //Add new group with remaining recipients.
        var stragglers = new RecipientGroup(newDate, ids.Skip(groupSize));
        deliveryDateGroups.Add(stragglers);
        return new GroupIterator(newDate, difference);
    }                    
    else
    {
        var group = new RecipientGroup(iterator.ScheduledDateTime, ids);
        deliveryDateGroups.Add(group);
        iterator.ScheduledDateTime = iterator.ScheduledDateTime.AddMinutes(1);
        iterator.Count += ids.Count();
        return iterator;
    }                      
});

//Output minute group count
Console.WriteLine($"Group count: {deliveryDateGroups.Count}");

//Groups by hour
var byHour = deliveryDateGroups.GroupBy(g => new DateTime(g.ScheduledDateTime.Year, g.ScheduledDateTime.Month, g.ScheduledDateTime.Day, g.ScheduledDateTime.Hour, 0, 0));

Console.WriteLine($"Hour Group count: {byHour.Count()}");
foreach (var group in byHour)
{
     Console.WriteLine($"Date: {group.Key.ToShortDateString()} {group.Key.ToShortTimeString()}; Count: {group.Count()}; Recipients: {group.Sum(g => g.Recipients.Count())}");
}

输出:

  

组数:5556

     

小时小组人数:1852

     

日期:10/17/2018 12:00 AM;数:3;收件人:270

     

日期:10/17/2018 1:00 AM;数:3;收件人:270

     

日期:10/17/2018 2:00 AM;数:3;收件人:270

     

日期:2018/10/17 3:00;数:3;收件人:270

     

日期:10/17/2018 4:00 AM;数:3;收件人:270

     

日期:2018/10/17 5:00;数:3;收件人:270

     

...以此类推,用于所有1852个群组。

这大约需要3秒钟。

我确定有一些极端情况。我很着急地写了这篇文章,所以请考虑一下。