开发者

How can I create an even distribution of objects with different weights (scheduling)?

开发者 https://www.devze.com 2023-02-21 17:12 出处:网络
Here is my problem in English: I\'ve got several WidgetContainer objects. Each WidgetContainer will have at least one Widget.

Here is my problem in English:

  • I've got several WidgetContainer objects.
  • Each WidgetContainer will have at least one Widget.
  • Each WidgetContainer wants to display one of its Widgets n amount of times per day.
  • Widgets could be displayed on 'x' number of Venues.
  • A Widget is displayed for exactly t seconds before the next scheduled WidgetContainer's Widget takes its place.
  • If the entire day's is not filled up then nothing should be displayed during those times (ads should be evenly dispersed throughout the day t seconds at a time)

And here are the objects represented by pseudo开发者_开发技巧 code:

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...*/;

I need to find an efficient way of calculating an evenly distributed schedule for these objects. These "objects" are linq-to-sql objects so some linq suggestions would be nice

My idea right now is to flatten the WidgetContainers to their Widgets; dividing their DailyImpressions by the number of Widgets.

I could figure it out easily if there weren't multiple and differing Venues to take into account.

I have a feeling I just need to see someone else's perspective on the problem since I've been staring at is so long.

So, any help that could possibly point me in the right direction or provide some perspective on the problem, even if it is obvious, would be greatly appreciated!


Based on that lot, if I've understood, this should give you correct answers:

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

        }
0

精彩评论

暂无评论...
验证码 换一张
取 消