广告预算平滑均分代码思路

最近在做广告预算均分,写了一个demo进行验证记录下;

 package main

import (
    "log"
    "math/rand"
    "time"
)
//思路:每次分配预算的时候
//先不考虑当前小时
//把过去的容量 - 过去的消耗 =余量 算出
// 从当前小时开始分配

func main() {

    for {
        budget := 1000.0                                                 //日总预算
        pubHours := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 19, 20, 21, 22, 23} // 投放的时间段
        hourCost := make(map[int]float64)                                //  消耗表
        //模拟消耗的初始化
        hourCost[1] = 30
        hourCost[2] = 40
        hourCost[3] = 30
        hourCost[19] = 30
        hourCost[20] = 50
        hourCost[21] = 100

        consume := int(rand.Float64() * 100)
        idx := rand.Intn(len(pubHours) - 1)
        hour := pubHours[idx]
        hourCost[hour] += float64(consume)         // 随机为某个小时增加消耗
        budget = budget + float64(rand.Intn(1000)) //假设预算变动
        log.Printf(" gen hour %v ,cost : %v ", hour, consume)
        BudgetAssign(budget, pubHours, hourCost)
        time.Sleep(5 * time.Second)
    }

}

// BudgetAssign 预算分配逻辑
func BudgetAssign(budget float64, pubHours []int, hourCost map[int]float64) {
    hourCap := make(map[int]float64)
    hourBudget := make(map[int]float64)
    pubLen := len(pubHours)
    for _, hour := range pubHours {
        hourCap[hour] = budget / float64(pubLen)
    }
    hour := time.Now().Hour()
    pastCap := 0.0              // 过去的容量和
    pastConsume := 0.0          // 过去的总消耗
    pastHours := 0              // 过去了多少个小时
    totalHours := len(pubHours) // 所有的投放时间的长度
    var leftBudget float64
    for _, pubhour := range pubHours {
        if pubhour < hour {
            pastCap += hourCap[pubhour]
            pastConsume += hourCost[pubhour]
            hourBudget[pubhour] = hourCost[pubhour]
            pastHours++
        } else {
            sub := pastCap - pastConsume // 差额
            leftBudget = budget - pastCap + sub
            hourBudget[pubhour] = leftBudget / float64(totalHours-pastHours)
        }
    }
    log.Printf("过去的消耗 %v 总和 %v \n", hourCost, pastConsume)
    log.Printf("当前时段 %d total %v 过去消耗 %v 剩余总预算 %v  剩余的时段 %v平均预算 %v\n ", hour, budget, pastConsume, leftBudget, totalHours-pastHours, hourBudget)
    //log.Printf(" 剩余分配预算 %v", hourBudget)

    // 当前小时开始 新的预算分配
    //fmt.Printf("hourCap :%v\n", hourCap)

}

本文链接:参与评论 »

--EOF--

提醒:本文最后更新于 467 天前,文中所描述的信息可能已发生改变,请谨慎使用。

Comments