|
|
#ifndef NMFITLM_H
|
|
|
#define NMFITLM_H
|
|
|
|
|
|
#include "nmFitTypes.h"
|
|
|
#include <QObject>
|
|
|
#include <QAtomicInt>
|
|
|
#include <limits>
|
|
|
|
|
|
#include "nmCalculation_global.h"
|
|
|
|
|
|
// 双对数曲线误差分解。预调整使用纵向偏差,形状阶段使用双曲线斜率残差,
|
|
|
// 总误差部分只使用 total 接受候选;两个 LM 目标均联合调整有效参数。
|
|
|
struct AutoFitObjectiveBreakdownLM
|
|
|
{
|
|
|
bool valid; ///< 本次曲线评价及诊断是否有效。
|
|
|
double total; ///< 压差与导数等权合并的对数残差均方根。
|
|
|
double pressureLoss; ///< 压差曲线的对数残差均方根。
|
|
|
double derivativeLoss; ///< 导数曲线的对数残差均方根。
|
|
|
QVector<double> residualVector; ///< 已包含采样权重的压差与导数残差。
|
|
|
double verticalCommonBias; ///< 两条曲线的公共纵向偏差,保留方向符号。
|
|
|
double verticalLoss;
|
|
|
bool verticalReliable; ///< 两个通道的纵向偏差是否适合高度调整。
|
|
|
double horizontalPhysicalShift; ///< 对数时间位移;正值表示模拟曲线偏右。
|
|
|
double horizontalLoss;
|
|
|
bool horizontalReliable; ///< 水平位移诊断是否可靠。
|
|
|
bool registrationAmbiguous; ///< 水平、纵向偏差是否存在配准歧义。
|
|
|
// 与整体误差共用 log-time 采样网格,斜率跨度约为完整对数时域的 10%。
|
|
|
QVector<double> shapeResiduals;
|
|
|
double pressureVerticalBias;
|
|
|
double derivativeVerticalBias;
|
|
|
double shapeLoss;
|
|
|
|
|
|
AutoFitObjectiveBreakdownLM()
|
|
|
: valid(false), total(1.0e10), pressureLoss(std::numeric_limits<double>::quiet_NaN()),
|
|
|
derivativeLoss(std::numeric_limits<double>::quiet_NaN()),
|
|
|
verticalCommonBias(std::numeric_limits<double>::quiet_NaN()),
|
|
|
verticalLoss(std::numeric_limits<double>::quiet_NaN()), verticalReliable(false),
|
|
|
horizontalPhysicalShift(std::numeric_limits<double>::quiet_NaN()),
|
|
|
horizontalLoss(std::numeric_limits<double>::quiet_NaN()), horizontalReliable(false),
|
|
|
registrationAmbiguous(false), pressureVerticalBias(0.0), derivativeVerticalBias(0.0),
|
|
|
shapeLoss(std::numeric_limits<double>::quiet_NaN())
|
|
|
{
|
|
|
}
|
|
|
};
|
|
|
|
|
|
// 有限差分 + LM/信赖域拟合的停止原因。
|
|
|
enum StopReasonLM
|
|
|
{
|
|
|
LM_CONTINUE_OPTIMIZATION = 0,
|
|
|
LM_TARGET_ACHIEVED = 1,
|
|
|
LM_LOCAL_OPTIMUM = 3,
|
|
|
LM_MAX_ITERATIONS = 4,
|
|
|
LM_USER_STOPPED = 5,
|
|
|
LM_CONSECUTIVE_FAILURES = 6,
|
|
|
LM_OPTIMIZATION_FAILED = 7
|
|
|
};
|
|
|
|
|
|
/** @brief LM 拟合模块,通过评价回调获取候选曲线并优化所选参数。 */
|
|
|
class NMCALCULATION_EXPORT nmFitLM : public QObject
|
|
|
{
|
|
|
Q_OBJECT
|
|
|
public:
|
|
|
explicit nmFitLM(QObject* parent = nullptr);
|
|
|
virtual ~nmFitLM();
|
|
|
/** @brief 同步拟合;回调及其完整模型在返回前必须存活,实例不可重入。 */
|
|
|
nmFitResult fit(const nmFitRequest& request, const nmFitEvaluator& evaluate,
|
|
|
const std::function<bool()>& cancelled = std::function<bool()>(),
|
|
|
const nmFitCallbacks& callbacks = nmFitCallbacks());
|
|
|
/** @brief 设置未取对数的时间、压差、导数三列曲线;fit 使用请求中的 targetCurve。 */
|
|
|
void setTargetLogLogData(const QVector<QVector<double>>& targetData);
|
|
|
/** @brief 请求停止拟合;正在执行的评价回调由调用方负责结束。 */
|
|
|
void stopFitting();
|
|
|
/** @brief 查询是否正在执行拟合;不用于跨线程同步。 */
|
|
|
bool isRunning() const;
|
|
|
/** @brief 查询当前内部迭代序号;运行前为 -1,迭代时从 0 开始。 */
|
|
|
int getCurrentIteration() const;
|
|
|
/** @brief 查询评价尝试次数,可能包含失败或取消的评价。 */
|
|
|
int getTotalEvaluations() const;
|
|
|
/** @brief 查询求解调用次数;独立使用时对应评价回调的调用次数。 */
|
|
|
int getSolverCallCount() const;
|
|
|
/** @brief 返回当前已接受参数值,顺序与请求中选中的参数一致。 */
|
|
|
QVector<double> getBestSolution() const;
|
|
|
/** @brief 返回当前已接受工作点的总误差。 */
|
|
|
double getBestFitness() const;
|
|
|
/** @brief 返回当前缓存的误差分解;拒绝候选后恢复为已接受工作点的数据。 */
|
|
|
AutoFitObjectiveBreakdownLM getLastObjectiveBreakdown() 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 查询本轮是否拟合影响网格的裂缝参数。 */
|
|
|
bool fitsFractureGeometry() const;
|
|
|
/** @brief 返回停止原因的可读说明。 */
|
|
|
QString getStopReasonDescription(StopReasonLM reason) const;
|
|
|
/** @brief 返回本轮有效评价次数。 */
|
|
|
int getSuccessfulEvaluations() const
|
|
|
{
|
|
|
return m_successfulEvaluations;
|
|
|
}
|
|
|
/** @brief 返回用户初始参数对应的误差;未评价或无有效结果时不小于 1e9。 */
|
|
|
double getInitialFitness() const
|
|
|
{
|
|
|
return m_userInitialFitness;
|
|
|
}
|
|
|
/** @brief 返回固定比较区间的起始时间,尚未建立区间时为 0。 */
|
|
|
double getComparisonTimeMin() const
|
|
|
{
|
|
|
return m_comparisonTimeMin;
|
|
|
}
|
|
|
/** @brief 返回固定比较区间的结束时间,尚未建立区间时为 0。 */
|
|
|
double getComparisonTimeMax() const
|
|
|
{
|
|
|
return m_comparisonTimeMax;
|
|
|
}
|
|
|
signals:
|
|
|
/** @brief 发布单次评价的诊断数据;指针仅在本次同步通知期间有效。 */
|
|
|
void traceRowGenerated(int iteration, int parameterIndex, const QString& phase, const QVector<double>& parameters,
|
|
|
double solverObjective, bool solverSuccess, int elapsedMs, const QString& decision,
|
|
|
const AutoFitObjectiveBreakdownLM* objectiveBreakdown);
|
|
|
/** @brief 本轮配置及运行状态初始化完成。 */
|
|
|
void runInitialized();
|
|
|
/** @brief 比较时间区间已确定,可通过区间查询接口读取。 */
|
|
|
void comparisonRangeChanged();
|
|
|
|
|
|
// 阶段通知:0 初始求解,1 高度,2 形状,3 总误差。
|
|
|
void fittingStageChanged(int stage);
|
|
|
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();
|
|
|
/** @brief 提取所选参数的初值,并限制到各自上下界内。 */
|
|
|
void extractUserInitialValues();
|
|
|
/** @brief 评价候选点,同时返回曲线、误差分解及耗时。 */
|
|
|
bool evaluateTrustRegionPoint(const QVector<double>& parameters, double* fitness,
|
|
|
AutoFitObjectiveBreakdownLM* breakdown, QVector<QVector<double>>* curve,
|
|
|
int* elapsedMs);
|
|
|
/** @brief 执行高度预调整、形状拟合及总误差拟合。 */
|
|
|
StopReasonLM runTrustRegionFitting();
|
|
|
/** @brief 检查最终结果,必要时恢复有效的用户初始解。 */
|
|
|
void validateAndProtectFinalResult();
|
|
|
int getEnabledParameterCount() const;
|
|
|
double calculateLogLogCurveError(const QVector<QVector<double>>& target, const QVector<QVector<double>>& result);
|
|
|
int parameterType(int index) const;
|
|
|
void emitTrialLog(const QString& title, const AutoFitObjectiveBreakdownLM& before,
|
|
|
const AutoFitObjectiveBreakdownLM& after, bool valid, bool accepted, bool shapeStage);
|
|
|
/** @brief 读取并校验本轮请求中的参数、边界和停止条件。 */
|
|
|
bool loadConfiguration();
|
|
|
/** @brief 将一次评价结果发送给诊断通知接收方。 */
|
|
|
void emitTraceRow(int iteration, int parameterIndex, const QString& phase, const QVector<double>& parameters,
|
|
|
double solverObjective, bool solverSuccess, int elapsedMs, const QString& decision,
|
|
|
const AutoFitObjectiveBreakdownLM* objectiveBreakdown = nullptr);
|
|
|
/** @brief 调用评价回调,校验返回曲线并计算拟合误差。 */
|
|
|
double evaluateFitness(const QVector<double>& parameters, bool retrySolver = true);
|
|
|
/** @brief 通过调用方回调恢复已接受的物理参数。 */
|
|
|
void restoreAcceptedParameters(const QVector<double>& parameters);
|
|
|
|
|
|
private:
|
|
|
bool m_isRunning;
|
|
|
QAtomicInt m_shouldStop;
|
|
|
int m_currentIteration;
|
|
|
QString m_lastError;
|
|
|
QVector<double> m_initialValues;
|
|
|
QVector<double> m_globalBestPosition;
|
|
|
double m_globalBestFitness;
|
|
|
AutoFitObjectiveBreakdownLM m_globalBestObjectiveBreakdown;
|
|
|
QVector<QVector<double>> m_lastEvaluatedLogLogData;
|
|
|
QVector<QVector<double>> m_globalBestLogLogData;
|
|
|
AutoFitObjectiveBreakdownLM m_lastObjectiveBreakdown;
|
|
|
QVector<QVector<double>> m_userInitialLogLogData;
|
|
|
AutoFitObjectiveBreakdownLM m_userInitialObjectiveBreakdown;
|
|
|
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;
|
|
|
double m_comparisonTimeMin;
|
|
|
double m_comparisonTimeMax;
|
|
|
QString m_targetWellName;
|
|
|
int m_maxIterations;
|
|
|
double m_targetError;
|
|
|
int m_totalEvaluations;
|
|
|
QAtomicInt m_solverCallCount;
|
|
|
int m_successfulEvaluations;
|
|
|
int m_maxConsecutiveFailures;
|
|
|
QVector<double> m_userInitialSolution;
|
|
|
double m_userInitialFitness;
|
|
|
bool m_hasValidUserSolution;
|
|
|
nmFitCallbacks m_callbacks;
|
|
|
nmFitRequest m_request;
|
|
|
nmFitEvaluator m_evaluate;
|
|
|
std::function<bool()> m_cancelled;
|
|
|
int m_stopReason;
|
|
|
bool m_skipShapeStage;
|
|
|
};
|
|
|
|
|
|
#endif // NMFITLM_H
|