2015-04-05 53 views
-6

我需要在隨機均勻分佈日期的月份中生成任務。例如,10人到10個任務,以便日期間隔不少於兩天。週末和假期不得使用。基本上均勻隨機分佈的多元性與附加條件的多樣性 - 人x月的日期。建議,我可以在哪裏看到算法。算法矩陣中元素的均勻分佈

+0

@ChaitanyaGadkari謝謝你,我已經解決了我的問題。 – SergS 2015-04-16 11:50:03

回答

0

一般決定。但是,我必須說,我不喜歡的一個決定,但沒有比這更好的想法。參數如下:tasksPerMonth - 每月工作的數量,distanceBetweenTasks - 兩個相鄰工作之間的最小距離,isTasksInWeekend - 考慮週末,分鐘是否從這一天開始(由於各種原因,這可能不是每月的第一天),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; 
    } 
} 

方法OptimizationTheDistributionOfTasks:

 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; 
} 

祝所有