131 lines
5.5 KiB
C++
131 lines
5.5 KiB
C++
#pragma once
|
||
/**
|
||
* @file sweep.h
|
||
* @brief 设计参数扫描 / DOE 采样与批量回调(P4-28,VDE-1253 最小实现)
|
||
*
|
||
* 提供与 UI/求解器无关的最小 DOE 能力:
|
||
* - 设计变量域定义(名称/上下界/整数标志;Grid 模式的 levels);
|
||
* - 采样:全因子网格(Grid)或 Latin Hypercube(LHS,确定性种子);
|
||
* - 批量执行:对每个样本调用用户回调并收集结果/错误/成功失败计数;
|
||
*
|
||
* 本模块只负责"样本生成 + 批量调度",不包含:
|
||
* - 几何重建/求解器本身(由宿主或 CAE/CAM 外部服务完成);
|
||
* - 优化算法、代理模型训练、多物理场耦合(见 docs/feedback/VDE-1253.md 的 VDE 侧响应)。
|
||
*
|
||
* @code{.cpp}
|
||
* std::vector<vde::param::DesignVariable> vars = {
|
||
* {"width", 10.0, 20.0, 3, false},
|
||
* {"height", 5.0, 15.0, 2, false},
|
||
* };
|
||
* vde::param::SweepOptions options;
|
||
* options.method = vde::param::SamplingMethod::Grid;
|
||
* auto batch = vde::param::run_batch(vars, options,
|
||
* [](const vde::param::DesignPoint& point) {
|
||
* vde::param::BatchSampleResult r;
|
||
* r.outputs["area"] = point.at("width") * point.at("height");
|
||
* return r;
|
||
* });
|
||
* // batch.completed == 6, batch.succeeded == 6
|
||
* @endcode
|
||
*
|
||
* @ingroup param
|
||
*/
|
||
|
||
#include <cstddef>
|
||
#include <cstdint>
|
||
#include <functional>
|
||
#include <map>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::param {
|
||
|
||
/// 一个设计变量(参数域)
|
||
struct DesignVariable {
|
||
std::string name; ///< 唯一变量名(与 ParameterManager 参数名对应)
|
||
double min_value = 0.0; ///< 下界(含)
|
||
double max_value = 1.0; ///< 上界(含)
|
||
std::size_t levels = 2; ///< Grid 模式的分级数(>=2;LHS 忽略)
|
||
bool integer = false; ///< true:采样值四舍五入并钳制到域内
|
||
};
|
||
|
||
/// 采样方式
|
||
enum class SamplingMethod {
|
||
Grid = 0, ///< 全因子网格(变量顺序为设计变量声明顺序;最后一个变量变化最快)
|
||
LatinHypercube = 1 ///< Latin Hypercube(每维每个分层各取一个样本,确定性种子)
|
||
};
|
||
|
||
/// 采样选项
|
||
struct SweepOptions {
|
||
SamplingMethod method = SamplingMethod::LatinHypercube;
|
||
std::size_t samples = 16; ///< LHS 样本数(Grid 忽略;必须 >0)
|
||
std::uint32_t seed = 0; ///< LHS 随机种子(同种子结果可复现)
|
||
std::size_t max_samples = 1000000; ///< 安全上限(Grid 乘积/LHS 样本数)
|
||
/// >1 时 run_batch 并行执行样本(OpenMP);回调必须线程安全。
|
||
/// 有 cancel 回调时仍串行,保证取消语义确定。
|
||
int max_threads = 1;
|
||
std::function<bool()> cancel; ///< 可选取消回调(每个样本前检查)
|
||
};
|
||
|
||
/// 单个设计点:变量名 → 数值
|
||
using DesignPoint = std::map<std::string, double>;
|
||
|
||
/// 采样结果
|
||
struct SampleSet {
|
||
std::vector<DesignPoint> points; ///< 生成的样本(顺序确定)
|
||
std::string error; ///< 非空 = 域/参数错误,points 为空
|
||
|
||
[[nodiscard]] bool ok() const { return error.empty(); }
|
||
};
|
||
|
||
/// 单个样本的回调结果
|
||
struct BatchSampleResult {
|
||
bool ok = true; ///< 样本是否成功
|
||
std::string error; ///< 失败原因(ok=false 时)
|
||
std::map<std::string, double> outputs; ///< 用户输出指标/结果(可为空)
|
||
};
|
||
|
||
/// 批量回调:输入一个设计点,返回该样本结果;异常会被捕获并计为失败
|
||
using BatchEvaluate = std::function<BatchSampleResult(const DesignPoint&)>;
|
||
|
||
/// 批量执行结果
|
||
struct BatchRunResult {
|
||
std::size_t requested = 0; ///< 采样得到的样本总数
|
||
std::size_t completed = 0; ///< 实际执行的样本数(取消时可能少于 requested)
|
||
std::size_t succeeded = 0; ///< ok=true 的样本数
|
||
std::size_t failed = 0; ///< ok=false/异常/回调缺失的样本数
|
||
bool cancelled = false; ///< 是否因 SweepOptions.cancel 提前停止
|
||
std::string setup_error; ///< 非空 = 域/选项错误,未执行任何样本
|
||
std::vector<BatchSampleResult> results; ///< 与已执行样本一一对应(按样本顺序)
|
||
|
||
[[nodiscard]] bool ok() const {
|
||
return setup_error.empty() && !cancelled && failed == 0;
|
||
}
|
||
};
|
||
|
||
/**
|
||
* @brief 生成 DOE 样本(Grid 或 LHS)
|
||
* @param variables 设计变量(非空、名称唯一、边界有限)
|
||
* @param options 采样方式/样本数/种子/上限
|
||
* @return 采样结果;参数错误时 error 非空、points 为空
|
||
*/
|
||
[[nodiscard]] SampleSet generate_samples(const std::vector<DesignVariable>& variables,
|
||
const SweepOptions& options = {});
|
||
|
||
/**
|
||
* @brief 对 DOE 样本批量调用回调并收集结果
|
||
*
|
||
* 回调按 generate_samples 的确定顺序串行执行;单个样本失败(ok=false 或抛异常)
|
||
* 不影响后续样本,最终通过 succeeded/failed/results 汇总。不依赖 UI/线程池。
|
||
*
|
||
* @param variables 设计变量
|
||
* @param options 采样/取消选项
|
||
* @param evaluate 每个样本的回调(空回调视为参数错误)
|
||
* @return 批量结果;域错误写 setup_error
|
||
*/
|
||
[[nodiscard]] BatchRunResult run_batch(const std::vector<DesignVariable>& variables,
|
||
const SweepOptions& options,
|
||
const BatchEvaluate& evaluate);
|
||
|
||
} // namespace vde::param
|