parameter-optimization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseParameter Optimization
参数优化
Goal
目标
Provide a workflow to design experiments, rank parameter influence, and select optimization strategies for materials simulation calibration.
提供一个用于材料模拟校准的实验设计、参数影响排序及优化策略选择的工作流。
Requirements
要求
- Python 3.10+
- No external dependencies (uses Python standard library only)
- Python 3.10+
- 无外部依赖(仅使用Python标准库)
Inputs to Gather
需要收集的输入信息
Before running any scripts, collect from the user:
| Input | Description | Example |
|---|---|---|
| Parameter bounds | Min/max for each parameter with units | |
| Evaluation budget | Max number of simulations allowed | |
| Noise level | Stochasticity of simulation outputs | |
| Constraints | Feasibility rules or forbidden regions | |
运行任何脚本前,需向用户收集以下信息:
| 输入项 | 描述 | 示例 |
|---|---|---|
| 参数范围 | 每个参数的最小值/最大值及单位 | |
| 评估预算 | 允许的最大模拟次数 | |
| 噪声水平 | 模拟输出的随机性 | |
| 约束条件 | 可行性规则或禁用区域 | |
Decision Guidance
决策指南
Choosing a DOE Method
选择DOE方法
Is dimension <= 3 AND full coverage needed?
├── YES → Use factorial
└── NO → Is sensitivity analysis the goal?
├── YES → Use quasi-random (preferred; "sobol" is accepted but deprecated)
└── NO → Use lhs (Latin Hypercube)| Method | Best For | Avoid When |
|---|---|---|
| General exploration, moderate dimensions (3-20) | Need exact grid coverage |
| Sensitivity analysis, uniform coverage (preferred) | Very high dimensions (>20) |
| Deprecated alias of | New code (use |
| Low dimension (<4), need all corners | High dimension (exponential growth) |
Factorial sizing: the factorial grid isevenly spaced values per parameter, producing exactlylevelssamples. Set the resolution explicitly withlevels ** params(e.g.--levels-> 16 samples). If you use--params 2 --levels 4instead, the script back-computes--budgetand warns whenever the realized sample count differs from the requested budget (e.g.levels = round(budget ** (1/params))realizes 16 samples). For an exact design, pass a perfect power (--budget 20 --params 2) or, preferably,--budget 16.--levels
维度 <= 3 且需要全覆盖?
├── 是 → 使用析因法(factorial)
└── 否 → 目标是敏感性分析?
├── 是 → 使用准随机法(quasi-random,为首选;"sobol"为兼容写法但已弃用)
└── 否 → 使用拉丁超立方抽样(lhs)| 方法 | 适用场景 | 避免场景 |
|---|---|---|
| 通用探索、中等维度(3-20) | 需要精确网格覆盖 |
| 敏感性分析、均匀覆盖(首选) | 极高维度(>20) |
| | 新代码(请使用 |
| 低维度(<4)、需要覆盖所有边界 | 高维度(样本量呈指数增长) |
析因法样本量说明:析因网格为每个参数设置个均匀间隔值,生成的样本量恰好为levels。可通过levels ** params显式设置分辨率(例如--levels→ 16个样本)。若使用--params 2 --levels 4替代,脚本会反向计算--budget,且当实际样本数与请求的预算不符时会发出警告(例如levels = round(budget ** (1/params))实际生成16个样本)。如需精确设计,请传入完全幂数的预算(如--budget 20 --params 2),或优先使用--budget 16。--levels
Choosing an Optimizer
选择优化器
Is dimension <= 10 AND budget <= 100?
├── YES → Bayesian Optimization
└── NO → Is dimension <= 20?
├── YES → CMA-ES
└── NO → Random Search with screening| Noise Level | Recommendation |
|---|---|
| Low | Gradient-based if derivatives available, else Bayesian Optimization |
| Medium | Bayesian Optimization with noise model |
| High | Evolutionary algorithms or robust Bayesian Optimization |
维度 <= 10 且预算 <= 100?
├── 是 → 贝叶斯优化(Bayesian Optimization)
└── 否 → 维度 <= 20?
├── 是 → CMA-ES
└── 否 → 带筛选的随机搜索| 噪声水平 | 推荐方案 |
|---|---|
| Low | 若可获取导数则使用基于梯度的方法,否则使用贝叶斯优化 |
| Medium | 带噪声模型的贝叶斯优化 |
| High | 进化算法或鲁棒贝叶斯优化 |
Script Outputs (JSON Fields)
脚本输出(JSON字段)
| Script | Output Fields |
|---|---|
| |
| |
| |
| |
| 脚本 | 输出字段 |
|---|---|
| |
| |
| |
| |
Workflow
工作流
- Generate DOE with
scripts/doe_generator.py - Run simulations at DOE sample points (user's responsibility)
- Summarize sensitivity with
scripts/sensitivity_summary.py - Choose optimizer using
scripts/optimizer_selector.py - (Optional) Fit surrogate with
scripts/surrogate_builder.py
- 使用生成DOE抽样方案
scripts/doe_generator.py - 在DOE抽样点运行模拟(由用户负责)
- 使用汇总敏感性分析结果
scripts/sensitivity_summary.py - 使用选择优化器
scripts/optimizer_selector.py - (可选) 使用拟合代理模型
scripts/surrogate_builder.py
CLI Examples
CLI示例
bash
undefinedbash
undefinedGenerate 20 LHS samples for 3 parameters
为3个参数生成20个LHS样本
python3 scripts/doe_generator.py --params 3 --budget 20 --method lhs --json
python3 scripts/doe_generator.py --params 3 --budget 20 --method lhs --json
Full factorial with 4 levels per parameter (2 params -> 16 samples)
每个参数取4个水平的全析因设计(2个参数 → 16个样本)
python3 scripts/doe_generator.py --params 2 --levels 4 --method factorial --json
python3 scripts/doe_generator.py --params 2 --levels 4 --method factorial --json
Rank parameters by sensitivity scores
根据敏感性分数对参数排序
python3 scripts/sensitivity_summary.py --scores 0.2,0.5,0.3 --names kappa,mobility,W --json
python3 scripts/sensitivity_summary.py --scores 0.2,0.5,0.3 --names kappa,mobility,W --json
Get optimizer recommendation for 3D problem with 50 eval budget
获取3维问题、50次评估预算下的优化器推荐
python3 scripts/optimizer_selector.py --dim 3 --budget 50 --noise low --json
python3 scripts/optimizer_selector.py --dim 3 --budget 50 --noise low --json
Build surrogate model from simulation data
根据模拟数据构建代理模型
python3 scripts/surrogate_builder.py --x 0,1,2 --y 10,12,15 --model rbf --json
undefinedpython3 scripts/surrogate_builder.py --x 0,1,2 --y 10,12,15 --model rbf --json
undefinedConversational Workflow Example
对话式工作流示例
User: I need to calibrate thermal conductivity and diffusivity for my FEM simulation. I can run about 30 simulations.
Agent workflow:
- Identify 2 parameters →
--params 2 - Budget is 30 →
--budget 30 - Use LHS for general exploration:
bash
python3 scripts/doe_generator.py --params 2 --budget 30 --method lhs --json - After user runs simulations and provides outputs, summarize sensitivity:
bash
python3 scripts/sensitivity_summary.py --scores 0.7,0.3 --names conductivity,diffusivity --json - Recommend optimizer:
bash
python3 scripts/optimizer_selector.py --dim 2 --budget 30 --noise low --json
用户:我需要为我的有限元模拟校准热导率和扩散率,我可以运行大约30次模拟。
Agent工作流:
- 识别出2个参数 →
--params 2 - 预算为30 →
--budget 30 - 使用LHS进行通用探索:
bash
python3 scripts/doe_generator.py --params 2 --budget 30 --method lhs --json - 用户运行模拟并提供输出后,汇总敏感性分析结果:
bash
python3 scripts/sensitivity_summary.py --scores 0.7,0.3 --names conductivity,diffusivity --json - 推荐优化器:
bash
python3 scripts/optimizer_selector.py --dim 2 --budget 30 --noise low --json
Error Handling
错误处理
| Error | Cause | Resolution |
|---|---|---|
| Zero or negative dimension | Ask user for valid parameter count |
| Zero or negative budget | Ask user for realistic simulation budget |
| Invalid method (argparse) | Use decision guidance to pick a valid method |
| Non-numeric value in | Reformat as |
| Empty | Provide at least one numeric score |
| 错误信息 | 原因 | 解决方法 |
|---|---|---|
| 参数维度为0或负数 | 请用户提供有效的参数数量 |
| 预算为0或负数 | 请用户提供合理的模拟预算 |
| 方法无效(argparse校验) | 根据决策指南选择有效的方法 |
| | 格式化为 |
| | 提供至少一个数值分数 |
Verification checklist
验证检查清单
- Recorded the exact
doe_generator.pyand confirmed it matches the intended design — forcoverage.count, verifiedfactorialand that nocount == levels ** params/notemismatch warning was emitted (or that the realized count is acceptable).requested_budget - Confirmed the chosen matches the Decision Guidance for the actual dimension/goal, and that
--methodwas used instead of the deprecatedquasi-randomalias (nosobolin output).DeprecationWarning - Recorded the
optimizer_selector.pystrategy andrecommended, and verifiedexpected_evalsso the plan is feasible within the stated evaluation budget.expected_evals <= budget - Logged the
sensitivity_summary.pyand checked whether the top sensitivity isranking(the "All sensitivities are low" note); if so, did not over-interpret the ranking and revisited the output metric.< 0.1 - For surrogate fits, judged quality with (leave-one-out), NOT in-sample
metrics.cv_error— especially formse, whererbfis near zero by construction — and comparedmseagainstcv_errorto confirm the surrogate beats the constant-mean baseline.metrics.output_variance - Confirmed any reported is a finite number (not
cv_error), i.e. there were enough samples for leave-one-out (NaN:poly;n > degree+1:rbf).n >= 3
- 记录输出的
doe_generator.py并确认其与预期设计匹配——对于析因法,需验证coverage.count且未触发count == levels ** params/预算不匹配警告(或确认实际样本数可接受)。note - 确认所选符合实际维度/目标的决策指南,且使用了
--method而非已弃用的quasi-random别名(输出中无sobol)。DeprecationWarning - 记录输出的
optimizer_selector.py策略和recommended,并验证expected_evals,确保方案在指定评估预算内可行。expected_evals <= budget - 记录输出的
sensitivity_summary.py,并检查最高敏感性分数是否ranking(对应“所有敏感性分数均较低”的提示);若是,则不要过度解读排序结果,需重新审视输出指标。< 0.1 - 对于代理模型拟合,使用(留一法)判断模型质量,而非样本内
metrics.cv_error——尤其是mse模型,其样本内rbf本质上接近零——并将mse与cv_error对比,确认代理模型优于常数均值基线。metrics.output_variance - 确认所有报告的为有限数值(非
cv_error),即有足够样本进行留一法评估(NaN模型:poly;n > degree+1模型:rbf)。n >= 3
Common pitfalls & rationalizations
常见误区及说明
| Tempting shortcut | Why it's wrong / what to do |
|---|---|
"RBF surrogate | RBF is an exact interpolant — in-sample |
"I asked for | Factorial honors |
" | |
| "The optimizer recommendation is just advice — budget doesn't matter." | The recommendation is gated on dimension AND budget (BO only for |
| "One sensitivity score is highest, so that parameter dominates." | The script only sorts the scores you pass in; it computes no sensitivity itself. If the top score is |
| "It printed JSON without erroring, so the result is valid." | Exit success only means inputs parsed. Verify the design size, |
| 诱人的捷径 | 错误原因/正确做法 |
|---|---|
“RBF代理模型的 | RBF是精确插值器——样本内 |
“我要求 | 析因设计遵循 |
“ | |
| “优化器推荐只是建议——预算无关紧要。” | 推荐方案受维度和预算双重限制(仅当 |
| “某个敏感性分数最高,所以该参数起主导作用。” | 脚本仅对传入的分数进行排序;本身不计算敏感性。若最高分数 |
| “输出了JSON且未报错,所以结果有效。” | 执行成功仅表示输入解析正确。在信任任何输出前,需验证设计规模、 |
Security
安全性
Input Validation
输入验证
- validates
sensitivity_summary.pyagainst--nameswith a 200-char limit, preventing shell metacharacter injection via crafted parameter names[a-zA-Z_][a-zA-Z0-9_ .-]* - All numeric list inputs are validated as finite numbers (/
NaNrejected)Inf - Comma-separated value lists are capped (10,000 for scores, 100,000 for surrogate data) to prevent resource exhaustion
- caps dimension at 1,000 and budget at 1,000,000;
doe_generator.pycaps dimension at 100,000 and budget at 10,000,000optimizer_selector.py - is validated against a fixed allowlist (
--method,lhs/quasi-random,sobol);factorialis an accepted but deprecated alias ofsobolquasi-random - is validated against a fixed allowlist (
--noise,low,medium)high - (surrogate type) is validated against a fixed allowlist (
--model,rbf)poly - (factorial grid resolution) is validated as an integer in
--levels[2, 1000]
- 会校验
sensitivity_summary.py是否符合--names格式且长度不超过200字符,防止通过构造参数名称注入shell元字符[a-zA-Z_][a-zA-Z0-9_ .-]* - 所有数值列表输入均会被校验为有限数值(拒绝/
NaN)Inf - 逗号分隔的值列表会被限制数量(分数最多10000个,代理模型数据最多100000个),防止资源耗尽
- 限制维度最大为1000,预算最大为1000000;
doe_generator.py限制维度最大为100000,预算最大为10000000optimizer_selector.py - 会被校验为固定允许列表(
--method,lhs/quasi-random,sobol);factorial是sobol的兼容但已弃用别名quasi-random - 会被校验为固定允许列表(
--noise,low,medium)high - (代理模型类型)会被校验为固定允许列表(
--model,rbf)poly - (析因网格分辨率)会被校验为
--levels范围内的整数[2, 1000]
File Access
文件访问
- Scripts read no external files; all inputs are provided via CLI arguments
- Scripts write only to stdout (JSON output); no files are created unless the agent explicitly uses the Write tool
- 脚本不读取任何外部文件;所有输入均通过CLI参数提供
- 脚本仅向stdout写入内容(JSON输出);除非Agent明确使用Write工具,否则不会创建文件
Tool Restrictions
工具限制
- Read: Used to inspect script source, references, and user data files
- Write: Used to save DOE sample plans, sensitivity rankings, or optimizer recommendations; writes are scoped to the user's working directory
- Grep/Glob: Used to locate relevant files and search references
- The skill's excludes
allowed-toolsto prevent the agent from executing arbitrary commands when processing user-provided parameter names and constraintsBash
- 读取:用于检查脚本源码、参考资料和用户数据文件
- 写入:用于保存DOE抽样方案、敏感性排序或优化器推荐;写入范围限定在用户工作目录内
- Grep/Glob:用于定位相关文件和搜索参考资料
- 本技能的排除了
allowed-tools,防止Agent处理用户提供的参数名称和约束时执行任意命令Bash
Safety Measures
安全措施
- No ,
eval(), or dynamic code generationexec() - All subprocess calls use explicit argument lists (no )
shell=True - Reduced tool surface (no Bash) limits the agent to read/write operations only
- Parameter names are sanitized before use, preventing injection via crafted identifiers
- 不使用、
eval()或动态代码生成exec() - 所有子进程调用均使用显式参数列表(不使用)
shell=True - 缩减工具范围(无Bash),将Agent限制为仅读写操作
- 参数名称会被清理后使用,防止通过构造标识符注入恶意内容
Limitations
局限性
- Not for real-time optimization: Scripts provide recommendations, not live optimization loops
- Surrogate is lightweight: fits a real 1-D least-squares polynomial (
surrogate_builder.py) or Gaussian RBF interpolant (poly) using only the standard library and reports honest residualrbf, leave-one-outmse, and the datacv_error; for production use scipy/scikit-learn/GPyTorch. Foroutput_variance, in-samplerbfis near zero by construction (exact interpolation) — judge fit quality withmsecv_error - No automatic simulation execution: User must run simulations externally and provide results
- 不支持实时优化:脚本仅提供推荐,不支持实时优化循环
- 代理模型轻量化:仅使用标准库拟合一维最小二乘多项式(
surrogate_builder.py)或高斯RBF插值器(poly),并如实报告残差rbf、留一法mse和数据cv_error;生产环境请使用scipy/scikit-learn/GPyTorch。对于output_variance模型,样本内rbf本质上接近零(精确插值)——需使用mse判断拟合质量cv_error - 不自动执行模拟:用户需在外部运行模拟并提供结果
References
参考资料
- - Detailed DOE method comparison
references/doe_methods.md - - Optimizer algorithm details
references/optimizer_selection.md - - Sensitivity analysis interpretation
references/sensitivity_guidelines.md - - Surrogate model selection
references/surrogate_guidelines.md
- - DOE方法详细对比
references/doe_methods.md - - 优化器算法细节
references/optimizer_selection.md - - 敏感性分析解读指南
references/sensitivity_guidelines.md - - 代理模型选择指南
references/surrogate_guidelines.md
Version History
版本历史
- v1.2.2 (2026-06-24): Added Verification checklist and Common pitfalls & rationalizations sections to drive evidence-based use of the DOE, optimizer, sensitivity, and surrogate scripts
- v1.2.0 (2026-06-23): Real surrogate fits (least-squares,
polyinterpolation) with honestrbf/mse/cv_error; explicit factorialoutput_variancewith budget-mismatch warnings; BO dimension cutoff harmonized to dim<=10; corrected Security/Error-Handling/output-field docs to match script behavior--levels - v1.1.0 (2024-12-24): Enhanced documentation, decision guidance, conversational examples
- v1.0.0: Initial release with core scripts
- v1.2.2(2026-06-24):新增验证检查清单和常见误区及说明章节,指导基于证据使用DOE、优化器、敏感性分析和代理模型脚本
- v1.2.0(2026-06-23):实现真实代理模型拟合(最小二乘、
poly插值),提供真实的rbf/mse/cv_error;析因法新增显式output_variance参数及预算不匹配警告;贝叶斯优化的维度限制统一为dim<=10;修正安全性/错误处理/输出字段文档以匹配脚本行为--levels - v1.1.0(2024-12-24):增强文档、决策指南和对话示例
- v1.0.0:初始版本,包含核心脚本