|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
#include "nmCalculationAutoFitPSO.h"
|
|
|
|
|
#include "nmCalculationDllPebiSolverTask.h"
|
|
|
|
|
#include "nmCalculationUtils.h"
|
|
|
|
|
#include "nmDataAnalyzeManager.h"
|
|
|
|
|
#include "nmDataWellBase.h"
|
|
|
|
|
#include "nmDataVerticalWell.h"
|
|
|
|
|
@ -31,6 +32,12 @@
|
|
|
|
|
#define DEBUG_OUT(msg) OutputDebugStringA(QString("[AutoFit] %1\n").arg(msg).toLocal8Bit().data())
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef QT_NO_DEBUG
|
|
|
|
|
static const bool kAutoFitDiagnosticTraceEnabled = false;
|
|
|
|
|
#else
|
|
|
|
|
static const bool kAutoFitDiagnosticTraceEnabled = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// ===== PSO基础常量 =====
|
|
|
|
|
// MIN_FITNESS_IMPROVEMENT:判断适应度是否有有效改善的最小阈值。
|
|
|
|
|
// VELOCITY_LIMIT_FACTOR:单次速度最大值占参数搜索区间的比例,防止粒子一步跨太远。
|
|
|
|
|
@ -467,10 +474,11 @@ nmCalculationAutoFitPSO::nmCalculationAutoFitPSO(QObject* parent)
|
|
|
|
|
, m_localOptimumWindow(8)
|
|
|
|
|
, m_farTargetFactor(10.0)
|
|
|
|
|
, m_targetWellName("")
|
|
|
|
|
, m_traceEnabled(true)
|
|
|
|
|
, m_traceEnabled(kAutoFitDiagnosticTraceEnabled)
|
|
|
|
|
, m_traceRunId("")
|
|
|
|
|
, m_traceFilePath("")
|
|
|
|
|
, m_traceMetaFilePath("")
|
|
|
|
|
, m_surrogateContextFilePath("")
|
|
|
|
|
, m_surrogateScreeningEnabled(false)
|
|
|
|
|
, m_psoRandomSeed(0)
|
|
|
|
|
, m_surrogateRunContextSummary("")
|
|
|
|
|
@ -505,8 +513,6 @@ nmCalculationAutoFitPSO::nmCalculationAutoFitPSO(QObject* parent)
|
|
|
|
|
{
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
@ -555,151 +561,38 @@ nmCalculationAutoFitPSO::~nmCalculationAutoFitPSO()
|
|
|
|
|
|
|
|
|
|
// ===== 临时目录工具 =====
|
|
|
|
|
//
|
|
|
|
|
// 真实求解器 DLL 和自动拟合过程会产生中间文件,因此每次创建独立的
|
|
|
|
|
// autofit_temp_<pid>_<timestamp> 目录。退出时只删除本类创建的目录,启动时顺便清理
|
|
|
|
|
// 旧进程遗留的 autofit_temp_*,避免长期调试后应用目录被临时文件堆满。
|
|
|
|
|
|
|
|
|
|
void nmCalculationAutoFitPSO::initializeTemporaryDirectory()
|
|
|
|
|
// 求解器中间文件和代理评分文件共用一个受控的单次运行目录。创建失败时
|
|
|
|
|
// 直接禁用本次拟合,绝不回退到程序目录。
|
|
|
|
|
bool 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++;
|
|
|
|
|
nmCalculationUtils::cleanupStaleAutoFitTemporaryDirectories();
|
|
|
|
|
m_tempDirectory = nmCalculationUtils::createAutoFitTemporaryDirectory();
|
|
|
|
|
if(m_tempDirectory.isEmpty()) {
|
|
|
|
|
DEBUG_OUT("Failed to create PSO auto-fit temporary directory");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
m_traceRunId = QFileInfo(m_tempDirectory).fileName();
|
|
|
|
|
DEBUG_OUT(QString("Initialized temp directory: %1").arg(m_tempDirectory));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
if(m_tempDirectory.isEmpty()) {
|
|
|
|
|
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++;
|
|
|
|
|
}
|
|
|
|
|
const QString directoryPath = m_tempDirectory;
|
|
|
|
|
m_tempDirectory.clear();
|
|
|
|
|
m_surrogateContextFilePath.clear();
|
|
|
|
|
if(nmCalculationUtils::removeAutoFitTemporaryDirectory(directoryPath)) {
|
|
|
|
|
DEBUG_OUT("Temp directory cleaned up successfully");
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_OUT(QString("Failed to clean auto-fit temporary directory: %1")
|
|
|
|
|
.arg(directoryPath));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 输出清理统计信息
|
|
|
|
|
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)
|
|
|
|
|
@ -725,6 +618,10 @@ void nmCalculationAutoFitPSO::stopFitting()
|
|
|
|
|
// 这样可以减少 DLL 任务被硬中断导致的数据状态残留。
|
|
|
|
|
if(m_simulationMode && m_simulationTimer) {
|
|
|
|
|
m_simulationTimer->stop();
|
|
|
|
|
m_shouldStop = true;
|
|
|
|
|
m_isRunning = false;
|
|
|
|
|
emit logMessageGenerated(tr("PSO simulation stop request processed"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m_isRunning) {
|
|
|
|
|
@ -745,16 +642,11 @@ void nmCalculationAutoFitPSO::stopFitting()
|
|
|
|
|
waitCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 超时时强制重置
|
|
|
|
|
// 当前 DLL 任务可能仍持有临时文件,等待主循环自然收尾后再统一清理。
|
|
|
|
|
if(m_evaluationInProgress > 0) {
|
|
|
|
|
DEBUG_OUT("Force resetting evaluation counter");
|
|
|
|
|
emit logMessageGenerated(tr("Force stopping current evaluation..."));
|
|
|
|
|
m_evaluationInProgress = 0;
|
|
|
|
|
emit logMessageGenerated(tr("Waiting for current solver evaluation to stop..."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保运行标志被清除
|
|
|
|
|
m_isRunning = false;
|
|
|
|
|
|
|
|
|
|
emit logMessageGenerated(tr("PSO optimization stop request processed"));
|
|
|
|
|
DEBUG_OUT("Stop request processed");
|
|
|
|
|
} else {
|
|
|
|
|
@ -762,8 +654,6 @@ void nmCalculationAutoFitPSO::stopFitting()
|
|
|
|
|
emit logMessageGenerated(tr("Stop request received but optimization is not running"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeTraceFile();
|
|
|
|
|
cleanupTemporaryDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool nmCalculationAutoFitPSO::isRunning() const
|
|
|
|
|
@ -828,6 +718,7 @@ void nmCalculationAutoFitPSO::resetOptimizer()
|
|
|
|
|
m_velocityHistory.clear();
|
|
|
|
|
m_particleStagnationHistory.clear();
|
|
|
|
|
m_traceMetaFilePath.clear();
|
|
|
|
|
m_surrogateContextFilePath.clear();
|
|
|
|
|
resetRunSummary();
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT("Optimizer reset");
|
|
|
|
|
@ -842,45 +733,60 @@ void nmCalculationAutoFitPSO::setPSOTargetWellName(const QString& wellName)
|
|
|
|
|
|
|
|
|
|
void nmCalculationAutoFitPSO::initializeTraceFile()
|
|
|
|
|
{
|
|
|
|
|
// 创建本次 PSO 的可复盘文件:
|
|
|
|
|
// - pso_baseline_trace_<run_id>.csv:逐代逐粒子的参数、真实误差、代理误差和筛选决策;
|
|
|
|
|
// - pso_baseline_trace_<run_id>.meta.json:目标曲线、流量制度、参数上下界、PSO/代理配置。
|
|
|
|
|
//
|
|
|
|
|
// 代理模型评分脚本也会读取 meta.json,因此 trace meta 不是单纯日志,而是C++ 与 Python 代理模型之间的运行上下文契约。
|
|
|
|
|
if(!m_traceEnabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeTraceFile();
|
|
|
|
|
m_traceFilePath.clear();
|
|
|
|
|
m_traceMetaFilePath.clear();
|
|
|
|
|
m_surrogateContextFilePath.clear();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_traceFilePath = traceDir.absoluteFilePath(QString("pso_baseline_trace_%1.csv").arg(m_traceRunId));
|
|
|
|
|
m_traceMetaFilePath = traceDir.absoluteFilePath(QString("pso_baseline_trace_%1.meta.json").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;
|
|
|
|
|
// 代理加速开启时生成运行上下文文件,供 Python 评分程序使用。
|
|
|
|
|
if(isSurrogateScreeningEnabled()) {
|
|
|
|
|
QDir surrogateDirectory(
|
|
|
|
|
QDir(m_tempDirectory).absoluteFilePath("surrogate"));
|
|
|
|
|
if(QDir().mkpath(surrogateDirectory.absolutePath())) {
|
|
|
|
|
const QString contextPath = surrogateDirectory.absoluteFilePath(
|
|
|
|
|
"context.meta.json");
|
|
|
|
|
if(writeTraceMetaFile(contextPath)) {
|
|
|
|
|
m_surrogateContextFilePath = contextPath;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_OUT(QString("Failed to create surrogate temporary directory: %1")
|
|
|
|
|
.arg(surrogateDirectory.absolutePath()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeTraceHeader();
|
|
|
|
|
writeTraceMetaFile();
|
|
|
|
|
DEBUG_OUT(QString("PSO baseline trace initialized: %1").arg(m_traceFilePath));
|
|
|
|
|
emit logMessageGenerated(tr("PSO baseline trace: %1").arg(m_traceFilePath));
|
|
|
|
|
|
|
|
|
|
if(!m_traceMetaFilePath.isEmpty()) {
|
|
|
|
|
emit logMessageGenerated(tr("PSO baseline trace meta: %1").arg(m_traceMetaFilePath));
|
|
|
|
|
// 诊断轨迹只在 Debug 构建中启用,并写入系统临时目录而不是仓库。
|
|
|
|
|
if(m_traceEnabled) {
|
|
|
|
|
QDir traceDirectory(
|
|
|
|
|
QDir(QDir::tempPath()).absoluteFilePath("WTAI/AutoFitTrace/PSO"));
|
|
|
|
|
if(!QDir().mkpath(traceDirectory.absolutePath())) {
|
|
|
|
|
DEBUG_OUT(QString("Failed to create PSO trace directory: %1")
|
|
|
|
|
.arg(traceDirectory.absolutePath()));
|
|
|
|
|
} else {
|
|
|
|
|
m_traceFilePath = traceDirectory.absoluteFilePath(
|
|
|
|
|
QString("pso_baseline_trace_%1.csv").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();
|
|
|
|
|
} else {
|
|
|
|
|
writeTraceHeader();
|
|
|
|
|
const QString metaPath = traceDirectory.absoluteFilePath(
|
|
|
|
|
QString("pso_baseline_trace_%1.meta.json").arg(m_traceRunId));
|
|
|
|
|
if(writeTraceMetaFile(metaPath)) {
|
|
|
|
|
m_traceMetaFilePath = metaPath;
|
|
|
|
|
}
|
|
|
|
|
emit logMessageGenerated(
|
|
|
|
|
tr("PSO baseline trace: %1").arg(m_traceFilePath));
|
|
|
|
|
if(!m_traceMetaFilePath.isEmpty()) {
|
|
|
|
|
emit logMessageGenerated(
|
|
|
|
|
tr("PSO baseline trace meta: %1")
|
|
|
|
|
.arg(m_traceMetaFilePath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit logMessageGenerated(tr("PSO surrogate screening: %1, model=%2, keep=%3, audit=%4, warmup=%5, min_solver=%6")
|
|
|
|
|
@ -1039,21 +945,20 @@ void nmCalculationAutoFitPSO::writeTraceHeader()
|
|
|
|
|
out << cols.join(",") << "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmCalculationAutoFitPSO::writeTraceMetaFile()
|
|
|
|
|
bool nmCalculationAutoFitPSO::writeTraceMetaFile(const QString& filePath)
|
|
|
|
|
{
|
|
|
|
|
// 写 trace 的配套 JSON。它把“如何解释 CSV”的上下文固化下来:
|
|
|
|
|
// 目标井、目标 log-log 曲线、流量制度、参数名和上下界、初始参数、PSO 配置、
|
|
|
|
|
// 代理模型 tag/stage/筛选比例等。Python 评分脚本会读取这个文件来构造模型输入。
|
|
|
|
|
if(m_traceMetaFilePath.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
if(filePath.isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile metaFile(m_traceMetaFilePath);
|
|
|
|
|
QFile metaFile(filePath);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
DEBUG_OUT(QString("Failed to open PSO context/meta file: %1").arg(filePath));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nmDataAnalyzeManager* dataManager = nmDataAnalyzeManager::getCurrentInstance();
|
|
|
|
|
@ -1129,6 +1034,7 @@ void nmCalculationAutoFitPSO::writeTraceMetaFile()
|
|
|
|
|
out << "}\n";
|
|
|
|
|
metaFile.flush();
|
|
|
|
|
metaFile.close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<double> nmCalculationAutoFitPSO::buildTraceParameterVector(const QVector<double>& selectedParameters) const
|
|
|
|
|
@ -1388,7 +1294,7 @@ bool nmCalculationAutoFitPSO::writeSurrogateCandidateCsv(const QString& candidat
|
|
|
|
|
{
|
|
|
|
|
// 写给 Python 代理模型的候选粒子 CSV。
|
|
|
|
|
//
|
|
|
|
|
// 文件位置通常是 ML/nmWTAI-ML/data/temp/pso_screen_candidates_<run_id>_gen<N>.csv。
|
|
|
|
|
// 文件位于本次运行目录的 surrogate/candidates.csv,并在每代读取后删除。
|
|
|
|
|
// 字段顺序必须与 ML 脚本 score_pso_candidates*.py 保持一致:
|
|
|
|
|
// particle_id,k,skin,wellboreC,phi,h,Cf,solverType
|
|
|
|
|
//
|
|
|
|
|
@ -1488,8 +1394,9 @@ bool nmCalculationAutoFitPSO::ensureSurrogateScoringServer(QString* failureReaso
|
|
|
|
|
//
|
|
|
|
|
// serverKey 由 Python 路径、脚本路径、跟踪元数据、模型标识和阶段组成。
|
|
|
|
|
// 只要其中任何一个变化,就停止旧进程并启动新进程,避免拿旧模型或旧目标曲线评分。
|
|
|
|
|
if(m_traceMetaFilePath.isEmpty() || !QFileInfo(m_traceMetaFilePath).exists()) {
|
|
|
|
|
QString reason = "trace meta file is missing";
|
|
|
|
|
if(m_surrogateContextFilePath.isEmpty() ||
|
|
|
|
|
!QFileInfo(m_surrogateContextFilePath).exists()) {
|
|
|
|
|
QString reason = "surrogate context file is missing";
|
|
|
|
|
DEBUG_OUT(QString("Surrogate scoring server skipped: %1").arg(reason));
|
|
|
|
|
|
|
|
|
|
if(failureReason) {
|
|
|
|
|
@ -1517,7 +1424,7 @@ bool nmCalculationAutoFitPSO::ensureSurrogateScoringServer(QString* failureReaso
|
|
|
|
|
QString serverKey = QString("%1|%2|%3|%4|%5")
|
|
|
|
|
.arg(pythonPath)
|
|
|
|
|
.arg(scriptPath)
|
|
|
|
|
.arg(m_traceMetaFilePath)
|
|
|
|
|
.arg(m_surrogateContextFilePath)
|
|
|
|
|
.arg(getSurrogateTag())
|
|
|
|
|
.arg(getSurrogateStage());
|
|
|
|
|
|
|
|
|
|
@ -1532,7 +1439,7 @@ bool nmCalculationAutoFitPSO::ensureSurrogateScoringServer(QString* failureReaso
|
|
|
|
|
// -u 关闭 Python 标准输出和错误输出的缓冲,C++ 可以及时读到就绪或完成消息。
|
|
|
|
|
args << "-u"
|
|
|
|
|
<< scriptPath
|
|
|
|
|
<< "--trace-meta" << m_traceMetaFilePath
|
|
|
|
|
<< "--trace-meta" << m_surrogateContextFilePath
|
|
|
|
|
<< "--tag" << getSurrogateTag()
|
|
|
|
|
<< "--stage" << getSurrogateStage();
|
|
|
|
|
|
|
|
|
|
@ -1786,8 +1693,9 @@ bool nmCalculationAutoFitPSO::runSurrogateScoringScriptOnce(const QString& candi
|
|
|
|
|
//
|
|
|
|
|
// 输出:
|
|
|
|
|
// - scorePath:Python 写出的评分 CSV,随后由 readSurrogateScores() 读取。
|
|
|
|
|
if(m_traceMetaFilePath.isEmpty() || !QFileInfo(m_traceMetaFilePath).exists()) {
|
|
|
|
|
QString reason = "trace meta file is missing";
|
|
|
|
|
if(m_surrogateContextFilePath.isEmpty() ||
|
|
|
|
|
!QFileInfo(m_surrogateContextFilePath).exists()) {
|
|
|
|
|
QString reason = "surrogate context file is missing";
|
|
|
|
|
DEBUG_OUT(QString("Surrogate scoring skipped: %1").arg(reason));
|
|
|
|
|
|
|
|
|
|
if(failureReason) {
|
|
|
|
|
@ -1815,7 +1723,7 @@ bool nmCalculationAutoFitPSO::runSurrogateScoringScriptOnce(const QString& candi
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << scriptPath
|
|
|
|
|
<< "--candidates" << candidatePath
|
|
|
|
|
<< "--trace-meta" << m_traceMetaFilePath
|
|
|
|
|
<< "--trace-meta" << m_surrogateContextFilePath
|
|
|
|
|
<< "--output" << scorePath
|
|
|
|
|
<< "--tag" << getSurrogateTag()
|
|
|
|
|
<< "--stage" << getSurrogateStage();
|
|
|
|
|
@ -2502,19 +2410,17 @@ QVector<bool> nmCalculationAutoFitPSO::buildSurrogateEvaluationMask()
|
|
|
|
|
return evaluateMask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString mlRoot = getMlRootPath();
|
|
|
|
|
QDir tempDir(QDir(mlRoot).absoluteFilePath("data/temp"));
|
|
|
|
|
|
|
|
|
|
if(!tempDir.exists()) {
|
|
|
|
|
QDir().mkpath(tempDir.absolutePath());
|
|
|
|
|
QDir tempDir(QDir(m_tempDirectory).absoluteFilePath("surrogate"));
|
|
|
|
|
if(!tempDir.exists() && !QDir().mkpath(tempDir.absolutePath())) {
|
|
|
|
|
emit logMessageGenerated(
|
|
|
|
|
tr("Surrogate temporary directory is unavailable; using the full solver"));
|
|
|
|
|
resetGuideBestToVerified();
|
|
|
|
|
return evaluateMask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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++ 的代理评分结果
|
|
|
|
|
// 同一代评分是串行请求,固定文件名可安全覆盖,避免每代累积两个 CSV。
|
|
|
|
|
const QString candidatePath = tempDir.absoluteFilePath("candidates.csv");
|
|
|
|
|
const QString scorePath = tempDir.absoluteFilePath("scores.csv");
|
|
|
|
|
|
|
|
|
|
QString scoringFailureReason;
|
|
|
|
|
|
|
|
|
|
@ -2523,6 +2429,9 @@ QVector<bool> nmCalculationAutoFitPSO::buildSurrogateEvaluationMask()
|
|
|
|
|
|
|
|
|
|
if(!writeSurrogateCandidateCsv(candidatePath, includeGuideCandidates) ||
|
|
|
|
|
!runSurrogateScoringProcess(candidatePath, scorePath, &scoringFailureReason)) {
|
|
|
|
|
// 评分失败时删除本代生成的临时 CSV。
|
|
|
|
|
QFile::remove(candidatePath);
|
|
|
|
|
QFile::remove(scorePath);
|
|
|
|
|
// Python 环境、checkpoint、processed 数据或脚本异常时都走这里。
|
|
|
|
|
// 降级策略是全量真实求解器,保证代理模块故障不会中断 PSO 拟合。
|
|
|
|
|
emit logMessageGenerated(tr("Surrogate screening unavailable; falling back to full solver for iteration %1")
|
|
|
|
|
@ -2538,6 +2447,9 @@ QVector<bool> nmCalculationAutoFitPSO::buildSurrogateEvaluationMask()
|
|
|
|
|
|
|
|
|
|
int expectedScoreCount = m_swarm.size() * (includeGuideCandidates ? 2 : 1);
|
|
|
|
|
QVector<double> allScores = readSurrogateScores(scorePath, expectedScoreCount);
|
|
|
|
|
// 分数读入内存后删除本代候选和评分 CSV。
|
|
|
|
|
QFile::remove(candidatePath);
|
|
|
|
|
QFile::remove(scorePath);
|
|
|
|
|
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;
|
|
|
|
|
@ -2915,6 +2827,12 @@ bool nmCalculationAutoFitPSO::startAutoFitting()
|
|
|
|
|
|
|
|
|
|
// 重置状态
|
|
|
|
|
resetOptimizer();
|
|
|
|
|
cleanupTemporaryDirectory();
|
|
|
|
|
if(!initializeTemporaryDirectory()) {
|
|
|
|
|
m_lastError = tr("Cannot create the automatic fitting temporary directory");
|
|
|
|
|
emit logMessageGenerated(tr("ERROR: %1").arg(m_lastError));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_isRunning = true;
|
|
|
|
|
m_shouldStop = false;
|
|
|
|
|
m_isPaused = false;
|
|
|
|
|
@ -2978,6 +2896,9 @@ bool nmCalculationAutoFitPSO::startAutoFitting()
|
|
|
|
|
|
|
|
|
|
if(m_userInitialSolution.isEmpty()) {
|
|
|
|
|
emit logMessageGenerated(tr("ERROR: m_userInitialSolution is empty!"));
|
|
|
|
|
closeTraceFile();
|
|
|
|
|
cleanupTemporaryDirectory();
|
|
|
|
|
m_isRunning = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -3385,9 +3306,8 @@ bool nmCalculationAutoFitPSO::startAutoFitting()
|
|
|
|
|
|
|
|
|
|
// 现在才发送完成信号,这会触发消息框
|
|
|
|
|
closeTraceFile();
|
|
|
|
|
emit fittingFinished(success, message);
|
|
|
|
|
|
|
|
|
|
cleanupTemporaryDirectory();
|
|
|
|
|
emit fittingFinished(success, message);
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
|
|
|
|
|
|