|
@@ -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 com.tzld.piaoquan.ad.engine.service.predict.impl.PredictModelServiceImpl;
|
|
import com.tzld.piaoquan.ad.engine.service.predict.impl.PredictModelServiceImpl;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -42,10 +43,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 +58,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 +103,40 @@ 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));
|
|
|
|
+ if(DateUtils.getCurrentHour()<=8){
|
|
|
|
+ temp.put(creativeId,cacheItem);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ conversion=Double.parseDouble(cols[1]);
|
|
|
|
+ cpa=Double.parseDouble(cols[2]);
|
|
|
|
+ realCost=Double.parseDouble(cols[3]);
|
|
|
|
+ if(conversion<5d){
|
|
|
|
+ conversion=1d;
|
|
|
|
+ }
|
|
|
|
+ latestRealCPA=realCost/conversion;
|
|
|
|
+ Double lambdaNew =cacheItem.calculate(kp,ki,kd,cpa,latestRealCPA);
|
|
|
|
+
|
|
|
|
+ cacheItem.lambda=lambdaNew;
|
|
|
|
+ cacheItem.latestRealCpa=latestRealCPA;
|
|
|
|
+ cacheItem.sumError=sumE;
|
|
|
|
+ cacheItem.latestConv=conversion;
|
|
|
|
+
|
|
|
|
+ temp.put(creativeId,cacheItem);
|
|
|
|
+
|
|
|
|
+ log.info("svc=calNewLambdaV1 creativeId={} lambdaNew={}", creativeId,lambdaNew);
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
@@ -116,7 +144,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=calNewLambdaV1 status=failed error={}", Arrays.toString(e.getStackTrace()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -130,7 +158,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=writeLambdaV1FileToOss status=failed error={}", Arrays.toString(e.getStackTrace()));
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -148,83 +176,61 @@ 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=loadLambdaV1File 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;
|
|
|
|
|
|
+
|
|
|
|
+ public static class V1CacheItem{
|
|
|
|
+
|
|
|
|
+ public V1CacheItem(){
|
|
|
|
+
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- 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 V1CacheItem(Long creativeId){
|
|
|
|
+ this.creativeId=creativeId;
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- 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 Long creativeId;
|
|
|
|
+
|
|
|
|
+ public double lambda=1d;
|
|
|
|
+
|
|
|
|
+ public double latestConv=0d;
|
|
|
|
+
|
|
|
|
+ public double sumError=0d;
|
|
|
|
+
|
|
|
|
+ public double latestRealCpa=0d;
|
|
|
|
+
|
|
|
|
+ public double processVariable=0d; // 处理变量
|
|
|
|
+ public double integral=0d; // 积分项
|
|
|
|
+ public double lastError=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;
|
|
|
|
+ return kp * error + ki * integral + kd * derivative;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void reset() {
|
|
|
|
+ integral = 0;
|
|
|
|
+ lastError = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|