我正在尝试对 Mathematica 中的“黑盒”函数进行数值优化。原理图是这样的:
NMinimize[{comb[x,y,z], x > 0}, {x,y,z}]
其中 comb[x,y,z] 的定义与此类似:
comb[x_,y_,z_] := Module[{},
Print[x,y,z];
M = FindMaximum[SkewNormal[a,x,y,z], {a,x}] // First;
val = f[x,y,z,M];
Return[val];
];
然而,我尝试过的所有最小化函数似乎都没有立即为 comb[x,y,z] 提供数值,它最终试图用 x,y,z 的符号值来评估 FindMaximum(这很容易验证因为 Print[x,y,z] 也象征性地计算)。 Findmaximum 因此失败(FindMaximum::nrnum:函数值等等不是实数),因此最小化失败。
如何修复评估顺序,以便用数值评估 comb 的子功能?
最佳答案
FindMinimum
的评估顺序, FindMaximum
, FindRoot
和 FindFit
记录在 tutorial/UnconstrainedOptimizationSymbolicEvaluation 上文档页面。我认为非常相似的东西适用于 NMinimize
功能。描述很长,所以我将在此处仅引用该页面中提出的解决方案:
If your function is such that symbolic evaluation will not keep the function as intended or will be prohibitively slow, you should define your function so that it only evaluates for numerical values of the variables. The simplest way to do this is by defining your function using PatternTest (?), as in f[x_?NumberQ]:=definition.
It may seem that symbolic evaluation just creates a bother since you have to define the function specifically to prevent it. However, without symbolic evaluation, it is hard for Mathematica to take advantage of its unique combination of numerical and symbolic power. Symbolic evaluation means that the commands can consistently take advantage of benefits that come from symbolic analysis, such as algorithm determination, automatic computation of derivatives, automatic optimization and compilation, and structural analysis.
关于optimization - 数学 : Evaluation order during numerical optimisation of black box functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6990285/