Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.2K Views
|
8 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Use NMinimize calling an own defined function?

Posted 7 years ago

I want to call an own defined function through NMinimize. For the sake of simplicity, let us define the problem as follow:

radpatt[x1_] := (Print[x1]; x1 )

NMinimize[{radpatt[xx], 0.1 <= xx <= 1.1}, {xx}]

If you run the above-listed instructions, you will notice that x1 is not number as soon as the function radpatt is called by NMinimize, but it is equal to xx. This causes me an issue because in my original problem x1 needs to be a number just at the beginning of my own function. Any ideas?

Many many thanks in advance.

8 Replies
Posted 1 year ago

The name of the global context is "Global`". Context names always end in a backtick. The * is a "wildcard" that matches any characters in a symbol name. It is similar to the Unix shell wildcard * — and wildcards on other systems, I'm sure, but I learned computers on a multi-user Unix system before PCs were available. It is explained in the details section near the top of the documentation for Remove. Thus "Global`*" matches all symbol names in the global context. And "Global`q*" would match only symbol names that begin with q.

I use Quit[], which quits and restarts the kernel, instead of Remove["Global`*"], with an empty kernel init.m. Quit[] is usually slower (a few seconds) than Remove[], I think. How slow seems to change from version to version. Restarting the kernel seems closest to what you get when you run a compiled program (just system definitions, no prior user-defined values). As an interpreted environment with persistent definitions from one shift-enter to another (and a shared kernel for all notebooks), it's never exactly the same, though.

There is also the CleanSlate package, which some people use to do a bit more cleanup than Remove["Global`*"].

So much for dealing with clean runs of programs. As for the complexity of the programming environment, as I said in my first post, I don't have a quick answer.

POSTED BY: Updating Name

Thanks a lot! (But I am sorry to say I cannot understand what "context" really means, and I suppose I shall sometimes crash even after understanding it. Sweat ^^;)

POSTED BY: Unknown Unknown

Hi,

Numerical solvers in Mathematica usually analyze the symbolic form of the objective function. To do this analysis, NMinimize[] evaluates radpatt[xx] (in the OP at the top). This analysis may be used for method selection, singularity handling, and perhaps other adjustments. Mathematica is designed to do this automatically and to allow for users to override its automatic choices. It uses heuristics that are not always perfect. Symbolic processing takes time. So user-overrides sometimes are needed and can speed things up when the user knows what they are doing.

Using _?NumericQ in defining the objective function prevents symbolic analysis, and the automatic choice of method is more likely to be less than optimal. Symbolic analysis mainly done on formulas, as far as I know. So if your objective functions calls a numerical routine, such as NIntegrate[] or FindRoot[], then you do not lose much by using _?NumericQ. And if you need to specify singularities or method, you can do that through options. (One irritating-to-me exception: Plot[f[x], {x, 0, 1}, WorkingPrecision -> 32] evaluates f[x] on a machine-precision number at the beginning, and sometimes this causes problems.)

Below are variations on an example. The method, speed, and quality of the result are altered by using _?NumericQ in obj[]. Also the last variant shows that the superior method may not be used on obj[].

(* Uses Couenne NLP *)
NMinimize[{Cos[x] - Exp[(x - 0.5)  y], x^2 + y^2 < 1}, {x, y}] // AbsoluteTiming
(*  {0.03809, {-1.60047, {x -> -0.657103, y -> -0.753805}}}  *)

(* Ends up using diff. evol. after rejecting Couenne *)
obj // ClearAll; (* I always start function definitions this way (or ClearAll[obj] *)
obj[x_?NumericQ, y_?NumericQ] := Cos[x] - Exp[(x - 0.5)  y];
NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}] // AbsoluteTiming
(*  {0.154981, {-1.60046, {x -> -0.657221, y -> -0.753698}}}  *)

NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}, Method -> "Couenne"]
(*  NMinimize::unproc: Could not process the objective and constraints in a form suitable for Couenne.  *)
(*  NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}, Method -> "Couenne"]  *)

If you want to know how I know what is going, here are some undocumented ways to peak under the hood of NMinimize/NMaximize:

Optimization`Debug`SetPrintLevel[2]; 
Block[{Optimization`NMinimizeDump`$DiagnosticLevel = 2},
 (* Uses Couenne NLP *)
 NMinimize[{Cos[x] - Exp[(x - 0.5)  y], x^2 + y^2 < 1}, {x, y}]
 ]
Optimization`Debug`SetPrintLevel[0];

Optimization`Debug`SetPrintLevel[2];
Block[{Optimization`NMinimizeDump`$DiagnosticLevel = 2},
 (* Ends up using diff. evol. after rejecting Couenne *)
 NMinimize[{obj[x, y], x^2 + y^2 < 1}, {x, y}]
 ]
Optimization`Debug`SetPrintLevel[0];
POSTED BY: Michael Rogers

For protecting an objective which is a function of a list of variables use:

obj[vars_?(AllTrue[#, NumberQ] &)]

as suggested to me by Wolfram support

POSTED BY: Frank Kampas

A user-supplied objective function for NMinimize or FindMinimum needs to be "protected" from premature evaluation when given symbolic input. Therefore, use a definition like this

radpatt[x1_?NumberQ] := (Print[x1]; x1 )
POSTED BY: Robert Nachbar

I had the same problem as the original question posted above, so thank you for your answer. However, I would appreciate it if you could answer my question as to why Mathematica does not provide an answer to such a frequently encountered problem. I also wonder if there are too many random, no-regularity, cop-out patch options like "?NumberQ". Since the contents of Mathametica are a black box to users, I would like to know about the critical language design policies and syntax of this useful software.

POSTED BY: Unknown Unknown
Posted 1 year ago
POSTED BY: Updating Name
POSTED BY: Unknown Unknown
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard