Browse Source

增加算法保量

xueyiming 2 days ago
parent
commit
8013032142

+ 5 - 0
ad-engine-commons/src/main/java/com/tzld/piaoquan/ad/engine/commons/redis/AdRedisHelper.java

@@ -7,6 +7,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
@@ -51,4 +52,8 @@ public class AdRedisHelper {
         return Boolean.TRUE.equals(adRedisTemplate.hasKey(key));
     }
 
+    public List<String> mget(List<String> keys) {
+        return adRedisTemplate.opsForValue().multiGet(keys);
+    }
+
 }

+ 19 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/entity/GuaranteeView.java

@@ -0,0 +1,19 @@
+package com.tzld.piaoquan.ad.engine.service.entity;
+
+import lombok.Data;
+import lombok.ToString;
+
+@Data
+@ToString
+public class GuaranteeView {
+
+    private String adrId;
+
+    private Integer adrAlogViewNum;
+
+    private Integer allViewNum;
+
+    private Integer adrGuaranteeViewNum;
+
+    private Double guaranteeRate;
+}

+ 19 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBasic.java

@@ -2,6 +2,7 @@ package com.tzld.piaoquan.ad.engine.service.score.strategy;
 
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.commons.enums.RedisPrefixEnum;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
 import com.tzld.piaoquan.ad.engine.commons.redis.AdRedisHelper;
@@ -98,4 +99,22 @@ public abstract class RankStrategyBasic implements RankStrategy {
         }
 
     }
+
+    protected double getGuaranteeWeight(GuaranteeView guaranteeView) {
+        double guaranteeWeight;
+        if (guaranteeView.getAdrGuaranteeViewNum() == null || guaranteeView.getAdrGuaranteeViewNum() == 0
+                || guaranteeView.getGuaranteeRate() == null || guaranteeView.getGuaranteeRate() == 0.0) {
+            guaranteeWeight = 1.0;
+        } else {
+            if (guaranteeView.getAdrAlogViewNum() != null &&
+                    guaranteeView.getAdrGuaranteeViewNum() <= guaranteeView.getAdrAlogViewNum()) {
+                guaranteeWeight = 0.0;
+            } else {
+                int allViewNum = guaranteeView.getAllViewNum() == null || guaranteeView.getAllViewNum() == 0 ? 1 : guaranteeView.getAllViewNum();
+                int adrAlogViewNum = guaranteeView.getAdrAlogViewNum() == null || guaranteeView.getAdrAlogViewNum() == 0 ? 1 : guaranteeView.getAdrAlogViewNum();
+                guaranteeWeight = guaranteeView.getGuaranteeRate() * allViewNum / adrAlogViewNum;
+            }
+        }
+        return guaranteeWeight;
+    }
 }

+ 36 - 3
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy688.java

@@ -1,10 +1,12 @@
 package com.tzld.piaoquan.ad.engine.service.score.strategy;
 
+import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 import com.tzld.piaoquan.ad.engine.commons.util.*;
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
@@ -42,6 +44,9 @@ public class RankStrategyBy688 extends RankStrategyBasic {
     @Value("${word2vec.exp:694}")
     private String word2vecExp;
 
+    @Value("${guarantee.exp:742}")
+    private String guaranteeExp;
+
     // FIXME(zhoutian): 可能需要独立配置
     @ApolloJsonValue("${rank.score.weight.680:{}}")
     private Map<String, Double> weightMap;
@@ -49,6 +54,9 @@ public class RankStrategyBy688 extends RankStrategyBasic {
     @ApolloJsonValue("${rank.score.neg_sample_rate:0.01}")
     Double negSampleRate;
 
+    @ApolloJsonValue("${alpha:1.0}")
+    Double alpha;
+
     Set<String> sparseFeatureSet;
 
     @PostConstruct
@@ -150,6 +158,17 @@ public class RankStrategyBy688 extends RankStrategyBasic {
                 userFeatureMap.put("root_source_channel", rootSourceChannel);
             }
         }
+        List<String> adVerIdList = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getAdVerId).distinct().collect(Collectors.toList());
+        List<String> values = new ArrayList<>();
+        Map<String, GuaranteeView> map = new HashMap<>();
+        for (int i = 0; i < adVerIdList.size(); i++) {
+            String value = values.get(i);
+            if (value != null) {
+                GuaranteeView guaranteeView = JSONObject.parseObject(value, GuaranteeView.class);
+                map.put(adVerIdList.get(i), guaranteeView);
+            }
+        }
+
 
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
@@ -176,8 +195,12 @@ public class RankStrategyBy688 extends RankStrategyBasic {
                     } else {
                         adRankItem.getExt().put("isApi", "1");
                     }
-
+                    GuaranteeView guaranteeView = map.get(dto.getAdVerId());
+                    double guaranteeWeight = getGuaranteeWeight(guaranteeView);
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
+                    adRankItem.getExt().put("alpha", alpha);
+                    adRankItem.getExt().put("guaranteeView", guaranteeView.toString());
+                    adRankItem.getExt().put("guaranteeWeight", guaranteeWeight);
 
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
@@ -290,8 +313,18 @@ public class RankStrategyBy688 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
-
+            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+                double guaranteeScoreCoefficient;
+                if (item.getExt().get("guaranteeWeight") == null) {
+                    guaranteeScoreCoefficient = 1.0;
+                } else {
+                    guaranteeScoreCoefficient = Math.pow((double) item.getExt().get("guaranteeWeight"), alpha);
+                }
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+                item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
+            } else {
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            }
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);