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.
158 lines
6.6 KiB
C++
158 lines
6.6 KiB
C++
#ifndef NMCALCULATIONAUTOFITPSO_H
|
|
#define NMCALCULATIONAUTOFITPSO_H
|
|
|
|
#include "nmCalculation_global.h"
|
|
#include "nmDataAutomaticFitting.h"
|
|
#include "nmFitPSO.h"
|
|
#include "nmFitPlatformParameters.h"
|
|
#include <QFile>
|
|
#include <QTimer>
|
|
#include "nmSurrogate.h"
|
|
|
|
/** @brief 软件拟合入口:读取项目配置、调用真实求解器并提交正式成果。 */
|
|
class NMCALCULATION_EXPORT nmCalculationAutoFitPSO : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit nmCalculationAutoFitPSO(QObject* parent = nullptr);
|
|
virtual ~nmCalculationAutoFitPSO();
|
|
/** @brief 调用独立算法,再按原软件流程提交正式成果。 */
|
|
bool startAutoFitting();
|
|
void stopFitting();
|
|
void setSimulationMode(bool enabled);
|
|
void setSimulationTargetParams(const QVector<double>& targetParams, double targetError);
|
|
bool isSimulationMode() const
|
|
{
|
|
return m_simulationMode;
|
|
}
|
|
|
|
/** @brief 设置目标曲线及查询运行结果,保持原界面调用方式。 */
|
|
void setTargetLogLogData(const QVector<QVector<double>>& data);
|
|
bool isRunning() const;
|
|
int getCurrentIteration() const;
|
|
int getTotalEvaluations() const;
|
|
QVector<double> getBestSolution() const;
|
|
double getBestFitness() const;
|
|
QString getLastError() const;
|
|
void resetOptimizer();
|
|
void setPSOTargetWellName(const QString& name);
|
|
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);
|
|
void finalizingStarted();
|
|
void logMessageGenerated(const QString& message);
|
|
|
|
private slots:
|
|
void initializeTraceFile();
|
|
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);
|
|
|
|
private:
|
|
// 项目读写、求解与成果提交只在软件入口中实现。
|
|
void syncAlgorithmProgress();
|
|
int getEnabledParameterCount() const
|
|
{
|
|
return m_enabledParamIndices.size();
|
|
}
|
|
bool initializeTemporaryDirectory();
|
|
void cleanupTemporaryDirectory();
|
|
void resetRunSummary();
|
|
void captureSurrogateRunContextSummary();
|
|
void emitRunSummary(bool success, StopReasonPSO finalReason);
|
|
void closeTraceFile();
|
|
void writeTraceHeader();
|
|
bool writeTraceMetaFile(const QString& filePath);
|
|
QVector<double> buildTraceParameterVector(const QVector<double>& selectedParameters) const;
|
|
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;
|
|
void stopSurrogateScoringServer();
|
|
bool isSurrogateRunContextSupported(QString* reason) const;
|
|
bool loadConfiguration();
|
|
void loadOptimizationConfig();
|
|
void loadParameterBounds();
|
|
nmFitCurve evaluateCurve(const QVector<double>& parameters, bool retrySolver);
|
|
void applyParametersToDataManager(const QVector<double>& parameters);
|
|
QVector<QVector<double>> runSolver();
|
|
void saveOptimizationResult();
|
|
QVector<QVector<double>> runSolverDll();
|
|
bool runFinalFullSolver();
|
|
QVector<QVector<double>> buildSimulatedLogLogData(double progress) const;
|
|
void runSimulatedFitting();
|
|
private slots:
|
|
void onSimulationTimerTick();
|
|
|
|
private:
|
|
double calculateSimulatedError(int iteration);
|
|
QVector<double> calculateSimulatedParams(int iteration);
|
|
void startNewSimulationIteration();
|
|
void emitParticleLog(int particleIndex);
|
|
void finishCurrentIteration();
|
|
void finishSimulation();
|
|
void pollEvents();
|
|
|
|
private:
|
|
nmFitPSO* m_engine; ///< 独立算法对象,通过 fit 接口调用。
|
|
bool m_isRunning;
|
|
QAtomicInt m_shouldStop;
|
|
bool m_isFinalizing;
|
|
int m_currentIteration;
|
|
QString m_lastError;
|
|
QVector<double> m_initialValues;
|
|
QVector<double> m_globalBestPosition;
|
|
double m_globalBestFitness;
|
|
nmFitCurve m_lastEvaluatedLogLogData;
|
|
nmFitCurve m_globalBestLogLogData;
|
|
QVector<nmFitParameterValue> m_modelParameters;
|
|
QVector<bool> m_parameterSelected;
|
|
QVector<double> m_parameterLower;
|
|
QVector<double> m_parameterUpper;
|
|
QVector<int> m_enabledParamIndices;
|
|
nmFitCurve m_targetLogLogData;
|
|
QString m_targetWellName;
|
|
int m_maxIterations;
|
|
double m_targetError;
|
|
int m_totalEvaluations;
|
|
int m_successfulEvaluations;
|
|
int m_evaluationInProgress;
|
|
QString m_tempDirectory;
|
|
QString m_traceRunId;
|
|
QString m_traceFilePath;
|
|
QString m_traceMetaFilePath;
|
|
QFile m_traceFile;
|
|
int m_swarmSize; ///< 仅用于演示模式和诊断元数据。
|
|
bool m_traceEnabled;
|
|
bool m_surrogateScreeningEnabled;
|
|
|
|
QString m_surrogateRunContextSummary; ///< 软件侧运行摘要。
|
|
nmSurrogate* m_proxy; ///< 独占代理会话,由 QObject 父对象管理。
|
|
void configureSurrogate();
|
|
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; ///< 仿真日志输出间隔,单位秒。
|
|
int m_simulationCurrentParticle; ///< 当前正在处理的粒子索引
|
|
int m_simulationSuccessCount; ///< 当前迭代成功的粒子数
|
|
int m_simulationFailCount; ///< 当前迭代失败的粒子数
|
|
bool m_simulationIterationStarted; ///< 当前迭代是否已开始
|
|
double m_simulationPreviousIterError; ///< 上一次迭代的误差
|
|
};
|
|
|
|
#endif // NMCALCULATIONAUTOFITPSO_H
|