|  | @@ -1,54 +1,103 @@
 | 
	
		
			
				|  |  |  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.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import java.util.ArrayList;
 | 
	
		
			
				|  |  | -import java.util.LinkedList;
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Component
 | 
	
		
			
				|  |  |  public class VideoAdThompsonScorerV2 {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private AlgorithmRedisHelper redisHelper;
 | 
	
		
			
				|  |  | -    private String redis_creative_="redis:cid_aciton:";
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    // redis:cid_aciton:{$cid}
 | 
	
		
			
				|  |  | +    private String redisCreativeStatisticsPrefix="redis:cid_aciton:";
 | 
	
		
			
				|  |  | +    //redis:vid_cid_aciton:{$vid}_{$cid}
 | 
	
		
			
				|  |  | +    private String redisVideoCreativeStatisticsPrefix="redis:vid_cid_aciton:";
 | 
	
		
			
				|  |  | +    Random random=new Random();
 | 
	
		
			
				|  |  | +    Gson gson=new Gson();
 | 
	
		
			
				|  |  |      public List<AdRankItem> thompsonScorerByExp1(ScoreParam param, List<AdPlatformCreativeDTO> adIdList){
 | 
	
		
			
				|  |  |          List<AdRankItem> result=new LinkedList<>();
 | 
	
		
			
				|  |  | +        String jsonStr;
 | 
	
		
			
				|  |  | +        CreativeStatistic statistic;
 | 
	
		
			
				|  |  | +        double score=0d;
 | 
	
		
			
				|  |  |          for(AdPlatformCreativeDTO dto:adIdList){
 | 
	
		
			
				|  |  |              try {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +                AdRankItem item=new AdRankItem();
 | 
	
		
			
				|  |  | +                item.setVideoId(param.getVideoId());
 | 
	
		
			
				|  |  | +                item.setCpa(dto.getCpa());
 | 
	
		
			
				|  |  | +                item.setAdId(dto.getCreativeId());
 | 
	
		
			
				|  |  | +                jsonStr=redisHelper.getString(redisCreativeStatisticsPrefix+dto.getCreativeId());
 | 
	
		
			
				|  |  | +                if(jsonStr==null){
 | 
	
		
			
				|  |  | +                    score = 0.000001 + (random.nextDouble() * (0.00001 - 0.000001));
 | 
	
		
			
				|  |  | +                }else {
 | 
	
		
			
				|  |  | +                    statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
 | 
	
		
			
				|  |  | +                    score = (1d+Long.parseLong(statistic.getOrder())) / (10000d+Long.parseLong(statistic.getExp())) *dto.getCpa();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                item.setScore(score);
 | 
	
		
			
				|  |  | +                result.add(item);
 | 
	
		
			
				|  |  |              }catch (Exception e){
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        Collections.sort(result);
 | 
	
		
			
				|  |  |          return result;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      public List<AdRankItem> thompsonScorerByExp2(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
 | 
	
		
			
				|  |  |          List<AdRankItem> result=new LinkedList<>();
 | 
	
		
			
				|  |  | +        String jsonStr;
 | 
	
		
			
				|  |  | +        CreativeStatistic statistic;
 | 
	
		
			
				|  |  | +        double score=0d;
 | 
	
		
			
				|  |  |          for(AdPlatformCreativeDTO dto:adIdList){
 | 
	
		
			
				|  |  |              try {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +                AdRankItem item=new AdRankItem();
 | 
	
		
			
				|  |  | +                item.setVideoId(param.getVideoId());
 | 
	
		
			
				|  |  | +                item.setCpa(dto.getCpa());
 | 
	
		
			
				|  |  | +                item.setAdId(dto.getCreativeId());
 | 
	
		
			
				|  |  | +                jsonStr=redisHelper.getString(redisVideoCreativeStatisticsPrefix+param.getVideoId()+"_"+dto.getCreativeId());
 | 
	
		
			
				|  |  | +                if(jsonStr==null){
 | 
	
		
			
				|  |  | +                    score = 0.000001 + (random.nextDouble() * (0.00001 - 0.000001));
 | 
	
		
			
				|  |  | +                }else {
 | 
	
		
			
				|  |  | +                    statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
 | 
	
		
			
				|  |  | +                    score = (1d+Long.parseLong(statistic.getOrder())) / (10000d+Long.parseLong(statistic.getExp())) *dto.getCpa();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                item.setScore(score);
 | 
	
		
			
				|  |  | +                result.add(item);
 | 
	
		
			
				|  |  |              }catch (Exception e){
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        Collections.sort(result);
 | 
	
		
			
				|  |  |          return result;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      public List<AdRankItem> thompsonScorerByExp3(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
 | 
	
		
			
				|  |  |          List<AdRankItem> result=new LinkedList<>();
 | 
	
		
			
				|  |  | +        String jsonStr;
 | 
	
		
			
				|  |  | +        CreativeStatistic statistic;
 | 
	
		
			
				|  |  | +        double score=0d;
 | 
	
		
			
				|  |  |          for(AdPlatformCreativeDTO dto:adIdList){
 | 
	
		
			
				|  |  |              try {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +                AdRankItem item=new AdRankItem();
 | 
	
		
			
				|  |  | +                item.setVideoId(param.getVideoId());
 | 
	
		
			
				|  |  | +                item.setCpa(dto.getCpa());
 | 
	
		
			
				|  |  | +                item.setAdId(dto.getCreativeId());
 | 
	
		
			
				|  |  | +                jsonStr=redisHelper.getString(redisCreativeStatisticsPrefix+dto.getCreativeId());
 | 
	
		
			
				|  |  | +                if(jsonStr==null){
 | 
	
		
			
				|  |  | +                    score = betaSampler(1d,100000d);
 | 
	
		
			
				|  |  | +                }else {
 | 
	
		
			
				|  |  | +                    statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
 | 
	
		
			
				|  |  | +                    score = betaSampler(1d+Long.parseLong(statistic.getOrder()) , 10000d+Long.parseLong(statistic.getExp())) *dto.getCpa();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                item.setScore(score);
 | 
	
		
			
				|  |  | +                result.add(item);
 | 
	
		
			
				|  |  |              }catch (Exception e){
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +        Collections.sort(result);
 | 
	
		
			
				|  |  |          return result;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      public List<AdRankItem> thompsonScorerByExp4(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
 | 
	
	
		
			
				|  | @@ -62,5 +111,49 @@ public class VideoAdThompsonScorerV2 {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          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() {
 | 
	
		
			
				|  |  | +            return exp;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        public void setExp(String exp) {
 | 
	
		
			
				|  |  | +            this.exp = exp;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public String getClick() {
 | 
	
		
			
				|  |  | +            return click;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setClick(String click) {
 | 
	
		
			
				|  |  | +            this.click = click;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public String getOrder() {
 | 
	
		
			
				|  |  | +            return order;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setOrder(String order) {
 | 
	
		
			
				|  |  | +            this.order = order;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public String getCpa() {
 | 
	
		
			
				|  |  | +            return cpa;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +     double betaSampler(double alpha, double beta) {
 | 
	
		
			
				|  |  | +        BetaDistribution betaSample = new BetaDistribution(alpha, beta);
 | 
	
		
			
				|  |  | +        return betaSample.sample();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |