创建一个横幅交换算法旋转广告 [英] Creating a Banner Swapping Algorithm to Rotate Ads

查看:184
本文介绍了创建一个横幅交换算法旋转广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个广告横幅旋转脚本的基于IM pressions 的,在整个月的平均显示广告。该计算将在广告请求被显示的每个时间进行。所以,这将在飞行完成。该广告应该出现旋转通过,一个又一个,而不是仅仅显示一个广告1000 IM pressions,那么其他广告1000 IM pressions。这在很大程度上应显示为1 IM pression,然后切换广告(当然,除非你的广告有很多IM pressions比其他使用了)。

I'm working on building an ad banner rotation script based on impressions that displays ads evenly throughout the month. The calculations will be done each time the ad is requested to be displayed. So this will be done on the fly. The ads should appear to rotate through, one after another, and not just display one ad for 1000 impressions, then the other ad for 1000 impressions. It for the most part should display for 1 impression, then switch ads (unless of course one ad has a lot more impressions than the other to use up).

让我们说我有5个广告,每个人都有不同数量的IM pressions已购买的,什么是公式/你怎么成为了广告?我在寻找在PHP中做到这一点。

Let's say I have 5 ads and each has a different number of impressions that were purchased, what would be the formula/how do you serve up the ads? I'm looking to do this in PHP.

广告#1:1000购买IM pressions

Ad #1: 1,000 purchased impressions

广告#2:12.000购买IM pressions

Ad #2: 12,000 purchased impressions

广告#3:3000购买IM pressions

Ad #3: 3,000 purchased impressions

广告#4:20,000购买IM pressions

Ad #4: 20,000 purchased impressions

广告#5:10,000购买IM pressions

Ad #5: 10,000 purchased impressions

如果有购买了1000 IM pressions全部为同一时间内的多个广告,它应该表现出一个又一个,直到IM pressions使用。虽然,我认为这可能是很好的,如果一个人购买了1000 IM pressions很短的时间内,我应该考虑这一点,让他们以更快的速度。我打开的建议。

if there are multiple ads that bought 1000 impressions all for the same time frame, it should show one after another until the impressions are used. Though, I think it might be good that if a person bought 1000 impressions for a short time frame, I should account for that and show them at a faster rate. I'm open to suggestions.

推荐答案

我认为你应该使用算法的最佳类型最好的工作,我只会告诉你如何实现一些少数的可能性,例如

I think you should use the best type of algorithm for the best JOB i'll only show you some few possibility on how to implement such

我现在的例子显示使用

  • shuffle
  • fisherYatesShuffle
  • robinShuffle
  • ratioShuffle

您也可以实现

  • 基于隐修会的洗牌
  • 时基随机播放
  • 百分比
  • 点击随机播放

简单的证明概念

// Create Add Infroamtion
$ads = array();
$ads[] = new Ad(10, "A.jpg", 2);
$ads[] = new Ad(12, "B.gif", 3);
$ads[] = new Ad(30, "C.png", 7);
$ads[] = new Ad(20, "D.swf", 5);

// Add ads to banner
$banner = new Banner($ads);

// You can also add addional ads
$banner->add(new Ad(10, "E.swf"));

echo "<pre>";

//Lets Emulate first 100 rotations 
for($i = 0; $i < 1000; $i ++) {
    // Select Algorithm
    $banner->randomise("ratioShuffle");

    // Display Add
    echo $banner->getDisplay(), PHP_EOL;
}

简单的播放功能,可用于

function fisherYatesShuffle(array &$items) {
    for($i = count($items) - 1; $i > 0; $i --) {
        $j = @mt_rand(0, $i);
        $tmp = $items[$i];
        $items[$i] = $items[$j];
        $items[$j] = $tmp;
    }
}

function robinShuffle(array &$items) {
    usort($items, function ($a, $b) {
        $a = $a->getDisplay();
        $b = $b->getDisplay();
        return $a == $b ? 0 : ($a < $b ? - 1 : 1);
    });
}

function ratioShuffle(array &$items) {
    static $called = false;
    if ($called === false) {
        $ads = array();
        foreach ( $items as &$ad ) {
            for($i = 0; $i < $ad->getRatio(); $i ++) {
                $ads[] = $ad;
            }
        }
        $called = true;
        $items = $ads;
    }
    shuffle($items);
}

类中使用

class Ad implements JsonSerializable {
    private $impressions;
    private $media;
    private $ratio = 1;
    private $display = 0;

    function __construct($impressions, $media = null, $ratio = 1) {
        $this->impressions = $impressions;
        $this->media = $media;
        $this->ratio = $ratio;
    }

    function torch() {
        $this->impressions --;
        $this->display ++;
    }

    public function getImpression() {
        return $this->impressions;
    }

    public function getDisplay() {
        return $this->display;
    }

    public function getRatio() {
        return $this->ratio;
    }

    public function getMeadia() {
        return $this->media;
    }

    public function __toString() {
        return json_encode($this->jsonSerialize());
    }

    public function jsonSerialize() {
        return get_object_vars($this);
    }
}


class Banner implements Countable, JsonSerializable {
    private $totalImpressions;
    private $ads = array();

    function __construct(array $ads) {
        foreach ( $ads as $ad )
            $this->add($ad);
    }

    public function add(Ad $ad) {
        $this->ads[] = $ad;
        $this->totalImpressions += $ad->getImpression();
    }

    public function randomise($function = null) {
        if (is_callable($function, false, $callable_name)) {
            return $callable_name($this->ads);
        } else {
            return shuffle($this->ads);
        }
    }

    public function getDisplay() {
        foreach ( $this->ads as &$ad ) {
            if ($ad->getImpression() < 1) {
                unset($ad);
                continue;
            }
            $ad->torch();
            break;
        }
        return isset($ad) ? $ad : null;
    }

    public function jsonSerialize() {
        $array = $this->ads;
        foreach ( $array as &$ad ) {
            $ad = $ad->jsonSerialize();
        }
        return $array;
    }

    public function __toString() {
        return json_encode($this->jsonSerialize());
    }

    function count() {
        return count($this->ads);
    }
}

正如你可以看到这是一个例子....只是试图让你的解决方案,灵活

As you can see this is an example .... Just try and make your solution flexible

这篇关于创建一个横幅交换算法旋转广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆