|
@@ -5,7 +5,9 @@ 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.lang3.StringUtils;
|
|
|
import org.apache.commons.math3.distribution.BetaDistribution;
|
|
|
+import org.checkerframework.checker.units.qual.C;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
public class VideoAdThompsonScorerV2 {
|
|
@@ -29,6 +32,9 @@ public class VideoAdThompsonScorerV2 {
|
|
|
private Map<String,Double> exp664Param=new HashMap<>();
|
|
|
private Map<String,Double> exp665Param=new HashMap<>();
|
|
|
private Map<String,Double> exp666Param=new HashMap<>();
|
|
|
+ private Map<String,Double> exp669Param=new HashMap<>();
|
|
|
+ private Map<String,Double> exp670Param=new HashMap<>();
|
|
|
+
|
|
|
Random random=new Random();
|
|
|
Gson gson=new Gson();
|
|
|
public List<AdRankItem> thompsonScorerByExp663(ScoreParam param, List<AdPlatformCreativeDTO> adIdList){
|
|
@@ -208,19 +214,107 @@ public class VideoAdThompsonScorerV2 {
|
|
|
Collections.sort(result);
|
|
|
return result;
|
|
|
}
|
|
|
- class CreativeStatistic{
|
|
|
- public CreativeStatistic() {
|
|
|
+
|
|
|
+ public List<AdRankItem> thompsonScorerByExp669(ScoreParam param, List<AdPlatformCreativeDTO> adIdList) {
|
|
|
+ Map<Long, CreativeStatistic> creativeStatisticsMap = this.batchFindCreativeRedisCache(redisCreativeStatisticsPrefix, adIdList);
|
|
|
+ Map<Long, CreativeStatistic> videoCreativeStatisticsMap = this.batchFindCreativeRedisCache(redisVideoCreativeStatisticsPrefix + param.getVideoId() + "_", adIdList);
|
|
|
+ Double creativeExpSum = this.sumCreativeStatisticExp(creativeStatisticsMap.values());
|
|
|
+ Double videoCreativeExpSum = this.sumCreativeStatisticExp(videoCreativeStatisticsMap.values());
|
|
|
+
|
|
|
+ List<AdRankItem> result = new ArrayList<>(adIdList.size());
|
|
|
+ this.calcScore(result, adIdList, creativeExpSum, videoCreativeExpSum, creativeStatisticsMap, videoCreativeStatisticsMap);
|
|
|
+ result.sort(equalsRandomComparator());
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<AdRankItem> thompsonScorerByExp670(ScoreParam param, List<AdPlatformCreativeDTO> adIdList) {
|
|
|
+ Map<Long, CreativeStatistic> creativeStatisticsMap = this.batchFindCreativeRedisCache(redisCreativeStatisticsPrefix, adIdList);
|
|
|
+ Map<Long, CreativeStatistic> videoCreativeStatisticsMap = this.batchFindCreativeRedisCache(redisVideoCreativeStatisticsPrefix + param.getVideoId() + "_", adIdList);
|
|
|
+
|
|
|
+ Double cidBeta = exp670Param.getOrDefault("cidBeta", 0.0);
|
|
|
+ Double vidCidBeta = exp670Param.getOrDefault("vidCidBeta", 0.0);
|
|
|
+
|
|
|
+ List<AdRankItem> result = new ArrayList<>(adIdList.size());
|
|
|
+ this.calcScore(result, adIdList, cidBeta, vidCidBeta, creativeStatisticsMap, videoCreativeStatisticsMap);
|
|
|
+ result.sort(equalsRandomComparator());
|
|
|
+
|
|
|
+ return result; }
|
|
|
+
|
|
|
+ private void calcScore(List<AdRankItem> result, List<AdPlatformCreativeDTO> adIdList, Double cidBeta, Double vidCidBeta, Map<Long, CreativeStatistic> cidMap, Map<Long, CreativeStatistic> vidCidMap) {
|
|
|
+ for (AdPlatformCreativeDTO dto : adIdList) {
|
|
|
+ AdRankItem item = new AdRankItem();
|
|
|
+ item.setCpa(dto.getCpa());
|
|
|
+ item.setAdId(dto.getCreativeId());
|
|
|
+ CreativeStatistic cidStatistic = cidMap.getOrDefault(dto.getCreativeId(), new CreativeStatistic());
|
|
|
+ CreativeStatistic vidCidStatistic = vidCidMap.getOrDefault(dto.getCreativeId(), new CreativeStatistic());
|
|
|
+ Double score = this.calcThompsonScore(cidStatistic.getDoubleOrder(), cidStatistic.getDoubleExp() + cidBeta, vidCidStatistic.getDoubleOrder(), vidCidStatistic.getDoubleExp() + vidCidBeta, exp669Param);
|
|
|
+ item.setScore(score);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- public void setCpa(String cpa) {
|
|
|
- this.cpa = cpa;
|
|
|
+
|
|
|
+ private Map<Long, CreativeStatistic> batchFindCreativeRedisCache(String keyPrefix, List<AdPlatformCreativeDTO> adIdList) {
|
|
|
+ Map<Long, CreativeStatistic> resultMap = new HashMap<>();
|
|
|
+ for (AdPlatformCreativeDTO dto : adIdList) {
|
|
|
+ String redisKey = keyPrefix + dto.getCreativeId();
|
|
|
+ String value = redisHelper.get(redisKey);
|
|
|
+ if (StringUtils.isNotBlank(value)) {
|
|
|
+ resultMap.put(dto.getCreativeId(), gson.fromJson(value, CreativeStatistic.class));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Double calcThompsonScore(Double cidAlpha, Double cidBeta, Double vidCidAlpha, Double vidCidBeta, Map<String, Double> expParam) {
|
|
|
+ if (cidAlpha == 0 || cidBeta == 0 || vidCidBeta == 0 | vidCidBeta == 0) {
|
|
|
+ return 0.0;
|
|
|
}
|
|
|
|
|
|
+ double cidThompson = this.betaSampler(cidAlpha, cidBeta);
|
|
|
+ double vidCidThompson = this.betaSampler(vidCidAlpha, vidCidBeta);
|
|
|
+ double w1 = expParam.getOrDefault("w1", 1d);
|
|
|
+ double w2 = expParam.getOrDefault("w1", 1d);
|
|
|
+
|
|
|
+ return w1 * vidCidThompson + w2 * cidThompson;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Double sumCreativeStatisticExp(Collection<CreativeStatistic> creativeStatistics) {
|
|
|
+ return creativeStatistics.stream()
|
|
|
+ .map(CreativeStatistic::getExp)
|
|
|
+ .filter(StringUtils::isNotBlank)
|
|
|
+ .collect(Collectors.summarizingDouble(Double::parseDouble))
|
|
|
+ .getSum();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Comparator<AdRankItem> equalsRandomComparator(){
|
|
|
+ return new Comparator<AdRankItem>() {
|
|
|
+ @Override
|
|
|
+ public int compare(AdRankItem o1, AdRankItem o2) {
|
|
|
+ if (o1.getScore() < o2.getScore()) {
|
|
|
+ return 1;
|
|
|
+ } else if (o1.getScore() > o2.getScore()) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return random.nextInt(20) - 10;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ class CreativeStatistic{
|
|
|
+
|
|
|
private String exp;
|
|
|
private String click;
|
|
|
private String order;
|
|
|
private String cpa;
|
|
|
|
|
|
+ public CreativeStatistic() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCpa(String cpa) {
|
|
|
+ this.cpa = cpa;
|
|
|
+ }
|
|
|
+
|
|
|
public String getExp() {
|
|
|
if (exp == null || "".equals(exp)) {
|
|
|
return "0";
|
|
@@ -228,6 +322,13 @@ public class VideoAdThompsonScorerV2 {
|
|
|
return exp;
|
|
|
}
|
|
|
|
|
|
+ public Double getDoubleExp() {
|
|
|
+ if (StringUtils.isBlank(exp)) {
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+ return Double.parseDouble(exp);
|
|
|
+ }
|
|
|
+
|
|
|
public void setExp(String exp) {
|
|
|
this.exp = exp;
|
|
|
}
|
|
@@ -239,6 +340,13 @@ public class VideoAdThompsonScorerV2 {
|
|
|
return click;
|
|
|
}
|
|
|
|
|
|
+ public Double getDoubleClick(){
|
|
|
+ if (StringUtils.isBlank(click)){
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+ return Double.parseDouble(click);
|
|
|
+ }
|
|
|
+
|
|
|
public void setClick(String click) {
|
|
|
this.click = click;
|
|
|
}
|
|
@@ -250,6 +358,13 @@ public class VideoAdThompsonScorerV2 {
|
|
|
return order;
|
|
|
}
|
|
|
|
|
|
+ public Double getDoubleOrder(){
|
|
|
+ if (StringUtils.isBlank(order)){
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+ return Double.parseDouble(order);
|
|
|
+ }
|
|
|
+
|
|
|
public void setOrder(String order) {
|
|
|
this.order = order;
|
|
|
}
|
|
@@ -260,6 +375,13 @@ public class VideoAdThompsonScorerV2 {
|
|
|
}
|
|
|
return cpa;
|
|
|
}
|
|
|
+
|
|
|
+ public Double getDoubleCpa() {
|
|
|
+ if (StringUtils.isBlank(cpa)) {
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+ return Double.parseDouble(cpa);
|
|
|
+ }
|
|
|
}
|
|
|
double betaSampler(double alpha, double beta) {
|
|
|
BetaDistribution betaSample = new BetaDistribution(alpha, beta);
|
|
@@ -281,4 +403,13 @@ public class VideoAdThompsonScorerV2 {
|
|
|
public void setExp666Param(String str){
|
|
|
this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
}
|
|
|
+
|
|
|
+ @Value("${ad.engine.new.thompson.exp.V2.666:{}}")
|
|
|
+ public void setExp669Param(String str){
|
|
|
+ this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
+ }
|
|
|
+ @Value("${ad.engine.new.thompson.exp.V2.666:{}}")
|
|
|
+ public void setExp670Param(String str){
|
|
|
+ this.exp663Param=gson.fromJson(str,Map.class);
|
|
|
+ }
|
|
|
}
|