创建横幅交换算法来旋转广告

stw*_*ite 1 php algorithm ads banner

我正在制作广告横幅广告轮播脚本,该脚本基于每月均匀展示广告的展示次数.每次请求显示广告时都会进行计算.所以这将在飞行中完成.广告应该一个接一个地展开,而不仅仅展示一次展示1000次展示的广告,然后展示另一个展示1000次展示的广告.它在大多数情况下应显示1次展示,然后切换广告(当然,除非一个广告的展示次数比其他广告用得多).

假设我有5个广告,每个广告都有不同的展示次数购买,公式/您如何投放广告?我希望用PHP做到这一点.

广告#1:1,000次购买的展示次数

广告#2:购买了12,000次展示

广告#3:3,000次购买的展示次数

广告#4:20,000次购买的展示次数

广告#5:10,000次购买的展示次数

如果有多个广告在同一时间段内全部购买了1000次展示,则应逐个展示,直到使用展示次数为止.虽然,我认为如果一个人在短时间内购买1000次展示可能会很好,我应该考虑到这一点并以更快的速度展示它们.我愿意接受建议.

Bab*_*aba 7

我认为你应该使用最好的算法类型来获得最好的JOB,我只会告诉你如何实现这些算法的可能性

我目前的例子将显示使用

你也可以实现

  • Priory Based shuffle
  • 时基洗牌
  • 百分比
  • 单击随机播放
  • 等等

简单的概念证明

// 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;
}
Run Code Online (Sandbox Code Playgroud)

可以使用的简单随机播放功能

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);
}
Run Code Online (Sandbox Code Playgroud)

使用的类

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这是一个示例....只需尝试使您的解决方案变得灵活

  • +1为简洁明了的美丽例子. (2认同)