|
|
#ifndef NMFITPSO_H
|
|
|
#define NMFITPSO_H
|
|
|
|
|
|
#include "nmFitTypes.h"
|
|
|
#include <QObject>
|
|
|
#include <QAtomicInt>
|
|
|
#include "nmPsoAcceleration.h"
|
|
|
|
|
|
#include "nmCalculation_global.h"
|
|
|
|
|
|
// PSO 停止原因。主循环每一代结束时会根据目标误差、收敛历史、粒子群多样性、
|
|
|
// 连续失败次数等状态给出一个停止判断。
|
|
|
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 ///< 优化失败
|
|
|
};
|
|
|
|
|
|
/** @brief PSO 拟合模块,通过评价回调获取候选参数对应的曲线。 */
|
|
|
class NMCALCULATION_EXPORT nmFitPSO : public QObject
|
|
|
{
|
|
|
Q_OBJECT
|
|
|
public:
|
|
|
explicit nmFitPSO(QObject* parent = nullptr);
|
|
|
virtual ~nmFitPSO();
|
|
|
/** @brief 同步拟合;回调及其完整模型在返回前必须存活,实例不可重入。 */
|
|
|
nmFitResult fit(const nmFitRequest& request, const nmFitEvaluator& evaluate,
|
|
|
const std::function<bool()>& cancelled = std::function<bool()>(),
|
|
|
nmPsoAcceleration* acceleration = nullptr, const nmFitCallbacks& callbacks = nmFitCallbacks());
|
|
|
/** @brief 协作式停止,正在执行的评价由调用方结束。 */
|
|
|
void stopFitting();
|
|
|
/** @brief 设置未取对数的时间、压差(或拟压差)、导数三列曲线;fit 使用请求中的 targetCurve。 */
|
|
|
void setTargetLogLogData(const QVector<QVector<double>>& targetData);
|
|
|
/** @brief 查询是否正在执行拟合;不用于跨线程同步。 */
|
|
|
bool isRunning() const;
|
|
|
/** @brief 查询当前迭代序号,从 0 开始。 */
|
|
|
int getCurrentIteration() const;
|
|
|
/** @brief 查询评价尝试次数,可能包含失败或取消的评价。 */
|
|
|
int getTotalEvaluations() const;
|
|
|
/** @brief 返回已选中参数的最优值,顺序与请求参数表一致。 */
|
|
|
QVector<double> getBestSolution() const;
|
|
|
/** @brief 返回真实评价得到的最优误差。 */
|
|
|
double getBestFitness() const;
|
|
|
/** @brief 返回最近一次输入或计算错误说明。 */
|
|
|
QString getLastError() const;
|
|
|
/** @brief 清空迭代状态;只能在拟合结束后由调用方调用。 */
|
|
|
void resetOptimizer();
|
|
|
/** @brief 校验候选参数的维度、有限性及上下界。 */
|
|
|
bool validateParameters(const QVector<double>& parameters) const;
|
|
|
/** @brief 校验双对数曲线的三列数据。 */
|
|
|
bool validateLogLogData(const QVector<QVector<double>>& logLogData) const;
|
|
|
/** @brief 校验真实求解返回的压力曲线。 */
|
|
|
bool validateSolverResult(const QVector<QVector<double>>& result) const;
|
|
|
/** @brief 返回停止原因的可读说明。 */
|
|
|
QString getStopReasonDescription(StopReasonPSO reason) const;
|
|
|
/** @brief 返回本轮有效评价次数。 */
|
|
|
int getSuccessfulEvaluations() const
|
|
|
{
|
|
|
return m_successfulEvaluations;
|
|
|
}
|
|
|
/** @brief 返回用户初始参数对应的误差;未评价或无有效结果时不小于 1e9。 */
|
|
|
double getInitialFitness() const
|
|
|
{
|
|
|
return m_userInitialFitness;
|
|
|
}
|
|
|
/** @brief 返回当前惯性权重。 */
|
|
|
double getInertiaWeight() const
|
|
|
{
|
|
|
return m_inertiaWeight;
|
|
|
}
|
|
|
/** @brief 返回当前个体学习因子。 */
|
|
|
double getCognitiveParam() const
|
|
|
{
|
|
|
return m_cognitiveParam;
|
|
|
}
|
|
|
/** @brief 返回当前群体学习因子。 */
|
|
|
double getSocialParam() const
|
|
|
{
|
|
|
return m_socialParam;
|
|
|
}
|
|
|
/** @brief 返回本轮粒子搜索使用的随机种子。 */
|
|
|
unsigned int getRandomSeed() const
|
|
|
{
|
|
|
return m_psoRandomSeed;
|
|
|
}
|
|
|
signals:
|
|
|
/** @brief 同步诊断通知,接收方自行决定是否记录。 */
|
|
|
void traceRowGenerated(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);
|
|
|
/** @brief 本轮配置及运行状态初始化完成。 */
|
|
|
void runInitialized();
|
|
|
|
|
|
void progressUpdated(int iteration, double bestFitness);
|
|
|
void bestCurveUpdated(QVector<QVector<double>> targetData, QVector<QVector<double>> bestData, int iteration,
|
|
|
double fitness);
|
|
|
|
|
|
void logMessageGenerated(const QString& message);
|
|
|
|
|
|
private:
|
|
|
/** @brief 执行输入校验、粒子群初始化及优化迭代。 */
|
|
|
bool runOptimization();
|
|
|
/** @brief 通过可选回调调度事件并检查取消状态。 */
|
|
|
void pollEvents();
|
|
|
void extractUserInitialValues();
|
|
|
void initializeSwarm();
|
|
|
void updateParticle(int particleIndex);
|
|
|
void updateGlobalBest();
|
|
|
void updateVelocityAndPosition();
|
|
|
QVector<QPointF> interpolateData(const QVector<QPointF>& source, const QVector<double>& targetX) const;
|
|
|
void adaptiveParameterUpdate(int iteration);
|
|
|
void validateAndProtectFinalResult();
|
|
|
double random01() const;
|
|
|
void clampToLimits(QVector<double>& parameters) const;
|
|
|
int getEnabledParameterCount() 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 calculatePointError(double target, double result) const;
|
|
|
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;
|
|
|
void resetGuideBestToVerified();
|
|
|
/** @brief 从本轮拟合请求读取配置并校验参数。 */
|
|
|
bool loadConfiguration();
|
|
|
/** @brief 发布单次评价的诊断通知。 */
|
|
|
void emitTraceRow(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);
|
|
|
/** @brief 通过评价回调获取曲线并计算适应度。 */
|
|
|
double evaluateFitness(const QVector<double>& parameters);
|
|
|
/** @brief 发布本代全部粒子的诊断通知。 */
|
|
|
void emitIterationTraceRows();
|
|
|
QVector<bool> buildSurrogateEvaluationMask();
|
|
|
bool isSurrogateScreeningEnabled() const;
|
|
|
|
|
|
private:
|
|
|
bool m_isRunning; ///< 当前是否有一次自动拟合正在运行。
|
|
|
QAtomicInt m_shouldStop; ///< 协作式停止标志,由迭代流程及评价前后的检查读取。
|
|
|
int m_currentIteration; ///< 当前 PSO 迭代序号,从 0 开始。
|
|
|
QString m_lastError; ///< 最近一次失败原因,供调用方查询。
|
|
|
QVector<double> m_initialValues; ///< 从本轮请求提取的参数初始值,顺序与 m_enabledParamIndices 一致。
|
|
|
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<nmFitParameterValue> m_modelParameters; ///< 按井分组的完整参数列表,储层参数共享。
|
|
|
QVector<bool> m_parameterSelected; ///< 每项参数的勾选状态。
|
|
|
QVector<double> m_parameterLower; ///< 完整参数列表的搜索下界。
|
|
|
QVector<double> m_parameterUpper; ///< 完整参数列表的搜索上界。
|
|
|
QVector<int> m_enabledParamIndices; ///< 被勾选参数在完整列表中的索引。
|
|
|
QVector<QVector<double>> m_targetLogLogData; ///< 目标曲线:时间、压差(或拟压差)、导数,均未取对数。
|
|
|
int m_swarmSize; ///< 粒子数量,当前默认 20。
|
|
|
int m_maxIterations; ///< 最大迭代次数,来自自动拟合配置。
|
|
|
double m_targetError; ///< 目标误差,小于该值视为达到拟合目标。
|
|
|
double m_inertiaWeight; ///< 惯性权重,控制粒子保留上一轮速度的程度。
|
|
|
double m_cognitiveParam; ///< 个体学习因子,控制粒子靠近自身 pbest 的程度。
|
|
|
double m_socialParam; ///< 群体学习因子,控制粒子靠近全局 gbest 的程度。
|
|
|
int m_totalEvaluations; ///< 本轮评价尝试次数,可能包含失败或取消的评价。
|
|
|
int m_successfulEvaluations; ///< 真实求解器成功且误差有效的评价次数。
|
|
|
QVector<double> m_convergenceHistory; ///< 每代全局最优误差历史,用于收敛判断。
|
|
|
QVector<double> m_userInitialSolution; ///< 用户初始解参数,若最终改进不足会恢复它。
|
|
|
double m_userInitialFitness; ///< 用户初始解真实误差。
|
|
|
double m_improvementThreshold; ///< 最终结果相对初始解至少需要达到的改进阈值。
|
|
|
bool m_hasValidUserSolution; ///< 初始解是否成功跑过真实求解器。
|
|
|
int m_consecutiveFailedIterations; ///< 连续失败迭代次数。
|
|
|
int m_maxConsecutiveFailures; ///< 最大允许连续失败次数。
|
|
|
double m_diversityThreshold; ///< 多样性阈值。
|
|
|
double m_convergenceVarianceThreshold; ///< 收敛方差阈值。
|
|
|
double m_velocityConvergenceThreshold; ///< 速度收敛阈值。
|
|
|
int m_trueConvergenceWindow; ///< 真收敛观察窗口。
|
|
|
int m_localOptimumWindow; ///< 局部最优观察窗口。
|
|
|
double m_farTargetFactor; ///< 远离目标的倍数因子。
|
|
|
unsigned int m_psoRandomSeed; ///< PSO 随机种子,用于复现粒子搜索过程。
|
|
|
static const double VELOCITY_LIMIT_FACTOR; ///< 粒子速度上限占参数搜索区间的比例。
|
|
|
nmFitCallbacks m_callbacks;
|
|
|
nmFitRequest m_request;
|
|
|
nmFitEvaluator m_evaluate;
|
|
|
std::function<bool()> m_cancelled;
|
|
|
int m_stopReason;
|
|
|
nmPsoAcceleration* m_acceleration;
|
|
|
};
|
|
|
|
|
|
#endif // NMFITPSO_H
|