You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
364 lines
12 KiB
C++
364 lines
12 KiB
C++
#ifndef NMCALCULATIONAUTOFIT_H
|
|
#define NMCALCULATIONAUTOFIT_H
|
|
|
|
#include <QObject>
|
|
#include <QVector>
|
|
#include <QPointF>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QMutex>
|
|
#include <QDateTime>
|
|
#include <QFile>
|
|
|
|
#include "nmCalculation_global.h"
|
|
|
|
// 前向声明
|
|
class nmCalculationDllPebiSolverTask;
|
|
class nmDataWellBase;
|
|
|
|
class QTimer;
|
|
class QProcess;
|
|
|
|
// PSO粒子结构
|
|
struct AutoFitParticle {
|
|
QVector<QVector<double> > currentLogLogData;
|
|
QVector<QVector<double> > bestLogLogData;
|
|
QVector<double> position; // 当前位置(参数值)
|
|
QVector<double> velocity; // 速度
|
|
QVector<double> bestPosition; // 个体最优位置
|
|
double fitness; // 当前适应度
|
|
double bestFitness; // 个体最优适应度
|
|
|
|
bool evaluatedThisIteration;
|
|
bool lastEvaluationSuccess;
|
|
int lastEvaluationElapsedMs;
|
|
double surrogateObjective;
|
|
bool selectedForSolver;
|
|
bool selectedByAudit;
|
|
QString screeningDecision;
|
|
|
|
AutoFitParticle()
|
|
: fitness(1e10)
|
|
, bestFitness(1e10)
|
|
, evaluatedThisIteration(false)
|
|
, lastEvaluationSuccess(false)
|
|
, lastEvaluationElapsedMs(-1)
|
|
, surrogateObjective(1e10)
|
|
, selectedForSolver(true)
|
|
, selectedByAudit(false)
|
|
, screeningDecision("full_solver")
|
|
{}
|
|
};
|
|
|
|
// 判断类型枚举
|
|
enum StopReasonPSO {
|
|
PSO_CONTINUE_OPTIMIZATION = 0, // 继续优化
|
|
PSO_TARGET_ACHIEVED = 1, // 达到目标精度
|
|
PSO_TRUE_CONVERGENCE = 2, // 真正收敛
|
|
PSO_LOCAL_OPTIMUM = 3, // 陷入局部最优
|
|
PSO_MAX_ITERATIONS = 4, // 达到最大迭代数
|
|
PSO_USER_STOPPED = 5, // 用户停止
|
|
PSO_CONSECUTIVE_FAILURES = 6, // 连续失败停止
|
|
PSO_OPTIMIZATION_FAILED = 7 // 优化失败
|
|
};
|
|
|
|
class NMCALCULATION_EXPORT nmCalculationAutoFitPSO : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
// 构造函数和析构函数
|
|
explicit nmCalculationAutoFitPSO(QObject* parent = 0);
|
|
~nmCalculationAutoFitPSO();
|
|
|
|
// ===== 核心接口:完全数据驱动 =====
|
|
void setTargetLogLogData(const QVector<QVector<double> >& targetData);
|
|
bool startAutoFitting();
|
|
void stopFitting();
|
|
QVector<double> getBestSolution() const;
|
|
double getBestFitness() const;
|
|
QString getLastError() const;
|
|
bool isRunning() const;
|
|
int getCurrentIteration() const;
|
|
void resetOptimizer();
|
|
|
|
void setPSOTargetWellName(const QString& wellName);
|
|
//QString getTargetWellName() const;
|
|
|
|
public:
|
|
// 模拟拟合模式
|
|
void setSimulationMode(bool enabled);
|
|
bool isSimulationMode() const { return m_simulationMode; }
|
|
void setSimulationTargetParams(const QVector<double>& targetParams, double targetError);
|
|
private slots:
|
|
void onSimulationTimerTick();
|
|
|
|
signals:
|
|
void progressUpdated(int iteration, double bestFitness);
|
|
void fittingFinished(bool success, const QString& message);
|
|
void bestCurveUpdated(QVector<QVector<double> > targetData,
|
|
QVector<QVector<double> > bestData,
|
|
int iteration,
|
|
double fitness);
|
|
|
|
|
|
signals:
|
|
void logMessageGenerated(const QString& message);
|
|
|
|
|
|
private:
|
|
// 临时目录管理
|
|
void initializeTemporaryDirectory();
|
|
void cleanupTemporaryDirectory();
|
|
bool removeDirectoryRecursively(const QString& path);
|
|
void cleanupOldTemporaryDirectories();
|
|
|
|
// ===== 数据加载方法 =====
|
|
bool loadAllConfigFromDataManager();
|
|
void loadOptimizationConfig();
|
|
void loadParameterBounds();
|
|
|
|
// ===== PSO核心算法 =====
|
|
void extractUserInitialValues();
|
|
void initializeSwarm();
|
|
void updateVelocityAndPosition();
|
|
double evaluateFitness(const QVector<double>& parameters);
|
|
void updateGlobalBest();
|
|
void updateParticle(int particleIndex);
|
|
|
|
// ===== 参数应用方法 =====
|
|
void applyParametersToDataManager(const QVector<double>& parameters);
|
|
void updateReservoirParameters(const QVector<double>& parameters);
|
|
void updateWellParameters(const QVector<double>& parameters);
|
|
void updateWellToDataManager(nmDataWellBase* pWell);
|
|
|
|
// ===== 求解器相关 =====
|
|
QVector<QVector<double> > runSolver();
|
|
QVector<QVector<double>> runSolverDll();
|
|
QVector<QVector<double>> runSolverExe();
|
|
|
|
// ===== 数据处理 =====
|
|
QVector<QPointF> interpolateData(const QVector<QPointF>& source,
|
|
const QVector<double>& targetX) const;
|
|
|
|
// ===== 算法辅助 =====
|
|
void adaptiveParameterUpdate(int iteration);
|
|
void saveOptimizationResult();
|
|
void validateAndProtectFinalResult();
|
|
|
|
// 收敛判断方法
|
|
StopReasonPSO analyzeOptimizationStatus();
|
|
bool checkTrueConvergence() const;
|
|
bool checkLocalOptimumTrap() const;
|
|
|
|
// 粒子群状态分析
|
|
double calculateSwarmDiversity() const;
|
|
double calculateAverageVelocity() const;
|
|
double calculateParticleStagnationRate() const;
|
|
|
|
// 适应度稳定性分析
|
|
double calculateFitnessVariance(int windowSize) const;
|
|
double calculateLongTermImprovement(int windowSize) const;
|
|
|
|
// 工具方法
|
|
QString getStopReasonDescription(StopReasonPSO reason) const;
|
|
void updateConvergenceMetrics();
|
|
|
|
// ===== 工具方法 =====
|
|
double random01() const;
|
|
void clampToLimits(QVector<double>& parameters) const;
|
|
int getEnabledParameterCount() const;
|
|
void logDebugInfo(const QString& message) const;
|
|
|
|
// ===== Baseline trace =====
|
|
void initializeTraceFile();
|
|
void closeTraceFile();
|
|
void writeTraceHeader();
|
|
void writeTraceMetaFile();
|
|
void 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);
|
|
void writeIterationTraceRows();
|
|
QVector<double> buildTraceParameterVector(const QVector<double>& selectedParameters) const;
|
|
void resetRunSummary();
|
|
void captureSurrogateRunContextSummary();
|
|
void emitRunSummary(bool success, StopReasonPSO finalReason);
|
|
|
|
// ===== Surrogate screening prototype =====
|
|
bool isSurrogateScreeningEnabled() 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;
|
|
QVector<bool> buildSurrogateEvaluationMask();
|
|
bool writeSurrogateCandidateCsv(const QString& candidatePath) const;
|
|
bool runSurrogateScoringProcess(const QString& candidatePath, const QString& scorePath, QString* failureReason = nullptr);
|
|
bool runSurrogateScoringScriptOnce(const QString& candidatePath, const QString& scorePath, QString* failureReason = nullptr);
|
|
bool ensureSurrogateScoringServer(QString* failureReason = nullptr);
|
|
bool requestSurrogateScoresFromServer(const QString& candidatePath, const QString& scorePath, QString* failureReason = nullptr);
|
|
void stopSurrogateScoringServer();
|
|
QVector<double> readSurrogateScores(const QString& scorePath) const;
|
|
bool forceSolverByFallbackGate(const QVector<double>& selectedParameters) const;
|
|
bool isStrongDecliningProductionSchedule(double* endStartRatio = nullptr) const;
|
|
bool isSurrogateRunContextSupported(QString* reason) const;
|
|
bool isSurrogateCandidateInDomain(const QVector<double>& selectedParameters, QString* reason) const;
|
|
|
|
// ===== 验证和处理方法 =====
|
|
bool validateParameters(const QVector<double>& parameters) const;
|
|
bool validateLogLogData(const QVector<QVector<double> >& logLogData) const;
|
|
bool validateInitialValues() const; // 验证初始值有效性
|
|
bool validateSolverResult(const QVector<QVector<double>>& result) const;
|
|
double calculateCurveError(const QVector<QPointF>& curve1, const QVector<QPointF>& curve2) const;
|
|
double calculateLogLogCurveError(const QVector<QVector<double> >& target,
|
|
const QVector<QVector<double> >& result) const;
|
|
double calculateWeightedPointError(double target, double result, double timeWeight) const;
|
|
|
|
private:
|
|
// ===== 运行状态 =====
|
|
bool m_isRunning;
|
|
bool m_shouldStop;
|
|
bool m_isPaused;
|
|
int m_currentIteration;
|
|
QString m_lastError;
|
|
|
|
// ===== PSO数据 =====
|
|
QVector<double> m_initialValues; // 用户设置的初始值
|
|
QVector<AutoFitParticle> m_swarm;
|
|
QVector<double> m_globalBestPosition;
|
|
double m_globalBestFitness;
|
|
double m_previousBestFitness;
|
|
QVector<QVector<double> > m_lastEvaluatedLogLogData;
|
|
QVector<QVector<double> > m_globalBestLogLogData;
|
|
QVector<QVector<double> > m_userInitialLogLogData;
|
|
|
|
// ===== 优化配置 =====
|
|
QVector<bool> m_parameterSelected;
|
|
QVector<double> m_parameterLower;
|
|
QVector<double> m_parameterUpper;
|
|
QVector<int> m_enabledParamIndices;
|
|
QVector<QVector<double> > m_targetLogLogData;
|
|
QString m_targetWellName;
|
|
|
|
// ===== 算法配置 =====
|
|
int m_swarmSize;
|
|
int m_maxIterations;
|
|
double m_targetError;
|
|
double m_inertiaWeight;
|
|
double m_cognitiveParam;
|
|
double m_socialParam;
|
|
|
|
// ===== 统计信息 =====
|
|
int m_totalEvaluations;
|
|
int m_successfulEvaluations;
|
|
QVector<double> m_convergenceHistory;
|
|
|
|
// ===== 常量 =====
|
|
static const double MIN_FITNESS_IMPROVEMENT;
|
|
static const double VELOCITY_LIMIT_FACTOR;
|
|
static const int CONVERGENCE_CHECK_INTERVAL;
|
|
|
|
// ===== 资源管理 =====
|
|
volatile int m_evaluationInProgress; // 并发控制
|
|
int m_consecutiveFailures; // 连续失败计数
|
|
|
|
// ===== 精英保护 =====
|
|
QVector<double> m_userInitialSolution; // 用户初始解
|
|
double m_userInitialFitness; // 用户初始解的适应度
|
|
double m_improvementThreshold; // 改进阈值
|
|
bool m_hasValidUserSolution; // 是否有有效的用户解
|
|
|
|
int m_consecutiveFailedIterations; // 连续失败迭代次数
|
|
int m_maxConsecutiveFailures; // 最大允许连续失败次数
|
|
|
|
// 收敛判断相关
|
|
double m_diversityThreshold; // 多样性阈值
|
|
QVector<double> m_diversityHistory; // 粒子群多样性历史
|
|
QVector<double> m_velocityHistory; // 平均速度历史
|
|
QVector<double> m_particleStagnationHistory; // 粒子停滞率历史
|
|
|
|
// 收敛判断参数
|
|
double m_convergenceVarianceThreshold; // 收敛方差阈值
|
|
double m_velocityConvergenceThreshold; // 速度收敛阈值
|
|
int m_trueConvergenceWindow; // 真收敛观察窗口
|
|
int m_localOptimumWindow; // 局部最优观察窗口
|
|
|
|
// 质量评估参数
|
|
double m_nearTargetFactor; // 接近目标的倍数因子
|
|
double m_farTargetFactor; // 远离目标的倍数因子
|
|
|
|
// DLL求解器需要的临时目录
|
|
QString m_tempDirectory;
|
|
|
|
// Baseline trace output. It is kept separate from legacy UI counters.
|
|
bool m_traceEnabled;
|
|
QString m_traceRunId;
|
|
QString m_traceFilePath;
|
|
QString m_traceMetaFilePath;
|
|
QFile m_traceFile;
|
|
bool m_surrogateScreeningEnabled;
|
|
unsigned int m_psoRandomSeed;
|
|
QString m_surrogateRunContextSummary;
|
|
int m_surrogateWarmupIterationCount;
|
|
int m_surrogatePeriodicAuditIterationCount;
|
|
int m_surrogateContextBlockedIterationCount;
|
|
int m_surrogateActiveIterationCount;
|
|
int m_surrogateSelectedParticleCount;
|
|
int m_surrogateScreenedParticleCount;
|
|
int m_surrogateTopKParticleCount;
|
|
int m_surrogateAuditParticleCount;
|
|
int m_surrogateFallbackParticleCount;
|
|
int m_surrogateDomainParticleCount;
|
|
int m_surrogateMinFloorParticleCount;
|
|
QProcess* m_surrogateScoringProcess;
|
|
QString m_surrogateScoringServerKey;
|
|
|
|
private:
|
|
// 模拟拟合相关成员
|
|
bool m_simulationMode;
|
|
QTimer* m_simulationTimer;
|
|
int m_simulationIteration;
|
|
int m_simulationMaxIterations;
|
|
QVector<double> m_simulationTargetParams;
|
|
QVector<double> m_simulationStartParams;
|
|
double m_simulationTargetError;
|
|
double m_simulationStartError;
|
|
double m_simulationCurrentError;
|
|
int m_simulationLogInterval;
|
|
QTime m_simulationStartTime;
|
|
|
|
// 用于逐粒子输出的状态
|
|
int m_simulationCurrentParticle; // 当前正在处理的粒子索引
|
|
int m_simulationSuccessCount; // 当前迭代成功的粒子数
|
|
int m_simulationFailCount; // 当前迭代失败的粒子数
|
|
bool m_simulationIterationStarted; // 当前迭代是否已开始
|
|
double m_simulationPreviousIterError; // 上一次迭代的误差
|
|
|
|
// 模拟拟合方法
|
|
void runSimulatedFitting();
|
|
void emitSimulationLog(int iteration);
|
|
double calculateSimulatedError(int iteration);
|
|
QVector<double> calculateSimulatedParams(int iteration);
|
|
private:
|
|
void startNewSimulationIteration();
|
|
void emitParticleLog(int particleIndex);
|
|
void finishCurrentIteration();
|
|
void finishSimulation();
|
|
QVector<QVector<double> > buildSimulatedLogLogData(double progress) const;
|
|
|
|
};
|
|
|
|
#endif // NMCALCULATIONAUTOFIT_H
|