Mathematica: Evaluation order during numerical optimisation of black box functions

给你一囗甜甜゛ 提交于 2019-12-05 07:22:35

Evaluation order for FindMinimum, FindMaximum, FindRoot and FindFit is documented on the tutorial/UnconstrainedOptimizationSymbolicEvaluation Documentation page. I think that something very similar is applicable to the NMinimize function. The description is quite lengthy so I will cite here only the proposed solution from that page:

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.

How about changing comb to

comb[x_?NumericQ, y_?NumericQ, z_?NumericQ] := 
 Module[{}, Print[x, y, z];
 M = FindMaximum[SkewNormal[a, x, y, z], {a, x}] // First;
 val = f[x, y, z, M];
 Return[val];];

which causes the definition of comb to be evaluated only if its arguments are numbers?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!