|
|
|
|
|
#ifndef NMSURROGATE_H
|
|
|
|
|
|
#define NMSURROGATE_H
|
|
|
|
|
|
#include "nmPsoAcceleration.h"
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
#include <QAtomicInt>
|
|
|
|
|
|
#include "nmCalculation_global.h"
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 代理模型的冻结输入;模型特征与当前项目对象完全分离。 */
|
|
|
|
|
|
struct nmSurrogateContext
|
|
|
|
|
|
{
|
|
|
|
|
|
nmSurrogateContext()
|
|
|
|
|
|
: available(false), pebiGrid(false), targetAvailable(false), verticalWell(false), activeWells(0),
|
|
|
|
|
|
complexGeometry(false), solverType(1), sectionIndex(0), initialPressure(0), enabled(true)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
bool available; ///< 调用方已提供完整工况数据。
|
|
|
|
|
|
bool pebiGrid; ///< 模型是否使用 PEBI 网格。
|
|
|
|
|
|
bool targetAvailable; ///< 目标井是否存在。
|
|
|
|
|
|
bool verticalWell; ///< 目标井是否为普通直井。
|
|
|
|
|
|
int activeWells; ///< 有效计算井数量,用于原单井训练域检查。
|
|
|
|
|
|
bool complexGeometry; ///< 是否存在断层、裂缝或区域变化。
|
|
|
|
|
|
int solverType; ///< 1 至 5 对应现有 T1 至 T5 模型。
|
|
|
|
|
|
int sectionIndex; ///< 目标流量段序号,沿用软件的一基序号。
|
|
|
|
|
|
double initialPressure; ///< 初始压力,单位沿用原软件。
|
|
|
|
|
|
bool enabled; ///< 是否启用代理,关闭时 PSO 全量真实评价。
|
|
|
|
|
|
QString targetWell; ///< 目标井名称。
|
|
|
|
|
|
QString targetWellCode; ///< 目标井稳定编码。
|
|
|
|
|
|
QVector<QPointF> flowPoints; ///< 每段持续时长与流量,沿用原软件定义。
|
|
|
|
|
|
nmFitCurve targetCurve; ///< 原始目标曲线,与拟合模块输入一致。
|
|
|
|
|
|
QVector<nmFitParameterValue> parameters; ///< 与拟合请求完全相同的参数映射。
|
|
|
|
|
|
QVector<double> physicalParameters; ///< k/Skin/C/phi/h/Ct/Cf,沿用现有单位。
|
|
|
|
|
|
QString mlRoot; ///< Python 项目根目录,交付使用时建议显式指定绝对路径。
|
|
|
|
|
|
QString pythonExecutable; ///< Python 解释器路径。
|
|
|
|
|
|
QString modelTag; ///< 模型标签,空值沿用原 T1–T4/T5 选择规则。
|
|
|
|
|
|
QString modelStage; ///< 训练阶段,空值沿用原配置。
|
|
|
|
|
|
QString workDirectory; ///< 临时工作根目录,空值使用系统临时目录。
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 一次代理会话的诊断计数,不参与真实最优解计算。 */
|
|
|
|
|
|
struct nmSurrogateStatistics
|
|
|
|
|
|
{
|
|
|
|
|
|
int m_surrogateWarmupIterationCount; ///< warmup 全量真实评价代数。
|
|
|
|
|
|
int m_surrogatePeriodicAuditIterationCount; ///< 周期性全量审计代数。
|
|
|
|
|
|
int m_surrogateContextBlockedIterationCount; ///< 因工况不支持而全量真实评价的代数。
|
|
|
|
|
|
int m_surrogateActiveIterationCount; ///< 代理筛选真正参与的代数。
|
|
|
|
|
|
int m_surrogateSelectedParticleCount; ///< 被选中跑真实求解器的粒子累计数。
|
|
|
|
|
|
int m_surrogateScreenedParticleCount; ///< 被代理筛掉的粒子累计数。
|
|
|
|
|
|
int m_surrogateTopKParticleCount; ///< 因 surrogate top-k 被选中的粒子累计数。
|
|
|
|
|
|
int m_surrogateAuditParticleCount; ///< 因 random audit 被选中的粒子累计数。
|
|
|
|
|
|
int m_surrogateFallbackParticleCount; ///< 因 fallback gate 被选中的粒子累计数。
|
|
|
|
|
|
int m_surrogateDomainParticleCount; ///< 因训练域 gate 被选中的粒子累计数。
|
|
|
|
|
|
int m_surrogateMinFloorParticleCount; ///< 因最低真实求解比例被补选中的粒子累计数。
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 独立代理评分及 PSO 加速模块,不链接 PSO 或真实求解器。 */
|
|
|
|
|
|
class NMCALCULATION_EXPORT nmSurrogate : public QObject, public nmPsoAcceleration
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
public:
|
|
|
|
|
|
explicit nmSurrogate(QObject* parent = nullptr);
|
|
|
|
|
|
virtual ~nmSurrogate();
|
|
|
|
|
|
virtual void begin();
|
|
|
|
|
|
/** @brief 开始新会话,复制输入并清理上一会话的进程和工作文件。 */
|
|
|
|
|
|
bool configure(const nmSurrogateContext& context, QString* error = nullptr);
|
|
|
|
|
|
/** @brief 直接批量评分;无效候选返回 NaN,评分失败说明通过 error 返回。 */
|
|
|
|
|
|
QVector<double> scoreCandidates(const QVector<QVector<double>>& candidates, QString* error = nullptr);
|
|
|
|
|
|
/** @brief 返回只读诊断统计。 */
|
|
|
|
|
|
const nmSurrogateStatistics& statistics() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_statistics;
|
|
|
|
|
|
}
|
|
|
|
|
|
/** @brief 取消评分等待,后续不会自动启动新的评分进程。 */
|
|
|
|
|
|
void cancel();
|
|
|
|
|
|
/** @brief 外围事件调度只用于原软件兼容,独立调用无需提供。 */
|
|
|
|
|
|
void setEventPump(const std::function<void()>& pump)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_eventPump = pump;
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual QVector<AutoFitParticle> select(const QVector<AutoFitParticle>& particles, int iteration,
|
|
|
|
|
|
unsigned int seed);
|
|
|
|
|
|
virtual bool preserveGuide(const AutoFitParticle& particle, bool beatsGlobalBest) const;
|
|
|
|
|
|
QString getMlRootPath() const;
|
|
|
|
|
|
QString getPythonExecutablePath() const;
|
|
|
|
|
|
QString getSurrogateTag() const;
|
|
|
|
|
|
QString getSurrogateStage() const;
|
|
|
|
|
|
double getSurrogateKeepFraction() const;
|
|
|
|
|
|
double getSurrogateAuditFraction() const;
|
|
|
|
|
|
double getSurrogateMinSolverFraction() const;
|
|
|
|
|
|
int getSurrogateWarmupIterations() const;
|
|
|
|
|
|
int getSurrogateFullSolverInterval() const;
|
|
|
|
|
|
bool ensureSurrogateScoringServer(QString* failureReason = nullptr, bool waitForReady = true);
|
|
|
|
|
|
void stopSurrogateScoringServer();
|
|
|
|
|
|
bool isSurrogateRunContextSupported(QString* reason) const;
|
|
|
|
|
|
void resetRunSummary();
|
|
|
|
|
|
signals:
|
|
|
|
|
|
void logMessageGenerated(const QString& message);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
// 文件协议和训练域规则仅在代理模块内部使用。
|
|
|
|
|
|
bool writeSurrogateCandidateCsv(const QString& candidatePath, bool includeGuideCandidates) const;
|
|
|
|
|
|
bool runSurrogateScoringProcess(const QString& candidatePath, const QString& scorePath,
|
|
|
|
|
|
QString* failureReason = nullptr);
|
|
|
|
|
|
bool requestSurrogateScoresFromServer(const QString& candidatePath, const QString& scorePath,
|
|
|
|
|
|
QString* failureReason = nullptr);
|
|
|
|
|
|
bool runSurrogateScoringScriptOnce(const QString& candidatePath, const QString& scorePath,
|
|
|
|
|
|
QString* failureReason = nullptr);
|
|
|
|
|
|
QVector<double> readSurrogateScores(const QString& scorePath, int expectedCount) const;
|
|
|
|
|
|
bool isSurrogateCandidateInDomain(const QVector<double>& selectedParameters, QString* reason) const;
|
|
|
|
|
|
bool forceSolverByFallbackGate(const QVector<double>& selectedParameters) const;
|
|
|
|
|
|
bool isStrongDecliningProductionSchedule(double* endStartRatio = nullptr) const;
|
|
|
|
|
|
void resetGuideBestToVerified();
|
|
|
|
|
|
QVector<bool> buildSurrogateEvaluationMask();
|
|
|
|
|
|
QVector<double> buildTraceParameterVector(const QVector<double>& parameters) const;
|
|
|
|
|
|
bool isSurrogateScreeningEnabled() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_context.enabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
void pollEvents();
|
|
|
|
|
|
bool writeContext();
|
|
|
|
|
|
void releaseSession();
|
|
|
|
|
|
nmSurrogateContext m_context;
|
|
|
|
|
|
nmSurrogateStatistics m_statistics;
|
|
|
|
|
|
QVector<AutoFitParticle> m_swarm;
|
|
|
|
|
|
QVector<nmFitParameterValue> m_modelParameters;
|
|
|
|
|
|
QVector<int> m_enabledParamIndices;
|
|
|
|
|
|
QString m_tempDirectory, m_surrogateContextFilePath;
|
|
|
|
|
|
int m_currentIteration;
|
|
|
|
|
|
unsigned int m_psoRandomSeed;
|
|
|
|
|
|
QProcess* m_surrogateScoringProcess;
|
|
|
|
|
|
QString m_surrogateScoringServerKey;
|
|
|
|
|
|
bool m_surrogateScoringServerReady;
|
|
|
|
|
|
QAtomicInt m_cancelRequested;
|
|
|
|
|
|
std::function<void()> m_eventPump;
|
|
|
|
|
|
};
|
|
|
|
|
|
#endif // NMSURROGATE_H
|