如何创建具有不同权重(调度)的对象的均匀分布?

时间:2011-04-01 15:06:38

标签: c# linq-to-sql scheduling distribution

这是我的英文问题:

  • 我有几个WidgetContainer个对象。
  • 每个WidgetContainer至少有一个Widget
  • 每个WidgetContainer想要每天Widgets n次显示其中一个。
  • Widgets可以显示在Venues
  • 在下一个预定的Widget t取代之前,WidgetContainer's会显示Widget秒。
  • 如果整天没有填满,那么在这些时间内不应显示任何内容(广告应该在一天t秒内均匀分散)

以下是伪代码所代表的对象:

var WidgetContainers = [
   {
       DailyImpressionsRequired: 52, // should be split between Venues
       Widgets: ["one", "two"],
       Venues: ["here", "there"]
   },
   {
       DailyImpressionsRequired: 20,
       Widgets: ["foo"],
       Venues: ["here", "there", "everywhere"]
   },
   {
       DailyImpressionsRequired: 78,
       Widgets: ["bar", "bat", "heyhey!"],
       Venues: ["up", "down", "allAround"]
   }
];
var SecondsInADay = 86400;
var DisplayInterval = 30; // seconds
var TotalNumverOrVenues = /*eh, some calulations...*/;
var AvailableSlots = /*eh, some calulations...*/;
var SlotsNeeded = /*eh, some calulations...*/;

我需要找到一种有效的方法来计算这些对象的均匀分布式计划。这些“对象”是linq-to-sql对象,因此一些linq建议会很好

我现在的想法是将WidgetContainers压平为Widgets;将DailyImpressions除以Widgets的数量。

如果没有多个不同的场地可以考虑,我可以轻松搞清楚。

我有一种感觉,我需要看到别人对这个问题的看法,因为我一直盯着这么久。

所以,任何帮助可能会指出我正确的方向或提供一些问题的观点,即使很明显,也会非常感激!

1 个答案:

答案 0 :(得分:0)

基于那个地段,如果我明白了,这应该给你正确答案:

static void Main(string[] args)
        {
            List<WidgetContainer> data = new List<WidgetContainer>();

            data.Add(new WidgetContainer {
                Widgets = new List<String> {"one","two"},
                Venues = new List<String>{"here","there"},
                DailyImpressionsRequired=52});

            data.Add(new WidgetContainer {
                Widgets = new List<String> {"foo"},
                Venues = new List<String>{"here","there","everywhere"},
                DailyImpressionsRequired=20});

            data.Add(new WidgetContainer {
                Widgets = new List<String> {"bar","bat", "heyhey!"},
                Venues = new List<String>{"up","down", "allAround"},
                DailyImpressionsRequired=78});

            var SecondsInADay = 86400;
            var DisplayInterval = 30; // seconds

            var TotalNumverOfVenues = data.SelectMany(x=> x.Venues).Distinct().Count();
            var AvailableSlots = SecondsInADay * data.SelectMany(x=> x.Venues).Distinct().Count() / DisplayInterval ; //assuming you didn't already have the count as a variable - will re-evaluate so don't use this for real!
            //var AvailableSlots = SecondsInADay * TotalNumverOfVenues / DisplayInterval ; //the better way - avoids recalculating count
            var SlotsNeeded = data.Sum(x => x.DailyImpressionsRequired);

        }