Ver Fonte

Merge branch 'feature_gufengshou_20240401_pid_v6' into pre-master

gufengshou1 há 1 ano atrás
pai
commit
c49dfdbca1

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

@@ -9,6 +9,7 @@ import com.aliyun.oss.common.auth.DefaultCredentialProvider;
 import com.aliyun.oss.model.CopyObjectResult;
 import com.aliyun.oss.model.OSSObject;
 import com.aliyun.oss.model.PutObjectResult;
+import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
 import com.tzld.piaoquan.ad.engine.service.predict.impl.PredictModelServiceImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -107,14 +108,18 @@ public class PidLambdaContainer {
             Double cpa=0d;
             Double realCost=0d;
             Double latestRealCPA=0d;
-            double et=0d;
+//            double et=0d;
             double sumE=0d;
-            double ve=0d;
+//            double ve=0d;
             while ((line = bufferedReader.readLine()) != null){
                 try {
                     String[] cols=line.split(",");
                     Long creativeId=Long.parseLong(cols[0]);
                     V1CacheItem cacheItem=lambdaCache.getOrDefault(creativeId,new V1CacheItem(creativeId));
+                    if(DateUtils.getCurrentHour()<=8){
+                        temp.put(creativeId,cacheItem);
+                        continue;
+                    }
                     conversion=Double.parseDouble(cols[1]);
                     cpa=Double.parseDouble(cols[2]);
                     realCost=Double.parseDouble(cols[3]);
@@ -123,14 +128,8 @@ public class PidLambdaContainer {
                         conversion=1d;
                     }
                     latestRealCPA=realCost/conversion;
-                    et=cpa-latestRealCPA;
-                    sumE=cacheItem.sumError+et;
-                    ve=et-(cpa-cacheItem.latestRealCpa);
-                    lambdaNew=kp*et+ki*sumE+kd*ve;
+                    lambdaNew =cacheItem.calculate(kp,ki,kd,cpa,latestRealCPA);
 
-                    if(cpa+lambdaNew<=1) {
-                        lambdaNew=cpa-1;
-                    }
                     cacheItem.lambda=lambdaNew;
                     cacheItem.latestRealCpa=latestRealCPA;
                     cacheItem.sumError=sumE;
@@ -138,7 +137,7 @@ public class PidLambdaContainer {
 
                     temp.put(creativeId,cacheItem);
 
-                    log.info("svc=calNewLambdaV2 creativeId={} lambdaNew={}", creativeId,lambdaNew);
+                    log.info("svc=calNewLambdaV1 creativeId={} lambdaNew={}", creativeId,lambdaNew);
                 }catch (Exception e){
                     e.printStackTrace();
                 }
@@ -146,7 +145,7 @@ public class PidLambdaContainer {
             lambdaCache.clear();
             lambdaCache=temp;
         }catch (Exception e){
-            log.error("svc=calNewLambdaV2 status=failed error={}", Arrays.toString(e.getStackTrace()));
+            log.error("svc=calNewLambdaV1 status=failed error={}", Arrays.toString(e.getStackTrace()));
         }
     }
 
@@ -160,7 +159,7 @@ public class PidLambdaContainer {
             this.cacheDate= copyObjectResult.getLastModified();
             client.deleteObject(bucketName, tempFile);
         }catch (Exception e){
-            log.error("svc=writeLambdaV2FileToOss status=failed error={}", Arrays.toString(e.getStackTrace()));
+            log.error("svc=writeLambdaV1FileToOss status=failed error={}", Arrays.toString(e.getStackTrace()));
             e.printStackTrace();
         }
     }
@@ -181,7 +180,7 @@ public class PidLambdaContainer {
             lambdaCache=JSONObject.parseObject(builder.toString(),new TypeReference<ConcurrentHashMap<Long, V1CacheItem>>(){});
             this.cacheDate=object.getObjectMetadata().getLastModified();
         }catch (Exception e){
-            log.error("svc=loadLambdaV2File status=failed error={}", Arrays.toString(e.getStackTrace()));
+            log.error("svc=loadLambdaV1File status=failed error={}", Arrays.toString(e.getStackTrace()));
             e.printStackTrace();
         }
     }
@@ -214,5 +213,25 @@ public class PidLambdaContainer {
         public double sumError=0d;
 
         public double latestRealCpa=0d;
+
+        private double processVariable; // 处理变量
+        private double integral; // 积分项
+        private double lastError; // 上一个误差
+
+        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;
+            return kp * error + ki * integral + kd * derivative;
+        }
+
+
+        public void reset() {
+            integral = 0;
+            lastError = 0;
+        }
     }
 }