|  | @@ -42,10 +42,10 @@ public class PidLambdaContainer {
 | 
											
												
													
														|  |      @Value("${model.oss.bucketName:art-recommend}")
 |  |      @Value("${model.oss.bucketName:art-recommend}")
 | 
											
												
													
														|  |      String bucketName = "";
 |  |      String bucketName = "";
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    @Value("${model.oss.pid.filename.lambda:pid/lambda}")
 |  | 
 | 
											
												
													
														|  | 
 |  | +    @Value("${model.oss.pid.filename.lambda:pid/lambda.txt}")
 | 
											
												
													
														|  |      String lambdaFileName = "";
 |  |      String lambdaFileName = "";
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    @Value("${model.oss.pid.filename.dCpa:pid/dCpa}")
 |  | 
 | 
											
												
													
														|  | 
 |  | +    @Value("${model.oss.pid.v2.filename.dCpa:pid/dCpaV2.txt}")
 | 
											
												
													
														|  |      String dCpaFileName = "";
 |  |      String dCpaFileName = "";
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      @Value("${ad.model.pid.kp:0.4}")
 |  |      @Value("${ad.model.pid.kp:0.4}")
 | 
											
										
											
												
													
														|  | @@ -57,9 +57,14 @@ public class PidLambdaContainer {
 | 
											
												
													
														|  |      @Value("${ad.model.pid.kd:0.2}")
 |  |      @Value("${ad.model.pid.kd:0.2}")
 | 
											
												
													
														|  |      Double kd = 0d;
 |  |      Double kd = 0d;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +    @Value("${ad.model.pid.v2.lambda.max:5.0}")
 | 
											
												
													
														|  | 
 |  | +    Double maxLambda = 0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Value("${ad.model.pid.v2.lambda.min:0.8}")
 | 
											
												
													
														|  | 
 |  | +    Double minLambda = 0d;
 | 
											
												
													
														|  |      OSS client;
 |  |      OSS client;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    private static ConcurrentHashMap<Long,Double>  lambdaCache=new ConcurrentHashMap<>();
 |  | 
 | 
											
												
													
														|  | 
 |  | +    private static ConcurrentHashMap<Long, V1CacheItem>  lambdaCache=new ConcurrentHashMap<>();
 | 
											
												
													
														|  |      private Date cacheDate;
 |  |      private Date cacheDate;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      @PostConstruct
 |  |      @PostConstruct
 | 
											
										
											
												
													
														|  | @@ -97,18 +102,43 @@ public class PidLambdaContainer {
 | 
											
												
													
														|  |              InputStreamReader isr=new InputStreamReader(is);
 |  |              InputStreamReader isr=new InputStreamReader(is);
 | 
											
												
													
														|  |              BufferedReader bufferedReader = new BufferedReader(isr);
 |  |              BufferedReader bufferedReader = new BufferedReader(isr);
 | 
											
												
													
														|  |              String line = null;
 |  |              String line = null;
 | 
											
												
													
														|  | -            ConcurrentHashMap<Long,Double>  temp=new ConcurrentHashMap<>();
 |  | 
 | 
											
												
													
														|  | 
 |  | +            ConcurrentHashMap<Long, V1CacheItem>  temp=new ConcurrentHashMap<>();
 | 
											
												
													
														|  | 
 |  | +            Double conversion=0d;
 | 
											
												
													
														|  | 
 |  | +            Double cpa=0d;
 | 
											
												
													
														|  | 
 |  | +            Double realCost=0d;
 | 
											
												
													
														|  | 
 |  | +            Double latestRealCPA=0d;
 | 
											
												
													
														|  | 
 |  | +            double et=0d;
 | 
											
												
													
														|  | 
 |  | +            double sumE=0d;
 | 
											
												
													
														|  | 
 |  | +            double ve=0d;
 | 
											
												
													
														|  |              while ((line = bufferedReader.readLine()) != null){
 |  |              while ((line = bufferedReader.readLine()) != null){
 | 
											
												
													
														|  |                  try {
 |  |                  try {
 | 
											
												
													
														|  |                      String[] cols=line.split(",");
 |  |                      String[] cols=line.split(",");
 | 
											
												
													
														|  |                      Long creativeId=Long.parseLong(cols[0]);
 |  |                      Long creativeId=Long.parseLong(cols[0]);
 | 
											
												
													
														|  | -//                    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
 |  | 
 | 
											
												
													
														|  | -//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.info("svc=calNewLambda creativeId={} lambdaNew={}", creativeId,lambdaNew);
 |  | 
 | 
											
												
													
														|  | 
 |  | +                    V1CacheItem cacheItem=lambdaCache.getOrDefault(creativeId,new V1CacheItem(creativeId));
 | 
											
												
													
														|  | 
 |  | +                    conversion=Double.parseDouble(cols[1]);
 | 
											
												
													
														|  | 
 |  | +                    cpa=Double.parseDouble(cols[2]);
 | 
											
												
													
														|  | 
 |  | +                    realCost=Double.parseDouble(cols[3]);
 | 
											
												
													
														|  | 
 |  | +                    Double lambdaNew=1d;
 | 
											
												
													
														|  | 
 |  | +                    if(conversion<5d){
 | 
											
												
													
														|  | 
 |  | +                        conversion=1d;
 | 
											
												
													
														|  | 
 |  | +                    }
 | 
											
												
													
														|  | 
 |  | +                    latestRealCPA=realCost/conversion;
 | 
											
												
													
														|  | 
 |  | +                    et=cpa-latestRealCPA;
 | 
											
												
													
														|  | 
 |  | +                    sumE=cacheItem.sumError+et;
 | 
											
												
													
														|  | 
 |  | +                    ve=et-(cpa-cacheItem.latestRealCpa);
 | 
											
												
													
														|  | 
 |  | +                    lambdaNew=kp*et+ki*sumE+kd*ve;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +                    if(lambdaNew>cpa) {
 | 
											
												
													
														|  | 
 |  | +                        lambdaNew=cpa-1;
 | 
											
												
													
														|  | 
 |  | +                    }
 | 
											
												
													
														|  | 
 |  | +                    cacheItem.lambda=lambdaNew;
 | 
											
												
													
														|  | 
 |  | +                    cacheItem.latestRealCpa=latestRealCPA;
 | 
											
												
													
														|  | 
 |  | +                    cacheItem.sumError=sumE;
 | 
											
												
													
														|  | 
 |  | +                    cacheItem.latestConv=conversion;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +                    temp.put(creativeId,cacheItem);
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +                    log.info("svc=calNewLambdaV2 creativeId={} lambdaNew={}", creativeId,lambdaNew);
 | 
											
												
													
														|  |                  }catch (Exception e){
 |  |                  }catch (Exception e){
 | 
											
												
													
														|  |                      e.printStackTrace();
 |  |                      e.printStackTrace();
 | 
											
												
													
														|  |                  }
 |  |                  }
 | 
											
										
											
												
													
														|  | @@ -116,7 +146,7 @@ public class PidLambdaContainer {
 | 
											
												
													
														|  |              lambdaCache.clear();
 |  |              lambdaCache.clear();
 | 
											
												
													
														|  |              lambdaCache=temp;
 |  |              lambdaCache=temp;
 | 
											
												
													
														|  |          }catch (Exception e){
 |  |          }catch (Exception e){
 | 
											
												
													
														|  | -            log.error("svc=calNewLambda status=failed error={}", Arrays.toString(e.getStackTrace()));
 |  | 
 | 
											
												
													
														|  | 
 |  | +            log.error("svc=calNewLambdaV2 status=failed error={}", Arrays.toString(e.getStackTrace()));
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -130,7 +160,7 @@ public class PidLambdaContainer {
 | 
											
												
													
														|  |              this.cacheDate= copyObjectResult.getLastModified();
 |  |              this.cacheDate= copyObjectResult.getLastModified();
 | 
											
												
													
														|  |              client.deleteObject(bucketName, tempFile);
 |  |              client.deleteObject(bucketName, tempFile);
 | 
											
												
													
														|  |          }catch (Exception e){
 |  |          }catch (Exception e){
 | 
											
												
													
														|  | -            log.error("svc=writeLambdaFileToOss status=failed error={}", Arrays.toString(e.getStackTrace()));
 |  | 
 | 
											
												
													
														|  | 
 |  | +            log.error("svc=writeLambdaV2FileToOss status=failed error={}", Arrays.toString(e.getStackTrace()));
 | 
											
												
													
														|  |              e.printStackTrace();
 |  |              e.printStackTrace();
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
										
											
												
													
														|  | @@ -148,83 +178,41 @@ public class PidLambdaContainer {
 | 
											
												
													
														|  |              while ((line=bufferedReader.readLine())!=null){
 |  |              while ((line=bufferedReader.readLine())!=null){
 | 
											
												
													
														|  |                  builder.append(line);
 |  |                  builder.append(line);
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  | -            lambdaCache=JSONObject.parseObject(builder.toString(),new TypeReference<ConcurrentHashMap<Long,Double>>(){});
 |  | 
 | 
											
												
													
														|  | 
 |  | +            lambdaCache=JSONObject.parseObject(builder.toString(),new TypeReference<ConcurrentHashMap<Long, V1CacheItem>>(){});
 | 
											
												
													
														|  |              this.cacheDate=object.getObjectMetadata().getLastModified();
 |  |              this.cacheDate=object.getObjectMetadata().getLastModified();
 | 
											
												
													
														|  |          }catch (Exception e){
 |  |          }catch (Exception e){
 | 
											
												
													
														|  | -            log.error("svc=loadLambdaFile status=failed error={}", Arrays.toString(e.getStackTrace()));
 |  | 
 | 
											
												
													
														|  | 
 |  | +            log.error("svc=loadLambdaV2File status=failed error={}", Arrays.toString(e.getStackTrace()));
 | 
											
												
													
														|  |              e.printStackTrace();
 |  |              e.printStackTrace();
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      public static Double getPidLambda(Long creativeId){
 |  |      public static Double getPidLambda(Long creativeId){
 | 
											
												
													
														|  |          try {
 |  |          try {
 | 
											
												
													
														|  | -            return lambdaCache.getOrDefault(creativeId,1d);
 |  | 
 | 
											
												
													
														|  | 
 |  | +            return lambdaCache.getOrDefault(creativeId,new V1CacheItem(creativeId)).lambda;
 | 
											
												
													
														|  |          }catch (Exception e){
 |  |          }catch (Exception e){
 | 
											
												
													
														|  |              return 1d;
 |  |              return 1d;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    private  Double getLatestHourLambda(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=1d;
 |  | 
 | 
											
												
													
														|  | -            if((pctr*pcvr)==0d||ctr*cvr==0d){
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -            }else {
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=cvr*ctr/(pctr*pcvr);
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            //避免异常数据
 |  | 
 | 
											
												
													
														|  | -            if(lambdaNew>5){
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            return lambdaNew;
 |  | 
 | 
											
												
													
														|  | -        }catch (Exception e){
 |  | 
 | 
											
												
													
														|  | -            return 1d;
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    private  Double getTodayLambda(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=1d;
 |  | 
 | 
											
												
													
														|  | -            if((pctr*pcvr)==0d||ctr*cvr==0d){
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -            }else {
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=cvr*ctr/(pctr*pcvr);
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            if(lambdaNew>3){
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            return lambdaNew;
 |  | 
 | 
											
												
													
														|  | -        }catch (Exception e){
 |  | 
 | 
											
												
													
														|  | -            return 1d;
 |  | 
 | 
											
												
													
														|  | 
 |  | +    public static class V1CacheItem{
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public V1CacheItem(){
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    private  Double getYesterdayLambda(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=1d;
 |  | 
 | 
											
												
													
														|  | -            if((pctr*pcvr)==0d||ctr*cvr==0d){
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -            }else {
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=cvr*ctr/(pctr*pcvr);
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            if(lambdaNew>3){
 |  | 
 | 
											
												
													
														|  | -                lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            return lambdaNew;
 |  | 
 | 
											
												
													
														|  | -        }catch (Exception e){
 |  | 
 | 
											
												
													
														|  | -            return 1d;
 |  | 
 | 
											
												
													
														|  | 
 |  | +        public V1CacheItem(Long creativeId){
 | 
											
												
													
														|  | 
 |  | +            this.creativeId=creativeId;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public Long creativeId;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public double lambda=1d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public double latestConv=0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public double sumError=0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public double latestRealCpa=0d;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  }
 |  |  }
 |