谐波责任量化java版本重构,需测试,单体已经测试没有问题
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
package com.njcn.advance.responsibility.analysis;
|
||||
|
||||
import com.njcn.advance.responsibility.constant.HarmonicConstants;
|
||||
import com.njcn.advance.responsibility.utils.MathUtils;
|
||||
import org.apache.commons.math3.linear.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 典则相关分析类
|
||||
* 实现典则相关系数的计算
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CanonicalCorrelationAnalysis {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CanonicalCorrelationAnalysis.class);
|
||||
|
||||
/**
|
||||
* 计算典则相关系数
|
||||
* 对应C代码中的TransCancor函数
|
||||
*
|
||||
* @param powerData 功率数据矩阵 [时间][节点]
|
||||
* @param harmonicData 谐波数据向量
|
||||
* @param windowSize 窗口大小
|
||||
* @param nodeCount 节点数量
|
||||
* @return 典则相关系数
|
||||
*/
|
||||
public static float computeCanonicalCorrelation(float[][] powerData, float[] harmonicData,
|
||||
int windowSize, int nodeCount) {
|
||||
try {
|
||||
// 提取窗口数据
|
||||
double[][] x = new double[windowSize][nodeCount];
|
||||
double[] y = new double[windowSize];
|
||||
|
||||
for (int i = 0; i < windowSize; i++) {
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
x[i][j] = powerData[i][j];
|
||||
}
|
||||
y[i] = harmonicData[i];
|
||||
}
|
||||
|
||||
// 计算协方差矩阵 SXX
|
||||
double[][] sxxMatrix = MathUtils.covarianceMatrix(x, windowSize, nodeCount);
|
||||
|
||||
// 计算协方差 SYY
|
||||
double syyMatrix = MathUtils.covariance(y, y, windowSize);
|
||||
if (Math.abs(syyMatrix) < HarmonicConstants.MIN_COVARIANCE) {
|
||||
syyMatrix = HarmonicConstants.MIN_COVARIANCE;
|
||||
}
|
||||
|
||||
// 计算协方差向量 SXY
|
||||
double[] sxyVector = MathUtils.covarianceVector(x, y, windowSize, nodeCount);
|
||||
|
||||
// 使用Apache Commons Math进行矩阵运算
|
||||
RealMatrix sxx = new Array2DRowRealMatrix(sxxMatrix);
|
||||
RealVector sxy = new ArrayRealVector(sxyVector);
|
||||
|
||||
// 计算 SXX^(-1)
|
||||
DecompositionSolver solver = new LUDecomposition(sxx).getSolver();
|
||||
RealMatrix invSxx;
|
||||
|
||||
if (!solver.isNonSingular()) {
|
||||
// 如果矩阵奇异,使用伪逆
|
||||
logger.warn("SXX matrix is singular, using pseudo-inverse");
|
||||
SingularValueDecomposition svd = new SingularValueDecomposition(sxx);
|
||||
invSxx = svd.getSolver().getInverse();
|
||||
} else {
|
||||
invSxx = solver.getInverse();
|
||||
}
|
||||
|
||||
// 计算 U = SXX^(-1) * SXY * (1/SYY) * SXY'
|
||||
RealVector temp = invSxx.operate(sxy);
|
||||
double scale = 1.0 / syyMatrix;
|
||||
RealMatrix uMatrix = temp.outerProduct(sxy).scalarMultiply(scale);
|
||||
|
||||
// 计算特征值
|
||||
EigenDecomposition eigenDecomposition = new EigenDecomposition(uMatrix);
|
||||
double[] eigenvalues = eigenDecomposition.getRealEigenvalues();
|
||||
|
||||
// 找最大特征值
|
||||
double maxEigenvalue = 0.0;
|
||||
for (double eigenvalue : eigenvalues) {
|
||||
maxEigenvalue = Math.max(maxEigenvalue, Math.abs(eigenvalue));
|
||||
}
|
||||
|
||||
// 典则相关系数是最大特征值的平方根
|
||||
double canonicalCorr = Math.sqrt(maxEigenvalue);
|
||||
|
||||
// 限制在[0,1]范围内
|
||||
if (canonicalCorr > 1.0) {
|
||||
canonicalCorr = 1.0;
|
||||
}
|
||||
|
||||
return (float) canonicalCorr;
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error computing canonical correlation", e);
|
||||
return 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 滑动窗口计算典则相关系数序列
|
||||
* 对应C代码中的SlideCanCor函数
|
||||
*
|
||||
* @param powerData 功率数据矩阵 [时间][节点]
|
||||
* @param harmonicData 谐波数据向量
|
||||
* @param windowSize 窗口大小
|
||||
* @param nodeCount 节点数量
|
||||
* @param dataLength 数据总长度
|
||||
* @return 典则相关系数序列
|
||||
*/
|
||||
public static float[] slidingCanonicalCorrelation(float[][] powerData, float[] harmonicData,
|
||||
int windowSize, int nodeCount, int dataLength) {
|
||||
int slideLength = dataLength - windowSize;
|
||||
if (slideLength <= 0) {
|
||||
throw new IllegalArgumentException("Data length must be greater than window size");
|
||||
}
|
||||
|
||||
float[] slideCanCor = new float[slideLength];
|
||||
|
||||
logger.info("Starting sliding canonical correlation analysis, slide length: {}", slideLength);
|
||||
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
// 提取窗口数据
|
||||
float[][] windowPower = new float[windowSize][nodeCount];
|
||||
float[] windowHarmonic = new float[windowSize];
|
||||
|
||||
for (int j = 0; j < windowSize; j++) {
|
||||
System.arraycopy(powerData[i + j], 0, windowPower[j], 0, nodeCount);
|
||||
windowHarmonic[j] = harmonicData[i + j];
|
||||
}
|
||||
|
||||
// 计算当前窗口的典则相关系数
|
||||
slideCanCor[i] = computeCanonicalCorrelation(windowPower, windowHarmonic,
|
||||
windowSize, nodeCount);
|
||||
|
||||
if (i % 10 == 0) {
|
||||
logger.debug("Processed window {}/{}", i, slideLength);
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("Sliding canonical correlation analysis completed");
|
||||
|
||||
return slideCanCor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算包含/不包含背景的动态相关系数
|
||||
* 对应C代码中的SlideCor函数
|
||||
*
|
||||
* @param powerData 功率数据(单个节点)
|
||||
* @param harmonicData 谐波数据
|
||||
* @param slideCanCor 滑动典则相关系数
|
||||
* @param windowSize 窗口大小
|
||||
* @return 动态相关系数序列
|
||||
*/
|
||||
public static float[] slidingCorrelation(float[] powerData, float[] harmonicData,
|
||||
float[] slideCanCor, int windowSize) {
|
||||
int slideLength = slideCanCor.length;
|
||||
float[] slideCor = new float[slideLength];
|
||||
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
float[] tempPower = new float[windowSize];
|
||||
float[] tempHarmonic = new float[windowSize];
|
||||
|
||||
for (int j = 0; j < windowSize; j++) {
|
||||
tempPower[j] = powerData[i + j];
|
||||
tempHarmonic[j] = harmonicData[i + j] * slideCanCor[i];
|
||||
}
|
||||
|
||||
slideCor[i] = MathUtils.pearsonCorrelation(tempHarmonic, tempPower, windowSize);
|
||||
}
|
||||
|
||||
return slideCor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,424 @@
|
||||
package com.njcn.advance.responsibility.calculator;
|
||||
|
||||
import com.njcn.advance.responsibility.analysis.CanonicalCorrelationAnalysis;
|
||||
import com.njcn.advance.responsibility.constant.CalculationMode;
|
||||
import com.njcn.advance.responsibility.constant.CalculationStatus;
|
||||
import com.njcn.advance.responsibility.constant.HarmonicConstants;
|
||||
import com.njcn.advance.responsibility.model.HarmonicData;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 谐波责任计算主引擎
|
||||
* 严格对应C代码中的harm_res系列函数
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 2.0 - 修复版本,严格对照C代码实现
|
||||
*/
|
||||
public class HarmonicCalculationEngine {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HarmonicCalculationEngine.class);
|
||||
|
||||
// 对应C代码中的全局变量
|
||||
private int P; // 节点数 p_node
|
||||
private int TL; // 功率数据长度 p_num
|
||||
private int LL; // 谐波数据长度 harm_num
|
||||
private int JIANGE; // 数据间隔比例
|
||||
private int width; // 窗口大小
|
||||
private float XIANE; // 谐波门槛
|
||||
|
||||
/**
|
||||
* 主计算入口
|
||||
* 对应C代码中的harm_res函数
|
||||
*
|
||||
* @param data 谐波数据对象
|
||||
* @return 计算是否成功
|
||||
*/
|
||||
public boolean calculate(HarmonicData data) {
|
||||
logger.info("Starting harmonic calculation, mode: {}", data.getCalculationMode());
|
||||
|
||||
try {
|
||||
if (data.getCalculationMode() == CalculationMode.FULL_CALCULATION) {
|
||||
return fullCalculation(data);
|
||||
} else {
|
||||
return partialCalculation(data);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Calculation failed with exception: " + e.getMessage(), e);
|
||||
e.printStackTrace();
|
||||
data.setCalculationStatus(CalculationStatus.FAILED);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 完整计算模式
|
||||
* 严格对应C代码中的harm_res_all函数
|
||||
*
|
||||
* @param data 谐波数据对象
|
||||
* @return 计算是否成功
|
||||
*/
|
||||
private boolean fullCalculation(HarmonicData data) {
|
||||
logger.info("Executing full calculation mode");
|
||||
|
||||
// 1. 数据初始化 - 对应 data_init_all()
|
||||
if (!initializeFullCalculationData(data)) {
|
||||
logger.error("Data initialization failed");
|
||||
data.setCalculationStatus(CalculationStatus.FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 创建工作数组 - 对应C代码行536-540
|
||||
float[][] a = new float[TL][P]; // 功率数据副本
|
||||
float[] b = new float[LL]; // 谐波数据副本
|
||||
float[] u = new float[TL]; // 对齐后的谐波数据
|
||||
|
||||
// 3. 复制数据 - 对应C代码行542-552
|
||||
for (int i = 0; i < TL; i++) {
|
||||
for (int j = 0; j < P; j++) {
|
||||
a[i][j] = data.getPowerData()[i][j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < LL; i++) {
|
||||
b[i] = data.getHarmonicData()[i];
|
||||
}
|
||||
|
||||
// 4. 数据对齐处理 - 严格对应C代码行554-562
|
||||
// 注意:C代码是原地修改数组b
|
||||
for (int i = 0; i < LL; i += JIANGE) {
|
||||
float tempt = 0.0f;
|
||||
for (int j = 0; j < JIANGE; j++) {
|
||||
tempt += b[i + j];
|
||||
}
|
||||
b[i] = tempt / JIANGE; // 覆盖原位置
|
||||
}
|
||||
|
||||
// 5. 构建Udata - 严格对应C代码行570-580
|
||||
// 注意:使用 i*JIANGE 索引
|
||||
for (int i = 0; i < TL; i++) {
|
||||
u[i] = b[i * JIANGE]; // 关键:使用 i*JIANGE 索引
|
||||
}
|
||||
|
||||
int slcorlength = TL - width;
|
||||
|
||||
// 6. 计算滑动典则相关系数 - 对应C代码行584
|
||||
logger.info("Computing sliding canonical correlation");
|
||||
float[] cancorrelation = CanonicalCorrelationAnalysis.slidingCanonicalCorrelation(
|
||||
a, u, width, P, TL
|
||||
);
|
||||
|
||||
// 7. 保存典则相关系数 - 对应C代码行592-601
|
||||
float[] Core = new float[slcorlength];
|
||||
float[] BjCore = new float[slcorlength];
|
||||
for (int i = 0; i < slcorlength; i++) {
|
||||
Core[i] = cancorrelation[i];
|
||||
BjCore[i] = 1 - cancorrelation[i];
|
||||
}
|
||||
data.setCanonicalCorrelation(Core);
|
||||
data.setBackgroundCanonicalCorr(BjCore);
|
||||
|
||||
// 8. 计算动态相关系数矩阵 - 对应C代码行605-635
|
||||
logger.info("Computing correlation matrix");
|
||||
float[][] simCor = new float[slcorlength][P];
|
||||
|
||||
// 对应C代码行618-632:对每个节点计算动态相关系数
|
||||
for (int i = 0; i < P; i++) {
|
||||
// 提取第i个节点的功率数据
|
||||
float[] xe = new float[TL];
|
||||
for (int m = 0; m < TL; m++) {
|
||||
xe[m] = a[m][i]; // 对应 Pdata.block(0, i, TL, 1)
|
||||
}
|
||||
|
||||
// 计算该节点的滑动相关系数
|
||||
float[] slidecor = CanonicalCorrelationAnalysis.slidingCorrelation(
|
||||
xe, u, cancorrelation, width
|
||||
);
|
||||
|
||||
// 存储结果
|
||||
for (int j = 0; j < slcorlength; j++) {
|
||||
simCor[j][i] = slidecor[j];
|
||||
}
|
||||
}
|
||||
data.setCorrelationData(simCor);
|
||||
|
||||
// 9. 计算EK值 - 对应C代码行642-654
|
||||
logger.info("Computing EK values");
|
||||
float[][] EKdata = ResponsibilityCalculator.computeEK(
|
||||
simCor, a, width, P, TL
|
||||
);
|
||||
|
||||
// 10. 计算FK值 - 对应C代码行660-673
|
||||
logger.info("Computing FK values");
|
||||
float[][] FKdata = ResponsibilityCalculator.computeFK(
|
||||
EKdata, width, P, TL
|
||||
);
|
||||
data.setFkData(FKdata);
|
||||
|
||||
// 11. 计算HK值 - 对应C代码行678-691
|
||||
logger.info("Computing HK values");
|
||||
float[][] HKdata = ResponsibilityCalculator.computeHK(
|
||||
BjCore, EKdata, width, P, TL
|
||||
);
|
||||
data.setHkData(HKdata);
|
||||
|
||||
// 12. 设置结果数量 - 对应C代码行693
|
||||
data.setResponsibilityDataCount(slcorlength);
|
||||
|
||||
// 13. 统计超限时段的责任 - 对应C代码行696-724
|
||||
logger.info("Computing responsibility sums");
|
||||
|
||||
// 重要修正:C代码的SumHK函数中,虽然Udata长度是TL,但是循环只遍历前slg(=TL-width)个元素
|
||||
// 所以我们需要传入完整的u数组(长度TL),让sumResponsibility内部处理
|
||||
// 对应C代码:VectorXd Udata(TL); 以及 SumHK函数调用
|
||||
|
||||
// 统计HK责任(包含背景)- 对应C代码行698-710
|
||||
// 注意:传入完整的u数组(TL长度),而不是截取的数组
|
||||
float[] sumHK = ResponsibilityCalculator.sumResponsibility(
|
||||
HKdata, u, XIANE, width, P + 1, TL
|
||||
);
|
||||
data.setSumHKData(sumHK);
|
||||
|
||||
// 统计FK责任(不包含背景)- 对应C代码行712-724
|
||||
// 同样传入完整的u数组和TL参数
|
||||
float[] sumFK = ResponsibilityCalculator.sumResponsibility(
|
||||
FKdata, u, XIANE, width, P, TL
|
||||
);
|
||||
data.setSumFKData(sumFK);
|
||||
|
||||
// 14. 标记计算成功 - 对应C代码行739
|
||||
data.setCalculationStatus(CalculationStatus.CALCULATED);
|
||||
logger.info("Full calculation completed successfully");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化完整计算数据
|
||||
* 对应C代码中的data_init_all函数
|
||||
*/
|
||||
private boolean initializeFullCalculationData(HarmonicData data) {
|
||||
// 设置全局变量 - 对应C代码行478-483
|
||||
P = data.getPowerNodeCount();
|
||||
TL = data.getPowerCount();
|
||||
LL = data.getHarmonicCount();
|
||||
// 对应C代码第481行:JIANGE = pq_buf.harm_num/pq_buf.p_num;
|
||||
// 重要修正:JIANGE应该是 谐波数量/功率点数,不是谐波数量/节点数
|
||||
JIANGE = LL / TL; // 这个是正确的:harm_num / p_num (其中p_num是功率点数)
|
||||
width = data.getWindowSize();
|
||||
XIANE = data.getHarmonicThreshold();
|
||||
|
||||
// 验证数据 - 对应C代码行485-504
|
||||
if (JIANGE * TL != LL || JIANGE < 1) {
|
||||
logger.error("Data length mismatch: JIANGE({}) * TL({}) != LL({})",
|
||||
JIANGE, TL, LL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (width < HarmonicConstants.MIN_WIN_LEN || width > HarmonicConstants.MAX_WIN_LEN) {
|
||||
logger.error("Invalid window size: {}", width);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TL < 2 * width) {
|
||||
logger.error("Power data length {} is too short for window size {}", TL, width);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (P > HarmonicConstants.MAX_P_NODE || TL > HarmonicConstants.MAX_P_NUM ||
|
||||
LL > HarmonicConstants.MAX_HARM_NUM) {
|
||||
logger.error("Data size exceeds limits");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分重算模式
|
||||
* 对应C代码中的harm_res_part函数
|
||||
*
|
||||
* @param data 谐波数据对象
|
||||
* @return 计算是否成功
|
||||
*/
|
||||
private boolean partialCalculation(HarmonicData data) {
|
||||
logger.info("Executing partial calculation mode");
|
||||
|
||||
// 1. 数据初始化 - 对应 data_init_part()
|
||||
if (!initializePartialCalculationData(data)) {
|
||||
logger.error("Data initialization failed for partial calculation");
|
||||
data.setCalculationStatus(CalculationStatus.FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 准备Udata - 对应C代码行816-818
|
||||
// C代码:VectorXd Udata(TL); 并从pq_buf.harm_data复制TL个元素
|
||||
int res_num = data.getResponsibilityDataCount();
|
||||
|
||||
// 验证责任数据行数
|
||||
if (res_num != TL - width) {
|
||||
logger.warn("责任数据行数({})与期望值(TL-width={})不匹配", res_num, TL - width);
|
||||
res_num = TL - width; // 使用正确的值
|
||||
}
|
||||
|
||||
// 重要修正:与C代码保持一致,Udata长度应该是TL,而不是res_num
|
||||
// C代码:VectorXd Udata(TL);
|
||||
float[] Udata = new float[TL];
|
||||
|
||||
// 从harm_data复制TL个元素到Udata
|
||||
// C代码:for (int j = 0; j < TL; j++) Udata[j] = pq_buf.harm_data[j];
|
||||
if (data.getHarmonicData().length < TL) {
|
||||
logger.warn("谐波数据长度({})小于TL({}), 将补零",
|
||||
data.getHarmonicData().length, TL);
|
||||
System.arraycopy(data.getHarmonicData(), 0, Udata, 0, data.getHarmonicData().length);
|
||||
// 剩余部分自动补零
|
||||
} else {
|
||||
System.arraycopy(data.getHarmonicData(), 0, Udata, 0, TL);
|
||||
}
|
||||
|
||||
logger.debug("准备Udata完成: 长度={} (对应C代码TL), 责任数据行数={}", Udata.length, res_num);
|
||||
|
||||
// 3. 统计HK责任 - 对应C代码行806-830
|
||||
logger.info("Recalculating HK responsibility sums");
|
||||
|
||||
// 对应C代码第808-814行:创建新的HKdata矩阵,只包含RES_NUM行
|
||||
// C代码:MatrixXd HKdata(RES_NUM, (P + 1));
|
||||
|
||||
// 添加数据验证
|
||||
if (data.getHkData() == null || data.getHkData().length == 0) {
|
||||
logger.error("HK数据为空或长度为0");
|
||||
data.setCalculationStatus(CalculationStatus.FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 重要:C代码创建了新的RES_NUM行的HKdata,从原始数据复制前RES_NUM行
|
||||
// 对应C代码第808-814行
|
||||
float[][] HKdataForCalc = new float[res_num][P + 1];
|
||||
int copyRows = Math.min(res_num, data.getHkData().length);
|
||||
|
||||
logger.debug("创建用于计算的HK数据矩阵: {}x{}, 从原始数据复制{}行",
|
||||
res_num, P + 1, copyRows);
|
||||
|
||||
for (int i = 0; i < copyRows; i++) {
|
||||
for (int j = 0; j < P + 1; j++) {
|
||||
if (j < data.getHkData()[i].length) {
|
||||
HKdataForCalc[i][j] = data.getHkData()[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
logger.debug("调用HK sumResponsibility参数: HKdata[{}x{}], Udata[{}], TL={}, width={}",
|
||||
HKdataForCalc.length, HKdataForCalc.length > 0 ? HKdataForCalc[0].length : 0,
|
||||
Udata.length, TL, width);
|
||||
|
||||
try {
|
||||
// 对应C代码第819行:arrHKsum = SumHK(HKdata, Udata, wdith, colK, TL);
|
||||
float[] sumHK = ResponsibilityCalculator.sumResponsibility(
|
||||
HKdataForCalc, // 使用新创建的RES_NUM行的HK数据
|
||||
Udata, // 长度为TL的数组
|
||||
XIANE,
|
||||
width,
|
||||
P + 1,
|
||||
TL // 传入TL参数
|
||||
);
|
||||
data.setSumHKData(sumHK);
|
||||
logger.debug("HK责任计算完成,结果长度: {}", sumHK != null ? sumHK.length : "null");
|
||||
} catch (Exception e) {
|
||||
logger.error("HK责任计算失败: " + e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// 4. 统计FK责任 - 对应C代码行839-851
|
||||
logger.info("Recalculating FK responsibility sums");
|
||||
|
||||
// 对应C代码:虽然没有显式创建新的FKdata,但逻辑相同
|
||||
|
||||
// 添加数据验证
|
||||
if (data.getFkData() == null || data.getFkData().length == 0) {
|
||||
logger.error("FK数据为空或长度为0");
|
||||
data.setCalculationStatus(CalculationStatus.FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 创建用于计算的FK数据矩阵(RES_NUM行)
|
||||
float[][] FKdataForCalc = new float[res_num][P];
|
||||
int copyRowsFK = Math.min(res_num, data.getFkData().length);
|
||||
|
||||
logger.debug("创建用于计算的FK数据矩阵: {}x{}, 从原始数据复制{}行",
|
||||
res_num, P, copyRowsFK);
|
||||
|
||||
for (int i = 0; i < copyRowsFK; i++) {
|
||||
for (int j = 0; j < P; j++) {
|
||||
if (j < data.getFkData()[i].length) {
|
||||
FKdataForCalc[i][j] = data.getFkData()[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
logger.debug("调用FK sumResponsibility参数: FKdata[{}x{}], Udata[{}], TL={}, width={}",
|
||||
FKdataForCalc.length, FKdataForCalc.length > 0 ? FKdataForCalc[0].length : 0,
|
||||
Udata.length, TL, width);
|
||||
|
||||
try {
|
||||
// 对应C代码第840行:arrHKsum = SumHK(FKdata, Udata, wdith, colK, TL);
|
||||
float[] sumFK = ResponsibilityCalculator.sumResponsibility(
|
||||
FKdataForCalc, // 使用新创建的RES_NUM行的FK数据
|
||||
Udata, // 使用相同的Udata(长度TL)
|
||||
XIANE,
|
||||
width,
|
||||
P,
|
||||
TL // 传入TL参数
|
||||
);
|
||||
data.setSumFKData(sumFK);
|
||||
logger.debug("FK责任计算完成,结果长度: {}", sumFK != null ? sumFK.length : "null");
|
||||
} catch (Exception e) {
|
||||
logger.error("FK责任计算失败: " + e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// 5. 标记计算成功 - 对应C代码行858
|
||||
data.setCalculationStatus(CalculationStatus.CALCULATED);
|
||||
logger.info("Partial calculation completed successfully");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化部分计算数据
|
||||
* 对应C代码中的data_init_part函数
|
||||
*/
|
||||
private boolean initializePartialCalculationData(HarmonicData data) {
|
||||
// 设置变量 - 对应C代码行762-766
|
||||
int RES_NUM = data.getResponsibilityDataCount();
|
||||
P = data.getPowerNodeCount();
|
||||
TL = data.getWindowSize() + RES_NUM;
|
||||
width = data.getWindowSize();
|
||||
XIANE = data.getHarmonicThreshold();
|
||||
|
||||
// 验证数据 - 对应C代码行756-778
|
||||
if ((RES_NUM + width) != data.getHarmonicCount()) {
|
||||
logger.error("Data length mismatch: res_num({}) + win({}) != harm_num({})",
|
||||
RES_NUM, width, data.getHarmonicCount());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (width < HarmonicConstants.MIN_WIN_LEN || width > HarmonicConstants.MAX_WIN_LEN) {
|
||||
logger.error("Invalid window size: {}", width);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (P > HarmonicConstants.MAX_P_NODE || TL > HarmonicConstants.MAX_P_NUM) {
|
||||
logger.error("Data size exceeds limits");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 验证FK和HK数据存在
|
||||
if (data.getFkData() == null || data.getHkData() == null) {
|
||||
logger.error("FK or HK data is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
package com.njcn.advance.responsibility.calculator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 责任指标计算类
|
||||
* 计算谐波责任的各项指标
|
||||
* 严格对应C代码实现
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 2.0 - 修复版本,严格对照C代码实现
|
||||
*/
|
||||
public class ResponsibilityCalculator {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ResponsibilityCalculator.class);
|
||||
|
||||
/**
|
||||
* 计算EK值(动态责任指标)
|
||||
* 严格对应C代码中的DyEKCom函数(行300-357)
|
||||
*
|
||||
* @param correlationData 动态相关系数矩阵 [时间][节点]
|
||||
* @param powerData 功率数据矩阵 [时间][节点]
|
||||
* @param windowSize 窗口大小
|
||||
* @param nodeCount 节点数量
|
||||
* @param dataLength 数据长度
|
||||
* @return EK值矩阵
|
||||
*/
|
||||
public static float[][] computeEK(float[][] correlationData, float[][] powerData,
|
||||
int windowSize, int nodeCount, int dataLength) {
|
||||
int slideLength = dataLength - windowSize;
|
||||
float[][] ekData = new float[slideLength][nodeCount];
|
||||
float[][] akData = new float[slideLength][nodeCount];
|
||||
|
||||
logger.info("Computing EK values, slide length: {}", slideLength);
|
||||
|
||||
// 计算AK值 - 对应C代码行307-319
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
float sumPower = 0;
|
||||
|
||||
// 计算功率总和 - 对应C代码行309-313
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
sumPower += powerData[i][j]; // 注意:这里用的是powerData[i][j]
|
||||
}
|
||||
|
||||
// 计算AK值 - 对应C代码行314-318
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
if (sumPower > 0) {
|
||||
akData[i][j] = correlationData[i][j] * (powerData[i][j] / sumPower);
|
||||
} else {
|
||||
akData[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 归一化处理得到EK值 - 对应C代码行320-342
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
// 重要:C代码初始化为0,而不是Float.MIN_VALUE/MAX_VALUE
|
||||
// 对应C代码行322-323
|
||||
float maxValue = 0;
|
||||
float minValue = 0;
|
||||
|
||||
// 找最大最小值 - 对应C代码行322-334
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
if (akData[i][j] > maxValue) {
|
||||
maxValue = akData[i][j];
|
||||
}
|
||||
if (akData[i][j] < minValue) {
|
||||
minValue = akData[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
float range = maxValue - minValue;
|
||||
|
||||
// 归一化 - 对应C代码行338-341
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
if (Math.abs(range) > 1e-10) {
|
||||
ekData[i][j] = (akData[i][j] - minValue) / range;
|
||||
} else {
|
||||
ekData[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("EK computation completed");
|
||||
|
||||
return ekData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算FK值(不包含背景的责任指标)
|
||||
* 严格对应C代码中的DyFKCom函数(行358-389)
|
||||
*
|
||||
* @param ekData EK值矩阵
|
||||
* @param windowSize 窗口大小
|
||||
* @param nodeCount 节点数量
|
||||
* @param dataLength 数据长度
|
||||
* @return FK值矩阵
|
||||
*/
|
||||
public static float[][] computeFK(float[][] ekData, int windowSize,
|
||||
int nodeCount, int dataLength) {
|
||||
int slideLength = dataLength - windowSize;
|
||||
float[][] fkData = new float[slideLength][nodeCount];
|
||||
|
||||
logger.info("Computing FK values");
|
||||
|
||||
// 对应C代码行364-376
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
float sumEK = 0;
|
||||
|
||||
// 计算EK总和 - 对应C代码行366-370
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
sumEK += ekData[i][j];
|
||||
}
|
||||
|
||||
// 计算FK值(归一化)- 对应C代码行372-375
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
if (sumEK > 0) {
|
||||
fkData[i][j] = ekData[i][j] / sumEK;
|
||||
} else {
|
||||
fkData[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("FK computation completed");
|
||||
|
||||
return fkData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算HK值(包含背景的责任指标)
|
||||
* 严格对应C代码中的DyHKCom函数(行390-429)
|
||||
*
|
||||
* @param backgroundCanCor 背景典则相关系数(1-典则相关系数)
|
||||
* @param ekData EK值矩阵
|
||||
* @param windowSize 窗口大小
|
||||
* @param nodeCount 节点数量
|
||||
* @param dataLength 数据长度
|
||||
* @return HK值矩阵
|
||||
*/
|
||||
public static float[][] computeHK(float[] backgroundCanCor, float[][] ekData,
|
||||
int windowSize, int nodeCount, int dataLength) {
|
||||
int slideLength = dataLength - windowSize;
|
||||
float[][] hkData = new float[slideLength][nodeCount + 1];
|
||||
float[][] newEK = new float[slideLength][nodeCount + 1];
|
||||
|
||||
logger.info("Computing HK values");
|
||||
|
||||
// 构建包含背景的EK矩阵 - 对应C代码行396-403
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
// 复制原有EK值
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
newEK[i][j] = ekData[i][j];
|
||||
}
|
||||
// 添加背景值
|
||||
newEK[i][nodeCount] = backgroundCanCor[i];
|
||||
}
|
||||
|
||||
// 计算HK值 - 对应C代码行405-416
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
float sumEK = 0;
|
||||
|
||||
// 计算总和 - 对应C代码行407-411
|
||||
for (int j = 0; j < nodeCount + 1; j++) {
|
||||
sumEK += newEK[i][j];
|
||||
}
|
||||
|
||||
// 归一化得到HK值 - 对应C代码行412-415
|
||||
for (int j = 0; j < nodeCount + 1; j++) {
|
||||
if (sumEK > 0) {
|
||||
hkData[i][j] = newEK[i][j] / sumEK;
|
||||
} else {
|
||||
hkData[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("HK computation completed");
|
||||
|
||||
return hkData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算超限时段的责任总和
|
||||
* 严格对应C代码中的SumHK函数(行431-461)
|
||||
*
|
||||
* @param responsibilityData 责任数据矩阵(FK或HK)[时间][节点]
|
||||
* @param harmonicData 谐波数据(Udata) - 长度为TL
|
||||
* @param threshold 谐波门槛(XIANE)
|
||||
* @param windowSize 窗口大小
|
||||
* @param columnCount 列数(节点数或节点数+1)
|
||||
* @param tl_num 对应C代码的TL参数(总数据点数)
|
||||
* @return 各节点的责任总和百分比
|
||||
*/
|
||||
public static float[] sumResponsibility(float[][] responsibilityData, float[] harmonicData,
|
||||
float threshold, int windowSize, int columnCount, int tl_num) {
|
||||
// 对应C代码:int slg = tl_num - width;
|
||||
int slideLength = tl_num - windowSize; // 使用传入的tl_num计算,而不是从responsibilityData.length推断
|
||||
float[] sumData = new float[columnCount]; // 对应C代码中的 arrHKsum
|
||||
double[] HKSum = new double[columnCount]; // 对应C代码中的 VectorXd HKSum - 使用double精度
|
||||
int exceedCount = 0; // 对应C代码中的 coutt
|
||||
|
||||
logger.debug("sumResponsibility参数: threshold={}, windowSize={}, columnCount={}, tl_num={}, slideLength={}",
|
||||
threshold, windowSize, columnCount, tl_num, slideLength);
|
||||
|
||||
// 数据验证
|
||||
if (harmonicData == null) {
|
||||
logger.error("错误: harmonicData为null!");
|
||||
throw new NullPointerException("harmonicData不能为null");
|
||||
}
|
||||
|
||||
if (responsibilityData == null) {
|
||||
logger.error("错误: responsibilityData为null!");
|
||||
throw new NullPointerException("responsibilityData不能为null");
|
||||
}
|
||||
|
||||
|
||||
// 关键验证:检查数组长度是否充足
|
||||
// C代码中Udata长度是TL,循环遍历slg=TL-width个元素
|
||||
logger.debug("数据验证: slideLength={}, harmonicData.length={}, responsibilityData.length={}",
|
||||
slideLength, harmonicData.length, responsibilityData.length);
|
||||
|
||||
if (harmonicData.length < slideLength) {
|
||||
logger.error("!!!谐波数据长度不足!!!");
|
||||
logger.error("需要访问harmonicData[0]到harmonicData[{}], 但数组长度只有{}",
|
||||
slideLength - 1, harmonicData.length);
|
||||
throw new IllegalArgumentException(
|
||||
String.format("谐波数据长度不足: 需要%d, 实际%d", slideLength, harmonicData.length));
|
||||
}
|
||||
|
||||
if (responsibilityData.length < slideLength) {
|
||||
logger.error("!!!责任数据行数不足!!!");
|
||||
logger.error("需要访问responsibilityData[0]到responsibilityData[{}], 但数组长度只有{}",
|
||||
slideLength - 1, responsibilityData.length);
|
||||
throw new IllegalArgumentException(
|
||||
String.format("责任数据行数不足: 需要%d, 实际%d", slideLength, responsibilityData.length));
|
||||
}
|
||||
|
||||
// 统计超限时段的责任 - 对应C代码行437-449
|
||||
// 重要:C代码中有一个设计缺陷:coutt在每个j循环中被重置,
|
||||
// 但最后计算百分比时使用的是最后一次j循环的coutt值
|
||||
// 为了严格保持一致,我们也要复现这个逻辑
|
||||
for (int j = 0; j < columnCount; j++) {
|
||||
HKSum[j] = 0;
|
||||
exceedCount = 0; // 对应C代码行440: coutt = 0;
|
||||
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
// 添加越界检查
|
||||
if (i >= harmonicData.length) {
|
||||
logger.error("!!!数组越界!!! 尝试访问harmonicData[{}], 但数组长度只有{}",
|
||||
i, harmonicData.length);
|
||||
logger.error("发生在: j={}, i={}", j, i);
|
||||
throw new ArrayIndexOutOfBoundsException(
|
||||
String.format("访问harmonicData[%d]越界, 数组长度=%d", i, harmonicData.length));
|
||||
}
|
||||
|
||||
// 对应C代码行443-447
|
||||
if (harmonicData[i] > threshold) {
|
||||
HKSum[j] += responsibilityData[i][j]; // 对应C代码行445
|
||||
exceedCount++; // 对应C代码行446
|
||||
}
|
||||
}
|
||||
}
|
||||
// 注意:这里exceedCount保留的是最后一列(j=columnCount-1)的超限次数
|
||||
// 这与C代码的行为一致
|
||||
|
||||
logger.debug("最终exceedCount={} (来自最后一列的计算)", exceedCount);
|
||||
|
||||
// 计算平均责任百分比 - 对应C代码行453-459
|
||||
for (int i = 0; i < columnCount; i++) {
|
||||
sumData[i] = 0; // 对应C代码行454
|
||||
}
|
||||
|
||||
for (int i = 0; i < columnCount; i++) {
|
||||
if (exceedCount > 0) {
|
||||
// 对应C代码行458: arrHKsum[i] = 100 * (HKSum(i)/coutt);
|
||||
// 使用double进行计算,然后转换为float
|
||||
sumData[i] = (float)(100.0 * (HKSum[i] / (double)exceedCount));
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("Exceeded count: {}, average responsibilities calculated", exceedCount);
|
||||
|
||||
return sumData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算超限时段的责任总和(兼容版本)
|
||||
* 为了向后兼容,保留不带tl_num参数的版本
|
||||
*
|
||||
* @param responsibilityData 责任数据矩阵(FK或HK)[时间][节点]
|
||||
* @param harmonicData 谐波数据(Udata)
|
||||
* @param threshold 谐波门槛(XIANE)
|
||||
* @param windowSize 窗口大小
|
||||
* @param columnCount 列数(节点数或节点数+1)
|
||||
* @return 各节点的责任总和百分比
|
||||
*/
|
||||
public static float[] sumResponsibility(float[][] responsibilityData, float[] harmonicData,
|
||||
float threshold, int windowSize, int columnCount) {
|
||||
// 如果没有提供tl_num,从数据推断
|
||||
int tl_num = responsibilityData.length + windowSize;
|
||||
return sumResponsibility(responsibilityData, harmonicData, threshold, windowSize, columnCount, tl_num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算所有节点的动态相关系数矩阵
|
||||
* 这个函数在主引擎中已经内联实现,这里保留作为辅助方法
|
||||
*
|
||||
* @param powerData 功率数据矩阵
|
||||
* @param harmonicData 谐波数据
|
||||
* @param canonicalCorr 典则相关系数序列
|
||||
* @param windowSize 窗口大小
|
||||
* @param nodeCount 节点数量
|
||||
* @return 动态相关系数矩阵
|
||||
*/
|
||||
public static float[][] computeCorrelationMatrix(float[][] powerData, float[] harmonicData,
|
||||
float[] canonicalCorr, int windowSize,
|
||||
int nodeCount) {
|
||||
int slideLength = canonicalCorr.length;
|
||||
float[][] correlationMatrix = new float[slideLength][nodeCount];
|
||||
|
||||
logger.info("Computing correlation matrix for all nodes");
|
||||
|
||||
for (int nodeIdx = 0; nodeIdx < nodeCount; nodeIdx++) {
|
||||
// 提取单个节点的功率数据
|
||||
float[] nodePower = new float[powerData.length];
|
||||
for (int i = 0; i < powerData.length; i++) {
|
||||
nodePower[i] = powerData[i][nodeIdx];
|
||||
}
|
||||
|
||||
// 计算该节点的动态相关系数
|
||||
float[] nodeCorr = com.njcn.advance.responsibility.analysis.CanonicalCorrelationAnalysis
|
||||
.slidingCorrelation(nodePower, harmonicData, canonicalCorr, windowSize);
|
||||
|
||||
// 存储结果
|
||||
for (int i = 0; i < slideLength; i++) {
|
||||
correlationMatrix[i][nodeIdx] = nodeCorr[i];
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("Correlation matrix computation completed");
|
||||
|
||||
return correlationMatrix;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.njcn.advance.responsibility.constant;
|
||||
|
||||
/**
|
||||
* 计算模式枚举
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public enum CalculationMode {
|
||||
/**
|
||||
* 完整计算模式
|
||||
* 使用电压和功率数据计算相关系数和责任
|
||||
*/
|
||||
FULL_CALCULATION(0, "完整计算模式"),
|
||||
|
||||
/**
|
||||
* 部分重算模式
|
||||
* 使用已有的动态相关系数计算责任
|
||||
*/
|
||||
PARTIAL_RECALCULATION(1, "部分重算模式");
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
|
||||
CalculationMode(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public static CalculationMode fromCode(int code) {
|
||||
for (CalculationMode mode : values()) {
|
||||
if (mode.code == code) {
|
||||
return mode;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid calculation mode code: " + code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.njcn.advance.responsibility.constant;
|
||||
|
||||
/**
|
||||
* 计算状态枚举
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public enum CalculationStatus {
|
||||
/**
|
||||
* 未计算
|
||||
*/
|
||||
NOT_CALCULATED(0, "未计算"),
|
||||
|
||||
/**
|
||||
* 计算完成
|
||||
*/
|
||||
CALCULATED(1, "计算完成"),
|
||||
|
||||
/**
|
||||
* 计算失败
|
||||
*/
|
||||
FAILED(-1, "计算失败");
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
|
||||
CalculationStatus(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public static CalculationStatus fromCode(int code) {
|
||||
for (CalculationStatus status : values()) {
|
||||
if (status.code == code) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return NOT_CALCULATED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.njcn.advance.responsibility.constant;
|
||||
|
||||
/**
|
||||
* 谐波责任量化系统常量定义
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public final class HarmonicConstants {
|
||||
|
||||
private HarmonicConstants() {
|
||||
// 防止实例化
|
||||
}
|
||||
|
||||
/**
|
||||
* 最大谐波数据个数 (1440*100)
|
||||
* 按一分钟间隔,100天处理
|
||||
*/
|
||||
public static final int MAX_HARM_NUM = 144000;
|
||||
|
||||
/**
|
||||
* 最大功率数据个数 (96*100)
|
||||
* 按15分钟间隔,100天处理
|
||||
*/
|
||||
public static final int MAX_P_NUM = 9600;
|
||||
|
||||
/**
|
||||
* 最大功率节点个数
|
||||
* 按200个限制
|
||||
*/
|
||||
public static final int MAX_P_NODE = 200;
|
||||
|
||||
/**
|
||||
* 最大数据窗长度 (96*10)
|
||||
* 按15分钟算10天
|
||||
*/
|
||||
public static final int MAX_WIN_LEN = 960;
|
||||
|
||||
/**
|
||||
* 最小数据窗长度
|
||||
* 按15分钟算一小时
|
||||
*/
|
||||
public static final int MIN_WIN_LEN = 4;
|
||||
|
||||
/**
|
||||
* 默认数据窗大小
|
||||
* 一天的数据量(15分钟间隔)
|
||||
*/
|
||||
public static final int DEFAULT_WINDOW_SIZE = 96;
|
||||
|
||||
/**
|
||||
* 数值计算精度阈值
|
||||
*/
|
||||
public static final double EPSILON = 1e-10;
|
||||
|
||||
/**
|
||||
* 协方差计算最小值(避免除零)
|
||||
*/
|
||||
public static final double MIN_COVARIANCE = 1e-5;
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
package com.njcn.advance.responsibility.model;
|
||||
|
||||
import com.njcn.advance.responsibility.constant.CalculationMode;
|
||||
import com.njcn.advance.responsibility.constant.CalculationStatus;
|
||||
import com.njcn.advance.responsibility.constant.HarmonicConstants;
|
||||
|
||||
/**
|
||||
* 谐波数据结构类
|
||||
* 对应C语言中的harm_data_struct结构体
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public class HarmonicData {
|
||||
|
||||
// 输入参数
|
||||
private CalculationMode calculationMode; // 计算标志
|
||||
private int harmonicCount; // 谐波数据个数
|
||||
private int powerCount; // 功率数据个数
|
||||
private int powerNodeCount; // 功率负荷节点数
|
||||
private int windowSize; // 数据窗大小
|
||||
private int responsibilityDataCount; // 代入的责任数据个数
|
||||
private float harmonicThreshold; // 谐波电压门槛
|
||||
|
||||
// 数据数组
|
||||
private float[] harmonicData; // 谐波数据序列
|
||||
private float[][] powerData; // 功率数据序列
|
||||
|
||||
// 输入输出数据
|
||||
private float[][] correlationData; // 动态相关系数数据序列
|
||||
private float[][] fkData; // 不包含背景动态谐波责任数据序列
|
||||
private float[][] hkData; // 包含背景动态谐波责任数据序列
|
||||
private float[] canonicalCorrelation; // 典则相关系数
|
||||
private float[] backgroundCanonicalCorr; // 包含背景典则相关系数
|
||||
|
||||
// 输出结果
|
||||
private CalculationStatus calculationStatus; // 计算状态
|
||||
private float[] sumFKData; // 不包含背景谐波责任
|
||||
private float[] sumHKData; // 包含背景谐波责任
|
||||
|
||||
/**
|
||||
* 默认构造函数
|
||||
*/
|
||||
public HarmonicData() {
|
||||
this.calculationMode = CalculationMode.FULL_CALCULATION;
|
||||
this.calculationStatus = CalculationStatus.NOT_CALCULATED;
|
||||
this.windowSize = HarmonicConstants.DEFAULT_WINDOW_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder模式构造器
|
||||
*/
|
||||
public static class Builder {
|
||||
private HarmonicData data = new HarmonicData();
|
||||
|
||||
public Builder calculationMode(CalculationMode mode) {
|
||||
data.calculationMode = mode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder harmonicCount(int count) {
|
||||
data.harmonicCount = count;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder powerCount(int count) {
|
||||
data.powerCount = count;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder powerNodeCount(int count) {
|
||||
data.powerNodeCount = count;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder windowSize(int size) {
|
||||
data.windowSize = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder harmonicThreshold(float threshold) {
|
||||
data.harmonicThreshold = threshold;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder harmonicData(float[] data) {
|
||||
this.data.harmonicData = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder powerData(float[][] data) {
|
||||
this.data.powerData = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HarmonicData build() {
|
||||
// 验证数据
|
||||
validateData();
|
||||
// 初始化数组
|
||||
initializeArrays();
|
||||
return data;
|
||||
}
|
||||
|
||||
private void validateData() {
|
||||
if (data.harmonicCount <= 0 || data.harmonicCount > HarmonicConstants.MAX_HARM_NUM) {
|
||||
throw new IllegalArgumentException("Invalid harmonic count: " + data.harmonicCount);
|
||||
}
|
||||
if (data.powerCount <= 0 || data.powerCount > HarmonicConstants.MAX_P_NUM) {
|
||||
throw new IllegalArgumentException("Invalid power count: " + data.powerCount);
|
||||
}
|
||||
if (data.powerNodeCount <= 0 || data.powerNodeCount > HarmonicConstants.MAX_P_NODE) {
|
||||
throw new IllegalArgumentException("Invalid power node count: " + data.powerNodeCount);
|
||||
}
|
||||
if (data.windowSize < HarmonicConstants.MIN_WIN_LEN ||
|
||||
data.windowSize > HarmonicConstants.MAX_WIN_LEN) {
|
||||
throw new IllegalArgumentException("Invalid window size: " + data.windowSize);
|
||||
}
|
||||
|
||||
// 验证数据对齐
|
||||
if (data.calculationMode == CalculationMode.FULL_CALCULATION) {
|
||||
int ratio = data.harmonicCount / data.powerCount;
|
||||
if (ratio * data.powerCount != data.harmonicCount || ratio < 1) {
|
||||
throw new IllegalArgumentException("Harmonic data count must be integer multiple of power data count");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeArrays() {
|
||||
if (data.harmonicData == null) {
|
||||
data.harmonicData = new float[data.harmonicCount];
|
||||
}
|
||||
if (data.powerData == null) {
|
||||
data.powerData = new float[data.powerCount][data.powerNodeCount];
|
||||
}
|
||||
|
||||
int resultCount = data.powerCount - data.windowSize;
|
||||
if (resultCount > 0) {
|
||||
data.correlationData = new float[resultCount][data.powerNodeCount];
|
||||
data.fkData = new float[resultCount][data.powerNodeCount];
|
||||
data.hkData = new float[resultCount][data.powerNodeCount + 1];
|
||||
data.canonicalCorrelation = new float[resultCount];
|
||||
data.backgroundCanonicalCorr = new float[resultCount];
|
||||
}
|
||||
|
||||
data.sumFKData = new float[data.powerNodeCount];
|
||||
data.sumHKData = new float[data.powerNodeCount + 1];
|
||||
}
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
public CalculationMode getCalculationMode() {
|
||||
return calculationMode;
|
||||
}
|
||||
|
||||
public void setCalculationMode(CalculationMode calculationMode) {
|
||||
this.calculationMode = calculationMode;
|
||||
}
|
||||
|
||||
public int getHarmonicCount() {
|
||||
return harmonicCount;
|
||||
}
|
||||
|
||||
public void setHarmonicCount(int harmonicCount) {
|
||||
this.harmonicCount = harmonicCount;
|
||||
}
|
||||
|
||||
public int getPowerCount() {
|
||||
return powerCount;
|
||||
}
|
||||
|
||||
public void setPowerCount(int powerCount) {
|
||||
this.powerCount = powerCount;
|
||||
}
|
||||
|
||||
public int getPowerNodeCount() {
|
||||
return powerNodeCount;
|
||||
}
|
||||
|
||||
public void setPowerNodeCount(int powerNodeCount) {
|
||||
this.powerNodeCount = powerNodeCount;
|
||||
}
|
||||
|
||||
public int getWindowSize() {
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
public void setWindowSize(int windowSize) {
|
||||
this.windowSize = windowSize;
|
||||
}
|
||||
|
||||
public int getResponsibilityDataCount() {
|
||||
return responsibilityDataCount;
|
||||
}
|
||||
|
||||
public void setResponsibilityDataCount(int responsibilityDataCount) {
|
||||
this.responsibilityDataCount = responsibilityDataCount;
|
||||
}
|
||||
|
||||
public float getHarmonicThreshold() {
|
||||
return harmonicThreshold;
|
||||
}
|
||||
|
||||
public void setHarmonicThreshold(float harmonicThreshold) {
|
||||
this.harmonicThreshold = harmonicThreshold;
|
||||
}
|
||||
|
||||
public float[] getHarmonicData() {
|
||||
return harmonicData;
|
||||
}
|
||||
|
||||
public void setHarmonicData(float[] harmonicData) {
|
||||
this.harmonicData = harmonicData;
|
||||
}
|
||||
|
||||
public float[][] getPowerData() {
|
||||
return powerData;
|
||||
}
|
||||
|
||||
public void setPowerData(float[][] powerData) {
|
||||
this.powerData = powerData;
|
||||
}
|
||||
|
||||
public float[][] getCorrelationData() {
|
||||
return correlationData;
|
||||
}
|
||||
|
||||
public void setCorrelationData(float[][] correlationData) {
|
||||
this.correlationData = correlationData;
|
||||
}
|
||||
|
||||
public float[][] getFkData() {
|
||||
return fkData;
|
||||
}
|
||||
|
||||
public void setFkData(float[][] fkData) {
|
||||
this.fkData = fkData;
|
||||
}
|
||||
|
||||
public float[][] getHkData() {
|
||||
return hkData;
|
||||
}
|
||||
|
||||
public void setHkData(float[][] hkData) {
|
||||
this.hkData = hkData;
|
||||
}
|
||||
|
||||
public float[] getCanonicalCorrelation() {
|
||||
return canonicalCorrelation;
|
||||
}
|
||||
|
||||
public void setCanonicalCorrelation(float[] canonicalCorrelation) {
|
||||
this.canonicalCorrelation = canonicalCorrelation;
|
||||
}
|
||||
|
||||
public float[] getBackgroundCanonicalCorr() {
|
||||
return backgroundCanonicalCorr;
|
||||
}
|
||||
|
||||
public void setBackgroundCanonicalCorr(float[] backgroundCanonicalCorr) {
|
||||
this.backgroundCanonicalCorr = backgroundCanonicalCorr;
|
||||
}
|
||||
|
||||
public CalculationStatus getCalculationStatus() {
|
||||
return calculationStatus;
|
||||
}
|
||||
|
||||
public void setCalculationStatus(CalculationStatus calculationStatus) {
|
||||
this.calculationStatus = calculationStatus;
|
||||
}
|
||||
|
||||
public float[] getSumFKData() {
|
||||
return sumFKData;
|
||||
}
|
||||
|
||||
public void setSumFKData(float[] sumFKData) {
|
||||
this.sumFKData = sumFKData;
|
||||
}
|
||||
|
||||
public float[] getSumHKData() {
|
||||
return sumHKData;
|
||||
}
|
||||
|
||||
public void setSumHKData(float[] sumHKData) {
|
||||
this.sumHKData = sumHKData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.njcn.advance.responsibility.service;
|
||||
|
||||
import com.njcn.advance.responsibility.model.HarmonicData;
|
||||
|
||||
/**
|
||||
* 谐波责任计算服务接口
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface IHarmonicResponsibilityService {
|
||||
|
||||
/**
|
||||
* 执行谐波责任计算
|
||||
*
|
||||
* @param data 输入的谐波数据
|
||||
* @return 计算是否成功
|
||||
*/
|
||||
boolean calculate(HarmonicData data);
|
||||
|
||||
/**
|
||||
* 执行完整计算
|
||||
*
|
||||
* @param harmonicData 谐波数据数组
|
||||
* @param powerData 功率数据矩阵
|
||||
* @param harmonicCount 谐波数据个数
|
||||
* @param powerCount 功率数据个数
|
||||
* @param nodeCount 节点数量
|
||||
* @param windowSize 窗口大小
|
||||
* @param threshold 谐波门槛
|
||||
* @return 计算结果
|
||||
*/
|
||||
HarmonicData fullCalculation(float[] harmonicData, float[][] powerData,
|
||||
int harmonicCount, int powerCount, int nodeCount,
|
||||
int windowSize, float threshold);
|
||||
|
||||
/**
|
||||
* 执行部分重算
|
||||
*
|
||||
* @param harmonicData 谐波数据数组
|
||||
* @param fkData FK数据矩阵
|
||||
* @param hkData HK数据矩阵
|
||||
* @param harmonicCount 谐波数据个数
|
||||
* @param nodeCount 节点数量
|
||||
* @param windowSize 窗口大小
|
||||
* @param responsibilityCount 责任数据个数
|
||||
* @param threshold 谐波门槛
|
||||
* @return 计算结果
|
||||
*/
|
||||
HarmonicData partialCalculation(float[] harmonicData, float[][] fkData, float[][] hkData,
|
||||
int harmonicCount, int nodeCount, int windowSize,
|
||||
int responsibilityCount, float threshold);
|
||||
|
||||
/**
|
||||
* 验证输入数据的有效性
|
||||
*
|
||||
* @param data 待验证的数据
|
||||
* @return 验证结果消息,null表示验证通过
|
||||
*/
|
||||
String validateData(HarmonicData data);
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package com.njcn.advance.responsibility.service.impl;
|
||||
|
||||
import com.njcn.advance.responsibility.calculator.HarmonicCalculationEngine;
|
||||
import com.njcn.advance.responsibility.constant.CalculationMode;
|
||||
import com.njcn.advance.responsibility.constant.HarmonicConstants;
|
||||
import com.njcn.advance.responsibility.model.HarmonicData;
|
||||
import com.njcn.advance.responsibility.service.IHarmonicResponsibilityService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 谐波责任计算服务实现类
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class HarmonicResponsibilityServiceImpl implements IHarmonicResponsibilityService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HarmonicResponsibilityServiceImpl.class);
|
||||
|
||||
private final HarmonicCalculationEngine engine;
|
||||
|
||||
public HarmonicResponsibilityServiceImpl() {
|
||||
this.engine = new HarmonicCalculationEngine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean calculate(HarmonicData data) {
|
||||
if (data == null) {
|
||||
logger.error("Input data is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
String validationError = validateData(data);
|
||||
if (validationError != null) {
|
||||
logger.error("Data validation failed: {}", validationError);
|
||||
return false;
|
||||
}
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
boolean result = engine.calculate(data);
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
logger.info("Calculation completed in {} ms, result: {}", (endTime - startTime), result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HarmonicData fullCalculation(float[] harmonicData, float[][] powerData,
|
||||
int harmonicCount, int powerCount, int nodeCount,
|
||||
int windowSize, float threshold) {
|
||||
logger.info("Starting full calculation with harmonicCount={}, powerCount={}, nodeCount={}, windowSize={}",
|
||||
harmonicCount, powerCount, nodeCount, windowSize);
|
||||
|
||||
HarmonicData data = new HarmonicData.Builder()
|
||||
.calculationMode(CalculationMode.FULL_CALCULATION)
|
||||
.harmonicCount(harmonicCount)
|
||||
.powerCount(powerCount)
|
||||
.powerNodeCount(nodeCount)
|
||||
.windowSize(windowSize)
|
||||
.harmonicThreshold(threshold)
|
||||
.harmonicData(harmonicData)
|
||||
.powerData(powerData)
|
||||
.build();
|
||||
|
||||
calculate(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HarmonicData partialCalculation(float[] harmonicData, float[][] fkData, float[][] hkData,
|
||||
int harmonicCount, int nodeCount, int windowSize,
|
||||
int responsibilityCount, float threshold) {
|
||||
logger.info("Starting partial calculation with harmonicCount={}, nodeCount={}, windowSize={}, responsibilityCount={}",
|
||||
harmonicCount, nodeCount, windowSize, responsibilityCount);
|
||||
|
||||
HarmonicData data = new HarmonicData();
|
||||
data.setCalculationMode(CalculationMode.PARTIAL_RECALCULATION);
|
||||
data.setHarmonicCount(harmonicCount);
|
||||
data.setPowerNodeCount(nodeCount);
|
||||
data.setWindowSize(windowSize);
|
||||
data.setResponsibilityDataCount(responsibilityCount);
|
||||
data.setHarmonicThreshold(threshold);
|
||||
data.setHarmonicData(harmonicData);
|
||||
data.setFkData(fkData);
|
||||
data.setHkData(hkData);
|
||||
|
||||
// 初始化输出数组
|
||||
data.setSumFKData(new float[nodeCount]);
|
||||
data.setSumHKData(new float[nodeCount + 1]);
|
||||
|
||||
calculate(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String validateData(HarmonicData data) {
|
||||
if (data == null) {
|
||||
return "Data object is null";
|
||||
}
|
||||
|
||||
// 验证基本参数
|
||||
if (data.getHarmonicCount() <= 0 || data.getHarmonicCount() > HarmonicConstants.MAX_HARM_NUM) {
|
||||
return String.format("Invalid harmonic count: %d (should be 1-%d)",
|
||||
data.getHarmonicCount(), HarmonicConstants.MAX_HARM_NUM);
|
||||
}
|
||||
|
||||
if (data.getCalculationMode() == CalculationMode.FULL_CALCULATION) {
|
||||
// 完整计算模式验证
|
||||
if (data.getPowerCount() <= 0 || data.getPowerCount() > HarmonicConstants.MAX_P_NUM) {
|
||||
return String.format("Invalid power count: %d (should be 1-%d)",
|
||||
data.getPowerCount(), HarmonicConstants.MAX_P_NUM);
|
||||
}
|
||||
|
||||
if (data.getPowerNodeCount() <= 0 || data.getPowerNodeCount() > HarmonicConstants.MAX_P_NODE) {
|
||||
return String.format("Invalid power node count: %d (should be 1-%d)",
|
||||
data.getPowerNodeCount(), HarmonicConstants.MAX_P_NODE);
|
||||
}
|
||||
|
||||
// 验证数据对齐
|
||||
int ratio = data.getHarmonicCount() / data.getPowerCount();
|
||||
if (ratio * data.getPowerCount() != data.getHarmonicCount()) {
|
||||
return String.format("Harmonic count %d is not aligned with power count %d",
|
||||
data.getHarmonicCount(), data.getPowerCount());
|
||||
}
|
||||
|
||||
// 验证数据数组
|
||||
if (data.getHarmonicData() == null || data.getHarmonicData().length < data.getHarmonicCount()) {
|
||||
return "Harmonic data array is null or insufficient";
|
||||
}
|
||||
|
||||
if (data.getPowerData() == null || data.getPowerData().length < data.getPowerCount()) {
|
||||
return "Power data array is null or insufficient";
|
||||
}
|
||||
|
||||
} else if (data.getCalculationMode() == CalculationMode.PARTIAL_RECALCULATION) {
|
||||
// 部分计算模式验证
|
||||
if (data.getResponsibilityDataCount() + data.getWindowSize() != data.getHarmonicCount()) {
|
||||
return String.format("Data length mismatch: resNum(%d) + winSize(%d) != harmCount(%d)",
|
||||
data.getResponsibilityDataCount(), data.getWindowSize(), data.getHarmonicCount());
|
||||
}
|
||||
|
||||
if (data.getFkData() == null || data.getHkData() == null) {
|
||||
return "FK or HK data is null for partial calculation";
|
||||
}
|
||||
}
|
||||
|
||||
// 验证窗口大小
|
||||
if (data.getWindowSize() < HarmonicConstants.MIN_WIN_LEN ||
|
||||
data.getWindowSize() > HarmonicConstants.MAX_WIN_LEN) {
|
||||
return String.format("Invalid window size: %d (should be %d-%d)",
|
||||
data.getWindowSize(), HarmonicConstants.MIN_WIN_LEN, HarmonicConstants.MAX_WIN_LEN);
|
||||
}
|
||||
|
||||
return null; // 验证通过
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
package com.njcn.advance.responsibility.utils;
|
||||
|
||||
import com.njcn.advance.responsibility.constant.HarmonicConstants;
|
||||
import org.apache.commons.math3.linear.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 数学工具类
|
||||
* 提供基础数学计算功能
|
||||
*
|
||||
* @author hongawen
|
||||
* @version 1.0
|
||||
*/
|
||||
public class MathUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MathUtils.class);
|
||||
|
||||
/**
|
||||
* 计算协方差
|
||||
*
|
||||
* @param x 数据序列1
|
||||
* @param y 数据序列2
|
||||
* @param width 数据窗宽度
|
||||
* @return 协方差值
|
||||
*/
|
||||
public static double covariance(double[] x, double[] y, int width) {
|
||||
if (x == null || y == null || width <= 0) {
|
||||
throw new IllegalArgumentException("Invalid input parameters for covariance calculation");
|
||||
}
|
||||
|
||||
if (x.length < width || y.length < width) {
|
||||
throw new IllegalArgumentException("Data length is less than window width");
|
||||
}
|
||||
|
||||
double meanX = 0.0;
|
||||
double meanY = 0.0;
|
||||
|
||||
// 计算均值
|
||||
for (int i = 0; i < width; i++) {
|
||||
meanX += x[i];
|
||||
meanY += y[i];
|
||||
}
|
||||
meanX /= width;
|
||||
meanY /= width;
|
||||
|
||||
// 计算协方差
|
||||
double cov = 0.0;
|
||||
for (int i = 0; i < width; i++) {
|
||||
cov += (x[i] - meanX) * (y[i] - meanY);
|
||||
}
|
||||
|
||||
return cov / (width - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算协方差(float版本)
|
||||
*/
|
||||
public static float covariance(float[] x, float[] y, int width) {
|
||||
double[] dx = new double[x.length];
|
||||
double[] dy = new double[y.length];
|
||||
for (int i = 0; i < x.length; i++) dx[i] = x[i];
|
||||
for (int i = 0; i < y.length; i++) dy[i] = y[i];
|
||||
return (float) covariance(dx, dy, width);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算Pearson相关系数
|
||||
*
|
||||
* @param x 数据序列1
|
||||
* @param y 数据序列2
|
||||
* @param count 数据长度
|
||||
* @return Pearson相关系数
|
||||
*/
|
||||
public static double pearsonCorrelation(double[] x, double[] y, int count) {
|
||||
if (x == null || y == null || count <= 0) {
|
||||
throw new IllegalArgumentException("Invalid input parameters for Pearson correlation");
|
||||
}
|
||||
|
||||
double meanX = 0.0;
|
||||
double meanY = 0.0;
|
||||
|
||||
// 计算均值
|
||||
for (int i = 0; i < count; i++) {
|
||||
meanX += x[i];
|
||||
meanY += y[i];
|
||||
}
|
||||
meanX /= count;
|
||||
meanY /= count;
|
||||
|
||||
// 计算相关系数的各个部分
|
||||
double numerator = 0.0;
|
||||
double denomX = 0.0;
|
||||
double denomY = 0.0;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
double dx = x[i] - meanX;
|
||||
double dy = y[i] - meanY;
|
||||
numerator += dx * dy;
|
||||
denomX += dx * dx;
|
||||
denomY += dy * dy;
|
||||
}
|
||||
|
||||
double denominator = Math.sqrt(denomX * denomY);
|
||||
|
||||
if (Math.abs(denominator) < HarmonicConstants.EPSILON) {
|
||||
logger.warn("Denominator is too small in Pearson correlation calculation");
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return numerator / denominator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算Pearson相关系数(float版本)
|
||||
*/
|
||||
public static float pearsonCorrelation(float[] x, float[] y, int count) {
|
||||
double[] dx = new double[count];
|
||||
double[] dy = new double[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
dx[i] = x[i];
|
||||
dy[i] = y[i];
|
||||
}
|
||||
return (float) pearsonCorrelation(dx, dy, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算协方差矩阵(SXX)
|
||||
*
|
||||
* @param data 数据矩阵 [时间][节点]
|
||||
* @param width 窗口宽度
|
||||
* @param nodeCount 节点数
|
||||
* @return 协方差矩阵
|
||||
*/
|
||||
public static double[][] covarianceMatrix(double[][] data, int width, int nodeCount) {
|
||||
double[][] covMatrix = new double[nodeCount][nodeCount];
|
||||
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
double[] col1 = new double[width];
|
||||
double[] col2 = new double[width];
|
||||
|
||||
for (int k = 0; k < width; k++) {
|
||||
col1[k] = data[k][i];
|
||||
col2[k] = data[k][j];
|
||||
}
|
||||
|
||||
covMatrix[i][j] = covariance(col1, col2, width);
|
||||
}
|
||||
}
|
||||
|
||||
return covMatrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算协方差矩阵(float版本)
|
||||
*/
|
||||
public static float[][] covarianceMatrix(float[][] data, int width, int nodeCount) {
|
||||
float[][] covMatrix = new float[nodeCount][nodeCount];
|
||||
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
for (int j = 0; j < nodeCount; j++) {
|
||||
float[] col1 = new float[width];
|
||||
float[] col2 = new float[width];
|
||||
|
||||
for (int k = 0; k < width; k++) {
|
||||
col1[k] = data[k][i];
|
||||
col2[k] = data[k][j];
|
||||
}
|
||||
|
||||
covMatrix[i][j] = covariance(col1, col2, width);
|
||||
}
|
||||
}
|
||||
|
||||
return covMatrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算协方差向量(SXY)
|
||||
*
|
||||
* @param data 数据矩阵 [时间][节点]
|
||||
* @param y 目标向量
|
||||
* @param width 窗口宽度
|
||||
* @param nodeCount 节点数
|
||||
* @return 协方差向量
|
||||
*/
|
||||
public static double[] covarianceVector(double[][] data, double[] y, int width, int nodeCount) {
|
||||
double[] covVector = new double[nodeCount];
|
||||
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
double[] col = new double[width];
|
||||
for (int k = 0; k < width; k++) {
|
||||
col[k] = data[k][i];
|
||||
}
|
||||
covVector[i] = covariance(col, y, width);
|
||||
}
|
||||
|
||||
return covVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算协方差向量(float版本)
|
||||
*/
|
||||
public static float[] covarianceVector(float[][] data, float[] y, int width, int nodeCount) {
|
||||
float[] covVector = new float[nodeCount];
|
||||
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
float[] col = new float[width];
|
||||
for (int k = 0; k < width; k++) {
|
||||
col[k] = data[k][i];
|
||||
}
|
||||
covVector[i] = covariance(col, y, width);
|
||||
}
|
||||
|
||||
return covVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* 矩阵求逆
|
||||
* 使用Apache Commons Math库
|
||||
*
|
||||
* @param matrix 输入矩阵
|
||||
* @return 逆矩阵
|
||||
*/
|
||||
public static double[][] matrixInverse(double[][] matrix) {
|
||||
RealMatrix realMatrix = new Array2DRowRealMatrix(matrix);
|
||||
|
||||
try {
|
||||
// 使用LU分解求逆
|
||||
DecompositionSolver solver = new LUDecomposition(realMatrix).getSolver();
|
||||
RealMatrix inverseMatrix = solver.getInverse();
|
||||
return inverseMatrix.getData();
|
||||
} catch (SingularMatrixException e) {
|
||||
logger.error("Matrix is singular, cannot compute inverse", e);
|
||||
throw new RuntimeException("Matrix inversion failed: singular matrix");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算矩阵的特征值
|
||||
*
|
||||
* @param matrix 输入矩阵
|
||||
* @return 特征值数组
|
||||
*/
|
||||
public static double[] eigenvalues(double[][] matrix) {
|
||||
RealMatrix realMatrix = new Array2DRowRealMatrix(matrix);
|
||||
EigenDecomposition eigenDecomposition = new EigenDecomposition(realMatrix);
|
||||
return eigenDecomposition.getRealEigenvalues();
|
||||
}
|
||||
|
||||
/**
|
||||
* 归一化处理
|
||||
* 将数据归一化到[0,1]区间
|
||||
*
|
||||
* @param data 输入数据
|
||||
* @return 归一化后的数据
|
||||
*/
|
||||
public static double[] normalize(double[] data) {
|
||||
if (data == null || data.length == 0) {
|
||||
return data;
|
||||
}
|
||||
|
||||
double min = Double.MAX_VALUE;
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
// 找最大最小值
|
||||
for (double value : data) {
|
||||
min = Math.min(min, value);
|
||||
max = Math.max(max, value);
|
||||
}
|
||||
|
||||
double range = max - min;
|
||||
if (Math.abs(range) < HarmonicConstants.EPSILON) {
|
||||
return new double[data.length]; // 返回全0数组
|
||||
}
|
||||
|
||||
double[] normalized = new double[data.length];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
normalized[i] = (data[i] - min) / range;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据对齐处理
|
||||
* 将不同采样间隔的数据对齐到相同的时间间隔
|
||||
*
|
||||
* @param data 原始数据
|
||||
* @param originalInterval 原始采样间隔
|
||||
* @param targetInterval 目标采样间隔
|
||||
* @return 对齐后的数据
|
||||
*/
|
||||
public static float[] alignData(float[] data, int originalInterval, int targetInterval) {
|
||||
if (targetInterval % originalInterval != 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Target interval must be multiple of original interval");
|
||||
}
|
||||
|
||||
int ratio = targetInterval / originalInterval;
|
||||
int newLength = data.length / ratio;
|
||||
float[] alignedData = new float[newLength];
|
||||
|
||||
for (int i = 0; i < newLength; i++) {
|
||||
float sum = 0;
|
||||
for (int j = 0; j < ratio; j++) {
|
||||
sum += data[i * ratio + j];
|
||||
}
|
||||
alignedData[i] = sum / ratio;
|
||||
}
|
||||
|
||||
return alignedData;
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,12 @@ import java.util.List;
|
||||
*/
|
||||
public interface IRespDataService extends IService<RespData> {
|
||||
|
||||
ResponsibilityResult getDynamicDataOld(ResponsibilityCalculateParam responsibilityCalculateParam);
|
||||
|
||||
ResponsibilityResult getDynamicData(ResponsibilityCalculateParam responsibilityCalculateParam);
|
||||
|
||||
ResponsibilityResult getResponsibilityDataOld(ResponsibilitySecondCalParam responsibilitySecondCalParam);
|
||||
|
||||
ResponsibilityResult getResponsibilityData(ResponsibilitySecondCalParam responsibilitySecondCalParam);
|
||||
|
||||
Page<RespDataDTO> responsibilityList(BaseParam queryParam);
|
||||
|
||||
@@ -27,6 +27,9 @@ import com.njcn.advance.pojo.param.ResponsibilityCalculateParam;
|
||||
import com.njcn.advance.pojo.param.ResponsibilitySecondCalParam;
|
||||
import com.njcn.advance.pojo.po.responsibility.RespData;
|
||||
import com.njcn.advance.pojo.po.responsibility.RespDataResult;
|
||||
import com.njcn.advance.responsibility.constant.CalculationStatus;
|
||||
import com.njcn.advance.responsibility.model.HarmonicData;
|
||||
import com.njcn.advance.responsibility.service.IHarmonicResponsibilityService;
|
||||
import com.njcn.advance.service.responsibility.IRespDataResultService;
|
||||
import com.njcn.advance.service.responsibility.IRespDataService;
|
||||
import com.njcn.advance.service.responsibility.IRespUserDataService;
|
||||
@@ -91,6 +94,8 @@ public class RespDataServiceImpl extends ServiceImpl<RespDataMapper, RespData> i
|
||||
|
||||
private final CommTerminalGeneralClient commTerminalGeneralClient;
|
||||
|
||||
private final IHarmonicResponsibilityService harmonicResponsibilityService;
|
||||
|
||||
public final static int SORT_10 = 10;
|
||||
public final static int INTERVAL_TIME_1 = 1;
|
||||
public final static int INTERVAL_TIME_3 = 3;
|
||||
@@ -161,7 +166,8 @@ public class RespDataServiceImpl extends ServiceImpl<RespDataMapper, RespData> i
|
||||
|
||||
|
||||
@Override
|
||||
public ResponsibilityResult getDynamicData(ResponsibilityCalculateParam responsibilityCalculateParam) {
|
||||
@Deprecated
|
||||
public ResponsibilityResult getDynamicDataOld(ResponsibilityCalculateParam responsibilityCalculateParam) {
|
||||
ResponsibilityResult result = new ResponsibilityResult();
|
||||
//调用c++依赖需要待初始化的参数
|
||||
int pNode, pNum, win, harmNum;
|
||||
@@ -419,7 +425,268 @@ public class RespDataServiceImpl extends ServiceImpl<RespDataMapper, RespData> i
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponsibilityResult getResponsibilityData(ResponsibilitySecondCalParam responsibilitySecondCalParam) {
|
||||
public ResponsibilityResult getDynamicData(ResponsibilityCalculateParam responsibilityCalculateParam) {
|
||||
ResponsibilityResult result = new ResponsibilityResult();
|
||||
//调用c++依赖需要待初始化的参数
|
||||
int pNode, pNum, win, harmNum;
|
||||
float harmMk;
|
||||
List<UserDataExcel> userDataExcels = respUserDataService.getUserDataExcelList(responsibilityCalculateParam.getUserDataId());
|
||||
//开始处理,根据接口参数需求,需要节点数(用户数,用户名+监测点号为一个用户),时间范围内功率数据
|
||||
DealDataResult dealDataResult = RespUserDataServiceImpl.getStanderData(userDataExcels, 1);
|
||||
List<String> dateStr = PubUtils.getTimes(DateUtil.parse(responsibilityCalculateParam.getSearchBeginTime(), DatePattern.NORM_DATE_PATTERN), DateUtil.parse(responsibilityCalculateParam.getSearchEndTime(), DatePattern.NORM_DATE_PATTERN));
|
||||
Map<String/*户号@监测点号@户名*/, Map<String/*yyyy-MM-dd天日期*/, List<UserDataExcel>>> finalData = getFinalUserData(dealDataResult, dateStr);
|
||||
//至此,finalData便是我们最终获得的用于计算责任数据,第一个参数节点数值pNode获取到
|
||||
//第一个参数pNode
|
||||
pNode = finalData.size();
|
||||
if (pNode < 1) {
|
||||
//没有合理的用采数据直接返回
|
||||
throw new BusinessException(AdvanceResponseEnum.USER_DATA_P_NODE_PARAMETER_ERROR);
|
||||
}
|
||||
//第二个参数pNum,根据起始时间和截止时间以及监测点测量间隔计算数量
|
||||
RespCommon pNumAndInterval = getPNumAndInterval(finalData, responsibilityCalculateParam.getLineId(), dateStr);
|
||||
pNum = pNumAndInterval.getPNum();
|
||||
int userIntervalTime = pNumAndInterval.getUserIntervalTime();
|
||||
int lineInterval = pNumAndInterval.getLineInterval();
|
||||
//第三个参数win,根据起始时间和截止时间的间隔
|
||||
if (dateStr.size() > 1) {
|
||||
if (userIntervalTime == INTERVAL_TIME_15) {
|
||||
win = WINDOW_96;
|
||||
} else {
|
||||
win = WINDOW_48;
|
||||
}
|
||||
} else {
|
||||
win = WINDOW_4;
|
||||
}
|
||||
//第四个参数harmMk,默认为0f
|
||||
harmMk = 0f;
|
||||
//第五个参数harmNum,与功率数据保持一致
|
||||
harmNum = pNum;
|
||||
//至此基础数据组装完毕,开始组装功率数据和谐波数据
|
||||
//先做谐波数据,理论上到这步的时候,谐波数据是满足完整性并已经补充完整性到100%,此处需要将谐波数据与功率数据长度匹配上
|
||||
RespHarmData respHarmData = getRespHarmData(responsibilityCalculateParam, lineInterval);
|
||||
//harmData填充完毕后,开始组装功率数据
|
||||
//首先获取当前时间内的各个用户的数据
|
||||
Map<String/*用户名*/, List<UserDataExcel>> originalPData = new HashMap<>(16);
|
||||
List<String> names = new ArrayList<>();
|
||||
Set<String> userNamesFinal = finalData.keySet();
|
||||
for (String userName : userNamesFinal) {
|
||||
List<UserDataExcel> tempData = new ArrayList<>();
|
||||
//根据日期将日期数据全部获取出来z
|
||||
Map<String, List<UserDataExcel>> tempResult = finalData.get(userName);
|
||||
for (String date : dateStr) {
|
||||
tempData.addAll(tempResult.get(date));
|
||||
}
|
||||
//按日期排序
|
||||
Collections.sort(tempData);
|
||||
originalPData.put(userName, tempData);
|
||||
names.add(userName);
|
||||
}
|
||||
//然后开始组装数据
|
||||
float[][] pData = new float[QvvrDataEntity.MAX_P_NUM][QvvrDataEntity.MAX_P_NODE];
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
//当前某用户测量节点的所有数据
|
||||
List<UserDataExcel> userDataExcelBodies1 = originalPData.get(names.get(i));
|
||||
for (int k = 0; k < userDataExcelBodies1.size(); k++) {
|
||||
float[] pDataStruct = pData[k];
|
||||
if (pDataStruct == null) {
|
||||
pDataStruct = new float[QvvrDataEntity.MAX_P_NODE];
|
||||
}
|
||||
float[] p = pDataStruct;
|
||||
p[i] = userDataExcelBodies1.get(k).getWork().floatValue();
|
||||
pData[k] = pDataStruct;
|
||||
}
|
||||
}
|
||||
//至此功率数据也组装完毕,调用友谊提供的接口
|
||||
// QvvrDataEntity qvvrDataEntity = new QvvrDataEntity();
|
||||
// qvvrDataEntity.calFlag = 0;
|
||||
// qvvrDataEntity.pNode = pNode;
|
||||
// qvvrDataEntity.pNum = pNum;
|
||||
// qvvrDataEntity.win = win;
|
||||
// qvvrDataEntity.harmNum = harmNum;
|
||||
// qvvrDataEntity.harmMk = harmMk;
|
||||
// qvvrDataEntity.pData = pData;
|
||||
// qvvrDataEntity.harmData = respHarmData.getHarmData();
|
||||
// ResponsibilityAlgorithm responsibilityAlgorithm = new ResponsibilityAlgorithm();
|
||||
// qvvrDataEntity = responsibilityAlgorithm.getResponsibilityResult(qvvrDataEntity);
|
||||
|
||||
HarmonicData harmonicData = harmonicResponsibilityService.fullCalculation(respHarmData.getHarmData(), pData, harmNum, pNum, pNode, win, harmMk);
|
||||
//至此接口调用结束,开始组装动态责任数据和用户责任量化结果
|
||||
//首先判断cal_ok的标识位是否为1,为0表示程序没有计算出结果
|
||||
if (harmonicData.getCalculationStatus() == CalculationStatus.FAILED) {
|
||||
throw new BusinessException(AdvanceResponseEnum.RESPONSIBILITY_PARAMETER_ERROR);
|
||||
}
|
||||
//没问题后,先玩动态责任数据
|
||||
CustomerData[] customerDatas = new CustomerData[harmonicData.getPowerNodeCount()];
|
||||
float[][] fKdata/*无背景的动态责任数据*/ = harmonicData.getFkData();
|
||||
//第一个时间节点是起始时间+win窗口得到的时间
|
||||
Date sTime = DateUtil.parse(dateStr.get(0).concat(" 00:00:00"), DatePattern.NORM_DATETIME_PATTERN);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(sTime);
|
||||
calendar.add(Calendar.MINUTE, (win - 1) * userIntervalTime);
|
||||
List<Long> timeDatas = new ArrayList<>();
|
||||
for (int i = 0; i < harmonicData.getPowerCount() - harmonicData.getWindowSize(); i++) {
|
||||
calendar.add(Calendar.MINUTE, userIntervalTime);
|
||||
//一个时间点所有的用户数据
|
||||
float[] fKdatum = fKdata[i];
|
||||
for (int k = 0; k < harmonicData.getPowerNodeCount(); k++) {
|
||||
CustomerData customerData = customerDatas[k];
|
||||
if (null == customerData) {
|
||||
customerData = new CustomerData();
|
||||
customerData.setCustomerName(names.get(k));
|
||||
}
|
||||
List<Float> valueDatas = customerData.getValueDatas();
|
||||
Float valueTemp = fKdatum[k];
|
||||
if (valueTemp.isNaN()) {
|
||||
valueTemp = 0.0f;
|
||||
}
|
||||
valueDatas.add(valueTemp);
|
||||
customerData.setValueDatas(valueDatas);
|
||||
customerDatas[k] = customerData;
|
||||
}
|
||||
timeDatas.add(calendar.getTimeInMillis());
|
||||
}
|
||||
//OK拿到所有测量点的数据了,现在就是看如何将相同户号的动态数据进行算术和求值,之前的用户name为:户号@测量点号@用户名
|
||||
Map<String/*用户名(户号)*/, List<CustomerData>> customerDataTemp = new HashMap<>(16);
|
||||
for (CustomerData data : customerDatas) {
|
||||
String customerName = data.getCustomerName();
|
||||
String[] customerInfo = customerName.split("@");
|
||||
String name = customerInfo[2] + "(" + customerInfo[0] + ")";
|
||||
List<CustomerData> customerData = customerDataTemp.get(name);
|
||||
CustomerData temp = data;
|
||||
temp.setCustomerName(name);
|
||||
if (CollectionUtils.isEmpty(customerData)) {
|
||||
customerData = new ArrayList<>();
|
||||
}
|
||||
customerData.add(temp);
|
||||
customerDataTemp.put(name, customerData);
|
||||
}
|
||||
//动态数据组装完成后,开始组装责任数据
|
||||
List<CustomerResponsibility> customerResponsibilities = getCustomerResponsibilityData(names, harmonicData.getSumFKData(), harmonicData.getPowerNodeCount());
|
||||
//根据前十的用户数据,获取这些用户的动态责任数据
|
||||
List<CustomerData> customerData = new ArrayList<>();
|
||||
for (CustomerResponsibility customerResponsibility : customerResponsibilities) {
|
||||
String cusName = customerResponsibility.getCustomerName();
|
||||
List<CustomerData> customerData1 = customerDataTemp.get(cusName);
|
||||
if (CollectionUtils.isEmpty(customerData1)) {
|
||||
continue;
|
||||
}
|
||||
if (customerData1.size() == 1) {
|
||||
//表示用户唯一的
|
||||
customerData.add(customerData1.get(0));
|
||||
} else {
|
||||
// 表示用户可能包含多个监测点号,需要进行数据累加
|
||||
CustomerData customerDataT = new CustomerData();
|
||||
customerDataT.setCustomerName(cusName);
|
||||
//进行数值累加
|
||||
List<Float> valueDatas = new ArrayList<>();
|
||||
for (int i = 0; i < customerData1.get(0).getValueDatas().size(); i++) {
|
||||
float original = 0.0f;
|
||||
for (CustomerData data : customerData1) {
|
||||
original = original + data.getValueDatas().get(i);
|
||||
}
|
||||
valueDatas.add(original);
|
||||
}
|
||||
customerDataT.setValueDatas(valueDatas);
|
||||
customerData.add(customerDataT);
|
||||
}
|
||||
}
|
||||
result.setDatas(customerData);
|
||||
result.setTimeDatas(timeDatas);
|
||||
result.setResponsibilities(customerResponsibilities);
|
||||
//此次的操作进行入库操作responsibilityData表数据
|
||||
//根据监测点名称+谐波框选的时间来查询,是否做过责任量化
|
||||
String timeWin = responsibilityCalculateParam.getSearchBeginTime().replaceAll(StrPool.DASHED, "").concat(StrPool.DASHED).concat(responsibilityCalculateParam.getSearchEndTime().replaceAll(StrPool.DASHED, ""));
|
||||
String type = responsibilityCalculateParam.getType() == 0 ? "谐波电流" : "谐波电压";
|
||||
//为了避免有监测点名称重复的,最终还是选择使用监测点索引来判断唯一性
|
||||
LambdaQueryWrapper<RespData> respDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
respDataLambdaQueryWrapper.eq(RespData::getLineId, responsibilityCalculateParam.getLineId())
|
||||
.eq(RespData::getUserDataId, responsibilityCalculateParam.getUserDataId())
|
||||
.eq(RespData::getTimeWindow, timeWin)
|
||||
.eq(RespData::getDataType, type)
|
||||
.eq(RespData::getState, DataStateEnum.ENABLE.getCode());
|
||||
List<RespData> responsibilityDataTemp = this.baseMapper.selectList(respDataLambdaQueryWrapper);
|
||||
RespData responsibilityData;
|
||||
if (CollectionUtils.isEmpty(responsibilityDataTemp)) {
|
||||
responsibilityData = new RespData();
|
||||
//库中没有记录则可以新建数据进行插入
|
||||
responsibilityData.setLineId(responsibilityCalculateParam.getLineId());
|
||||
responsibilityData.setUserDataId(responsibilityCalculateParam.getUserDataId());
|
||||
responsibilityData.setDataType(type);
|
||||
responsibilityData.setDataTimes(responsibilityCalculateParam.getTime().toString());
|
||||
responsibilityData.setTimeWindow(timeWin);
|
||||
responsibilityData.setState(DataStateEnum.ENABLE.getCode());
|
||||
//进行插入操作
|
||||
this.baseMapper.insert(responsibilityData);
|
||||
} else {
|
||||
//库中存在记录只需要判断次数进行数据更新
|
||||
responsibilityData = responsibilityDataTemp.get(0);
|
||||
String times = responsibilityData.getDataTimes();
|
||||
List<String> timesList = Stream.of(times.split(StrPool.COMMA)).collect(Collectors.toList());
|
||||
Integer time = responsibilityCalculateParam.getTime();
|
||||
if (!timesList.contains(time.toString())) {
|
||||
timesList.add(time.toString());
|
||||
timesList = timesList.stream().sorted().collect(Collectors.toList());
|
||||
responsibilityData.setDataTimes(String.join(StrPool.COMMA, timesList));
|
||||
}
|
||||
//执行更新操作
|
||||
this.baseMapper.updateById(responsibilityData);
|
||||
}
|
||||
//入库完毕之后,需要将必要数据进行序列化存储,方便后期的重复利用
|
||||
/*
|
||||
* 需要序列化三种数据结构 1 cal_flag置为1时需要的一些列参数的CacheQvvrData 2 cal_flag为0时的,动态结果。3 用户责任量化结果
|
||||
* 其中1/2都只需要一个文件即可
|
||||
* 3因为用户限值的变化调整,可能存在很多个文件,具体根据用户的选择而定
|
||||
*
|
||||
* 路径的结构为,temPath+userData+excelName+type+timeWin+lineIndex+time+文件名
|
||||
* 用户责任量化结果,需要再细化到限值
|
||||
*/
|
||||
//首先判断有没有存储记录,没有则存储,有就略过 指定测点、时间窗口、谐波类型、谐波次数判断唯一性
|
||||
LambdaQueryWrapper<RespDataResult> respDataResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
respDataResultLambdaQueryWrapper.eq(RespDataResult::getResDataId, responsibilityData.getId())
|
||||
.eq(RespDataResult::getTime, responsibilityCalculateParam.getTime())
|
||||
.eq(RespDataResult::getStartTime, DateUtil.parse(responsibilityCalculateParam.getSearchBeginTime() + " 00:00:00", DatePattern.NORM_DATETIME_PATTERN))
|
||||
.eq(RespDataResult::getEndTime, DateUtil.parse(responsibilityCalculateParam.getSearchEndTime() + " 23:59:59", DatePattern.NORM_DATETIME_PATTERN))
|
||||
.eq(RespDataResult::getLimitValue, respHarmData.getOverLimit());
|
||||
RespDataResult respDataResult = respDataResultService.getOne(respDataResultLambdaQueryWrapper);
|
||||
if (Objects.isNull(respDataResult)) {
|
||||
respDataResult = new RespDataResult();
|
||||
respDataResult.setResDataId(responsibilityData.getId());
|
||||
respDataResult.setTime(responsibilityCalculateParam.getTime());
|
||||
respDataResult.setStartTime(DateUtil.parse(responsibilityCalculateParam.getSearchBeginTime() + " 00:00:00", DatePattern.NORM_DATETIME_PATTERN));
|
||||
respDataResult.setEndTime(DateUtil.parse(responsibilityCalculateParam.getSearchEndTime() + " 23:59:59", DatePattern.NORM_DATETIME_PATTERN));
|
||||
respDataResult.setLimitValue(respHarmData.getOverLimit());
|
||||
//时间横轴数据 timeDatas
|
||||
JSONArray timeDataJson = JSONArray.parseArray(JSON.toJSONString(timeDatas));
|
||||
InputStream timeDataStream = IoUtil.toUtf8Stream(timeDataJson.toString());
|
||||
String timeDataPath = fileStorageUtil.uploadStream(timeDataStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setTimeData(timeDataPath);
|
||||
//用户每时刻对应的责任数据
|
||||
JSONArray customerDataJson = JSONArray.parseArray(JSON.toJSONString(customerData));
|
||||
InputStream customerStream = IoUtil.toUtf8Stream(customerDataJson.toString());
|
||||
String customerPath = fileStorageUtil.uploadStream(customerStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setUserDetailData(customerPath);
|
||||
//调用qvvr生成的中间数据
|
||||
CacheQvvrData cacheQvvrData = new CacheQvvrData(harmonicData.getPowerNodeCount(), harmonicData.getHarmonicCount(), harmonicData.getHarmonicData(), harmonicData.getFkData(), harmonicData.getHkData(), names, userIntervalTime, harmonicData.getWindowSize(), userIntervalTime, respHarmData.getHarmTime());
|
||||
String cacheJson = PubUtils.obj2json(cacheQvvrData);
|
||||
InputStream cacheQvvrDataStream = IoUtil.toUtf8Stream(cacheJson);
|
||||
String cacheQvvrDataPath = fileStorageUtil.uploadStream(cacheQvvrDataStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setQvvrData(cacheQvvrDataPath);
|
||||
//用户前10数据存储
|
||||
JSONArray customerResJson = JSONArray.parseArray(JSON.toJSONString(customerResponsibilities));
|
||||
InputStream customerResStream = IoUtil.toUtf8Stream(customerResJson.toString());
|
||||
String customerResPath = fileStorageUtil.uploadStream(customerResStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setUserResponsibility(customerResPath);
|
||||
respDataResultService.save(respDataResult);
|
||||
}
|
||||
//防止过程中创建了大量的对象,主动调用下GC处理
|
||||
System.gc();
|
||||
result.setResponsibilityDataIndex(responsibilityData.getId());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public ResponsibilityResult getResponsibilityDataOld(ResponsibilitySecondCalParam responsibilitySecondCalParam) {
|
||||
ResponsibilityResult result = new ResponsibilityResult();
|
||||
//根据时间天数,获取理论上多少次用采数据
|
||||
RespData responsibilityData = this.baseMapper.selectById(responsibilitySecondCalParam.getResDataId());
|
||||
@@ -664,6 +931,258 @@ public class RespDataServiceImpl extends ServiceImpl<RespDataMapper, RespData> i
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponsibilityResult getResponsibilityData(ResponsibilitySecondCalParam responsibilitySecondCalParam) {
|
||||
ResponsibilityResult result = new ResponsibilityResult();
|
||||
//根据时间天数,获取理论上多少次用采数据
|
||||
RespData responsibilityData = this.baseMapper.selectById(responsibilitySecondCalParam.getResDataId());
|
||||
if (Objects.isNull(responsibilityData)) {
|
||||
throw new BusinessException(AdvanceResponseEnum.RESP_DATA_NOT_FOUND);
|
||||
}
|
||||
Overlimit overlimit = lineFeignClient.getOverLimitData(responsibilityData.getLineId()).getData();
|
||||
//获取总数据
|
||||
LambdaQueryWrapper<RespDataResult> respDataResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
respDataResultLambdaQueryWrapper.eq(RespDataResult::getResDataId, responsibilityData.getId())
|
||||
.eq(RespDataResult::getTime, responsibilitySecondCalParam.getTime());
|
||||
if (responsibilitySecondCalParam.getType() == 0) {
|
||||
respDataResultLambdaQueryWrapper.eq(RespDataResult::getLimitValue, PubUtils.getValueByMethod(overlimit, "getIharm", responsibilitySecondCalParam.getTime()));
|
||||
} else {
|
||||
respDataResultLambdaQueryWrapper.eq(RespDataResult::getLimitValue, PubUtils.getValueByMethod(overlimit, "getUharm", responsibilitySecondCalParam.getTime()));
|
||||
}
|
||||
RespDataResult respDataResultTemp = respDataResultService.getOne(respDataResultLambdaQueryWrapper);
|
||||
if (Objects.isNull(respDataResultTemp)) {
|
||||
throw new BusinessException(AdvanceResponseEnum.RESP_DATA_NOT_FOUND);
|
||||
}
|
||||
CacheQvvrData cacheQvvrData;
|
||||
try {
|
||||
InputStream fileStream = fileStorageUtil.getFileStream(respDataResultTemp.getQvvrData());
|
||||
String qvvrDataStr = IoUtil.readUtf8(fileStream);
|
||||
cacheQvvrData = PubUtils.json2obj(qvvrDataStr, CacheQvvrData.class);
|
||||
|
||||
} catch (Exception exception) {
|
||||
throw new BusinessException(AdvanceResponseEnum.RESP_RESULT_DATA_NOT_FOUND);
|
||||
}
|
||||
//获取成功后,延长该缓存的生命周期为初始生命时长
|
||||
int win = cacheQvvrData.getWin();
|
||||
//不管窗口为4或者96,都需要考虑最小公倍数
|
||||
//最小公倍数根据监测点测量间隔来获取,可以考虑也由第一步操作缓存起来
|
||||
int minMultiple = cacheQvvrData.getMinMultiple();
|
||||
//谐波横轴所有的时间
|
||||
List<Long> times = cacheQvvrData.getTimes();
|
||||
//首先根据窗口判断限值时间范围是否满足最小窗口
|
||||
Long limitSL = DateUtil.parse(responsibilitySecondCalParam.getLimitStartTime(), DatePattern.NORM_DATETIME_PATTERN).getTime();
|
||||
Long limitEL = DateUtil.parse(responsibilitySecondCalParam.getLimitEndTime(), DatePattern.NORM_DATETIME_PATTERN).getTime();
|
||||
List<Integer> temp = getTimes(times, limitSL, limitEL);
|
||||
//在动态责任数据中,时间的起始索引位置和截止索引位置
|
||||
Integer timeStartIndex = temp.get(0);
|
||||
Integer timeEndIndex = temp.get(1);
|
||||
//间隔中的时间长度
|
||||
int minus = timeEndIndex - timeStartIndex + 1;
|
||||
//组装参数
|
||||
QvvrDataEntity qvvrDataEntity = new QvvrDataEntity();
|
||||
qvvrDataEntity.calFlag = 1;
|
||||
qvvrDataEntity.pNode = cacheQvvrData.getPNode();
|
||||
qvvrDataEntity.harmMk = responsibilitySecondCalParam.getLimitValue();
|
||||
qvvrDataEntity.win = win;
|
||||
int resNum;
|
||||
float[][] FKdata = new float[9600][QvvrDataEntity.MAX_P_NODE];
|
||||
float[][] HKdata = new float[9600][QvvrDataEntity.MAX_P_NODE + 1];
|
||||
float[] harmData = new float[1440 * 100];
|
||||
float[][] fKdataOriginal = cacheQvvrData.getFKData();
|
||||
float[][] hKdataOriginal = cacheQvvrData.getHKData();
|
||||
float[] harmDataOriginal = cacheQvvrData.getHarmData();
|
||||
//如果起始索引与截止索引的差值等于时间轴的长度,则说明用户没有选择限值时间,直接带入全部的原始数据,参与计算即可
|
||||
if (minus == times.size()) {
|
||||
qvvrDataEntity.harmNum = cacheQvvrData.getHarmNum();
|
||||
qvvrDataEntity.resNum = cacheQvvrData.getHarmNum() - cacheQvvrData.getWin();
|
||||
qvvrDataEntity.setFKData(cacheQvvrData.getFKData());
|
||||
qvvrDataEntity.setHKData(cacheQvvrData.getHKData());
|
||||
qvvrDataEntity.harmData = cacheQvvrData.getHarmData();
|
||||
} else {
|
||||
if (win == WINDOW_4) {
|
||||
//当窗口为4时,两个时间限制范围在最小公倍数为15时,最起码有5个有效时间点,在最小公倍数为30时,最起码有3个有效时间点
|
||||
if (minMultiple == INTERVAL_TIME_15) {
|
||||
if (minus < MINUS_5) {
|
||||
throw new BusinessException(AdvanceResponseEnum.WIN_TIME_ERROR);
|
||||
}
|
||||
resNum = minus - MINUS_4;
|
||||
|
||||
} else if (minMultiple == INTERVAL_TIME_30) {
|
||||
if (minus < MINUS_3) {
|
||||
throw new BusinessException(AdvanceResponseEnum.WIN_TIME_ERROR);
|
||||
}
|
||||
resNum = minus - MINUS_2;
|
||||
} else {
|
||||
throw new BusinessException(AdvanceResponseEnum.CALCULATE_INTERVAL_ERROR);
|
||||
}
|
||||
} else if (win == WINDOW_96) {
|
||||
//当窗口为96时,两个时间限值范围在最小公倍数为15时,最起码有97个有效时间点,在最小公倍数为30时,最起码有49个有效时间点
|
||||
if (minMultiple == INTERVAL_TIME_15) {
|
||||
if (minus <= WINDOW_96) {
|
||||
throw new BusinessException(AdvanceResponseEnum.WIN_TIME_ERROR);
|
||||
}
|
||||
resNum = minus - WINDOW_96;
|
||||
} else if (minMultiple == INTERVAL_TIME_30) {
|
||||
if (minus <= WINDOW_48) {
|
||||
throw new BusinessException(AdvanceResponseEnum.WIN_TIME_ERROR);
|
||||
}
|
||||
resNum = minus - WINDOW_48;
|
||||
} else {
|
||||
throw new BusinessException(AdvanceResponseEnum.CALCULATE_INTERVAL_ERROR);
|
||||
}
|
||||
} else {
|
||||
throw new BusinessException(AdvanceResponseEnum.CALCULATE_INTERVAL_ERROR);
|
||||
}
|
||||
qvvrDataEntity.resNum = resNum;
|
||||
qvvrDataEntity.harmNum = minus;
|
||||
//因为限值时间实际是含头含尾的,所以harmNum需要索引差值+1
|
||||
for (int i = timeStartIndex; i <= timeEndIndex; i++) {
|
||||
harmData[i - timeStartIndex] = harmDataOriginal[i];
|
||||
}
|
||||
qvvrDataEntity.harmData = harmData;
|
||||
//FKData与HKData的值则等于resNum
|
||||
for (int i = timeStartIndex; i < timeStartIndex + resNum; i++) {
|
||||
FKdata[i - timeStartIndex] = fKdataOriginal[i];
|
||||
HKdata[i - timeStartIndex] = hKdataOriginal[i];
|
||||
}
|
||||
|
||||
qvvrDataEntity.setFKData(FKdata);
|
||||
qvvrDataEntity.setHKData(HKdata);
|
||||
}
|
||||
// ResponsibilityAlgorithm responsibilityAlgorithm = new ResponsibilityAlgorithm();
|
||||
// qvvrDataEntity = responsibilityAlgorithm.getResponsibilityResult(qvvrDataEntity);
|
||||
//
|
||||
// if (qvvrDataEntity.calOk == 0) {
|
||||
// throw new BusinessException(AdvanceResponseEnum.RESPONSIBILITY_PARAMETER_ERROR);
|
||||
// }
|
||||
HarmonicData harmonicData = harmonicResponsibilityService.partialCalculation(
|
||||
qvvrDataEntity.getHarmData(), qvvrDataEntity.getFKData(), qvvrDataEntity.getHKData(), qvvrDataEntity.getHarmNum(), qvvrDataEntity.getPNode(), qvvrDataEntity.getWin(), qvvrDataEntity.getResNum(), qvvrDataEntity.getHarmMk());
|
||||
if (harmonicData.getCalculationStatus() == CalculationStatus.FAILED) {
|
||||
throw new BusinessException(AdvanceResponseEnum.RESPONSIBILITY_PARAMETER_ERROR);
|
||||
}
|
||||
|
||||
//没问题后,先玩动态责任数据
|
||||
List<String> names = cacheQvvrData.getNames();
|
||||
CustomerData[] customerDatas = new CustomerData[harmonicData.getPowerNodeCount()];
|
||||
float[][] fKdata/*无背景的动态责任数据*/ = harmonicData.getFkData();
|
||||
//第一个时间节点是起始时间+win窗口得到的时间
|
||||
Date sTime = new Date();
|
||||
sTime.setTime(times.get(timeStartIndex));
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(sTime);
|
||||
calendar.add(Calendar.MINUTE, (win - 1) * minMultiple);
|
||||
List<Long> timeDatas = new ArrayList<>();
|
||||
for (int i = 0; i < harmonicData.getHarmonicCount() - harmonicData.getWindowSize(); i++) {
|
||||
calendar.add(Calendar.MINUTE, minMultiple);
|
||||
//一个时间点所有的用户数据
|
||||
float[] fKdatum = fKdata[i];
|
||||
for (int k = 0; k < harmonicData.getPowerNodeCount(); k++) {
|
||||
CustomerData customerData = customerDatas[k];
|
||||
if (null == customerData) {
|
||||
customerData = new CustomerData();
|
||||
customerData.setCustomerName(names.get(k));
|
||||
}
|
||||
List<Float> valueDatas = customerData.getValueDatas();
|
||||
Float valueTemp = fKdatum[k];
|
||||
if (valueTemp.isNaN()) {
|
||||
valueTemp = 0.0f;
|
||||
}
|
||||
valueDatas.add(valueTemp);
|
||||
customerData.setValueDatas(valueDatas);
|
||||
customerDatas[k] = customerData;
|
||||
}
|
||||
timeDatas.add(calendar.getTimeInMillis());
|
||||
}
|
||||
//OK拿到所有测量点的数据了,现在就是看如何将相同户号的动态数据进行算术和求值,之前的用户name为:户号@测量点号@用户名
|
||||
Map<String/*用户名(户号)*/, List<CustomerData>> customerDataTemp = new HashMap<>(32);
|
||||
for (CustomerData data : customerDatas) {
|
||||
String customerName = data.getCustomerName();
|
||||
String[] customerInfo = customerName.split("@");
|
||||
String name = customerInfo[2] + "(" + customerInfo[0] + ")";
|
||||
List<CustomerData> customerData = customerDataTemp.get(name);
|
||||
CustomerData customerTemp = data;
|
||||
customerTemp.setCustomerName(name);
|
||||
if (CollectionUtils.isEmpty(customerData)) {
|
||||
customerData = new ArrayList<>();
|
||||
}
|
||||
customerData.add(customerTemp);
|
||||
customerDataTemp.put(name, customerData);
|
||||
}
|
||||
//调用程序接口后,首先组装责任量化结果
|
||||
float[] sumFKdata = harmonicData.getSumFKData();
|
||||
List<CustomerResponsibility> customerResponsibilities = getCustomerResponsibilityData(names, sumFKdata, harmonicData.getPowerNodeCount());
|
||||
//根据前十的用户数据,获取这些用户的动态责任数据
|
||||
List<CustomerData> customerData = new ArrayList<>();
|
||||
|
||||
for (CustomerResponsibility customerResponsibility : customerResponsibilities) {
|
||||
String cusName = customerResponsibility.getCustomerName();
|
||||
List<CustomerData> customerData1 = customerDataTemp.get(cusName);
|
||||
if (CollectionUtils.isEmpty(customerData1)) {
|
||||
continue;
|
||||
}
|
||||
if (customerData1.size() == 1) {
|
||||
//表示用户唯一的
|
||||
customerData.add(customerData1.get(0));
|
||||
} else {
|
||||
// 表示用户可能包含多个监测点号,需要进行数据累加
|
||||
CustomerData customerDataT = new CustomerData();
|
||||
customerDataT.setCustomerName(cusName);
|
||||
//进行数值累加
|
||||
List<Float> valueDatas = new ArrayList<>();
|
||||
for (int i = 0; i < customerData1.get(0).getValueDatas().size(); i++) {
|
||||
float original = 0.0f;
|
||||
for (CustomerData data : customerData1) {
|
||||
original = original + data.getValueDatas().get(i);
|
||||
}
|
||||
valueDatas.add(original);
|
||||
}
|
||||
customerDataT.setValueDatas(valueDatas);
|
||||
customerData.add(customerDataT);
|
||||
}
|
||||
}
|
||||
//接着组装动态数据结果
|
||||
result.setResponsibilities(customerResponsibilities);
|
||||
result.setDatas(customerData);
|
||||
result.setTimeDatas(timeDatas);
|
||||
|
||||
//首先判断有没有存储记录,没有则存储,有就略过 指定测点、时间窗口、谐波类型、谐波次数判断唯一性
|
||||
LambdaQueryWrapper<RespDataResult> respDataResultLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
|
||||
respDataResultLambdaQueryWrapper1.eq(RespDataResult::getResDataId, responsibilityData.getId())
|
||||
.eq(RespDataResult::getTime, responsibilitySecondCalParam.getTime())
|
||||
.eq(RespDataResult::getStartTime, DateUtil.parse(responsibilitySecondCalParam.getLimitStartTime(), DatePattern.NORM_DATETIME_PATTERN))
|
||||
.eq(RespDataResult::getEndTime, DateUtil.parse(responsibilitySecondCalParam.getLimitEndTime(), DatePattern.NORM_DATETIME_PATTERN))
|
||||
.eq(RespDataResult::getLimitValue, responsibilitySecondCalParam.getLimitValue());
|
||||
RespDataResult respDataResult = respDataResultService.getOne(respDataResultLambdaQueryWrapper1);
|
||||
if (Objects.isNull(respDataResult)) {
|
||||
respDataResult = new RespDataResult();
|
||||
respDataResult.setResDataId(responsibilityData.getId());
|
||||
respDataResult.setTime(responsibilitySecondCalParam.getTime());
|
||||
respDataResult.setStartTime(DateUtil.parse(responsibilitySecondCalParam.getLimitStartTime(), DatePattern.NORM_DATETIME_PATTERN));
|
||||
respDataResult.setEndTime(DateUtil.parse(responsibilitySecondCalParam.getLimitEndTime(), DatePattern.NORM_DATETIME_PATTERN));
|
||||
respDataResult.setLimitValue(responsibilitySecondCalParam.getLimitValue());
|
||||
//时间横轴数据 timeDatas
|
||||
JSONArray timeDataJson = JSONArray.parseArray(JSON.toJSONString(timeDatas));
|
||||
InputStream timeDataStream = IoUtil.toUtf8Stream(timeDataJson.toString());
|
||||
String timeDataPath = fileStorageUtil.uploadStream(timeDataStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setTimeData(timeDataPath);
|
||||
//用户每时刻对应的责任数据
|
||||
JSONArray customerDataJson = JSONArray.parseArray(JSON.toJSONString(customerData));
|
||||
InputStream customerStream = IoUtil.toUtf8Stream(customerDataJson.toString());
|
||||
String customerPath = fileStorageUtil.uploadStream(customerStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setUserDetailData(customerPath);
|
||||
//用户前10数据存储
|
||||
JSONArray customerResJson = JSONArray.parseArray(JSON.toJSONString(customerResponsibilities));
|
||||
InputStream customerResStream = IoUtil.toUtf8Stream(customerResJson.toString());
|
||||
String customerResPath = fileStorageUtil.uploadStream(customerResStream, OssPath.RESPONSIBILITY_USER_RESULT_DATA, FileUtil.generateFileName("json"));
|
||||
respDataResult.setUserResponsibility(customerResPath);
|
||||
respDataResultService.save(respDataResult);
|
||||
}
|
||||
//防止过程中创建了大量的对象,主动调用下GC处理
|
||||
System.gc();
|
||||
result.setResponsibilityDataIndex(responsibilityData.getId());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 监测点测量间隔获取最后用于计算的功率数据
|
||||
|
||||
Reference in New Issue
Block a user