算法在矩阵中均匀分布元素

时间:2015-04-05 04:08:25

标签: c# algorithm

我需要在随机均匀分布的日期生成任务。例如,对于10人到10个任务,使日期间隔不少于两天。不得使用周末和假日。基本上均匀随机分布的元素在多重性与附加条件的多样性 - 人x月的日期。建议,我可以在哪里观看算法。

1 个答案:

答案 0 :(得分:0)

总的来说不知怎的。但是,我必须说,我不喜欢的决定,但没有更好的想法。参数如下:tasksPerMonth - 每月的作业数,distanceBetweenTasks - 两个相邻作业之间的最小距离,isTasksInWeekend - 考虑周末,minDay - 从这一天开始(由于各种原因,这可能不是listOfDays - 最初是空的,假日 - 休假和假期的日期,workdaysInMonth - 工作日列表,随机 - 空的Random()。其余的,我认为,原则上很明显,功能代码如下所示OptimizationTheDistributionOfTasks

      public void GenerateRandomTasks(int tasksPerMonth, int distanceBetweenTasks, bool isTasksInWeekend, int minDay, List<int> listOfDays, List<int> holidays, List<int> workdaysInMonth, Random random)
{
    if (tasksPerMonth == 0)
        tasksPerMonth = 1;
    var daysInMonth = workdaysInMonth.Count + holidays.Count;
    var tasksDaysInMonth = !isTasksInWeekend ? workdaysInMonth.Count : daysInMonth;
    for (int i = 0; i < tasksPerMonth; i++)
    {
        int maxDayInPeriod;
        if (i < tasksPerMonth - 1)
        {
            maxDayInPeriod = minDay + tasksDaysInMonth / tasksPerMonth;
            if (!isTasksInWeekend && holidays.Contains(maxDayInPeriod))
                maxDayInPeriod = workdaysInMonth.First(v => v > maxDayInPeriod);
        }
        else
        {
            maxDayInPeriod = daysInMonth;
            if (!isTasksInWeekend && holidays.Contains(maxDayInPeriod))
                maxDayInPeriod = workdaysInMonth.Last();
        }

        if (minDay > maxDayInPeriod)
            minDay = maxDayInPeriod;

        var day = random.Next(minDay, maxDayInPeriod);

        if ((isTasksInWeekend != true && holidays.Contains(day)))
            day = OptimizationTheDistributionOfTasks(minDay, maxDayInPeriod, listOfDays, day, holidays);

        if (day > daysInMonth)
            day = daysInMonth;

        listOfDays.Add(day);
        minDay = maxDayInPeriod;
        if (minDay <= day + distanceBetweenTasks)
            minDay = day + distanceBetweenTasks + 1;
    }
}

方法优化TheDistributionOfTasks:

       private int OptimizationTheDistributionOfTasks(int minDay, int maxDay, List<int> listDays, int day, List<int> holidays)
{
    var listOfDays = new List<DaysForTaskPlan>();
    for (int k = minDay; k <= maxDay; k++)
    {
        var tempCountDays = listDays.Count(d => k == d);
        if (!holidays.Contains(k))
            listOfDays.Add(new DaysForTaskPlan(k, tempCountDays));
    }
    if (listOfDays.Any())
    {
        day = listOfDays.First(p => p.AmountDays == listOfDays.Min(z => z.AmountDays)).CurrDay;
    }
    listOfDays.Clear();
    return day;
}

祝所有人好运