|
|
#include "nmCalculationAutoFitPSO.h"
|
|
|
#include "nmCalculationDllPebiSolverTask.h"
|
|
|
#include "nmDataAnalyzeManager.h"
|
|
|
#include "nmDataWellBase.h"
|
|
|
#include "nmDataVerticalWell.h"
|
|
|
#include "nmDataVerticalFracturedWell.h"
|
|
|
#include "nmDataHorizontalFracturedWell.h"
|
|
|
#include "nmDataReservoir.h"
|
|
|
#include "nmDataAutomaticFitting.h"
|
|
|
|
|
|
#include "nmCalculationPebiGrid.h"
|
|
|
|
|
|
#include <QApplication>
|
|
|
#include <QDebug>
|
|
|
#include <QTime>
|
|
|
#include <QDir>
|
|
|
#include <QTextStream>
|
|
|
#include <QFileInfo>
|
|
|
#include <QProcess>
|
|
|
#include <QProcessEnvironment>
|
|
|
#include <QPair>
|
|
|
#include <QtCore/qmath.h>
|
|
|
#include <cmath>
|
|
|
#include <algorithm>
|
|
|
#include <limits>
|
|
|
#include <QTimer>
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
#include <windows.h>
|
|
|
#include <float.h>
|
|
|
#define DEBUG_OUT(msg) OutputDebugStringA(QString("[AutoFit] %1\n").arg(msg).toLocal8Bit().data())
|
|
|
#endif
|
|
|
|
|
|
// ===== PSO基础常量 =====
|
|
|
// MIN_FITNESS_IMPROVEMENT:判断适应度是否有有效改善的最小阈值。
|
|
|
// VELOCITY_LIMIT_FACTOR:单次速度最大值占参数搜索区间的比例,防止粒子一步跨太远。
|
|
|
// CONVERGENCE_CHECK_INTERVAL:收敛检查的基础间隔,部分历史逻辑和日志会使用它。
|
|
|
const double nmCalculationAutoFitPSO::MIN_FITNESS_IMPROVEMENT = 1e-8;
|
|
|
const double nmCalculationAutoFitPSO::VELOCITY_LIMIT_FACTOR = 0.2;
|
|
|
const int nmCalculationAutoFitPSO::CONVERGENCE_CHECK_INTERVAL = 5;
|
|
|
|
|
|
// ===== 代理模型筛选参数 =====
|
|
|
//
|
|
|
// PSO acceleration 的原则是“少跑一部分真实求解器,但不能完全信任代理模型”:
|
|
|
// - keep_fraction:代理评分排名靠前的粒子进入真实求解器;
|
|
|
// - audit_fraction:从被筛掉的粒子中随机抽查一部分,避免代理模型系统性漏掉好解;
|
|
|
// - min_solver_fraction:每代至少有一定比例粒子跑真实求解器,保证 pbest/gbest 仍由真实误差支撑;
|
|
|
// - warmup_iterations:前几代全量真实评价,让每个粒子先拿到可靠的个体最优;
|
|
|
// - full_solver_interval:周期性全量审计,用于纠偏代理模型排序误差。
|
|
|
static const double kSurrogateKeepFraction = 0.40; // 常规工况:代理排序前 40% 粒子进入真实求解器。
|
|
|
static const double kSurrogateHighRiskKeepFraction = 0.70; // 高风险流量制度:保留更多 top 粒子。
|
|
|
static const double kSurrogateAuditFraction = 0.05; // 从被筛掉候选中随机审计 5%。
|
|
|
static const double kSurrogateMinSolverFraction = 0.45; // 常规工况:每代至少 45% 粒子跑真实求解器。
|
|
|
static const double kSurrogateHighRiskMinSolverFraction = 0.80; // 高风险流量制度:每代至少 80% 粒子跑真实求解器。
|
|
|
static const int kSurrogateWarmupIterations = 2; // 前 2 代全量真实评价,给每个粒子建立 pbest。
|
|
|
static const int kSurrogateFullSolverInterval = 10; // 每 10 代做一次全量真实求解审计。
|
|
|
static const double kSurrogateGuidePbestMinRelativeImprovement = 1.0e-4; // 引导 pbest 至少改善 0.01%,过滤数值噪声。
|
|
|
static const int kSurrogateScoringMaxAttempts = 2; // Python 评分失败时最多重试 2 次。
|
|
|
static const bool kUseFixedPsoSeed = true; // 固定随机种子便于复现 trace 和交接演示。
|
|
|
static const unsigned int kFixedPsoSeed = 1792008679u; // 当前固定种子值。
|
|
|
|
|
|
// ===== 代理模型训练域约束 =====
|
|
|
//
|
|
|
// 这些范围必须与 ML/nmWTAI-ML/configs/data_gen_family_random_v2_q.yaml 中的
|
|
|
// 数据生成范围保持一致。超出范围时,C++ 侧会强制退回真实求解器,避免代理模型
|
|
|
// 在训练域外给出不可靠排序。
|
|
|
static const double kSurrogateKMin = 1.0e-3; // 代理模型训练域:渗透率下界。
|
|
|
static const double kSurrogateKMax = 10.0; // 代理模型训练域:渗透率上界。
|
|
|
static const double kSurrogateSkinMin = -10.0; // 代理模型训练域:skin 下界。
|
|
|
static const double kSurrogateSkinMax = 10.0; // 代理模型训练域:skin 上界。
|
|
|
static const double kSurrogateWellboreCMin = 1.0e-4; // 代理模型训练域:井筒储集下界。
|
|
|
static const double kSurrogateWellboreCMax = 2.0; // 代理模型训练域:井筒储集上界。
|
|
|
static const double kSurrogatePhiMin = 1.0e-2; // 代理模型训练域:孔隙度下界。
|
|
|
static const double kSurrogatePhiMax = 0.50; // 代理模型训练域:孔隙度上界。
|
|
|
static const double kSurrogateHMin = 2.0; // 代理模型训练域:储层厚度下界。
|
|
|
static const double kSurrogateHMax = 50.0; // 代理模型训练域:储层厚度上界。
|
|
|
static const double kSurrogateConstPvtCfFixed = 4.3e-4; // T1/T3 模型输入使用训练数据中的固定 Cf。
|
|
|
static const double kSurrogateT5CfFixed = 3.0e-4; // T5 专用训练数据中的固定 Cf。
|
|
|
static const double kSurrogateConstPvtCtFixed = 1.0e-2; // T1/T3 训练数据中的固定 Ct。
|
|
|
static const double kSurrogateFixedParamRelTol = 1.0e-4; // 固定参数仅允许 0.01% 相对偏差。
|
|
|
static const double kSurrogateCfMin = 1.0e-5; // T2/T4 训练数据中的 Cf 下界。
|
|
|
static const double kSurrogateCfMax = 1.0e-2; // T2/T4 训练数据中的 Cf 上界。
|
|
|
static const double kSurrogateTrainingInitialPressure = 34.47;
|
|
|
static const int kSurrogateMinProdSections = 3; // 代理训练流量制度:生产段数量下界。
|
|
|
static const int kSurrogateMaxProdSections = 5; // 代理训练流量制度:生产段数量上界。
|
|
|
static const double kSurrogateProdTimeMin = 24.0; // 代理训练流量制度:生产总时长下界。
|
|
|
static const double kSurrogateProdTimeMax = 220.0; // 代理训练流量制度:生产总时长上界。
|
|
|
static const double kSurrogateShutinTimeMin = 12.0; // 代理训练流量制度:关井段时长下界。
|
|
|
static const double kSurrogateShutinTimeMax = 140.0; // 代理训练流量制度:关井段时长上界。
|
|
|
static const double kSurrogateQMin = 50.0; // 代理训练流量制度:产量下界。
|
|
|
static const double kSurrogateQMax = 500.0; // 代理训练流量制度:产量上界。
|
|
|
static const double kSurrogateQZeroThreshold = 1.0e-6; // 判断最后一段是否关井的零产量阈值。
|
|
|
static const double kSurrogateMaxRateStepRatio = 2.0; // 相邻生产段产量最大跳变比。
|
|
|
static const double kSurrogateStrongDeclineEndStartRatio = 0.70; // 末段/首段产量小于该值视为强递减。
|
|
|
static const double kSurrogateRateTrendRelTol = 1.0e-9; // 判断产量趋势时的相对容差。
|
|
|
|
|
|
// 判断 double 是否为有限数。PSO、误差函数、trace 写出都依赖它过滤 NaN/Inf。
|
|
|
static inline bool isFiniteNumber(double value)
|
|
|
{
|
|
|
#ifdef Q_OS_WIN
|
|
|
return _finite(value) != 0;
|
|
|
#else
|
|
|
return std::isfinite(value);
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
// 两个 RMSE 的差不能直接解释为被消除的独立误差。RMSE 的平方才对应
|
|
|
// 均方能量,因此先计算 reduced^2-full^2,再开方恢复原量纲。这里用于分别
|
|
|
// 提取“消除公共上下偏差”和“消除水平位移”实际减少的误差贡献。
|
|
|
static double nestedRmsContribution(double reducedModelLoss,
|
|
|
double fullModelLoss)
|
|
|
{
|
|
|
return qSqrt(qMax(0.0,
|
|
|
reducedModelLoss * reducedModelLoss -
|
|
|
fullModelLoss * fullModelLoss));
|
|
|
}
|
|
|
|
|
|
static inline bool isInClosedRange(double value, double lower, double upper)
|
|
|
{
|
|
|
return isFiniteNumber(value) && value >= lower && value <= upper;
|
|
|
}
|
|
|
|
|
|
// 判断 value 是否接近 reference。用于 Ct/Cf 等固定训练参数的运行域检查。
|
|
|
static inline bool isNearRelative(double value, double reference, double relTol)
|
|
|
{
|
|
|
if(!isFiniteNumber(value) || !isFiniteNumber(reference)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return qAbs(value - reference) <= qMax(1e-12, qAbs(reference) * relTol);
|
|
|
}
|
|
|
|
|
|
static NM_SOLVER_MODEL_TYPE currentSolverModelType()
|
|
|
{
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
return dataManager ? dataManager->getSolverModelType() : SMT_Oil_ConstPvt;
|
|
|
}
|
|
|
|
|
|
struct SurrogateParameterDomain
|
|
|
{
|
|
|
double kMin;
|
|
|
double kMax;
|
|
|
double skinMin;
|
|
|
double skinMax;
|
|
|
double wellboreCMin;
|
|
|
double wellboreCMax;
|
|
|
double phiMin;
|
|
|
double phiMax;
|
|
|
double hMin;
|
|
|
double hMax;
|
|
|
double cfMin;
|
|
|
double cfMax;
|
|
|
double cfFixed;
|
|
|
double cfRelTol;
|
|
|
double ctFixed;
|
|
|
double ctRelTol;
|
|
|
bool checkCf;
|
|
|
bool checkCt;
|
|
|
bool variableCf;
|
|
|
};
|
|
|
|
|
|
// 每种相态只能在生成该相态训练数据时使用的参数范围内调用代理模型。
|
|
|
static bool getSurrogateParameterDomain(NM_SOLVER_MODEL_TYPE solverModelType, SurrogateParameterDomain* domain)
|
|
|
{
|
|
|
if(!domain) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
domain->kMin = kSurrogateKMin;
|
|
|
domain->kMax = kSurrogateKMax;
|
|
|
domain->skinMin = kSurrogateSkinMin;
|
|
|
domain->skinMax = kSurrogateSkinMax;
|
|
|
domain->wellboreCMin = kSurrogateWellboreCMin;
|
|
|
domain->wellboreCMax = kSurrogateWellboreCMax;
|
|
|
domain->phiMin = kSurrogatePhiMin;
|
|
|
domain->phiMax = kSurrogatePhiMax;
|
|
|
domain->hMin = kSurrogateHMin;
|
|
|
domain->hMax = kSurrogateHMax;
|
|
|
domain->cfMin = kSurrogateCfMin;
|
|
|
domain->cfMax = kSurrogateCfMax;
|
|
|
domain->cfFixed = kSurrogateT5CfFixed;
|
|
|
domain->cfRelTol = kSurrogateFixedParamRelTol;
|
|
|
domain->ctFixed = kSurrogateConstPvtCtFixed;
|
|
|
domain->ctRelTol = kSurrogateFixedParamRelTol;
|
|
|
domain->checkCf = false;
|
|
|
domain->checkCt = false;
|
|
|
domain->variableCf = false;
|
|
|
|
|
|
switch(solverModelType) {
|
|
|
case SMT_Oil_ConstPvt:
|
|
|
case SMT_Water_ConstPvt:
|
|
|
domain->checkCt = true;
|
|
|
return true;
|
|
|
|
|
|
case SMT_Oil_VariablePvt:
|
|
|
domain->checkCf = true;
|
|
|
domain->variableCf = true;
|
|
|
return true;
|
|
|
|
|
|
case SMT_Water_VariablePvt:
|
|
|
domain->checkCf = true;
|
|
|
domain->variableCf = true;
|
|
|
return true;
|
|
|
|
|
|
case SMT_Gas_VariablePvt:
|
|
|
domain->kMin = 4.5e-6;
|
|
|
domain->kMax = 6.2e-6;
|
|
|
domain->skinMin = 0.0;
|
|
|
domain->skinMax = 4.5;
|
|
|
domain->wellboreCMin = 5.0e-5;
|
|
|
domain->wellboreCMax = 1.6e-4;
|
|
|
domain->phiMin = 8.0e-3;
|
|
|
domain->phiMax = 4.0e-2;
|
|
|
domain->hMin = 7.0;
|
|
|
domain->hMax = 11.0;
|
|
|
domain->cfFixed = kSurrogateT5CfFixed;
|
|
|
domain->checkCf = true;
|
|
|
domain->variableCf = false;
|
|
|
return true;
|
|
|
|
|
|
default:
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Windows 下的毫秒级睡眠封装。用于重试间隔、UI 事件循环间隔和模拟模式节奏控制。
|
|
|
static inline void msleep(int ms)
|
|
|
{
|
|
|
#ifdef Q_OS_WIN
|
|
|
Sleep(ms);
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
static QString csvEscape(const QString& text)
|
|
|
{
|
|
|
// CSV 字段转义。trace 中的 run_id、phase、screeningDecision 等字符串都走这里,
|
|
|
// 防止逗号或引号破坏表格结构。
|
|
|
QString escaped = text;
|
|
|
escaped.replace("\"", "\"\"");
|
|
|
return QString("\"%1\"").arg(escaped);
|
|
|
}
|
|
|
|
|
|
static QString traceNumber(double value)
|
|
|
{
|
|
|
// trace CSV 中的数值格式化。不可用数值写空字段,便于后续 pandas/Excel 读取。
|
|
|
if(!isFiniteNumber(value)) {
|
|
|
return QString();
|
|
|
}
|
|
|
|
|
|
return QString::number(value, 'g', 17);
|
|
|
}
|
|
|
|
|
|
static QString traceParamAt(const QVector<double>& params, int index)
|
|
|
{
|
|
|
// 从完整参数向量中安全取值并格式化。越界时写空字段,避免 trace 写出崩溃。
|
|
|
if(index < 0 || index >= params.size()) {
|
|
|
return QString();
|
|
|
}
|
|
|
|
|
|
return traceNumber(params[index]);
|
|
|
}
|
|
|
|
|
|
static QString jsonEscape(const QString& text)
|
|
|
{
|
|
|
// 手写 JSON 字符串转义。trace meta 不依赖第三方 JSON writer,
|
|
|
// 所以所有字符串字段写入前必须统一经过这里。
|
|
|
QString escaped = text;
|
|
|
escaped.replace("\\", "\\\\");
|
|
|
escaped.replace("\"", "\\\"");
|
|
|
escaped.replace("\b", "\\b");
|
|
|
escaped.replace("\f", "\\f");
|
|
|
escaped.replace("\n", "\\n");
|
|
|
escaped.replace("\r", "\\r");
|
|
|
escaped.replace("\t", "\\t");
|
|
|
return QString("\"%1\"").arg(escaped);
|
|
|
}
|
|
|
|
|
|
static QString jsonNumber(double value)
|
|
|
{
|
|
|
// JSON 数值格式化。NaN/Inf 在 JSON 中没有合法表示,因此写 null。
|
|
|
if(!isFiniteNumber(value)) {
|
|
|
return "null";
|
|
|
}
|
|
|
|
|
|
return QString::number(value, 'g', 17);
|
|
|
}
|
|
|
|
|
|
static QString jsonDoubleArray(const QVector<double>& values)
|
|
|
{
|
|
|
// 将 double 数组写成 JSON 数组,用于 trace meta 的目标曲线、参数上下界等。
|
|
|
QStringList items;
|
|
|
|
|
|
for(int i = 0; i < values.size(); ++i) {
|
|
|
items << jsonNumber(values[i]);
|
|
|
}
|
|
|
|
|
|
return QString("[%1]").arg(items.join(","));
|
|
|
}
|
|
|
|
|
|
static QString jsonIntArray(const QVector<int>& values)
|
|
|
{
|
|
|
// 将 int 数组写成 JSON 数组,用于 enabled_param_indices 等字段。
|
|
|
QStringList items;
|
|
|
|
|
|
for(int i = 0; i < values.size(); ++i) {
|
|
|
items << QString::number(values[i]);
|
|
|
}
|
|
|
|
|
|
return QString("[%1]").arg(items.join(","));
|
|
|
}
|
|
|
|
|
|
static QString jsonBoolArray(const QVector<bool>& values)
|
|
|
{
|
|
|
// 将 bool 数组写成 JSON 数组,用于参数是否启用等字段。
|
|
|
QStringList items;
|
|
|
|
|
|
for(int i = 0; i < values.size(); ++i) {
|
|
|
items << (values[i] ? "true" : "false");
|
|
|
}
|
|
|
|
|
|
return QString("[%1]").arg(items.join(","));
|
|
|
}
|
|
|
|
|
|
static double deterministicAuditRandom01(unsigned int seed, int generation, int particleIndex)
|
|
|
{
|
|
|
// 代理筛选的随机审计需要“随机但可复现”:
|
|
|
// 同一个 PSO 种子、代号、粒子号会得到同一个 0-1 值,方便复盘 trace。
|
|
|
unsigned int x = seed;
|
|
|
x ^= 0x9e3779b9u + static_cast<unsigned int>(generation + 1) + (x << 6) + (x >> 2);
|
|
|
x ^= 0x85ebca6bu + static_cast<unsigned int>(particleIndex + 1) + (x << 6) + (x >> 2);
|
|
|
x ^= x >> 16;
|
|
|
x *= 0x7feb352du;
|
|
|
x ^= x >> 15;
|
|
|
x *= 0x846ca68bu;
|
|
|
x ^= x >> 16;
|
|
|
return static_cast<double>(x) / 4294967295.0;
|
|
|
}
|
|
|
|
|
|
static QString jsonStringArray(const QStringList& values)
|
|
|
{
|
|
|
// 将字符串列表写成 JSON 数组,用于参数名、启用参数名等字段。
|
|
|
QStringList items;
|
|
|
|
|
|
for(int i = 0; i < values.size(); ++i) {
|
|
|
items << jsonEscape(values[i]);
|
|
|
}
|
|
|
|
|
|
return QString("[%1]").arg(items.join(","));
|
|
|
}
|
|
|
|
|
|
static QString jsonPointArray(const QVector<QPointF>& points)
|
|
|
{
|
|
|
// 将 QPointF 数组写成 [{"x":...,"y":...}],用于 trace meta 保存流量制度。
|
|
|
QStringList items;
|
|
|
|
|
|
for(int i = 0; i < points.size(); ++i) {
|
|
|
items << QString("{\"x\":%1,\"y\":%2}")
|
|
|
.arg(jsonNumber(points[i].x()))
|
|
|
.arg(jsonNumber(points[i].y()));
|
|
|
}
|
|
|
|
|
|
return QString("[%1]").arg(items.join(","));
|
|
|
}
|
|
|
|
|
|
static bool isValidMlRootDirectory(const QString& mlRootPath)
|
|
|
{
|
|
|
// 判断一个目录是不是可用的 ML/nmWTAI-ML 根目录。
|
|
|
// 目前以评分脚本是否存在作为最小可用条件。
|
|
|
if(mlRootPath.isEmpty()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QFileInfo scriptInfo(QDir(mlRootPath).absoluteFilePath("scripts/score_pso_candidates.py"));
|
|
|
return scriptInfo.exists() && scriptInfo.isFile();
|
|
|
}
|
|
|
|
|
|
static QString findExecutableInPath(const QString& executableName)
|
|
|
{
|
|
|
// 在系统 PATH 中查找可执行文件。找不到 .venv 里的 python.exe 时会用它兜底。
|
|
|
if(executableName.isEmpty()) {
|
|
|
return QString();
|
|
|
}
|
|
|
|
|
|
QString pathValue = QProcessEnvironment::systemEnvironment().value("PATH");
|
|
|
|
|
|
if(pathValue.isEmpty()) {
|
|
|
pathValue = QString::fromLocal8Bit(qgetenv("PATH"));
|
|
|
}
|
|
|
|
|
|
if(pathValue.isEmpty()) {
|
|
|
return QString();
|
|
|
}
|
|
|
|
|
|
QStringList executableCandidates;
|
|
|
executableCandidates << executableName;
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
|
if(!executableName.contains('.')) {
|
|
|
executableCandidates << (executableName + ".exe")
|
|
|
<< (executableName + ".bat")
|
|
|
<< (executableName + ".cmd");
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
QChar pathSeparator(';');
|
|
|
#else
|
|
|
QChar pathSeparator(':');
|
|
|
#endif
|
|
|
QStringList pathEntries = pathValue.split(pathSeparator, QString::SkipEmptyParts);
|
|
|
|
|
|
for(int i = 0; i < pathEntries.size(); ++i) {
|
|
|
QDir dir(pathEntries[i].trimmed());
|
|
|
|
|
|
if(!dir.exists()) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
for(int j = 0; j < executableCandidates.size(); ++j) {
|
|
|
QFileInfo executableInfo(dir.absoluteFilePath(executableCandidates[j]));
|
|
|
|
|
|
if(executableInfo.exists() && executableInfo.isFile()) {
|
|
|
return QDir::cleanPath(executableInfo.absoluteFilePath());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return QString();
|
|
|
}
|
|
|
|
|
|
static QStringList traceParameterNames()
|
|
|
{
|
|
|
// trace 和 trace meta 使用的完整参数名顺序。
|
|
|
// 这个顺序必须与 buildTraceParameterVector() 和 m_parameterSelected 的 0-9 索引一致。
|
|
|
QStringList names;
|
|
|
names << "k"
|
|
|
<< "skin"
|
|
|
<< "wellboreC"
|
|
|
<< "phi"
|
|
|
<< "h"
|
|
|
<< "Ct"
|
|
|
<< "Cf"
|
|
|
<< "Swi"
|
|
|
<< "Dfc"
|
|
|
<< "fractureHalfLength";
|
|
|
return names;
|
|
|
}
|
|
|
|
|
|
// 构造函数:初始化 PSO 参数、收敛判断阈值、trace/代理统计和模拟模式状态。
|
|
|
// 这里设置的是默认值,真正运行前会由 loadOptimizationConfig() 和
|
|
|
// loadParameterBounds() 从 DataManager 读取界面配置覆盖一部分字段。
|
|
|
nmCalculationAutoFitPSO::nmCalculationAutoFitPSO(QObject* parent)
|
|
|
: QObject(parent)
|
|
|
, m_isRunning(false)
|
|
|
, m_shouldStop(false)
|
|
|
, m_isPaused(false)
|
|
|
, m_currentIteration(0)
|
|
|
, m_globalBestFitness(1e10)
|
|
|
, m_previousBestFitness(1e10)
|
|
|
, m_swarmSize(20)
|
|
|
, m_maxIterations(100)
|
|
|
, m_targetError(0.001)
|
|
|
, m_inertiaWeight(0.4)
|
|
|
, m_cognitiveParam(2.0)
|
|
|
, m_socialParam(1.2)
|
|
|
, m_totalEvaluations(0)
|
|
|
, m_successfulEvaluations(0)
|
|
|
, m_evaluationInProgress(0)
|
|
|
, m_consecutiveFailures(0)
|
|
|
, m_improvementThreshold(0.05)
|
|
|
, m_userInitialFitness(1e10)
|
|
|
, m_consecutiveFailedIterations(0)
|
|
|
, m_maxConsecutiveFailures(3)
|
|
|
, m_hasValidUserSolution(false)
|
|
|
, m_diversityThreshold(0.05)
|
|
|
, m_convergenceVarianceThreshold(1e-8)
|
|
|
, m_velocityConvergenceThreshold(0.01)
|
|
|
, m_trueConvergenceWindow(15)
|
|
|
, m_localOptimumWindow(8)
|
|
|
, m_farTargetFactor(10.0)
|
|
|
, m_targetWellName("")
|
|
|
, m_traceEnabled(true)
|
|
|
, m_traceRunId("")
|
|
|
, m_traceFilePath("")
|
|
|
, m_traceMetaFilePath("")
|
|
|
, m_surrogateScreeningEnabled(false)
|
|
|
, m_psoRandomSeed(0)
|
|
|
, m_surrogateRunContextSummary("")
|
|
|
, m_surrogateWarmupIterationCount(0)
|
|
|
, m_surrogatePeriodicAuditIterationCount(0)
|
|
|
, m_surrogateContextBlockedIterationCount(0)
|
|
|
, m_surrogateActiveIterationCount(0)
|
|
|
, m_surrogateSelectedParticleCount(0)
|
|
|
, m_surrogateScreenedParticleCount(0)
|
|
|
, m_surrogateTopKParticleCount(0)
|
|
|
, m_surrogateAuditParticleCount(0)
|
|
|
, m_surrogateFallbackParticleCount(0)
|
|
|
, m_surrogateDomainParticleCount(0)
|
|
|
, m_surrogateMinFloorParticleCount(0)
|
|
|
, m_surrogateScoringProcess(nullptr)
|
|
|
, m_surrogateScoringServerKey("")
|
|
|
, m_surrogateScoringServerReady(false)
|
|
|
|
|
|
, m_simulationMode(false)
|
|
|
, m_simulationTimer(nullptr)
|
|
|
, m_simulationIteration(0)
|
|
|
, m_simulationMaxIterations(100)
|
|
|
, m_simulationTargetError(0.001)
|
|
|
, m_simulationStartError(0.5)
|
|
|
, m_simulationCurrentError(0.5)
|
|
|
, m_simulationLogInterval(2)
|
|
|
, m_simulationCurrentParticle(0)
|
|
|
, m_simulationSuccessCount(0)
|
|
|
, m_simulationFailCount(0)
|
|
|
, m_simulationIterationStarted(false)
|
|
|
, m_simulationPreviousIterError(0.5)
|
|
|
{
|
|
|
DEBUG_OUT(QString("PSO Constructor: this=0x%1").arg((quintptr)this, 0, 16));
|
|
|
|
|
|
initializeTemporaryDirectory();
|
|
|
|
|
|
DEBUG_OUT("AutoFit calculator initialized (data-driven mode)");
|
|
|
DEBUG_OUT("PSO Constructor completed");
|
|
|
}
|
|
|
|
|
|
// 析构函数:停止仍在进行的拟合、关闭代理评分服务、关闭 trace 文件并清理临时目录。
|
|
|
// 自动拟合可能在 UI 线程中被窗口关闭打断,因此析构时要尽量温和地等待当前评价结束;
|
|
|
// 如果等待超时,再强制清除运行标志,避免对象销毁后还有信号回调或后台进程访问成员变量。
|
|
|
nmCalculationAutoFitPSO::~nmCalculationAutoFitPSO()
|
|
|
{
|
|
|
DEBUG_OUT(QString("PSO Destructor: this=0x%1").arg((quintptr)this, 0, 16));
|
|
|
|
|
|
if(m_simulationTimer) {
|
|
|
m_simulationTimer->stop();
|
|
|
delete m_simulationTimer;
|
|
|
m_simulationTimer = nullptr;
|
|
|
}
|
|
|
|
|
|
// Stop algorithm.
|
|
|
if(m_isRunning) {
|
|
|
m_shouldStop = true;
|
|
|
|
|
|
int waitCount = 0;
|
|
|
|
|
|
while(m_isRunning && waitCount < 100) {
|
|
|
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 50);
|
|
|
msleep(50);
|
|
|
waitCount++;
|
|
|
}
|
|
|
|
|
|
if(m_isRunning) {
|
|
|
DEBUG_OUT("Force stopping PSO - timeout reached");
|
|
|
m_isRunning = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Clean temp directory.
|
|
|
stopSurrogateScoringServer();
|
|
|
closeTraceFile();
|
|
|
cleanupTemporaryDirectory();
|
|
|
|
|
|
// Disconnect all signals to avoid callbacks after destruction.
|
|
|
disconnect(this, nullptr, nullptr, nullptr);
|
|
|
|
|
|
DEBUG_OUT("PSO Destructor completed");
|
|
|
}
|
|
|
|
|
|
// ===== 临时目录工具 =====
|
|
|
//
|
|
|
// 真实求解器 DLL 和自动拟合过程会产生中间文件,因此每次创建独立的
|
|
|
// autofit_temp_<pid>_<timestamp> 目录。退出时只删除本类创建的目录,启动时顺便清理
|
|
|
// 旧进程遗留的 autofit_temp_*,避免长期调试后应用目录被临时文件堆满。
|
|
|
|
|
|
void nmCalculationAutoFitPSO::initializeTemporaryDirectory()
|
|
|
{
|
|
|
// 先清理历史遗留目录,再为本次对象创建唯一目录。
|
|
|
// 目录名包含进程 ID 和毫秒时间戳,通常足够唯一;counter 是极端重名时的兜底。
|
|
|
cleanupOldTemporaryDirectories();
|
|
|
|
|
|
QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss_zzz");
|
|
|
QString processId = QString::number(QCoreApplication::applicationPid());
|
|
|
|
|
|
m_tempDirectory = QApplication::applicationDirPath() +
|
|
|
"/autofit_temp_" + processId + "_" + timestamp;
|
|
|
|
|
|
int counter = 0;
|
|
|
QString originalPath = m_tempDirectory;
|
|
|
|
|
|
while(QDir(m_tempDirectory).exists() && counter < 100) {
|
|
|
m_tempDirectory = originalPath + "_" + QString::number(counter);
|
|
|
counter++;
|
|
|
}
|
|
|
|
|
|
if(QDir().mkpath(m_tempDirectory)) {
|
|
|
DEBUG_OUT(QString("Initialized temp directory: %1").arg(m_tempDirectory));
|
|
|
} else {
|
|
|
DEBUG_OUT(QString("Warning: Failed to create temp directory: %1").arg(m_tempDirectory));
|
|
|
m_tempDirectory = QApplication::applicationDirPath();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::cleanupTemporaryDirectory()
|
|
|
{
|
|
|
// 析构或用户停止时调用。删除失败一般是文件仍被 DLL/系统占用,
|
|
|
// 这里只记录 debug 信息,不让清理失败影响 UI 退出。
|
|
|
if(QDir(m_tempDirectory).exists()) {
|
|
|
if(removeDirectoryRecursively(m_tempDirectory)) {
|
|
|
DEBUG_OUT("Temp directory cleaned up successfully");
|
|
|
} else {
|
|
|
DEBUG_OUT("Warning: Failed to clean up temp directory completely");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::removeDirectoryRecursively(const QString& path)
|
|
|
{
|
|
|
// Qt 旧版本没有统一可用的 removeRecursively 行为时,用本函数递归删除。
|
|
|
// 调用方传入的是本类创建的临时目录或旧 autofit_temp_* 目录。
|
|
|
QDir dir(path);
|
|
|
|
|
|
if(!dir.exists()) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 递归删除子目录和文件。包含 Hidden,避免隐藏中间文件阻塞目录删除。
|
|
|
QFileInfoList entries = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
|
|
|
bool allRemoved = true;
|
|
|
|
|
|
for(int i = 0; i < entries.size(); ++i) {
|
|
|
const QFileInfo& entry = entries[i];
|
|
|
|
|
|
if(entry.isDir()) {
|
|
|
if(!removeDirectoryRecursively(entry.absoluteFilePath())) {
|
|
|
allRemoved = false;
|
|
|
}
|
|
|
} else {
|
|
|
QFile file(entry.absoluteFilePath());
|
|
|
|
|
|
// 处理只读文件。某些求解器输出可能带只读属性,删除前先补写权限。
|
|
|
if(!file.permissions().testFlag(QFile::WriteUser)) {
|
|
|
file.setPermissions(file.permissions() | QFile::WriteUser);
|
|
|
}
|
|
|
|
|
|
if(!file.remove()) {
|
|
|
DEBUG_OUT(QString("Failed to remove file: %1").arg(entry.absoluteFilePath()));
|
|
|
allRemoved = false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 删除目录本身
|
|
|
if(allRemoved) {
|
|
|
return dir.rmdir(path);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::cleanupOldTemporaryDirectories()
|
|
|
{
|
|
|
// 应用启动或新建自动拟合对象时清理旧目录。
|
|
|
// 不删除当前进程 ID 对应目录,防止同进程内多个拟合对象或正在运行的求解器被误删。
|
|
|
QString appDir = QApplication::applicationDirPath();
|
|
|
QDir dir(appDir);
|
|
|
|
|
|
// 获取当前进程ID,避免删除当前进程可能使用的目录
|
|
|
QString currentProcessId = QString::number(QCoreApplication::applicationPid());
|
|
|
|
|
|
// 查找所有以 "autofit_temp_" 开头的目录
|
|
|
QStringList filters;
|
|
|
filters << "autofit_temp_*";
|
|
|
QFileInfoList tempDirs = dir.entryInfoList(filters, QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
|
if(tempDirs.isEmpty()) {
|
|
|
DEBUG_OUT("No old temporary directories found");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Found %1 potential old temporary directories to clean up").arg(tempDirs.size()));
|
|
|
|
|
|
int cleanedCount = 0;
|
|
|
int failedCount = 0;
|
|
|
int skippedCount = 0;
|
|
|
|
|
|
for(int i = 0; i < tempDirs.size(); ++i) {
|
|
|
const QFileInfo& dirInfo = tempDirs[i];
|
|
|
QString dirPath = dirInfo.absoluteFilePath();
|
|
|
QString dirName = dirInfo.fileName();
|
|
|
|
|
|
// 检查是否是当前进程的目录(虽然理论上不应该存在,但为了安全起见)
|
|
|
if(dirName.contains("_" + currentProcessId + "_")) {
|
|
|
DEBUG_OUT(QString("Skipping current process directory: %1").arg(dirName));
|
|
|
skippedCount++;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 尝试删除目录
|
|
|
DEBUG_OUT(QString("Attempting to remove old temp directory: %1").arg(dirName));
|
|
|
|
|
|
if(removeDirectoryRecursively(dirPath)) {
|
|
|
DEBUG_OUT(QString("Successfully cleaned up: %1").arg(dirName));
|
|
|
cleanedCount++;
|
|
|
} else {
|
|
|
DEBUG_OUT(QString("Failed to clean up: %1 (may be in use by another process)").arg(dirName));
|
|
|
failedCount++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 输出清理统计信息
|
|
|
DEBUG_OUT(QString("Old temp directories cleanup summary: %1 removed, %2 failed, %3 skipped")
|
|
|
.arg(cleanedCount).arg(failedCount).arg(skippedCount));
|
|
|
}
|
|
|
|
|
|
|
|
|
// ==================== 公共接口方法 ====================
|
|
|
|
|
|
void nmCalculationAutoFitPSO::setTargetLogLogData(const QVector<QVector<double> >& targetData)
|
|
|
{
|
|
|
// 目标曲线由界面层从目标井 history log-log 传入。
|
|
|
// 约定 targetData[0]=time,targetData[1]=pressure,targetData[2]=pressure derivative。
|
|
|
m_targetLogLogData = targetData;
|
|
|
DEBUG_OUT(QString("Target LogLog data set: %1 arrays").arg(targetData.size()));
|
|
|
|
|
|
if(targetData.size() >= 3) {
|
|
|
DEBUG_OUT(QString("LogLog data points: X=%1, Y1=%2, Y2=%3")
|
|
|
.arg(targetData[0].size())
|
|
|
.arg(targetData[1].size())
|
|
|
.arg(targetData[2].size()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void nmCalculationAutoFitPSO::stopFitting()
|
|
|
{
|
|
|
// 用户点击停止时走这里。停止策略是“请求式停止”:
|
|
|
// 只置 m_shouldStop,让主循环/求解器等待逻辑退出并自行维护任务计数。
|
|
|
// 不在这里清理临时目录或强制清零计数,避免与正在返回的 DLL 任务竞争。
|
|
|
if(m_simulationMode && m_simulationTimer) {
|
|
|
m_simulationTimer->stop();
|
|
|
}
|
|
|
|
|
|
if(m_isRunning) {
|
|
|
DEBUG_OUT("Stop request received, setting stop flag...");
|
|
|
|
|
|
// 添加停止日志
|
|
|
emit logMessageGenerated(tr("=== User Stop Request Received ==="));
|
|
|
emit logMessageGenerated(tr("Gracefully stopping PSO optimization..."));
|
|
|
|
|
|
m_shouldStop = true;
|
|
|
|
|
|
if(m_simulationMode) {
|
|
|
m_isRunning = false;
|
|
|
closeTraceFile();
|
|
|
cleanupTemporaryDirectory();
|
|
|
emit logMessageGenerated(tr("PSO simulation stop request processed"));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 给当前评价一个短暂的自然退出时间。若仍在运行,
|
|
|
// runSolverDll() 会在下一个等待周期检查 m_shouldStop 并结束任务。
|
|
|
int waitCount = 0;
|
|
|
|
|
|
while(m_evaluationInProgress > 0 && waitCount < 30) {
|
|
|
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 50);
|
|
|
msleep(50);
|
|
|
waitCount++;
|
|
|
}
|
|
|
|
|
|
if(m_evaluationInProgress > 0) {
|
|
|
emit logMessageGenerated(tr("Waiting for current solver evaluation to stop..."));
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("PSO optimization stop request processed"));
|
|
|
DEBUG_OUT("Stop request processed");
|
|
|
} else {
|
|
|
DEBUG_OUT("Stop request received but PSO is not running");
|
|
|
emit logMessageGenerated(tr("Stop request received but optimization is not running"));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::isRunning() const
|
|
|
{
|
|
|
// UI 查询当前是否处于自动拟合运行状态。
|
|
|
return m_isRunning;
|
|
|
}
|
|
|
|
|
|
int nmCalculationAutoFitPSO::getCurrentIteration() const
|
|
|
{
|
|
|
// UI 进度条和日志展示用的当前迭代序号。
|
|
|
return m_currentIteration;
|
|
|
}
|
|
|
|
|
|
QVector<double> nmCalculationAutoFitPSO::getBestSolution() const
|
|
|
{
|
|
|
// 返回紧凑的“启用参数向量”,顺序与 m_enabledParamIndices 一致。
|
|
|
return m_globalBestPosition;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::getBestFitness() const
|
|
|
{
|
|
|
// 当前全局最优真实误差。越小越好,1e10 附近通常表示尚无有效解。
|
|
|
return m_globalBestFitness;
|
|
|
}
|
|
|
|
|
|
AutoFitObjectiveBreakdown nmCalculationAutoFitPSO::getLastObjectiveBreakdown() const
|
|
|
{
|
|
|
// 返回最近一次损失评价的误差分解,供界面或后续优化逻辑读取。
|
|
|
return m_lastObjectiveBreakdown;
|
|
|
}
|
|
|
|
|
|
QString nmCalculationAutoFitPSO::getLastError() const
|
|
|
{
|
|
|
// 上一次失败的人类可读错误信息,主要给 UI 层弹窗或日志使用。
|
|
|
return m_lastError;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::resetOptimizer()
|
|
|
{
|
|
|
// 清空一次运行产生的状态,但不销毁对象。
|
|
|
// 配置字段会在 startAutoFitting() 中重新从 DataManager 读取;
|
|
|
// trace 文件先关闭,避免新一轮 run 继续写到旧 CSV。
|
|
|
closeTraceFile();
|
|
|
m_swarm.clear();
|
|
|
m_globalBestPosition.clear();
|
|
|
m_globalBestFitness = 1e10;
|
|
|
m_previousBestFitness = 1e10;
|
|
|
m_globalBestObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
m_lastEvaluatedLogLogData.clear();
|
|
|
m_globalBestLogLogData.clear();
|
|
|
m_lastObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
m_userInitialLogLogData.clear();
|
|
|
m_userInitialObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
m_currentIteration = 0;
|
|
|
m_totalEvaluations = 0;
|
|
|
m_successfulEvaluations = 0;
|
|
|
m_convergenceHistory.clear();
|
|
|
m_lastError.clear();
|
|
|
m_initialValues.clear();
|
|
|
m_userInitialSolution.clear();
|
|
|
m_userInitialFitness = 1e10;
|
|
|
m_hasValidUserSolution = false;
|
|
|
m_diversityHistory.clear();
|
|
|
m_velocityHistory.clear();
|
|
|
m_particleStagnationHistory.clear();
|
|
|
m_traceMetaFilePath.clear();
|
|
|
resetRunSummary();
|
|
|
|
|
|
DEBUG_OUT("Optimizer reset");
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::setPSOTargetWellName(const QString& wellName)
|
|
|
{
|
|
|
// 目标井名是贯穿拟合流程的关键索引:
|
|
|
// 读目标曲线、写 skin/wellboreC、求解后取 resultLogLog 都依赖这个名字。
|
|
|
m_targetWellName = wellName;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::initializeTraceFile()
|
|
|
{
|
|
|
// 创建本次自动拟合的可复盘文件。代理 PSO 与非代理信赖域使用不同前缀,
|
|
|
// 防止代理回放脚本把信赖域记录误当成最新 PSO 粒子记录。
|
|
|
//
|
|
|
// 代理模型评分脚本也会读取 meta.json,因此 trace meta 不是单纯日志,而是C++ 与 Python 代理模型之间的运行上下文契约。
|
|
|
if(!m_traceEnabled) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
closeTraceFile();
|
|
|
captureSurrogateRunContextSummary();
|
|
|
|
|
|
m_traceRunId = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss_zzz");
|
|
|
QDir traceDir(QDir(getMlRootPath()).absoluteFilePath("data/temp"));
|
|
|
|
|
|
if(!traceDir.exists() && !QDir().mkpath(traceDir.absolutePath())) {
|
|
|
DEBUG_OUT(QString("Failed to create PSO trace directory: %1").arg(traceDir.absolutePath()));
|
|
|
m_traceFilePath.clear();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
QString tracePrefix = isSurrogateScreeningEnabled()
|
|
|
? "pso_baseline_trace"
|
|
|
: "trust_region_trace";
|
|
|
m_traceFilePath = traceDir.absoluteFilePath(
|
|
|
QString("%1_%2.csv").arg(tracePrefix).arg(m_traceRunId));
|
|
|
m_traceMetaFilePath = traceDir.absoluteFilePath(
|
|
|
QString("%1_%2.meta.json").arg(tracePrefix).arg(m_traceRunId));
|
|
|
m_traceFile.setFileName(m_traceFilePath);
|
|
|
|
|
|
if(!m_traceFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
|
DEBUG_OUT(QString("Failed to open PSO trace file: %1").arg(m_traceFilePath));
|
|
|
m_traceFilePath.clear();
|
|
|
m_traceMetaFilePath.clear();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
writeTraceHeader();
|
|
|
writeTraceMetaFile();
|
|
|
DEBUG_OUT(QString("Automatic fitting trace initialized: %1").arg(m_traceFilePath));
|
|
|
emit logMessageGenerated(tr("Automatic fitting trace: %1").arg(m_traceFilePath));
|
|
|
|
|
|
if(!m_traceMetaFilePath.isEmpty()) {
|
|
|
emit logMessageGenerated(
|
|
|
tr("Automatic fitting trace meta: %1").arg(m_traceMetaFilePath));
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("PSO surrogate screening: %1, model=%2, keep=%3, audit=%4, warmup=%5, min_solver=%6")
|
|
|
.arg(isSurrogateScreeningEnabled() ? tr("enabled") : tr("disabled"))
|
|
|
.arg(getSurrogateTag())
|
|
|
.arg(getSurrogateKeepFraction(), 0, 'f', 2)
|
|
|
.arg(getSurrogateAuditFraction(), 0, 'f', 2)
|
|
|
.arg(getSurrogateWarmupIterations())
|
|
|
.arg(getSurrogateMinSolverFraction(), 0, 'f', 2));
|
|
|
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
emit logMessageGenerated(tr("Surrogate screening is turned off in the automatic fitting configuration"));
|
|
|
} else {
|
|
|
if(m_surrogateRunContextSummary == "passed") {
|
|
|
emit logMessageGenerated(tr("Surrogate run context check: passed"));
|
|
|
} else {
|
|
|
emit logMessageGenerated(tr("Surrogate run context check: %1").arg(m_surrogateRunContextSummary));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::resetRunSummary()
|
|
|
{
|
|
|
// 重置本次运行的代理筛选统计。这里只记录统计口径,不参与 PSO 更新。
|
|
|
m_surrogateRunContextSummary.clear();
|
|
|
m_surrogateWarmupIterationCount = 0;
|
|
|
m_surrogatePeriodicAuditIterationCount = 0;
|
|
|
m_surrogateContextBlockedIterationCount = 0;
|
|
|
m_surrogateActiveIterationCount = 0;
|
|
|
m_surrogateSelectedParticleCount = 0;
|
|
|
m_surrogateScreenedParticleCount = 0;
|
|
|
m_surrogateTopKParticleCount = 0;
|
|
|
m_surrogateAuditParticleCount = 0;
|
|
|
m_surrogateFallbackParticleCount = 0;
|
|
|
m_surrogateDomainParticleCount = 0;
|
|
|
m_surrogateMinFloorParticleCount = 0;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::captureSurrogateRunContextSummary()
|
|
|
{
|
|
|
// 在 trace 初始化阶段记录一次“本项目是否支持代理模型”的结论。
|
|
|
// 后续每代仍会再次检查,防止运行中 DataManager 状态发生变化。
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
m_surrogateRunContextSummary = "disabled_by_config";
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
QString contextReason;
|
|
|
|
|
|
if(isSurrogateRunContextSupported(&contextReason)) {
|
|
|
m_surrogateRunContextSummary = "passed";
|
|
|
} else {
|
|
|
m_surrogateRunContextSummary = QString("failed - %1").arg(contextReason);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::emitRunSummary(bool success, StopReasonPSO finalReason)
|
|
|
{
|
|
|
// 运行结束时输出一组交接/排障最有用的摘要:
|
|
|
// 真实求解次数、成功失败数、代理模型启用情况、各类筛选决策计数,以及 trace 文件路径。
|
|
|
emit logMessageGenerated(tr("=== PSO Run Summary ==="));
|
|
|
emit logMessageGenerated(tr("Stop reason: %1").arg(getStopReasonDescription(finalReason)));
|
|
|
emit logMessageGenerated(tr("Result: %1, final error=%2, iterations=%3, evaluations=%4 (successful=%5, failed=%6)")
|
|
|
.arg(success ? "SUCCESS" : "FAILED")
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4)
|
|
|
.arg(m_currentIteration + 1)
|
|
|
.arg(m_totalEvaluations)
|
|
|
.arg(m_successfulEvaluations)
|
|
|
.arg(m_totalEvaluations - m_successfulEvaluations));
|
|
|
emit logMessageGenerated(tr("Surrogate config: %1, context=%2")
|
|
|
.arg(isSurrogateScreeningEnabled() ? "enabled" : "disabled")
|
|
|
.arg(m_surrogateRunContextSummary.isEmpty() ? QString("unknown") : m_surrogateRunContextSummary));
|
|
|
emit logMessageGenerated(tr("Surrogate iterations: active=%1, warmup=%2, periodic_audit=%3, context_blocked=%4")
|
|
|
.arg(m_surrogateActiveIterationCount)
|
|
|
.arg(m_surrogateWarmupIterationCount)
|
|
|
.arg(m_surrogatePeriodicAuditIterationCount)
|
|
|
.arg(m_surrogateContextBlockedIterationCount));
|
|
|
emit logMessageGenerated(tr("Surrogate particles: selected=%1, screened_out=%2, topk=%3, random_audit=%4, fallback=%5, domain_gate=%6, min_solver_floor=%7")
|
|
|
.arg(m_surrogateSelectedParticleCount)
|
|
|
.arg(m_surrogateScreenedParticleCount)
|
|
|
.arg(m_surrogateTopKParticleCount)
|
|
|
.arg(m_surrogateAuditParticleCount)
|
|
|
.arg(m_surrogateFallbackParticleCount)
|
|
|
.arg(m_surrogateDomainParticleCount)
|
|
|
.arg(m_surrogateMinFloorParticleCount));
|
|
|
|
|
|
if(!m_traceFilePath.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Artifacts: trace=%1").arg(m_traceFilePath));
|
|
|
}
|
|
|
|
|
|
if(!m_traceMetaFilePath.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Artifacts: trace_meta=%1").arg(m_traceMetaFilePath));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::closeTraceFile()
|
|
|
{
|
|
|
// trace 与代理评分 server 生命周期绑定在一次 PSO run 内。
|
|
|
// 关闭 trace 时同时停止 server,避免 Python 进程继续占用模型或 stdout 管道。
|
|
|
stopSurrogateScoringServer();
|
|
|
|
|
|
if(m_traceFile.isOpen()) {
|
|
|
m_traceFile.flush();
|
|
|
m_traceFile.close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::writeTraceHeader()
|
|
|
{
|
|
|
// trace CSV 字段说明:
|
|
|
// - 代理模式保持原 k/skin/wellboreC/phi/h/Ct/Cf 契约;
|
|
|
// - 非代理模式额外记录 Swi/Dfc/裂缝半长,便于复盘信赖域调整;
|
|
|
// - solver_objective 是真实求解器误差;
|
|
|
// - surrogate_objective 是 Python 代理评分;
|
|
|
// - screening_decision 说明该粒子为什么跑/不跑真实求解器;
|
|
|
// - pbest/gbest 字段用于离线复盘 PSO 更新是否只依赖真实误差;
|
|
|
// - 末尾诊断字段记录同一次真实评价的分量误差,便于核对引导方向和接受结果。
|
|
|
if(!m_traceFile.isOpen()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
QStringList cols;
|
|
|
cols << "run_id"
|
|
|
<< "generation"
|
|
|
<< "particle_id"
|
|
|
<< "phase"
|
|
|
<< "k"
|
|
|
<< "skin"
|
|
|
<< "wellboreC"
|
|
|
<< "phi"
|
|
|
<< "h"
|
|
|
<< "Ct"
|
|
|
<< "Cf";
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << "Swi"
|
|
|
<< "Dfc"
|
|
|
<< "fractureHalfLength";
|
|
|
}
|
|
|
cols << "solver_objective"
|
|
|
<< "solver_success"
|
|
|
<< "elapsed_ms"
|
|
|
<< "surrogate_objective"
|
|
|
<< "screening_decision"
|
|
|
<< "pbest_objective"
|
|
|
<< "pbest_k"
|
|
|
<< "pbest_skin"
|
|
|
<< "pbest_wellboreC"
|
|
|
<< "pbest_phi"
|
|
|
<< "pbest_h"
|
|
|
<< "pbest_Ct"
|
|
|
<< "pbest_Cf";
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << "pbest_Swi"
|
|
|
<< "pbest_Dfc"
|
|
|
<< "pbest_fractureHalfLength";
|
|
|
}
|
|
|
cols << "gbest_objective"
|
|
|
<< "gbest_k"
|
|
|
<< "gbest_skin"
|
|
|
<< "gbest_wellboreC"
|
|
|
<< "gbest_phi"
|
|
|
<< "gbest_h"
|
|
|
<< "gbest_Ct"
|
|
|
<< "gbest_Cf";
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << "gbest_Swi"
|
|
|
<< "gbest_Dfc"
|
|
|
<< "gbest_fractureHalfLength";
|
|
|
}
|
|
|
cols << "enabled_param_indices"
|
|
|
<< "pressure_loss"
|
|
|
<< "derivative_loss";
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << "vertical_common_bias";
|
|
|
}
|
|
|
cols << "vertical_loss";
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << "vertical_reliable"
|
|
|
<< "horizontal_physical_shift";
|
|
|
}
|
|
|
cols << "horizontal_loss";
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << "horizontal_reliable";
|
|
|
}
|
|
|
cols << "shape_loss"
|
|
|
<< "late_trend_loss"
|
|
|
<< "late_slope_bias"
|
|
|
<< "late_trend_reliable"
|
|
|
<< "registration_ambiguous";
|
|
|
|
|
|
QTextStream out(&m_traceFile);
|
|
|
out << cols.join(",") << "\n";
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::writeTraceMetaFile()
|
|
|
{
|
|
|
// 写 trace 的配套 JSON。它把“如何解释 CSV”的上下文固化下来:
|
|
|
// 目标井、目标 log-log 曲线、流量制度、参数名和上下界、初始参数、PSO 配置、
|
|
|
// 代理模型 tag/stage/筛选比例等。Python 评分脚本会读取这个文件来构造模型输入。
|
|
|
if(m_traceMetaFilePath.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
QFile metaFile(m_traceMetaFilePath);
|
|
|
|
|
|
if(!metaFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
|
DEBUG_OUT(QString("Failed to open PSO trace meta file: %1").arg(m_traceMetaFilePath));
|
|
|
m_traceMetaFilePath.clear();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
nmDataWellBase* pTargetWell = dataManager ? dataManager->findWellByName(m_targetWellName) : nullptr;
|
|
|
|
|
|
QStringList parameterNames = traceParameterNames();
|
|
|
QStringList enabledNames;
|
|
|
|
|
|
for(int i = 0; i < m_enabledParamIndices.size(); ++i) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
enabledNames << ((paramIndex >= 0 && paramIndex < parameterNames.size()) ? parameterNames[paramIndex] : QString::number(paramIndex));
|
|
|
}
|
|
|
|
|
|
QVector<double> initialFullParams = buildTraceParameterVector(m_initialValues);
|
|
|
QVector<double> targetTime = m_targetLogLogData.size() > 0 ? m_targetLogLogData[0] : QVector<double>();
|
|
|
QVector<double> targetPressure = m_targetLogLogData.size() > 1 ? m_targetLogLogData[1] : QVector<double>();
|
|
|
QVector<double> targetDerivative = m_targetLogLogData.size() > 2 ? m_targetLogLogData[2] : QVector<double>();
|
|
|
QVector<QPointF> flowPoints = pTargetWell ? pTargetWell->getFlowPoints() : QVector<QPointF>();
|
|
|
|
|
|
QTextStream out(&metaFile);
|
|
|
out << "{\n";
|
|
|
// 非代理 v5 增加带符号诊断列;代理 PSO 保留原 v3 字段和目标,避免改变
|
|
|
// 已有模型的训练和回放契约。
|
|
|
out << " \"schema_version\": "
|
|
|
<< (isSurrogateScreeningEnabled() ? 3 : 7) << ",\n";
|
|
|
out << " \"trace_type\": "
|
|
|
<< jsonEscape(isSurrogateScreeningEnabled()
|
|
|
? "pso_baseline_replay_meta"
|
|
|
: "diagnostic_trust_region_meta") << ",\n";
|
|
|
out << " \"run_id\": " << jsonEscape(m_traceRunId) << ",\n";
|
|
|
out << " \"created_at\": " << jsonEscape(QDateTime::currentDateTime().toString(Qt::ISODate)) << ",\n";
|
|
|
out << " \"trace_csv\": " << jsonEscape(QFileInfo(m_traceFilePath).fileName()) << ",\n";
|
|
|
out << " \"target\": {\n";
|
|
|
out << " \"well_name\": " << jsonEscape(m_targetWellName) << ",\n";
|
|
|
out << " \"well_type\": " << (pTargetWell ? QString::number(static_cast<int>(pTargetWell->getWellType())) : "null") << ",\n";
|
|
|
out << " \"section_policy\": \"current_target_well_indexF\",\n";
|
|
|
out << " \"section_index\": " << (pTargetWell ? QString::number(pTargetWell->getIndexF()) : "null") << ",\n";
|
|
|
out << " \"target_loglog\": {\n";
|
|
|
out << " \"time\": " << jsonDoubleArray(targetTime) << ",\n";
|
|
|
out << " \"pressure\": " << jsonDoubleArray(targetPressure) << ",\n";
|
|
|
out << " \"derivative\": " << jsonDoubleArray(targetDerivative) << "\n";
|
|
|
out << " },\n";
|
|
|
out << " \"flow_points\": " << jsonPointArray(flowPoints) << "\n";
|
|
|
out << " },\n";
|
|
|
out << " \"pso\": {\n";
|
|
|
out << " \"swarm_size\": " << m_swarmSize << ",\n";
|
|
|
out << " \"max_iterations\": " << m_maxIterations << ",\n";
|
|
|
out << " \"target_error\": " << jsonNumber(m_targetError) << ",\n";
|
|
|
out << " \"inertia_weight\": " << jsonNumber(m_inertiaWeight) << ",\n";
|
|
|
out << " \"cognitive_param\": " << jsonNumber(m_cognitiveParam) << ",\n";
|
|
|
out << " \"social_param\": " << jsonNumber(m_socialParam) << ",\n";
|
|
|
out << " \"random_seed\": " << m_psoRandomSeed << "\n";
|
|
|
out << " },\n";
|
|
|
out << " \"surrogate_screening\": {\n";
|
|
|
out << " \"enabled\": " << (isSurrogateScreeningEnabled() ? "true" : "false") << ",\n";
|
|
|
out << " \"model_tag\": " << jsonEscape(getSurrogateTag()) << ",\n";
|
|
|
out << " \"keep_fraction\": " << jsonNumber(getSurrogateKeepFraction()) << ",\n";
|
|
|
out << " \"audit_fraction\": " << jsonNumber(getSurrogateAuditFraction()) << ",\n";
|
|
|
out << " \"min_solver_fraction\": " << jsonNumber(getSurrogateMinSolverFraction()) << ",\n";
|
|
|
out << " \"warmup_iterations\": " << getSurrogateWarmupIterations() << ",\n";
|
|
|
out << " \"full_solver_interval\": " << getSurrogateFullSolverInterval() << ",\n";
|
|
|
out << " \"audit_random_source\": \"deterministic_hash\"\n";
|
|
|
out << " },\n";
|
|
|
out << " \"parameters\": {\n";
|
|
|
out << " \"names\": " << jsonStringArray(parameterNames) << ",\n";
|
|
|
out << " \"enabled_indices\": " << jsonIntArray(m_enabledParamIndices) << ",\n";
|
|
|
out << " \"enabled_names\": " << jsonStringArray(enabledNames) << ",\n";
|
|
|
out << " \"selected_flags\": " << jsonBoolArray(m_parameterSelected) << ",\n";
|
|
|
out << " \"lower\": " << jsonDoubleArray(m_parameterLower) << ",\n";
|
|
|
out << " \"upper\": " << jsonDoubleArray(m_parameterUpper) << ",\n";
|
|
|
out << " \"initial_selected\": " << jsonDoubleArray(m_initialValues) << ",\n";
|
|
|
out << " \"initial_full\": " << jsonDoubleArray(initialFullParams) << "\n";
|
|
|
out << " },\n";
|
|
|
out << " \"replay_notes\": [\n";
|
|
|
out << " \"CSV rows contain true solver objectives for evaluated particles.\",\n";
|
|
|
out << " \"This meta file contains the target curve and run context needed for surrogate replay.\",\n";
|
|
|
out << " \"pbest and gbest must be updated only from true solver objectives.\"\n";
|
|
|
out << " ]\n";
|
|
|
out << "}\n";
|
|
|
metaFile.flush();
|
|
|
metaFile.close();
|
|
|
}
|
|
|
|
|
|
QVector<double> nmCalculationAutoFitPSO::buildTraceParameterVector(const QVector<double>& selectedParameters) const
|
|
|
{
|
|
|
// 将粒子内部使用的“启用参数向量”还原成完整 10 维参数向量。
|
|
|
// 未启用的参数从当前 DataManager 读取,启用的参数用 selectedParameters 覆盖。
|
|
|
// trace CSV、候选 CSV、代理训练域检查都需要这个完整向量。
|
|
|
QVector<double> fullParams(10, 0.0);
|
|
|
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
|
if(dataManager) {
|
|
|
nmDataReservoir reservoirData = dataManager->getReservoirDataCopy();
|
|
|
fullParams[0] = reservoirData.getPermeability().getValue().toDouble();
|
|
|
fullParams[3] = reservoirData.getPorosity().getValue().toDouble();
|
|
|
fullParams[4] = reservoirData.getThickness().getValue().toDouble();
|
|
|
fullParams[5] = reservoirData.getCt().getValue().toDouble();
|
|
|
fullParams[6] = reservoirData.getCf().getValue().toDouble();
|
|
|
fullParams[7] = reservoirData.getSwi().getValue().toDouble();
|
|
|
|
|
|
nmDataWellBase* pTargetWell = dataManager->findWellByName(m_targetWellName);
|
|
|
|
|
|
if(pTargetWell) {
|
|
|
nmDataPerforation* perforation = pTargetWell->getPerforation(0);
|
|
|
if(perforation) {
|
|
|
fullParams[1] = perforation->getSkin().getValue().toDouble();
|
|
|
}
|
|
|
fullParams[2] = pTargetWell->getWellboreStorage().getValue().toDouble();
|
|
|
|
|
|
// Dfc 只存在于两类压裂井,普通井在完整向量中保持为 0。
|
|
|
if(pTargetWell->getWellType() == NM_WELL_MODEL::Vertical_Fractured_Well) {
|
|
|
nmDataVerticalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataVerticalFracturedWell*>(pTargetWell);
|
|
|
if(fracturedWell) {
|
|
|
fullParams[8] = fracturedWell->getDfc().getValue().toDouble();
|
|
|
fullParams[9] = fracturedWell->getFractureHalfLength().getValue().toDouble();
|
|
|
}
|
|
|
} else if(pTargetWell->getWellType() == NM_WELL_MODEL::Horizontal_Fractured_Well) {
|
|
|
nmDataHorizontalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataHorizontalFracturedWell*>(pTargetWell);
|
|
|
if(fracturedWell) {
|
|
|
fullParams[8] = fracturedWell->getDfc().getValue().toDouble();
|
|
|
fullParams[9] = fracturedWell->getFractureHalfLength().getValue().toDouble();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < selectedParameters.size() && i < m_enabledParamIndices.size(); ++i) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
|
|
|
if(paramIndex >= 0 && paramIndex < fullParams.size()) {
|
|
|
fullParams[paramIndex] = selectedParameters[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return fullParams;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::writeTraceRow(int generation,
|
|
|
int particleIndex,
|
|
|
const QString& phase,
|
|
|
const QVector<double>& parameters,
|
|
|
double solverObjective,
|
|
|
bool solverSuccess,
|
|
|
int elapsedMs,
|
|
|
double surrogateObjective,
|
|
|
const QString& screeningDecision,
|
|
|
const QVector<double>& pbestPosition,
|
|
|
double pbestObjective,
|
|
|
const AutoFitObjectiveBreakdown* objectiveBreakdown)
|
|
|
{
|
|
|
// 写一行 trace。generation=-1/particleIndex=-1 表示用户初始解;
|
|
|
// 普通粒子行的 phase 为 particle_solver、particle_verified_cache 或 particle_not_evaluated。
|
|
|
// 被代理筛掉的粒子 solver_objective 写空值,但仍保留 surrogate_objective 和 decision。
|
|
|
if(!m_traceFile.isOpen()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
QVector<double> currentParams = buildTraceParameterVector(parameters);
|
|
|
QVector<double> pbestParams = pbestPosition.isEmpty() ? QVector<double>() : buildTraceParameterVector(pbestPosition);
|
|
|
QVector<double> gbestParams = m_globalBestPosition.isEmpty() ? QVector<double>() : buildTraceParameterVector(m_globalBestPosition);
|
|
|
|
|
|
QStringList enabledIndices;
|
|
|
|
|
|
for(int i = 0; i < m_enabledParamIndices.size(); ++i) {
|
|
|
enabledIndices << QString::number(m_enabledParamIndices[i]);
|
|
|
}
|
|
|
|
|
|
QStringList cols;
|
|
|
cols << csvEscape(m_traceRunId)
|
|
|
<< QString::number(generation)
|
|
|
<< QString::number(particleIndex)
|
|
|
<< csvEscape(phase)
|
|
|
<< traceParamAt(currentParams, 0)
|
|
|
<< traceParamAt(currentParams, 1)
|
|
|
<< traceParamAt(currentParams, 2)
|
|
|
<< traceParamAt(currentParams, 3)
|
|
|
<< traceParamAt(currentParams, 4)
|
|
|
<< traceParamAt(currentParams, 5)
|
|
|
<< traceParamAt(currentParams, 6);
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << traceParamAt(currentParams, 7)
|
|
|
<< traceParamAt(currentParams, 8)
|
|
|
<< traceParamAt(currentParams, 9);
|
|
|
}
|
|
|
cols << traceNumber(solverObjective)
|
|
|
<< QString::number(solverSuccess ? 1 : 0)
|
|
|
<< QString::number(elapsedMs)
|
|
|
<< traceNumber(surrogateObjective)
|
|
|
<< csvEscape(screeningDecision)
|
|
|
<< traceNumber(pbestObjective)
|
|
|
<< traceParamAt(pbestParams, 0)
|
|
|
<< traceParamAt(pbestParams, 1)
|
|
|
<< traceParamAt(pbestParams, 2)
|
|
|
<< traceParamAt(pbestParams, 3)
|
|
|
<< traceParamAt(pbestParams, 4)
|
|
|
<< traceParamAt(pbestParams, 5)
|
|
|
<< traceParamAt(pbestParams, 6);
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << traceParamAt(pbestParams, 7)
|
|
|
<< traceParamAt(pbestParams, 8)
|
|
|
<< traceParamAt(pbestParams, 9);
|
|
|
}
|
|
|
cols << traceNumber(m_globalBestFitness)
|
|
|
<< traceParamAt(gbestParams, 0)
|
|
|
<< traceParamAt(gbestParams, 1)
|
|
|
<< traceParamAt(gbestParams, 2)
|
|
|
<< traceParamAt(gbestParams, 3)
|
|
|
<< traceParamAt(gbestParams, 4)
|
|
|
<< traceParamAt(gbestParams, 5)
|
|
|
<< traceParamAt(gbestParams, 6);
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << traceParamAt(gbestParams, 7)
|
|
|
<< traceParamAt(gbestParams, 8)
|
|
|
<< traceParamAt(gbestParams, 9);
|
|
|
}
|
|
|
cols << csvEscape(enabledIndices.join(";"));
|
|
|
|
|
|
if(objectiveBreakdown && objectiveBreakdown->valid) {
|
|
|
cols << traceNumber(objectiveBreakdown->pressureLoss)
|
|
|
<< traceNumber(objectiveBreakdown->derivativeLoss);
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << traceNumber(objectiveBreakdown->verticalCommonBias);
|
|
|
}
|
|
|
cols << traceNumber(objectiveBreakdown->verticalLoss);
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << QString::number(objectiveBreakdown->verticalReliable ? 1 : 0)
|
|
|
<< traceNumber(objectiveBreakdown->horizontalPhysicalShift);
|
|
|
}
|
|
|
cols << traceNumber(objectiveBreakdown->horizontalLoss);
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
cols << QString::number(objectiveBreakdown->horizontalReliable ? 1 : 0);
|
|
|
}
|
|
|
cols << traceNumber(objectiveBreakdown->shapeLoss)
|
|
|
<< traceNumber(objectiveBreakdown->lateDerivativeTrendLoss)
|
|
|
<< traceNumber(objectiveBreakdown->lateDerivativeSlopeBias)
|
|
|
<< QString::number(objectiveBreakdown->lateDerivativeTrendReliable ? 1 : 0)
|
|
|
<< QString::number(objectiveBreakdown->registrationAmbiguous ? 1 : 0);
|
|
|
} else {
|
|
|
// 未运行真实求解器或评价无效时保持列数一致,诊断字段写空值。
|
|
|
int diagnosticColumnCount =
|
|
|
isSurrogateScreeningEnabled() ? 9 : 13;
|
|
|
for(int i = 0; i < diagnosticColumnCount; ++i) {
|
|
|
cols << QString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QTextStream out(&m_traceFile);
|
|
|
out << cols.join(",") << "\n";
|
|
|
m_traceFile.flush();
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::writeIterationTraceRows()
|
|
|
{
|
|
|
// 每代真实评价和全局最优更新后写出所有粒子。
|
|
|
// 这样 trace 中能同时看到“这一代谁被筛掉”和“当前 pbest/gbest 是什么”。
|
|
|
if(!m_traceFile.isOpen()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
const AutoFitParticle& particle = m_swarm[i];
|
|
|
double solverObjective = particle.evaluatedThisIteration
|
|
|
? particle.fitness
|
|
|
: std::numeric_limits<double>::quiet_NaN();
|
|
|
QString phase = !particle.evaluatedThisIteration
|
|
|
? "particle_not_evaluated"
|
|
|
: (particle.screeningDecision == "initial_solution_cache"
|
|
|
? "particle_verified_cache"
|
|
|
: "particle_solver");
|
|
|
writeTraceRow(m_currentIteration,
|
|
|
i,
|
|
|
phase,
|
|
|
particle.position,
|
|
|
solverObjective,
|
|
|
particle.lastEvaluationSuccess,
|
|
|
particle.lastEvaluationElapsedMs,
|
|
|
particle.surrogateObjective,
|
|
|
particle.screeningDecision,
|
|
|
particle.bestPosition,
|
|
|
particle.bestFitness,
|
|
|
particle.evaluatedThisIteration
|
|
|
? &particle.currentObjectiveBreakdown : nullptr);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::isSurrogateScreeningEnabled() const
|
|
|
{
|
|
|
// 对应界面上的 PSO acceleration / surrogate screening 开关。
|
|
|
return m_surrogateScreeningEnabled;
|
|
|
}
|
|
|
|
|
|
QString nmCalculationAutoFitPSO::getMlRootPath() const
|
|
|
{
|
|
|
// 查找 ML/nmWTAI-ML 根目录。程序可能从 build/bin/release 等不同目录启动,
|
|
|
// 因此按多个相对路径尝试。isValidMlRootDirectory() 用评分脚本是否存在来确认。
|
|
|
QString appDir = QApplication::applicationDirPath();
|
|
|
QString cwd = QDir::currentPath();
|
|
|
|
|
|
QStringList candidateRoots;
|
|
|
candidateRoots << QDir::cleanPath(appDir + "/../../ML/nmWTAI-ML")
|
|
|
<< QDir::cleanPath(appDir + "/../ML/nmWTAI-ML")
|
|
|
<< QDir::cleanPath(cwd + "/ML/nmWTAI-ML")
|
|
|
<< QDir::cleanPath(cwd + "/../ML/nmWTAI-ML")
|
|
|
<< QDir::cleanPath(cwd + "/../../ML/nmWTAI-ML");
|
|
|
|
|
|
for(int i = 0; i < candidateRoots.size(); ++i) {
|
|
|
if(isValidMlRootDirectory(candidateRoots[i])) {
|
|
|
return candidateRoots[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return candidateRoots.isEmpty()
|
|
|
? QString()
|
|
|
: candidateRoots.first();
|
|
|
}
|
|
|
|
|
|
QString nmCalculationAutoFitPSO::getPythonExecutablePath() const
|
|
|
{
|
|
|
// 优先使用 ML 项目内的虚拟环境 python,其次使用 PATH 中的 python。
|
|
|
// 这样开发机上只要 ML/nmWTAI-ML/.venv 已配置,就不依赖全局 Python 环境。
|
|
|
QString mlRoot = getMlRootPath();
|
|
|
QStringList candidateExecutables;
|
|
|
|
|
|
if(!mlRoot.isEmpty()) {
|
|
|
candidateExecutables << QDir(mlRoot).absoluteFilePath(".venv/Scripts/python.exe");
|
|
|
candidateExecutables << QDir(mlRoot).absoluteFilePath(".venv/bin/python");
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < candidateExecutables.size(); ++i) {
|
|
|
QFileInfo pythonInfo(candidateExecutables[i]);
|
|
|
|
|
|
if(pythonInfo.exists() && pythonInfo.isFile()) {
|
|
|
return QDir::cleanPath(pythonInfo.absoluteFilePath());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QString resolvedPython = findExecutableInPath("python");
|
|
|
|
|
|
if(!resolvedPython.isEmpty()) {
|
|
|
return QDir::cleanPath(resolvedPython);
|
|
|
}
|
|
|
|
|
|
return "python";
|
|
|
}
|
|
|
|
|
|
QString nmCalculationAutoFitPSO::getSurrogateTag() const
|
|
|
{
|
|
|
// 当前 C++ 绑定的代理模型实验标识,对应 ML 侧训练输出目录/checkpoint 命名。
|
|
|
// 如果以后换模型,需要同步更新这里和 ML 脚本可识别的 tag。
|
|
|
if(currentSolverModelType() == SMT_Gas_VariablePvt) {
|
|
|
return "T5_family_random_18448_local_delta";
|
|
|
}
|
|
|
|
|
|
return "T1_T4_family_random_40k_merged_autofit";
|
|
|
}
|
|
|
|
|
|
QString nmCalculationAutoFitPSO::getSurrogateStage() const
|
|
|
{
|
|
|
// 当前代理模型的数据生成阶段。v2_q 表示训练时包含流量制度 q 的变化。
|
|
|
return "family_random_v2_q";
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::getSurrogateKeepFraction() const
|
|
|
{
|
|
|
// 强递减生产制度下代理排序风险更高,因此保留更多 top 粒子进入真实求解器。
|
|
|
if(getSurrogateStage().contains("v2_q") && isStrongDecliningProductionSchedule()) {
|
|
|
return kSurrogateHighRiskKeepFraction;
|
|
|
}
|
|
|
|
|
|
return kSurrogateKeepFraction;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::getSurrogateAuditFraction() const
|
|
|
{
|
|
|
// 随机审计比例,审计的是代理模型筛掉的候选。
|
|
|
return kSurrogateAuditFraction;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::getSurrogateMinSolverFraction() const
|
|
|
{
|
|
|
// 每代真实求解器最低覆盖比例。强递减生产制度下提高到高风险配置。
|
|
|
if(getSurrogateStage().contains("v2_q") && isStrongDecliningProductionSchedule()) {
|
|
|
return kSurrogateHighRiskMinSolverFraction;
|
|
|
}
|
|
|
|
|
|
return kSurrogateMinSolverFraction;
|
|
|
}
|
|
|
|
|
|
int nmCalculationAutoFitPSO::getSurrogateWarmupIterations() const
|
|
|
{
|
|
|
// 前若干代不筛选,确保粒子先建立真实 pbest。
|
|
|
return kSurrogateWarmupIterations;
|
|
|
}
|
|
|
|
|
|
int nmCalculationAutoFitPSO::getSurrogateFullSolverInterval() const
|
|
|
{
|
|
|
// 周期性全量真实评价间隔,用于审计代理模型排序。
|
|
|
return kSurrogateFullSolverInterval;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::writeSurrogateCandidateCsv(const QString& candidatePath, bool includeGuideCandidates) const
|
|
|
{
|
|
|
// 写给 Python 代理模型的候选粒子 CSV。
|
|
|
//
|
|
|
// 文件位置通常是 ML/nmWTAI-ML/data/temp/pso_screen_candidates_<run_id>_gen<N>.csv。
|
|
|
// 字段顺序必须与 ML 脚本 score_pso_candidates*.py 保持一致:
|
|
|
// particle_id,k,skin,wellboreC,phi,h,Cf,solverType
|
|
|
//
|
|
|
// 这里写的是“完整参数体系”中的关键字段,不是粒子内部紧凑向量。
|
|
|
// T1/T3 的 Cf 不影响真实求解,模型输入固定为训练值,避免界面 Cf 把模型带出训练分布。
|
|
|
QFile file(candidatePath);
|
|
|
|
|
|
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
|
DEBUG_OUT(QString("Failed to open surrogate candidate file: %1").arg(candidatePath));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QTextStream out(&file);
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
|
if(!dataManager) {
|
|
|
DEBUG_OUT("Failed to write surrogate candidates: data manager is unavailable");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
int solverType = static_cast<int>(dataManager->getSolverModelType());
|
|
|
bool useConstPvtTrainingCf = solverType == static_cast<int>(SMT_Oil_ConstPvt) ||
|
|
|
solverType == static_cast<int>(SMT_Water_ConstPvt);
|
|
|
// 代理模型只需要这些输入字段;其它参数当前不在训练输入集中。
|
|
|
out << "particle_id,k,skin,wellboreC,phi,h,Cf,solverType\n";
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
QVector<double> params = buildTraceParameterVector(m_swarm[i].position);
|
|
|
QString cfForModel = useConstPvtTrainingCf
|
|
|
? QString::number(kSurrogateConstPvtCfFixed, 'g', 17)
|
|
|
: traceParamAt(params, 6);
|
|
|
QStringList cols;
|
|
|
cols << QString::number(i)
|
|
|
<< traceParamAt(params, 0)
|
|
|
<< traceParamAt(params, 1)
|
|
|
<< traceParamAt(params, 2)
|
|
|
<< traceParamAt(params, 3)
|
|
|
<< traceParamAt(params, 4)
|
|
|
<< cfForModel
|
|
|
<< QString::number(solverType);
|
|
|
out << cols.join(",") << "\n";
|
|
|
}
|
|
|
|
|
|
if(includeGuideCandidates) {
|
|
|
// T1-T5 都需要判断当前位置是否优于自己的 guide pbest。当前位置和 guide 必须在
|
|
|
// 同一次代理推理中评分,避免把模型预测误差与真实求解器误差直接比较。
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
const QVector<double>& guidePosition =
|
|
|
m_swarm[i].guideBestPosition.size() == m_swarm[i].position.size()
|
|
|
? m_swarm[i].guideBestPosition
|
|
|
: m_swarm[i].bestPosition;
|
|
|
QVector<double> params = buildTraceParameterVector(guidePosition);
|
|
|
QString cfForModel = useConstPvtTrainingCf
|
|
|
? QString::number(kSurrogateConstPvtCfFixed, 'g', 17)
|
|
|
: traceParamAt(params, 6);
|
|
|
QStringList cols;
|
|
|
cols << QString::number(m_swarm.size() + i)
|
|
|
<< traceParamAt(params, 0)
|
|
|
<< traceParamAt(params, 1)
|
|
|
<< traceParamAt(params, 2)
|
|
|
<< traceParamAt(params, 3)
|
|
|
<< traceParamAt(params, 4)
|
|
|
<< cfForModel
|
|
|
<< QString::number(solverType);
|
|
|
out << cols.join(",") << "\n";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
file.flush();
|
|
|
file.close();
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::runSurrogateScoringProcess(const QString& candidatePath, const QString& scorePath, QString* failureReason)
|
|
|
{
|
|
|
// 代理评分统一入口。优先走常驻 Python server,失败后降级为一次性脚本。
|
|
|
// 常驻 server 可以避免每一代重复加载模型,速度更快;一次性脚本更稳,适合作为兜底。
|
|
|
QString serverFailure;
|
|
|
|
|
|
if(requestSurrogateScoresFromServer(candidatePath, scorePath, &serverFailure)) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("Surrogate scoring server unavailable; falling back to one-shot Python scoring"));
|
|
|
|
|
|
if(!serverFailure.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring server detail: %1").arg(serverFailure));
|
|
|
}
|
|
|
|
|
|
stopSurrogateScoringServer();
|
|
|
return runSurrogateScoringScriptOnce(candidatePath, scorePath, failureReason);
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::ensureSurrogateScoringServer(QString* failureReason, bool waitForReady)
|
|
|
{
|
|
|
// 确保常驻代理评分 server 已启动且与当前 run 配置匹配。
|
|
|
//
|
|
|
// serverKey 由 Python 路径、脚本路径、跟踪元数据、模型标识和阶段组成。
|
|
|
// 只要其中任何一个变化,就停止旧进程并启动新进程,避免拿旧模型或旧目标曲线评分。
|
|
|
if(m_traceMetaFilePath.isEmpty() || !QFileInfo(m_traceMetaFilePath).exists()) {
|
|
|
QString reason = "trace meta file is missing";
|
|
|
DEBUG_OUT(QString("Surrogate scoring server skipped: %1").arg(reason));
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QString mlRoot = getMlRootPath();
|
|
|
QString scriptPath = QDir(mlRoot).absoluteFilePath("scripts/score_pso_candidates_server.py");
|
|
|
|
|
|
if(!QFileInfo(scriptPath).exists()) {
|
|
|
QString reason = QString("surrogate scoring server script not found: %1").arg(scriptPath);
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QString pythonPath = getPythonExecutablePath();
|
|
|
QString serverKey = QString("%1|%2|%3|%4|%5")
|
|
|
.arg(pythonPath)
|
|
|
.arg(scriptPath)
|
|
|
.arg(m_traceMetaFilePath)
|
|
|
.arg(getSurrogateTag())
|
|
|
.arg(getSurrogateStage());
|
|
|
|
|
|
bool matchingProcessRunning = m_surrogateScoringProcess &&
|
|
|
m_surrogateScoringProcess->state() == QProcess::Running &&
|
|
|
m_surrogateScoringServerKey == serverKey;
|
|
|
|
|
|
if(!matchingProcessRunning) {
|
|
|
stopSurrogateScoringServer();
|
|
|
|
|
|
QStringList args;
|
|
|
// -u 关闭 Python 标准输出和错误输出的缓冲,C++ 可以及时读到就绪或完成消息。
|
|
|
args << "-u"
|
|
|
<< scriptPath
|
|
|
<< "--trace-meta" << m_traceMetaFilePath
|
|
|
<< "--tag" << getSurrogateTag()
|
|
|
<< "--stage" << getSurrogateStage();
|
|
|
|
|
|
m_surrogateScoringProcess = new QProcess(this);
|
|
|
m_surrogateScoringProcess->setWorkingDirectory(mlRoot);
|
|
|
|
|
|
emit logMessageGenerated(tr("Starting surrogate scoring server: python=%1, mlRoot=%2")
|
|
|
.arg(pythonPath)
|
|
|
.arg(mlRoot));
|
|
|
|
|
|
m_surrogateScoringProcess->start(pythonPath, args);
|
|
|
|
|
|
if(!m_surrogateScoringProcess->waitForStarted(15000)) {
|
|
|
QString reason = QString("failed to start scoring server: python=%1, script=%2, error=%3")
|
|
|
.arg(pythonPath)
|
|
|
.arg(scriptPath)
|
|
|
.arg(m_surrogateScoringProcess->errorString());
|
|
|
DEBUG_OUT(reason);
|
|
|
stopSurrogateScoringServer();
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
m_surrogateScoringServerKey = serverKey;
|
|
|
m_surrogateScoringServerReady = false;
|
|
|
}
|
|
|
|
|
|
// 预热调用只负责尽早启动 Python,让模型加载与初始解、预热代真实求解重叠。
|
|
|
// 第一次真正评分时会再次进入本函数并等待 ready=true。
|
|
|
if(!waitForReady) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(m_surrogateScoringServerReady) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
QTime timer;
|
|
|
timer.start();
|
|
|
QByteArray readyLine;
|
|
|
|
|
|
while(timer.elapsed() < 120000) {
|
|
|
// server 启动协议:Python 首先输出一行 JSON,包含 ready=true 或 ok=false。
|
|
|
// C++ 不做完整 JSON 解析,只检查关键字段,降低 Qt 版本依赖。
|
|
|
if(m_surrogateScoringProcess->state() != QProcess::Running) {
|
|
|
QString stdOut = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardOutput()).trimmed();
|
|
|
QString stdErr = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardError()).trimmed();
|
|
|
QString reason = QString("scoring server exited before ready: stdout=%1, stderr=%2")
|
|
|
.arg(stdOut.isEmpty() ? QString("<empty>") : stdOut)
|
|
|
.arg(stdErr.isEmpty() ? QString("<empty>") : stdErr);
|
|
|
DEBUG_OUT(reason);
|
|
|
stopSurrogateScoringServer();
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!m_surrogateScoringProcess->canReadLine()) {
|
|
|
m_surrogateScoringProcess->waitForReadyRead(500);
|
|
|
QApplication::processEvents();
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
readyLine = m_surrogateScoringProcess->readLine().trimmed();
|
|
|
|
|
|
if(readyLine.contains("\"ready\"") && readyLine.contains("true")) {
|
|
|
m_surrogateScoringServerReady = true;
|
|
|
QString stdErr = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardError()).trimmed();
|
|
|
|
|
|
if(!stdErr.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring server stderr: %1").arg(stdErr));
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(readyLine.contains("\"ok\"") && readyLine.contains("false")) {
|
|
|
QString reason = QString("scoring server failed during initialization: %1").arg(QString::fromLocal8Bit(readyLine));
|
|
|
DEBUG_OUT(reason);
|
|
|
stopSurrogateScoringServer();
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QString stdErr = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardError()).trimmed();
|
|
|
QString reason = QString("scoring server ready timeout: stderr=%1")
|
|
|
.arg(stdErr.isEmpty() ? QString("<empty>") : stdErr);
|
|
|
DEBUG_OUT(reason);
|
|
|
stopSurrogateScoringServer();
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::requestSurrogateScoresFromServer(const QString& candidatePath, const QString& scorePath, QString* failureReason)
|
|
|
{
|
|
|
// 向常驻 Python server 发送一次评分请求。
|
|
|
// 请求通过 stdin 传 JSON 行,server 读取候选 CSV 后把评分写入 scorePath,
|
|
|
// 再通过 stdout 返回 ok=true/false。
|
|
|
if(!ensureSurrogateScoringServer(failureReason)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QFile::remove(scorePath);
|
|
|
|
|
|
QString command = QString("{\"cmd\":\"score\",\"candidates\":%1,\"output\":%2}\n")
|
|
|
.arg(jsonEscape(candidatePath))
|
|
|
.arg(jsonEscape(scorePath));
|
|
|
|
|
|
QByteArray bytes = command.toUtf8();
|
|
|
qint64 written = m_surrogateScoringProcess->write(bytes);
|
|
|
|
|
|
if(written != bytes.size() || !m_surrogateScoringProcess->waitForBytesWritten(10000)) {
|
|
|
QString reason = QString("failed to send scoring request to server: written=%1 expected=%2 error=%3")
|
|
|
.arg(written)
|
|
|
.arg(bytes.size())
|
|
|
.arg(m_surrogateScoringProcess->errorString());
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QTime timer;
|
|
|
timer.start();
|
|
|
|
|
|
while(timer.elapsed() < 120000) {
|
|
|
// 每次请求最多等待 120 秒。超时或 server 退出都会让本代代理筛选降级为全量真实求解器。
|
|
|
if(m_surrogateScoringProcess->state() != QProcess::Running) {
|
|
|
QString stdOut = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardOutput()).trimmed();
|
|
|
QString stdErr = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardError()).trimmed();
|
|
|
QString reason = QString("scoring server exited during request: stdout=%1, stderr=%2")
|
|
|
.arg(stdOut.isEmpty() ? QString("<empty>") : stdOut)
|
|
|
.arg(stdErr.isEmpty() ? QString("<empty>") : stdErr);
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!m_surrogateScoringProcess->canReadLine()) {
|
|
|
m_surrogateScoringProcess->waitForReadyRead(500);
|
|
|
QApplication::processEvents();
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
QByteArray lineBytes = m_surrogateScoringProcess->readLine().trimmed();
|
|
|
QString line = QString::fromLocal8Bit(lineBytes);
|
|
|
|
|
|
if(line.contains("\"ok\"") && line.contains("true")) {
|
|
|
QFileInfo scoreInfo(scorePath);
|
|
|
|
|
|
if(scoreInfo.exists() && scoreInfo.size() > 0) {
|
|
|
QString stdErr = QString::fromLocal8Bit(m_surrogateScoringProcess->readAllStandardError()).trimmed();
|
|
|
|
|
|
if(!stdErr.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring server stderr: %1").arg(stdErr));
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
QString reason = QString("scoring server reported success but score file is missing: %1").arg(scorePath);
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(line.contains("\"ok\"") && line.contains("false")) {
|
|
|
QString reason = QString("scoring server request failed: %1").arg(line);
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QString reason = "scoring server request timed out";
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::stopSurrogateScoringServer()
|
|
|
{
|
|
|
// 停止常驻 Python 评分进程。
|
|
|
// 先发送 quit 指令,短时间内不退出再 kill,避免应用关闭时遗留 python.exe。
|
|
|
if(!m_surrogateScoringProcess) {
|
|
|
m_surrogateScoringServerKey.clear();
|
|
|
m_surrogateScoringServerReady = false;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if(m_surrogateScoringProcess->state() == QProcess::Running) {
|
|
|
m_surrogateScoringProcess->write("{\"cmd\":\"quit\"}\n");
|
|
|
m_surrogateScoringProcess->waitForBytesWritten(1000);
|
|
|
m_surrogateScoringProcess->waitForFinished(3000);
|
|
|
|
|
|
if(m_surrogateScoringProcess->state() == QProcess::Running) {
|
|
|
m_surrogateScoringProcess->kill();
|
|
|
m_surrogateScoringProcess->waitForFinished(3000);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
delete m_surrogateScoringProcess;
|
|
|
m_surrogateScoringProcess = nullptr;
|
|
|
m_surrogateScoringServerKey.clear();
|
|
|
m_surrogateScoringServerReady = false;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::runSurrogateScoringScriptOnce(const QString& candidatePath, const QString& scorePath, QString* failureReason)
|
|
|
{
|
|
|
// 一次性代理评分脚本兜底路径。
|
|
|
//
|
|
|
// 输入:
|
|
|
// - candidates:writeSurrogateCandidateCsv() 写出的粒子参数;
|
|
|
// - trace-meta:目标曲线、流量制度、参数上下界等上下文;
|
|
|
// - tag/stage:选择 ML 侧模型。
|
|
|
//
|
|
|
// 输出:
|
|
|
// - scorePath:Python 写出的评分 CSV,随后由 readSurrogateScores() 读取。
|
|
|
if(m_traceMetaFilePath.isEmpty() || !QFileInfo(m_traceMetaFilePath).exists()) {
|
|
|
QString reason = "trace meta file is missing";
|
|
|
DEBUG_OUT(QString("Surrogate scoring skipped: %1").arg(reason));
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QString mlRoot = getMlRootPath();
|
|
|
QString scriptPath = QDir(mlRoot).absoluteFilePath("scripts/score_pso_candidates.py");
|
|
|
|
|
|
if(!QFileInfo(scriptPath).exists()) {
|
|
|
QString reason = QString("surrogate scoring script not found: %1").arg(scriptPath);
|
|
|
DEBUG_OUT(reason);
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = reason;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QString pythonPath = getPythonExecutablePath();
|
|
|
QStringList args;
|
|
|
args << scriptPath
|
|
|
<< "--candidates" << candidatePath
|
|
|
<< "--trace-meta" << m_traceMetaFilePath
|
|
|
<< "--output" << scorePath
|
|
|
<< "--tag" << getSurrogateTag()
|
|
|
<< "--stage" << getSurrogateStage();
|
|
|
|
|
|
QFile::remove(scorePath);
|
|
|
|
|
|
QString lastFailure;
|
|
|
|
|
|
for(int attempt = 1; attempt <= kSurrogateScoringMaxAttempts; ++attempt) {
|
|
|
// 评分脚本可能因首次加载模型、文件占用或环境抖动失败,因此允许有限重试。
|
|
|
emit logMessageGenerated(tr("Surrogate scoring attempt %1/%2: python=%3, mlRoot=%4")
|
|
|
.arg(attempt)
|
|
|
.arg(kSurrogateScoringMaxAttempts)
|
|
|
.arg(pythonPath)
|
|
|
.arg(mlRoot));
|
|
|
|
|
|
QProcess process;
|
|
|
process.setWorkingDirectory(mlRoot);
|
|
|
process.start(pythonPath, args);
|
|
|
|
|
|
if(!process.waitForStarted(10000)) {
|
|
|
lastFailure = QString("failed to start scoring process (attempt %1/%2): python=%3, script=%4, error=%5")
|
|
|
.arg(attempt)
|
|
|
.arg(kSurrogateScoringMaxAttempts)
|
|
|
.arg(pythonPath)
|
|
|
.arg(scriptPath)
|
|
|
.arg(process.errorString());
|
|
|
DEBUG_OUT(lastFailure);
|
|
|
|
|
|
if(attempt < kSurrogateScoringMaxAttempts) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring start failed, retrying once: %1").arg(process.errorString()));
|
|
|
msleep(500);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = lastFailure;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!process.waitForFinished(120000)) {
|
|
|
process.kill();
|
|
|
process.waitForFinished(3000);
|
|
|
QString stdOut = QString::fromLocal8Bit(process.readAllStandardOutput()).trimmed();
|
|
|
QString stdErr = QString::fromLocal8Bit(process.readAllStandardError()).trimmed();
|
|
|
lastFailure = QString("scoring process timed out (attempt %1/%2): python=%3, script=%4, stdout=%5, stderr=%6")
|
|
|
.arg(attempt)
|
|
|
.arg(kSurrogateScoringMaxAttempts)
|
|
|
.arg(pythonPath)
|
|
|
.arg(scriptPath)
|
|
|
.arg(stdOut.isEmpty() ? QString("<empty>") : stdOut)
|
|
|
.arg(stdErr.isEmpty() ? QString("<empty>") : stdErr);
|
|
|
DEBUG_OUT(lastFailure);
|
|
|
|
|
|
if(attempt < kSurrogateScoringMaxAttempts) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring timed out, retrying once"));
|
|
|
msleep(500);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = lastFailure;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QString stdOut = QString::fromLocal8Bit(process.readAllStandardOutput()).trimmed();
|
|
|
QString stdErr = QString::fromLocal8Bit(process.readAllStandardError()).trimmed();
|
|
|
|
|
|
if(process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
|
|
|
lastFailure = QString("scoring process failed (attempt %1/%2): exitStatus=%3, exitCode=%4, python=%5, script=%6, stdout=%7, stderr=%8")
|
|
|
.arg(attempt)
|
|
|
.arg(kSurrogateScoringMaxAttempts)
|
|
|
.arg(process.exitStatus() == QProcess::NormalExit ? "NormalExit" : "CrashExit")
|
|
|
.arg(process.exitCode())
|
|
|
.arg(pythonPath)
|
|
|
.arg(scriptPath)
|
|
|
.arg(stdOut.isEmpty() ? QString("<empty>") : stdOut)
|
|
|
.arg(stdErr.isEmpty() ? QString("<empty>") : stdErr);
|
|
|
DEBUG_OUT(lastFailure);
|
|
|
|
|
|
if(attempt < kSurrogateScoringMaxAttempts) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring process failed, retrying once: exitCode=%1").arg(process.exitCode()));
|
|
|
msleep(500);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = lastFailure;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QFileInfo scoreInfo(scorePath);
|
|
|
|
|
|
if(!scoreInfo.exists() || scoreInfo.size() <= 0) {
|
|
|
lastFailure = QString("scoring process finished without a usable score file (attempt %1/%2): output=%3, stdout=%4, stderr=%5")
|
|
|
.arg(attempt)
|
|
|
.arg(kSurrogateScoringMaxAttempts)
|
|
|
.arg(scorePath)
|
|
|
.arg(stdOut.isEmpty() ? QString("<empty>") : stdOut)
|
|
|
.arg(stdErr.isEmpty() ? QString("<empty>") : stdErr);
|
|
|
DEBUG_OUT(lastFailure);
|
|
|
|
|
|
if(attempt < kSurrogateScoringMaxAttempts) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring produced no score file, retrying once"));
|
|
|
msleep(500);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = lastFailure;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!stdErr.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring stderr: %1").arg(stdErr));
|
|
|
}
|
|
|
|
|
|
if(!stdOut.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring stdout: %1").arg(stdOut));
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(failureReason) {
|
|
|
*failureReason = lastFailure;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QVector<double> nmCalculationAutoFitPSO::readSurrogateScores(const QString& scorePath, int expectedCount) const
|
|
|
{
|
|
|
// 读取 Python 输出的评分 CSV。
|
|
|
//
|
|
|
// 当前脚本约定至少包含:
|
|
|
// particle_id,surrogate_objective,...,success
|
|
|
// 本函数只读取 particle_id、score 和 success 标志。失败或缺失的粒子保持 NaN,
|
|
|
// 后续 buildSurrogateEvaluationMask() 会通过 fallback_gate 强制跑真实求解器。
|
|
|
QVector<double> scores(expectedCount, std::numeric_limits<double>::quiet_NaN());
|
|
|
QFile file(scorePath);
|
|
|
|
|
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
DEBUG_OUT(QString("Failed to open surrogate score file: %1").arg(scorePath));
|
|
|
return scores;
|
|
|
}
|
|
|
|
|
|
QTextStream in(&file);
|
|
|
bool firstLine = true;
|
|
|
|
|
|
while(!in.atEnd()) {
|
|
|
QString line = in.readLine().trimmed();
|
|
|
|
|
|
if(line.isEmpty()) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(firstLine) {
|
|
|
firstLine = false;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
QStringList parts = line.split(",");
|
|
|
|
|
|
if(parts.size() < 5) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
bool idOk = false;
|
|
|
bool scoreOk = false;
|
|
|
int particleId = parts[0].toInt(&idOk);
|
|
|
double score = parts[1].toDouble(&scoreOk);
|
|
|
bool success = parts[4].trimmed() == "1";
|
|
|
|
|
|
if(idOk && scoreOk && success && particleId >= 0 && particleId < scores.size()) {
|
|
|
scores[particleId] = score;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return scores;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::isSurrogateRunContextSupported(QString* reason) const
|
|
|
{
|
|
|
// 判断“当前项目工况”是否适合使用代理模型。
|
|
|
// 这是运行级别的 gate:只要项目整体不满足训练域条件,本次 PSO 全程退回真实求解器。
|
|
|
// 这样做比盲目调用代理模型更稳,因为代理模型只在特定 PEBI/单井/垂直井/无复杂几何场景训练过。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
|
if(!dataManager) {
|
|
|
if(reason) *reason = "data manager is unavailable";
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
NM_SOLVER_MODEL_TYPE solverModelType = dataManager->getSolverModelType();
|
|
|
SurrogateParameterDomain parameterDomain;
|
|
|
bool isSupportedSolverType = getSurrogateParameterDomain(solverModelType, ¶meterDomain);
|
|
|
|
|
|
if(!isSupportedSolverType) {
|
|
|
if(reason) {
|
|
|
*reason = QString("solverType=%1 is outside the T1-T5 surrogate model")
|
|
|
.arg(static_cast<int>(solverModelType));
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 网格类型 gate:当前代理模型只按 PEBI 网格样本训练。
|
|
|
if(dataManager->getGridType() != NM_Grid_PEBI) {
|
|
|
if(reason) *reason = "surrogate model is trained only for PEBI grid cases";
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
nmDataWellBase* pTargetWell = dataManager->findWellByName(m_targetWellName);
|
|
|
|
|
|
if(!pTargetWell) {
|
|
|
if(reason) *reason = QString("target well '%1' is missing").arg(m_targetWellName);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 井型 gate:当前只支持垂直井。水平井/压裂井等几何特征不在训练集中。
|
|
|
if(pTargetWell->getWellType() != NM_WELL_MODEL::Vertical_Well) {
|
|
|
if(reason) *reason = "surrogate model is currently limited to vertical-well cases";
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
int validCalculationWells = 0;
|
|
|
QVector<QPair<NM_WELL_MODEL, QString> > calculationWells = dataManager->getCalculationWells();
|
|
|
|
|
|
for(int i = 0; i < calculationWells.size(); ++i) {
|
|
|
if(calculationWells[i].first != NM_WELL_MODEL::Unknow_Well) {
|
|
|
validCalculationWells++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 单井 gate:训练数据假设只有一口有效计算井,多井干扰会改变压力响应形态。
|
|
|
if(validCalculationWells != 1) {
|
|
|
if(reason) *reason = QString("surrogate model expects one active calculation well, got %1").arg(validCalculationWells);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 几何 gate:断层、裂缝、区域变化会引入训练集中没有的边界/非均质影响。
|
|
|
if(!dataManager->getFaultDataList().isEmpty() ||
|
|
|
!dataManager->getFractureDataList().isEmpty() ||
|
|
|
!dataManager->getRegionDataList().isEmpty()) {
|
|
|
if(reason) *reason = "surrogate model is not enabled for changed fault/fracture/region geometry";
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 参数门控:只有训练数据中 Cf 实际变化的 T2/T4 允许 Cf 参与拟合。
|
|
|
// 用户勾选其它参数时,代理无法可靠反映这些参数变化,直接禁用代理筛选。
|
|
|
QVector<int> allowedParamIndices;
|
|
|
allowedParamIndices << 0 << 1 << 2 << 3 << 4;
|
|
|
|
|
|
if(parameterDomain.variableCf) {
|
|
|
allowedParamIndices << 6;
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < m_enabledParamIndices.size(); ++i) {
|
|
|
if(!allowedParamIndices.contains(m_enabledParamIndices[i])) {
|
|
|
if(reason) {
|
|
|
QStringList names = traceParameterNames();
|
|
|
int idx = m_enabledParamIndices[i];
|
|
|
QString name = (idx >= 0 && idx < names.size()) ? names[idx] : QString::number(idx);
|
|
|
*reason = QString("parameter '%1' is outside surrogate input set").arg(name);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
nmDataReservoir reservoirData = dataManager->getReservoirDataCopy();
|
|
|
if(parameterDomain.checkCt) {
|
|
|
double ct = reservoirData.getCt().getValue().toDouble();
|
|
|
|
|
|
if(!isNearRelative(ct, parameterDomain.ctFixed, parameterDomain.ctRelTol)) {
|
|
|
if(reason) {
|
|
|
*reason = QString("Ct=%1 does not match surrogate training value %2")
|
|
|
.arg(ct, 0, 'g', 10)
|
|
|
.arg(parameterDomain.ctFixed, 0, 'g', 10);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 固定 Cf 的 T5 在整次拟合入口检查;T2/T4 的可变 Cf 留到逐粒子阶段检查。
|
|
|
if(parameterDomain.checkCf && !parameterDomain.variableCf) {
|
|
|
double cf = reservoirData.getCf().getValue().toDouble();
|
|
|
bool cfSupported = isNearRelative(cf, parameterDomain.cfFixed, parameterDomain.cfRelTol);
|
|
|
if(!cfSupported) {
|
|
|
if(reason) {
|
|
|
*reason = QString("Cf=%1 is outside surrogate domain").arg(cf, 0, 'g', 10);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double initialPressure = reservoirData.getInitialPressure().getValue().toDouble();
|
|
|
|
|
|
if(!isNearRelative(initialPressure, kSurrogateTrainingInitialPressure, 1.0e-4)) {
|
|
|
if(reason) {
|
|
|
*reason = QString("initialPressure=%1 does not match surrogate training context")
|
|
|
.arg(initialPressure, 0, 'g', 10);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QVector<QPointF> flowPoints = pTargetWell->getFlowPoints();
|
|
|
|
|
|
// flowPoints 有时第一段是 (0,0) 占位点,不代表真实生产段,先移除。
|
|
|
if(!flowPoints.isEmpty() && qAbs(flowPoints[0].x()) <= 1e-12 && qAbs(flowPoints[0].y()) <= 1e-12) {
|
|
|
flowPoints.remove(0);
|
|
|
}
|
|
|
|
|
|
if(flowPoints.size() < 2) {
|
|
|
if(reason) *reason = "flow schedule is too short for surrogate screening";
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 约定最后一段是关井段,因此生产段数量 = 总段数 - 1。
|
|
|
int productionSections = flowPoints.size() - 1;
|
|
|
|
|
|
if(productionSections < kSurrogateMinProdSections || productionSections > kSurrogateMaxProdSections) {
|
|
|
if(reason) {
|
|
|
*reason = QString("production section count %1 is outside surrogate range [%2,%3]")
|
|
|
.arg(productionSections)
|
|
|
.arg(kSurrogateMinProdSections)
|
|
|
.arg(kSurrogateMaxProdSections);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
double productionTime = 0.0;
|
|
|
|
|
|
for(int i = 0; i < productionSections; ++i) {
|
|
|
double dt = flowPoints[i].x();
|
|
|
double q = flowPoints[i].y();
|
|
|
|
|
|
if(!isFiniteNumber(dt) || dt <= 0.0 || !isInClosedRange(q, kSurrogateQMin, kSurrogateQMax)) {
|
|
|
if(reason) {
|
|
|
*reason = QString("production schedule section %1 is outside surrogate dt/q domain").arg(i + 1);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(i > 0) {
|
|
|
// 相邻生产段产量跳变不能太大,否则代理模型可能没见过这种制度。
|
|
|
double previousQ = flowPoints[i - 1].y();
|
|
|
double stepRatio = qMax(q, previousQ) / qMax(1e-12, qMin(q, previousQ));
|
|
|
|
|
|
if(stepRatio > kSurrogateMaxRateStepRatio) {
|
|
|
if(reason) {
|
|
|
*reason = QString("rate step ratio %1 exceeds surrogate domain").arg(stepRatio, 0, 'g', 10);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
productionTime += dt;
|
|
|
}
|
|
|
|
|
|
// 旧阶段模型对强递减制度不稳;v2_q 阶段允许但会提高真实求解比例。
|
|
|
if(!getSurrogateStage().contains("v2_q")) {
|
|
|
double endStartRatio = std::numeric_limits<double>::quiet_NaN();
|
|
|
|
|
|
if(isStrongDecliningProductionSchedule(&endStartRatio)) {
|
|
|
if(reason) {
|
|
|
*reason = QString("strongly decreasing production schedule ratio %1 is outside surrogate domain")
|
|
|
.arg(endStartRatio, 0, 'g', 10);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!isInClosedRange(productionTime, kSurrogateProdTimeMin, kSurrogateProdTimeMax)) {
|
|
|
if(reason) {
|
|
|
*reason = QString("production total time %1 is outside surrogate range").arg(productionTime, 0, 'g', 10);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
double shutinTime = flowPoints.last().x();
|
|
|
double shutinRate = flowPoints.last().y();
|
|
|
|
|
|
if(!isInClosedRange(shutinTime, kSurrogateShutinTimeMin, kSurrogateShutinTimeMax) ||
|
|
|
qAbs(shutinRate) > kSurrogateQZeroThreshold) {
|
|
|
if(reason) *reason = "last flow section is not a trained shut-in section";
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 当前目标段必须是最后的关井段,对应试井解释的双对数分析段。
|
|
|
if(pTargetWell->getIndexF() != flowPoints.size()) {
|
|
|
if(reason) {
|
|
|
*reason = QString("target section index %1 is not the last section %2")
|
|
|
.arg(pTargetWell->getIndexF())
|
|
|
.arg(flowPoints.size());
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::isSurrogateCandidateInDomain(const QVector<double>& selectedParameters, QString* reason) const
|
|
|
{
|
|
|
// 判断“单个粒子候选参数”是否在代理模型训练域内。
|
|
|
// 与 isSurrogateRunContextSupported() 不同,这里检查的是粒子参数值本身。
|
|
|
// 超出范围的粒子会被 domain_gate 强制跑真实求解器。
|
|
|
QVector<double> params = buildTraceParameterVector(selectedParameters);
|
|
|
double k = params.size() > 0 ? params[0] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double skin = params.size() > 1 ? params[1] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double wellboreC = params.size() > 2 ? params[2] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double phi = params.size() > 3 ? params[3] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double h = params.size() > 4 ? params[4] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double cf = params.size() > 6 ? params[6] : std::numeric_limits<double>::quiet_NaN();
|
|
|
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
NM_SOLVER_MODEL_TYPE solverModelType = dataManager
|
|
|
? dataManager->getSolverModelType()
|
|
|
: SMT_Oil_ConstPvt;
|
|
|
SurrogateParameterDomain parameterDomain;
|
|
|
|
|
|
if(!getSurrogateParameterDomain(solverModelType, ¶meterDomain)) {
|
|
|
if(reason) *reason = QString("solverType=%1").arg(static_cast<int>(solverModelType));
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!isInClosedRange(k, parameterDomain.kMin, parameterDomain.kMax)) {
|
|
|
if(reason) *reason = QString("k=%1").arg(k, 0, 'g', 10);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!isInClosedRange(skin, parameterDomain.skinMin, parameterDomain.skinMax)) {
|
|
|
if(reason) *reason = QString("skin=%1").arg(skin, 0, 'g', 10);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!isInClosedRange(wellboreC, parameterDomain.wellboreCMin, parameterDomain.wellboreCMax)) {
|
|
|
if(reason) *reason = QString("wellboreC=%1").arg(wellboreC, 0, 'g', 10);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!isInClosedRange(phi, parameterDomain.phiMin, parameterDomain.phiMax)) {
|
|
|
if(reason) *reason = QString("phi=%1").arg(phi, 0, 'g', 10);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(!isInClosedRange(h, parameterDomain.hMin, parameterDomain.hMax)) {
|
|
|
if(reason) *reason = QString("h=%1").arg(h, 0, 'g', 10);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 只有参与搜索的 T2/T4 Cf 需要逐粒子检查;T5 固定 Cf 已在运行入口确认。
|
|
|
if(parameterDomain.checkCf && parameterDomain.variableCf) {
|
|
|
bool cfSupported = isInClosedRange(cf, parameterDomain.cfMin, parameterDomain.cfMax);
|
|
|
if(!cfSupported) {
|
|
|
if(reason) *reason = QString("Cf=%1").arg(cf, 0, 'g', 10);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::forceSolverByFallbackGate(const QVector<double>& selectedParameters) const
|
|
|
{
|
|
|
// 经验 fallback gate。
|
|
|
// 有些点虽然还在训练域边界内,但组合起来属于代理模型容易排序失真的区域,
|
|
|
// 例如极低渗透率配大井筒储集、极端 skin 等。遇到这些候选时直接跑真实求解器。
|
|
|
QVector<double> params = buildTraceParameterVector(selectedParameters);
|
|
|
double k = params.size() > 0 ? params[0] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double skin = params.size() > 1 ? params[1] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double wellboreC = params.size() > 2 ? params[2] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double phi = params.size() > 3 ? params[3] : std::numeric_limits<double>::quiet_NaN();
|
|
|
double h = params.size() > 4 ? params[4] : std::numeric_limits<double>::quiet_NaN();
|
|
|
|
|
|
if(!isFiniteNumber(k) || !isFiniteNumber(skin) || !isFiniteNumber(wellboreC) || !isFiniteNumber(phi) || !isFiniteNumber(h)) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(k <= 1e-3 && wellboreC > 0.5) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(phi < 0.03 && h < 4.0) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(k < 0.1 && h >= 50.0) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if(qAbs(skin) > 6.0) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::isStrongDecliningProductionSchedule(double* endStartRatio) const
|
|
|
{
|
|
|
// 判断生产段产量是否整体非增且首末下降明显。
|
|
|
// 这类流量制度代理模型风险更高,所以 keep/min_solver 会自动提高。
|
|
|
if(endStartRatio) {
|
|
|
*endStartRatio = std::numeric_limits<double>::quiet_NaN();
|
|
|
}
|
|
|
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
|
if(!dataManager) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
nmDataWellBase* pTargetWell = dataManager->findWellByName(m_targetWellName);
|
|
|
|
|
|
if(!pTargetWell) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QVector<QPointF> flowPoints = pTargetWell->getFlowPoints();
|
|
|
|
|
|
if(!flowPoints.isEmpty() && qAbs(flowPoints[0].x()) <= 1e-12 && qAbs(flowPoints[0].y()) <= 1e-12) {
|
|
|
flowPoints.remove(0);
|
|
|
}
|
|
|
|
|
|
if(flowPoints.size() < 2) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
int productionSections = flowPoints.size() - 1;
|
|
|
bool productionRatesNonIncreasing = true;
|
|
|
bool productionRatesHaveMeaningfulDrop = false;
|
|
|
|
|
|
for(int i = 0; i < productionSections; ++i) {
|
|
|
double q = flowPoints[i].y();
|
|
|
|
|
|
if(!isFiniteNumber(q)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(i > 0) {
|
|
|
double previousQ = flowPoints[i - 1].y();
|
|
|
|
|
|
if(q > previousQ * (1.0 + kSurrogateRateTrendRelTol)) {
|
|
|
productionRatesNonIncreasing = false;
|
|
|
}
|
|
|
|
|
|
if(q < previousQ * (1.0 - kSurrogateRateTrendRelTol)) {
|
|
|
productionRatesHaveMeaningfulDrop = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!productionRatesNonIncreasing || !productionRatesHaveMeaningfulDrop) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
double firstRate = flowPoints.first().y();
|
|
|
double lastProductionRate = flowPoints[productionSections - 1].y();
|
|
|
double ratio = lastProductionRate / qMax(1e-12, firstRate);
|
|
|
|
|
|
if(endStartRatio) {
|
|
|
*endStartRatio = ratio;
|
|
|
}
|
|
|
|
|
|
return ratio < kSurrogateStrongDeclineEndStartRatio;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::resetGuideBestToVerified()
|
|
|
{
|
|
|
// 全量真实评价或代理降级时,丢弃尚未验证的引导位置。
|
|
|
// 真实 bestPosition/bestFitness 始终是唯一可信的 pbest/gbest 数据源。
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
AutoFitParticle& particle = m_swarm[i];
|
|
|
|
|
|
if(particle.bestPosition.size() == particle.position.size()) {
|
|
|
particle.guideBestPosition = particle.bestPosition;
|
|
|
particle.guideBestObjective = particle.bestFitness;
|
|
|
particle.guideBestFromSurrogate = false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QVector<bool> nmCalculationAutoFitPSO::buildSurrogateEvaluationMask()
|
|
|
{
|
|
|
// 返回值 evaluateMask 与 m_swarm 一一对应:
|
|
|
// true 表示该粒子本代需要继续跑真实数值求解器;
|
|
|
// false 表示该粒子本代被代理模型筛掉,只记录 screened_out,不更新 pbest/gbest。
|
|
|
//
|
|
|
// 这个函数是 PSO acceleration 的唯一入口。为了保证结果可信,它包含多层保护:
|
|
|
// 1. 工况不支持代理模型时,全量真实求解器;
|
|
|
// 2. 前几代 warmup,全量真实求解器;
|
|
|
// 3. 周期性审计,全量真实求解器;
|
|
|
// 4. 代理评分失败时,全量真实求解器;
|
|
|
// 5. top-k + random audit + fallback + min solver floor 共同决定最终 mask。
|
|
|
QVector<bool> evaluateMask(m_swarm.size(), true);
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
m_swarm[i].surrogateObjective = std::numeric_limits<double>::quiet_NaN();
|
|
|
m_swarm[i].selectedForSolver = true;
|
|
|
m_swarm[i].selectedByAudit = false;
|
|
|
m_swarm[i].screeningDecision = "full_solver";
|
|
|
}
|
|
|
|
|
|
if(!isSurrogateScreeningEnabled()) {
|
|
|
// 用户关闭 PSO acceleration:保持原始 PSO 行为,所有粒子都跑真实求解器。
|
|
|
resetGuideBestToVerified();
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
QString contextReason;
|
|
|
|
|
|
if(!isSurrogateRunContextSupported(&contextReason)) {
|
|
|
// 当前项目工况不在代理模型训练域内。这里不报错,只降级为全量真实求解器,
|
|
|
// 这样用户仍能完成自动拟合,只是没有代理加速收益。
|
|
|
m_surrogateContextBlockedIterationCount++;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
m_swarm[i].screeningDecision = "context_gate_full_solver";
|
|
|
}
|
|
|
|
|
|
resetGuideBestToVerified();
|
|
|
emit logMessageGenerated(tr("Surrogate screening disabled for this run context: %1").arg(contextReason));
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
// 前几代全量真实评价,让每个粒子都先获得可信的个体最优 pbest。
|
|
|
// 否则代理模型一开始就筛掉粒子,可能导致某些粒子从未有真实误差基准。
|
|
|
if(m_currentIteration < getSurrogateWarmupIterations()) {
|
|
|
m_surrogateWarmupIterationCount++;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
m_swarm[i].screeningDecision = "warmup_full_solver";
|
|
|
}
|
|
|
|
|
|
resetGuideBestToVerified();
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
if(getSurrogateFullSolverInterval() > 0 &&
|
|
|
m_currentIteration > 0 &&
|
|
|
((m_currentIteration + 1) % getSurrogateFullSolverInterval()) == 0) {
|
|
|
// 周期性全量审计。它牺牲少量速度,换取对代理排序偏差的长期纠偏能力。
|
|
|
m_surrogatePeriodicAuditIterationCount++;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
m_swarm[i].screeningDecision = "periodic_full_solver";
|
|
|
}
|
|
|
|
|
|
resetGuideBestToVerified();
|
|
|
emit logMessageGenerated(tr("Surrogate audit iteration %1: running full solver for all particles")
|
|
|
.arg(m_currentIteration + 1));
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
QString mlRoot = getMlRootPath();
|
|
|
QDir tempDir(QDir(mlRoot).absoluteFilePath("data/temp"));
|
|
|
|
|
|
if(!tempDir.exists()) {
|
|
|
QDir().mkpath(tempDir.absolutePath());
|
|
|
}
|
|
|
|
|
|
QString candidatePath = tempDir.absoluteFilePath(QString("pso_screen_candidates_%1_gen%2.csv")
|
|
|
.arg(m_traceRunId)
|
|
|
.arg(m_currentIteration));// C++ 写给 Python 的候选粒子参数
|
|
|
QString scorePath = tempDir.absoluteFilePath(QString("pso_screen_scores_%1_gen%2.csv")
|
|
|
.arg(m_traceRunId)
|
|
|
.arg(m_currentIteration));// Python 写回给 C++ 的代理评分结果
|
|
|
|
|
|
QString scoringFailureReason;
|
|
|
|
|
|
// 运行上下文已经通过 T1-T5 代理模型门控,所有支持相态都使用引导 pbest。
|
|
|
const bool includeGuideCandidates = true;
|
|
|
|
|
|
if(!writeSurrogateCandidateCsv(candidatePath, includeGuideCandidates) ||
|
|
|
!runSurrogateScoringProcess(candidatePath, scorePath, &scoringFailureReason)) {
|
|
|
// Python 环境、checkpoint、processed 数据或脚本异常时都走这里。
|
|
|
// 降级策略是全量真实求解器,保证代理模块故障不会中断 PSO 拟合。
|
|
|
emit logMessageGenerated(tr("Surrogate screening unavailable; falling back to full solver for iteration %1")
|
|
|
.arg(m_currentIteration + 1));
|
|
|
|
|
|
if(!scoringFailureReason.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring failure detail: %1").arg(scoringFailureReason));
|
|
|
}
|
|
|
|
|
|
resetGuideBestToVerified();
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
int expectedScoreCount = m_swarm.size() * (includeGuideCandidates ? 2 : 1);
|
|
|
QVector<double> allScores = readSurrogateScores(scorePath, expectedScoreCount);
|
|
|
QVector<double> scores(m_swarm.size(), std::numeric_limits<double>::quiet_NaN());
|
|
|
QVector<double> guideScores(m_swarm.size(), std::numeric_limits<double>::quiet_NaN());
|
|
|
QVector<int> finiteScoreIndices;
|
|
|
|
|
|
// 只接受成功写出的有限代理目标函数。失败候选后面会通过 fallback 强制真实评价。
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
if(i < allScores.size()) {
|
|
|
scores[i] = allScores[i];
|
|
|
}
|
|
|
|
|
|
if(includeGuideCandidates && m_swarm.size() + i < allScores.size()) {
|
|
|
guideScores[i] = allScores[m_swarm.size() + i];
|
|
|
|
|
|
if(isFiniteNumber(guideScores[i])) {
|
|
|
m_swarm[i].guideBestObjective = guideScores[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
m_swarm[i].surrogateObjective = scores[i];
|
|
|
|
|
|
if(isFiniteNumber(scores[i])) {
|
|
|
finiteScoreIndices.append(i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(finiteScoreIndices.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate scoring returned no valid scores; falling back to full solver"));
|
|
|
resetGuideBestToVerified();
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
std::sort(finiteScoreIndices.begin(), finiteScoreIndices.end(), [&](int a, int b) {
|
|
|
return scores[a] < scores[b];
|
|
|
});
|
|
|
|
|
|
// 代理目标函数越小越好。先将所有粒子设为 screened_out,再逐步放回需要真实评价的粒子。
|
|
|
evaluateMask.fill(false);
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
m_swarm[i].selectedForSolver = false;
|
|
|
m_swarm[i].screeningDecision = "screened_out";
|
|
|
}
|
|
|
|
|
|
int keepN = qMax(1, qCeil(m_swarm.size() * getSurrogateKeepFraction()));
|
|
|
keepN = qMin(keepN, finiteScoreIndices.size());
|
|
|
|
|
|
// 代理评分 top-k:这些粒子最可能给出好解,优先跑真实求解器。
|
|
|
for(int rank = 0; rank < keepN; ++rank) {
|
|
|
int idx = finiteScoreIndices[rank];
|
|
|
evaluateMask[idx] = true;
|
|
|
m_swarm[idx].selectedForSolver = true;
|
|
|
m_swarm[idx].screeningDecision = "surrogate_topk";
|
|
|
}
|
|
|
|
|
|
QVector<QPair<double, int> > auditCandidates;
|
|
|
|
|
|
// 随机审计:从代理模型认为“不好”的候选中抽查一部分。
|
|
|
// 使用 deterministicAuditRandom01 保证同一随机种子下 trace 可复现。
|
|
|
for(int i = 0; i < finiteScoreIndices.size(); ++i) {
|
|
|
int idx = finiteScoreIndices[i];
|
|
|
|
|
|
if(!evaluateMask[idx]) {
|
|
|
auditCandidates.append(qMakePair(deterministicAuditRandom01(m_psoRandomSeed, m_currentIteration, idx), idx));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
std::sort(auditCandidates.begin(), auditCandidates.end(), [](const QPair<double, int>& a, const QPair<double, int>& b) {
|
|
|
return a.first < b.first;
|
|
|
});
|
|
|
|
|
|
int auditN = qMin(auditCandidates.size(), qCeil(m_swarm.size() * getSurrogateAuditFraction()));
|
|
|
|
|
|
for(int i = 0; i < auditN; ++i) {
|
|
|
int idx = auditCandidates[i].second;
|
|
|
evaluateMask[idx] = true;
|
|
|
m_swarm[idx].selectedForSolver = true;
|
|
|
m_swarm[idx].selectedByAudit = true;
|
|
|
m_swarm[idx].screeningDecision = "random_audit";
|
|
|
}
|
|
|
|
|
|
int fallbackCount = 0;
|
|
|
int domainFallbackCount = 0;
|
|
|
|
|
|
// fallback gate:只要候选点超出代理训练域、代理评分无效、粒子还没有可靠 pbest,
|
|
|
// 或落入经验高风险区域,就强制真实求解。这样做是为了保护 PSO 收敛质量。
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
QString domainReason;
|
|
|
bool domainFallback = !isSurrogateCandidateInDomain(m_swarm[i].position, &domainReason);
|
|
|
bool fallback = !isFiniteNumber(scores[i]) ||
|
|
|
m_swarm[i].bestFitness >= 1e9 ||
|
|
|
forceSolverByFallbackGate(m_swarm[i].position) ||
|
|
|
domainFallback;
|
|
|
|
|
|
if(fallback) {
|
|
|
if(!evaluateMask[i]) {
|
|
|
fallbackCount++;
|
|
|
}
|
|
|
|
|
|
if(domainFallback) {
|
|
|
domainFallbackCount++;
|
|
|
}
|
|
|
|
|
|
evaluateMask[i] = true;
|
|
|
m_swarm[i].selectedForSolver = true;
|
|
|
m_swarm[i].screeningDecision = domainFallback ? "domain_gate" : "fallback_gate";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int selectedCount = 0;
|
|
|
|
|
|
for(int i = 0; i < evaluateMask.size(); ++i) {
|
|
|
if(evaluateMask[i]) {
|
|
|
selectedCount++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int minSolverN = qMax(1, qCeil(m_swarm.size() * getSurrogateMinSolverFraction()));
|
|
|
|
|
|
if(selectedCount < minSolverN) {
|
|
|
// 最小真实求解比例:防止代理模型过度筛选,导致一代中真实误差信息太少。
|
|
|
QVector<QPair<double, int> > extraCandidates;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
if(!evaluateMask[i] && isFiniteNumber(scores[i])) {
|
|
|
extraCandidates.append(qMakePair(scores[i], i));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
std::sort(extraCandidates.begin(), extraCandidates.end(), [](const QPair<double, int>& a, const QPair<double, int>& b) {
|
|
|
return a.first < b.first;
|
|
|
});
|
|
|
|
|
|
for(int i = 0; i < extraCandidates.size() && selectedCount < minSolverN; ++i) {
|
|
|
int idx = extraCandidates[i].second;
|
|
|
evaluateMask[idx] = true;
|
|
|
m_swarm[idx].selectedForSolver = true;
|
|
|
m_swarm[idx].screeningDecision = "min_solver_floor";
|
|
|
selectedCount++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int guidePbestUpdateCount = 0;
|
|
|
|
|
|
// 单纯按本代绝对误差 top-k 可能漏掉“当前还不够好、但正在显著改善自己
|
|
|
// pbest”的探索粒子。对真正被筛掉的粒子,仅更新一套
|
|
|
// 用于个体认知速度项的引导位置;真实 bestPosition、bestFitness 和 gbest 完全不变。
|
|
|
if(includeGuideCandidates) {
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
AutoFitParticle& particle = m_swarm[i];
|
|
|
|
|
|
if(evaluateMask[i] || !isFiniteNumber(scores[i]) || !isFiniteNumber(guideScores[i])) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double requiredImprovement = qMax(1.0e-10,
|
|
|
qAbs(guideScores[i]) * kSurrogateGuidePbestMinRelativeImprovement);
|
|
|
|
|
|
if(guideScores[i] - scores[i] > requiredImprovement) {
|
|
|
particle.guideBestPosition = particle.position;
|
|
|
particle.guideBestObjective = scores[i];
|
|
|
particle.guideBestFromSurrogate = true;
|
|
|
particle.screeningDecision = "screened_out_guide_pbest";
|
|
|
guidePbestUpdateCount++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("Surrogate screening iteration %1: selected %2/%3 particles (keep=%4, audit=%5, min_solver=%6, fallback=%7, domain=%8, guide_pbest=%9)")
|
|
|
.arg(m_currentIteration + 1)
|
|
|
.arg(selectedCount)
|
|
|
.arg(m_swarm.size())
|
|
|
.arg(getSurrogateKeepFraction(), 0, 'f', 2)
|
|
|
.arg(getSurrogateAuditFraction(), 0, 'f', 2)
|
|
|
.arg(getSurrogateMinSolverFraction(), 0, 'f', 2)
|
|
|
.arg(fallbackCount)
|
|
|
.arg(domainFallbackCount)
|
|
|
.arg(guidePbestUpdateCount));
|
|
|
|
|
|
m_surrogateActiveIterationCount++;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
const QString& decision = m_swarm[i].screeningDecision;
|
|
|
|
|
|
if(m_swarm[i].selectedForSolver) {
|
|
|
m_surrogateSelectedParticleCount++;
|
|
|
}
|
|
|
|
|
|
if(decision.startsWith("screened_out")) {
|
|
|
m_surrogateScreenedParticleCount++;
|
|
|
} else if(decision == "surrogate_topk") {
|
|
|
m_surrogateTopKParticleCount++;
|
|
|
} else if(decision == "random_audit") {
|
|
|
m_surrogateAuditParticleCount++;
|
|
|
} else if(decision == "fallback_gate") {
|
|
|
m_surrogateFallbackParticleCount++;
|
|
|
} else if(decision == "domain_gate") {
|
|
|
m_surrogateDomainParticleCount++;
|
|
|
} else if(decision == "min_solver_floor") {
|
|
|
m_surrogateMinFloorParticleCount++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return evaluateMask;
|
|
|
}
|
|
|
|
|
|
//QString nmCalculationAutoFitPSO::getTargetWellName() const
|
|
|
//{
|
|
|
// return m_targetWellName;
|
|
|
//}
|
|
|
|
|
|
// ==================== 数据加载方法 ====================
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::loadAllConfigFromDataManager()
|
|
|
{
|
|
|
// 统一从 DataManager 加载本次运行所需配置。
|
|
|
// UI 层只负责把用户选择保存到 nmDataAutomaticFitting,本类从这里开始完全数据驱动。
|
|
|
try {
|
|
|
loadOptimizationConfig();
|
|
|
loadParameterBounds();
|
|
|
extractUserInitialValues(); // 直接提取初始值,无需条件判断
|
|
|
return true;
|
|
|
} catch(...) {
|
|
|
m_lastError = "Failed to load configuration from data manager";
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::loadOptimizationConfig()
|
|
|
{
|
|
|
// 读取“优化控制”配置:迭代次数、目标误差、是否启用代理筛选。
|
|
|
// PSO 的 swarm/inertia/cognitive/social 目前是类内固定默认值,
|
|
|
// 不是界面输入项;后续若要开放高级设置,可以从这里扩展。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
nmDataAutomaticFitting fittingData = dataManager->getAutomaticFittingDataCopy();
|
|
|
|
|
|
// 加载基本配置
|
|
|
m_maxIterations = fittingData.getIterationCount().getValue().toInt();
|
|
|
m_targetError = fittingData.getErrorTolerance().getValue().toDouble();
|
|
|
m_surrogateScreeningEnabled = fittingData.getSurrogateScreeningEnabled();
|
|
|
|
|
|
// 设置 PSO 默认参数:
|
|
|
// swarmSize=20 控制每代粒子数;inertia/cognitive/social 控制搜索惯性、
|
|
|
// 个体经验和群体经验的权重。adaptiveParameterUpdate() 会在运行中微调这些值。
|
|
|
m_swarmSize = 20;
|
|
|
m_inertiaWeight = 0.4;
|
|
|
m_cognitiveParam = 2.0;
|
|
|
m_socialParam = 1.2;
|
|
|
|
|
|
DEBUG_OUT(QString("Loaded optimization config: iterations=%1, error=%2, surrogate=%3")
|
|
|
.arg(m_maxIterations).arg(m_targetError).arg(m_surrogateScreeningEnabled ? "enabled" : "disabled"));
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::loadParameterBounds()
|
|
|
{
|
|
|
// 读取用户勾选的拟合参数及上下界。
|
|
|
//
|
|
|
// 这里构建三个核心数组:
|
|
|
// - m_parameterSelected[10]:完整参数体系中每个参数是否参与拟合;
|
|
|
// - m_parameterLower/Upper[10]:完整参数体系的搜索上下界;
|
|
|
// - m_enabledParamIndices:把粒子内部紧凑向量映射回完整参数索引。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
nmDataAutomaticFitting fittingData = dataManager->getAutomaticFittingDataCopy();
|
|
|
|
|
|
// 获取参数选择状态
|
|
|
m_parameterSelected.resize(10);
|
|
|
m_parameterSelected[0] = fittingData.getPermeabilitySelected();
|
|
|
m_parameterSelected[1] = fittingData.getSkinSelected();
|
|
|
m_parameterSelected[2] = fittingData.getWellboreStorageSelected();
|
|
|
m_parameterSelected[3] = fittingData.getPorositySelected();
|
|
|
m_parameterSelected[4] = fittingData.getThicknessSelected();
|
|
|
m_parameterSelected[5] = fittingData.getCtSelected();
|
|
|
m_parameterSelected[6] = fittingData.getCfSelected();
|
|
|
m_parameterSelected[7] = fittingData.getSwiSelected();
|
|
|
m_parameterSelected[8] = fittingData.getFractureConductivitySelected();
|
|
|
m_parameterSelected[9] = fittingData.getFractureHalfLengthSelected();
|
|
|
|
|
|
// 获取参数边界
|
|
|
m_parameterLower.resize(10);
|
|
|
m_parameterUpper.resize(10);
|
|
|
|
|
|
m_parameterLower[0] = fittingData.getPermeabilityMin().getValue().toDouble();
|
|
|
m_parameterUpper[0] = fittingData.getPermeabilityMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[1] = fittingData.getSkinMin().getValue().toDouble();
|
|
|
m_parameterUpper[1] = fittingData.getSkinMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[2] = fittingData.getWellboreStorageMin().getValue().toDouble();
|
|
|
m_parameterUpper[2] = fittingData.getWellboreStorageMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[3] = fittingData.getPorosityMin().getValue().toDouble();
|
|
|
m_parameterUpper[3] = fittingData.getPorosityMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[4] = fittingData.getThicknessMin().getValue().toDouble();
|
|
|
m_parameterUpper[4] = fittingData.getThicknessMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[5] = fittingData.getCtMin().getValue().toDouble();
|
|
|
m_parameterUpper[5] = fittingData.getCtMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[6] = fittingData.getCfMin().getValue().toDouble();
|
|
|
m_parameterUpper[6] = fittingData.getCfMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[7] = fittingData.getSwiMin().getValue().toDouble();
|
|
|
m_parameterUpper[7] = fittingData.getSwiMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[8] = fittingData.getFractureConductivityMin().getValue().toDouble();
|
|
|
m_parameterUpper[8] = fittingData.getFractureConductivityMax().getValue().toDouble();
|
|
|
|
|
|
m_parameterLower[9] = fittingData.getFractureHalfLengthMin().getValue().toDouble();
|
|
|
m_parameterUpper[9] = fittingData.getFractureHalfLengthMax().getValue().toDouble();
|
|
|
|
|
|
// 更新启用参数索引
|
|
|
m_enabledParamIndices.clear();
|
|
|
|
|
|
for(int i = 0; i < m_parameterSelected.size(); ++i) {
|
|
|
if(m_parameterSelected[i]) {
|
|
|
m_enabledParamIndices.append(i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Loaded parameter bounds: %1 enabled parameters")
|
|
|
.arg(m_enabledParamIndices.size()));
|
|
|
}
|
|
|
|
|
|
// ==================== 自动拟合核心方法 ====================
|
|
|
bool nmCalculationAutoFitPSO::startAutoFitting()
|
|
|
{
|
|
|
// 自动拟合总入口:代理开启时保留原 PSO 筛选流程;代理关闭时改走
|
|
|
// 诊断灵敏度信赖域搜索。两条路径共用初始解评价、真实求解器和结果写回。
|
|
|
StopReasonPSO finalReason = PSO_CONTINUE_OPTIMIZATION;
|
|
|
bool useParticleSwarm = false;
|
|
|
|
|
|
if(m_isRunning) {
|
|
|
m_lastError = "Auto fitting is already running";
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
// 从 DataManager 读取界面保存的自动拟合配置。
|
|
|
// 本类不直接依赖 UI 控件,便于后续从脚本或其他入口复用。
|
|
|
if(!loadAllConfigFromDataManager()) {
|
|
|
emit logMessageGenerated(tr("ERROR: Failed to load configuration from data manager"));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
useParticleSwarm = isSurrogateScreeningEnabled();
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("Algorithm: Particle Swarm Optimization")
|
|
|
: tr("Algorithm: Diagnostic Trust-Region Search"));
|
|
|
|
|
|
if(m_simulationMode) {
|
|
|
// 调试/演示用快速路径,不调用真实求解器。正式工况通常不走这里。
|
|
|
DEBUG_OUT("=== SIMULATION MODE ACTIVATED ===");
|
|
|
runSimulatedFitting();
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
int enabledParams = getEnabledParameterCount();
|
|
|
emit logMessageGenerated(tr("Enabled parameters count: %1").arg(enabledParams));
|
|
|
|
|
|
if(enabledParams == 0) {
|
|
|
m_lastError = "No parameters enabled for optimization";
|
|
|
emit logMessageGenerated(tr("ERROR: No parameters enabled for optimization"));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(m_targetLogLogData.isEmpty() || m_targetLogLogData.size() < 3) {
|
|
|
m_lastError = "Target LogLog data is empty or insufficient";
|
|
|
emit logMessageGenerated(tr("ERROR: Target LogLog data is empty or insufficient"));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 目标曲线必须至少包含三列:time、pressure、pressure derivative,
|
|
|
// 且三列长度一致。后续误差函数会按 time 做插值对齐。
|
|
|
if(m_targetLogLogData[0].size() != m_targetLogLogData[1].size() ||
|
|
|
m_targetLogLogData[0].size() != m_targetLogLogData[2].size()) {
|
|
|
m_lastError = "Target LogLog data arrays have inconsistent sizes";
|
|
|
emit logMessageGenerated(tr("ERROR: Target LogLog data arrays have inconsistent sizes"));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("Target data validation passed (%1 data points)").arg(m_targetLogLogData[0].size()));
|
|
|
|
|
|
if(m_targetWellName.isEmpty()) {
|
|
|
m_lastError = "Target well name is empty";
|
|
|
emit logMessageGenerated(tr("ERROR: Target well name is empty"));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("Particle evaluation mode: solve all wells, retain target well '%1' only")
|
|
|
.arg(m_targetWellName));
|
|
|
|
|
|
// 使用保存的初始值进行精英保护。resetOptimizer() 会清空部分运行状态,
|
|
|
// 所以先把用户当前模型参数缓存下来,后面再恢复用于初始解评价和粒子初始化。
|
|
|
QVector<double> savedInitialValues = m_initialValues;
|
|
|
|
|
|
// 重置状态
|
|
|
resetOptimizer();
|
|
|
m_isRunning = true;
|
|
|
m_shouldStop = false;
|
|
|
m_isPaused = false;
|
|
|
m_currentIteration = 0;
|
|
|
m_consecutiveFailures = 0;
|
|
|
|
|
|
if(kUseFixedPsoSeed) {
|
|
|
// 固定随机种子便于复现 trace 和代理筛选效果。若做生产随机搜索,
|
|
|
// 可以关闭 kUseFixedPsoSeed,改用当前时间生成种子。
|
|
|
m_psoRandomSeed = kFixedPsoSeed;
|
|
|
} else {
|
|
|
QTime seedTime = QTime::currentTime();
|
|
|
m_psoRandomSeed = static_cast<unsigned int>(QDateTime::currentDateTime().toTime_t());
|
|
|
m_psoRandomSeed ^= static_cast<unsigned int>((seedTime.hour() << 22) ^ (seedTime.minute() << 16) ^ (seedTime.second() << 10) ^ seedTime.msec());
|
|
|
}
|
|
|
|
|
|
if(m_psoRandomSeed == 0u) {
|
|
|
m_psoRandomSeed = 1u;
|
|
|
}
|
|
|
|
|
|
qsrand(m_psoRandomSeed);
|
|
|
m_consecutiveFailedIterations = 0; // 重置连续失败迭代计数
|
|
|
|
|
|
// 精英保护:先评价用户当前模型。
|
|
|
// 如果后续 PSO 没找到更好解,最终结果不会比用户已有模型更差。
|
|
|
m_initialValues = savedInitialValues;
|
|
|
initializeTraceFile();
|
|
|
|
|
|
// 尽早启动代理评分进程,但不在这里等待模型加载完成。
|
|
|
// Python 会与初始解及前两代真实求解并行预热;第一次需要评分时再等待就绪。
|
|
|
if(isSurrogateScreeningEnabled()) {
|
|
|
QString contextReason;
|
|
|
|
|
|
if(isSurrogateRunContextSupported(&contextReason)) {
|
|
|
QString prewarmFailureReason;
|
|
|
|
|
|
if(ensureSurrogateScoringServer(&prewarmFailureReason, false)) {
|
|
|
emit logMessageGenerated(tr("Surrogate model prewarming started in background"));
|
|
|
} else if(!prewarmFailureReason.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("Surrogate model prewarm deferred: %1").arg(prewarmFailureReason));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!savedInitialValues.isEmpty()) {
|
|
|
m_userInitialSolution = savedInitialValues;
|
|
|
emit logMessageGenerated(tr("=== Evaluating Initial Solution (Elite Protection) ==="));
|
|
|
|
|
|
// 输出初始参数值,方便在 trace / 日志中确认初始解对应的参数顺序。
|
|
|
QString paramStr = tr("Initial parameters: ");
|
|
|
|
|
|
for(int i = 0; i < m_userInitialSolution.size(); ++i) {
|
|
|
paramStr += QString("[%1]=%2 ").arg(i).arg(m_userInitialSolution[i], 0, 'f', 6);
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(paramStr);
|
|
|
|
|
|
try {
|
|
|
emit logMessageGenerated(tr("Starting initial solution evaluation..."));
|
|
|
DEBUG_OUT(QString("Before evaluateError: m_userInitialSolution size = %1").arg(m_userInitialSolution.size()));
|
|
|
|
|
|
if(m_userInitialSolution.isEmpty()) {
|
|
|
emit logMessageGenerated(tr("ERROR: m_userInitialSolution is empty!"));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
QTime initialEvalTimer;
|
|
|
initialEvalTimer.start();
|
|
|
// 初始精英解也是一次真实 DLL 调用。第一代第0号粒子会复用它,
|
|
|
// 因此在这里计数,保证汇总中的评价次数等于实际求解调用数。
|
|
|
m_totalEvaluations++;
|
|
|
m_userInitialFitness = evaluateFitness(m_userInitialSolution);
|
|
|
int initialEvalElapsedMs = initialEvalTimer.elapsed();
|
|
|
|
|
|
if(m_userInitialFitness < 1e9) {
|
|
|
m_successfulEvaluations++;
|
|
|
m_hasValidUserSolution = true;
|
|
|
m_globalBestFitness = m_userInitialFitness;
|
|
|
m_globalBestPosition = m_userInitialSolution;
|
|
|
m_userInitialLogLogData = m_lastEvaluatedLogLogData;
|
|
|
m_globalBestLogLogData = m_userInitialLogLogData;
|
|
|
m_userInitialObjectiveBreakdown = m_lastObjectiveBreakdown;
|
|
|
m_globalBestObjectiveBreakdown = m_userInitialObjectiveBreakdown;
|
|
|
|
|
|
emit logMessageGenerated(tr("Initial solution evaluation successful"));
|
|
|
emit logMessageGenerated(tr("Initial Error: %1").arg(m_userInitialFitness, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Elite protection activated - initial solution will be preserved if no significant improvement found"));
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, 0, m_globalBestFitness);
|
|
|
} else {
|
|
|
m_hasValidUserSolution = false;
|
|
|
emit logMessageGenerated(tr("Initial solution evaluation failed - starting with random initialization"));
|
|
|
}
|
|
|
|
|
|
writeTraceRow(-1,
|
|
|
-1,
|
|
|
"initial_solution",
|
|
|
m_userInitialSolution,
|
|
|
m_userInitialFitness,
|
|
|
m_userInitialFitness < 1e9,
|
|
|
initialEvalElapsedMs,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
"initial_solution",
|
|
|
m_hasValidUserSolution ? m_userInitialSolution : QVector<double>(),
|
|
|
m_userInitialFitness,
|
|
|
m_hasValidUserSolution
|
|
|
? &m_userInitialObjectiveBreakdown : nullptr);
|
|
|
} catch(...) {
|
|
|
m_hasValidUserSolution = false;
|
|
|
emit logMessageGenerated(tr("Exception during initial solution evaluation"));
|
|
|
}
|
|
|
|
|
|
// 恢复初始值供粒子群初始化使用
|
|
|
m_initialValues = savedInitialValues;
|
|
|
}
|
|
|
|
|
|
if(useParticleSwarm) {
|
|
|
// 初始化粒子群。粒子维度等于用户勾选的参数数量,而不是固定 11 维。
|
|
|
if(kUseFixedPsoSeed) {
|
|
|
emit logMessageGenerated(tr("PSO random seed: %1 ").arg(m_psoRandomSeed));
|
|
|
} else {
|
|
|
emit logMessageGenerated(tr("PSO random seed: %1 ").arg(m_psoRandomSeed));
|
|
|
}
|
|
|
|
|
|
initializeSwarm();
|
|
|
emit logMessageGenerated(tr("Swarm initialized: %1 particles, %2 dimensions").arg(m_swarmSize).arg(getEnabledParameterCount()));
|
|
|
|
|
|
// PSO主循环:每一代先决定哪些粒子需要真实评价,再更新全局最优和粒子位置。
|
|
|
emit logMessageGenerated(tr("=== Starting PSO Main Loop ==="));
|
|
|
|
|
|
for(m_currentIteration = 0; m_currentIteration < m_maxIterations && !m_shouldStop; ++m_currentIteration) {
|
|
|
// 每次迭代都输出标题,或者只在重要迭代输出详细信息
|
|
|
bool shouldOutputDetail = (m_currentIteration % qMax(1, m_maxIterations / 10) == 0) ||
|
|
|
(m_currentIteration < 5) ||
|
|
|
(m_currentIteration >= m_maxIterations - 2); // 总是输出前5次和最后2次的详细信息
|
|
|
|
|
|
// 每次迭代都输出标题
|
|
|
emit logMessageGenerated(tr("--- Iteration %1/%2 ---").arg(m_currentIteration + 1).arg(m_maxIterations));
|
|
|
|
|
|
// 只在特定迭代输出详细统计信息
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr("Current best error: %1").arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Total evaluations: %1 (successful: %2, failures: %3)")
|
|
|
.arg(m_totalEvaluations).arg(m_successfulEvaluations).arg(m_totalEvaluations - m_successfulEvaluations));
|
|
|
}
|
|
|
|
|
|
// 检查暂停状态
|
|
|
while(m_isPaused && !m_shouldStop) {
|
|
|
QApplication::processEvents();
|
|
|
msleep(100);
|
|
|
}
|
|
|
|
|
|
if(m_shouldStop) {
|
|
|
emit logMessageGenerated(tr("Optimization stopped by user request"));
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
int currentIterationSuccessful = 0;
|
|
|
int currentIterationTotal = 0;
|
|
|
int currentIterationFailed = 0;
|
|
|
int currentIterationSkipped = 0;
|
|
|
QVector<bool> evaluateMask = buildSurrogateEvaluationMask();
|
|
|
|
|
|
for(int i = 0; i < m_swarmSize && !m_shouldStop; ++i) {
|
|
|
try {
|
|
|
if(i < evaluateMask.size() && !evaluateMask[i]) {
|
|
|
// 被代理模型筛掉的粒子本代不跑真实求解器。
|
|
|
// 这里不会用 surrogateObjective 更新 pbest,避免代理误差污染真实最优。
|
|
|
AutoFitParticle& particle = m_swarm[i];
|
|
|
particle.evaluatedThisIteration = false;
|
|
|
particle.lastEvaluationSuccess = false;
|
|
|
particle.lastEvaluationElapsedMs = -1;
|
|
|
particle.pbestRelativeImprovementThisIteration = 0.0;
|
|
|
particle.selectedForSolver = false;
|
|
|
|
|
|
if(particle.screeningDecision.isEmpty()) {
|
|
|
particle.screeningDecision = "screened_out";
|
|
|
}
|
|
|
|
|
|
currentIterationSkipped++;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double previousFitness = m_swarm[i].fitness;
|
|
|
// 对单个粒子执行真实评价:写参数 -> 跑求解器 -> 算曲线误差 -> 更新 pbest。
|
|
|
updateParticle(i);
|
|
|
currentIterationTotal++;
|
|
|
|
|
|
if(m_swarm[i].fitness < 1e9) {
|
|
|
currentIterationSuccessful++;
|
|
|
|
|
|
if(shouldOutputDetail && m_swarm[i].fitness < previousFitness) {
|
|
|
// 计算改进幅度
|
|
|
double improvement = (previousFitness - m_swarm[i].fitness) / qMax(1e-10, previousFitness);
|
|
|
|
|
|
if(improvement > 0.01) { // 1%以上改进才输出
|
|
|
emit logMessageGenerated(tr(" Particle %1 improved: %2 -> %3 (%4% improvement)")
|
|
|
.arg(i + 1).arg(previousFitness, 0, 'e', 3).arg(m_swarm[i].fitness, 0, 'e', 3)
|
|
|
.arg(improvement * 100, 0, 'f', 2));
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
currentIterationFailed++;
|
|
|
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr(" Particle %1: evaluation failed").arg(i + 1));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 每评估2个粒子处理一次事件
|
|
|
if(i % 2 == 0) {
|
|
|
QApplication::processEvents();
|
|
|
}
|
|
|
|
|
|
} catch(const std::exception& e) {
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr(" Particle %1: Exception: %2").arg(i + 1).arg(e.what()));
|
|
|
}
|
|
|
|
|
|
m_swarm[i].fitness = 1e10;
|
|
|
currentIterationTotal++;
|
|
|
currentIterationFailed++;
|
|
|
m_totalEvaluations++;
|
|
|
} catch(...) {
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr(" Particle %1: Unknown exception").arg(i + 1));
|
|
|
}
|
|
|
|
|
|
m_swarm[i].fitness = 1e10;
|
|
|
currentIterationTotal++;
|
|
|
currentIterationFailed++;
|
|
|
m_totalEvaluations++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(m_shouldStop) break;
|
|
|
|
|
|
double currentSuccessRate = currentIterationTotal > 0 ?
|
|
|
(double)currentIterationSuccessful / currentIterationTotal : 0.0;
|
|
|
|
|
|
// 更新连续失败迭代计数
|
|
|
if(currentIterationSuccessful == 0) {
|
|
|
m_consecutiveFailedIterations++;
|
|
|
emit logMessageGenerated(tr("WARNING: No successful evaluations in iteration %1 (consecutive failures: %2)")
|
|
|
.arg(m_currentIteration + 1).arg(m_consecutiveFailedIterations));
|
|
|
} else {
|
|
|
m_consecutiveFailedIterations = 0; // 重置连续失败计数
|
|
|
}
|
|
|
|
|
|
// 输出当前迭代统计
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr("Current iteration stats: %1 successful, %2 failed, %3 skipped out of %4 particles (solver success rate: %5%)")
|
|
|
.arg(currentIterationSuccessful).arg(currentIterationFailed)
|
|
|
.arg(currentIterationSkipped)
|
|
|
.arg(m_swarmSize).arg(currentSuccessRate * 100, 0, 'f', 1));
|
|
|
}
|
|
|
|
|
|
// 检查是否需要停止优化
|
|
|
if(m_consecutiveFailedIterations >= m_maxConsecutiveFailures) {
|
|
|
m_lastError = QString("Too many consecutive failed iterations (%1)").arg(m_consecutiveFailedIterations);
|
|
|
emit logMessageGenerated(tr("ERROR: Too many consecutive failed iterations (%1/%2) - stopping optimization")
|
|
|
.arg(m_consecutiveFailedIterations).arg(m_maxConsecutiveFailures));
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 警告低成功率但不立即停止
|
|
|
if(currentSuccessRate < 0.5 && m_currentIteration > 3) {
|
|
|
emit logMessageGenerated(tr("WARNING: Low success rate (%1%) in iteration %2, but continuing optimization")
|
|
|
.arg(currentSuccessRate * 100, 0, 'f', 1).arg(m_currentIteration + 1));
|
|
|
}
|
|
|
|
|
|
// 只从真实求解器确认的粒子 bestFitness 更新全局最优,不使用代理分数。
|
|
|
updateGlobalBest();
|
|
|
// 记录本代所有粒子的真实/代理误差和筛选决策,用于复盘和排障。
|
|
|
writeIterationTraceRows();
|
|
|
|
|
|
// 自适应参数更新
|
|
|
adaptiveParameterUpdate(m_currentIteration);
|
|
|
|
|
|
emit progressUpdated(m_currentIteration + 1, m_globalBestFitness);
|
|
|
|
|
|
// 记录收敛历史
|
|
|
m_convergenceHistory.append(m_globalBestFitness);
|
|
|
|
|
|
// 每5次迭代输出收敛状态
|
|
|
if((m_currentIteration + 1) % 5 == 0) {
|
|
|
double recentImprovement = 0.0;
|
|
|
|
|
|
if(m_convergenceHistory.size() >= 5) {
|
|
|
double oldBest = m_convergenceHistory[m_convergenceHistory.size() - 5];
|
|
|
recentImprovement = (oldBest - m_globalBestFitness) / qMax(1e-10, oldBest);
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr(" Convergence check: recent improvement = %1% over last 5 iterations")
|
|
|
.arg(recentImprovement * 100, 0, 'f', 4));
|
|
|
}
|
|
|
|
|
|
// 更新收敛指标
|
|
|
updateConvergenceMetrics();
|
|
|
|
|
|
// 智能收敛判断:目标达成、稳定收敛、局部最优、连续失败等都会在这里决策。
|
|
|
StopReasonPSO stopReason = analyzeOptimizationStatus();
|
|
|
|
|
|
if(stopReason == PSO_TARGET_ACHIEVED) {
|
|
|
emit logMessageGenerated(tr("=== TARGET ACHIEVED ==="));
|
|
|
emit logMessageGenerated(tr("Target error achieved! Current error: %1 < Target: %2")
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_targetError, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Optimization completed successfully after %1 iterations")
|
|
|
.arg(m_currentIteration + 1));
|
|
|
break;
|
|
|
} else if(stopReason == PSO_TRUE_CONVERGENCE) {
|
|
|
emit logMessageGenerated(tr("=== TRUE CONVERGENCE DETECTED ==="));
|
|
|
emit logMessageGenerated(tr("Algorithm has converged to a stable solution"));
|
|
|
emit logMessageGenerated(tr("Final error: %1 after %2 iterations")
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1));
|
|
|
emit logMessageGenerated(tr("Solution quality: %1 (1.0 = target achieved)")
|
|
|
.arg(m_targetError / qMax(1e-10, m_globalBestFitness), 0, 'f', 3));
|
|
|
break;
|
|
|
} else if(stopReason == PSO_LOCAL_OPTIMUM) {
|
|
|
emit logMessageGenerated(tr("=== LOCAL OPTIMUM DETECTED ==="));
|
|
|
emit logMessageGenerated(tr("Algorithm appears to be trapped in local optimum"));
|
|
|
emit logMessageGenerated(tr("Current error: %1 after %2 iterations")
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1));
|
|
|
emit logMessageGenerated(tr("Suggestion: Try restarting with different parameters or larger search space"));
|
|
|
break;
|
|
|
} else if(stopReason == PSO_CONSECUTIVE_FAILURES) {
|
|
|
emit logMessageGenerated(tr("=== CONSECUTIVE FAILURES ==="));
|
|
|
emit logMessageGenerated(tr("Too many consecutive failed iterations (%1/%2)")
|
|
|
.arg(m_consecutiveFailedIterations).arg(m_maxConsecutiveFailures));
|
|
|
break;
|
|
|
} else if(stopReason == PSO_CONTINUE_OPTIMIZATION) {
|
|
|
// 继续优化,每10次迭代输出一次状态
|
|
|
if(m_currentIteration > 0 && m_currentIteration % 10 == 0) {
|
|
|
double diversity = calculateSwarmDiversity();
|
|
|
double avgVel = calculateAverageVelocity();
|
|
|
double stagnation = calculateParticleStagnationRate();
|
|
|
|
|
|
emit logMessageGenerated(tr(" Optimization status: diversity=%1, avgVel=%2, stagnation=%3%")
|
|
|
.arg(diversity, 0, 'f', 4).arg(avgVel, 0, 'f', 4).arg(stagnation * 100, 0, 'f', 1));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新粒子位置和速度
|
|
|
updateVelocityAndPosition();
|
|
|
|
|
|
// 输出迭代结束标记
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr(" Iteration %1 completed - Current best: %2")
|
|
|
.arg(m_currentIteration + 1).arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
}
|
|
|
|
|
|
// 强制处理事件,保持界面响应
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
// 在迭代间稍作停顿,减少系统负载
|
|
|
if(m_currentIteration % 3 == 2) {
|
|
|
msleep(200);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
finalReason = analyzeOptimizationStatus();
|
|
|
} else {
|
|
|
// 非代理路径不初始化粒子,也不使用 pbest/gbest 速度更新。
|
|
|
finalReason = runTrustRegionFitting();
|
|
|
}
|
|
|
|
|
|
// 最终结果验证和保护
|
|
|
validateAndProtectFinalResult();
|
|
|
|
|
|
if(!useParticleSwarm && !m_globalBestPosition.isEmpty() &&
|
|
|
m_globalBestObjectiveBreakdown.valid) {
|
|
|
// 精英保护可能恢复用户初始解,最终行必须在保护之后写入,确保 trace
|
|
|
// 中最后记录的就是实际回写 DataManager 的参数,而非最后一次接受候选。
|
|
|
writeTraceRow(m_currentIteration, -1,
|
|
|
"trust_region_final",
|
|
|
m_globalBestPosition,
|
|
|
m_globalBestFitness,
|
|
|
m_globalBestFitness < 1.0e9,
|
|
|
-1,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
"final_result",
|
|
|
m_globalBestPosition,
|
|
|
m_globalBestFitness,
|
|
|
&m_globalBestObjectiveBreakdown);
|
|
|
}
|
|
|
|
|
|
if(m_globalBestFitness < m_targetError) {
|
|
|
finalReason = PSO_TARGET_ACHIEVED;
|
|
|
}
|
|
|
|
|
|
} catch(const std::exception& e) {
|
|
|
m_lastError = useParticleSwarm
|
|
|
? QString(tr("Critical exception in PSO main loop: %1")).arg(e.what())
|
|
|
: QString(tr("Critical exception in automatic fitting: %1")).arg(e.what());
|
|
|
emit logMessageGenerated(tr("CRITICAL ERROR: %1").arg(e.what()));
|
|
|
closeTraceFile();
|
|
|
cleanupTemporaryDirectory();
|
|
|
m_isRunning = false;
|
|
|
emit fittingFinished(false, m_lastError);
|
|
|
return false;
|
|
|
} catch(...) {
|
|
|
m_lastError = useParticleSwarm
|
|
|
? QString(tr("Unknown critical exception in PSO main loop"))
|
|
|
: QString(tr("Unknown critical exception in automatic fitting"));
|
|
|
emit logMessageGenerated(tr("CRITICAL ERROR: Unknown exception in PSO main loop"));
|
|
|
closeTraceFile();
|
|
|
cleanupTemporaryDirectory();
|
|
|
m_isRunning = false;
|
|
|
emit fittingFinished(false, m_lastError);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool finalFullSolverSucceeded = true;
|
|
|
bool finalFullSolverExecuted = false;
|
|
|
|
|
|
// 应用最终参数
|
|
|
if(!m_globalBestPosition.isEmpty()) {
|
|
|
try {
|
|
|
emit logMessageGenerated(tr("Applying optimized parameters to model..."));
|
|
|
applyParametersToDataManager(m_globalBestPosition);
|
|
|
|
|
|
// 即使用户此时停止、不再执行最终完整计算,也要把 PEBI 缓存恢复为
|
|
|
// 最终已接受的裂缝参数,避免缓存仍停留在最后一个被拒绝的候选值。
|
|
|
const bool fractureGridParameterSelected =
|
|
|
(m_parameterSelected.size() > 8 && m_parameterSelected[8]) ||
|
|
|
(m_parameterSelected.size() > 9 && m_parameterSelected[9]);
|
|
|
if(fractureGridParameterSelected) {
|
|
|
nmCalculationPebiGrid* pebiGrid = nmCalculationPebiGrid::getInstance();
|
|
|
if(!pebiGrid || !pebiGrid->generateOutputPara()) {
|
|
|
throw std::runtime_error("Failed to refresh final fracture parameters");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(m_shouldStop) {
|
|
|
// 手动停止优先保持快速返回,仅写回已确认的最优参数。
|
|
|
emit logMessageGenerated(tr("Final full-field calculation skipped after user stop"));
|
|
|
} else {
|
|
|
// 粒子阶段只保留目标井临时曲线。正常结束后用最优参数完整计算一次,
|
|
|
// 将全部井曲线和网格压力场写回项目,该次不计入 PSO 粒子评价数。
|
|
|
emit logMessageGenerated(tr("Running final full-field calculation with optimized parameters..."));
|
|
|
finalFullSolverExecuted = true;
|
|
|
finalFullSolverSucceeded = runFinalFullSolver();
|
|
|
|
|
|
if(finalFullSolverSucceeded) {
|
|
|
emit logMessageGenerated(tr("Final full-field calculation completed successfully"));
|
|
|
} else if(m_shouldStop) {
|
|
|
finalFullSolverExecuted = false;
|
|
|
finalFullSolverSucceeded = true;
|
|
|
emit logMessageGenerated(tr("Final full-field calculation stopped by user"));
|
|
|
} else {
|
|
|
emit logMessageGenerated(tr("ERROR: Final full-field calculation failed"));
|
|
|
m_lastError = tr("Optimized parameters were found, but the final full-field calculation failed");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
saveOptimizationResult();
|
|
|
|
|
|
// 输出最终优化结果
|
|
|
emit logMessageGenerated(tr("=== Optimization Results ==="));
|
|
|
emit logMessageGenerated(tr("Final error: %1").arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Total iterations: %1").arg(m_currentIteration + 1)); // 显示实际完成的迭代数
|
|
|
emit logMessageGenerated(tr("Total evaluations: %1 (successful: %2)")
|
|
|
.arg(m_totalEvaluations).arg(m_successfulEvaluations));
|
|
|
|
|
|
// 输出最优参数值
|
|
|
QString finalParams = tr("Optimized parameters: ");
|
|
|
|
|
|
for(int i = 0; i < m_globalBestPosition.size(); ++i) {
|
|
|
finalParams += QString("[%1]=%2 ").arg(i).arg(m_globalBestPosition[i], 0, 'f', 6);
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(finalParams);
|
|
|
|
|
|
if(finalFullSolverExecuted && finalFullSolverSucceeded) {
|
|
|
emit logMessageGenerated(tr("Parameters and full-field results applied successfully to data manager"));
|
|
|
} else if(!finalFullSolverExecuted) {
|
|
|
emit logMessageGenerated(tr("Optimized parameters applied to data manager"));
|
|
|
}
|
|
|
} catch(const std::exception& e) {
|
|
|
finalFullSolverSucceeded = false;
|
|
|
emit logMessageGenerated(tr("ERROR: Failed to apply final parameters: %1").arg(e.what()));
|
|
|
m_lastError = QString("Failed to apply final parameters: %1").arg(e.what());
|
|
|
} catch(...) {
|
|
|
finalFullSolverSucceeded = false;
|
|
|
emit logMessageGenerated(tr("ERROR: Unknown error applying final parameters"));
|
|
|
m_lastError = "Failed to apply final parameters due to unknown error";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
m_isRunning = false;
|
|
|
|
|
|
// 判断系统确定最终结果
|
|
|
bool success;
|
|
|
QString message;
|
|
|
|
|
|
if(finalReason == PSO_TARGET_ACHIEVED) {
|
|
|
success = true;
|
|
|
message = QString(tr("Target achieved. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION SUCCESSFUL ===")
|
|
|
: tr("=== AUTOMATIC FITTING SUCCESSFUL ==="));
|
|
|
} else if(finalReason == PSO_TRUE_CONVERGENCE) {
|
|
|
success = true;
|
|
|
message = useParticleSwarm
|
|
|
? QString(tr("PSO optimization converged to stable solution. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1)
|
|
|
: QString(tr("Automatic fitting converged to a stable solution. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION CONVERGED ===")
|
|
|
: tr("=== AUTOMATIC FITTING CONVERGED ==="));
|
|
|
} else if(finalReason == PSO_LOCAL_OPTIMUM) {
|
|
|
success = true;
|
|
|
message = useParticleSwarm
|
|
|
? QString(tr("PSO optimization trapped in local optimum. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1)
|
|
|
: QString(tr("Automatic fitting reached a local optimum. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION - LOCAL OPTIMUM ===")
|
|
|
: tr("=== AUTOMATIC FITTING - LOCAL OPTIMUM ==="));
|
|
|
} else if(finalReason == PSO_MAX_ITERATIONS) {
|
|
|
success = true;
|
|
|
message = QString(tr("Max iterations reached. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION - MAX ITERATIONS ===")
|
|
|
: tr("=== AUTOMATIC FITTING - MAX ITERATIONS ==="));
|
|
|
} else if(finalReason == PSO_USER_STOPPED) {
|
|
|
success = true;
|
|
|
message = QString(tr("Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION STOPPED BY USER ===")
|
|
|
: tr("=== AUTOMATIC FITTING STOPPED BY USER ==="));
|
|
|
} else if(finalReason == PSO_CONSECUTIVE_FAILURES) {
|
|
|
success = false;
|
|
|
message = useParticleSwarm
|
|
|
? QString(tr("PSO optimization failed due to consecutive failures. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1)
|
|
|
: QString(tr("Automatic fitting failed due to consecutive failures. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION FAILED ===")
|
|
|
: tr("=== AUTOMATIC FITTING FAILED ==="));
|
|
|
} else {
|
|
|
success = false;
|
|
|
message = useParticleSwarm
|
|
|
? QString(tr("PSO optimization ended unexpectedly. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1)
|
|
|
: QString(tr("Automatic fitting ended unexpectedly. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_currentIteration + 1);
|
|
|
emit logMessageGenerated(useParticleSwarm
|
|
|
? tr("=== PSO OPTIMIZATION - UNKNOWN END ===")
|
|
|
: tr("=== AUTOMATIC FITTING - UNKNOWN END ==="));
|
|
|
}
|
|
|
|
|
|
if(!finalFullSolverSucceeded) {
|
|
|
success = false;
|
|
|
message = m_lastError;
|
|
|
}
|
|
|
|
|
|
emitRunSummary(success, finalReason);
|
|
|
|
|
|
// 先发送最终进度更新,确保进度条达到100%
|
|
|
emit progressUpdated(m_maxIterations, m_globalBestFitness);
|
|
|
|
|
|
// 确保界面更新完成
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
// 短暂延迟,让进度条动画完成
|
|
|
msleep(200);
|
|
|
|
|
|
// 再次确保界面更新
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
// 现在才发送完成信号,这会触发消息框
|
|
|
closeTraceFile();
|
|
|
emit fittingFinished(success, message);
|
|
|
|
|
|
cleanupTemporaryDirectory();
|
|
|
|
|
|
return success;
|
|
|
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::extractUserInitialValues()
|
|
|
{
|
|
|
// 从当前项目模型读取用户已有初始参数。
|
|
|
// 只提取用户勾选的参数,并按 m_enabledParamIndices 的顺序写入 m_initialValues。
|
|
|
// 这些值用于:
|
|
|
// 1. 初始解真实评价;
|
|
|
// 2. 粒子群引导初始化;
|
|
|
// 3. 最终精英保护。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
nmDataReservoir reservoirData = dataManager->getReservoirDataCopy();
|
|
|
//QVector<nmDataWellBase*> wells = dataManager->getWellDataList();
|
|
|
|
|
|
nmDataWellBase* pTargetWell = dataManager->findWellByName(m_targetWellName);
|
|
|
|
|
|
m_initialValues.clear();
|
|
|
|
|
|
// 按照启用参数的顺序提取初始值。井参数来自目标井,储层参数来自 reservoirData。
|
|
|
for(int i = 0; i < m_enabledParamIndices.size(); ++i) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
double initialValue = 0.0;
|
|
|
|
|
|
switch(paramIndex) {
|
|
|
case 0: // 渗透率
|
|
|
initialValue = reservoirData.getPermeability().getValue().toDouble();
|
|
|
break;
|
|
|
|
|
|
case 1: // 表皮系数
|
|
|
if(pTargetWell) {
|
|
|
initialValue = pTargetWell->getPerforation(0)->getSkin().getValue().toDouble();
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2: // 井筒储集系数
|
|
|
if(pTargetWell) {
|
|
|
initialValue = pTargetWell->getWellboreStorage().getValue().toDouble();
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3: // 孔隙度
|
|
|
initialValue = reservoirData.getPorosity().getValue().toDouble();
|
|
|
break;
|
|
|
|
|
|
case 4: // 储层厚度
|
|
|
initialValue = reservoirData.getThickness().getValue().toDouble();
|
|
|
break;
|
|
|
|
|
|
case 5: // 综合压缩系数
|
|
|
initialValue = reservoirData.getCt().getValue().toDouble();
|
|
|
break;
|
|
|
|
|
|
case 6: // 岩石压缩系数
|
|
|
initialValue = reservoirData.getCf().getValue().toDouble();
|
|
|
break;
|
|
|
|
|
|
case 7: // 初始含水饱和度
|
|
|
initialValue = reservoirData.getSwi().getValue().toDouble();
|
|
|
break;
|
|
|
|
|
|
case 8: // 裂缝导流能力
|
|
|
if(pTargetWell && pTargetWell->getWellType() == NM_WELL_MODEL::Vertical_Fractured_Well) {
|
|
|
nmDataVerticalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataVerticalFracturedWell*>(pTargetWell);
|
|
|
if(fracturedWell) {
|
|
|
initialValue = fracturedWell->getDfc().getValue().toDouble();
|
|
|
}
|
|
|
} else if(pTargetWell && pTargetWell->getWellType() == NM_WELL_MODEL::Horizontal_Fractured_Well) {
|
|
|
nmDataHorizontalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataHorizontalFracturedWell*>(pTargetWell);
|
|
|
if(fracturedWell) {
|
|
|
initialValue = fracturedWell->getDfc().getValue().toDouble();
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case 9: // 裂缝半长
|
|
|
if(pTargetWell && pTargetWell->getWellType() == NM_WELL_MODEL::Vertical_Fractured_Well) {
|
|
|
nmDataVerticalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataVerticalFracturedWell*>(pTargetWell);
|
|
|
if(fracturedWell) {
|
|
|
initialValue = fracturedWell->getFractureHalfLength().getValue().toDouble();
|
|
|
}
|
|
|
} else if(pTargetWell && pTargetWell->getWellType() == NM_WELL_MODEL::Horizontal_Fractured_Well) {
|
|
|
nmDataHorizontalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataHorizontalFracturedWell*>(pTargetWell);
|
|
|
if(fracturedWell) {
|
|
|
initialValue = fracturedWell->getFractureHalfLength().getValue().toDouble();
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
m_initialValues.append(initialValue);
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Extracted %1 user initial values").arg(m_initialValues.size()));
|
|
|
|
|
|
for(int i = 0; i < m_initialValues.size(); ++i) {
|
|
|
DEBUG_OUT(QString(" Initial[%1] = %2").arg(i).arg(m_initialValues[i], 0, 'e', 3));
|
|
|
}
|
|
|
|
|
|
// 验证初始值
|
|
|
if(!validateInitialValues()) {
|
|
|
DEBUG_OUT("Warning: Some initial values are outside parameter bounds");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::initializeSwarm()
|
|
|
{
|
|
|
// 初始化粒子群。
|
|
|
// 若用户已有初始模型有效,则一部分粒子围绕初始值做局部搜索;
|
|
|
// 另一部分粒子在全局上下界内随机分布,保留跳出局部区域的能力。
|
|
|
int dimensions = getEnabledParameterCount();
|
|
|
|
|
|
if(dimensions == 0) return;
|
|
|
|
|
|
m_swarm.resize(m_swarmSize);
|
|
|
m_globalBestPosition.resize(dimensions);
|
|
|
|
|
|
bool hasValidInitials = !m_initialValues.isEmpty() && m_initialValues.size() >= dimensions;
|
|
|
|
|
|
// 引导搜索比例。guided 粒子围绕用户初始值,非 guided 粒子做全局随机搜索。
|
|
|
//int guidedCount = hasValidInitials ? qMax(2, (m_swarmSize * 3) / 4) : qMax(1, m_swarmSize / 3);
|
|
|
int guidedCount = hasValidInitials ? qMax(2, m_swarmSize / 2) : qMax(1, m_swarmSize / 3);
|
|
|
|
|
|
DEBUG_OUT(QString("Enhanced swarm initialization: %1 particles, %2 guided, %3 random")
|
|
|
.arg(m_swarmSize).arg(guidedCount).arg(m_swarmSize - guidedCount));
|
|
|
|
|
|
for(int i = 0; i < m_swarmSize; ++i) {
|
|
|
AutoFitParticle& particle = m_swarm[i];
|
|
|
particle.position.resize(dimensions);
|
|
|
particle.velocity.resize(dimensions);
|
|
|
particle.bestPosition.resize(dimensions);
|
|
|
particle.guideBestPosition.resize(dimensions);
|
|
|
particle.currentObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
particle.bestObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
particle.bestFitness = 1e10;
|
|
|
particle.guideBestObjective = 1e10;
|
|
|
particle.guideBestFromSurrogate = false;
|
|
|
particle.pbestRelativeImprovementThisIteration = 0.0;
|
|
|
particle.fitness = 1e10;
|
|
|
particle.evaluatedThisIteration = false;
|
|
|
particle.lastEvaluationSuccess = false;
|
|
|
particle.lastEvaluationElapsedMs = -1;
|
|
|
particle.surrogateObjective = 1e10;
|
|
|
particle.selectedForSolver = true;
|
|
|
particle.selectedByAudit = false;
|
|
|
particle.screeningDecision = "full_solver";
|
|
|
|
|
|
for(int j = 0; j < dimensions; ++j) {
|
|
|
int paramIndex = m_enabledParamIndices[j];
|
|
|
double range = m_parameterUpper[paramIndex] - m_parameterLower[paramIndex];
|
|
|
|
|
|
if(i == 0 && hasValidInitials) {
|
|
|
// 第一个粒子:完全使用用户初始值,确保初始模型本身参与粒子群。
|
|
|
particle.position[j] = m_initialValues[j];
|
|
|
|
|
|
} else if(i < guidedCount && hasValidInitials) {
|
|
|
// 引导粒子采用分层搜索半径:
|
|
|
// 近邻精细搜索 + 中等扰动 + 较大扰动,覆盖初始模型周围不同尺度。
|
|
|
double searchRadius;
|
|
|
|
|
|
if(i <= guidedCount / 3) {
|
|
|
searchRadius = range * 0.03; // 3%范围内精细搜索
|
|
|
} else if(i <= guidedCount * 2 / 3) {
|
|
|
searchRadius = range * 0.08; // 8%范围内搜索
|
|
|
} else {
|
|
|
searchRadius = range * 0.15; // 15%范围内搜索
|
|
|
}
|
|
|
|
|
|
double offset = (random01() - 0.5) * searchRadius;
|
|
|
particle.position[j] = m_initialValues[j] + offset;
|
|
|
|
|
|
} else {
|
|
|
// 少数粒子进行全局搜索,防止所有粒子都被初始模型附近的局部最优限制。
|
|
|
particle.position[j] = m_parameterLower[paramIndex] + random01() * range;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 边界检查
|
|
|
clampToLimits(particle.position);
|
|
|
|
|
|
// 速度初始化。引导粒子速度更小,避免刚开始就离开初始模型邻域;
|
|
|
// 全局粒子速度稍大,便于探索更宽的搜索空间。
|
|
|
for(int j = 0; j < dimensions; ++j) {
|
|
|
int paramIndex = m_enabledParamIndices[j];
|
|
|
double range = m_parameterUpper[paramIndex] - m_parameterLower[paramIndex];
|
|
|
|
|
|
double velocityFactor;
|
|
|
|
|
|
if(i < guidedCount) {
|
|
|
velocityFactor = VELOCITY_LIMIT_FACTOR * 0.15; // 更小的速度
|
|
|
} else {
|
|
|
velocityFactor = VELOCITY_LIMIT_FACTOR * 0.8; // 适中速度
|
|
|
}
|
|
|
|
|
|
particle.velocity[j] = (random01() - 0.5) * range * velocityFactor;
|
|
|
}
|
|
|
|
|
|
particle.bestPosition = particle.position;
|
|
|
particle.guideBestPosition = particle.position;
|
|
|
|
|
|
// 第一个粒子可能直接复用用户初始解,因此同步保存初始解的误差分解,
|
|
|
// 后续误差引导只能使用真实求解器确认过的 breakdown。
|
|
|
if(i == 0 && m_hasValidUserSolution) {
|
|
|
particle.currentObjectiveBreakdown = m_userInitialObjectiveBreakdown;
|
|
|
particle.bestObjectiveBreakdown = m_userInitialObjectiveBreakdown;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 信赖域搜索统一在 [0, 1] 内部坐标工作。正值参数使用对数坐标,使内部相同步长
|
|
|
// 表示近似相同的相对变化,避免 k、C、Ct、Cf 等跨数量级参数被线性尺度支配;
|
|
|
// skin 可为负数、Swi 的物理意义是线性比例,因此二者保持有界线性坐标。
|
|
|
static bool useTrustRegionLogScale(int parameterIndex, double lower, double upper)
|
|
|
{
|
|
|
return parameterIndex != 1 && parameterIndex != 7 &&
|
|
|
lower > 0.0 && upper > lower;
|
|
|
}
|
|
|
|
|
|
static double toTrustRegionCoordinate(double value,
|
|
|
int parameterIndex,
|
|
|
double lower,
|
|
|
double upper)
|
|
|
{
|
|
|
// 所有进入优化器的物理值先投影到用户上下界,再转换成无量纲坐标。
|
|
|
// 这样有限差分步长、信赖半径和参数间相关性可以在统一尺度上比较。
|
|
|
value = qMax(lower, qMin(upper, value));
|
|
|
|
|
|
if(useTrustRegionLogScale(parameterIndex, lower, upper)) {
|
|
|
return (qLn(value) - qLn(lower)) / (qLn(upper) - qLn(lower));
|
|
|
}
|
|
|
|
|
|
return upper > lower ? (value - lower) / (upper - lower) : 0.0;
|
|
|
}
|
|
|
|
|
|
static double fromTrustRegionCoordinate(double coordinate,
|
|
|
int parameterIndex,
|
|
|
double lower,
|
|
|
double upper)
|
|
|
{
|
|
|
// 候选内部坐标先限制在 [0,1],再执行上述映射的逆变换,保证写回
|
|
|
// DataManager 的参数始终位于用户设置的物理范围内。
|
|
|
coordinate = qMax(0.0, qMin(1.0, coordinate));
|
|
|
|
|
|
if(useTrustRegionLogScale(parameterIndex, lower, upper)) {
|
|
|
return qExp(qLn(lower) + coordinate * (qLn(upper) - qLn(lower)));
|
|
|
}
|
|
|
|
|
|
return lower + coordinate * (upper - lower);
|
|
|
}
|
|
|
|
|
|
enum TrustRegionErrorComponent
|
|
|
{
|
|
|
TRUST_REGION_VERTICAL_COMPONENT = 0,
|
|
|
TRUST_REGION_HORIZONTAL_COMPONENT,
|
|
|
TRUST_REGION_SHAPE_COMPONENT,
|
|
|
TRUST_REGION_TOTAL_COMPONENT
|
|
|
};
|
|
|
|
|
|
// 一次真实求解的完整快照。除了参数和总误差,还保存内部坐标、诊断分量和
|
|
|
// 双对数曲线,因此拒绝候选后可以完整恢复上一个已接受工作点。
|
|
|
struct TrustRegionEvaluation
|
|
|
{
|
|
|
QVector<double> parameters;
|
|
|
QVector<double> coordinates;
|
|
|
AutoFitObjectiveBreakdown breakdown;
|
|
|
QVector<QVector<double> > curve;
|
|
|
double fitness;
|
|
|
int elapsedMs;
|
|
|
bool valid;
|
|
|
|
|
|
TrustRegionEvaluation()
|
|
|
: fitness(1.0e10)
|
|
|
, elapsedMs(-1)
|
|
|
, valid(false)
|
|
|
{}
|
|
|
};
|
|
|
|
|
|
// LM 只使用固定长度、全部有限的普通残差。代理路径不会进入本搜索器。
|
|
|
static bool trustRegionResidualsValid(
|
|
|
const AutoFitObjectiveBreakdown& breakdown)
|
|
|
{
|
|
|
// 损失函数固定使用 80 个压力点和 80 个导数点。严格校验长度,避免
|
|
|
// Jacobian 沿用旧维度后访问另一候选的短残差向量。
|
|
|
if(!breakdown.valid || breakdown.residualVector.size() != 160) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < breakdown.residualVector.size(); ++i) {
|
|
|
if(!isFiniteNumber(breakdown.residualVector[i])) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 计算向量二范数的平方,避免在只比较能量或计算正规方程时反复开方。
|
|
|
static double trustRegionSquaredNorm(const QVector<double>& values)
|
|
|
{
|
|
|
double sum = 0.0;
|
|
|
for(int i = 0; i < values.size(); ++i) {
|
|
|
sum += values[i] * values[i];
|
|
|
}
|
|
|
return sum;
|
|
|
}
|
|
|
|
|
|
// 计算同维向量内积;维度不一致表示局部模型无效,返回零让调用方放弃修正。
|
|
|
static double trustRegionDotProduct(const QVector<double>& left,
|
|
|
const QVector<double>& right)
|
|
|
{
|
|
|
if(left.size() != right.size()) {
|
|
|
return 0.0;
|
|
|
}
|
|
|
|
|
|
double sum = 0.0;
|
|
|
for(int i = 0; i < left.size(); ++i) {
|
|
|
sum += left[i] * right[i];
|
|
|
}
|
|
|
return sum;
|
|
|
}
|
|
|
|
|
|
// trace 和运行日志使用稳定的英文标识,便于现有离线脚本继续按字段筛选。
|
|
|
static QString trustRegionComponentName(int component)
|
|
|
{
|
|
|
if(component == TRUST_REGION_VERTICAL_COMPONENT) {
|
|
|
return "vertical";
|
|
|
}
|
|
|
if(component == TRUST_REGION_HORIZONTAL_COMPONENT) {
|
|
|
return "horizontal";
|
|
|
}
|
|
|
if(component == TRUST_REGION_SHAPE_COMPONENT) {
|
|
|
return "shape";
|
|
|
}
|
|
|
return "total";
|
|
|
}
|
|
|
|
|
|
// 三类损失量纲一致,直接选择当前最大的可靠分量;都很小时退回总残差梯度。
|
|
|
static int trustRegionDominantComponent(
|
|
|
const AutoFitObjectiveBreakdown& breakdown,
|
|
|
double diagnosisThreshold)
|
|
|
{
|
|
|
int component = TRUST_REGION_TOTAL_COMPONENT;
|
|
|
double largestLoss = diagnosisThreshold;
|
|
|
|
|
|
if(breakdown.verticalReliable &&
|
|
|
isFiniteNumber(breakdown.verticalLoss) &&
|
|
|
breakdown.verticalLoss > largestLoss) {
|
|
|
component = TRUST_REGION_VERTICAL_COMPONENT;
|
|
|
largestLoss = breakdown.verticalLoss;
|
|
|
}
|
|
|
if(breakdown.horizontalReliable &&
|
|
|
!breakdown.registrationAmbiguous &&
|
|
|
isFiniteNumber(breakdown.horizontalLoss) &&
|
|
|
breakdown.horizontalLoss > largestLoss) {
|
|
|
component = TRUST_REGION_HORIZONTAL_COMPONENT;
|
|
|
largestLoss = breakdown.horizontalLoss;
|
|
|
}
|
|
|
if(isFiniteNumber(breakdown.shapeLoss) &&
|
|
|
breakdown.shapeLoss > largestLoss) {
|
|
|
component = TRUST_REGION_SHAPE_COMPONENT;
|
|
|
}
|
|
|
|
|
|
return component;
|
|
|
}
|
|
|
|
|
|
// 求解选中参数对应的阻尼正规方程。上下和左右诊断量保留方向;形状没有
|
|
|
// 天然正负,因此使用 shapeLoss 对参数的局部导数。参数最多八维,使用带
|
|
|
// 部分主元的高斯消元即可处理该小矩阵,并在主元退化时明确返回失败。
|
|
|
static bool solveTrustRegionLinearSystem(
|
|
|
QVector<QVector<double> > matrix,
|
|
|
QVector<double> rightHandSide,
|
|
|
QVector<double>* solution)
|
|
|
{
|
|
|
if(!solution || matrix.isEmpty() ||
|
|
|
matrix.size() != rightHandSide.size()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
const int size = matrix.size();
|
|
|
for(int i = 0; i < size; ++i) {
|
|
|
if(matrix[i].size() != size) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for(int column = 0; column < size; ++column) {
|
|
|
int pivotRow = column;
|
|
|
double pivotMagnitude = qAbs(matrix[column][column]);
|
|
|
for(int row = column + 1; row < size; ++row) {
|
|
|
double magnitude = qAbs(matrix[row][column]);
|
|
|
if(magnitude > pivotMagnitude) {
|
|
|
pivotMagnitude = magnitude;
|
|
|
pivotRow = row;
|
|
|
}
|
|
|
}
|
|
|
if(pivotMagnitude <= 1.0e-14) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(pivotRow != column) {
|
|
|
qSwap(matrix[pivotRow], matrix[column]);
|
|
|
qSwap(rightHandSide[pivotRow], rightHandSide[column]);
|
|
|
}
|
|
|
|
|
|
for(int row = column + 1; row < size; ++row) {
|
|
|
double factor = matrix[row][column] /
|
|
|
matrix[column][column];
|
|
|
matrix[row][column] = 0.0;
|
|
|
for(int nextColumn = column + 1;
|
|
|
nextColumn < size; ++nextColumn) {
|
|
|
matrix[row][nextColumn] -=
|
|
|
factor * matrix[column][nextColumn];
|
|
|
}
|
|
|
rightHandSide[row] -= factor * rightHandSide[column];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
solution->fill(0.0, size);
|
|
|
for(int row = size - 1; row >= 0; --row) {
|
|
|
double value = rightHandSide[row];
|
|
|
for(int column = row + 1; column < size; ++column) {
|
|
|
value -= matrix[row][column] * (*solution)[column];
|
|
|
}
|
|
|
double pivot = matrix[row][row];
|
|
|
if(qAbs(pivot) <= 1.0e-14) {
|
|
|
return false;
|
|
|
}
|
|
|
(*solution)[row] = value / pivot;
|
|
|
if(!isFiniteNumber((*solution)[row])) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 计算两个 Jacobian 列向量的绝对余弦相似度。接近 1 表示两个参数在当前
|
|
|
// 工作点对曲线的影响几乎相同,联合调整容易产生不可辨识方向。
|
|
|
static double trustRegionJacobianColumnCorrelation(
|
|
|
const QVector<QVector<double> >& jacobian,
|
|
|
int leftColumn,
|
|
|
int rightColumn)
|
|
|
{
|
|
|
double product = 0.0;
|
|
|
double leftNorm = 0.0;
|
|
|
double rightNorm = 0.0;
|
|
|
for(int row = 0; row < jacobian.size(); ++row) {
|
|
|
if(leftColumn >= jacobian[row].size() ||
|
|
|
rightColumn >= jacobian[row].size()) {
|
|
|
return 1.0;
|
|
|
}
|
|
|
double left = jacobian[row][leftColumn];
|
|
|
double right = jacobian[row][rightColumn];
|
|
|
product += left * right;
|
|
|
leftNorm += left * left;
|
|
|
rightNorm += right * right;
|
|
|
}
|
|
|
|
|
|
if(leftNorm <= 1.0e-20 || rightNorm <= 1.0e-20) {
|
|
|
return 0.0;
|
|
|
}
|
|
|
return qAbs(product) / qSqrt(leftNorm * rightNorm);
|
|
|
}
|
|
|
|
|
|
// 每次接受一个真实候选后,使用满足最新割线条件的秩一修正更新完整残差
|
|
|
// Jacobian。这样模型吸收了刚得到的真实变化,又不必立即逐参数重新试算。
|
|
|
static void updateTrustRegionJacobian(
|
|
|
QVector<QVector<double> >* jacobian,
|
|
|
const QVector<double>& oldResidual,
|
|
|
const QVector<double>& newResidual,
|
|
|
const QVector<double>& coordinateStep)
|
|
|
{
|
|
|
if(!jacobian || jacobian->size() != oldResidual.size() ||
|
|
|
oldResidual.size() != newResidual.size()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
double denominator = trustRegionSquaredNorm(coordinateStep);
|
|
|
if(denominator <= 1.0e-12) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
for(int row = 0; row < jacobian->size(); ++row) {
|
|
|
if((*jacobian)[row].size() != coordinateStep.size()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
double predictedChange = 0.0;
|
|
|
for(int column = 0; column < coordinateStep.size(); ++column) {
|
|
|
predictedChange +=
|
|
|
(*jacobian)[row][column] * coordinateStep[column];
|
|
|
}
|
|
|
double correction =
|
|
|
(newResidual[row] - oldResidual[row] - predictedChange) /
|
|
|
denominator;
|
|
|
for(int column = 0; column < coordinateStep.size(); ++column) {
|
|
|
(*jacobian)[row][column] +=
|
|
|
correction * coordinateStep[column];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 对上下偏差、左右偏差和形状损失的梯度执行同样的割线秩一修正,使诊断
|
|
|
// 选参模型与完整残差 Jacobian 保持在同一个已接受工作点。
|
|
|
static void updateTrustRegionScalarGradient(
|
|
|
QVector<double>* gradient,
|
|
|
double oldValue,
|
|
|
double newValue,
|
|
|
const QVector<double>& coordinateStep)
|
|
|
{
|
|
|
if(!gradient || gradient->size() != coordinateStep.size() ||
|
|
|
!isFiniteNumber(oldValue) || !isFiniteNumber(newValue)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
double denominator = trustRegionSquaredNorm(coordinateStep);
|
|
|
if(denominator <= 1.0e-12) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
double predictedChange = trustRegionDotProduct(
|
|
|
*gradient, coordinateStep);
|
|
|
double correction =
|
|
|
(newValue - oldValue - predictedChange) / denominator;
|
|
|
for(int i = 0; i < gradient->size(); ++i) {
|
|
|
(*gradient)[i] += correction * coordinateStep[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::evaluateTrustRegionPoint(
|
|
|
const QVector<double>& parameters,
|
|
|
double* fitness,
|
|
|
AutoFitObjectiveBreakdown* breakdown,
|
|
|
QVector<QVector<double> >* curve,
|
|
|
int* elapsedMs)
|
|
|
{
|
|
|
if(!fitness || !breakdown || !curve || !elapsedMs || m_shouldStop) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// evaluateFitness() 会写入 DataManager 并调用真实求解器。这里统一统计
|
|
|
// 真实评价次数和耗时,同时严格要求固定残差、诊断结构和结果曲线均有效。
|
|
|
QTime timer;
|
|
|
timer.start();
|
|
|
*fitness = evaluateFitness(parameters);
|
|
|
*elapsedMs = timer.elapsed();
|
|
|
*breakdown = m_lastObjectiveBreakdown;
|
|
|
*curve = m_lastEvaluatedLogLogData;
|
|
|
++m_totalEvaluations;
|
|
|
|
|
|
bool valid = isFiniteNumber(*fitness) && *fitness < 1.0e9 &&
|
|
|
breakdown->valid &&
|
|
|
trustRegionResidualsValid(*breakdown) &&
|
|
|
!curve->isEmpty();
|
|
|
if(valid) {
|
|
|
++m_successfulEvaluations;
|
|
|
}
|
|
|
|
|
|
return valid;
|
|
|
}
|
|
|
|
|
|
StopReasonPSO nmCalculationAutoFitPSO::runTrustRegionFitting()
|
|
|
{
|
|
|
const int dimensions = getEnabledParameterCount();
|
|
|
if(dimensions <= 0 || m_enabledParamIndices.size() != dimensions) {
|
|
|
m_lastError = tr("No valid parameters are available for trust-region fitting");
|
|
|
return PSO_OPTIMIZATION_FAILED;
|
|
|
}
|
|
|
|
|
|
// 真实求解次数比“外层迭代次数”更能反映耗时。预算至少允许完成一次全参数
|
|
|
// 灵敏度和两次候选评价,同时避免连续重建 Jacobian 导致运行时间失控。
|
|
|
const int maximumEvaluations = qMax(
|
|
|
m_totalEvaluations + dimensions + 2,
|
|
|
qMax(20, m_maxIterations * 3));
|
|
|
// 下列步长均位于归一化内部坐标:0.04 表示参数范围的 4%,信赖半径
|
|
|
// 限制一次联合移动的二范数,相关性门槛用于排除响应近乎共线的参数。
|
|
|
const double sensitivityStep = 0.04;
|
|
|
const double minimumCoordinateStep = 1.0e-5;
|
|
|
const double minimumTrustRadius = 2.0e-3;
|
|
|
const double maximumTrustRadius = 0.30;
|
|
|
const double columnCorrelationLimit = 0.995;
|
|
|
const double diagnosisThreshold = 1.0e-5;
|
|
|
// 误差下降至少达到绝对 1e-5 且相对当前有效基准 0.2% 才算有效改善。
|
|
|
// 更小的下降仍保留为最佳解,但不能反复清除停滞状态、延长拟合时间。
|
|
|
const double effectiveRelativeImprovement = 2.0e-3;
|
|
|
const double effectiveAbsoluteImprovement = 1.0e-5;
|
|
|
const int maximumIneffectiveSteps = 3;
|
|
|
|
|
|
// damping 是 LM 阻尼;拒绝或预测失准时增大,真实下降与预测一致时减小。
|
|
|
// 两组累计量控制 Jacobian 重建,避免长期使用已偏离当前工作点的局部模型。
|
|
|
double trustRadius = 0.12;
|
|
|
double damping = 1.0e-2;
|
|
|
int consecutiveRejectedSteps = 0;
|
|
|
int consecutiveSolverFailures = 0;
|
|
|
int acceptedSinceRebuild = 0;
|
|
|
int consecutiveIneffectiveSteps = 0;
|
|
|
double movementSinceRebuild = 0.0;
|
|
|
bool rebuildRequested = true;
|
|
|
bool modelRebuiltAtMinimumRadius = false;
|
|
|
bool stagnationConfirmationRequested = false;
|
|
|
StopReasonPSO stopReason = PSO_MAX_ITERATIONS;
|
|
|
|
|
|
// jacobian 的行对应固定 160 维残差,列对应用户勾选的参数。
|
|
|
// 三个 gradient 单独描述诊断分量对参数的局部变化,只用于本轮选参。
|
|
|
QVector<QVector<double> > jacobian;
|
|
|
QVector<double> verticalGradient(dimensions, 0.0);
|
|
|
QVector<double> horizontalGradient(dimensions, 0.0);
|
|
|
QVector<double> shapeGradient(dimensions, 0.0);
|
|
|
QVector<bool> jacobianColumnValid(dimensions, false);
|
|
|
|
|
|
// 参数向量的顺序始终与 m_enabledParamIndices 一致,不能按完整参数索引
|
|
|
// 直接访问;下面两个转换函数集中维护这层映射关系。
|
|
|
auto coordinatesFromParameters = [&](const QVector<double>& parameters)
|
|
|
-> QVector<double> {
|
|
|
QVector<double> coordinates(dimensions, 0.0);
|
|
|
for(int i = 0; i < dimensions; ++i) {
|
|
|
int parameterIndex = m_enabledParamIndices[i];
|
|
|
coordinates[i] = toTrustRegionCoordinate(
|
|
|
parameters[i], parameterIndex,
|
|
|
m_parameterLower[parameterIndex],
|
|
|
m_parameterUpper[parameterIndex]);
|
|
|
}
|
|
|
return coordinates;
|
|
|
};
|
|
|
|
|
|
auto parametersFromCoordinates = [&](const QVector<double>& coordinates)
|
|
|
-> QVector<double> {
|
|
|
QVector<double> parameters(dimensions, 0.0);
|
|
|
for(int i = 0; i < dimensions; ++i) {
|
|
|
int parameterIndex = m_enabledParamIndices[i];
|
|
|
parameters[i] = fromTrustRegionCoordinate(
|
|
|
coordinates[i], parameterIndex,
|
|
|
m_parameterLower[parameterIndex],
|
|
|
m_parameterUpper[parameterIndex]);
|
|
|
}
|
|
|
return parameters;
|
|
|
};
|
|
|
|
|
|
auto restoreEvaluationState = [&](const TrustRegionEvaluation& evaluation) {
|
|
|
// evaluateFitness() 会把试算参数写入 DataManager。无论候选是否接受,
|
|
|
// 下一次计算前都恢复到唯一的已接受工作点,防止失败试算污染后续求解。
|
|
|
applyParametersToDataManager(evaluation.parameters);
|
|
|
m_lastObjectiveBreakdown = evaluation.breakdown;
|
|
|
m_lastEvaluatedLogLogData = evaluation.curve;
|
|
|
};
|
|
|
|
|
|
// 只有真实总误差更小的工作点才能发布为全局最优;曲线和诊断快照必须
|
|
|
// 与参数同步更新,防止界面显示或最终精英保护使用错配的数据。
|
|
|
auto publishAcceptedPoint = [&](const TrustRegionEvaluation& evaluation) {
|
|
|
m_previousBestFitness = m_globalBestFitness;
|
|
|
m_globalBestPosition = evaluation.parameters;
|
|
|
m_globalBestFitness = evaluation.fitness;
|
|
|
m_globalBestObjectiveBreakdown = evaluation.breakdown;
|
|
|
m_globalBestLogLogData = evaluation.curve;
|
|
|
emit bestCurveUpdated(m_targetLogLogData,
|
|
|
m_globalBestLogLogData,
|
|
|
m_currentIteration + 1,
|
|
|
m_globalBestFitness);
|
|
|
};
|
|
|
|
|
|
auto processPauseAndStop = [&]() -> bool {
|
|
|
while(m_isPaused && !m_shouldStop) {
|
|
|
QApplication::processEvents();
|
|
|
msleep(100);
|
|
|
}
|
|
|
QApplication::processEvents();
|
|
|
return !m_shouldStop;
|
|
|
};
|
|
|
|
|
|
// current 始终代表唯一已接受工作点。优先复用启动阶段已经真实验证的
|
|
|
// 用户初始解,避免在信赖域入口重复调用一次昂贵求解器。
|
|
|
TrustRegionEvaluation current;
|
|
|
if(m_hasValidUserSolution &&
|
|
|
m_globalBestPosition.size() == dimensions &&
|
|
|
trustRegionResidualsValid(m_globalBestObjectiveBreakdown) &&
|
|
|
!m_globalBestLogLogData.isEmpty()) {
|
|
|
current.parameters = m_globalBestPosition;
|
|
|
current.coordinates = coordinatesFromParameters(current.parameters);
|
|
|
current.breakdown = m_globalBestObjectiveBreakdown;
|
|
|
current.curve = m_globalBestLogLogData;
|
|
|
current.fitness = m_globalBestFitness;
|
|
|
current.elapsedMs = 0;
|
|
|
current.valid = true;
|
|
|
} else {
|
|
|
// 用户初始解无效时只做一次确定性的范围中点回退;所有正值参数在对数
|
|
|
// 坐标取中点,避免线性中点过分偏向跨数量级范围的上界。
|
|
|
current.coordinates.fill(0.5, dimensions);
|
|
|
current.parameters = parametersFromCoordinates(current.coordinates);
|
|
|
current.valid = evaluateTrustRegionPoint(
|
|
|
current.parameters,
|
|
|
¤t.fitness,
|
|
|
¤t.breakdown,
|
|
|
¤t.curve,
|
|
|
¤t.elapsedMs);
|
|
|
writeTraceRow(-1, -1,
|
|
|
"trust_region_midpoint",
|
|
|
current.parameters,
|
|
|
current.fitness,
|
|
|
current.valid,
|
|
|
current.elapsedMs,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
current.valid ? "midpoint_valid" : "midpoint_invalid",
|
|
|
QVector<double>(),
|
|
|
1.0e10,
|
|
|
current.valid ? ¤t.breakdown : nullptr);
|
|
|
if(!current.valid) {
|
|
|
m_lastError = tr("The initial solution and parameter-range midpoint are both invalid");
|
|
|
return m_shouldStop
|
|
|
? PSO_USER_STOPPED
|
|
|
: PSO_OPTIMIZATION_FAILED;
|
|
|
}
|
|
|
publishAcceptedPoint(current);
|
|
|
}
|
|
|
|
|
|
restoreEvaluationState(current);
|
|
|
m_convergenceHistory.append(current.fitness);
|
|
|
emit logMessageGenerated(
|
|
|
tr("Trust-region initial error: %1; evaluation budget: %2")
|
|
|
.arg(current.fitness, 0, 'e', 4)
|
|
|
.arg(maximumEvaluations));
|
|
|
|
|
|
// 有效改善始终相对“上一次有效改善后的误差”累计判断,避免一连串微小
|
|
|
// 下降每次都清零计数;累计达到门槛后才开始新的有效改善基准。
|
|
|
double effectiveImprovementBaseline = current.fitness;
|
|
|
auto registerEffectiveImprovement = [&](double fitness) -> bool {
|
|
|
const double requiredImprovement = qMax(
|
|
|
effectiveAbsoluteImprovement,
|
|
|
qAbs(effectiveImprovementBaseline) *
|
|
|
effectiveRelativeImprovement);
|
|
|
const double improvement = effectiveImprovementBaseline - fitness;
|
|
|
if(improvement < requiredImprovement) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
effectiveImprovementBaseline = fitness;
|
|
|
consecutiveIneffectiveSteps = 0;
|
|
|
stagnationConfirmationRequested = false;
|
|
|
return true;
|
|
|
};
|
|
|
|
|
|
// 连续三次没有有效改善时只请求一次灵敏度重建。重建完成后由主循环
|
|
|
// 直接检查累计改善,仍达不到门槛就判定局部收敛,不再继续微小试探。
|
|
|
auto recordIneffectiveStep = [&]() -> bool {
|
|
|
++consecutiveIneffectiveSteps;
|
|
|
if(consecutiveIneffectiveSteps < maximumIneffectiveSteps) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(stagnationConfirmationRequested) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
consecutiveIneffectiveSteps = 0;
|
|
|
stagnationConfirmationRequested = true;
|
|
|
rebuildRequested = true;
|
|
|
emit logMessageGenerated(
|
|
|
tr("No effective improvement for %1 consecutive steps; "
|
|
|
"rebuilding sensitivity model for confirmation")
|
|
|
.arg(maximumIneffectiveSteps));
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
emit logMessageGenerated(
|
|
|
tr("Effective improvement threshold: max(%1, %2% of baseline error); "
|
|
|
"%3 consecutive ineffective steps trigger convergence confirmation")
|
|
|
.arg(effectiveAbsoluteImprovement, 0, 'e', 2)
|
|
|
.arg(effectiveRelativeImprovement * 100.0, 0, 'f', 2)
|
|
|
.arg(maximumIneffectiveSteps));
|
|
|
|
|
|
if(current.fitness < m_targetError) {
|
|
|
return PSO_TARGET_ACHIEVED;
|
|
|
}
|
|
|
|
|
|
// 在同一个真实工作点逐参数做单边差分。首选可用空间更大的方向;只有该方向
|
|
|
// 求解失败时才补算反方向,因此初次建模通常每个参数只增加一次真实求解。
|
|
|
auto rebuildSensitivity = [&]() -> bool {
|
|
|
const TrustRegionEvaluation base = current;
|
|
|
const int residualCount = base.breakdown.residualVector.size();
|
|
|
if(residualCount <= 0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
jacobian = QVector<QVector<double> >(
|
|
|
residualCount, QVector<double>(dimensions, 0.0));
|
|
|
verticalGradient.fill(0.0, dimensions);
|
|
|
horizontalGradient.fill(0.0, dimensions);
|
|
|
shapeGradient.fill(0.0, dimensions);
|
|
|
jacobianColumnValid.fill(false, dimensions);
|
|
|
|
|
|
TrustRegionEvaluation bestProbe;
|
|
|
int bestProbeColumn = -1;
|
|
|
double bestProbeDelta = 0.0;
|
|
|
// 差分步长不超过参数范围的 4%,信赖域收缩后同步减小,但保留 0.5%
|
|
|
// 下限,避免步长太小使求解器数值噪声淹没真实灵敏度。
|
|
|
const double finiteDifferenceStep = qMin(
|
|
|
sensitivityStep,
|
|
|
qMax(5.0e-3, trustRadius * 0.5));
|
|
|
|
|
|
for(int column = 0;
|
|
|
column < dimensions &&
|
|
|
m_totalEvaluations < maximumEvaluations &&
|
|
|
processPauseAndStop();
|
|
|
++column) {
|
|
|
// 单边差分优先选择离边界空间更大的方向;首方向求解无效时才反向
|
|
|
// 补算,因此正常情况下每个参数只消耗一次真实求解。
|
|
|
double positiveRoom = 1.0 - base.coordinates[column];
|
|
|
double negativeRoom = base.coordinates[column];
|
|
|
double preferredSign = positiveRoom >= negativeRoom ? 1.0 : -1.0;
|
|
|
bool columnBuilt = false;
|
|
|
|
|
|
for(int directionAttempt = 0;
|
|
|
directionAttempt < 2 &&
|
|
|
!columnBuilt &&
|
|
|
m_totalEvaluations < maximumEvaluations;
|
|
|
++directionAttempt) {
|
|
|
double direction = directionAttempt == 0
|
|
|
? preferredSign : -preferredSign;
|
|
|
double availableRoom = direction > 0.0
|
|
|
? positiveRoom : negativeRoom;
|
|
|
double deltaMagnitude = qMin(
|
|
|
finiteDifferenceStep, availableRoom);
|
|
|
if(deltaMagnitude < minimumCoordinateStep) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
TrustRegionEvaluation probe;
|
|
|
probe.coordinates = base.coordinates;
|
|
|
probe.coordinates[column] += direction * deltaMagnitude;
|
|
|
probe.parameters = parametersFromCoordinates(probe.coordinates);
|
|
|
probe.valid = evaluateTrustRegionPoint(
|
|
|
probe.parameters,
|
|
|
&probe.fitness,
|
|
|
&probe.breakdown,
|
|
|
&probe.curve,
|
|
|
&probe.elapsedMs);
|
|
|
|
|
|
QString decision = probe.valid
|
|
|
? "sensitivity_valid"
|
|
|
: (directionAttempt == 0
|
|
|
? "sensitivity_retry_opposite"
|
|
|
: "sensitivity_invalid");
|
|
|
writeTraceRow(m_currentIteration,
|
|
|
column,
|
|
|
"trust_region_sensitivity",
|
|
|
probe.parameters,
|
|
|
probe.fitness,
|
|
|
probe.valid,
|
|
|
probe.elapsedMs,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
decision,
|
|
|
base.parameters,
|
|
|
base.fitness,
|
|
|
probe.valid ? &probe.breakdown : nullptr);
|
|
|
|
|
|
if(!probe.valid) {
|
|
|
restoreEvaluationState(base);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double delta = probe.coordinates[column] -
|
|
|
base.coordinates[column];
|
|
|
if(qAbs(delta) < minimumCoordinateStep ||
|
|
|
probe.breakdown.residualVector.size() != residualCount) {
|
|
|
restoreEvaluationState(base);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 第 column 列是固定残差向量相对内部参数坐标的有限差分:
|
|
|
// J[:,column] = (r_probe-r_base)/delta。
|
|
|
for(int row = 0; row < residualCount; ++row) {
|
|
|
jacobian[row][column] =
|
|
|
(probe.breakdown.residualVector[row] -
|
|
|
base.breakdown.residualVector[row]) / delta;
|
|
|
}
|
|
|
|
|
|
// 有符号诊断量只有在基点和试算点都可靠时才能计算方向梯度;
|
|
|
// shapeLoss 无方向可靠性标志,始终记录其局部变化率。
|
|
|
if(base.breakdown.verticalReliable &&
|
|
|
probe.breakdown.verticalReliable &&
|
|
|
!base.breakdown.registrationAmbiguous &&
|
|
|
!probe.breakdown.registrationAmbiguous) {
|
|
|
verticalGradient[column] =
|
|
|
(probe.breakdown.verticalCommonBias -
|
|
|
base.breakdown.verticalCommonBias) / delta;
|
|
|
}
|
|
|
if(base.breakdown.horizontalReliable &&
|
|
|
probe.breakdown.horizontalReliable &&
|
|
|
!base.breakdown.registrationAmbiguous &&
|
|
|
!probe.breakdown.registrationAmbiguous) {
|
|
|
horizontalGradient[column] =
|
|
|
(probe.breakdown.horizontalPhysicalShift -
|
|
|
base.breakdown.horizontalPhysicalShift) / delta;
|
|
|
}
|
|
|
shapeGradient[column] =
|
|
|
(probe.breakdown.shapeLoss -
|
|
|
base.breakdown.shapeLoss) / delta;
|
|
|
jacobianColumnValid[column] = true;
|
|
|
columnBuilt = true;
|
|
|
|
|
|
if(probe.fitness < base.fitness &&
|
|
|
(!bestProbe.valid ||
|
|
|
probe.fitness < bestProbe.fitness)) {
|
|
|
bestProbe = probe;
|
|
|
bestProbeColumn = column;
|
|
|
bestProbeDelta = delta;
|
|
|
}
|
|
|
restoreEvaluationState(base);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int validColumnCount = 0;
|
|
|
for(int i = 0; i < jacobianColumnValid.size(); ++i) {
|
|
|
if(jacobianColumnValid[i]) {
|
|
|
++validColumnCount;
|
|
|
}
|
|
|
}
|
|
|
if(validColumnCount == 0 || m_shouldStop) {
|
|
|
restoreEvaluationState(base);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 灵敏度试算本身若找到更优真实解也应保留。所有列先基于同一个 base
|
|
|
// 建完,再用该已知割线把 Jacobian 平移到新工作点,避免边算边移动基点。
|
|
|
if(bestProbe.valid && bestProbeColumn >= 0) {
|
|
|
QVector<double> acceptedStep(dimensions, 0.0);
|
|
|
acceptedStep[bestProbeColumn] = bestProbeDelta;
|
|
|
updateTrustRegionJacobian(
|
|
|
&jacobian,
|
|
|
base.breakdown.residualVector,
|
|
|
bestProbe.breakdown.residualVector,
|
|
|
acceptedStep);
|
|
|
if(base.breakdown.verticalReliable &&
|
|
|
bestProbe.breakdown.verticalReliable) {
|
|
|
updateTrustRegionScalarGradient(
|
|
|
&verticalGradient,
|
|
|
base.breakdown.verticalCommonBias,
|
|
|
bestProbe.breakdown.verticalCommonBias,
|
|
|
acceptedStep);
|
|
|
}
|
|
|
if(base.breakdown.horizontalReliable &&
|
|
|
bestProbe.breakdown.horizontalReliable) {
|
|
|
updateTrustRegionScalarGradient(
|
|
|
&horizontalGradient,
|
|
|
base.breakdown.horizontalPhysicalShift,
|
|
|
bestProbe.breakdown.horizontalPhysicalShift,
|
|
|
acceptedStep);
|
|
|
}
|
|
|
updateTrustRegionScalarGradient(
|
|
|
&shapeGradient,
|
|
|
base.breakdown.shapeLoss,
|
|
|
bestProbe.breakdown.shapeLoss,
|
|
|
acceptedStep);
|
|
|
|
|
|
current = bestProbe;
|
|
|
publishAcceptedPoint(current);
|
|
|
restoreEvaluationState(current);
|
|
|
m_convergenceHistory.append(current.fitness);
|
|
|
writeTraceRow(m_currentIteration,
|
|
|
bestProbeColumn,
|
|
|
"trust_region_sensitivity_accept",
|
|
|
current.parameters,
|
|
|
current.fitness,
|
|
|
true,
|
|
|
0,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
"accepted_cached_probe",
|
|
|
current.parameters,
|
|
|
current.fitness,
|
|
|
¤t.breakdown);
|
|
|
emit logMessageGenerated(
|
|
|
tr("Sensitivity probe accepted: error reduced to %1")
|
|
|
.arg(current.fitness, 0, 'e', 4));
|
|
|
} else {
|
|
|
restoreEvaluationState(current);
|
|
|
}
|
|
|
|
|
|
acceptedSinceRebuild = 0;
|
|
|
movementSinceRebuild = 0.0;
|
|
|
consecutiveRejectedSteps = 0;
|
|
|
rebuildRequested = false;
|
|
|
// 若重建过程中接受了试算点,当前模型已通过割线平移而不是在新点完整
|
|
|
// 重算;再遇到最小半径停滞时仍允许做一次真正的新点重建。
|
|
|
modelRebuiltAtMinimumRadius =
|
|
|
trustRadius <= minimumTrustRadius * 1.01 &&
|
|
|
!bestProbe.valid;
|
|
|
emit logMessageGenerated(
|
|
|
tr("Sensitivity model rebuilt: %1/%2 parameter columns valid")
|
|
|
.arg(validColumnCount)
|
|
|
.arg(dimensions));
|
|
|
return true;
|
|
|
};
|
|
|
|
|
|
int completedIterations = 0;
|
|
|
for(int iteration = 0;
|
|
|
iteration < m_maxIterations &&
|
|
|
m_totalEvaluations < maximumEvaluations &&
|
|
|
!m_shouldStop;
|
|
|
++iteration) {
|
|
|
m_currentIteration = iteration;
|
|
|
completedIterations = iteration + 1;
|
|
|
|
|
|
if(!processPauseAndStop()) {
|
|
|
break;
|
|
|
}
|
|
|
if(rebuildRequested) {
|
|
|
const bool confirmingStagnation =
|
|
|
stagnationConfirmationRequested;
|
|
|
if(!rebuildSensitivity()) {
|
|
|
stopReason = m_shouldStop
|
|
|
? PSO_USER_STOPPED
|
|
|
: PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
if(current.fitness < m_targetError) {
|
|
|
stopReason = PSO_TARGET_ACHIEVED;
|
|
|
break;
|
|
|
}
|
|
|
if(m_totalEvaluations >= maximumEvaluations) {
|
|
|
stopReason = PSO_MAX_ITERATIONS;
|
|
|
break;
|
|
|
}
|
|
|
const bool rebuildEffective =
|
|
|
registerEffectiveImprovement(current.fitness);
|
|
|
if(confirmingStagnation && !rebuildEffective) {
|
|
|
emit logMessageGenerated(
|
|
|
tr("Sensitivity rebuild produced no effective improvement; "
|
|
|
"local convergence detected"));
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 先确定当前最突出的可靠诊断误差,用其梯度回答“哪些参数最能改善
|
|
|
// 当前问题”;实际 LM 方向仍由完整残差梯度和 Jacobian 共同计算。
|
|
|
int dominantComponent = trustRegionDominantComponent(
|
|
|
current.breakdown, diagnosisThreshold);
|
|
|
const QVector<double>* componentGradient = nullptr;
|
|
|
if(dominantComponent == TRUST_REGION_VERTICAL_COMPONENT) {
|
|
|
componentGradient = &verticalGradient;
|
|
|
} else if(dominantComponent == TRUST_REGION_HORIZONTAL_COMPONENT) {
|
|
|
componentGradient = &horizontalGradient;
|
|
|
} else if(dominantComponent == TRUST_REGION_SHAPE_COMPONENT) {
|
|
|
componentGradient = &shapeGradient;
|
|
|
}
|
|
|
|
|
|
// 主目标采用 0.5*||r||^2,其对参数的梯度为 J^T*r。这里不再叠加
|
|
|
// vertical/horizontal/shape,保证诊断分量不会改变真实接受目标。
|
|
|
QVector<double> totalGradient(dimensions, 0.0);
|
|
|
for(int column = 0; column < dimensions; ++column) {
|
|
|
if(!jacobianColumnValid[column]) {
|
|
|
continue;
|
|
|
}
|
|
|
for(int row = 0; row < jacobian.size(); ++row) {
|
|
|
totalGradient[column] +=
|
|
|
jacobian[row][column] *
|
|
|
current.breakdown.residualVector[row];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 每轮最多联合调整三个灵敏参数。按当前诊断梯度绝对值由大到小选取,
|
|
|
// 并剔除 Jacobian 响应过度共线的列,降低弱可辨识参数互相补偿的风险。
|
|
|
QVector<int> selectedColumns;
|
|
|
QVector<bool> alreadyConsidered(dimensions, false);
|
|
|
for(int selection = 0; selection < qMin(3, dimensions); ++selection) {
|
|
|
int bestColumn = -1;
|
|
|
double bestScore = 0.0;
|
|
|
for(int column = 0; column < dimensions; ++column) {
|
|
|
if(alreadyConsidered[column] ||
|
|
|
!jacobianColumnValid[column]) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double score = componentGradient
|
|
|
? qAbs((*componentGradient)[column])
|
|
|
: qAbs(totalGradient[column]);
|
|
|
if(!isFiniteNumber(score) || score <= bestScore) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
bool excessivelyCorrelated = false;
|
|
|
for(int selectedIndex = 0;
|
|
|
selectedIndex < selectedColumns.size();
|
|
|
++selectedIndex) {
|
|
|
if(trustRegionJacobianColumnCorrelation(
|
|
|
jacobian,
|
|
|
column,
|
|
|
selectedColumns[selectedIndex]) >
|
|
|
columnCorrelationLimit) {
|
|
|
excessivelyCorrelated = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if(!excessivelyCorrelated) {
|
|
|
bestColumn = column;
|
|
|
bestScore = score;
|
|
|
}
|
|
|
}
|
|
|
if(bestColumn < 0 || bestScore <= 1.0e-12) {
|
|
|
break;
|
|
|
}
|
|
|
selectedColumns.append(bestColumn);
|
|
|
alreadyConsidered[bestColumn] = true;
|
|
|
}
|
|
|
|
|
|
// 诊断梯度接近零时,说明该分量在当前局部无法可靠选参,退回完整残差
|
|
|
// 梯度,但接受标准仍然只有真实 total,诊断值不会重复计入目标函数。
|
|
|
if(selectedColumns.isEmpty() && componentGradient) {
|
|
|
dominantComponent = TRUST_REGION_TOTAL_COMPONENT;
|
|
|
componentGradient = nullptr;
|
|
|
alreadyConsidered.fill(false, dimensions);
|
|
|
for(int selection = 0;
|
|
|
selection < qMin(3, dimensions);
|
|
|
++selection) {
|
|
|
int bestColumn = -1;
|
|
|
double bestScore = 0.0;
|
|
|
for(int column = 0; column < dimensions; ++column) {
|
|
|
if(alreadyConsidered[column] ||
|
|
|
!jacobianColumnValid[column]) {
|
|
|
continue;
|
|
|
}
|
|
|
double score = qAbs(totalGradient[column]);
|
|
|
if(score <= bestScore) {
|
|
|
continue;
|
|
|
}
|
|
|
bool excessivelyCorrelated = false;
|
|
|
for(int selectedIndex = 0;
|
|
|
selectedIndex < selectedColumns.size();
|
|
|
++selectedIndex) {
|
|
|
if(trustRegionJacobianColumnCorrelation(
|
|
|
jacobian,
|
|
|
column,
|
|
|
selectedColumns[selectedIndex]) >
|
|
|
columnCorrelationLimit) {
|
|
|
excessivelyCorrelated = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if(!excessivelyCorrelated) {
|
|
|
bestColumn = column;
|
|
|
bestScore = score;
|
|
|
}
|
|
|
}
|
|
|
if(bestColumn < 0 || bestScore <= 1.0e-12) {
|
|
|
break;
|
|
|
}
|
|
|
selectedColumns.append(bestColumn);
|
|
|
alreadyConsidered[bestColumn] = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 当前局部没有可用方向时先缩小半径并重建灵敏度;只有已经在最小
|
|
|
// 半径完整重建后仍无方向,才把它判定为局部最优。
|
|
|
if(selectedColumns.isEmpty()) {
|
|
|
if(trustRadius <= minimumTrustRadius * 1.01 &&
|
|
|
modelRebuiltAtMinimumRadius) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
trustRadius = qMax(minimumTrustRadius, trustRadius * 0.5);
|
|
|
damping = qMin(1.0e8, damping * 4.0);
|
|
|
rebuildRequested = true;
|
|
|
if(recordIneffectiveStep()) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 在选中参数子空间构造 LM 正规方程:
|
|
|
// (J^T*J + damping*diag(J^T*J))*step = -J^T*r。
|
|
|
// 对角缩放使不同参数列的灵敏度量级差异不会直接改变阻尼强弱。
|
|
|
const int selectedCount = selectedColumns.size();
|
|
|
QVector<QVector<double> > normalMatrix(
|
|
|
selectedCount, QVector<double>(selectedCount, 0.0));
|
|
|
QVector<double> rightHandSide(selectedCount, 0.0);
|
|
|
for(int left = 0; left < selectedCount; ++left) {
|
|
|
int leftColumn = selectedColumns[left];
|
|
|
rightHandSide[left] = -totalGradient[leftColumn];
|
|
|
for(int right = 0; right < selectedCount; ++right) {
|
|
|
int rightColumn = selectedColumns[right];
|
|
|
for(int row = 0; row < jacobian.size(); ++row) {
|
|
|
normalMatrix[left][right] +=
|
|
|
jacobian[row][leftColumn] *
|
|
|
jacobian[row][rightColumn];
|
|
|
}
|
|
|
}
|
|
|
double diagonalScale = qMax(
|
|
|
1.0e-10, normalMatrix[left][left]);
|
|
|
normalMatrix[left][left] += damping * diagonalScale;
|
|
|
}
|
|
|
|
|
|
QVector<double> selectedStep;
|
|
|
bool solved = solveTrustRegionLinearSystem(
|
|
|
normalMatrix, rightHandSide, &selectedStep);
|
|
|
QVector<double> coordinateStep(dimensions, 0.0);
|
|
|
if(solved) {
|
|
|
for(int i = 0; i < selectedCount; ++i) {
|
|
|
coordinateStep[selectedColumns[i]] = selectedStep[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double stepNorm = qSqrt(trustRegionSquaredNorm(coordinateStep));
|
|
|
if(!solved || !isFiniteNumber(stepNorm) ||
|
|
|
stepNorm < minimumCoordinateStep) {
|
|
|
// 正规方程退化时使用投影最速下降方向,仍只移动本轮已选择的参数。
|
|
|
coordinateStep.fill(0.0, dimensions);
|
|
|
double gradientNormSquared = 0.0;
|
|
|
for(int i = 0; i < selectedCount; ++i) {
|
|
|
int column = selectedColumns[i];
|
|
|
double stepDirection = -totalGradient[column];
|
|
|
if((current.coordinates[column] <= minimumCoordinateStep &&
|
|
|
stepDirection < 0.0) ||
|
|
|
(current.coordinates[column] >=
|
|
|
1.0 - minimumCoordinateStep &&
|
|
|
stepDirection > 0.0)) {
|
|
|
stepDirection = 0.0;
|
|
|
}
|
|
|
coordinateStep[column] = stepDirection;
|
|
|
gradientNormSquared += stepDirection * stepDirection;
|
|
|
}
|
|
|
double gradientNorm = qSqrt(gradientNormSquared);
|
|
|
if(gradientNorm > minimumCoordinateStep) {
|
|
|
double scale = trustRadius / gradientNorm;
|
|
|
for(int i = 0; i < selectedCount; ++i) {
|
|
|
int column = selectedColumns[i];
|
|
|
coordinateStep[column] *= scale;
|
|
|
}
|
|
|
}
|
|
|
stepNorm = qSqrt(trustRegionSquaredNorm(coordinateStep));
|
|
|
}
|
|
|
|
|
|
// LM 解只给出局部模型建议方向;若超出当前信赖半径,保持方向不变并
|
|
|
// 等比例截短,避免一次试算离开 Jacobian 有效的局部区域。
|
|
|
if(stepNorm > trustRadius && stepNorm > 0.0) {
|
|
|
double scale = trustRadius / stepNorm;
|
|
|
for(int i = 0; i < coordinateStep.size(); ++i) {
|
|
|
coordinateStep[i] *= scale;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 将 LM 步长投影到用户给定的参数范围,实际用于预测下降的也是投影后步长。
|
|
|
QVector<double> candidateCoordinates = current.coordinates;
|
|
|
for(int i = 0; i < dimensions; ++i) {
|
|
|
candidateCoordinates[i] = qBound(
|
|
|
0.0,
|
|
|
current.coordinates[i] + coordinateStep[i],
|
|
|
1.0);
|
|
|
coordinateStep[i] = candidateCoordinates[i] -
|
|
|
current.coordinates[i];
|
|
|
}
|
|
|
stepNorm = qSqrt(trustRegionSquaredNorm(coordinateStep));
|
|
|
|
|
|
// 用线性模型 r_new ~= r_current + J*step 预测残差,再用平方能量
|
|
|
// 的下降量与真实候选下降量比较,作为调整阻尼和半径的依据。
|
|
|
QVector<double> predictedResidual =
|
|
|
current.breakdown.residualVector;
|
|
|
for(int row = 0; row < jacobian.size(); ++row) {
|
|
|
for(int column = 0; column < dimensions; ++column) {
|
|
|
predictedResidual[row] +=
|
|
|
jacobian[row][column] * coordinateStep[column];
|
|
|
}
|
|
|
}
|
|
|
double predictedReduction = 0.5 *
|
|
|
(trustRegionSquaredNorm(current.breakdown.residualVector) -
|
|
|
trustRegionSquaredNorm(predictedResidual));
|
|
|
|
|
|
// 无实际移动或模型预测不下降时没有必要调用昂贵求解器。将它按一次
|
|
|
// 拒绝处理,并在连续发生后重建灵敏度,防止继续沿失效模型试算。
|
|
|
if(stepNorm < minimumCoordinateStep ||
|
|
|
!isFiniteNumber(predictedReduction) ||
|
|
|
predictedReduction <= 1.0e-14) {
|
|
|
trustRadius = qMax(minimumTrustRadius, trustRadius * 0.5);
|
|
|
damping = qMin(1.0e8, damping * 4.0);
|
|
|
++consecutiveRejectedSteps;
|
|
|
if(consecutiveRejectedSteps >= 2) {
|
|
|
if(trustRadius <= minimumTrustRadius * 1.01 &&
|
|
|
modelRebuiltAtMinimumRadius) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
rebuildRequested = true;
|
|
|
}
|
|
|
if(recordIneffectiveStep()) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
TrustRegionEvaluation candidate;
|
|
|
candidate.coordinates = candidateCoordinates;
|
|
|
candidate.parameters = parametersFromCoordinates(candidate.coordinates);
|
|
|
candidate.valid = evaluateTrustRegionPoint(
|
|
|
candidate.parameters,
|
|
|
&candidate.fitness,
|
|
|
&candidate.breakdown,
|
|
|
&candidate.curve,
|
|
|
&candidate.elapsedMs);
|
|
|
|
|
|
if(!candidate.valid) {
|
|
|
// 求解失败的候选不能改变 current。先完整恢复上一个已接受参数和
|
|
|
// 对应误差快照,再缩小信赖域;连续失败达到上限才终止整个拟合。
|
|
|
++consecutiveSolverFailures;
|
|
|
++consecutiveRejectedSteps;
|
|
|
trustRadius = qMax(minimumTrustRadius, trustRadius * 0.5);
|
|
|
damping = qMin(1.0e8, damping * 4.0);
|
|
|
writeTraceRow(m_currentIteration,
|
|
|
-1,
|
|
|
"trust_region_candidate",
|
|
|
candidate.parameters,
|
|
|
candidate.fitness,
|
|
|
false,
|
|
|
candidate.elapsedMs,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
"solver_invalid",
|
|
|
current.parameters,
|
|
|
current.fitness,
|
|
|
nullptr);
|
|
|
restoreEvaluationState(current);
|
|
|
if(consecutiveRejectedSteps >= 2) {
|
|
|
rebuildRequested = true;
|
|
|
}
|
|
|
if(consecutiveSolverFailures >= m_maxConsecutiveFailures) {
|
|
|
stopReason = PSO_CONSECUTIVE_FAILURES;
|
|
|
break;
|
|
|
}
|
|
|
if(recordIneffectiveStep()) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
consecutiveSolverFailures = 0;
|
|
|
// 有效候选即使最终被拒绝,也提供了一条真实割线,可用于修正下一轮
|
|
|
// 局部模型;是否成为新工作点仍只由下面的 total 严格比较决定。
|
|
|
const AutoFitObjectiveBreakdown oldBreakdown = current.breakdown;
|
|
|
updateTrustRegionJacobian(
|
|
|
&jacobian,
|
|
|
oldBreakdown.residualVector,
|
|
|
candidate.breakdown.residualVector,
|
|
|
coordinateStep);
|
|
|
if(oldBreakdown.verticalReliable &&
|
|
|
candidate.breakdown.verticalReliable &&
|
|
|
!oldBreakdown.registrationAmbiguous &&
|
|
|
!candidate.breakdown.registrationAmbiguous) {
|
|
|
updateTrustRegionScalarGradient(
|
|
|
&verticalGradient,
|
|
|
oldBreakdown.verticalCommonBias,
|
|
|
candidate.breakdown.verticalCommonBias,
|
|
|
coordinateStep);
|
|
|
}
|
|
|
if(oldBreakdown.horizontalReliable &&
|
|
|
candidate.breakdown.horizontalReliable &&
|
|
|
!oldBreakdown.registrationAmbiguous &&
|
|
|
!candidate.breakdown.registrationAmbiguous) {
|
|
|
updateTrustRegionScalarGradient(
|
|
|
&horizontalGradient,
|
|
|
oldBreakdown.horizontalPhysicalShift,
|
|
|
candidate.breakdown.horizontalPhysicalShift,
|
|
|
coordinateStep);
|
|
|
}
|
|
|
updateTrustRegionScalarGradient(
|
|
|
&shapeGradient,
|
|
|
oldBreakdown.shapeLoss,
|
|
|
candidate.breakdown.shapeLoss,
|
|
|
coordinateStep);
|
|
|
|
|
|
// reductionRatio 衡量局部线性模型的可信度:接近 1 表示预测准确;
|
|
|
// 值较小表示虽然可能下降,但模型低估了非线性,需要收紧下一步。
|
|
|
double actualReduction = 0.5 *
|
|
|
(current.fitness * current.fitness -
|
|
|
candidate.fitness * candidate.fitness);
|
|
|
double reductionRatio = actualReduction / predictedReduction;
|
|
|
bool accepted = candidate.fitness < current.fitness;
|
|
|
QString componentName = trustRegionComponentName(dominantComponent);
|
|
|
|
|
|
if(accepted) {
|
|
|
// 真实总误差下降后才正式替换 current,并同步发布参数、曲线和诊断。
|
|
|
// 模型预测可靠时减小阻尼并可扩大半径,预测较差时保守收缩。
|
|
|
current = candidate;
|
|
|
publishAcceptedPoint(current);
|
|
|
restoreEvaluationState(current);
|
|
|
++acceptedSinceRebuild;
|
|
|
movementSinceRebuild += stepNorm;
|
|
|
consecutiveRejectedSteps = 0;
|
|
|
m_convergenceHistory.append(current.fitness);
|
|
|
|
|
|
if(reductionRatio > 0.75) {
|
|
|
damping = qMax(1.0e-8, damping * 0.5);
|
|
|
if(stepNorm >= trustRadius * 0.8) {
|
|
|
trustRadius = qMin(
|
|
|
maximumTrustRadius, trustRadius * 1.6);
|
|
|
}
|
|
|
} else if(reductionRatio > 0.25) {
|
|
|
damping = qMax(1.0e-8, damping * 0.8);
|
|
|
} else {
|
|
|
damping = qMin(1.0e8, damping * 2.0);
|
|
|
trustRadius = qMax(
|
|
|
minimumTrustRadius, trustRadius * 0.75);
|
|
|
}
|
|
|
|
|
|
if(acceptedSinceRebuild >= 6 ||
|
|
|
movementSinceRebuild >= 0.30) {
|
|
|
rebuildRequested = true;
|
|
|
}
|
|
|
modelRebuiltAtMinimumRadius = false;
|
|
|
} else {
|
|
|
// 拒绝时 candidate 只保留在 trace 中,DataManager 和内存状态都恢复
|
|
|
// 到 current。连续拒绝说明割线模型可能失真,因此请求重新试算灵敏度。
|
|
|
++consecutiveRejectedSteps;
|
|
|
damping = qMin(1.0e8, damping * 4.0);
|
|
|
trustRadius = qMax(minimumTrustRadius, trustRadius * 0.5);
|
|
|
restoreEvaluationState(current);
|
|
|
if(consecutiveRejectedSteps >= 2) {
|
|
|
rebuildRequested = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 候选只要更优就继续作为 current 保存;是否足以解除停滞,则统一
|
|
|
// 相对上一次有效改善基准判断。拒绝和微小改善都会累计无效次数。
|
|
|
const bool effectiveImprovement =
|
|
|
registerEffectiveImprovement(current.fitness);
|
|
|
if(!effectiveImprovement && recordIneffectiveStep()) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
}
|
|
|
|
|
|
writeTraceRow(m_currentIteration,
|
|
|
-1,
|
|
|
"trust_region_candidate",
|
|
|
candidate.parameters,
|
|
|
candidate.fitness,
|
|
|
true,
|
|
|
candidate.elapsedMs,
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
accepted
|
|
|
? QString("accepted_%1").arg(componentName)
|
|
|
: QString("rejected_%1").arg(componentName),
|
|
|
current.parameters,
|
|
|
current.fitness,
|
|
|
&candidate.breakdown);
|
|
|
|
|
|
emit logMessageGenerated(
|
|
|
tr("Iteration %1: focus=%2, parameters=%3, error=%4, result=%5")
|
|
|
.arg(iteration + 1)
|
|
|
.arg(componentName)
|
|
|
.arg(selectedColumns.size())
|
|
|
.arg(candidate.fitness, 0, 'e', 4)
|
|
|
.arg(accepted ? tr("accepted") : tr("rejected")));
|
|
|
emit progressUpdated(iteration + 1, m_globalBestFitness);
|
|
|
|
|
|
if(stopReason == PSO_LOCAL_OPTIMUM) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if(current.fitness < m_targetError) {
|
|
|
stopReason = PSO_TARGET_ACHIEVED;
|
|
|
break;
|
|
|
}
|
|
|
if(trustRadius <= minimumTrustRadius * 1.01 &&
|
|
|
consecutiveRejectedSteps >= 2) {
|
|
|
if(modelRebuiltAtMinimumRadius) {
|
|
|
stopReason = PSO_LOCAL_OPTIMUM;
|
|
|
break;
|
|
|
}
|
|
|
rebuildRequested = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(completedIterations > 0) {
|
|
|
m_currentIteration = completedIterations - 1;
|
|
|
}
|
|
|
restoreEvaluationState(current);
|
|
|
|
|
|
if(m_shouldStop) {
|
|
|
return PSO_USER_STOPPED;
|
|
|
}
|
|
|
if(current.fitness < m_targetError) {
|
|
|
return PSO_TARGET_ACHIEVED;
|
|
|
}
|
|
|
if(stopReason == PSO_CONSECUTIVE_FAILURES ||
|
|
|
stopReason == PSO_LOCAL_OPTIMUM ||
|
|
|
stopReason == PSO_OPTIMIZATION_FAILED) {
|
|
|
return stopReason;
|
|
|
}
|
|
|
return PSO_MAX_ITERATIONS;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateParticle(int particleIndex)
|
|
|
{
|
|
|
if(particleIndex < 0 || particleIndex >= m_swarm.size()) return;
|
|
|
|
|
|
AutoFitParticle& particle = m_swarm[particleIndex];
|
|
|
|
|
|
// 单粒子真实评价入口。
|
|
|
// 这里调用 evaluateFitness(),因此会真实写 DataManager、调用求解器、计算误差。
|
|
|
// 被代理模型筛掉的粒子不会进入这个函数。
|
|
|
particle.evaluatedThisIteration = false;
|
|
|
particle.lastEvaluationSuccess = false;
|
|
|
particle.lastEvaluationElapsedMs = -1;
|
|
|
particle.pbestRelativeImprovementThisIteration = 0.0;
|
|
|
particle.selectedForSolver = true;
|
|
|
|
|
|
if(particle.screeningDecision.isEmpty()) {
|
|
|
particle.screeningDecision = "full_solver";
|
|
|
}
|
|
|
|
|
|
bool reuseInitialSolution = m_currentIteration == 0 &&
|
|
|
particleIndex == 0 &&
|
|
|
m_hasValidUserSolution &&
|
|
|
m_userInitialFitness < 1e9 &&
|
|
|
particle.position.size() == m_userInitialSolution.size();
|
|
|
|
|
|
for(int i = 0; reuseInitialSolution && i < particle.position.size(); ++i) {
|
|
|
double tolerance = qMax(1.0e-12, qAbs(m_userInitialSolution[i]) * 1.0e-12);
|
|
|
reuseInitialSolution = qAbs(particle.position[i] - m_userInitialSolution[i]) <= tolerance;
|
|
|
}
|
|
|
|
|
|
if(reuseInitialSolution) {
|
|
|
// 初始解在进入粒子群前已经真实求解过。第一代第0号粒子位置完全相同,
|
|
|
// 直接复用真实误差和曲线,避免一次重复 DLL 调用且不改变 PSO 数学状态。
|
|
|
particle.fitness = m_userInitialFitness;
|
|
|
particle.currentLogLogData = m_userInitialLogLogData;
|
|
|
particle.currentObjectiveBreakdown = m_userInitialObjectiveBreakdown;
|
|
|
particle.lastEvaluationElapsedMs = 0;
|
|
|
particle.evaluatedThisIteration = true;
|
|
|
particle.lastEvaluationSuccess = true;
|
|
|
particle.screeningDecision = "initial_solution_cache";
|
|
|
emit logMessageGenerated(tr("Particle 1 reused the verified initial solution"));
|
|
|
} else {
|
|
|
QTime evalTimer;
|
|
|
evalTimer.start();
|
|
|
particle.fitness = evaluateFitness(particle.position);
|
|
|
particle.currentLogLogData = m_lastEvaluatedLogLogData;
|
|
|
particle.currentObjectiveBreakdown = m_lastObjectiveBreakdown;
|
|
|
particle.lastEvaluationElapsedMs = evalTimer.elapsed();
|
|
|
particle.evaluatedThisIteration = true;
|
|
|
particle.lastEvaluationSuccess = (particle.fitness < 1e9);
|
|
|
m_totalEvaluations++;
|
|
|
|
|
|
if(particle.fitness < 1e9) {
|
|
|
m_successfulEvaluations++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新个体最优 pbest。这里使用的是真实求解器误差 particle.fitness,
|
|
|
// 不是代理模型给出的 surrogateObjective。
|
|
|
double previousBestFitness = particle.bestFitness;
|
|
|
|
|
|
if(particle.fitness < previousBestFitness) {
|
|
|
particle.pbestRelativeImprovementThisIteration = previousBestFitness >= 1e9
|
|
|
? 1.0
|
|
|
: (previousBestFitness - particle.fitness) /
|
|
|
qMax(1e-10, qAbs(previousBestFitness));
|
|
|
|
|
|
// 如果当前位置尚未成为新的真实全局最优,而上一代存在尚未真实验证、且代理
|
|
|
// 仍判断更优的 guide,就保留它继续引导速度;真实 pbest 仍照常更新。
|
|
|
bool preserveSurrogateGuide = false;
|
|
|
|
|
|
bool currentBeatsGlobalBest = particle.fitness < m_globalBestFitness;
|
|
|
|
|
|
if(!currentBeatsGlobalBest &&
|
|
|
isSurrogateScreeningEnabled() &&
|
|
|
particle.guideBestFromSurrogate &&
|
|
|
isFiniteNumber(particle.guideBestObjective) &&
|
|
|
isFiniteNumber(particle.surrogateObjective)) {
|
|
|
double requiredImprovement = qMax(1.0e-10,
|
|
|
qAbs(particle.guideBestObjective) *
|
|
|
kSurrogateGuidePbestMinRelativeImprovement);
|
|
|
preserveSurrogateGuide = particle.surrogateObjective -
|
|
|
particle.guideBestObjective > requiredImprovement;
|
|
|
}
|
|
|
|
|
|
particle.bestFitness = particle.fitness;
|
|
|
particle.bestPosition = particle.position;
|
|
|
particle.bestLogLogData = particle.currentLogLogData;
|
|
|
particle.bestObjectiveBreakdown = particle.currentObjectiveBreakdown;
|
|
|
|
|
|
if(!preserveSurrogateGuide) {
|
|
|
particle.guideBestPosition = particle.position;
|
|
|
particle.guideBestObjective = isFiniteNumber(particle.surrogateObjective)
|
|
|
? particle.surrogateObjective
|
|
|
: particle.fitness;
|
|
|
particle.guideBestFromSurrogate = false;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Particle %1 improved: error = %2")
|
|
|
.arg(particleIndex).arg(particle.fitness, 0, 'e', 4));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateGlobalBest()
|
|
|
{
|
|
|
// 保存上一轮的全局最优,用于后续自适应参数调整等
|
|
|
m_previousBestFitness = m_globalBestFitness;
|
|
|
bool globalBestUpdated = false;
|
|
|
|
|
|
// 遍历所有粒子,寻找比当前 global best 更好的个体最优。
|
|
|
// 注意:particle.bestFitness 只有在真实求解器评价成功后才会更新。
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
const AutoFitParticle& particle = m_swarm[i];
|
|
|
|
|
|
// 只要个体最优比当前全局最优小,就认为是更好的解
|
|
|
if(particle.bestFitness < m_globalBestFitness) {
|
|
|
|
|
|
double improvement = m_globalBestFitness - particle.bestFitness;
|
|
|
double relativeImprovement =
|
|
|
improvement / qMax(1e-10, qAbs(m_globalBestFitness));
|
|
|
|
|
|
// 区分显著改进和微小改进,但无论如何都会更新全局最优
|
|
|
if(relativeImprovement > m_improvementThreshold) {
|
|
|
DEBUG_OUT(QString(tr("Global best updated with %1% improvement: %2"))
|
|
|
.arg(relativeImprovement * 100.0, 0, 'f', 3)
|
|
|
.arg(particle.bestFitness, 0, 'e', 4));
|
|
|
} else {
|
|
|
DEBUG_OUT(QString(tr("Global best updated (minor improvement %1% < %2%) to %3"))
|
|
|
.arg(relativeImprovement * 100.0, 0, 'f', 3)
|
|
|
.arg(m_improvementThreshold * 100.0, 0, 'f', 2)
|
|
|
.arg(particle.bestFitness, 0, 'e', 4));
|
|
|
}
|
|
|
|
|
|
// 无论相对改进是否超过阈值,都要更新全局最优
|
|
|
m_globalBestFitness = particle.bestFitness;
|
|
|
m_globalBestPosition = particle.bestPosition;
|
|
|
m_globalBestLogLogData = particle.bestLogLogData;
|
|
|
m_globalBestObjectiveBreakdown = particle.bestObjectiveBreakdown;
|
|
|
globalBestUpdated = true;
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, m_currentIteration + 1, m_globalBestFitness);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 只有在本轮没有找到任何更好的解时才考虑恢复用户初始解
|
|
|
if(!globalBestUpdated && m_hasValidUserSolution && m_userInitialFitness < m_globalBestFitness) {
|
|
|
|
|
|
double improvement = m_globalBestFitness - m_userInitialFitness;
|
|
|
double relativeImprovement =
|
|
|
improvement / qMax(1e-10, qAbs(m_globalBestFitness));
|
|
|
|
|
|
DEBUG_OUT("Elite protection: Restoring user initial solution as global best");
|
|
|
DEBUG_OUT(QString("Elite solution is better than current best by %1%")
|
|
|
.arg(relativeImprovement * 100.0, 0, 'f', 3));
|
|
|
|
|
|
m_globalBestFitness = m_userInitialFitness;
|
|
|
m_globalBestPosition = m_userInitialSolution;
|
|
|
m_globalBestLogLogData = m_userInitialLogLogData;
|
|
|
m_globalBestObjectiveBreakdown = m_userInitialObjectiveBreakdown;
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, m_currentIteration + 1, m_globalBestFitness);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//void nmCalculationAutoFitPSO::updateGlobalBest()
|
|
|
//{
|
|
|
// m_previousBestFitness = m_globalBestFitness;
|
|
|
// bool globalBestUpdated = false;
|
|
|
//
|
|
|
// for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
// const AutoFitParticle& particle = m_swarm[i];
|
|
|
//
|
|
|
// if(particle.bestFitness < m_globalBestFitness) {
|
|
|
// // 只有显著改进时才更新
|
|
|
// double improvement = (m_globalBestFitness - particle.bestFitness);
|
|
|
// double relativeImprovement = improvement / qMax(1e-10, qAbs(m_globalBestFitness));
|
|
|
//
|
|
|
// if(relativeImprovement > m_improvementThreshold) {
|
|
|
// m_globalBestFitness = particle.bestFitness;
|
|
|
// m_globalBestPosition = particle.bestPosition;
|
|
|
// globalBestUpdated = true;
|
|
|
//
|
|
|
// DEBUG_OUT(QString("Global best updated with %1% improvement: %2")
|
|
|
// .arg(relativeImprovement * 100, 0, 'f', 3)
|
|
|
// .arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
// } else {
|
|
|
// DEBUG_OUT(QString("Rejecting minor improvement: %1% (threshold: %2%)")
|
|
|
// .arg(relativeImprovement * 100, 0, 'f', 4)
|
|
|
// .arg(m_improvementThreshold * 100, 0, 'f', 2));
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// // 在没有找到更好解时才检查
|
|
|
// if(!globalBestUpdated && m_hasValidUserSolution && m_userInitialFitness < m_globalBestFitness) {
|
|
|
// DEBUG_OUT("Elite protection: No significant improvement found, checking initial solution");
|
|
|
//
|
|
|
// // 检查初始解是否仍然是最优的
|
|
|
// double improvement = m_globalBestFitness - m_userInitialFitness;
|
|
|
// double relativeImprovement = improvement / qMax(1e-10, qAbs(m_globalBestFitness));
|
|
|
//
|
|
|
// if(relativeImprovement > m_improvementThreshold * 0.5) { // 使用更宽松的阈值
|
|
|
// DEBUG_OUT("Elite protection: Restoring user initial solution");
|
|
|
// m_globalBestFitness = m_userInitialFitness;
|
|
|
// m_globalBestPosition = m_userInitialSolution;
|
|
|
// }
|
|
|
// }
|
|
|
//}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateVelocityAndPosition()
|
|
|
{
|
|
|
bool useSurrogateGuidePbest = isSurrogateScreeningEnabled();
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
AutoFitParticle& particle = m_swarm[i];
|
|
|
const QVector<double>& cognitiveBest = useSurrogateGuidePbest &&
|
|
|
particle.guideBestPosition.size() == particle.position.size()
|
|
|
? particle.guideBestPosition
|
|
|
: particle.bestPosition;
|
|
|
|
|
|
for(int j = 0; j < particle.position.size(); ++j) {
|
|
|
// PSO速度更新公式:
|
|
|
// inertia 保留上一轮搜索方向;
|
|
|
// cognitive 项让粒子靠近自己的历史最优;
|
|
|
// social 项让粒子靠近群体全局最优。
|
|
|
double r1 = random01();
|
|
|
double r2 = random01();
|
|
|
|
|
|
particle.velocity[j] = m_inertiaWeight * particle.velocity[j] +
|
|
|
m_cognitiveParam * r1 * (cognitiveBest[j] - particle.position[j]) +
|
|
|
m_socialParam * r2 * (m_globalBestPosition[j] - particle.position[j]);
|
|
|
|
|
|
// 速度限制。不同参数量纲差异很大,所以按该参数搜索区间的一定比例限速。
|
|
|
int paramIndex = m_enabledParamIndices[j];
|
|
|
double range = m_parameterUpper[paramIndex] - m_parameterLower[paramIndex];
|
|
|
double maxVel = range * VELOCITY_LIMIT_FACTOR;
|
|
|
|
|
|
particle.velocity[j] = qMax(-maxVel, qMin(maxVel, particle.velocity[j]));
|
|
|
|
|
|
// 更新位置
|
|
|
particle.position[j] += particle.velocity[j];
|
|
|
}
|
|
|
|
|
|
// 边界约束。PSO 位置更新后如果越界,直接夹回用户设置的上下界。
|
|
|
clampToLimits(particle.position);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//double nmCalculationAutoFitPSO::evaluateFitness(const QVector<double>& parameters)
|
|
|
//{
|
|
|
// const QString funcName = QString("evaluateError[%1]").arg(m_currentIteration);
|
|
|
// static int callCount = 0;
|
|
|
// callCount++;
|
|
|
//
|
|
|
// try {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Starting evaluation with %3 parameters")
|
|
|
// .arg(funcName).arg(callCount).arg(parameters.size()));
|
|
|
//
|
|
|
// // 打印参数值用于对比
|
|
|
// QString paramStr = "Parameters: ";
|
|
|
//
|
|
|
// for(int i = 0; i < parameters.size(); ++i) {
|
|
|
// paramStr += QString("[%1]=%2 ").arg(i).arg(parameters[i], 0, 'f', 6);
|
|
|
// }
|
|
|
//
|
|
|
// DEBUG_OUT(QString("%1: %2").arg(funcName).arg(paramStr));
|
|
|
//
|
|
|
// // 1. 参数有效性检查
|
|
|
// if(!validateParameters(parameters)) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Invalid parameters").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// // 2. 检查数据管理器状态
|
|
|
// nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
//
|
|
|
// if(!dataManager) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - DataManager is null").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// // 3. 应用参数到数据管理器
|
|
|
// try {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Applying parameters to DataManager").arg(funcName).arg(callCount));
|
|
|
// applyParametersToDataManager(parameters);
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Parameters applied successfully").arg(funcName).arg(callCount));
|
|
|
// } catch(const std::exception& e) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Failed to apply parameters: %3").arg(funcName).arg(callCount).arg(e.what()));
|
|
|
// return 1e10;
|
|
|
// } catch(...) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Unknown error applying parameters").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// // 4. 运行求解器
|
|
|
// QVector<QVector<double> > solverResult;
|
|
|
// const int maxRetries = 2;
|
|
|
// bool solverSuccess = false;
|
|
|
//
|
|
|
// for(int retry = 0; retry <= maxRetries; ++retry) {
|
|
|
// if(m_shouldStop) return 1e10;
|
|
|
//
|
|
|
// try {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Solver attempt %3/%4")
|
|
|
// .arg(funcName).arg(callCount).arg(retry + 1).arg(maxRetries + 1));
|
|
|
//
|
|
|
// // 在求解器调用前添加短暂延迟,确保状态稳定
|
|
|
// if(retry > 0) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Retry delay before solver attempt")
|
|
|
// .arg(funcName).arg(callCount));
|
|
|
// msleep(1000); // 增加延迟时间
|
|
|
// }
|
|
|
//
|
|
|
// solverResult = runSolver();
|
|
|
//
|
|
|
// if(!solverResult.isEmpty() && validateSolverResult(solverResult)) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Solver successful on attempt %3, result size: %4")
|
|
|
// .arg(funcName).arg(callCount).arg(retry + 1).arg(solverResult[0].size()));
|
|
|
// solverSuccess = true;
|
|
|
// break;
|
|
|
// } else {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Solver failed on attempt %3 - empty or invalid result")
|
|
|
// .arg(funcName).arg(callCount).arg(retry + 1));
|
|
|
//
|
|
|
// if(retry < maxRetries) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Will retry solver").arg(funcName).arg(callCount));
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// } catch(const std::exception& e) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Solver exception on attempt %3: %4")
|
|
|
// .arg(funcName).arg(callCount).arg(retry + 1).arg(e.what()));
|
|
|
// } catch(...) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Unknown solver exception on attempt %3")
|
|
|
// .arg(funcName).arg(callCount).arg(retry + 1));
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// if(!solverSuccess) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - All solver attempts failed").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// // 5. 获取双对数结果数据
|
|
|
// QVector<QVector<double> > resultLogLogData;
|
|
|
//
|
|
|
// try {
|
|
|
// //wells = dataManager->getWellDataList(); // 重新获取,确保是最新的
|
|
|
// nmDataWellBase* pTargetWell = dataManager->findWellByName(m_targetWellName);
|
|
|
//
|
|
|
// if(pTargetWell) {
|
|
|
// resultLogLogData = pTargetWell->getResultLogLog();
|
|
|
//
|
|
|
// if(!validateLogLogData(resultLogLogData)) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Invalid result LogLog data").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - LogLog result data obtained, size: %3")
|
|
|
// .arg(funcName).arg(callCount).arg(resultLogLogData[0].size()));
|
|
|
// } else {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - No wells found after solver").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
// } catch(const std::exception& e) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Error getting LogLog result: %3").arg(funcName).arg(callCount).arg(e.what()));
|
|
|
// return 1e10;
|
|
|
// } catch(...) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Unknown error getting LogLog result").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// // 6. 双对数曲线对齐和误差计算
|
|
|
// double error;
|
|
|
//
|
|
|
// try {
|
|
|
// error = calculateLogLogCurveError(m_targetLogLogData, resultLogLogData);
|
|
|
//
|
|
|
// if(!isFiniteNumber(error) || error < 0) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Invalid error value: %3").arg(funcName).arg(callCount).arg(error));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Evaluation successful, error = %3")
|
|
|
// .arg(funcName).arg(callCount).arg(error, 0, 'e', 6));
|
|
|
//
|
|
|
// } catch(const std::exception& e) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - LogLog error calculation failed: %3").arg(funcName).arg(callCount).arg(e.what()));
|
|
|
// return 1e10;
|
|
|
// } catch(...) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Unknown error in LogLog error calculation").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//
|
|
|
// return error;
|
|
|
//
|
|
|
// } catch(const std::exception& e) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Top-level exception: %3").arg(funcName).arg(callCount).arg(e.what()));
|
|
|
// return 1e10;
|
|
|
// } catch(...) {
|
|
|
// DEBUG_OUT(QString("%1: Call #%2 - Unknown top-level exception").arg(funcName).arg(callCount));
|
|
|
// return 1e10;
|
|
|
// }
|
|
|
//}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::evaluateFitness(const QVector<double>& parameters)
|
|
|
{
|
|
|
// 粒子适应度评价函数,也是自动拟合最核心的闭环:
|
|
|
// 1. 校验粒子参数是否在用户设置的上下界和基本物理范围内;
|
|
|
// 2. 将参数写入 DataManager 的储层/目标井对象;
|
|
|
// 3. 调用真实数值求解器,生成模拟结果;
|
|
|
// 4. 从本次求解任务读取目标井 result log-log 曲线;
|
|
|
// 5. 与目标 history log-log 曲线计算误差,误差越小代表拟合越好。
|
|
|
//
|
|
|
// 返回 1e10 表示该粒子评价失败或结果不可用。PSO 会把它当成很差的解。
|
|
|
const QString funcName = QString("evaluateError[%1]").arg(m_currentIteration);
|
|
|
static int callCount = 0;
|
|
|
callCount++;
|
|
|
m_lastEvaluatedLogLogData.clear();
|
|
|
m_lastObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
|
|
|
try {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Starting evaluation with %3 parameters")
|
|
|
.arg(funcName).arg(callCount).arg(parameters.size()));
|
|
|
|
|
|
// 打印参数值
|
|
|
QString paramStr = "Parameters: ";
|
|
|
|
|
|
for(int i = 0; i < parameters.size(); ++i) {
|
|
|
paramStr += QString("[%1]=%2 ").arg(i).arg(parameters[i], 0, 'f', 6);
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("%1: %2").arg(funcName).arg(paramStr));
|
|
|
|
|
|
// 1. 参数有效性检查。这里先在 PSO 层拦截明显非法的候选,
|
|
|
// 避免把非有限数、越界值或极端危险值传给求解器。
|
|
|
if(!validateParameters(parameters)) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - VALIDATION FAILED").arg(funcName).arg(callCount));
|
|
|
|
|
|
// 详细检查每个参数
|
|
|
for(int i = 0; i < parameters.size(); ++i) {
|
|
|
if(!isFiniteNumber(parameters[i])) {
|
|
|
DEBUG_OUT(QString(" -> Param[%1] is NOT finite: %2").arg(i).arg(parameters[i]));
|
|
|
}
|
|
|
|
|
|
if(i < m_enabledParamIndices.size()) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
|
|
|
if(paramIndex >= 0 && paramIndex < m_parameterLower.size()) {
|
|
|
double lower = m_parameterLower[paramIndex];
|
|
|
double upper = m_parameterUpper[paramIndex];
|
|
|
|
|
|
if(parameters[i] < lower) {
|
|
|
DEBUG_OUT(QString(" -> Param[%1]=%2 < lower bound %3")
|
|
|
.arg(i).arg(parameters[i]).arg(lower));
|
|
|
}
|
|
|
|
|
|
if(parameters[i] > upper) {
|
|
|
DEBUG_OUT(QString(" -> Param[%1]=%2 > upper bound %3")
|
|
|
.arg(i).arg(parameters[i]).arg(upper));
|
|
|
}
|
|
|
|
|
|
// 检查危险值。这些条件不是严格物理模型定义,
|
|
|
// 而是工程保护:避免求解器在明显异常输入下崩溃或返回无意义曲线。
|
|
|
switch(paramIndex) {
|
|
|
case 0: // 渗透率
|
|
|
if(parameters[i] <= 1e-6) {
|
|
|
DEBUG_OUT(QString(" -> REJECTED: Permeability too small: %1").arg(parameters[i]));
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2: // 井筒储集系数
|
|
|
if(parameters[i] <= 1e-8) {
|
|
|
DEBUG_OUT(QString(" -> REJECTED: Wellbore storage too small: %1").arg(parameters[i]));
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3: // 孔隙度
|
|
|
if(parameters[i] <= 1e-4 || parameters[i] >= 0.95) {
|
|
|
DEBUG_OUT(QString(" -> REJECTED: Unrealistic porosity: %1").arg(parameters[i]));
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Parameters validated OK").arg(funcName).arg(callCount));
|
|
|
|
|
|
// 2. 数据管理器检查。后续参数写回和求解器组装都依赖当前 DataManager。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
|
if(!dataManager) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - DataManager is NULL").arg(funcName).arg(callCount));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - DataManager OK").arg(funcName).arg(callCount));
|
|
|
|
|
|
// 3. 应用参数。parameters 的顺序与 m_enabledParamIndices 对齐,
|
|
|
// applyParametersToDataManager() 会把它们拆分写入储层参数和目标井参数。
|
|
|
try {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Applying parameters...").arg(funcName).arg(callCount));
|
|
|
applyParametersToDataManager(parameters);
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Parameters applied successfully").arg(funcName).arg(callCount));
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - FAILED to apply parameters: %3")
|
|
|
.arg(funcName).arg(callCount).arg(e.what()));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
// Dfc 和裂缝半长都通过 PEBI 裂缝数组传入,不属于每次求解都会重新组装的 Base/CS 参数。
|
|
|
// 勾选任一裂缝参数时刷新网格输出,保证本次真实试算使用新的导流能力和端点坐标。
|
|
|
const bool fractureGridParameterSelected =
|
|
|
(m_parameterSelected.size() > 8 && m_parameterSelected[8]) ||
|
|
|
(m_parameterSelected.size() > 9 && m_parameterSelected[9]);
|
|
|
if(fractureGridParameterSelected) {
|
|
|
nmCalculationPebiGrid* pebiGrid = nmCalculationPebiGrid::getInstance();
|
|
|
if(!pebiGrid || !pebiGrid->generateOutputPara()) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Failed to refresh PEBI fracture parameters")
|
|
|
.arg(funcName).arg(callCount));
|
|
|
return 1e10;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 4. 运行求解器。真实求解器偶发失败时允许重试,避免一次 DLL 调用异常
|
|
|
// 直接让整个粒子评价失败。
|
|
|
QVector<QVector<double>> solverResult;
|
|
|
const int maxRetries = 2;
|
|
|
bool solverSuccess = false;
|
|
|
|
|
|
for(int retry = 0; retry <= maxRetries; ++retry) {
|
|
|
if(m_shouldStop) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - User stop requested").arg(funcName).arg(callCount));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Solver attempt %3/%4")
|
|
|
.arg(funcName).arg(callCount).arg(retry + 1).arg(maxRetries + 1));
|
|
|
|
|
|
if(retry > 0) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Retry delay...").arg(funcName).arg(callCount));
|
|
|
msleep(1000);
|
|
|
}
|
|
|
|
|
|
solverResult = runSolver();
|
|
|
|
|
|
// 详细检查求解器结果
|
|
|
if(solverResult.isEmpty()) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Solver returned EMPTY result").arg(funcName).arg(callCount));
|
|
|
} else {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Solver returned %3 arrays")
|
|
|
.arg(funcName).arg(callCount).arg(solverResult.size()));
|
|
|
|
|
|
for(int i = 0; i < solverResult.size(); ++i) {
|
|
|
DEBUG_OUT(QString(" -> Array[%1] size: %2").arg(i).arg(solverResult[i].size()));
|
|
|
}
|
|
|
|
|
|
if(validateSolverResult(solverResult)) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Solver result VALIDATED on attempt %3")
|
|
|
.arg(funcName).arg(callCount).arg(retry + 1));
|
|
|
solverSuccess = true;
|
|
|
break;
|
|
|
} else {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Solver result VALIDATION FAILED on attempt %3")
|
|
|
.arg(funcName).arg(callCount).arg(retry + 1));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Solver EXCEPTION on attempt %3: %4")
|
|
|
.arg(funcName).arg(callCount).arg(retry + 1).arg(e.what()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!solverSuccess) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - ALL SOLVER ATTEMPTS FAILED").arg(funcName).arg(callCount));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
// 5. 获取 LogLog 数据。runSolverDll() 直接从求解任务复制目标井曲线,
|
|
|
// 不再依赖 DataManager 中可能被其它井或上一粒子改写的共享结果。
|
|
|
QVector<QVector<double>> resultLogLogData = m_lastEvaluatedLogLogData;
|
|
|
|
|
|
try {
|
|
|
if(!validateLogLogData(resultLogLogData)) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - LogLog data VALIDATION FAILED")
|
|
|
.arg(funcName).arg(callCount));
|
|
|
|
|
|
// 详细输出LogLog数据问题
|
|
|
if(resultLogLogData.size() < 3) {
|
|
|
DEBUG_OUT(QString(" -> LogLog arrays count: %1 (need 3)")
|
|
|
.arg(resultLogLogData.size()));
|
|
|
} else {
|
|
|
DEBUG_OUT(QString(" -> LogLog array sizes: X=%1, Y1=%2, Y2=%3")
|
|
|
.arg(resultLogLogData[0].size())
|
|
|
.arg(resultLogLogData[1].size())
|
|
|
.arg(resultLogLogData[2].size()));
|
|
|
|
|
|
// 检查数据有效性
|
|
|
for(int i = 0; i < qMin(5, resultLogLogData[0].size()); ++i) {
|
|
|
if(!isFiniteNumber(resultLogLogData[0][i]) ||
|
|
|
!isFiniteNumber(resultLogLogData[1][i]) ||
|
|
|
!isFiniteNumber(resultLogLogData[2][i])) {
|
|
|
DEBUG_OUT(QString(" -> Invalid data at index %1: X=%2, Y1=%3, Y2=%4")
|
|
|
.arg(i)
|
|
|
.arg(resultLogLogData[0][i])
|
|
|
.arg(resultLogLogData[1][i])
|
|
|
.arg(resultLogLogData[2][i]));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - LogLog data validated, size: %3")
|
|
|
.arg(funcName).arg(callCount).arg(resultLogLogData[0].size()));
|
|
|
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Error getting LogLog data: %3")
|
|
|
.arg(funcName).arg(callCount).arg(e.what()));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
// 6. 计算误差。这里比较的是目标井 history log-log 与当前模拟 result log-log。
|
|
|
double error;
|
|
|
|
|
|
try {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Calculating error...")
|
|
|
.arg(funcName).arg(callCount));
|
|
|
|
|
|
error = calculateLogLogCurveError(m_targetLogLogData, resultLogLogData);
|
|
|
|
|
|
if(!isFiniteNumber(error) || error < 0) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - INVALID error value: %3")
|
|
|
.arg(funcName).arg(callCount).arg(error));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - SUCCESS! Error = %3")
|
|
|
.arg(funcName).arg(callCount).arg(error, 0, 'e', 6));
|
|
|
// 保存最后一次有效曲线,供 updateParticle() 写入粒子 currentLogLogData。
|
|
|
m_lastEvaluatedLogLogData = resultLogLogData;
|
|
|
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - Error calculation FAILED: %3")
|
|
|
.arg(funcName).arg(callCount).arg(e.what()));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
return error;
|
|
|
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - TOP-LEVEL EXCEPTION: %3")
|
|
|
.arg(funcName).arg(callCount).arg(e.what()));
|
|
|
return 1e10;
|
|
|
} catch(...) {
|
|
|
DEBUG_OUT(QString("%1: Call #%2 - UNKNOWN TOP-LEVEL EXCEPTION")
|
|
|
.arg(funcName).arg(callCount));
|
|
|
return 1e10;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// ==================== 参数应用方法 ====================
|
|
|
|
|
|
void nmCalculationAutoFitPSO::applyParametersToDataManager(const QVector<double>& parameters)
|
|
|
{
|
|
|
// 将粒子的“启用参数向量”写回项目数据。
|
|
|
// parameters 的维度必须等于用户勾选的参数数量,顺序由 m_enabledParamIndices 决定。
|
|
|
// 这里不直接跑求解器,只负责把 DataManager 调整到该粒子对应的模型状态。
|
|
|
if(parameters.size() != getEnabledParameterCount()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
updateReservoirParameters(parameters);
|
|
|
updateWellParameters(parameters);
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateReservoirParameters(const QVector<double>& parameters)
|
|
|
{
|
|
|
// 更新储层级参数。井级参数 skin/wellboreC 不在这里改,由 updateWellParameters() 负责。
|
|
|
// 这里先取 DataManager 中 reservoir 的副本,修改后再整体写回 DataManager。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
nmDataReservoir reservoirData = dataManager->getReservoirDataCopy();
|
|
|
|
|
|
// paramIndex 是粒子 position 中的索引;i 是完整 10 个参数体系中的索引。
|
|
|
// 只有 m_parameterSelected[i] 为 true 时,才从 parameters 中消费一个值。
|
|
|
int paramIndex = 0;
|
|
|
|
|
|
for(int i = 0; i < m_parameterSelected.size(); ++i) {
|
|
|
if(m_parameterSelected[i] && paramIndex < parameters.size()) {
|
|
|
double value = parameters[paramIndex];
|
|
|
|
|
|
switch(i) {
|
|
|
case 0: // 渗透率
|
|
|
reservoirData.getPermeability().setValue(value);
|
|
|
break;
|
|
|
|
|
|
case 3: // 孔隙度
|
|
|
reservoirData.getPorosity().setValue(value);
|
|
|
break;
|
|
|
|
|
|
case 4: // 储层厚度
|
|
|
reservoirData.getThickness().setValue(value);
|
|
|
break;
|
|
|
|
|
|
case 5: // 综合压缩系数
|
|
|
reservoirData.getCt().setValue(value);
|
|
|
break;
|
|
|
|
|
|
case 6: // 岩石压缩系数
|
|
|
reservoirData.getCf().setValue(value);
|
|
|
break;
|
|
|
|
|
|
case 7: // 初始含水饱和度
|
|
|
reservoirData.getSwi().setValue(value);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
paramIndex++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新数据管理器
|
|
|
dataManager->updateReservoirData(reservoirData);
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateWellParameters(const QVector<double>& parameters)
|
|
|
{
|
|
|
// 更新目标井上的拟合参数。目前井级可拟合参数主要是:
|
|
|
// - skin:写入第一个 perforation;
|
|
|
// - wellboreC:写入井筒储集系数;
|
|
|
// - Dfc/裂缝半长:只写入垂直压裂井或多段压裂水平井。
|
|
|
// 如果目标井不存在或没有射孔数据,这里只记录 debug,不抛异常。
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
|
// 只更新当前目标井的井参数,避免多井项目中误改其他井。
|
|
|
//QVector<nmDataWellBase*> wells = dataManager->getWellDataList();
|
|
|
nmDataWellBase* pWell = dataManager->findWellByName(m_targetWellName);
|
|
|
|
|
|
if(!pWell) return;
|
|
|
|
|
|
//nmDataWellBase* pWell = wells[0]; // 使用第一口井
|
|
|
|
|
|
int paramIndex = 0;
|
|
|
|
|
|
for(int i = 0; i < m_parameterSelected.size(); ++i) {
|
|
|
if(m_parameterSelected[i] && paramIndex < parameters.size()) {
|
|
|
double value = parameters[paramIndex];
|
|
|
|
|
|
switch(i) {
|
|
|
case 1: { // 表皮系数
|
|
|
nmDataPerforation* perf = pWell->getPerforation(0);
|
|
|
|
|
|
if(perf) {
|
|
|
nmDataAttribute skinAttr = perf->getSkin();
|
|
|
skinAttr.setValue(value);
|
|
|
perf->setSkin(skinAttr);
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case 2: { // 井筒储集系数
|
|
|
nmDataAttribute wellboreAttr = pWell->getWellboreStorage();
|
|
|
wellboreAttr.setValue(value);
|
|
|
pWell->setWellboreStorage(wellboreAttr);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case 8: { // 裂缝导流能力
|
|
|
if(pWell->getWellType() == NM_WELL_MODEL::Vertical_Fractured_Well) {
|
|
|
nmDataVerticalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataVerticalFracturedWell*>(pWell);
|
|
|
if(fracturedWell) {
|
|
|
nmDataAttribute dfc = fracturedWell->getDfc();
|
|
|
dfc.setValue(value);
|
|
|
fracturedWell->setDfc(dfc);
|
|
|
}
|
|
|
} else if(pWell->getWellType() == NM_WELL_MODEL::Horizontal_Fractured_Well) {
|
|
|
nmDataHorizontalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataHorizontalFracturedWell*>(pWell);
|
|
|
if(fracturedWell) {
|
|
|
nmDataAttribute dfc = fracturedWell->getDfc();
|
|
|
dfc.setValue(value);
|
|
|
fracturedWell->setDfc(dfc);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case 9: { // 裂缝半长
|
|
|
// 直接修改井对象中的属性,复用已有信号重算裂缝端点。
|
|
|
if(pWell->getWellType() == NM_WELL_MODEL::Vertical_Fractured_Well) {
|
|
|
nmDataVerticalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataVerticalFracturedWell*>(pWell);
|
|
|
if(fracturedWell) {
|
|
|
fracturedWell->getFractureHalfLength().setValue(value);
|
|
|
}
|
|
|
} else if(pWell->getWellType() == NM_WELL_MODEL::Horizontal_Fractured_Well) {
|
|
|
nmDataHorizontalFracturedWell* fracturedWell =
|
|
|
dynamic_cast<nmDataHorizontalFracturedWell*>(pWell);
|
|
|
if(fracturedWell) {
|
|
|
fracturedWell->getFractureHalfLength().setValue(value);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
paramIndex++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 根据井类型更新到数据管理器
|
|
|
updateWellToDataManager(pWell);
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateWellToDataManager(nmDataWellBase* pWell)
|
|
|
{
|
|
|
if(!pWell) return;
|
|
|
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
NM_WELL_MODEL wellType = pWell->getWellType();
|
|
|
|
|
|
// DataManager 内部按井型维护不同容器。修改基类指针后,需要根据实际井型
|
|
|
// 调用对应 update 接口,才能让后续求解器组装读到最新 skin / wellboreC。
|
|
|
switch(wellType) {
|
|
|
case NM_WELL_MODEL::Vertical_Well: {
|
|
|
nmDataVerticalWell* pVerticalWell = dynamic_cast<nmDataVerticalWell*>(pWell);
|
|
|
|
|
|
if(pVerticalWell) {
|
|
|
QVector<nmDataVerticalWell> wells;
|
|
|
wells.append(*pVerticalWell);
|
|
|
dataManager->updateVerticalWells(wells);
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case NM_WELL_MODEL::Vertical_Fractured_Well: {
|
|
|
nmDataVerticalFracturedWell* pVFracturedWell = dynamic_cast<nmDataVerticalFracturedWell*>(pWell);
|
|
|
|
|
|
if(pVFracturedWell) {
|
|
|
QVector<nmDataVerticalFracturedWell> wells;
|
|
|
wells.append(*pVFracturedWell);
|
|
|
dataManager->updateVerticalFracturedWells(wells);
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case NM_WELL_MODEL::Horizontal_Fractured_Well: {
|
|
|
nmDataHorizontalFracturedWell* pHFracturedWell = dynamic_cast<nmDataHorizontalFracturedWell*>(pWell);
|
|
|
|
|
|
if(pHFracturedWell) {
|
|
|
QVector<nmDataHorizontalFracturedWell> wells;
|
|
|
wells.append(*pHFracturedWell);
|
|
|
dataManager->updateHorizontalFracturedWells(wells);
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// ==================== 求解器相关方法 ====================
|
|
|
|
|
|
QVector<QVector<double>> nmCalculationAutoFitPSO::runSolver()
|
|
|
{
|
|
|
// 真实求解器入口。目前默认走 DLL 方式,保留 runSolverExe() 是历史/备用路径。
|
|
|
// evaluateFitness() 不关心具体求解器形态,只要求返回可验证的双对数结果。
|
|
|
//return runSolverExe();
|
|
|
return runSolverDll();
|
|
|
}
|
|
|
|
|
|
// ==================== 数据处理方法 ====================
|
|
|
QVector<QPointF> nmCalculationAutoFitPSO::interpolateData(
|
|
|
const QVector<QPointF>& source, const QVector<double>& targetX) const
|
|
|
{
|
|
|
// 将一条曲线插值到指定 targetX 网格。
|
|
|
// 双对数误差计算前会分别把目标曲线和模拟曲线插到同一个 commonX 上,
|
|
|
// 这样每个时间点才能逐点比较。
|
|
|
QVector<QPointF> result;
|
|
|
|
|
|
if(source.isEmpty() || targetX.isEmpty()) {
|
|
|
DEBUG_OUT("Warning: Empty data in interpolation");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// 数据清理和验证合并:跳过 NaN/Inf 和极端大值,尽量保留可用点继续计算。
|
|
|
// 这里不立即把整个粒子判失败,是为了提高求解器偶发异常输出下的鲁棒性。
|
|
|
QVector<QPointF> validSource;
|
|
|
const double MAX_REASONABLE_VALUE = 1e12;
|
|
|
|
|
|
for(int i = 0; i < source.size(); ++i) {
|
|
|
const QPointF& point = source[i];
|
|
|
|
|
|
// 检查数值有效性
|
|
|
if(!isFiniteNumber(point.x()) || !isFiniteNumber(point.y())) {
|
|
|
DEBUG_OUT(QString("Skipping invalid data point at index %1: X=%2, Y=%3")
|
|
|
.arg(i).arg(point.x()).arg(point.y()));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 检查极大值 - 跳过而不是失败
|
|
|
if(qAbs(point.y()) > MAX_REASONABLE_VALUE) {
|
|
|
DEBUG_OUT(QString("Skipping extremely large Y value at index %1: %2")
|
|
|
.arg(i).arg(point.y()));
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 只保留有效的数据点
|
|
|
validSource.append(point);
|
|
|
}
|
|
|
|
|
|
// 检查清理后的数据是否足够
|
|
|
if(validSource.size() < 3) {
|
|
|
DEBUG_OUT(QString("Insufficient valid data points after cleaning: %1")
|
|
|
.arg(validSource.size()));
|
|
|
return result; // 返回空结果,但不算失败
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Data cleaning: %1 -> %2 valid points")
|
|
|
.arg(source.size()).arg(validSource.size()));
|
|
|
|
|
|
// 对清理后的数据按 X 排序。后续线性插值要求横坐标单调。
|
|
|
QVector<QPointF> sortedSource = validSource;
|
|
|
|
|
|
for(int i = 0; i < sortedSource.size() - 1; ++i) {
|
|
|
for(int j = 0; j < sortedSource.size() - 1 - i; ++j) {
|
|
|
if(sortedSource[j].x() > sortedSource[j + 1].x()) {
|
|
|
QPointF temp = sortedSource[j];
|
|
|
sortedSource[j] = sortedSource[j + 1];
|
|
|
sortedSource[j + 1] = temp;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double sourceMinX = sortedSource.first().x();
|
|
|
double sourceMaxX = sortedSource.last().x();
|
|
|
|
|
|
double targetMinX = targetX[0];
|
|
|
double targetMaxX = targetX[0];
|
|
|
|
|
|
for(int i = 1; i < targetX.size(); ++i) {
|
|
|
if(targetX[i] < targetMinX) targetMinX = targetX[i];
|
|
|
|
|
|
if(targetX[i] > targetMaxX) targetMaxX = targetX[i];
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Source X range: [%1, %2], Target X range: [%3, %4]")
|
|
|
.arg(sourceMinX).arg(sourceMaxX).arg(targetMinX).arg(targetMaxX));
|
|
|
|
|
|
// 插值算法:
|
|
|
// - 在源数据范围内用线性内插;
|
|
|
// - 目标 X 落在源范围外时做有限斜率外推;
|
|
|
// - 找不到区间时使用最近点兜底。
|
|
|
for(int i = 0; i < targetX.size(); ++i) {
|
|
|
double x = targetX[i];
|
|
|
|
|
|
if(!isFiniteNumber(x)) continue;
|
|
|
|
|
|
double y = 0.0;
|
|
|
|
|
|
// 插值逻辑
|
|
|
if(x <= sourceMinX) {
|
|
|
if(sortedSource.size() >= 2) {
|
|
|
double dx = sortedSource[1].x() - sortedSource[0].x();
|
|
|
|
|
|
if(qAbs(dx) > 1e-10) {
|
|
|
double slope = (sortedSource[1].y() - sortedSource[0].y()) / dx;
|
|
|
slope = qMax(-1e6, qMin(1e6, slope));
|
|
|
y = sortedSource[0].y() + slope * (x - sortedSource[0].x());
|
|
|
} else {
|
|
|
y = sortedSource[0].y();
|
|
|
}
|
|
|
} else {
|
|
|
y = sortedSource[0].y();
|
|
|
}
|
|
|
} else if(x >= sourceMaxX) {
|
|
|
if(sortedSource.size() >= 2) {
|
|
|
int lastIdx = sortedSource.size() - 1;
|
|
|
double dx = sortedSource[lastIdx].x() - sortedSource[lastIdx - 1].x();
|
|
|
|
|
|
if(qAbs(dx) > 1e-10) {
|
|
|
double slope = (sortedSource[lastIdx].y() - sortedSource[lastIdx - 1].y()) / dx;
|
|
|
slope = qMax(-1e6, qMin(1e6, slope));
|
|
|
y = sortedSource[lastIdx].y() + slope * (x - sortedSource[lastIdx].x());
|
|
|
} else {
|
|
|
y = sortedSource[lastIdx].y();
|
|
|
}
|
|
|
} else {
|
|
|
y = sortedSource.last().y();
|
|
|
}
|
|
|
} else {
|
|
|
// 内插
|
|
|
bool found = false;
|
|
|
|
|
|
for(int j = 0; j < sortedSource.size() - 1; ++j) {
|
|
|
if(x >= sortedSource[j].x() && x <= sortedSource[j + 1].x()) {
|
|
|
double dx = sortedSource[j + 1].x() - sortedSource[j].x();
|
|
|
|
|
|
if(qAbs(dx) > 1e-10) {
|
|
|
double ratio = (x - sortedSource[j].x()) / dx;
|
|
|
y = sortedSource[j].y() + ratio * (sortedSource[j + 1].y() - sortedSource[j].y());
|
|
|
} else {
|
|
|
y = sortedSource[j].y();
|
|
|
}
|
|
|
|
|
|
found = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!found) {
|
|
|
// 使用最近点
|
|
|
double minDist = 1e10;
|
|
|
|
|
|
for(int k = 0; k < sortedSource.size(); ++k) {
|
|
|
double dist = qAbs(sortedSource[k].x() - x);
|
|
|
|
|
|
if(dist < minDist) {
|
|
|
minDist = dist;
|
|
|
y = sortedSource[k].y();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 最终数值检查
|
|
|
if(!isFiniteNumber(y)) {
|
|
|
y = sortedSource.size() > 0 ? sortedSource[0].y() : 1.0;
|
|
|
}
|
|
|
|
|
|
y = qMax(-1e12, qMin(1e12, y));
|
|
|
|
|
|
result.append(QPointF(x, y));
|
|
|
}
|
|
|
|
|
|
if(result.isEmpty()) {
|
|
|
DEBUG_OUT("LogLog interpolation failed");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT(QString("Interpolation completed: %1 -> %2 points")
|
|
|
.arg(validSource.size()).arg(result.size()));
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// ==================== 算法辅助方法 ====================
|
|
|
|
|
|
void nmCalculationAutoFitPSO::adaptiveParameterUpdate(int iteration)
|
|
|
{
|
|
|
// 自适应调整 PSO 系数。
|
|
|
// 随迭代推进降低惯性权重,让搜索从全局探索逐步转向局部收敛;
|
|
|
// 当近期改进很小时,提高 cognitive、降低 social,让粒子更多参考自身历史最优。
|
|
|
double progress = static_cast<double>(iteration) / m_maxIterations;
|
|
|
// 更快降低惯性权重,增强局部搜索
|
|
|
m_inertiaWeight = 0.4 - 0.25 * progress;
|
|
|
|
|
|
if(iteration > 0) {
|
|
|
double improvement = m_previousBestFitness - m_globalBestFitness;
|
|
|
double relativeImprovement = improvement / qMax(1e-10, qAbs(m_previousBestFitness));
|
|
|
|
|
|
if(relativeImprovement < 1e-5) {
|
|
|
// 改进很小时,增强局部搜索
|
|
|
m_cognitiveParam = qMin(2.5, m_cognitiveParam * 1.05);
|
|
|
m_socialParam = qMax(0.8, m_socialParam * 0.95);
|
|
|
} else {
|
|
|
// 有明显改进时,保持平衡
|
|
|
m_cognitiveParam = qMax(1.5, qMin(2.2, m_cognitiveParam));
|
|
|
m_socialParam = qMax(1.0, qMin(1.5, m_socialParam));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::saveOptimizationResult()
|
|
|
{
|
|
|
// 当前函数只做日志记录。真正把最优参数写回项目数据的是
|
|
|
// startAutoFitting() 结束阶段的 applyParametersToDataManager(m_globalBestPosition)。
|
|
|
DEBUG_OUT(QString("Optimization result: error=%1, evaluations=%2/%3")
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4)
|
|
|
.arg(m_successfulEvaluations)
|
|
|
.arg(m_totalEvaluations));
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::validateAndProtectFinalResult()
|
|
|
{
|
|
|
// 最终精英保护只阻止无效结果或真正变差的结果。任何真实误差下降都应保留,
|
|
|
// 不能再用固定百分比门槛把已经找到的更优解恢复成初始值。
|
|
|
if(!m_hasValidUserSolution) {
|
|
|
emit logMessageGenerated(tr("No initial solution for elite protection"));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(tr("=== Final Result Validation (Elite Protection) ==="));
|
|
|
|
|
|
// 使用已有的评估结果
|
|
|
double finalFitness = m_globalBestFitness;
|
|
|
double initialFitness = m_userInitialFitness;
|
|
|
|
|
|
emit logMessageGenerated(tr("Comparing results: Initial=%1, Final=%2")
|
|
|
.arg(initialFitness, 0, 'e', 4).arg(finalFitness, 0, 'e', 4));
|
|
|
|
|
|
bool finalValid = isFiniteNumber(finalFitness) &&
|
|
|
finalFitness < 1.0e9 &&
|
|
|
m_globalBestPosition.size() ==
|
|
|
m_userInitialSolution.size() &&
|
|
|
!m_globalBestLogLogData.isEmpty() &&
|
|
|
m_globalBestObjectiveBreakdown.valid;
|
|
|
if(finalValid) {
|
|
|
double improvement = initialFitness - finalFitness;
|
|
|
double relativeImprovement =
|
|
|
improvement / qMax(1.0e-10, qAbs(initialFitness));
|
|
|
emit logMessageGenerated(tr("Improvement: %1 (%2%)")
|
|
|
.arg(improvement, 0, 'e', 4)
|
|
|
.arg(relativeImprovement * 100, 0, 'f', 2));
|
|
|
}
|
|
|
|
|
|
if(!finalValid || finalFitness > initialFitness) {
|
|
|
emit logMessageGenerated(
|
|
|
tr("Elite protection triggered: final result is invalid or worse than initial"));
|
|
|
emit logMessageGenerated(tr("Restoring initial solution as final result"));
|
|
|
|
|
|
m_globalBestFitness = initialFitness;
|
|
|
m_globalBestPosition = m_userInitialSolution;
|
|
|
m_globalBestLogLogData = m_userInitialLogLogData;
|
|
|
m_globalBestObjectiveBreakdown = m_userInitialObjectiveBreakdown;
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, m_currentIteration + 1, m_globalBestFitness);
|
|
|
|
|
|
emit logMessageGenerated(tr("Initial solution restored successfully"));
|
|
|
} else {
|
|
|
emit logMessageGenerated(
|
|
|
tr("Final result validated - solution is not worse than initial"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// ==================== 工具方法 ====================
|
|
|
|
|
|
double nmCalculationAutoFitPSO::random01() const
|
|
|
{
|
|
|
// PSO 使用 Qt 的 qrand/qsrand。随机种子在 startAutoFitting() 中设置,
|
|
|
// 当前默认固定种子,便于复现实验和 trace。
|
|
|
return static_cast<double>(qrand()) / RAND_MAX;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::clampToLimits(QVector<double>& parameters) const
|
|
|
{
|
|
|
// 将粒子位置夹回用户给定上下界。parameters 是紧凑的启用参数向量,
|
|
|
// 需要通过 m_enabledParamIndices 找到它在完整参数体系中的上下界。
|
|
|
for(int i = 0; i < parameters.size() && i < m_enabledParamIndices.size(); ++i) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
|
|
|
if(paramIndex >= 0 && paramIndex < m_parameterLower.size()) {
|
|
|
parameters[i] = qMax(m_parameterLower[paramIndex],
|
|
|
qMin(m_parameterUpper[paramIndex], parameters[i]));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int nmCalculationAutoFitPSO::getEnabledParameterCount() const
|
|
|
{
|
|
|
// 返回粒子维度,即用户勾选参与拟合的参数数量。
|
|
|
int count = 0;
|
|
|
|
|
|
for(int i = 0; i < m_parameterSelected.size(); ++i) {
|
|
|
if(m_parameterSelected[i]) count++;
|
|
|
}
|
|
|
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
// ==================== 验证和处理方法 ====================
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::validateParameters(const QVector<double>& parameters) const
|
|
|
{
|
|
|
// 参数物理范围已由拟合窗口统一校验;候选评价只检查维度、有限数和
|
|
|
// 用户设置的上下界,避免另一套硬编码阈值与实际搜索范围冲突。
|
|
|
if(parameters.size() != getEnabledParameterCount()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < parameters.size(); ++i) {
|
|
|
if(!isFiniteNumber(parameters[i])) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 检查参数范围
|
|
|
if(i < m_enabledParamIndices.size()) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
|
|
|
if(paramIndex >= 0 && paramIndex < m_parameterLower.size()) {
|
|
|
if(parameters[i] < m_parameterLower[paramIndex] ||
|
|
|
parameters[i] > m_parameterUpper[paramIndex]) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::validateLogLogData(const QVector<QVector<double>>& logLogData) const
|
|
|
{
|
|
|
// 校验双对数曲线结构。约定:
|
|
|
// logLogData[0]=time,logLogData[1]=pressure,logLogData[2]=pressure derivative。
|
|
|
// 三列必须长度一致,且至少有足够点数用于插值和误差计算。
|
|
|
if(logLogData.size() < 3) {
|
|
|
DEBUG_OUT("LogLog data has less than 3 arrays");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 检查数组大小一致性
|
|
|
int size = logLogData[0].size();
|
|
|
|
|
|
if(size == 0) {
|
|
|
DEBUG_OUT("Empty LogLog data");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(logLogData[1].size() != size || logLogData[2].size() != size) {
|
|
|
DEBUG_OUT(QString("LogLog data size mismatch: X=%1, Y1=%2, Y2=%3")
|
|
|
.arg(logLogData[0].size())
|
|
|
.arg(logLogData[1].size())
|
|
|
.arg(logLogData[2].size()));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 检查最小数据点数
|
|
|
if(size < 5) {
|
|
|
DEBUG_OUT(QString("Too few LogLog data points: %1").arg(size));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 数据有效性检查
|
|
|
for(int i = 0; i < size; ++i) {
|
|
|
if(!isFiniteNumber(logLogData[0][i]) ||
|
|
|
!isFiniteNumber(logLogData[1][i]) ||
|
|
|
!isFiniteNumber(logLogData[2][i])) {
|
|
|
DEBUG_OUT(QString("Invalid LogLog data at index %1").arg(i));
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::validateInitialValues() const
|
|
|
{
|
|
|
// 检查当前模型读取出的初始参数是否和用户勾选维度一致,并且在上下界内。
|
|
|
// 如果初始值越界,算法仍可继续,但日志会提示,因为精英保护可能不可用或效果变差。
|
|
|
if(m_initialValues.size() != m_enabledParamIndices.size()) {
|
|
|
DEBUG_OUT("Initial values count mismatch with enabled parameters");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool allValid = true;
|
|
|
|
|
|
for(int i = 0; i < m_initialValues.size(); ++i) {
|
|
|
int paramIndex = m_enabledParamIndices[i];
|
|
|
double value = m_initialValues[i];
|
|
|
|
|
|
if(!isFiniteNumber(value)) {
|
|
|
DEBUG_OUT(QString("Initial value[%1] is not finite: %2").arg(i).arg(value));
|
|
|
allValid = false;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if(paramIndex < m_parameterLower.size() && paramIndex < m_parameterUpper.size()) {
|
|
|
double minVal = m_parameterLower[paramIndex];
|
|
|
double maxVal = m_parameterUpper[paramIndex];
|
|
|
|
|
|
if(value < minVal || value > maxVal) {
|
|
|
DEBUG_OUT(QString("Initial value[%1] = %2 is outside bounds [%3, %4]")
|
|
|
.arg(i).arg(value).arg(minVal).arg(maxVal));
|
|
|
allValid = false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return allValid;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::validateSolverResult(const QVector<QVector<double>>& result) const
|
|
|
{
|
|
|
// 校验求解器压力结果。这里检查的是 pressure result,至少需要 time 和 pressure 两列。
|
|
|
// result log-log 的结构会在 validateLogLogData() 中另行检查。
|
|
|
if(result.size() < 2) {
|
|
|
DEBUG_OUT("Solver result has less than 2 arrays");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(result[0].size() != result[1].size()) {
|
|
|
DEBUG_OUT(QString("Size mismatch: X=%1, Y=%2").arg(result[0].size()).arg(result[1].size()));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if(result[0].size() == 0) {
|
|
|
DEBUG_OUT("Empty solver result");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 检查最小数据点数
|
|
|
if(result[0].size() < 10) {
|
|
|
DEBUG_OUT(QString("Too few data points: %1").arg(result[0].size()));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 数据有效性检查
|
|
|
for(int i = 0; i < result[0].size(); ++i) {
|
|
|
if(!isFiniteNumber(result[0][i]) || !isFiniteNumber(result[1][i])) {
|
|
|
DEBUG_OUT(QString("Invalid data at index %1: X=%2, Y=%3")
|
|
|
.arg(i).arg(result[0][i]).arg(result[1][i]));
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 检查X值单调性
|
|
|
bool isMonotonic = true;
|
|
|
|
|
|
for(int i = 1; i < result[0].size(); ++i) {
|
|
|
if(result[0][i] <= result[0][i - 1]) {
|
|
|
isMonotonic = false;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!isMonotonic) {
|
|
|
DEBUG_OUT("X values are not monotonically increasing");
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateCurveError(
|
|
|
const QVector<QPointF>& curve1, const QVector<QPointF>& curve2) const
|
|
|
{
|
|
|
// 两条已经对齐到同一 X 网格的曲线误差。
|
|
|
// 点误差保持无量纲,避免油水压力和气井拟压力因数值尺度不同而不可比。
|
|
|
if(curve1.size() != curve2.size() || curve1.isEmpty()) {
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
// 预检查:确保没有无穷大值
|
|
|
for(int i = 0; i < curve1.size(); ++i) {
|
|
|
if(!isFiniteNumber(curve1[i].y()) || !isFiniteNumber(curve2[i].y())) {
|
|
|
DEBUG_OUT(QString("Infinite value detected at index %1: Y1=%2, Y2=%3")
|
|
|
.arg(i).arg(curve1[i].y()).arg(curve2[i].y()));
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
if(qAbs(curve1[i].y()) > 1e12 || qAbs(curve2[i].y()) > 1e12) {
|
|
|
DEBUG_OUT(QString("Extremely large value detected at index %1")
|
|
|
.arg(i));
|
|
|
return 1e10;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double totalError = 0.0;
|
|
|
int validPoints = 0;
|
|
|
|
|
|
for(int i = 0; i < curve1.size(); ++i) {
|
|
|
double y1 = curve1[i].y();
|
|
|
double y2 = curve2[i].y();
|
|
|
|
|
|
// 跳过异常值
|
|
|
if(!isFiniteNumber(y1) || !isFiniteNumber(y2)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double pointError = calculatePointError(y1, y2);
|
|
|
|
|
|
if(isFiniteNumber(pointError) && pointError < 1e10) {
|
|
|
totalError += pointError * pointError;
|
|
|
validPoints++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(validPoints > 0) {
|
|
|
double result = sqrt(totalError / validPoints);
|
|
|
|
|
|
// 最终检查
|
|
|
if(!isFiniteNumber(result)) {
|
|
|
DEBUG_OUT("Final error calculation produced infinite result");
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
return qMin(1e9, result); // 限制最大误差值
|
|
|
} else {
|
|
|
DEBUG_OUT(QString("No valid points for error calculation: validPoints=%1")
|
|
|
.arg(validPoints));
|
|
|
return 1e10;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateLogLogCurveError(
|
|
|
const QVector<QVector<double> >& target,
|
|
|
const QVector<QVector<double> >& result) const
|
|
|
{
|
|
|
// 主目标只比较固定网格上的压力和导数残差;上下、左右和形状只负责诊断
|
|
|
// 误差来源和选择参数,避免同一残差在 total 中被重复计算。整个计算过程均
|
|
|
// 位于 log(time)-log(value) 坐标,因此得到的是相对尺度偏差而非原始压力量纲。
|
|
|
const double invalidLoss = 1.0e10;
|
|
|
const double valueFloor = 1.0e-12;
|
|
|
const double minimumCoverage = 0.95;
|
|
|
const int numPoints = 80;
|
|
|
m_lastObjectiveBreakdown = AutoFitObjectiveBreakdown();
|
|
|
|
|
|
if(!validateLogLogData(target) || !validateLogLogData(result)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
// 无法比较的采样行先跳过;有限但非正的导数无法进入双对数空间,
|
|
|
// 当前数据又没有逐点有效掩码,因此遇到这种导数时判本次评价无效。
|
|
|
auto prepareCurve = [valueFloor](const QVector<QVector<double> >& data,
|
|
|
QVector<QPointF>* pressure,
|
|
|
QVector<QPointF>* derivative) -> bool {
|
|
|
if(!pressure || !derivative || data.size() < 3 ||
|
|
|
data[0].size() != data[1].size() ||
|
|
|
data[0].size() != data[2].size()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
for(int i = 0; i < data[0].size(); ++i) {
|
|
|
if(!isFiniteNumber(data[0][i]) ||
|
|
|
!isFiniteNumber(data[1][i]) ||
|
|
|
!isFiniteNumber(data[2][i]) ||
|
|
|
data[0][i] <= 0.0 ||
|
|
|
data[1][i] <= 0.0) {
|
|
|
continue;
|
|
|
}
|
|
|
if(data[2][i] <= 0.0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
pressure->append(QPointF(data[0][i], data[1][i]));
|
|
|
derivative->append(
|
|
|
QPointF(data[0][i], qMax(data[2][i], valueFloor)));
|
|
|
}
|
|
|
|
|
|
// 求解器输出可能不是严格升序,且同一时刻可能出现重复记录。
|
|
|
// 插值前统一排序并让后出现的记录覆盖同时间旧值,保证横坐标严格递增。
|
|
|
auto sortAndUnique = [](QVector<QPointF>* curve) {
|
|
|
std::stable_sort(
|
|
|
curve->begin(), curve->end(),
|
|
|
[](const QPointF& left, const QPointF& right) {
|
|
|
return left.x() < right.x();
|
|
|
});
|
|
|
|
|
|
QVector<QPointF> unique;
|
|
|
unique.reserve(curve->size());
|
|
|
for(int i = 0; i < curve->size(); ++i) {
|
|
|
if(unique.isEmpty() ||
|
|
|
curve->at(i).x() > unique.last().x()) {
|
|
|
unique.append(curve->at(i));
|
|
|
} else {
|
|
|
unique[unique.size() - 1] = curve->at(i);
|
|
|
}
|
|
|
}
|
|
|
*curve = unique;
|
|
|
};
|
|
|
|
|
|
sortAndUnique(pressure);
|
|
|
sortAndUnique(derivative);
|
|
|
return pressure->size() >= 3 && derivative->size() >= 3;
|
|
|
};
|
|
|
|
|
|
QVector<QPointF> targetPressure;
|
|
|
QVector<QPointF> targetDerivative;
|
|
|
QVector<QPointF> resultPressure;
|
|
|
QVector<QPointF> resultDerivative;
|
|
|
if(!prepareCurve(target, &targetPressure, &targetDerivative) ||
|
|
|
!prepareCurve(result, &resultPressure, &resultDerivative)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
// 在双对数坐标中插值。二分定位用于后面的多次水平配准试算。
|
|
|
auto interpolateLogValue = [valueFloor](
|
|
|
const QVector<QPointF>& curve,
|
|
|
double x,
|
|
|
double* value) -> bool {
|
|
|
if(!value || curve.size() < 2 || x <= 0.0 ||
|
|
|
x < curve.first().x() || x > curve.last().x()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
int low = 0;
|
|
|
int high = curve.size() - 1;
|
|
|
while(low < high) {
|
|
|
int middle = low + (high - low) / 2;
|
|
|
if(curve[middle].x() < x) {
|
|
|
low = middle + 1;
|
|
|
} else {
|
|
|
high = middle;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int right = qBound(1, low, curve.size() - 1);
|
|
|
int left = right - 1;
|
|
|
double leftLogX = qLn(curve[left].x());
|
|
|
double rightLogX = qLn(curve[right].x());
|
|
|
double denominator = rightLogX - leftLogX;
|
|
|
double leftLogY =
|
|
|
qLn(qMax(qAbs(curve[left].y()), valueFloor));
|
|
|
double rightLogY =
|
|
|
qLn(qMax(qAbs(curve[right].y()), valueFloor));
|
|
|
|
|
|
if(qAbs(denominator) <= 1.0e-12) {
|
|
|
*value = leftLogY;
|
|
|
} else {
|
|
|
double ratio = (qLn(x) - leftLogX) / denominator;
|
|
|
*value = leftLogY +
|
|
|
ratio * (rightLogY - leftLogY);
|
|
|
}
|
|
|
return isFiniteNumber(*value);
|
|
|
};
|
|
|
|
|
|
// 覆盖率通过后若只缺少首尾少量点,用模拟曲线自身的端点斜率作短距离
|
|
|
// 双对数外推。该外推只用于主损失的固定网格,不参与水平配准搜索。
|
|
|
auto extrapolateEndpointLogValue = [valueFloor](
|
|
|
const QVector<QPointF>& curve,
|
|
|
double x,
|
|
|
double* value) -> bool {
|
|
|
if(!value || curve.size() < 2 || x <= 0.0) {
|
|
|
return false;
|
|
|
}
|
|
|
int left = x < curve.first().x()
|
|
|
? 0
|
|
|
: curve.size() - 2;
|
|
|
int right = left + 1;
|
|
|
double leftLogX = qLn(curve[left].x());
|
|
|
double rightLogX = qLn(curve[right].x());
|
|
|
double denominator = rightLogX - leftLogX;
|
|
|
if(qAbs(denominator) <= 1.0e-12) {
|
|
|
return false;
|
|
|
}
|
|
|
double leftLogY =
|
|
|
qLn(qMax(qAbs(curve[left].y()), valueFloor));
|
|
|
double rightLogY =
|
|
|
qLn(qMax(qAbs(curve[right].y()), valueFloor));
|
|
|
double ratio = (qLn(x) - leftLogX) / denominator;
|
|
|
*value = leftLogY + ratio * (rightLogY - leftLogY);
|
|
|
return isFiniteNumber(*value);
|
|
|
};
|
|
|
|
|
|
const double targetMinX = targetPressure.first().x();
|
|
|
const double targetMaxX = targetPressure.last().x();
|
|
|
const double resultMinX = resultPressure.first().x();
|
|
|
const double resultMaxX = resultPressure.last().x();
|
|
|
if(targetMinX <= 0.0 || targetMaxX <= targetMinX ||
|
|
|
resultMinX <= 0.0 || resultMaxX <= resultMinX) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
QVector<double> commonX(numPoints);
|
|
|
QVector<double> commonLogX(numPoints);
|
|
|
QVector<double> targetLogPressure(numPoints);
|
|
|
QVector<double> targetLogDerivative(numPoints);
|
|
|
const double targetLogMinX = qLn(targetMinX);
|
|
|
const double targetLogMaxX = qLn(targetMaxX);
|
|
|
|
|
|
// 固定使用目标曲线的完整 log-time 网格,候选之间不会因采样点不同而失去可比性。
|
|
|
for(int i = 0; i < numPoints; ++i) {
|
|
|
double logX = targetLogMinX +
|
|
|
static_cast<double>(i) *
|
|
|
(targetLogMaxX - targetLogMinX) /
|
|
|
(numPoints - 1);
|
|
|
commonLogX[i] = logX;
|
|
|
commonX[i] = qExp(logX);
|
|
|
|
|
|
if(!interpolateLogValue(
|
|
|
targetPressure, commonX[i],
|
|
|
&targetLogPressure[i]) ||
|
|
|
!interpolateLogValue(
|
|
|
targetDerivative, commonX[i],
|
|
|
&targetLogDerivative[i])) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
QVector<double> pressureResidual(
|
|
|
numPoints, std::numeric_limits<double>::quiet_NaN());
|
|
|
QVector<double> derivativeResidual(
|
|
|
numPoints, std::numeric_limits<double>::quiet_NaN());
|
|
|
int firstSupported = -1;
|
|
|
int lastSupported = -1;
|
|
|
int supportedCount = 0;
|
|
|
|
|
|
// 残差定义为“模拟减目标”:正值表示模拟曲线偏高,负值表示偏低。
|
|
|
for(int i = 0; i < numPoints; ++i) {
|
|
|
if(commonX[i] < resultMinX || commonX[i] > resultMaxX) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double resultLogPressure = 0.0;
|
|
|
double resultLogDerivative = 0.0;
|
|
|
if(!interpolateLogValue(
|
|
|
resultPressure, commonX[i],
|
|
|
&resultLogPressure) ||
|
|
|
!interpolateLogValue(
|
|
|
resultDerivative, commonX[i],
|
|
|
&resultLogDerivative)) {
|
|
|
// 已位于结果时间范围内却无法插值说明数据存在内部断点,不能补线。
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
pressureResidual[i] =
|
|
|
resultLogPressure - targetLogPressure[i];
|
|
|
derivativeResidual[i] =
|
|
|
resultLogDerivative - targetLogDerivative[i];
|
|
|
++supportedCount;
|
|
|
if(firstSupported < 0) {
|
|
|
firstSupported = i;
|
|
|
}
|
|
|
lastSupported = i;
|
|
|
}
|
|
|
|
|
|
// 覆盖率同时约束“有效点数量”和“连续时间跨度”。取两者较小值可避免
|
|
|
// 点数很多但只集中在局部时段的候选被误认为覆盖充分。
|
|
|
AutoFitObjectiveBreakdown breakdown;
|
|
|
breakdown.coverage = supportedCount > 0
|
|
|
? static_cast<double>(supportedCount) /
|
|
|
numPoints
|
|
|
: 0.0;
|
|
|
if(firstSupported >= 0 && lastSupported >= firstSupported) {
|
|
|
double targetSpan =
|
|
|
qMax(1.0e-12, targetLogMaxX - targetLogMinX);
|
|
|
double coveredSpan =
|
|
|
commonLogX[lastSupported] - commonLogX[firstSupported];
|
|
|
breakdown.coverage = qMin(
|
|
|
breakdown.coverage,
|
|
|
qMax(0.0, coveredSpan / targetSpan));
|
|
|
}
|
|
|
|
|
|
// 覆盖率只判断候选是否有效,不再加入固定惩罚,避免所有误差被整体抬高。
|
|
|
if(breakdown.coverage < minimumCoverage) {
|
|
|
breakdown.total = invalidLoss;
|
|
|
m_lastObjectiveBreakdown = breakdown;
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
// 通过门槛后最多只缺少首尾少量目标点。按模拟曲线端点趋势补齐后,
|
|
|
// 每个候选仍在固定 80 点上计算均方根误差,不能靠少算难拟合端点获益。
|
|
|
for(int i = 0; i < numPoints; ++i) {
|
|
|
if(isFiniteNumber(pressureResidual[i]) &&
|
|
|
isFiniteNumber(derivativeResidual[i])) {
|
|
|
continue;
|
|
|
}
|
|
|
double resultLogPressure = 0.0;
|
|
|
double resultLogDerivative = 0.0;
|
|
|
if(!extrapolateEndpointLogValue(
|
|
|
resultPressure, commonX[i],
|
|
|
&resultLogPressure) ||
|
|
|
!extrapolateEndpointLogValue(
|
|
|
resultDerivative, commonX[i],
|
|
|
&resultLogDerivative)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
pressureResidual[i] =
|
|
|
resultLogPressure - targetLogPressure[i];
|
|
|
derivativeResidual[i] =
|
|
|
resultLogDerivative - targetLogDerivative[i];
|
|
|
}
|
|
|
|
|
|
// 在指定中心附近计算普通均方根误差。
|
|
|
auto rmseAround = [](
|
|
|
const QVector<double>& values,
|
|
|
int begin,
|
|
|
int end,
|
|
|
double center) -> double {
|
|
|
double sum = 0.0;
|
|
|
int count = 0;
|
|
|
int validBegin = qMax(0, begin);
|
|
|
int validEnd =
|
|
|
qMin(end, static_cast<int>(values.size()));
|
|
|
|
|
|
for(int i = validBegin; i < validEnd; ++i) {
|
|
|
if(!isFiniteNumber(values[i])) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double difference = values[i] - center;
|
|
|
sum += difference * difference;
|
|
|
++count;
|
|
|
}
|
|
|
|
|
|
return count > 0
|
|
|
? qSqrt(sum / count)
|
|
|
: std::numeric_limits<double>::quiet_NaN();
|
|
|
};
|
|
|
|
|
|
auto rmse = [&rmseAround](
|
|
|
const QVector<double>& values,
|
|
|
int begin,
|
|
|
int end) -> double {
|
|
|
return rmseAround(values, begin, end, 0.0);
|
|
|
};
|
|
|
|
|
|
// 普通算术平均中心保留上下偏差的符号。
|
|
|
auto meanCenterRange = [](
|
|
|
const QVector<double>& values,
|
|
|
int begin,
|
|
|
int end) -> double {
|
|
|
int validBegin = qMax(0, begin);
|
|
|
int validEnd =
|
|
|
qMin(end, static_cast<int>(values.size()));
|
|
|
double center = 0.0;
|
|
|
int count = 0;
|
|
|
|
|
|
for(int i = validBegin; i < validEnd; ++i) {
|
|
|
if(isFiniteNumber(values[i])) {
|
|
|
center += values[i];
|
|
|
++count;
|
|
|
}
|
|
|
}
|
|
|
if(count == 0) {
|
|
|
return std::numeric_limits<double>::quiet_NaN();
|
|
|
}
|
|
|
return center / count;
|
|
|
};
|
|
|
|
|
|
// 压力和导数合并后只求一个公共中心,表示两条曲线共同的上下位移。
|
|
|
// 分别去中心会把压力与导数之间真实的相对形状差异一并消除。
|
|
|
auto commonMeanCenterRange = [&meanCenterRange](
|
|
|
const QVector<double>& pressureValues,
|
|
|
const QVector<double>& derivativeValues,
|
|
|
int begin,
|
|
|
int end) -> double {
|
|
|
QVector<double> combined;
|
|
|
int validBegin = qMax(0, begin);
|
|
|
int validEnd = qMin(
|
|
|
end,
|
|
|
qMin(static_cast<int>(pressureValues.size()),
|
|
|
static_cast<int>(derivativeValues.size())));
|
|
|
combined.reserve(2 * qMax(0, validEnd - validBegin));
|
|
|
|
|
|
for(int i = validBegin; i < validEnd; ++i) {
|
|
|
if(isFiniteNumber(pressureValues[i])) {
|
|
|
combined.append(pressureValues[i]);
|
|
|
}
|
|
|
if(isFiniteNumber(derivativeValues[i])) {
|
|
|
combined.append(derivativeValues[i]);
|
|
|
}
|
|
|
}
|
|
|
return meanCenterRange(combined, 0, combined.size());
|
|
|
};
|
|
|
|
|
|
// 两个通道按能量等权合并,返回值与单通道 RMSE 保持同一量纲。
|
|
|
auto jointRmseAround = [&rmseAround](
|
|
|
const QVector<double>& pressureValues,
|
|
|
const QVector<double>& derivativeValues,
|
|
|
int begin,
|
|
|
int end,
|
|
|
double center) -> double {
|
|
|
double pressureLoss = rmseAround(
|
|
|
pressureValues, begin, end, center);
|
|
|
double derivativeLoss = rmseAround(
|
|
|
derivativeValues, begin, end, center);
|
|
|
if(!isFiniteNumber(pressureLoss) ||
|
|
|
!isFiniteNumber(derivativeLoss)) {
|
|
|
return std::numeric_limits<double>::quiet_NaN();
|
|
|
}
|
|
|
return qSqrt(0.5 *
|
|
|
(pressureLoss * pressureLoss +
|
|
|
derivativeLoss * derivativeLoss));
|
|
|
};
|
|
|
|
|
|
// 主目标始终使用未做上下或左右校正的完整曲线误差。
|
|
|
breakdown.pressureLoss =
|
|
|
rmse(pressureResidual, 0, numPoints);
|
|
|
breakdown.derivativeLoss =
|
|
|
rmse(derivativeResidual, 0, numPoints);
|
|
|
|
|
|
// 压力和导数各占一半权重。缩放后 residualVector 的二范数就是
|
|
|
// sqrt(0.5 * pressureLoss^2 + 0.5 * derivativeLoss^2)。
|
|
|
const double residualScale = qSqrt(0.5 / numPoints);
|
|
|
breakdown.residualVector.reserve(2 * numPoints);
|
|
|
for(int i = 0; i < numPoints; ++i) {
|
|
|
breakdown.residualVector.append(
|
|
|
residualScale *
|
|
|
pressureResidual[i]);
|
|
|
}
|
|
|
for(int i = 0; i < numPoints; ++i) {
|
|
|
breakdown.residualVector.append(
|
|
|
residualScale *
|
|
|
derivativeResidual[i]);
|
|
|
}
|
|
|
|
|
|
const double logGridStep =
|
|
|
(targetLogMaxX - targetLogMinX) /
|
|
|
(numPoints - 1);
|
|
|
const double resultLogMinX = qLn(resultMinX);
|
|
|
const double resultLogMaxX = qLn(resultMaxX);
|
|
|
// 左右配准只在所有候选位移都共同覆盖的固定区间比较,至少保留 80%
|
|
|
// 目标点;每个 log-time 网格间隔再细分为 8 份,提高位移分辨率。
|
|
|
const int minimumRegistrationPoints =
|
|
|
(numPoints * 4) / 5;
|
|
|
const int shiftSubdivisions = 8;
|
|
|
int maximumShiftIntervals = 4;
|
|
|
int registrationBegin = 0;
|
|
|
int registrationEnd = numPoints;
|
|
|
double maximumPhysicalShift =
|
|
|
maximumShiftIntervals * logGridStep;
|
|
|
|
|
|
// 所有位移候选使用同一组目标点。结果范围不足时逐步缩小最大位移,
|
|
|
// 但用于配准的固定公共区间不得少于目标网格的 80%。
|
|
|
auto updateRegistrationRange = [&](double maximumShift) {
|
|
|
registrationBegin = 0;
|
|
|
registrationEnd = numPoints;
|
|
|
while(registrationBegin < registrationEnd &&
|
|
|
commonLogX[registrationBegin] - maximumShift <
|
|
|
resultLogMinX - 1.0e-12) {
|
|
|
++registrationBegin;
|
|
|
}
|
|
|
while(registrationEnd > registrationBegin &&
|
|
|
commonLogX[registrationEnd - 1] + maximumShift >
|
|
|
resultLogMaxX + 1.0e-12) {
|
|
|
--registrationEnd;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
updateRegistrationRange(maximumPhysicalShift);
|
|
|
while(maximumShiftIntervals > 0 &&
|
|
|
registrationEnd - registrationBegin <
|
|
|
minimumRegistrationPoints) {
|
|
|
--maximumShiftIntervals;
|
|
|
maximumPhysicalShift =
|
|
|
maximumShiftIntervals * logGridStep;
|
|
|
updateRegistrationRange(maximumPhysicalShift);
|
|
|
}
|
|
|
if(registrationEnd - registrationBegin <
|
|
|
minimumRegistrationPoints) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
// physicalShift 为正表示模拟曲线偏右;对齐时在目标时刻右侧读取模拟值。
|
|
|
auto buildShiftResidual = [&](
|
|
|
double physicalShift,
|
|
|
int compareBegin,
|
|
|
int compareEnd,
|
|
|
QVector<double>* shiftedPressure,
|
|
|
QVector<double>* shiftedDerivative) -> bool {
|
|
|
if(!shiftedPressure || !shiftedDerivative) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
shiftedPressure->fill(
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
numPoints);
|
|
|
shiftedDerivative->fill(
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
numPoints);
|
|
|
|
|
|
int validBegin = qMax(0, compareBegin);
|
|
|
int validEnd = qMin(numPoints, compareEnd);
|
|
|
for(int i = validBegin; i < validEnd; ++i) {
|
|
|
double shiftedLogX =
|
|
|
commonLogX[i] + physicalShift;
|
|
|
if(shiftedLogX < resultLogMinX - 1.0e-12 ||
|
|
|
shiftedLogX >
|
|
|
resultLogMaxX + 1.0e-12) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
double shiftedX = qExp(
|
|
|
qBound(resultLogMinX,
|
|
|
shiftedLogX,
|
|
|
resultLogMaxX));
|
|
|
double resultLogPressure = 0.0;
|
|
|
double resultLogDerivative = 0.0;
|
|
|
if(!interpolateLogValue(
|
|
|
resultPressure, shiftedX,
|
|
|
&resultLogPressure) ||
|
|
|
!interpolateLogValue(
|
|
|
resultDerivative, shiftedX,
|
|
|
&resultLogDerivative)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
(*shiftedPressure)[i] =
|
|
|
resultLogPressure - targetLogPressure[i];
|
|
|
(*shiftedDerivative)[i] =
|
|
|
resultLogDerivative - targetLogDerivative[i];
|
|
|
}
|
|
|
return true;
|
|
|
};
|
|
|
|
|
|
// 损失相同时优先选择绝对位移更小的候选,避免平坦 profile 在数值噪声
|
|
|
// 下无故偏向搜索边界。
|
|
|
auto isBetterProfileValue = [](
|
|
|
double loss,
|
|
|
double shift,
|
|
|
double bestLoss,
|
|
|
double bestShift) -> bool {
|
|
|
const double tolerance = 1.0e-12;
|
|
|
return loss < bestLoss - tolerance ||
|
|
|
(qAbs(loss - bestLoss) <= tolerance &&
|
|
|
qAbs(shift) < qAbs(bestShift));
|
|
|
};
|
|
|
|
|
|
const int halfShiftStepCount =
|
|
|
maximumShiftIntervals * shiftSubdivisions;
|
|
|
const double physicalShiftStep =
|
|
|
logGridStep / shiftSubdivisions;
|
|
|
double zeroShiftCenteredLoss =
|
|
|
std::numeric_limits<double>::quiet_NaN();
|
|
|
double bestCenteredLoss =
|
|
|
std::numeric_limits<double>::infinity();
|
|
|
double bestPhysicalShift = 0.0;
|
|
|
int bestShiftStep = 0;
|
|
|
double bestPressureLoss =
|
|
|
std::numeric_limits<double>::infinity();
|
|
|
double bestPressureShift = 0.0;
|
|
|
double bestDerivativeLoss =
|
|
|
std::numeric_limits<double>::infinity();
|
|
|
double bestDerivativeShift = 0.0;
|
|
|
QVector<double> profileLosses(
|
|
|
2 * halfShiftStepCount + 1,
|
|
|
std::numeric_limits<double>::quiet_NaN());
|
|
|
QVector<double> profileCommonBiases(
|
|
|
2 * halfShiftStepCount + 1,
|
|
|
std::numeric_limits<double>::quiet_NaN());
|
|
|
QVector<double> shiftedPressureResidual;
|
|
|
QVector<double> shiftedDerivativeResidual;
|
|
|
|
|
|
// 位移和公共上下偏移联合求解,避免“先扣上下还是先扣左右”的顺序依赖。
|
|
|
for(int shiftStep = -halfShiftStepCount;
|
|
|
shiftStep <= halfShiftStepCount;
|
|
|
++shiftStep) {
|
|
|
double physicalShift =
|
|
|
shiftStep * physicalShiftStep;
|
|
|
if(!buildShiftResidual(
|
|
|
physicalShift,
|
|
|
registrationBegin,
|
|
|
registrationEnd,
|
|
|
&shiftedPressureResidual,
|
|
|
&shiftedDerivativeResidual)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double commonBias = commonMeanCenterRange(
|
|
|
shiftedPressureResidual,
|
|
|
shiftedDerivativeResidual,
|
|
|
registrationBegin,
|
|
|
registrationEnd);
|
|
|
double centeredLoss = jointRmseAround(
|
|
|
shiftedPressureResidual,
|
|
|
shiftedDerivativeResidual,
|
|
|
registrationBegin,
|
|
|
registrationEnd,
|
|
|
commonBias);
|
|
|
double pressureBias = meanCenterRange(
|
|
|
shiftedPressureResidual,
|
|
|
registrationBegin,
|
|
|
registrationEnd);
|
|
|
double derivativeBias = meanCenterRange(
|
|
|
shiftedDerivativeResidual,
|
|
|
registrationBegin,
|
|
|
registrationEnd);
|
|
|
double pressureLoss = rmseAround(
|
|
|
shiftedPressureResidual,
|
|
|
registrationBegin,
|
|
|
registrationEnd,
|
|
|
pressureBias);
|
|
|
double derivativeLoss = rmseAround(
|
|
|
shiftedDerivativeResidual,
|
|
|
registrationBegin,
|
|
|
registrationEnd,
|
|
|
derivativeBias);
|
|
|
|
|
|
if(!isFiniteNumber(centeredLoss) ||
|
|
|
!isFiniteNumber(pressureLoss) ||
|
|
|
!isFiniteNumber(derivativeLoss)) {
|
|
|
continue;
|
|
|
}
|
|
|
int profileIndex = shiftStep + halfShiftStepCount;
|
|
|
profileLosses[profileIndex] = centeredLoss;
|
|
|
profileCommonBiases[profileIndex] = commonBias;
|
|
|
if(shiftStep == 0) {
|
|
|
zeroShiftCenteredLoss = centeredLoss;
|
|
|
}
|
|
|
if(isBetterProfileValue(
|
|
|
centeredLoss,
|
|
|
physicalShift,
|
|
|
bestCenteredLoss,
|
|
|
bestPhysicalShift)) {
|
|
|
bestCenteredLoss = centeredLoss;
|
|
|
bestPhysicalShift = physicalShift;
|
|
|
bestShiftStep = shiftStep;
|
|
|
}
|
|
|
if(isBetterProfileValue(
|
|
|
pressureLoss,
|
|
|
physicalShift,
|
|
|
bestPressureLoss,
|
|
|
bestPressureShift)) {
|
|
|
bestPressureLoss = pressureLoss;
|
|
|
bestPressureShift = physicalShift;
|
|
|
}
|
|
|
if(isBetterProfileValue(
|
|
|
derivativeLoss,
|
|
|
physicalShift,
|
|
|
bestDerivativeLoss,
|
|
|
bestDerivativeShift)) {
|
|
|
bestDerivativeLoss = derivativeLoss;
|
|
|
bestDerivativeShift = physicalShift;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!isFiniteNumber(zeroShiftCenteredLoss) ||
|
|
|
!isFiniteNumber(bestCenteredLoss) ||
|
|
|
!isFiniteNumber(bestPressureLoss) ||
|
|
|
!isFiniteNumber(bestDerivativeLoss)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
// horizontalGain 是“允许水平位移”相对“固定零位移”减少的均方能量。
|
|
|
// 只有改善足够明显且最优点不是边界,才把位移解释为可靠左右偏差。
|
|
|
double horizontalGain = nestedRmsContribution(
|
|
|
zeroShiftCenteredLoss, bestCenteredLoss);
|
|
|
double horizontalSignalThreshold =
|
|
|
qMax(1.0e-5, zeroShiftCenteredLoss * 0.02);
|
|
|
int bestProfileIndex = bestShiftStep + halfShiftStepCount;
|
|
|
double nearbyProfileLoss =
|
|
|
std::numeric_limits<double>::infinity();
|
|
|
int leftProfileIndex =
|
|
|
bestProfileIndex - shiftSubdivisions;
|
|
|
int rightProfileIndex =
|
|
|
bestProfileIndex + shiftSubdivisions;
|
|
|
if(leftProfileIndex >= 0 &&
|
|
|
leftProfileIndex < profileLosses.size() &&
|
|
|
isFiniteNumber(profileLosses[leftProfileIndex])) {
|
|
|
nearbyProfileLoss = qMin(
|
|
|
nearbyProfileLoss,
|
|
|
profileLosses[leftProfileIndex]);
|
|
|
}
|
|
|
if(rightProfileIndex >= 0 &&
|
|
|
rightProfileIndex < profileLosses.size() &&
|
|
|
isFiniteNumber(profileLosses[rightProfileIndex])) {
|
|
|
nearbyProfileLoss = qMin(
|
|
|
nearbyProfileLoss,
|
|
|
profileLosses[rightProfileIndex]);
|
|
|
}
|
|
|
double profileContrast = isFiniteNumber(nearbyProfileLoss)
|
|
|
? nestedRmsContribution(
|
|
|
nearbyProfileLoss,
|
|
|
bestCenteredLoss)
|
|
|
: 0.0;
|
|
|
bool flatRegistrationProfile =
|
|
|
profileContrast <= horizontalSignalThreshold;
|
|
|
|
|
|
// 平台曲线的 profile 也可能很平,但公共 bias 在各个位移下保持稳定,
|
|
|
// 此时仍能可靠判断上下。只有近优位移会明显改变 bias 才说明上下/左右不可辨识。
|
|
|
double minimumNearOptimalBias =
|
|
|
std::numeric_limits<double>::infinity();
|
|
|
double maximumNearOptimalBias =
|
|
|
-std::numeric_limits<double>::infinity();
|
|
|
for(int i = 0; i < profileLosses.size(); ++i) {
|
|
|
if(isFiniteNumber(profileLosses[i]) &&
|
|
|
isFiniteNumber(profileCommonBiases[i]) &&
|
|
|
profileLosses[i] <=
|
|
|
bestCenteredLoss + horizontalSignalThreshold) {
|
|
|
minimumNearOptimalBias = qMin(
|
|
|
minimumNearOptimalBias,
|
|
|
profileCommonBiases[i]);
|
|
|
maximumNearOptimalBias = qMax(
|
|
|
maximumNearOptimalBias,
|
|
|
profileCommonBiases[i]);
|
|
|
}
|
|
|
}
|
|
|
double nearOptimalBiasSpread =
|
|
|
isFiniteNumber(minimumNearOptimalBias) &&
|
|
|
isFiniteNumber(maximumNearOptimalBias)
|
|
|
? maximumNearOptimalBias - minimumNearOptimalBias
|
|
|
: std::numeric_limits<double>::infinity();
|
|
|
bool commonBiasStable = nearOptimalBiasSpread <= 1.0e-2;
|
|
|
bool horizontalAtBoundary =
|
|
|
halfShiftStepCount > 0 &&
|
|
|
qAbs(bestShiftStep) == halfShiftStepCount;
|
|
|
// 压力和导数通道分别求出的最佳位移若方向相反或相差过大,说明一个
|
|
|
// 单一水平平移无法解释两条曲线,此时标记配准歧义并禁用左右引导。
|
|
|
bool pressureShiftDetected =
|
|
|
qAbs(bestPressureShift) >=
|
|
|
0.5 * physicalShiftStep;
|
|
|
bool derivativeShiftDetected =
|
|
|
qAbs(bestDerivativeShift) >=
|
|
|
0.5 * physicalShiftStep;
|
|
|
bool channelShiftConflict =
|
|
|
pressureShiftDetected &&
|
|
|
derivativeShiftDetected &&
|
|
|
(bestPressureShift * bestDerivativeShift < 0.0 ||
|
|
|
qAbs(bestPressureShift - bestDerivativeShift) >
|
|
|
2.0 * logGridStep);
|
|
|
|
|
|
breakdown.horizontalLoss = horizontalGain;
|
|
|
breakdown.horizontalReliable =
|
|
|
maximumShiftIntervals > 0 &&
|
|
|
!horizontalAtBoundary &&
|
|
|
!channelShiftConflict &&
|
|
|
!flatRegistrationProfile &&
|
|
|
horizontalGain > horizontalSignalThreshold &&
|
|
|
qAbs(bestPhysicalShift) >=
|
|
|
0.5 * physicalShiftStep;
|
|
|
breakdown.registrationAmbiguous =
|
|
|
channelShiftConflict ||
|
|
|
(qAbs(bestPhysicalShift) >=
|
|
|
0.5 * physicalShiftStep &&
|
|
|
!breakdown.horizontalReliable) ||
|
|
|
(flatRegistrationProfile && !commonBiasStable);
|
|
|
breakdown.horizontalPhysicalShift =
|
|
|
breakdown.horizontalReliable
|
|
|
? bestPhysicalShift
|
|
|
: 0.0;
|
|
|
|
|
|
// 可信水平位移确定后,在该位移实际覆盖的最大区间重新计算上下和形状。
|
|
|
int diagnosticBegin = 0;
|
|
|
int diagnosticEnd = numPoints;
|
|
|
while(diagnosticBegin < diagnosticEnd &&
|
|
|
commonLogX[diagnosticBegin] +
|
|
|
breakdown.horizontalPhysicalShift <
|
|
|
resultLogMinX - 1.0e-12) {
|
|
|
++diagnosticBegin;
|
|
|
}
|
|
|
while(diagnosticEnd > diagnosticBegin &&
|
|
|
commonLogX[diagnosticEnd - 1] +
|
|
|
breakdown.horizontalPhysicalShift >
|
|
|
resultLogMaxX + 1.0e-12) {
|
|
|
--diagnosticEnd;
|
|
|
}
|
|
|
if(diagnosticEnd - diagnosticBegin <
|
|
|
minimumRegistrationPoints ||
|
|
|
!buildShiftResidual(
|
|
|
breakdown.horizontalPhysicalShift,
|
|
|
diagnosticBegin,
|
|
|
diagnosticEnd,
|
|
|
&shiftedPressureResidual,
|
|
|
&shiftedDerivativeResidual)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
double commonBias = commonMeanCenterRange(
|
|
|
shiftedPressureResidual,
|
|
|
shiftedDerivativeResidual,
|
|
|
diagnosticBegin,
|
|
|
diagnosticEnd);
|
|
|
double rawAlignedLoss = jointRmseAround(
|
|
|
shiftedPressureResidual,
|
|
|
shiftedDerivativeResidual,
|
|
|
diagnosticBegin,
|
|
|
diagnosticEnd,
|
|
|
0.0);
|
|
|
double centeredAlignedLoss = jointRmseAround(
|
|
|
shiftedPressureResidual,
|
|
|
shiftedDerivativeResidual,
|
|
|
diagnosticBegin,
|
|
|
diagnosticEnd,
|
|
|
commonBias);
|
|
|
if(!isFiniteNumber(commonBias) ||
|
|
|
!isFiniteNumber(rawAlignedLoss) ||
|
|
|
!isFiniteNumber(centeredAlignedLoss)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
// 原始对齐误差减去公共中心后的能量差定义为上下误差贡献。只有它相对
|
|
|
// 当前对齐误差足够明显,且配准无歧义时,公共 bias 才可用于有符号选参。
|
|
|
breakdown.verticalCommonBias = commonBias;
|
|
|
breakdown.verticalLoss = nestedRmsContribution(
|
|
|
rawAlignedLoss, centeredAlignedLoss);
|
|
|
breakdown.verticalReliable =
|
|
|
!breakdown.registrationAmbiguous &&
|
|
|
breakdown.verticalLoss >
|
|
|
qMax(1.0e-5, rawAlignedLoss * 0.02);
|
|
|
|
|
|
QVector<double> shapePressure(
|
|
|
numPoints, std::numeric_limits<double>::quiet_NaN());
|
|
|
QVector<double> shapeDerivative(
|
|
|
numPoints, std::numeric_limits<double>::quiet_NaN());
|
|
|
for(int i = diagnosticBegin; i < diagnosticEnd; ++i) {
|
|
|
if(isFiniteNumber(shiftedPressureResidual[i])) {
|
|
|
shapePressure[i] =
|
|
|
shiftedPressureResidual[i] - commonBias;
|
|
|
}
|
|
|
if(isFiniteNumber(shiftedDerivativeResidual[i])) {
|
|
|
shapeDerivative[i] =
|
|
|
shiftedDerivativeResidual[i] - commonBias;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// shapeLoss 是去除可信左右位移和公共均值中心后的剩余误差。
|
|
|
double shapePressureLoss = rmse(
|
|
|
shapePressure, diagnosticBegin, diagnosticEnd);
|
|
|
double shapeDerivativeLoss = rmse(
|
|
|
shapeDerivative, diagnosticBegin, diagnosticEnd);
|
|
|
if(!isFiniteNumber(shapePressureLoss) ||
|
|
|
!isFiniteNumber(shapeDerivativeLoss)) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
breakdown.shapeLoss = qSqrt(
|
|
|
0.5 *
|
|
|
(shapePressureLoss * shapePressureLoss +
|
|
|
shapeDerivativeLoss * shapeDerivativeLoss));
|
|
|
|
|
|
// 现阶段不识别或单独调度晚期流动段;保留字段只为了维持现有 trace 列。
|
|
|
breakdown.lateDerivativeSlopeBias = 0.0;
|
|
|
breakdown.lateDerivativeTrendLoss = 0.0;
|
|
|
breakdown.lateDerivativeTrendReliable = false;
|
|
|
|
|
|
if(!isFiniteNumber(breakdown.pressureLoss) ||
|
|
|
!isFiniteNumber(breakdown.derivativeLoss) ||
|
|
|
!isFiniteNumber(breakdown.verticalCommonBias) ||
|
|
|
!isFiniteNumber(breakdown.verticalLoss) ||
|
|
|
!isFiniteNumber(breakdown.horizontalPhysicalShift) ||
|
|
|
!isFiniteNumber(breakdown.horizontalLoss) ||
|
|
|
!isFiniteNumber(breakdown.shapeLoss) ||
|
|
|
breakdown.residualVector.size() != 2 * numPoints) {
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
|
|
|
if(isSurrogateScreeningEnabled()) {
|
|
|
// 代理模型仍按原目标比较,不能用未参与训练的新损失改变候选排序。
|
|
|
breakdown.total =
|
|
|
0.5 * breakdown.pressureLoss +
|
|
|
0.5 * breakdown.derivativeLoss;
|
|
|
} else {
|
|
|
// 非代理总目标等于固定 160 维普通残差的二范数;压力和
|
|
|
// 导数各占一半能量。上下、左右和形状分量不参与候选排序与接受。
|
|
|
breakdown.total = qSqrt(
|
|
|
0.5 * breakdown.pressureLoss * breakdown.pressureLoss +
|
|
|
0.5 * breakdown.derivativeLoss * breakdown.derivativeLoss);
|
|
|
}
|
|
|
breakdown.valid =
|
|
|
isFiniteNumber(breakdown.total) &&
|
|
|
breakdown.total >= 0.0;
|
|
|
m_lastObjectiveBreakdown = breakdown;
|
|
|
|
|
|
DEBUG_OUT(
|
|
|
QString("LogLog objective: pressure=%1, derivative=%2, vertical=%3, horizontal=%4, shape=%5, ambiguous=%6, shift=%7, coverage=%8, total=%9")
|
|
|
.arg(breakdown.pressureLoss, 0, 'e', 4)
|
|
|
.arg(breakdown.derivativeLoss, 0, 'e', 4)
|
|
|
.arg(breakdown.verticalLoss, 0, 'e', 4)
|
|
|
.arg(breakdown.horizontalLoss, 0, 'e', 4)
|
|
|
.arg(breakdown.shapeLoss, 0, 'e', 4)
|
|
|
.arg(breakdown.registrationAmbiguous)
|
|
|
.arg(breakdown.horizontalPhysicalShift, 0, 'e', 4)
|
|
|
.arg(breakdown.coverage, 0, 'f', 4)
|
|
|
.arg(breakdown.total, 0, 'e', 4));
|
|
|
|
|
|
return breakdown.valid
|
|
|
? qMin(1.0e9, breakdown.total)
|
|
|
: invalidLoss;
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(
|
|
|
QString("Exception in LogLog error calculation: %1")
|
|
|
.arg(e.what()));
|
|
|
return invalidLoss;
|
|
|
} catch(...) {
|
|
|
DEBUG_OUT("Unknown exception in LogLog error calculation");
|
|
|
return invalidLoss;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculatePointError(double target, double result) const
|
|
|
{
|
|
|
if(!isFiniteNumber(target) || !isFiniteNumber(result)) {
|
|
|
return 1e10;
|
|
|
}
|
|
|
|
|
|
// 对数空间误差:关注数量级差异,适合双对数曲线。
|
|
|
double logTarget = qLn(qMax(1e-10, qAbs(target)));
|
|
|
double logResult = qLn(qMax(1e-10, qAbs(result)));
|
|
|
double logError = qAbs(logTarget - logResult);
|
|
|
|
|
|
// 线性空间相对误差:补充约束实际幅值差异。
|
|
|
double yMax = qMax(qAbs(target), qAbs(result));
|
|
|
double relativeError = qAbs(target - result) / qMax(1e-10, yMax);
|
|
|
|
|
|
// 组合误差:对数误差占 70%,线性误差占 30%。
|
|
|
return 0.7 * logError + 0.3 * relativeError;
|
|
|
}
|
|
|
|
|
|
QVector<QVector<double>> nmCalculationAutoFitPSO::runSolverDll()
|
|
|
{
|
|
|
// DLL 求解器路径。
|
|
|
// 这个函数负责把当前 DataManager 中的项目状态交给底层数值求解器,
|
|
|
// 数值求解仍包含全部计算井,以保留井间干扰;后处理只提取目标井曲线。
|
|
|
//
|
|
|
// 如果这里失败,通常需要优先检查:HX_NWTM.dll、license、网格/井数据是否完整、
|
|
|
// 目标井是否存在,以及 DataManager 中刚写入的参数是否导致求解器异常。
|
|
|
DEBUG_OUT("SOLVER DLL START");
|
|
|
|
|
|
if(m_evaluationInProgress > 0) {
|
|
|
DEBUG_OUT("DLL Solver already running, skipping");
|
|
|
return QVector<QVector<double>>();
|
|
|
}
|
|
|
|
|
|
++m_evaluationInProgress;
|
|
|
m_lastEvaluatedLogLogData.clear();
|
|
|
QVector<QVector<double>> result;
|
|
|
nmCalculationDllPebiSolverTask* dllTask = nullptr;
|
|
|
|
|
|
try {
|
|
|
DEBUG_OUT("Creating DLL solver task");
|
|
|
dllTask = new nmCalculationDllPebiSolverTask(m_tempDirectory);
|
|
|
|
|
|
if(m_targetWellName.isEmpty()) {
|
|
|
DEBUG_OUT("Target well name is empty - target-only solver cannot start");
|
|
|
delete dllTask;
|
|
|
--m_evaluationInProgress;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
dllTask->setAutoFitTargetWell(m_targetWellName);
|
|
|
|
|
|
if(m_shouldStop) {
|
|
|
DEBUG_OUT("Should stop - cleaning up and returning empty result");
|
|
|
delete dllTask;
|
|
|
--m_evaluationInProgress;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT("Starting DLL solver execution...");
|
|
|
|
|
|
// 异步执行 DLL 任务。循环等待期间持续 processEvents,保证界面不会完全卡死。
|
|
|
dllTask->start();
|
|
|
|
|
|
// 等待完成。最大等待 1 小时,适配大模型慢算;用户停止时会 terminate。
|
|
|
const int maxWait = 3600000; // 1h超时
|
|
|
const int checkInterval = 50;
|
|
|
QTime waitTimer;
|
|
|
waitTimer.start();
|
|
|
|
|
|
while(waitTimer.elapsed() < maxWait) {
|
|
|
// wait(timeout) 会在线程一完成时立即返回,避免原来固定 msleep(500)
|
|
|
// 带来的每次 0~500ms 额外等待;50ms 间隔仍可及时处理停止请求和界面事件。
|
|
|
if(dllTask->wait(checkInterval)) {
|
|
|
DEBUG_OUT("DLL solver task completed");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, checkInterval);
|
|
|
|
|
|
if(m_shouldStop) {
|
|
|
DEBUG_OUT("DLL solver task terminated by user");
|
|
|
dllTask->terminate();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 超时处理
|
|
|
if(dllTask->isRunning()) {
|
|
|
DEBUG_OUT("DLL solver task timeout, terminating...");
|
|
|
dllTask->terminate();
|
|
|
dllTask->wait(2000);
|
|
|
|
|
|
delete dllTask;
|
|
|
dllTask = nullptr;
|
|
|
--m_evaluationInProgress;
|
|
|
m_consecutiveFailures++;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// 线程结束后检查真实执行结果,防止失败时复用上一粒子的旧曲线。
|
|
|
dllTask->wait();
|
|
|
if(!dllTask->wasSuccessful()) {
|
|
|
DEBUG_OUT("DLL solver task reported failure");
|
|
|
delete dllTask;
|
|
|
dllTask = nullptr;
|
|
|
--m_evaluationInProgress;
|
|
|
m_consecutiveFailures++;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// 任务结束后复制其局部结果,删除任务前不再持有任务内部引用。
|
|
|
QVector<QVector<double>> pressureResult = dllTask->getAutoFitResultPressure();
|
|
|
QVector<QVector<double>> logLogResult = dllTask->getAutoFitResultLogLog();
|
|
|
|
|
|
DEBUG_OUT(QString("DLL result verification - Pressure arrays: %1, LogLog arrays: %2")
|
|
|
.arg(pressureResult.size()).arg(logLogResult.size()));
|
|
|
|
|
|
if(pressureResult.size() >= 2) {
|
|
|
DEBUG_OUT(QString("Pressure result - Time points: %1, Pressure points: %2")
|
|
|
.arg(pressureResult[0].size()).arg(pressureResult[1].size()));
|
|
|
|
|
|
if(pressureResult[0].size() > 0) {
|
|
|
DEBUG_OUT(QString("Sample pressure data - Time[0]: %1, Time[last]: %2, P[0]: %3, P[last]: %4")
|
|
|
.arg(pressureResult[0][0])
|
|
|
.arg(pressureResult[0][pressureResult[0].size() - 1])
|
|
|
.arg(pressureResult[1][0])
|
|
|
.arg(pressureResult[1][pressureResult[1].size() - 1]));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 数据有效性检查
|
|
|
if(pressureResult.size() >= 2
|
|
|
&& pressureResult[0].size() > 0
|
|
|
&& pressureResult[1].size() > 0
|
|
|
&& validateLogLogData(logLogResult)) {
|
|
|
result = pressureResult;
|
|
|
m_lastEvaluatedLogLogData = logLogResult;
|
|
|
DEBUG_OUT(QString("Got DLL solver result: %1 points").arg(result[0].size()));
|
|
|
m_consecutiveFailures = 0;
|
|
|
|
|
|
// 检查结果是否与之前不同。若连续粒子得到完全相同的压力曲线,
|
|
|
// 可能说明参数没有正确写入 DataManager,或求解器缓存/状态没有刷新。
|
|
|
static QVector<double> lastPressureResult;
|
|
|
bool isDifferentFromLast = false;
|
|
|
|
|
|
if(lastPressureResult.isEmpty() || lastPressureResult.size() != pressureResult[1].size()) {
|
|
|
isDifferentFromLast = true;
|
|
|
} else {
|
|
|
for(int i = 0; i < qMin(5, pressureResult[1].size()); ++i) {
|
|
|
if(qAbs(lastPressureResult[i] - pressureResult[1][i]) > 1e-12) {
|
|
|
isDifferentFromLast = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(isDifferentFromLast) {
|
|
|
DEBUG_OUT("RESULT VERIFICATION: Got NEW result data from DLL");
|
|
|
lastPressureResult = pressureResult[1];
|
|
|
} else {
|
|
|
DEBUG_OUT("!!! WARNING: Result data appears to be identical to previous run !!!");
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
DEBUG_OUT("DLL solver result is empty or invalid");
|
|
|
DEBUG_OUT(QString("Pressure result size: %1, Array sizes: %2, %3")
|
|
|
.arg(pressureResult.size())
|
|
|
.arg(pressureResult.size() > 0 ? pressureResult[0].size() : 0)
|
|
|
.arg(pressureResult.size() > 1 ? pressureResult[1].size() : 0));
|
|
|
m_consecutiveFailures++;
|
|
|
}
|
|
|
|
|
|
} catch(const std::bad_alloc& e) {
|
|
|
DEBUG_OUT(QString("Memory allocation failed in DLL solver: %1").arg(e.what()));
|
|
|
m_consecutiveFailures++;
|
|
|
} catch(const std::exception& e) {
|
|
|
DEBUG_OUT(QString("Exception in DLL solver: %1").arg(e.what()));
|
|
|
m_consecutiveFailures++;
|
|
|
} catch(...) {
|
|
|
DEBUG_OUT("Unknown exception in DLL solver");
|
|
|
m_consecutiveFailures++;
|
|
|
}
|
|
|
|
|
|
// 清理DLL任务
|
|
|
if(dllTask) {
|
|
|
if(dllTask->isRunning()) {
|
|
|
dllTask->terminate();
|
|
|
dllTask->wait(3000);
|
|
|
}
|
|
|
|
|
|
DEBUG_OUT("Cleaning up DLL solver task...");
|
|
|
delete dllTask;
|
|
|
dllTask = nullptr;
|
|
|
}
|
|
|
|
|
|
QApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
|
--m_evaluationInProgress;
|
|
|
|
|
|
DEBUG_OUT(QString("SOLVER DLL END - ResultPoints: %1")
|
|
|
.arg(result.isEmpty() ? 0 : result[0].size()));
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::runFinalFullSolver()
|
|
|
{
|
|
|
// 不设置目标井名,任务按原完整模式保存全部井和网格结果。
|
|
|
if(m_evaluationInProgress > 0) {
|
|
|
DEBUG_OUT("Cannot start final full-field solver while another evaluation is running");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
++m_evaluationInProgress;
|
|
|
nmCalculationDllPebiSolverTask dllTask(m_tempDirectory);
|
|
|
dllTask.start();
|
|
|
|
|
|
const int maxWait = 3600000;
|
|
|
const int checkInterval = 50;
|
|
|
QTime waitTimer;
|
|
|
waitTimer.start();
|
|
|
|
|
|
while(waitTimer.elapsed() < maxWait) {
|
|
|
if(dllTask.wait(checkInterval)) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 最终完整计算可能持续较长时间,此处需处理停止按钮事件。
|
|
|
QApplication::processEvents(QEventLoop::AllEvents, checkInterval);
|
|
|
|
|
|
if(!dllTask.isRunning()) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if(m_shouldStop) {
|
|
|
DEBUG_OUT("Final full-field solver terminated by user");
|
|
|
dllTask.terminate();
|
|
|
dllTask.wait(2000);
|
|
|
--m_evaluationInProgress;
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(dllTask.isRunning()) {
|
|
|
DEBUG_OUT("Final full-field solver timeout, terminating task");
|
|
|
dllTask.terminate();
|
|
|
dllTask.wait(2000);
|
|
|
--m_evaluationInProgress;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
dllTask.wait();
|
|
|
const bool succeeded = dllTask.wasSuccessful();
|
|
|
--m_evaluationInProgress;
|
|
|
return succeeded;
|
|
|
}
|
|
|
|
|
|
//QVector<QVector<double>> nmCalculationAutoFitPSO::runSolverExe()
|
|
|
//{
|
|
|
// DEBUG_OUT("SOLVER EXE START");
|
|
|
//
|
|
|
// if(m_evaluationInProgress > 0) {
|
|
|
// DEBUG_OUT("EXE Solver already running, skipping");
|
|
|
// return QVector<QVector<double>>();
|
|
|
// }
|
|
|
//
|
|
|
// ++m_evaluationInProgress;
|
|
|
// QVector<QVector<double>> result;
|
|
|
// nmCalculationExeSolverTask* exeTask = nullptr;
|
|
|
//
|
|
|
// try {
|
|
|
// DEBUG_OUT("Creating EXE solver task");
|
|
|
// exeTask = new nmCalculationExeSolverTask(QString());
|
|
|
//
|
|
|
// if(m_shouldStop) {
|
|
|
// DEBUG_OUT("Should stop - cleaning up and returning empty result");
|
|
|
// delete exeTask;
|
|
|
// --m_evaluationInProgress;
|
|
|
// return result;
|
|
|
// }
|
|
|
//
|
|
|
// DEBUG_OUT("Starting EXE solver execution...");
|
|
|
//
|
|
|
// // 启动
|
|
|
// bool executeSuccess = exeTask->execute();
|
|
|
//
|
|
|
// // 检查执行状态
|
|
|
// if(!executeSuccess) {
|
|
|
// DEBUG_OUT(QString("EXE solver execution failed: %1").arg(exeTask->getLastError()));
|
|
|
// DEBUG_OUT(QString("EXE solver exit code: %1").arg(exeTask->getExitCode()));
|
|
|
//
|
|
|
// // 清理并返回空结果
|
|
|
// delete exeTask;
|
|
|
// exeTask = nullptr;
|
|
|
// --m_evaluationInProgress;
|
|
|
// m_consecutiveFailures++;
|
|
|
// return result;
|
|
|
// }
|
|
|
//
|
|
|
// DEBUG_OUT("EXE solver execution completed successfully");
|
|
|
//
|
|
|
// // 验证结果数据是否已更新
|
|
|
// nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
// QVector<nmDataWellBase*> wells = dataManager->getWellDataList();
|
|
|
//
|
|
|
// if(wells.isEmpty()) {
|
|
|
// DEBUG_OUT("No wells found in data manager after EXE execution");
|
|
|
// delete exeTask;
|
|
|
// --m_evaluationInProgress;
|
|
|
// return result;
|
|
|
// }
|
|
|
//
|
|
|
// // 验证结果数据的时间戳或唯一性
|
|
|
// QVector<QVector<double>> pressureResult = wells[0]->getResultPressure();
|
|
|
// QVector<QVector<double>> logLogResult = wells[0]->getResultLogLog();
|
|
|
//
|
|
|
// DEBUG_OUT(QString("Raw result verification - Pressure arrays: %1, LogLog arrays: %2")
|
|
|
// .arg(pressureResult.size()).arg(logLogResult.size()));
|
|
|
//
|
|
|
// if(pressureResult.size() >= 2) {
|
|
|
// DEBUG_OUT(QString("Pressure result - Time points: %1, Pressure points: %2")
|
|
|
// .arg(pressureResult[0].size()).arg(pressureResult[1].size()));
|
|
|
//
|
|
|
// if(pressureResult[0].size() > 0) {
|
|
|
// DEBUG_OUT(QString("Sample pressure data - Time[0]: %1, Time[last]: %2, P[0]: %3, P[last]: %4")
|
|
|
// .arg(pressureResult[0][0])
|
|
|
// .arg(pressureResult[0][pressureResult[0].size()-1])
|
|
|
// .arg(pressureResult[1][0])
|
|
|
// .arg(pressureResult[1][pressureResult[1].size()-1]));
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// if(logLogResult.size() >= 3) {
|
|
|
// DEBUG_OUT(QString("LogLog result - X points: %1, Y1 points: %2, Y2 points: %3")
|
|
|
// .arg(logLogResult[0].size()).arg(logLogResult[1].size()).arg(logLogResult[2].size()));
|
|
|
//
|
|
|
// if(logLogResult[0].size() > 0) {
|
|
|
// DEBUG_OUT(QString("Sample LogLog data - X[0]: %1, X[last]: %2, Y1[0]: %3, Y2[0]: %4")
|
|
|
// .arg(logLogResult[0][0])
|
|
|
// .arg(logLogResult[0][logLogResult[0].size()-1])
|
|
|
// .arg(logLogResult[1][0])
|
|
|
// .arg(logLogResult[2][0]));
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// // 数据有效性检查
|
|
|
// if(pressureResult.size() >= 2 && pressureResult[0].size() > 0 && pressureResult[1].size() > 0) {
|
|
|
// result = pressureResult;
|
|
|
// DEBUG_OUT(QString("Got EXE solver result: %1 points").arg(result[0].size()));
|
|
|
// m_consecutiveFailures = 0;
|
|
|
//
|
|
|
// // 检查结果是否与之前不同
|
|
|
// static QVector<double> lastPressureResult;
|
|
|
// bool isDifferentFromLast = false;
|
|
|
//
|
|
|
// if(lastPressureResult.isEmpty() || lastPressureResult.size() != pressureResult[1].size()) {
|
|
|
// isDifferentFromLast = true;
|
|
|
// } else {
|
|
|
// for(int i = 0; i < qMin(5, pressureResult[1].size()); ++i) {
|
|
|
// if(qAbs(lastPressureResult[i] - pressureResult[1][i]) > 1e-12) {
|
|
|
// isDifferentFromLast = true;
|
|
|
// break;
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// if(isDifferentFromLast) {
|
|
|
// DEBUG_OUT("RESULT VERIFICATION: Got NEW result data from EXE");
|
|
|
// lastPressureResult = pressureResult[1];
|
|
|
// } else {
|
|
|
// DEBUG_OUT("!!! WARNING: Result data appears to be identical to previous run !!!");
|
|
|
// }
|
|
|
//
|
|
|
// } else {
|
|
|
// DEBUG_OUT("EXE solver result is empty or invalid");
|
|
|
// DEBUG_OUT(QString("Pressure result size: %1, Array sizes: %2, %3")
|
|
|
// .arg(pressureResult.size())
|
|
|
// .arg(pressureResult.size() > 0 ? pressureResult[0].size() : 0)
|
|
|
// .arg(pressureResult.size() > 1 ? pressureResult[1].size() : 0));
|
|
|
// m_consecutiveFailures++;
|
|
|
// }
|
|
|
//
|
|
|
// } catch(const std::exception& e) {
|
|
|
// DEBUG_OUT(QString("Exception in EXE solver: %1").arg(e.what()));
|
|
|
// m_consecutiveFailures++;
|
|
|
// } catch(...) {
|
|
|
// DEBUG_OUT("Unknown exception in EXE solver");
|
|
|
// m_consecutiveFailures++;
|
|
|
// }
|
|
|
//
|
|
|
// // 清理EXE任务
|
|
|
// if(exeTask) {
|
|
|
// DEBUG_OUT("Cleaning up EXE solver task...");
|
|
|
// delete exeTask;
|
|
|
// exeTask = nullptr;
|
|
|
// }
|
|
|
//
|
|
|
// QApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
|
// --m_evaluationInProgress;
|
|
|
//
|
|
|
// DEBUG_OUT(QString("SOLVER EXE END - ResultPoints: %1")
|
|
|
// .arg(result.isEmpty() ? 0 : result[0].size()));
|
|
|
//
|
|
|
// return result;
|
|
|
//}
|
|
|
|
|
|
StopReasonPSO nmCalculationAutoFitPSO::analyzeOptimizationStatus()
|
|
|
{
|
|
|
// 按优先级判断本轮是否停止。
|
|
|
// 目标达成和最大迭代是硬条件;真收敛/局部最优需要足够历史数据支撑。
|
|
|
// 返回值只描述原因,真正的日志和 UI 收尾在 startAutoFitting() 中处理。
|
|
|
|
|
|
// 1. 检查用户停止
|
|
|
if(m_shouldStop) {
|
|
|
return PSO_USER_STOPPED;
|
|
|
}
|
|
|
|
|
|
// 2. 检查连续失败
|
|
|
if(m_consecutiveFailedIterations >= m_maxConsecutiveFailures) {
|
|
|
return PSO_CONSECUTIVE_FAILURES;
|
|
|
}
|
|
|
|
|
|
// 3. 检查是否达到目标精度
|
|
|
if(m_globalBestFitness < m_targetError) {
|
|
|
return PSO_TARGET_ACHIEVED;
|
|
|
}
|
|
|
|
|
|
// 4. 检查是否达到最大迭代数
|
|
|
if(m_currentIteration >= m_maxIterations - 1) {
|
|
|
return PSO_MAX_ITERATIONS;
|
|
|
}
|
|
|
|
|
|
// 5. 需要足够的历史数据才能判断收敛
|
|
|
if(m_convergenceHistory.size() < m_localOptimumWindow) {
|
|
|
return PSO_CONTINUE_OPTIMIZATION;
|
|
|
}
|
|
|
|
|
|
// 6. 检查真正的收敛
|
|
|
if(m_convergenceHistory.size() >= m_trueConvergenceWindow && checkTrueConvergence()) {
|
|
|
return PSO_TRUE_CONVERGENCE;
|
|
|
}
|
|
|
|
|
|
// 7. 检查局部最优陷阱
|
|
|
if(checkLocalOptimumTrap()) {
|
|
|
return PSO_LOCAL_OPTIMUM;
|
|
|
}
|
|
|
|
|
|
return PSO_CONTINUE_OPTIMIZATION;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::checkTrueConvergence() const
|
|
|
{
|
|
|
// 真收敛判断:不是只看“最近没有改进”,而是同时看误差稳定性、粒子群多样性、
|
|
|
// 平均速度、长期改进和粒子停滞率。
|
|
|
// 这样可以减少把局部卡住误判成正常收敛的概率。
|
|
|
if(m_convergenceHistory.size() < m_trueConvergenceWindow) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 1. 检查适应度稳定性 - 长期小幅波动
|
|
|
double recentVariance = calculateFitnessVariance(10);
|
|
|
double recentMean = 0.0;
|
|
|
int windowSize = qMin(10, m_convergenceHistory.size());
|
|
|
|
|
|
// 计算最近窗口的均值
|
|
|
for(int i = m_convergenceHistory.size() - windowSize; i < m_convergenceHistory.size(); ++i) {
|
|
|
recentMean += m_convergenceHistory[i];
|
|
|
}
|
|
|
|
|
|
recentMean /= windowSize;
|
|
|
|
|
|
double relativeVariance = recentVariance / qMax(1e-10, recentMean * recentMean);
|
|
|
bool stableError = (relativeVariance < m_convergenceVarianceThreshold);
|
|
|
|
|
|
// 2. 检查粒子群多样性 - 应该收敛到同一区域
|
|
|
double currentDiversity = calculateSwarmDiversity();
|
|
|
bool lowDiversity = (currentDiversity < m_diversityThreshold);
|
|
|
|
|
|
// 3. 检查速度收敛 - 粒子应该几乎停止移动
|
|
|
double avgVelocity = calculateAverageVelocity();
|
|
|
bool velocityConverged = (avgVelocity < m_velocityConvergenceThreshold);
|
|
|
|
|
|
// 4. 检查长期改进趋势
|
|
|
double longTermImprovement = calculateLongTermImprovement(m_trueConvergenceWindow);
|
|
|
bool minimalLongTermImprovement = (longTermImprovement < 1e-4); // 0.01%
|
|
|
|
|
|
// 5. 检查粒子停滞情况
|
|
|
double stagnationRate = calculateParticleStagnationRate();
|
|
|
bool mostParticlesConverged = (stagnationRate > 0.8); // 80%以上粒子收敛
|
|
|
|
|
|
// 真收敛的判断条件
|
|
|
bool isConverged = stableError && lowDiversity && velocityConverged &&
|
|
|
minimalLongTermImprovement && mostParticlesConverged;
|
|
|
|
|
|
if(isConverged) {
|
|
|
DEBUG_OUT("=== TRUE CONVERGENCE ANALYSIS ===");
|
|
|
DEBUG_OUT(QString("stableError=%1 (relativeVariance=%2)")
|
|
|
.arg(stableError).arg(relativeVariance, 0, 'e', 6));
|
|
|
DEBUG_OUT(QString("lowDiversity=%1 (diversity=%2, threshold=%3)")
|
|
|
.arg(lowDiversity).arg(currentDiversity, 0, 'f', 6).arg(m_diversityThreshold));
|
|
|
DEBUG_OUT(QString("velocityConverged=%1 (avgVel=%2)")
|
|
|
.arg(velocityConverged).arg(avgVelocity, 0, 'f', 6));
|
|
|
DEBUG_OUT(QString("longTermImprovement=%1%, stagnationRate=%2%")
|
|
|
.arg(longTermImprovement * 100, 0, 'f', 4).arg(stagnationRate * 100, 0, 'f', 1));
|
|
|
}
|
|
|
|
|
|
return isConverged;
|
|
|
}
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::checkLocalOptimumTrap() const
|
|
|
{
|
|
|
// 局部最优判断:当误差离目标还远,但短期几乎不改进,
|
|
|
// 且粒子群过度聚集/异常分散或大部分粒子停滞时,认为可能卡住。
|
|
|
if(m_convergenceHistory.size() < m_localOptimumWindow) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 1. 检查解质量 - 如果距离目标还很远,停滞就可能是局部最优
|
|
|
bool farFromTarget = (m_globalBestFitness > m_targetError * m_farTargetFactor);
|
|
|
|
|
|
// 2. 检查短期改进 - 近期改进非常小
|
|
|
double shortTermImprovement = calculateLongTermImprovement(m_localOptimumWindow);
|
|
|
bool poorShortTermImprovement = (shortTermImprovement < 1e-5); // 0.001%
|
|
|
|
|
|
// 3. 检查粒子多样性 - 可能过早聚集或无效分散
|
|
|
double currentDiversity = calculateSwarmDiversity();
|
|
|
bool problematicDiversity = (currentDiversity < m_diversityThreshold * 0.1) ||
|
|
|
(currentDiversity > m_diversityThreshold * 5.0);
|
|
|
|
|
|
// 4. 检查粒子停滞率
|
|
|
double stagnationRate = calculateParticleStagnationRate();
|
|
|
bool mostParticlesStagnant = (stagnationRate > 0.9); // 90%以上停滞但解质量差
|
|
|
|
|
|
// 5. 检查适应度方差 - 可能卡在平坦区域
|
|
|
double fitnessVariance = calculateFitnessVariance(m_localOptimumWindow);
|
|
|
bool flatFitnessLandscape = (fitnessVariance < m_convergenceVarianceThreshold * 0.1);
|
|
|
|
|
|
// 局部最优的判断条件
|
|
|
bool isLocalOptimum = farFromTarget && poorShortTermImprovement &&
|
|
|
(problematicDiversity || (mostParticlesStagnant && flatFitnessLandscape));
|
|
|
|
|
|
if(isLocalOptimum) {
|
|
|
DEBUG_OUT("=== LOCAL OPTIMUM ANALYSIS ===");
|
|
|
DEBUG_OUT(QString("farFromTarget=%1 (error=%2, target*factor=%3)")
|
|
|
.arg(farFromTarget).arg(m_globalBestFitness, 0, 'e', 4)
|
|
|
.arg(m_targetError * m_farTargetFactor, 0, 'e', 4));
|
|
|
DEBUG_OUT(QString("poorShortTermImprovement=%1 (improvement=%2%)")
|
|
|
.arg(poorShortTermImprovement).arg(shortTermImprovement * 100, 0, 'f', 4));
|
|
|
DEBUG_OUT(QString("problematicDiversity=%1 (diversity=%2)")
|
|
|
.arg(problematicDiversity).arg(currentDiversity, 0, 'f', 6));
|
|
|
DEBUG_OUT(QString("stagnationRate=%1%, errorVariance=%2")
|
|
|
.arg(stagnationRate * 100, 0, 'f', 1).arg(fitnessVariance, 0, 'e', 6));
|
|
|
}
|
|
|
|
|
|
return isLocalOptimum;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateSwarmDiversity() const
|
|
|
{
|
|
|
// 粒子群多样性:逐维计算粒子位置标准差,再按该参数搜索范围归一化。
|
|
|
// 返回值越小,说明粒子越集中;越大,说明搜索仍然分散。
|
|
|
if(m_swarm.size() < 2) return 0.0;
|
|
|
|
|
|
int dimensions = getEnabledParameterCount();
|
|
|
|
|
|
if(dimensions == 0) return 0.0;
|
|
|
|
|
|
double totalDiversity = 0.0;
|
|
|
|
|
|
for(int dim = 0; dim < dimensions; ++dim) {
|
|
|
// 计算该维度上所有粒子的均值
|
|
|
double mean = 0.0;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
mean += m_swarm[i].position[dim];
|
|
|
}
|
|
|
|
|
|
mean /= m_swarm.size();
|
|
|
|
|
|
// 计算该维度上的方差
|
|
|
double variance = 0.0;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
double diff = m_swarm[i].position[dim] - mean;
|
|
|
variance += diff * diff;
|
|
|
}
|
|
|
|
|
|
variance /= m_swarm.size();
|
|
|
|
|
|
// 归一化到参数范围
|
|
|
int paramIndex = m_enabledParamIndices[dim];
|
|
|
double range = m_parameterUpper[paramIndex] - m_parameterLower[paramIndex];
|
|
|
double normalizedStd = sqrt(variance) / qMax(1e-10, range);
|
|
|
|
|
|
totalDiversity += normalizedStd;
|
|
|
}
|
|
|
|
|
|
return totalDiversity / dimensions;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateAverageVelocity() const
|
|
|
{
|
|
|
// 平均速度:逐维速度绝对值按参数范围归一化。
|
|
|
// 收敛阶段速度应逐渐变小。
|
|
|
if(m_swarm.isEmpty()) return 0.0;
|
|
|
|
|
|
int dimensions = getEnabledParameterCount();
|
|
|
|
|
|
if(dimensions == 0) return 0.0;
|
|
|
|
|
|
double totalVelocity = 0.0;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
for(int dim = 0; dim < dimensions; ++dim) {
|
|
|
int paramIndex = m_enabledParamIndices[dim];
|
|
|
double range = m_parameterUpper[paramIndex] - m_parameterLower[paramIndex];
|
|
|
double normalizedVel = qAbs(m_swarm[i].velocity[dim]) / qMax(1e-10, range);
|
|
|
totalVelocity += normalizedVel;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return totalVelocity / (static_cast<double>(m_swarm.size()) * dimensions);
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateParticleStagnationRate() const
|
|
|
{
|
|
|
// 粒子停滞率:只统计本代经过真实求解器评价的粒子。
|
|
|
// 被代理筛掉的粒子保留的是旧适应度,不能据此判断本代是否停滞。
|
|
|
if(m_swarm.isEmpty()) return 0.0;
|
|
|
|
|
|
int stagnantParticles = 0;
|
|
|
int evaluatedParticles = 0;
|
|
|
|
|
|
for(int i = 0; i < m_swarm.size(); ++i) {
|
|
|
if(!m_swarm[i].evaluatedThisIteration || !m_swarm[i].lastEvaluationSuccess) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
evaluatedParticles++;
|
|
|
|
|
|
// updateParticle() 会在计算该字段后才覆盖 pbest,因此“刚改善的粒子”
|
|
|
// 不会再被错误地当作停滞。
|
|
|
if(m_swarm[i].pbestRelativeImprovementThisIteration <= 1e-6) { // 改进不超过0.0001%
|
|
|
stagnantParticles++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return evaluatedParticles > 0
|
|
|
? static_cast<double>(stagnantParticles) / evaluatedParticles
|
|
|
: 0.0;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateFitnessVariance(int windowSize) const
|
|
|
{
|
|
|
// 最近 windowSize 代全局最优误差的方差。
|
|
|
// 方差越小,说明全局最优曲线越稳定。
|
|
|
if(m_convergenceHistory.size() < windowSize) {
|
|
|
return 1e10; // 数据不足,返回大值
|
|
|
}
|
|
|
|
|
|
// 计算最近windowSize次迭代的方差
|
|
|
double mean = 0.0;
|
|
|
int startIdx = m_convergenceHistory.size() - windowSize;
|
|
|
|
|
|
for(int i = startIdx; i < m_convergenceHistory.size(); ++i) {
|
|
|
mean += m_convergenceHistory[i];
|
|
|
}
|
|
|
|
|
|
mean /= windowSize;
|
|
|
|
|
|
double variance = 0.0;
|
|
|
|
|
|
for(int i = startIdx; i < m_convergenceHistory.size(); ++i) {
|
|
|
double diff = m_convergenceHistory[i] - mean;
|
|
|
variance += diff * diff;
|
|
|
}
|
|
|
|
|
|
variance /= windowSize;
|
|
|
|
|
|
return variance;
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateLongTermImprovement(int windowSize) const
|
|
|
{
|
|
|
// 最近 windowSize 代相对改进幅度。
|
|
|
// 数据不足时返回 1.0,表示“还不能认为没有改进”。
|
|
|
if(m_convergenceHistory.size() < windowSize) {
|
|
|
return 1.0; // 数据不足,假设有改进
|
|
|
}
|
|
|
|
|
|
double oldFitness = m_convergenceHistory[m_convergenceHistory.size() - windowSize];
|
|
|
double improvement = (oldFitness - m_globalBestFitness) / qMax(1e-10, qAbs(oldFitness));
|
|
|
|
|
|
return improvement;
|
|
|
}
|
|
|
|
|
|
QString nmCalculationAutoFitPSO::getStopReasonDescription(StopReasonPSO reason) const
|
|
|
{
|
|
|
// 将停止枚举转成人类可读文本,用于日志和运行摘要。
|
|
|
switch(reason) {
|
|
|
case PSO_TARGET_ACHIEVED:
|
|
|
return tr("Target error achieved");
|
|
|
|
|
|
case PSO_TRUE_CONVERGENCE:
|
|
|
return tr("Algorithm converged to stable solution");
|
|
|
|
|
|
case PSO_LOCAL_OPTIMUM:
|
|
|
return tr("Local optimum detected");
|
|
|
|
|
|
case PSO_MAX_ITERATIONS:
|
|
|
return tr("Maximum iterations reached");
|
|
|
|
|
|
case PSO_USER_STOPPED:
|
|
|
return tr("Stopped by user request");
|
|
|
|
|
|
case PSO_CONSECUTIVE_FAILURES:
|
|
|
return tr("Too many consecutive failures");
|
|
|
|
|
|
case PSO_OPTIMIZATION_FAILED:
|
|
|
return tr("Optimization failed");
|
|
|
|
|
|
default:
|
|
|
return tr("Unknown reason");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::updateConvergenceMetrics()
|
|
|
{
|
|
|
// 每代结束后缓存收敛辅助指标,只保留最近 50 个点,避免长时间运行无限增长。
|
|
|
|
|
|
// 更新多样性历史
|
|
|
m_diversityHistory.append(calculateSwarmDiversity());
|
|
|
|
|
|
// 更新速度历史
|
|
|
m_velocityHistory.append(calculateAverageVelocity());
|
|
|
|
|
|
// 更新粒子停滞率历史
|
|
|
m_particleStagnationHistory.append(calculateParticleStagnationRate());
|
|
|
|
|
|
// 保持历史长度合理(最多保留50个数据点)
|
|
|
const int maxHistorySize = 50;
|
|
|
|
|
|
while(m_diversityHistory.size() > maxHistorySize) {
|
|
|
m_diversityHistory.remove(0);
|
|
|
}
|
|
|
|
|
|
while(m_velocityHistory.size() > maxHistorySize) {
|
|
|
m_velocityHistory.remove(0);
|
|
|
}
|
|
|
|
|
|
while(m_particleStagnationHistory.size() > maxHistorySize) {
|
|
|
m_particleStagnationHistory.remove(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::setSimulationMode(bool enabled)
|
|
|
{
|
|
|
// 仿真模式开关。它用于 UI 演示/调试进度和日志,不调用真实 DLL 求解器。
|
|
|
// 正式自动拟合流程通常保持 false。
|
|
|
m_simulationMode = enabled;
|
|
|
DEBUG_OUT(QString("Simulation mode %1").arg(enabled ? "enabled" : "disabled"));
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::setSimulationTargetParams(const QVector<double>& targetParams, double targetError)
|
|
|
{
|
|
|
// 设置仿真模式的“目标参数”和“目标误差”。
|
|
|
// 仿真过程会把参数和误差平滑地从初始值过渡到这里。
|
|
|
m_simulationTargetParams = targetParams;
|
|
|
m_simulationTargetError = targetError;
|
|
|
DEBUG_OUT(QString("Simulation target params set: %1 parameters, target error: %2")
|
|
|
.arg(targetParams.size()).arg(targetError, 0, 'e', 4));
|
|
|
}
|
|
|
|
|
|
QVector<QVector<double> > nmCalculationAutoFitPSO::buildSimulatedLogLogData(double progress) const
|
|
|
{
|
|
|
// 为仿真模式生成一条看起来逐渐贴近目标曲线的 log-log 曲线。
|
|
|
// progress=0 表示偏离目标较多,progress=1 基本等于目标曲线。
|
|
|
QVector<QVector<double> > simulated;
|
|
|
|
|
|
if(m_targetLogLogData.size() < 3) {
|
|
|
return simulated;
|
|
|
}
|
|
|
|
|
|
int count = qMin(m_targetLogLogData[0].size(), qMin(m_targetLogLogData[1].size(), m_targetLogLogData[2].size()));
|
|
|
simulated.resize(3);
|
|
|
simulated[0].reserve(count);
|
|
|
simulated[1].reserve(count);
|
|
|
simulated[2].reserve(count);
|
|
|
|
|
|
double clampedProgress = qMax(0.0, qMin(1.0, progress));
|
|
|
double pressureOffset = 0.35 * (1.0 - clampedProgress);
|
|
|
double derivativeOffset = 0.28 * (1.0 - clampedProgress);
|
|
|
|
|
|
for(int i = 0; i < count; ++i) {
|
|
|
double x = m_targetLogLogData[0][i];
|
|
|
double y1 = m_targetLogLogData[1][i];
|
|
|
double y2 = m_targetLogLogData[2][i];
|
|
|
|
|
|
if(!isFiniteNumber(x) || !isFiniteNumber(y1) || !isFiniteNumber(y2) || x <= 0.0 || y1 <= 0.0 || y2 <= 0.0) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
double localWave = 1.0 + 0.04 * (1.0 - clampedProgress) * qSin(i * 0.17);
|
|
|
simulated[0].append(x);
|
|
|
simulated[1].append(qMax(1e-12, y1 * (1.0 + pressureOffset) * localWave));
|
|
|
simulated[2].append(qMax(1e-12, y2 * (1.0 - derivativeOffset) / localWave));
|
|
|
}
|
|
|
|
|
|
return simulated;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::runSimulatedFitting()
|
|
|
{
|
|
|
// 仿真模式入口:模拟完整 PSO 日志、进度、bestCurveUpdated 信号和最终写回。
|
|
|
// 它不评价粒子、不调用代理模型、不调用 DLL,只用于界面联调或演示自动拟合流程。
|
|
|
DEBUG_OUT("=== SIMULATED PSO AUTO FITTING START ===");
|
|
|
m_isRunning = true;
|
|
|
m_shouldStop = false;
|
|
|
m_simulationIteration = 0;
|
|
|
m_simulationMaxIterations = m_maxIterations;
|
|
|
m_simulationCurrentParticle = 0;
|
|
|
m_simulationSuccessCount = 0;
|
|
|
m_simulationFailCount = 0;
|
|
|
m_simulationIterationStarted = false;
|
|
|
|
|
|
// 设置起始误差
|
|
|
m_simulationStartError = 0.7324;
|
|
|
m_simulationCurrentError = m_simulationStartError;
|
|
|
m_simulationPreviousIterError = m_simulationStartError;
|
|
|
|
|
|
int enabledParams = getEnabledParameterCount();
|
|
|
|
|
|
// 从界面提取的初始值
|
|
|
m_simulationStartParams = m_initialValues;
|
|
|
|
|
|
m_globalBestPosition = m_simulationStartParams;
|
|
|
m_globalBestFitness = m_simulationStartError;
|
|
|
m_globalBestLogLogData = buildSimulatedLogLogData(0.0);
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, 0, m_globalBestFitness);
|
|
|
|
|
|
// 发送初始化日志
|
|
|
emit logMessageGenerated(tr("=== PSO Automatic Fitting Started ==="));
|
|
|
emit logMessageGenerated(tr("Algorithm: Particle Swarm Optimization"));
|
|
|
emit logMessageGenerated(tr("Enabled parameters count: %1").arg(enabledParams));
|
|
|
emit logMessageGenerated(tr("Target data validation passed (%1 data points)")
|
|
|
.arg(m_targetLogLogData.isEmpty() ? 156 : m_targetLogLogData[0].size()));
|
|
|
|
|
|
emit logMessageGenerated(tr("=== Evaluating Initial Solution (Elite Protection) ==="));
|
|
|
|
|
|
QString paramStr = tr("Initial parameters: ");
|
|
|
|
|
|
for(int i = 0; i < m_simulationStartParams.size(); ++i) {
|
|
|
paramStr += QString("[%1]=%2 ").arg(i).arg(m_simulationStartParams[i], 0, 'f', 6);
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(paramStr);
|
|
|
|
|
|
emit logMessageGenerated(tr("Starting initial solution evaluation..."));
|
|
|
|
|
|
QApplication::processEvents();
|
|
|
msleep(500);
|
|
|
|
|
|
emit logMessageGenerated(tr("evaluateError returned: %1").arg(m_simulationStartError, 0, 'e', 10));
|
|
|
emit logMessageGenerated(tr("Taking SUCCESS branch (Error < 1e9)"));
|
|
|
emit logMessageGenerated(tr("Initial solution evaluation successful"));
|
|
|
emit logMessageGenerated(tr("Initial Error: %1").arg(m_simulationStartError, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Elite protection activated - initial solution will be preserved if no significant improvement found"));
|
|
|
|
|
|
emit logMessageGenerated(tr("Swarm initialized: %1 particles, %2 dimensions").arg(m_swarmSize).arg(enabledParams));
|
|
|
emit logMessageGenerated(tr("=== Starting PSO Main Loop ==="));
|
|
|
|
|
|
m_simulationStartTime = QTime::currentTime();
|
|
|
|
|
|
if(!m_simulationTimer) {
|
|
|
m_simulationTimer = new QTimer(this);
|
|
|
connect(m_simulationTimer, SIGNAL(timeout()), this, SLOT(onSimulationTimerTick()));
|
|
|
}
|
|
|
|
|
|
int intervalMs = m_simulationLogInterval * 1000;
|
|
|
m_simulationTimer->start(intervalMs);
|
|
|
|
|
|
DEBUG_OUT(QString("Simulation timer started with interval: %1 ms").arg(intervalMs));
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::onSimulationTimerTick()
|
|
|
{
|
|
|
// 仿真模式用 QTimer 分步输出日志,避免一次性刷完,效果更接近真实长任务。
|
|
|
|
|
|
// 检查是否需要停止
|
|
|
if(m_shouldStop) {
|
|
|
m_simulationTimer->stop();
|
|
|
|
|
|
emit logMessageGenerated(tr("=== User Stop Request Received ==="));
|
|
|
emit logMessageGenerated(tr("Gracefully stopping PSO optimization..."));
|
|
|
emit logMessageGenerated(tr("PSO optimization stop request processed"));
|
|
|
|
|
|
m_globalBestPosition = calculateSimulatedParams(m_simulationIteration);
|
|
|
m_globalBestFitness = m_simulationCurrentError;
|
|
|
|
|
|
m_isRunning = false;
|
|
|
|
|
|
QString message = QString(tr("Stopped by user. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_simulationIteration);
|
|
|
|
|
|
emit logMessageGenerated(tr("=== PSO OPTIMIZATION STOPPED BY USER ==="));
|
|
|
emit progressUpdated(m_simulationMaxIterations, m_globalBestFitness);
|
|
|
|
|
|
QApplication::processEvents();
|
|
|
msleep(200);
|
|
|
|
|
|
emit fittingFinished(true, message);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 检查是否完成所有迭代
|
|
|
if(m_simulationIteration >= m_simulationMaxIterations) {
|
|
|
m_simulationTimer->stop();
|
|
|
finishSimulation();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 检查是否需要开始新的迭代
|
|
|
if(!m_simulationIterationStarted) {
|
|
|
startNewSimulationIteration();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 处理当前粒子
|
|
|
if(m_simulationCurrentParticle < m_swarmSize) {
|
|
|
emitParticleLog(m_simulationCurrentParticle);
|
|
|
m_simulationCurrentParticle++;
|
|
|
|
|
|
// 每处理2个粒子,处理一次事件
|
|
|
if(m_simulationCurrentParticle % 2 == 0) {
|
|
|
QApplication::processEvents();
|
|
|
}
|
|
|
} else {
|
|
|
// 当前迭代的所有粒子都处理完了,输出迭代统计
|
|
|
finishCurrentIteration();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::emitSimulationLog(int iteration)
|
|
|
{
|
|
|
// 旧版仿真日志函数:一次性输出某一代的模拟粒子情况。
|
|
|
// 当前 runSimulatedFitting() 主要通过 startNewSimulationIteration()/emitParticleLog()
|
|
|
// 分步输出,但保留本函数便于调试或恢复旧演示节奏。
|
|
|
m_simulationCurrentError = calculateSimulatedError(iteration);
|
|
|
|
|
|
if(m_simulationCurrentError < m_globalBestFitness) {
|
|
|
QVector<double> currentParams = calculateSimulatedParams(iteration);
|
|
|
m_globalBestFitness = m_simulationCurrentError;
|
|
|
m_globalBestPosition = currentParams;
|
|
|
}
|
|
|
|
|
|
bool shouldOutputDetail = (iteration % qMax(1, m_simulationMaxIterations / 10) == 0) ||
|
|
|
(iteration <= 5) ||
|
|
|
(iteration >= m_simulationMaxIterations - 2);
|
|
|
|
|
|
emit logMessageGenerated(tr("--- Iteration %1/%2 ---").arg(iteration).arg(m_simulationMaxIterations));
|
|
|
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr("Current best error: %1").arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Total evaluations: %1 (successful: %2, failures: %3)")
|
|
|
.arg(iteration * m_swarmSize)
|
|
|
.arg(iteration * m_swarmSize)
|
|
|
.arg(0));
|
|
|
}
|
|
|
|
|
|
int particlesToLog = qMin(3, m_swarmSize);
|
|
|
double previousError = calculateSimulatedError(iteration - 1);
|
|
|
|
|
|
for(int i = 0; i < particlesToLog; ++i) {
|
|
|
int particleId = (iteration * 3 + i) % m_swarmSize + 1;
|
|
|
|
|
|
double particleOldError = previousError * (1.0 + 0.05 * (qrand() % 100) / 100.0);
|
|
|
double particleNewError = m_simulationCurrentError * (1.0 + 0.03 * (qrand() % 100) / 100.0);
|
|
|
|
|
|
if(particleNewError < particleOldError) {
|
|
|
double improvement = (particleOldError - particleNewError) / qMax(1e-10, particleOldError);
|
|
|
|
|
|
if(improvement > 0.01) {
|
|
|
emit logMessageGenerated(tr(" Particle %1 improved: %2 -> %3 (%4% improvement)")
|
|
|
.arg(particleId)
|
|
|
.arg(particleOldError, 0, 'e', 3)
|
|
|
.arg(particleNewError, 0, 'e', 3)
|
|
|
.arg(improvement * 100, 0, 'f', 2));
|
|
|
} else {
|
|
|
emit logMessageGenerated(tr(" Particle %1: minor improvement %2% (< 1% threshold)")
|
|
|
.arg(particleId).arg(improvement * 100, 0, 'f', 3));
|
|
|
}
|
|
|
} else {
|
|
|
emit logMessageGenerated(tr(" Particle %1: no improvement (Error: %2)")
|
|
|
.arg(particleId).arg(particleNewError, 0, 'e', 3));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int successCount = m_swarmSize - (qrand() % 2);
|
|
|
int failCount = m_swarmSize - successCount;
|
|
|
double successRate = (double)successCount / m_swarmSize * 100;
|
|
|
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr("Current iteration stats: %1 successful, %2 failed out of %3 particles (success rate: %4%)")
|
|
|
.arg(successCount).arg(failCount).arg(m_swarmSize).arg(successRate, 0, 'f', 1));
|
|
|
}
|
|
|
|
|
|
if(iteration % 5 == 0 && iteration > 0) {
|
|
|
double recentImprovement = (previousError - m_simulationCurrentError) / qMax(1e-10, previousError);
|
|
|
emit logMessageGenerated(tr(" Convergence check: recent improvement = %1% over last 5 iterations")
|
|
|
.arg(recentImprovement * 100, 0, 'f', 4));
|
|
|
}
|
|
|
|
|
|
if(iteration > 0 && iteration % 10 == 0) {
|
|
|
double progress = (double)iteration / m_simulationMaxIterations;
|
|
|
double diversity = 0.15 * (1.0 - progress * 0.8) + 0.01;
|
|
|
double avgVel = 0.08 * (1.0 - progress * 0.9) + 0.005;
|
|
|
double stagnation = 20.0 + progress * 60.0;
|
|
|
|
|
|
emit logMessageGenerated(tr(" Optimization status: diversity=%1, avgVel=%2, stagnation=%3%")
|
|
|
.arg(diversity, 0, 'f', 4)
|
|
|
.arg(avgVel, 0, 'f', 4)
|
|
|
.arg(stagnation, 0, 'f', 1));
|
|
|
}
|
|
|
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr(" Iteration %1 completed - Current best: %2")
|
|
|
.arg(iteration).arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double nmCalculationAutoFitPSO::calculateSimulatedError(int iteration)
|
|
|
{
|
|
|
// 生成随迭代递减的模拟误差,并叠加少量随机扰动。
|
|
|
// 使用非线性 decay,让前期下降较快、后期逐渐变慢,更像真实优化曲线。
|
|
|
if(iteration <= 0) return m_simulationStartError;
|
|
|
|
|
|
if(iteration >= m_simulationMaxIterations) return m_simulationTargetError;
|
|
|
|
|
|
double progress = (double)iteration / m_simulationMaxIterations;
|
|
|
double decayFactor = 1.0 - pow(progress, 0.6);
|
|
|
|
|
|
double error = m_simulationTargetError +
|
|
|
(m_simulationStartError - m_simulationTargetError) * decayFactor;
|
|
|
|
|
|
double noise = (qrand() % 1000 - 500) / 500000.0;
|
|
|
error = error * (1.0 + noise);
|
|
|
error = qMax(m_simulationTargetError, error);
|
|
|
|
|
|
return error;
|
|
|
}
|
|
|
|
|
|
QVector<double> nmCalculationAutoFitPSO::calculateSimulatedParams(int iteration)
|
|
|
{
|
|
|
// 生成随迭代从初始参数平滑靠近目标参数的模拟参数。
|
|
|
// 返回向量仍然是“启用参数向量”,可直接传给 applyParametersToDataManager()。
|
|
|
QVector<double> currentParams;
|
|
|
|
|
|
if(iteration <= 0) return m_simulationStartParams;
|
|
|
|
|
|
if(iteration >= m_simulationMaxIterations) return m_simulationTargetParams;
|
|
|
|
|
|
double progress = (double)iteration / m_simulationMaxIterations;
|
|
|
double blendFactor = 1.0 - pow(1.0 - progress, 0.6);
|
|
|
|
|
|
for(int i = 0; i < m_simulationStartParams.size() && i < m_simulationTargetParams.size(); ++i) {
|
|
|
double startVal = m_simulationStartParams[i];
|
|
|
double targetVal = m_simulationTargetParams[i];
|
|
|
|
|
|
double currentVal = startVal + (targetVal - startVal) * blendFactor;
|
|
|
|
|
|
double noise = (qrand() % 1000 - 500) / 50000.0;
|
|
|
currentVal = currentVal * (1.0 + noise);
|
|
|
|
|
|
currentParams.append(currentVal);
|
|
|
}
|
|
|
|
|
|
return currentParams;
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::startNewSimulationIteration()
|
|
|
{
|
|
|
// 开始仿真模式的一代:更新误差/最优曲线,输出该代标题和累计评价数。
|
|
|
m_simulationIteration++;
|
|
|
m_simulationCurrentParticle = 0;
|
|
|
m_simulationSuccessCount = 0;
|
|
|
m_simulationFailCount = 0;
|
|
|
m_simulationIterationStarted = true;
|
|
|
|
|
|
// 保存上一次迭代的误差
|
|
|
m_simulationPreviousIterError = m_simulationCurrentError;
|
|
|
|
|
|
// 计算当前迭代的目标误差
|
|
|
m_simulationCurrentError = calculateSimulatedError(m_simulationIteration);
|
|
|
|
|
|
if(m_simulationCurrentError < m_globalBestFitness) {
|
|
|
QVector<double> currentParams = calculateSimulatedParams(m_simulationIteration);
|
|
|
m_globalBestFitness = m_simulationCurrentError;
|
|
|
m_globalBestPosition = currentParams;
|
|
|
double progress = (double)m_simulationIteration / qMax(1, m_simulationMaxIterations);
|
|
|
m_globalBestLogLogData = buildSimulatedLogLogData(progress);
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, m_simulationIteration, m_globalBestFitness);
|
|
|
}
|
|
|
|
|
|
bool shouldOutputDetail = (m_simulationIteration % qMax(1, m_simulationMaxIterations / 10) == 0) ||
|
|
|
(m_simulationIteration <= 5) ||
|
|
|
(m_simulationIteration >= m_simulationMaxIterations - 2);
|
|
|
|
|
|
// 输出迭代标题
|
|
|
emit logMessageGenerated(tr("--- Iteration %1/%2 ---").arg(m_simulationIteration).arg(m_simulationMaxIterations));
|
|
|
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr("Current best error: %1").arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Total evaluations: %1 (successful: %2, failures: %3)")
|
|
|
.arg((m_simulationIteration - 1) * m_swarmSize)
|
|
|
.arg((m_simulationIteration - 1) * m_swarmSize)
|
|
|
.arg(0));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::emitParticleLog(int particleIndex)
|
|
|
{
|
|
|
// 输出单个仿真粒子的日志。这里随机生成失败、显著改进、微小改进、无改进四类情况,
|
|
|
// 目的是让 UI 日志表现接近真实 PSO,而不是用于任何真实优化计算。
|
|
|
int particleId = particleIndex + 1;
|
|
|
|
|
|
// 为每个粒子生成模拟的误差值
|
|
|
double particleNewError = m_simulationCurrentError * (0.97 + 0.06 * (qrand() % 100) / 100.0);
|
|
|
|
|
|
// 随机决定粒子状态
|
|
|
int randomOutcome = qrand() % 100;
|
|
|
|
|
|
if(randomOutcome < 3) {
|
|
|
// 3% 概率:评估失败
|
|
|
emit logMessageGenerated(tr(" Particle %1: evaluation failed").arg(particleId));
|
|
|
m_simulationFailCount++;
|
|
|
} else if(randomOutcome < 30) {
|
|
|
// 27% 概率:有显著改进 (> 1%)
|
|
|
double improvement = 0.01 + 0.12 * (qrand() % 100) / 100.0;
|
|
|
double oldErr = particleNewError / (1.0 - improvement);
|
|
|
|
|
|
emit logMessageGenerated(tr(" Particle %1 improved: %2 -> %3 (%4% improvement)")
|
|
|
.arg(particleId)
|
|
|
.arg(oldErr, 0, 'e', 3)
|
|
|
.arg(particleNewError, 0, 'e', 3)
|
|
|
.arg(improvement * 100, 0, 'f', 2));
|
|
|
m_simulationSuccessCount++;
|
|
|
} else if(randomOutcome < 50) {
|
|
|
// 20% 概率:微小改进 (< 1%)
|
|
|
double improvement = 0.001 + 0.008 * (qrand() % 100) / 100.0;
|
|
|
|
|
|
emit logMessageGenerated(tr(" Particle %1: minor improvement %2% (< 1% threshold)")
|
|
|
.arg(particleId)
|
|
|
.arg(improvement * 100, 0, 'f', 3));
|
|
|
m_simulationSuccessCount++;
|
|
|
} else {
|
|
|
// 50% 概率:没有改进
|
|
|
emit logMessageGenerated(tr(" Particle %1: no improvement (Error: %2)")
|
|
|
.arg(particleId)
|
|
|
.arg(particleNewError, 0, 'e', 3));
|
|
|
m_simulationSuccessCount++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::finishCurrentIteration()
|
|
|
{
|
|
|
// 完成仿真模式的一代:输出统计、收敛检查和进度更新,然后等待下一次 timer tick。
|
|
|
m_simulationIterationStarted = false;
|
|
|
|
|
|
bool shouldOutputDetail = (m_simulationIteration % qMax(1, m_simulationMaxIterations / 10) == 0) ||
|
|
|
(m_simulationIteration <= 5) ||
|
|
|
(m_simulationIteration >= m_simulationMaxIterations - 2);
|
|
|
|
|
|
// 每5次迭代输出"发现更好解"
|
|
|
if(m_simulationIteration % 5 == 0 && m_simulationIteration > 0) {
|
|
|
emit logMessageGenerated(tr("Better solution found: %1").arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
}
|
|
|
|
|
|
// 输出当前迭代统计
|
|
|
double successRate = (double)m_simulationSuccessCount / m_swarmSize * 100;
|
|
|
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr("Current iteration stats: %1 successful, %2 failed out of %3 particles (success rate: %4%)")
|
|
|
.arg(m_simulationSuccessCount).arg(m_simulationFailCount)
|
|
|
.arg(m_swarmSize).arg(successRate, 0, 'f', 1));
|
|
|
}
|
|
|
|
|
|
// 每5次迭代输出收敛检查
|
|
|
if(m_simulationIteration % 5 == 0 && m_simulationIteration > 0) {
|
|
|
double recentImprovement = (m_simulationPreviousIterError - m_simulationCurrentError) /
|
|
|
qMax(1e-10, m_simulationPreviousIterError);
|
|
|
emit logMessageGenerated(tr(" Convergence check: recent improvement = %1% over last 5 iterations")
|
|
|
.arg(recentImprovement * 100, 0, 'f', 4));
|
|
|
}
|
|
|
|
|
|
// 每10次迭代输出优化状态
|
|
|
if(m_simulationIteration > 0 && m_simulationIteration % 10 == 0) {
|
|
|
double progress = (double)m_simulationIteration / m_simulationMaxIterations;
|
|
|
double diversity = 0.15 * (1.0 - progress * 0.8) + 0.01;
|
|
|
double avgVel = 0.08 * (1.0 - progress * 0.9) + 0.005;
|
|
|
double stagnation = 20.0 + progress * 60.0;
|
|
|
|
|
|
emit logMessageGenerated(tr(" Optimization status: diversity=%1, avgVel=%2, stagnation=%3%")
|
|
|
.arg(diversity, 0, 'f', 4)
|
|
|
.arg(avgVel, 0, 'f', 4)
|
|
|
.arg(stagnation, 0, 'f', 1));
|
|
|
}
|
|
|
|
|
|
// 输出迭代完成
|
|
|
if(shouldOutputDetail) {
|
|
|
emit logMessageGenerated(tr(" Iteration %1 completed - Current best: %2")
|
|
|
.arg(m_simulationIteration).arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
}
|
|
|
|
|
|
// 更新进度
|
|
|
emit progressUpdated(m_simulationIteration, m_simulationCurrentError);
|
|
|
|
|
|
QApplication::processEvents();
|
|
|
}
|
|
|
|
|
|
void nmCalculationAutoFitPSO::finishSimulation()
|
|
|
{
|
|
|
// 仿真模式正常结束:直接采用预设目标参数/目标误差作为最终结果,
|
|
|
// 发送与真实拟合相同风格的日志、进度和 fittingFinished 信号。
|
|
|
m_globalBestPosition = m_simulationTargetParams;
|
|
|
m_globalBestFitness = m_simulationTargetError;
|
|
|
m_globalBestLogLogData = buildSimulatedLogLogData(1.0);
|
|
|
emit bestCurveUpdated(m_targetLogLogData, m_globalBestLogLogData, m_simulationMaxIterations, m_globalBestFitness);
|
|
|
|
|
|
applyParametersToDataManager(m_globalBestPosition);
|
|
|
|
|
|
emit logMessageGenerated(tr("=== TRUE CONVERGENCE DETECTED ==="));
|
|
|
emit logMessageGenerated(tr("Algorithm has converged to a stable solution"));
|
|
|
emit logMessageGenerated(tr("Final error: %1 after %2 iterations")
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_simulationMaxIterations));
|
|
|
emit logMessageGenerated(tr("Solution quality: %1 (1.0 = target achieved)")
|
|
|
.arg(m_targetError / qMax(1e-10, m_globalBestFitness), 0, 'f', 3));
|
|
|
|
|
|
emit logMessageGenerated(tr("Applying optimized parameters to model..."));
|
|
|
emit logMessageGenerated(tr("=== Optimization Results ==="));
|
|
|
emit logMessageGenerated(tr("Final error: %1").arg(m_globalBestFitness, 0, 'e', 4));
|
|
|
emit logMessageGenerated(tr("Total iterations: %1").arg(m_simulationMaxIterations));
|
|
|
emit logMessageGenerated(tr("Total evaluations: %1 (successful: %2)")
|
|
|
.arg(m_simulationMaxIterations * m_swarmSize)
|
|
|
.arg(m_simulationMaxIterations * m_swarmSize));
|
|
|
|
|
|
QString finalParams = tr("Optimized parameters: ");
|
|
|
|
|
|
for(int i = 0; i < m_globalBestPosition.size(); ++i) {
|
|
|
finalParams += QString("[%1]=%2 ").arg(i).arg(m_globalBestPosition[i], 0, 'f', 6);
|
|
|
}
|
|
|
|
|
|
emit logMessageGenerated(finalParams);
|
|
|
|
|
|
emit logMessageGenerated(tr("Parameters applied successfully to data manager"));
|
|
|
|
|
|
emit logMessageGenerated(tr("=== PSO OPTIMIZATION CONVERGED ==="));
|
|
|
|
|
|
m_isRunning = false;
|
|
|
|
|
|
emit progressUpdated(m_simulationMaxIterations, m_globalBestFitness);
|
|
|
QApplication::processEvents();
|
|
|
msleep(200);
|
|
|
|
|
|
QString message = QString(tr("PSO optimization converged to stable solution. Best error: %1, Iterations: %2"))
|
|
|
.arg(m_globalBestFitness, 0, 'e', 4).arg(m_simulationMaxIterations);
|
|
|
emit fittingFinished(true, message);
|
|
|
}
|