Mathematica: evaluation procedure for numerical optimization of black box functions

I am trying to perform numerical optimization of the black box function in Mathematica. Schematically, it looks like this:

NMinimize[{comb[x,y,z], x > 0}, {x,y,z}] 

where the crest [x, y, z] is defined similarly to this:

 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]; ]; 

However, all the minimization functions that I tried, apparently, do not immediately give a comb [x, y, z] with numerical values, and it tries to evaluate FindMaximum with symbolic values ​​for x, y, z (which is easily verified, since Print [x, y, z] is also symbolically evaluated). Thus, Findmaximum fails (FindMaximum :: nrnum: the value of the blah blah function is not a real number), and therefore minimization is not performed.

How to fix the evaluation procedure so that comb sub-functions are evaluated with numerical values?

+6
source share
2 answers

The evaluation procedure for FindMinimum , FindMaximum , FindRoot and FindFit described in the tutorial / UnconstrainedOptimizationSymbolicEvaluation Documentation page. I think something very similar applies to the NMinimize function. The description is quite long, so I will provide here only the proposed solution from this page:

If your function is such that the symbolic evaluation will not be function as intended, or will be excessively slow, you must define your function so that it evaluates only the numerical values ​​of the variables. The easiest way to do this is to define your function using PatternTest (?), As in f [x_? NumberQ]: = definition.

It may seem that symbolic appreciation simply creates concern, as you must define the function specifically to prevent it. However, without a symbolic appreciation, Mathematica is difficult to take advantage of its unique combination of numerical and symbolic strength. Symbolic evaluation means that teams can consistently take advantage of the benefits of symbolic analysis, such as defining an algorithm, automatically calculating derivatives, automatic optimization and compilation, and structural analysis.

+5
source

How to change 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 forces the definition of comb be evaluated only if its arguments are numbers?

+4
source

Source: https://habr.com/ru/post/894596/


All Articles