|  | @@ -9,6 +9,7 @@ import com.aliyun.oss.common.auth.DefaultCredentialProvider;
 | 
											
												
													
														|  |  import com.aliyun.oss.model.CopyObjectResult;
 |  |  import com.aliyun.oss.model.CopyObjectResult;
 | 
											
												
													
														|  |  import com.aliyun.oss.model.OSSObject;
 |  |  import com.aliyun.oss.model.OSSObject;
 | 
											
												
													
														|  |  import com.aliyun.oss.model.PutObjectResult;
 |  |  import com.aliyun.oss.model.PutObjectResult;
 | 
											
												
													
														|  | 
 |  | +import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
 | 
											
												
													
														|  |  import org.slf4j.Logger;
 |  |  import org.slf4j.Logger;
 | 
											
												
													
														|  |  import org.slf4j.LoggerFactory;
 |  |  import org.slf4j.LoggerFactory;
 | 
											
												
													
														|  |  import org.springframework.beans.factory.annotation.Value;
 |  |  import org.springframework.beans.factory.annotation.Value;
 | 
											
										
											
												
													
														|  | @@ -52,6 +53,15 @@ public class PidLambdaV2Container {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      @Value("${ad.model.pid.v2.lambda.min:0.8}")
 |  |      @Value("${ad.model.pid.v2.lambda.min:0.8}")
 | 
											
												
													
														|  |      Double minLambda = 0d;
 |  |      Double minLambda = 0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Value("${ad.model.pid.v2.kp:1.2}")
 | 
											
												
													
														|  | 
 |  | +    Double kp = 0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Value("${ad.model.pid.v2.ki:0.4}")
 | 
											
												
													
														|  | 
 |  | +    Double ki = 0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Value("${ad.model.pid.v2.kd:0.1}")
 | 
											
												
													
														|  | 
 |  | +    Double kd = 0d;
 | 
											
												
													
														|  |      OSS client;
 |  |      OSS client;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      private static ConcurrentHashMap<Long,CacheItem>  lambdaCache=new ConcurrentHashMap<>();
 |  |      private static ConcurrentHashMap<Long,CacheItem>  lambdaCache=new ConcurrentHashMap<>();
 | 
											
										
											
												
													
														|  | @@ -92,49 +102,42 @@ public class PidLambdaV2Container {
 | 
											
												
													
														|  |              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,CacheItem>  temp=new ConcurrentHashMap<>();
 |  | 
 | 
											
												
													
														|  | 
 |  | +            ConcurrentHashMap<Long, CacheItem>  temp=new ConcurrentHashMap<>();
 | 
											
												
													
														|  |              Double conversion=0d;
 |  |              Double conversion=0d;
 | 
											
												
													
														|  |              Double cpa=0d;
 |  |              Double cpa=0d;
 | 
											
												
													
														|  |              Double realCost=0d;
 |  |              Double realCost=0d;
 | 
											
												
													
														|  | -            Double yesterdayConv=0d;
 |  | 
 | 
											
												
													
														|  | -            Double yesterdayCpa=0d;
 |  | 
 | 
											
												
													
														|  | -            Double yesterdayRealCost=0d;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | 
 |  | +            Double latestRealCPA=0d;
 | 
											
												
													
														|  | 
 |  | +            double sumE=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]);
 | 
											
												
													
														|  |                      CacheItem cacheItem=lambdaCache.getOrDefault(creativeId,new CacheItem(creativeId));
 |  |                      CacheItem cacheItem=lambdaCache.getOrDefault(creativeId,new CacheItem(creativeId));
 | 
											
												
													
														|  | 
 |  | +                    if(DateUtils.getCurrentHour()<=8){
 | 
											
												
													
														|  | 
 |  | +                        temp.put(creativeId,cacheItem);
 | 
											
												
													
														|  | 
 |  | +                        continue;
 | 
											
												
													
														|  | 
 |  | +                    }
 | 
											
												
													
														|  |                      conversion=Double.parseDouble(cols[1]);
 |  |                      conversion=Double.parseDouble(cols[1]);
 | 
											
												
													
														|  |                      cpa=Double.parseDouble(cols[2]);
 |  |                      cpa=Double.parseDouble(cols[2]);
 | 
											
												
													
														|  |                      realCost=Double.parseDouble(cols[3]);
 |  |                      realCost=Double.parseDouble(cols[3]);
 | 
											
												
													
														|  | -                    yesterdayConv=Double.parseDouble(cols[4]);
 |  | 
 | 
											
												
													
														|  | -                    yesterdayCpa=Double.parseDouble(cols[5]);
 |  | 
 | 
											
												
													
														|  | -                    yesterdayRealCost=Double.parseDouble(cols[6]);
 |  | 
 | 
											
												
													
														|  | -                    Double lambdaNew=1d;
 |  | 
 | 
											
												
													
														|  | -                    if((conversion+yesterdayConv)!=0){
 |  | 
 | 
											
												
													
														|  | -                        if((realCost*yesterdayRealCost)!=0){
 |  | 
 | 
											
												
													
														|  | -                            double yesterdayW=yesterdayConv/(yesterdayConv+2*conversion);
 |  | 
 | 
											
												
													
														|  | -                            lambdaNew=(yesterdayW*yesterdayConv*yesterdayCpa)/yesterdayRealCost
 |  | 
 | 
											
												
													
														|  | -                                    +(1-yesterdayW)*(cpa*conversion)/realCost;
 |  | 
 | 
											
												
													
														|  | -                        }else if(realCost!=0){
 |  | 
 | 
											
												
													
														|  | -                            lambdaNew=(cpa*conversion)/realCost;
 |  | 
 | 
											
												
													
														|  | -                        }else if(yesterdayRealCost!=0){
 |  | 
 | 
											
												
													
														|  | -                            lambdaNew=(yesterdayConv*yesterdayCpa)/yesterdayRealCost;
 |  | 
 | 
											
												
													
														|  | -                        }
 |  | 
 | 
											
												
													
														|  | 
 |  | +                    if(conversion<1d){
 | 
											
												
													
														|  | 
 |  | +                        temp.put(creativeId,cacheItem);
 | 
											
												
													
														|  | 
 |  | +                        continue;
 | 
											
												
													
														|  |                      }
 |  |                      }
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -                    lambdaNew=cacheItem.calculate( conversion,  realCost,  cpa,lambdaNew) ;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -                    if(lambdaNew>maxLambda){
 |  | 
 | 
											
												
													
														|  | -                        lambdaNew=maxLambda;
 |  | 
 | 
											
												
													
														|  | -                    }else if(lambdaNew<minLambda){
 |  | 
 | 
											
												
													
														|  | 
 |  | +                    latestRealCPA=realCost/conversion;
 | 
											
												
													
														|  | 
 |  | +                    if(Math.abs(latestRealCPA-cacheItem.latestRealCpa)<0.01){
 | 
											
												
													
														|  | 
 |  | +                        temp.put(creativeId,cacheItem);
 | 
											
												
													
														|  | 
 |  | +                        continue;
 | 
											
												
													
														|  | 
 |  | +                    }
 | 
											
												
													
														|  | 
 |  | +                    Double lambdaNew =cacheItem.calculate(kp,ki,kd,cpa,latestRealCPA);
 | 
											
												
													
														|  | 
 |  | +                    if(lambdaNew<minLambda){
 | 
											
												
													
														|  |                          lambdaNew=minLambda;
 |  |                          lambdaNew=minLambda;
 | 
											
												
													
														|  |                      }
 |  |                      }
 | 
											
												
													
														|  |                      cacheItem.lambda=lambdaNew;
 |  |                      cacheItem.lambda=lambdaNew;
 | 
											
												
													
														|  | 
 |  | +                    cacheItem.latestRealCpa=latestRealCPA;
 | 
											
												
													
														|  | 
 |  | +                    cacheItem.sumError=sumE;
 | 
											
												
													
														|  |                      cacheItem.latestConv=conversion;
 |  |                      cacheItem.latestConv=conversion;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |                      temp.put(creativeId,cacheItem);
 |  |                      temp.put(creativeId,cacheItem);
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |                      log.info("svc=calNewLambdaV2 creativeId={} lambdaNew={}", creativeId,lambdaNew);
 |  |                      log.info("svc=calNewLambdaV2 creativeId={} lambdaNew={}", creativeId,lambdaNew);
 | 
											
										
											
												
													
														|  | @@ -177,7 +180,7 @@ public class PidLambdaV2Container {
 | 
											
												
													
														|  |              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,CacheItem>>(){});
 |  | 
 | 
											
												
													
														|  | 
 |  | +            lambdaCache=JSONObject.parseObject(builder.toString(),new TypeReference<ConcurrentHashMap<Long, CacheItem>>(){});
 | 
											
												
													
														|  |              this.cacheDate=object.getObjectMetadata().getLastModified();
 |  |              this.cacheDate=object.getObjectMetadata().getLastModified();
 | 
											
												
													
														|  |          }catch (Exception e){
 |  |          }catch (Exception e){
 | 
											
												
													
														|  |              log.error("svc=loadLambdaV2File status=failed error={}", Arrays.toString(e.getStackTrace()));
 |  |              log.error("svc=loadLambdaV2File status=failed error={}", Arrays.toString(e.getStackTrace()));
 | 
											
										
											
												
													
														|  | @@ -204,33 +207,39 @@ public class PidLambdaV2Container {
 | 
											
												
													
														|  |              this.creativeId=creativeId;
 |  |              this.creativeId=creativeId;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        public double calculate(double conversion, double realCost, double cpa,double lambdaNew ) {
 |  | 
 | 
											
												
													
														|  | -            if(conversion!=0){
 |  | 
 | 
											
												
													
														|  | -                double latestRealCPA=realCost/conversion;
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -                if(this.latestRealCpa==0d){
 |  | 
 | 
											
												
													
														|  | -                    this.latestRealCpa=cpa;
 |  | 
 | 
											
												
													
														|  | -                }
 |  | 
 | 
											
												
													
														|  | -                if(Math.abs(latestRealCPA-cpa)-Math.abs(this.latestRealCpa-cpa)>0){
 |  | 
 | 
											
												
													
														|  | -                    this.pow=2d;
 |  | 
 | 
											
												
													
														|  | -                }else {
 |  | 
 | 
											
												
													
														|  | -                    this.pow=1.2;
 |  | 
 | 
											
												
													
														|  | -                }
 |  | 
 | 
											
												
													
														|  | -                this.latestRealCpa=latestRealCPA;
 |  | 
 | 
											
												
													
														|  | -                return Math.pow(lambdaNew,this.pow);
 |  | 
 | 
											
												
													
														|  | -            }
 |  | 
 | 
											
												
													
														|  | -            this.pow=1.5d;
 |  | 
 | 
											
												
													
														|  | -            return Math.pow(lambdaNew,this.pow);
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |          public Long creativeId;
 |  |          public Long creativeId;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        public double lambda=1d;
 |  | 
 | 
											
												
													
														|  | 
 |  | +        public double lambda=-1d;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |          public double latestConv=0d;
 |  |          public double latestConv=0d;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        public double pow=1d;
 |  | 
 | 
											
												
													
														|  | 
 |  | +        public double sumError=0d;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |          public double latestRealCpa=0d;
 |  |          public double latestRealCpa=0d;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public double processVariable=0d; // 处理变量
 | 
											
												
													
														|  | 
 |  | +        public double integral=0d; // 积分项
 | 
											
												
													
														|  | 
 |  | +        public double lastError=0d; // 上一个误差
 | 
											
												
													
														|  | 
 |  | +        public double lastPidValue=0d;
 | 
											
												
													
														|  | 
 |  | +        public double calculate(double kp, double ki, double kd, double setPoint,double currentValue) {
 | 
											
												
													
														|  | 
 |  | +            processVariable = currentValue;
 | 
											
												
													
														|  | 
 |  | +            double error = setPoint - processVariable;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            integral += error;
 | 
											
												
													
														|  | 
 |  | +            double derivative = (error - lastError) / 1; // 假设采样间隔为1
 | 
											
												
													
														|  | 
 |  | +            lastError = error;
 | 
											
												
													
														|  | 
 |  | +            lastPidValue=kp * error + ki * integral + kd * derivative;
 | 
											
												
													
														|  | 
 |  | +            double result=currentValue+lastPidValue;
 | 
											
												
													
														|  | 
 |  | +            if(result<=currentValue/8d){
 | 
											
												
													
														|  | 
 |  | +                result=currentValue/8d;
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            return result;
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        public void reset() {
 | 
											
												
													
														|  | 
 |  | +            integral = 0;
 | 
											
												
													
														|  | 
 |  | +            lastError = 0;
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  }
 |  |  }
 |