Browse Source

Merge branch 'feature_gufengshou_20240311_ad_immersion_with_head_ad' into test

gufengshou1 1 năm trước cách đây
mục cha
commit
a2acc9dda7

+ 33 - 33
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/container/ThresholdModelContainer.java

@@ -30,7 +30,7 @@ public class ThresholdModelContainer {
     private double position;
 
     public static Map<String,ThresholdPredictModel> modelMap=new HashMap<>();
-    public static Map<Integer,MergingDigest> mergingDigestMap=new HashMap<>();
+//    public static Map<Integer,MergingDigest> mergingDigestMap=new HashMap<>();
 
     private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
     @PostConstruct
@@ -40,21 +40,21 @@ public class ThresholdModelContainer {
             modelMap.put(model.getName(), model);
         });
         //只关注重点app
-        mergingDigestMap.put(0, new MergingDigest(10000));
-        mergingDigestMap.put(3, new MergingDigest(10000));
-        mergingDigestMap.put(4, new MergingDigest(10000));
-        mergingDigestMap.put(5, new MergingDigest(10000));
-        mergingDigestMap.put(21, new MergingDigest(10000));
-        final Runnable task = new Runnable() {
-            public void run() {
-                try {
-                    printDigestThreshold();
-                }catch (Exception e){
-                    e.printStackTrace();
-                }
-            }
-        };
-        scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES); // 10分钟
+//        mergingDigestMap.put(0, new MergingDigest(10000));
+//        mergingDigestMap.put(3, new MergingDigest(10000));
+//        mergingDigestMap.put(4, new MergingDigest(10000));
+//        mergingDigestMap.put(5, new MergingDigest(10000));
+//        mergingDigestMap.put(21, new MergingDigest(10000));
+//        final Runnable task = new Runnable() {
+//            public void run() {
+//                try {
+//                    printDigestThreshold();
+//                }catch (Exception e){
+//                    e.printStackTrace();
+//                }
+//            }
+//        };
+//        scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES); // 10分钟
     }
 
     public static ThresholdPredictModel getThresholdPredictModel(String modelName){
@@ -65,25 +65,25 @@ public class ThresholdModelContainer {
         return modelMap.get("basic");
     }
 
-    public static void mergingDigestAddScore(Integer appType,Double score){
-        mergingDigestMap.getOrDefault(appType,new MergingDigest(1)).add(score);
-    }
-
-    public static double getThresholdByTDigest(Integer appType,Double sortPosition){
-        return  mergingDigestMap.getOrDefault(appType,new MergingDigest(1)).quantile(sortPosition);
-    }
+//    public static void mergingDigestAddScore(Integer appType,Double score){
+//        mergingDigestMap.getOrDefault(appType,new MergingDigest(1)).add(score);
+//    }
 
-    public void printDigestThreshold(){
-        try {
-            for(Map.Entry<Integer,MergingDigest> entry:mergingDigestMap.entrySet()){
-                log.info("svc=printDigestThreshold modelName=modelV2 appType={} mergingDigestThreshold={}"
-                        , entry.getKey(),entry.getValue().quantile(position));
-            }
+//    public static double getThresholdByTDigest(Integer appType,Double sortPosition){
+//        return  mergingDigestMap.getOrDefault(appType,new MergingDigest(1)).quantile(sortPosition);
+//    }
 
-        }catch (Exception e){
-            e.printStackTrace();
-        }
-    }
+//    public void printDigestThreshold(){
+//        try {
+//            for(Map.Entry<Integer,MergingDigest> entry:mergingDigestMap.entrySet()){
+//                log.info("svc=printDigestThreshold modelName=modelV2 appType={} mergingDigestThreshold={}"
+//                        , entry.getKey(),entry.getValue().quantile(position));
+//            }
+//
+//        }catch (Exception e){
+//            e.printStackTrace();
+//        }
+//    }
 
 
 //    public static void main(String[] args){

+ 9 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/impl/PredictModelServiceImpl.java

@@ -56,6 +56,9 @@ public class PredictModelServiceImpl implements PredictModelService {
     @Value("${ad.predict.share0.exp.code:000}")
     private String adPredictNoShareUserExpCode;
 
+    @Value("${ad.predict.immersion.exp.code:607}")
+    private String adPredictImmersionExpCode;
+
     @Value("${ad.predict.param.testIds:0}")
     private String testIds;
     @Value("${ad.predict.without.ad.video_ids:0}")
@@ -212,6 +215,12 @@ public class PredictModelServiceImpl implements PredictModelService {
                     .predict(modelParam);
         } else if(expCodes.contains("599")){
 //        if(randomModelExpCode!=null){
+//            modelParam.addUserExtraFuture("randomModelKey",ExpCodeEnum.valueOfExpCode(randomModelExpCode).getRandomModelKey());
+            result = ThresholdModelContainer.
+                    getThresholdPredictModel("random")
+                    .predict(modelParam);
+        }else if(expCodes.contains(adPredictImmersionExpCode)){
+//        if(randomModelExpCode!=null){
 //            modelParam.addUserExtraFuture("randomModelKey",ExpCodeEnum.valueOfExpCode(randomModelExpCode).getRandomModelKey());
             result = ThresholdModelContainer.
                     getThresholdPredictModel("random")

+ 44 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/model/threshold/RandomImmersionPredictModel.java

@@ -0,0 +1,44 @@
+package com.tzld.piaoquan.ad.engine.service.predict.model.threshold;
+
+import com.tzld.piaoquan.ad.engine.service.predict.container.RandWContainer;
+import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Component
+public class RandomImmersionPredictModel extends ThresholdPredictModel {
+    private final static Logger log = LoggerFactory.getLogger(RandomImmersionPredictModel.class);
+
+    @Override
+    String initName() {
+        return "immersion";
+    }
+
+
+    @Override
+    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
+        int hash=modelParam.getMid().hashCode();
+        hash=hash<0?-hash:hash;
+        double score=(hash+ RandWContainer.getRandW())%100/100d;
+        double threshold=Double.parseDouble(
+                modelParam.getExtraParam().getOrDefault(modelParam.getAppType()+"_"+modelParam.getUserExtraFuture("shareType").toString().replace("return","").replace("mids",""),-1
+                ).toString());
+        if(threshold<0d){
+            threshold=Double.parseDouble(
+                    modelParam.getExtraParam().getOrDefault("default_threshold","0.5")
+                    .toString());
+        }
+        Map<String, Object> result = new HashMap<>();
+        result.put("ad_predict", score<threshold?2:1);
+        result.put("score", score);
+        result.put("threshold", threshold);
+        result.put("model", "random");
+
+        return result;
+    }
+
+}

+ 1 - 2
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/model/threshold/ScoreV2ThresholdPredictModel.java

@@ -8,7 +8,6 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.util.CommonCollectionUtils;
 import com.tzld.piaoquan.ad.engine.commons.util.TimerWatchUtil;
 import com.tzld.piaoquan.ad.engine.service.predict.container.PredictPidContainer;
-import com.tzld.piaoquan.ad.engine.service.predict.container.ThresholdModelContainer;
 import com.tzld.piaoquan.ad.engine.service.predict.impl.PredictModelServiceImpl;
 import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
 import com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
@@ -115,7 +114,7 @@ public class ScoreV2ThresholdPredictModel extends ThresholdPredictModel {
             adPredict = 2;
         }
         if(maxItem != null){
-            ThresholdModelContainer.mergingDigestAddScore(modelParam.getAppType(),maxItem.getScore());
+//            ThresholdModelContainer.mergingDigestAddScore(modelParam.getAppType(),maxItem.getScore());
             //删除多余打印
             maxItem.setItemFeature(null);
             maxItem.setLrSampleString(null);

+ 70 - 14
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/container/PidLambdaContainer.java

@@ -48,13 +48,13 @@ public class PidLambdaContainer {
     @Value("${model.oss.pid.filename.dCpa:pid/dCpa}")
     String dCpaFileName = "";
 
-    @Value("${ad.model.pid.kp:0.5}")
+    @Value("${ad.model.pid.kp:0.4}")
     Double kp = 0d;
 
-    @Value("${ad.model.pid.ki:0.05}")
+    @Value("${ad.model.pid.ki:0.4}")
     Double ki = 0d;
 
-    @Value("${ad.model.pid.kd:0.005}")
+    @Value("${ad.model.pid.kd:0.2}")
     Double kd = 0d;
 
     OSS client;
@@ -105,18 +105,10 @@ public class PidLambdaContainer {
 //                    Double lambdaNew=lambdaCache.getOrDefault(creativeId,0d)+
 //                            kp*Double.parseDouble(cols[1])+ki*Double.parseDouble(cols[2])+kd*Double.parseDouble(cols[3]);
 //                  Ecpm = CPA * pctr * pcvr * bid * lambad
-//                  Lambad = real_ctcvr / p_ctcvr (按上一个小时统计这两个值)
-                    Double pctr=Double.parseDouble(cols[1]);
-                    Double pcvr=Double.parseDouble(cols[2]);
-                    Double ctr=Double.parseDouble(cols[3]);
-                    Double cvr=Double.parseDouble(cols[4]);
-                    Double lambdaNew=null;
-                    if((pctr*pcvr)==0d||ctr*cvr==0d){
-                        lambdaNew=1d;
-                    }else {
-                        lambdaNew=cvr*ctr/(pctr*pctr);
-                    }
+//0.4* (昨天realctcvr / 昨天pctcvr ) + 0.4 * (当天累计realctcvr / 当天的pctcvr ) + 0.2 * (最近一个小时realctcvr / 最近一个小时pctcvr)
+                    Double lambdaNew=kp*getYesterdayLambda(cols)+ki*getTodayLambda(cols)+kd*getLatestHourLambda(cols);
                     temp.put(creativeId,lambdaNew);
+                    log.error("svc=calNewLambda creativeId={} lambdaNew={}", creativeId,lambdaNew);
                 }catch (Exception e){
                     e.printStackTrace();
                 }
@@ -171,4 +163,68 @@ public class PidLambdaContainer {
             return 1d;
         }
     }
+
+    private  Double getLatestHourLambda(String[] cols){
+        try {
+            Double pctr=Double.parseDouble(cols[1]);
+            Double pcvr=Double.parseDouble(cols[2]);
+            Double ctr=Double.parseDouble(cols[3]);
+            Double cvr=Double.parseDouble(cols[4]);
+            Double lambdaNew=null;
+            if((pctr*pcvr)==0d||ctr*cvr==0d){
+                lambdaNew=1d;
+            }else {
+                lambdaNew=cvr*ctr/(pctr*pctr);
+            }
+            //避免异常数据
+            if(lambdaNew>5){
+                lambdaNew=1d;
+            }
+            return lambdaNew;
+        }catch (Exception e){
+            return 1d;
+        }
+    }
+
+    private  Double getTodayLambda(String[] cols){
+        try {
+            Double pctr=Double.parseDouble(cols[9]);
+            Double pcvr=Double.parseDouble(cols[10]);
+            Double ctr=Double.parseDouble(cols[11]);
+            Double cvr=Double.parseDouble(cols[12]);
+            Double lambdaNew=null;
+            if((pctr*pcvr)==0d||ctr*cvr==0d){
+                lambdaNew=1d;
+            }else {
+                lambdaNew=cvr*ctr/(pctr*pctr);
+            }
+            if(lambdaNew>3){
+                lambdaNew=1d;
+            }
+            return lambdaNew;
+        }catch (Exception e){
+            return 1d;
+        }
+    }
+
+    private  Double getYesterdayLambda(String[] cols){
+        try {
+            Double pctr=Double.parseDouble(cols[5]);
+            Double pcvr=Double.parseDouble(cols[6]);
+            Double ctr=Double.parseDouble(cols[7]);
+            Double cvr=Double.parseDouble(cols[8]);
+            Double lambdaNew=null;
+            if((pctr*pcvr)==0d||ctr*cvr==0d){
+                lambdaNew=1d;
+            }else {
+                lambdaNew=cvr*ctr/(pctr*pctr);
+            }
+            if(lambdaNew>3){
+                lambdaNew=1d;
+            }
+            return lambdaNew;
+        }catch (Exception e){
+            return 1d;
+        }
+    }
 }

+ 2 - 5
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/impl/RankServiceImpl.java

@@ -173,12 +173,9 @@ public class RankServiceImpl implements RankService {
         result.setPcvr(topItem.getCvr());
         result.setCreativeCode(groupMap.get(topItem.getAdId()+"").get(0).getCreativeCode());
         double realECpm=0d;
-        if(rankResult.size()<=1){
             //经验值 待定
-            realECpm=topItem.getEcpm1()*0.9;
-        }else {
-            realECpm=rankResult.get(1).getEcpm1();
-        }
+        realECpm=topItem.getEcpm1();
+
         if(realECpm>cpmMax/1000d){
             realECpm=cpmMax/1000d;
         }