|
@@ -0,0 +1,278 @@
|
|
|
+package com.tzld.piaoquan.ad.engine.service.score;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
|
|
|
+import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
|
|
|
+import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
|
|
|
+import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
|
|
|
+import org.apache.commons.math3.distribution.BetaDistribution;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class VideoAdThompsonScorerV2 {
|
|
|
+ Logger log = LoggerFactory.getLogger(VideoAdThompsonScorerV2.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlgorithmRedisHelper redisHelper;
|
|
|
+ // redis:cid_action:{$cid}
|
|
|
+ private String redisCreativeStatisticsPrefix="redis:cid_action:";
|
|
|
+ //redis:vid_cid_action:{$vid}_{$cid}
|
|
|
+ private String redisVideoCreativeStatisticsPrefix="redis:vid_cid_action:";
|
|
|
+
|
|
|
+ private Map<String,Double> exp663Param=new HashMap<>();
|
|
|
+ private Map<String,Double> exp664Param=new HashMap<>();
|
|
|
+ private Map<String,Double> exp665Param=new HashMap<>();
|
|
|
+ private Map<String,Double> exp666Param=new HashMap<>();
|
|
|
+ Random random=new Random();
|
|
|
+ Gson gson=new Gson();
|
|
|
+ public List<AdRankItem> thompsonScorerByExp663(ScoreParam param, List<AdPlatformCreativeDTO> adIdList){
|
|
|
+ List<AdRankItem> result=new LinkedList<>();
|
|
|
+ String jsonStr;
|
|
|
+ CreativeStatistic statistic;
|
|
|
+ List<String> redisKey=new LinkedList<>();
|
|
|
+ adIdList.forEach(creativeDTO -> {
|
|
|
+ redisKey.add(redisCreativeStatisticsPrefix+creativeDTO.getCreativeId());
|
|
|
+ });
|
|
|
+ List<String> values=redisHelper.getValues(redisKey);
|
|
|
+ double score=0d;
|
|
|
+ int i=0;
|
|
|
+ for(AdPlatformCreativeDTO dto:adIdList){
|
|
|
+ AdRankItem item=new AdRankItem();
|
|
|
+ item.setVideoId(param.getVideoId());
|
|
|
+ item.setCpa(dto.getCpa());
|
|
|
+ item.setAdId(dto.getCreativeId());
|
|
|
+ try {
|
|
|
+ jsonStr=values.get(i);
|
|
|
+ if(jsonStr==null){
|
|
|
+ score = exp663Param.getOrDefault("randomMin",0.000001)
|
|
|
+ + (random.nextDouble() *
|
|
|
+ (exp663Param.getOrDefault("randomMax",0.00001 ) - exp663Param.getOrDefault("randomMin",0.000001 )));
|
|
|
+ }else {
|
|
|
+ statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
|
|
|
+ score = (exp663Param.getOrDefault("alpha",1d)+Long.parseLong(statistic.getOrder()))
|
|
|
+ / (exp663Param.getOrDefault("beta",10000d)+Long.parseLong(statistic.getExp())) ;
|
|
|
+ }
|
|
|
+ score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
|
|
|
+ item.setScore(score);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("svc=thompsonScorerByExp663 {}",gson.toJson(e.getStackTrace()));
|
|
|
+ }
|
|
|
+ result.add(item);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ Collections.sort(result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public List<AdRankItem> thompsonScorerByExp664(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
|
|
|
+ List<AdRankItem> result=new LinkedList<>();
|
|
|
+ String jsonStr;
|
|
|
+ CreativeStatistic statistic;
|
|
|
+ List<String> redisKey=new LinkedList<>();
|
|
|
+ adIdList.forEach(creativeDTO -> {
|
|
|
+ redisKey.add(redisVideoCreativeStatisticsPrefix+param.getVideoId()+"_"+creativeDTO.getCreativeId());
|
|
|
+ });
|
|
|
+ List<String> values=redisHelper.getValues(redisKey);
|
|
|
+ int i=0;
|
|
|
+ double score=0d;
|
|
|
+ for(AdPlatformCreativeDTO dto:adIdList){
|
|
|
+ AdRankItem item=new AdRankItem();
|
|
|
+ item.setVideoId(param.getVideoId());
|
|
|
+ item.setCpa(dto.getCpa());
|
|
|
+ item.setAdId(dto.getCreativeId());
|
|
|
+ try {
|
|
|
+
|
|
|
+ jsonStr=values.get(i);
|
|
|
+ if(jsonStr==null){
|
|
|
+ score = exp664Param.getOrDefault("randomMin",0.000001)
|
|
|
+ + (random.nextDouble() *
|
|
|
+ (exp664Param.getOrDefault("randomMax",0.00001 ) - exp664Param.getOrDefault("randomMin",0.000001 )));
|
|
|
+ }else {
|
|
|
+ statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
|
|
|
+ score = (exp664Param.getOrDefault("alpha",1d)+Long.parseLong(statistic.getOrder()))
|
|
|
+ / (exp664Param.getOrDefault("beta",10000d)+Long.parseLong(statistic.getExp())) ;
|
|
|
+ }
|
|
|
+ score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
|
|
|
+ item.setScore(score);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("svc=thompsonScorerByExp664 {}",gson.toJson(e.getStackTrace()));
|
|
|
+ }
|
|
|
+ result.add(item);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ Collections.sort(result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public List<AdRankItem> thompsonScorerByExp665(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
|
|
|
+ List<AdRankItem> result=new LinkedList<>();
|
|
|
+ String jsonStr;
|
|
|
+ CreativeStatistic statistic;
|
|
|
+ List<String> redisKey=new LinkedList<>();
|
|
|
+ adIdList.forEach(creativeDTO -> {
|
|
|
+ redisKey.add(redisCreativeStatisticsPrefix+creativeDTO.getCreativeId());
|
|
|
+ });
|
|
|
+ List<String> values=redisHelper.getValues(redisKey);
|
|
|
+ int i=0;
|
|
|
+ double score=0d;
|
|
|
+ for(AdPlatformCreativeDTO dto:adIdList){
|
|
|
+ AdRankItem item=new AdRankItem();
|
|
|
+ item.setVideoId(param.getVideoId());
|
|
|
+ item.setCpa(dto.getCpa());
|
|
|
+ item.setAdId(dto.getCreativeId());
|
|
|
+ try {
|
|
|
+ jsonStr=values.get(i);
|
|
|
+ if(jsonStr==null){
|
|
|
+ score = betaSampler(exp665Param.getOrDefault("alpha",1d),exp665Param.getOrDefault("beta",100000d));
|
|
|
+ }else {
|
|
|
+ statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
|
|
|
+ score = betaSampler(exp665Param.getOrDefault("alpha",1d)+Long.parseLong(statistic.getOrder()) ,
|
|
|
+ exp665Param.getOrDefault("beta",100000d)+Long.parseLong(statistic.getExp())) ;
|
|
|
+ }
|
|
|
+ score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
|
|
|
+ item.setScore(score);
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("svc=thompsonScorerByExp665 {}",gson.toJson(e.getStackTrace()));
|
|
|
+ }
|
|
|
+ result.add(item);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ Collections.sort(result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public List<AdRankItem> thompsonScorerByExp666(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
|
|
|
+ List<AdRankItem> result=new LinkedList<>();
|
|
|
+ String jsonStr;
|
|
|
+ CreativeStatistic statistic;
|
|
|
+
|
|
|
+ List<String> redisKey=new LinkedList<>();
|
|
|
+ adIdList.forEach(creativeDTO -> {
|
|
|
+ redisKey.add(redisCreativeStatisticsPrefix+creativeDTO.getCreativeId());
|
|
|
+ });
|
|
|
+ List<String> values=redisHelper.getValues(redisKey);
|
|
|
+ List<String> combineKeys=new LinkedList<>();
|
|
|
+ adIdList.forEach(creativeDTO -> {
|
|
|
+ combineKeys.add(redisVideoCreativeStatisticsPrefix+param.getVideoId()+"_"+creativeDTO.getCreativeId());
|
|
|
+ });
|
|
|
+ List<String> combineValues=redisHelper.getValues(combineKeys);
|
|
|
+ int i=0;
|
|
|
+ double score=0d;
|
|
|
+ for(AdPlatformCreativeDTO dto:adIdList){
|
|
|
+ AdRankItem item=new AdRankItem();
|
|
|
+ item.setVideoId(param.getVideoId());
|
|
|
+ item.setCpa(dto.getCpa());
|
|
|
+ item.setAdId(dto.getCreativeId());
|
|
|
+ try {
|
|
|
+ jsonStr=combineValues.get(i);
|
|
|
+ if(jsonStr==null){
|
|
|
+ jsonStr=values.get(i);
|
|
|
+ if(jsonStr==null){
|
|
|
+ score = betaSampler(exp666Param.getOrDefault("alpha",1d),exp666Param.getOrDefault("beta",100000d));
|
|
|
+ }else {
|
|
|
+ statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
|
|
|
+ score = betaSampler(exp666Param.getOrDefault("alpha",1d)+Long.parseLong(statistic.getOrder()) , exp666Param.getOrDefault("beta",100000d)+Long.parseLong(statistic.getExp())) ;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
|
|
|
+ if(Double.parseDouble(statistic.getExp())>exp666Param.getOrDefault("viewThreshold",5000d)){
|
|
|
+ score = betaSampler(1d+Long.parseLong(statistic.getOrder()) , exp666Param.getOrDefault("beta",100000d)+Long.parseLong(statistic.getExp())) ;
|
|
|
+ } else if( values.get(i)!=null) {
|
|
|
+ statistic =gson.fromJson(values.get(i),CreativeStatistic.class);
|
|
|
+ score = betaSampler(exp666Param.getOrDefault("alpha",1d)+Long.parseLong(statistic.getOrder()) , exp666Param.getOrDefault("beta",100000d)+Long.parseLong(statistic.getExp())) ;
|
|
|
+ }else {
|
|
|
+ score = betaSampler(exp666Param.getOrDefault("alpha",1d),exp666Param.getOrDefault("beta",100000d));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
|
|
|
+ item.setScore(score);
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("svc=thompsonScorerByExp666 {}",gson.toJson(e.getStackTrace()));
|
|
|
+ }
|
|
|
+ result.add(item);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ Collections.sort(result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ class CreativeStatistic{
|
|
|
+ public CreativeStatistic() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCpa(String cpa) {
|
|
|
+ this.cpa = cpa;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String exp;
|
|
|
+ private String click;
|
|
|
+ private String order;
|
|
|
+ private String cpa;
|
|
|
+
|
|
|
+ public String getExp() {
|
|
|
+ if (exp == null || "".equals(exp)) {
|
|
|
+ return "0";
|
|
|
+ }
|
|
|
+ return exp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setExp(String exp) {
|
|
|
+ this.exp = exp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getClick() {
|
|
|
+ if (click == null || "".equals(click)) {
|
|
|
+ return "0";
|
|
|
+ }
|
|
|
+ return click;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setClick(String click) {
|
|
|
+ this.click = click;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getOrder() {
|
|
|
+ if (order == null || "".equals(order)) {
|
|
|
+ return "0";
|
|
|
+ }
|
|
|
+ return order;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setOrder(String order) {
|
|
|
+ this.order = order;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCpa() {
|
|
|
+ if (cpa == null || "".equals(cpa)) {
|
|
|
+ return "0";
|
|
|
+ }
|
|
|
+ return cpa;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ double betaSampler(double alpha, double beta) {
|
|
|
+ BetaDistribution betaSample = new BetaDistribution(alpha, beta);
|
|
|
+ return betaSample.sample();
|
|
|
+ }
|
|
|
+ @Value("${ad.engine.new.thompson.exp.V2.663:{\"randomMin\":0.000001,\"randomMax\":0.00001,\"alpha\":1,\"beta\":10000}}")
|
|
|
+ public void setExp663Param(String str){
|
|
|
+ this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
+ }
|
|
|
+ @Value("${ad.engine.new.thompson.exp.V2.664:{}}")
|
|
|
+ public void setExp664Param(String str){
|
|
|
+ this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
+ }
|
|
|
+ @Value("${ad.engine.new.thompson.exp.V2.665:{}}")
|
|
|
+ public void setExp665Param(String str){
|
|
|
+ this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
+ }
|
|
|
+ @Value("${ad.engine.new.thompson.exp.V2.666:{}}")
|
|
|
+ public void setExp666Param(String str){
|
|
|
+ this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
+ }
|
|
|
+}
|