Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.9K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Use compiled function in FindMinimum/NMinimize (recurring issue...)?

POSTED BY: Eric Michielssen
6 Replies

why isn't xtable the first argument of ff and cff in FindMinimum:

In[1]:= xmax = 2.;

In[2]:= n = 10; nk = 15;

In[4]:= ktable = Table[Pi (i - 1)/nk; 2 Pi Cos[phi] // N, {i, 1, nk}];

In[9]:= xtable := RandomReal[{-xmax, xmax}, {n}];

In[6]:= ff[xtable_, ktable_, nk_, n_] := 
  Module[{is, ik}, 
   Min[Abs[Table[
      Sum[Exp[I ktable[[ik]] xtable[[is]]], {is, 1, n}], {ik, 1, 
       nk}]]]];

In[7]:= cff = 
  Compile[{{xtable, _Real, 1}, {ktable, _Real, 
     1}, {nk, _Integer}, {n, _Integer}}, 
   Module[{is, ik}, 
    Min[Abs[Table[
       Sum[Exp[I ktable[[ik]] xtable[[is]]], {is, 1, n}], {ik, 1, 
        nk}]]]], 
   "RuntimeOptions" -> {"EvaluateSymbolically" -> False}, 
   CompilationTarget -> "C"];

In[10]:= FindMinimum[{ff[xtable, ktable, nk, n]}, varsp, 
 Compiled -> True]

During evaluation of In[10]:= FindMinimum::fmgz: Encountered a gradient that is effectively zero. The result returned may not be a minimum; it may be a maximum or a saddle point.

Out[10]= {1.74504, {varsp -> 1.}}

In[11]:= FindMinimum[{cff[xtable, ktable, nk, n]}, varsp, 
 Compiled -> True]

During evaluation of In[11]:= FindMinimum::fmgz: Encountered a gradient that is effectively zero. The result returned may not be a minimum; it may be a maximum or a saddle point.

Out[11]= {1.15655, {varsp -> 1.}}
POSTED BY: Frank Kampas

Since xtable[] is defined with delayed evaluation, it will give a different result every time it is called. Is that your intention?

POSTED BY: Frank Kampas

yes, that was the intention, as it allowed me to test cff in a loop for many different arguments. The issue remains that FindMinimum and NMinimize appear to evaluate the objective function symbolically as implied by the error message

During evaluation of In[1157]:= CompiledFunction::cfta: Argument {x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9],x[10]} at position 1 should be a rank 1 tensor of machine-size real numbers.

POSTED BY: Eric Michielssen

It may not work for all the different values passed to it by FindMinimum

POSTED BY: Frank Kampas

the error messages are saying that your compiled function is not working correctly.

POSTED BY: Frank Kampas

The compiled function works when not called from FindMinimum

In[1166]:= cff[xtable[], ktable, nk, n]

Out[1166]= 1.30685

Similar issues were discussed in: - for NIntegrate: https://mathematica.stackexchange.com/questions/17993/using-a-compiled-function-inside-nintegrate-gives-compiledfunctioncfsa-messa

-For FindMinimum: https://mathematica.stackexchange.com/questions/98449/findminimum-with-compiled-function

Unfortunately I cannot seem to duplicate these successes.

POSTED BY: Eric Michielssen
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard